1 /*
2 * Copyright (C) 2022-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "dh_openssl.h"
17 #include "dh_openssl_common.h"
18
19 #include <openssl/bio.h>
20 #include <openssl/err.h>
21
22 #include "algorithm_parameter.h"
23 #include "openssl_adapter.h"
24 #include "openssl_class.h"
25 #include "openssl_common.h"
26 #include "log.h"
27 #include "memory.h"
28 #include "utils.h"
29
30 typedef struct {
31 HcfKeyAgreementSpi base;
32 } HcfKeyAgreementSpiDhOpensslImpl;
33
GetDhClass(void)34 static const char *GetDhClass(void)
35 {
36 return "HcfKeyAgreement.HcfKeyAgreementSpiDhOpensslImpl";
37 }
38
DestroyDh(HcfObjectBase * self)39 static void DestroyDh(HcfObjectBase *self)
40 {
41 if (self == NULL) {
42 LOGE("Invalid input parameter.");
43 return;
44 }
45 if (!HcfIsClassMatch(self, GetDhClass())) {
46 LOGE("Invalid class of self.");
47 return;
48 }
49 HcfFree(self);
50 }
51
EngineGenerateSecret(HcfKeyAgreementSpi * self,HcfPriKey * priKey,HcfPubKey * pubKey,HcfBlob * returnSecret)52 static HcfResult EngineGenerateSecret(HcfKeyAgreementSpi *self, HcfPriKey *priKey,
53 HcfPubKey *pubKey, HcfBlob *returnSecret)
54 {
55 if ((self == NULL) || (priKey == NULL) || (pubKey == NULL) || (returnSecret == NULL)) {
56 LOGE("Invalid input parameter.");
57 return HCF_INVALID_PARAMS;
58 }
59 if ((!HcfIsClassMatch((HcfObjectBase *)self, GetDhClass())) ||
60 (!HcfIsClassMatch((HcfObjectBase *)priKey, OPENSSL_DH_PRIKEY_CLASS)) ||
61 (!HcfIsClassMatch((HcfObjectBase *)pubKey, OPENSSL_DH_PUBKEY_CLASS))) {
62 LOGE("Invalid class of self.");
63 return HCF_INVALID_PARAMS;
64 }
65 EVP_PKEY *pubPKey = NewEvpPkeyByDh(((HcfOpensslDhPubKey *)pubKey)->pk, true);
66 if (pubPKey == NULL) {
67 LOGE("Failed to get public pkey.");
68 return HCF_ERR_CRYPTO_OPERATION;
69 }
70 EVP_PKEY *priPKey = NewEvpPkeyByDh(((HcfOpensslDhPriKey *)priKey)->sk, true);
71 if (priPKey == NULL) {
72 LOGE("Failed to get private pkey.");
73 OpensslEvpPkeyFree(pubPKey);
74 return HCF_ERR_CRYPTO_OPERATION;
75 }
76 HcfResult res = KeyDerive(priPKey, pubPKey, returnSecret);
77 OpensslEvpPkeyFree(priPKey);
78 OpensslEvpPkeyFree(pubPKey);
79 return res;
80 }
81
HcfKeyAgreementSpiDhCreate(HcfKeyAgreementParams * params,HcfKeyAgreementSpi ** returnObj)82 HcfResult HcfKeyAgreementSpiDhCreate(HcfKeyAgreementParams *params, HcfKeyAgreementSpi **returnObj)
83 {
84 (void)params;
85 if ((params == NULL) || (returnObj == NULL)) {
86 LOGE("Invalid input parameter.");
87 return HCF_INVALID_PARAMS;
88 }
89
90 HcfKeyAgreementSpiDhOpensslImpl *returnImpl = (HcfKeyAgreementSpiDhOpensslImpl *)HcfMalloc(
91 sizeof(HcfKeyAgreementSpiDhOpensslImpl), 0);
92 if (returnImpl == NULL) {
93 LOGE("Failed to allocate returnImpl memroy!");
94 return HCF_ERR_MALLOC;
95 }
96 returnImpl->base.base.getClass = GetDhClass;
97 returnImpl->base.base.destroy = DestroyDh;
98 returnImpl->base.engineGenerateSecret = EngineGenerateSecret;
99
100 *returnObj = (HcfKeyAgreementSpi *)returnImpl;
101 return HCF_SUCCESS;
102 }
103
104