1 /*
2 * Copyright (C) 2024 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
17 #include "crypto_sym_key.h"
18 #include "crypto_common.h"
19 #include "sym_key_generator.h"
20 #include "result.h"
21 #include "blob.h"
22 #include "object_base.h"
23 #include "native_common.h"
24
25
26 struct OH_CryptoSymKeyGenerator {
27 HcfObjectBase base;
28
29 /** Generate symmetric key object */
30 HcfResult (*generateSymKey)(HcfSymKeyGenerator *self, HcfSymKey **symKey);
31
32 /** Convert byte data to symmetric key object */
33 HcfResult (*convertSymKey)(HcfSymKeyGenerator *self, const HcfBlob *key, HcfSymKey **symKey);
34
35 /** Get the algorithm name of the current these key generator objects */
36 const char *(*getAlgoName)(HcfSymKeyGenerator *self);
37 };
38
39 struct OH_CryptoSymKey {
40 HcfKey key;
41
42 void (*clearMem)(HcfSymKey *self);
43 };
44
OH_CryptoSymKeyGenerator_Create(const char * algoName,OH_CryptoSymKeyGenerator ** ctx)45 OH_Crypto_ErrCode OH_CryptoSymKeyGenerator_Create(const char *algoName, OH_CryptoSymKeyGenerator **ctx)
46 {
47 if (ctx == NULL) {
48 return CRYPTO_INVALID_PARAMS;
49 }
50 HcfResult ret = HcfSymKeyGeneratorCreate(algoName, (HcfSymKeyGenerator **)ctx);
51 return GetOhCryptoErrCode(ret);
52 }
53
OH_CryptoSymKeyGenerator_Generate(OH_CryptoSymKeyGenerator * ctx,OH_CryptoSymKey ** keyCtx)54 OH_Crypto_ErrCode OH_CryptoSymKeyGenerator_Generate(OH_CryptoSymKeyGenerator *ctx, OH_CryptoSymKey **keyCtx)
55 {
56 if ((ctx == NULL) || (ctx->generateSymKey == NULL) || (keyCtx == NULL)) {
57 return CRYPTO_INVALID_PARAMS;
58 }
59 HcfResult ret = ctx->generateSymKey((HcfSymKeyGenerator *)ctx, (HcfSymKey **)keyCtx);
60 return GetOhCryptoErrCode(ret);
61 }
62
OH_CryptoSymKeyGenerator_Convert(OH_CryptoSymKeyGenerator * ctx,const Crypto_DataBlob * keyData,OH_CryptoSymKey ** keyCtx)63 OH_Crypto_ErrCode OH_CryptoSymKeyGenerator_Convert(OH_CryptoSymKeyGenerator *ctx,
64 const Crypto_DataBlob *keyData, OH_CryptoSymKey **keyCtx)
65 {
66 if ((ctx == NULL) || (ctx->convertSymKey == NULL) || (keyData == NULL) || (keyCtx == NULL)) {
67 return CRYPTO_INVALID_PARAMS;
68 }
69 HcfResult ret = ctx->convertSymKey((HcfSymKeyGenerator *)ctx, (HcfBlob *)keyData, (HcfSymKey **)keyCtx);
70 return GetOhCryptoErrCode(ret);
71 }
72
OH_CryptoSymKeyGenerator_GetAlgoName(OH_CryptoSymKeyGenerator * ctx)73 const char *OH_CryptoSymKeyGenerator_GetAlgoName(OH_CryptoSymKeyGenerator *ctx)
74 {
75 if (ctx == NULL || (ctx->getAlgoName == NULL)) {
76 return NULL;
77 }
78 return ctx->getAlgoName((HcfSymKeyGenerator *)ctx);
79 }
80
OH_CryptoSymKeyGenerator_Destroy(OH_CryptoSymKeyGenerator * ctx)81 void OH_CryptoSymKeyGenerator_Destroy(OH_CryptoSymKeyGenerator *ctx)
82 {
83 if (ctx == NULL || (ctx->base.destroy == NULL)) {
84 return;
85 }
86 ctx->base.destroy((HcfObjectBase *)ctx);
87 }
88
OH_CryptoSymKey_GetAlgoName(OH_CryptoSymKey * keyCtx)89 const char *OH_CryptoSymKey_GetAlgoName(OH_CryptoSymKey *keyCtx)
90 {
91 if (keyCtx == NULL || (keyCtx->key.getAlgorithm == NULL)) {
92 return NULL;
93 }
94 return keyCtx->key.getAlgorithm((HcfKey *)keyCtx);
95 }
96
OH_CryptoSymKey_GetKeyData(OH_CryptoSymKey * keyCtx,Crypto_DataBlob * out)97 OH_Crypto_ErrCode OH_CryptoSymKey_GetKeyData(OH_CryptoSymKey *keyCtx, Crypto_DataBlob *out)
98 {
99 if ((keyCtx == NULL) || (keyCtx->key.getEncoded == NULL) || (out == NULL)) {
100 return CRYPTO_INVALID_PARAMS;
101 }
102 HcfResult ret = keyCtx->key.getEncoded((HcfKey *)keyCtx, (HcfBlob *)out);
103 return GetOhCryptoErrCode(ret);
104 }
105
OH_CryptoSymKey_Destroy(OH_CryptoSymKey * keyCtx)106 void OH_CryptoSymKey_Destroy(OH_CryptoSymKey *keyCtx)
107 {
108 if ((keyCtx == NULL) || (keyCtx->key.base.destroy == NULL)) {
109 return;
110 }
111 keyCtx->key.base.destroy((HcfObjectBase *)keyCtx);
112 }
113