1 /*
2 * Copyright (c) 2025 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 "crypto_key_agreement.h"
17 #include "native_common.h"
18 #include "crypto_common.h"
19 #include "crypto_asym_key.h"
20 #include "key_agreement.h"
21
22 typedef struct OH_CryptoKeyAgreement {
23 HcfObjectBase base;
24
25 HcfResult (*generateSecret)(HcfKeyAgreement *self, HcfPriKey *priKey,
26 HcfPubKey *pubKey, HcfBlob *returnSecret);
27
28 const char *(*getAlgoName)(HcfKeyAgreement *self);
29 } OH_CryptoKeyAgreement;
30
OH_CryptoKeyAgreement_Create(const char * algoName,OH_CryptoKeyAgreement ** ctx)31 OH_Crypto_ErrCode OH_CryptoKeyAgreement_Create(const char *algoName, OH_CryptoKeyAgreement **ctx)
32 {
33 if (ctx == NULL) {
34 return CRYPTO_PARAMETER_CHECK_FAILED;
35 }
36 HcfResult ret = HcfKeyAgreementCreate(algoName, (HcfKeyAgreement **)ctx);
37 return GetOhCryptoErrCodeNew(ret);
38 }
39
OH_CryptoKeyAgreement_GenerateSecret(OH_CryptoKeyAgreement * ctx,OH_CryptoPrivKey * privkey,OH_CryptoPubKey * pubkey,Crypto_DataBlob * secret)40 OH_Crypto_ErrCode OH_CryptoKeyAgreement_GenerateSecret(OH_CryptoKeyAgreement *ctx, OH_CryptoPrivKey *privkey,
41 OH_CryptoPubKey *pubkey, Crypto_DataBlob *secret)
42 {
43 if ((ctx == NULL) || (ctx->generateSecret == NULL) || (privkey == NULL) || (pubkey == NULL) || (secret == NULL)) {
44 return CRYPTO_PARAMETER_CHECK_FAILED;
45 }
46 HcfResult ret = ctx->generateSecret((HcfKeyAgreement *)ctx, (HcfPriKey *)privkey, (HcfPubKey *)pubkey,
47 (HcfBlob *)secret);
48 return GetOhCryptoErrCodeNew(ret);
49 }
50
OH_CryptoKeyAgreement_Destroy(OH_CryptoKeyAgreement * ctx)51 void OH_CryptoKeyAgreement_Destroy(OH_CryptoKeyAgreement *ctx)
52 {
53 HcfObjDestroy((HcfKeyAgreement*)ctx);
54 }