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_asym_cipher.h"
17 #include <securec.h>
18 #include "result.h"
19 #include "memory.h"
20 #include "cipher.h"
21 #include "sm2_crypto_util.h"
22 #include "pub_key.h"
23 #include "pri_key.h"
24 #include "blob.h"
25 #include "object_base.h"
26 #include "native_common.h"
27 #include "crypto_common.h"
28
29 typedef struct OH_CryptoAsymCipher {
30 HcfObjectBase base;
31
32 HcfResult (*init)(HcfCipher *self, enum HcfCryptoMode opMode,
33 HcfKey *key, HcfParamsSpec *params);
34
35 HcfResult (*update)(HcfCipher *self, HcfBlob *input, HcfBlob *output);
36
37 HcfResult (*doFinal)(HcfCipher *self, HcfBlob *input, HcfBlob *output);
38
39 const char *(*getAlgorithm)(HcfCipher *self);
40
41 HcfResult (*setCipherSpecUint8Array)(HcfCipher *self, CipherSpecItem item, HcfBlob blob);
42
43 HcfResult (*getCipherSpecString)(HcfCipher *self, CipherSpecItem item, char **returnString);
44
45 HcfResult (*getCipherSpecUint8Array)(HcfCipher *self, CipherSpecItem item, HcfBlob *returnUint8Array);
46 } OH_CryptoAsymCipher;
47
48 typedef struct OH_CryptoKeyPair {
49 HcfObjectBase base;
50
51 HcfPriKey *priKey;
52
53 HcfPubKey *pubKey;
54 } OH_CryptoKeyPair;
55
56 typedef struct OH_CryptoSm2CiphertextSpec {
57 HcfBigInteger xCoordinate;
58 HcfBigInteger yCoordinate;
59 HcfBlob cipherTextData;
60 HcfBlob hashData;
61 } OH_CryptoSm2CiphertextSpec;
62
63 static const char *g_sm2ModeC1C3C2 = "C1C3C2";
64
OH_CryptoAsymCipher_Create(const char * algoName,OH_CryptoAsymCipher ** ctx)65 OH_Crypto_ErrCode OH_CryptoAsymCipher_Create(const char *algoName, OH_CryptoAsymCipher **ctx)
66 {
67 if ((algoName == NULL) || (ctx == NULL)) {
68 return CRYPTO_PARAMETER_CHECK_FAILED;
69 }
70 HcfResult ret = HcfCipherCreate(algoName, (HcfCipher **)ctx);
71 return GetOhCryptoErrCodeNew(ret);
72 }
73
OH_CryptoAsymCipher_Init(OH_CryptoAsymCipher * ctx,Crypto_CipherMode mode,OH_CryptoKeyPair * key)74 OH_Crypto_ErrCode OH_CryptoAsymCipher_Init(OH_CryptoAsymCipher *ctx, Crypto_CipherMode mode, OH_CryptoKeyPair *key)
75 {
76 if ((ctx == NULL) || (ctx->init == NULL) || (key == NULL)) {
77 return CRYPTO_PARAMETER_CHECK_FAILED;
78 }
79 HcfResult ret = HCF_SUCCESS;
80 switch (mode) {
81 case CRYPTO_ENCRYPT_MODE:
82 ret = ctx->init((HcfCipher *)ctx, (enum HcfCryptoMode)mode, (HcfKey *)(key->pubKey), NULL);
83 break;
84 case CRYPTO_DECRYPT_MODE:
85 ret = ctx->init((HcfCipher *)ctx, (enum HcfCryptoMode)mode, (HcfKey *)(key->priKey), NULL);
86 break;
87 default:
88 return CRYPTO_PARAMETER_CHECK_FAILED;
89 }
90 return GetOhCryptoErrCodeNew(ret);
91 }
92
OH_CryptoAsymCipher_Final(OH_CryptoAsymCipher * ctx,const Crypto_DataBlob * in,Crypto_DataBlob * out)93 OH_Crypto_ErrCode OH_CryptoAsymCipher_Final(OH_CryptoAsymCipher *ctx, const Crypto_DataBlob *in,
94 Crypto_DataBlob *out)
95 {
96 if ((ctx == NULL) || (ctx->doFinal == NULL) || (out == NULL)) {
97 return CRYPTO_PARAMETER_CHECK_FAILED;
98 }
99 HcfResult ret = ctx->doFinal((HcfCipher *)ctx, (HcfBlob *)in, (HcfBlob *)out);
100 return GetOhCryptoErrCodeNew(ret);
101 }
102
OH_CryptoAsymCipher_Destroy(OH_CryptoAsymCipher * ctx)103 void OH_CryptoAsymCipher_Destroy(OH_CryptoAsymCipher *ctx)
104 {
105 if ((ctx == NULL) || (ctx->base.destroy == NULL)) {
106 return;
107 }
108 ctx->base.destroy((HcfObjectBase *)ctx);
109 }
110
OH_CryptoSm2CiphertextSpec_Create(Crypto_DataBlob * sm2Ciphertext,OH_CryptoSm2CiphertextSpec ** spec)111 OH_Crypto_ErrCode OH_CryptoSm2CiphertextSpec_Create(Crypto_DataBlob *sm2Ciphertext, OH_CryptoSm2CiphertextSpec **spec)
112 {
113 if (spec == NULL) {
114 return CRYPTO_PARAMETER_CHECK_FAILED;
115 }
116 if (sm2Ciphertext == NULL) {
117 *spec = (OH_CryptoSm2CiphertextSpec *)HcfMalloc(sizeof(OH_CryptoSm2CiphertextSpec), 0);
118 if (*spec == NULL) {
119 return CRYPTO_MEMORY_ERROR;
120 }
121 return CRYPTO_SUCCESS;
122 }
123 HcfResult ret = HcfGetCipherTextSpec((HcfBlob *)sm2Ciphertext, g_sm2ModeC1C3C2, (Sm2CipherTextSpec **)spec);
124 return GetOhCryptoErrCodeNew(ret);
125 }
126
OH_CryptoSm2CiphertextSpec_GetItem(OH_CryptoSm2CiphertextSpec * spec,CryptoSm2CiphertextSpec_item item,Crypto_DataBlob * out)127 OH_Crypto_ErrCode OH_CryptoSm2CiphertextSpec_GetItem(OH_CryptoSm2CiphertextSpec *spec,
128 CryptoSm2CiphertextSpec_item item, Crypto_DataBlob *out)
129 {
130 if ((spec == NULL) || (out == NULL)) {
131 return CRYPTO_PARAMETER_CHECK_FAILED;
132 }
133 uint8_t *data = NULL;
134 size_t len = 0;
135 switch (item) {
136 case CRYPTO_SM2_CIPHERTEXT_C1_X:
137 data = spec->xCoordinate.data;
138 len = spec->xCoordinate.len;
139 break;
140 case CRYPTO_SM2_CIPHERTEXT_C1_Y:
141 data = spec->yCoordinate.data;
142 len = spec->yCoordinate.len;
143 break;
144 case CRYPTO_SM2_CIPHERTEXT_C2:
145 data = spec->cipherTextData.data;
146 len = spec->cipherTextData.len;
147 break;
148 case CRYPTO_SM2_CIPHERTEXT_C3:
149 data = spec->hashData.data;
150 len = spec->hashData.len;
151 break;
152 default:
153 return CRYPTO_PARAMETER_CHECK_FAILED;
154 }
155 if ((data == NULL) || (len == 0)) {
156 return CRYPTO_PARAMETER_CHECK_FAILED;
157 }
158 out->data = (uint8_t *)HcfMalloc(len, 0);
159 if (out->data == NULL) {
160 return CRYPTO_MEMORY_ERROR;
161 }
162 (void)memcpy_s(out->data, len, data, len);
163 out->len = len;
164 return CRYPTO_SUCCESS;
165 }
166
OH_CryptoSm2CiphertextSpec_SetItem(OH_CryptoSm2CiphertextSpec * spec,CryptoSm2CiphertextSpec_item item,Crypto_DataBlob * in)167 OH_Crypto_ErrCode OH_CryptoSm2CiphertextSpec_SetItem(OH_CryptoSm2CiphertextSpec *spec,
168 CryptoSm2CiphertextSpec_item item, Crypto_DataBlob *in)
169 {
170 if ((spec == NULL) || (in == NULL) || (in->data == NULL) || (in->len == 0)) {
171 return CRYPTO_PARAMETER_CHECK_FAILED;
172 }
173 uint8_t *data = (uint8_t *)HcfMalloc(in->len, 0);
174 if (data == NULL) {
175 return CRYPTO_MEMORY_ERROR;
176 }
177 (void)memcpy_s(data, in->len, in->data, in->len);
178 switch (item) {
179 case CRYPTO_SM2_CIPHERTEXT_C1_X:
180 HcfFree(spec->xCoordinate.data);
181 spec->xCoordinate.data = data;
182 spec->xCoordinate.len = in->len;
183 break;
184 case CRYPTO_SM2_CIPHERTEXT_C1_Y:
185 HcfFree(spec->yCoordinate.data);
186 spec->yCoordinate.data = data;
187 spec->yCoordinate.len = in->len;
188 break;
189 case CRYPTO_SM2_CIPHERTEXT_C2:
190 HcfFree(spec->cipherTextData.data);
191 spec->cipherTextData.data = data;
192 spec->cipherTextData.len = in->len;
193 break;
194 case CRYPTO_SM2_CIPHERTEXT_C3:
195 HcfFree(spec->hashData.data);
196 spec->hashData.data = data;
197 spec->hashData.len = in->len;
198 break;
199 default:
200 HcfFree(data);
201 data = NULL;
202 return CRYPTO_PARAMETER_CHECK_FAILED;
203 }
204
205 return CRYPTO_SUCCESS;
206 }
207
OH_CryptoSm2CiphertextSpec_Encode(OH_CryptoSm2CiphertextSpec * spec,Crypto_DataBlob * out)208 OH_Crypto_ErrCode OH_CryptoSm2CiphertextSpec_Encode(OH_CryptoSm2CiphertextSpec *spec, Crypto_DataBlob *out)
209 {
210 if ((spec == NULL) || (out == NULL)) {
211 return CRYPTO_PARAMETER_CHECK_FAILED;
212 }
213 HcfResult ret = HcfGenCipherTextBySpec((Sm2CipherTextSpec *)spec, g_sm2ModeC1C3C2, (HcfBlob *)out);
214 return GetOhCryptoErrCodeNew(ret);
215 }
216
OH_CryptoSm2CiphertextSpec_Destroy(OH_CryptoSm2CiphertextSpec * spec)217 void OH_CryptoSm2CiphertextSpec_Destroy(OH_CryptoSm2CiphertextSpec *spec)
218 {
219 DestroySm2CipherTextSpec((Sm2CipherTextSpec *)spec);
220 }