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 "ecdh_openssl.h"
17 
18 #include <openssl/bio.h>
19 #include <openssl/err.h>
20 
21 #include "algorithm_parameter.h"
22 #include "openssl_adapter.h"
23 #include "openssl_class.h"
24 #include "openssl_common.h"
25 #include "log.h"
26 #include "memory.h"
27 #include "utils.h"
28 
29 typedef struct {
30     HcfKeyAgreementSpi base;
31 } HcfKeyAgreementSpiEcdhOpensslImpl;
32 
AssignEcKeyToPkey(EC_KEY * ecKey)33 static EVP_PKEY *AssignEcKeyToPkey(EC_KEY *ecKey)
34 {
35     EVP_PKEY *pKey = OpensslEvpPkeyNew();
36     if (pKey == NULL) {
37         HcfPrintOpensslError();
38         return NULL;
39     }
40     if (OpensslEvpPkeyAssignEcKey(pKey, ecKey) != HCF_OPENSSL_SUCCESS) {
41         HcfPrintOpensslError();
42         OpensslEvpPkeyFree(pKey);
43         return NULL;
44     }
45     return pKey;
46 }
47 
NewPKeyByEccPubKey(HcfOpensslEccPubKey * publicKey)48 static EVP_PKEY *NewPKeyByEccPubKey(HcfOpensslEccPubKey *publicKey)
49 {
50     EC_KEY *ecKey = OpensslEcKeyDup(publicKey->ecKey);
51     if (ecKey == NULL) {
52         return NULL;
53     }
54     EVP_PKEY *res = AssignEcKeyToPkey(ecKey);
55     if (res == NULL) {
56         OpensslEcKeyFree(ecKey);
57     }
58     return res;
59 }
60 
NewPKeyByEccPriKey(HcfOpensslEccPriKey * privateKey)61 static EVP_PKEY *NewPKeyByEccPriKey(HcfOpensslEccPriKey *privateKey)
62 {
63     EC_KEY *ecKey = OpensslEcKeyDup(privateKey->ecKey);
64     if (ecKey == NULL) {
65         return NULL;
66     }
67     EVP_PKEY *res = AssignEcKeyToPkey(ecKey);
68     if (res == NULL) {
69         OpensslEcKeyFree(ecKey);
70     }
71     return res;
72 }
73 
GetEcdhClass(void)74 static const char *GetEcdhClass(void)
75 {
76     return "HcfKeyAgreement.HcfKeyAgreementSpiEcdhOpensslImpl";
77 }
78 
DestroyEcdh(HcfObjectBase * self)79 static void DestroyEcdh(HcfObjectBase *self)
80 {
81     if (self == NULL) {
82         return;
83     }
84     if (!HcfIsClassMatch(self, GetEcdhClass())) {
85         return;
86     }
87     HcfFree(self);
88 }
89 
EngineGenerateSecret(HcfKeyAgreementSpi * self,HcfPriKey * priKey,HcfPubKey * pubKey,HcfBlob * returnSecret)90 static HcfResult EngineGenerateSecret(HcfKeyAgreementSpi *self, HcfPriKey *priKey,
91     HcfPubKey *pubKey, HcfBlob *returnSecret)
92 {
93     if ((self == NULL) || (priKey == NULL) || (pubKey == NULL) || (returnSecret == NULL)) {
94         LOGE("Invalid input parameter.");
95         return HCF_INVALID_PARAMS;
96     }
97     if ((!HcfIsClassMatch((HcfObjectBase *)self, GetEcdhClass())) ||
98         (!HcfIsClassMatch((HcfObjectBase *)priKey, HCF_OPENSSL_ECC_PRI_KEY_CLASS)) ||
99         (!HcfIsClassMatch((HcfObjectBase *)pubKey, HCF_OPENSSL_ECC_PUB_KEY_CLASS))) {
100         return HCF_INVALID_PARAMS;
101     }
102 
103     EVP_PKEY *priPKey = NewPKeyByEccPriKey((HcfOpensslEccPriKey *)priKey);
104     if (priPKey == NULL) {
105         LOGD("[error] Gen EVP_PKEY priKey failed");
106         return HCF_ERR_CRYPTO_OPERATION;
107     }
108     EVP_PKEY *pubPKey = NewPKeyByEccPubKey((HcfOpensslEccPubKey *)pubKey);
109     if (pubPKey == NULL) {
110         LOGD("[error] Gen EVP_PKEY pubKey failed");
111         EVP_PKEY_free(priPKey);
112         return HCF_ERR_CRYPTO_OPERATION;
113     }
114 
115     HcfResult res = KeyDerive(priPKey, pubPKey, returnSecret);
116     OpensslEvpPkeyFree(priPKey);
117     OpensslEvpPkeyFree(pubPKey);
118     return res;
119 }
120 
HcfKeyAgreementSpiEcdhCreate(HcfKeyAgreementParams * params,HcfKeyAgreementSpi ** returnObj)121 HcfResult HcfKeyAgreementSpiEcdhCreate(HcfKeyAgreementParams *params, HcfKeyAgreementSpi **returnObj)
122 {
123     (void)params;
124     if ((params == NULL) || (returnObj == NULL)) {
125         LOGE("Invalid input parameter.");
126         return HCF_INVALID_PARAMS;
127     }
128 
129     HcfKeyAgreementSpiEcdhOpensslImpl *returnImpl = (HcfKeyAgreementSpiEcdhOpensslImpl *)HcfMalloc(
130         sizeof(HcfKeyAgreementSpiEcdhOpensslImpl), 0);
131     if (returnImpl == NULL) {
132         LOGE("Failed to allocate returnImpl memroy!");
133         return HCF_ERR_MALLOC;
134     }
135     returnImpl->base.base.getClass = GetEcdhClass;
136     returnImpl->base.base.destroy = DestroyEcdh;
137     returnImpl->base.engineGenerateSecret = EngineGenerateSecret;
138 
139     *returnObj = (HcfKeyAgreementSpi *)returnImpl;
140     return HCF_SUCCESS;
141 }
142