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 "x25519_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 } HcfKeyAgreementSpiX25519OpensslImpl;
32
GetX25519Class(void)33 static const char *GetX25519Class(void)
34 {
35 return "HcfKeyAgreement.HcfKeyAgreementSpiX25519OpensslImpl";
36 }
37
DestroyX25519(HcfObjectBase * self)38 static void DestroyX25519(HcfObjectBase *self)
39 {
40 if (self == NULL) {
41 LOGE("Invalid input parameter.");
42 return;
43 }
44 if (!HcfIsClassMatch(self, GetX25519Class())) {
45 LOGE("Invalid class of self.");
46 return;
47 }
48 HcfFree(self);
49 }
50
EngineGenerateSecret(HcfKeyAgreementSpi * self,HcfPriKey * priKey,HcfPubKey * pubKey,HcfBlob * returnSecret)51 static HcfResult EngineGenerateSecret(HcfKeyAgreementSpi *self, HcfPriKey *priKey,
52 HcfPubKey *pubKey, HcfBlob *returnSecret)
53 {
54 if ((self == NULL) || (priKey == NULL) || (pubKey == NULL) || (returnSecret == NULL)) {
55 LOGE("Invalid input parameter.");
56 return HCF_INVALID_PARAMS;
57 }
58 if ((!HcfIsClassMatch((HcfObjectBase *)self, GetX25519Class())) ||
59 (!HcfIsClassMatch((HcfObjectBase *)priKey, OPENSSL_ALG25519_PRIKEY_CLASS)) ||
60 (!HcfIsClassMatch((HcfObjectBase *)pubKey, OPENSSL_ALG25519_PUBKEY_CLASS))) {
61 LOGE("Invalid class of self.");
62 return HCF_INVALID_PARAMS;
63 }
64 EVP_PKEY *pubPKey = OpensslEvpPkeyDup(((HcfOpensslAlg25519PubKey *)pubKey)->pkey);
65 if (pubPKey == NULL) {
66 LOGE("Failed to dup public pkey.");
67 return HCF_ERR_CRYPTO_OPERATION;
68 }
69 EVP_PKEY *priPKey = OpensslEvpPkeyDup(((HcfOpensslAlg25519PriKey *)priKey)->pkey);
70 if (priPKey == NULL) {
71 LOGE("Failed to dup private pkey.");
72 OpensslEvpPkeyFree(pubPKey);
73 return HCF_ERR_CRYPTO_OPERATION;
74 }
75 HcfResult res = KeyDerive(priPKey, pubPKey, returnSecret);
76 OpensslEvpPkeyFree(priPKey);
77 OpensslEvpPkeyFree(pubPKey);
78 return res;
79 }
80
HcfKeyAgreementSpiX25519Create(HcfKeyAgreementParams * params,HcfKeyAgreementSpi ** returnObj)81 HcfResult HcfKeyAgreementSpiX25519Create(HcfKeyAgreementParams *params, HcfKeyAgreementSpi **returnObj)
82 {
83 (void)params;
84 if ((params == NULL) || (returnObj == NULL)) {
85 LOGE("Invalid input parameter.");
86 return HCF_INVALID_PARAMS;
87 }
88
89 HcfKeyAgreementSpiX25519OpensslImpl *returnImpl = (HcfKeyAgreementSpiX25519OpensslImpl *)HcfMalloc(
90 sizeof(HcfKeyAgreementSpiX25519OpensslImpl), 0);
91 if (returnImpl == NULL) {
92 LOGE("Failed to allocate returnImpl memroy!");
93 return HCF_ERR_MALLOC;
94 }
95 returnImpl->base.base.getClass = GetX25519Class;
96 returnImpl->base.base.destroy = DestroyX25519;
97 returnImpl->base.engineGenerateSecret = EngineGenerateSecret;
98
99 *returnObj = (HcfKeyAgreementSpi *)returnImpl;
100 return HCF_SUCCESS;
101 }
102
103