• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Huawei Technologies Co., Ltd.
3  * Licensed under the Mulan PSL v2.
4  * You can use this software according to the terms and conditions of the Mulan PSL v2.
5  * You may obtain a copy of Mulan PSL v2 at:
6  *     http://license.coscl.org.cn/MulanPSL2
7  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8  * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9  * PURPOSE.
10  * See the Mulan PSL v2 for more details.
11  */
12 
13 #include "soft_gmssl.h"
14 #include "gmssl_internal.h"
15 #include "soft_err.h"
16 #include <securec.h>
17 #include <openssl/hmac.h>
18 #include <hmac/hmac_local.h>
19 #include <openssl/ossl_typ.h>
20 #include <evp/evp_local.h>
21 #include <tee_log.h>
22 #include <tee_crypto_api.h>
23 #include <tee_property_inner.h>
24 #include "crypto_inner_defines.h"
25 
26 #ifdef CRYPTO_SUPPORT_SOFT_SM4
sm4_cipher_init_params_check(uint32_t alg_type,const struct memref_t * iv)27 static TEE_Result sm4_cipher_init_params_check(uint32_t alg_type, const struct memref_t *iv)
28 {
29     bool check = (alg_type == TEE_ALG_SM4_CTR) || (alg_type == TEE_ALG_SM4_CBC_NOPAD) ||
30                  (alg_type == TEE_ALG_SM4_CFB128) || (alg_type == TEE_ALG_SM4_CBC_PKCS7) ||
31                  (alg_type == TEE_ALG_SM4_GCM);
32     if (check) {
33         bool check_iv = (iv == NULL || iv->buffer == 0 || iv->size == 0);
34         if (check_iv) {
35             tloge("IV is NULL, please set IV first\n");
36             return CRYPTO_BAD_PARAMETERS;
37         }
38     }
39 
40     return CRYPTO_SUCCESS;
41 }
42 
sm4_cbc_encrypt_init(uint32_t direction,EVP_CIPHER_CTX * ctx,uint8_t * key_buffer,uint8_t * iv_buffer)43 static int32_t sm4_cbc_encrypt_init(uint32_t direction, EVP_CIPHER_CTX *ctx, uint8_t *key_buffer, uint8_t *iv_buffer)
44 {
45     if (direction == ENC_MODE)
46         return EVP_EncryptInit_ex(ctx, EVP_sm4_cbc(), NULL, key_buffer,
47             (unsigned char *)iv_buffer);
48     return EVP_DecryptInit_ex(ctx, EVP_sm4_cbc(), NULL, key_buffer,
49         (unsigned char *)iv_buffer);
50 }
51 
sm4_ecb_encrypt_init(uint32_t direction,EVP_CIPHER_CTX * ctx,uint8_t * key_buffer,uint8_t * iv_buffer)52 static int32_t sm4_ecb_encrypt_init(uint32_t direction, EVP_CIPHER_CTX *ctx, uint8_t *key_buffer, uint8_t *iv_buffer)
53 {
54     if (direction == ENC_MODE)
55         return EVP_EncryptInit_ex(ctx, EVP_sm4_ecb(), NULL, key_buffer,
56             (unsigned char *)iv_buffer);
57     return EVP_DecryptInit_ex(ctx, EVP_sm4_ecb(), NULL, key_buffer,
58         (unsigned char *)iv_buffer);
59 }
60 
sm4_ctr_encrypt_init(uint32_t direction,EVP_CIPHER_CTX * ctx,uint8_t * key_buffer,uint8_t * iv_buffer)61 static int32_t sm4_ctr_encrypt_init(uint32_t direction, EVP_CIPHER_CTX *ctx, uint8_t *key_buffer, uint8_t *iv_buffer)
62 {
63     if (direction == ENC_MODE)
64         return EVP_EncryptInit_ex(ctx, EVP_sm4_ctr(), NULL, key_buffer,
65             (unsigned char *)iv_buffer);
66     return EVP_DecryptInit_ex(ctx, EVP_sm4_ctr(), NULL, key_buffer,
67         (unsigned char *)iv_buffer);
68 }
69 
sm4_cfb_encrypt_init(uint32_t direction,EVP_CIPHER_CTX * ctx,const uint8_t * key_buffer,const uint8_t * iv_buffer)70 static int32_t sm4_cfb_encrypt_init(uint32_t direction, EVP_CIPHER_CTX *ctx,
71     const uint8_t *key_buffer, const uint8_t *iv_buffer)
72 {
73     if (direction == ENC_MODE)
74         return EVP_EncryptInit_ex(ctx, EVP_sm4_cfb128(), NULL, key_buffer,
75             (unsigned char *)iv_buffer);
76     return EVP_DecryptInit_ex(ctx, EVP_sm4_cfb128(), NULL, key_buffer,
77         (unsigned char *)iv_buffer);
78 }
79 
sm4_do_encrypt_init(EVP_CIPHER_CTX * ctx,uint32_t alg_type,uint32_t direction,const struct symmerit_key_t * key,const struct memref_t * iv)80 static int32_t sm4_do_encrypt_init(EVP_CIPHER_CTX *ctx, uint32_t alg_type, uint32_t direction,
81     const struct symmerit_key_t *key, const struct memref_t *iv)
82 {
83     uint8_t *iv_buffer = NULL;
84     bool check = (alg_type == TEE_ALG_SM4_CBC_NOPAD || alg_type == TEE_ALG_SM4_CTR) ||
85                  (alg_type == TEE_ALG_SM4_CBC_PKCS7) || (alg_type == TEE_ALG_SM4_CFB128);
86     if (check)
87         iv_buffer = (uint8_t *)(uintptr_t)(iv->buffer);
88     uint8_t *key_buffer = (uint8_t *)(uintptr_t)(key->key_buffer);
89 
90     switch (alg_type) {
91     case TEE_ALG_SM4_CBC_NOPAD:
92     case TEE_ALG_SM4_CBC_PKCS7:
93         return sm4_cbc_encrypt_init(direction, ctx, key_buffer, iv_buffer);
94     case TEE_ALG_SM4_ECB_NOPAD:
95         return sm4_ecb_encrypt_init(direction, ctx, key_buffer, iv_buffer);
96     case TEE_ALG_SM4_CTR:
97         return sm4_ctr_encrypt_init(direction, ctx, key_buffer, iv_buffer);
98     case TEE_ALG_SM4_CFB128:
99         return sm4_cfb_encrypt_init(direction, ctx, key_buffer, iv_buffer);
100     default:
101         return GMSSL_ERR;
102     }
103 }
104 
tee_sm4_cipher_init(uint32_t alg_type,uint32_t direction,const struct symmerit_key_t * key,const struct memref_t * iv)105 void *tee_sm4_cipher_init(uint32_t alg_type, uint32_t direction,
106     const struct symmerit_key_t *key, const struct memref_t *iv)
107 {
108     int32_t ret;
109     TEE_Result ret_c;
110     bool check = (key == NULL || key->key_buffer == 0 || key->key_size == 0);
111     if (check) {
112         tloge("keybuf is NULL");
113         return NULL;
114     }
115 
116     ret_c = sm4_cipher_init_params_check(alg_type, iv);
117     if (ret_c != CRYPTO_SUCCESS) {
118         tloge("check iv failed\n");
119         return NULL;
120     }
121 
122     EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
123     if (ctx == NULL) {
124         tloge("New SM4 ctx failed");
125         return NULL;
126     }
127     ret = EVP_CIPHER_CTX_reset(ctx);
128     if (ret != 1) {
129         tloge("init SM4 ctx failed\n");
130         EVP_CIPHER_CTX_free(ctx);
131         return NULL;
132     }
133 
134     ret = sm4_do_encrypt_init(ctx, alg_type, direction, key, iv);
135     if (ret != 1)
136         goto exit;
137 
138     if (alg_type == TEE_ALG_SM4_CBC_PKCS7)
139         (void)EVP_CIPHER_CTX_set_padding(ctx, EVP_PADDING_PKCS7);
140     else
141         (void)EVP_CIPHER_CTX_set_padding(ctx, 0);
142     return ctx;
143 
144 exit:
145     tloge("EVP sm4 cipher init failed\n");
146     EVP_CIPHER_CTX_free(ctx);
147     return NULL;
148 }
149 
sm4_cipher_init(struct ctx_handle_t * ctx,const struct symmerit_key_t * key,const struct memref_t * iv)150 int32_t sm4_cipher_init(struct ctx_handle_t *ctx, const struct symmerit_key_t *key, const struct memref_t *iv)
151 {
152     bool check = (ctx == NULL || key == NULL || key->key_buffer == 0 || key->key_size == 0);
153     if (check) {
154         tloge("input is null\n");
155         return CRYPTO_BAD_PARAMETERS;
156     }
157 
158     void *sm4_ctx = tee_sm4_cipher_init(ctx->alg_type, ctx->direction, key, iv);
159     if (sm4_ctx == NULL) {
160         tloge("sm4 init failed");
161         return get_soft_crypto_error(CRYPTO_BAD_PARAMETERS);
162     }
163     ctx->ctx_buffer = (uint64_t)(uintptr_t)sm4_ctx;
164     ctx->free_context = free_sm4_context;
165     return CRYPTO_SUCCESS;
166 }
167 
sm4_update_params_check(uint32_t alg_type,uint32_t src_len,uint32_t dest_len)168 static int32_t sm4_update_params_check(uint32_t alg_type, uint32_t src_len, uint32_t dest_len)
169 {
170     if (alg_type == TEE_ALG_SM4_CBC_PKCS7)
171         return CRYPTO_SUCCESS;
172 
173     bool check = (alg_type == TEE_ALG_SM4_ECB_NOPAD) || (alg_type == TEE_ALG_SM4_CBC_NOPAD);
174     if (check) {
175         if ((src_len % SM4_BLOCK) != 0) {
176             tloge("DataSize should be 16 bytes aligned!");
177             return CRYPTO_BAD_PARAMETERS;
178         }
179     }
180 
181     if (dest_len < src_len || dest_len == 0) {
182         tloge("output buffer is too small\n");
183         return CRYPTO_SHORT_BUFFER;
184     }
185 
186     return CRYPTO_SUCCESS;
187 }
188 
tee_sm4_update(struct ctx_handle_t * ctx,const struct memref_t * data_in,struct memref_t * data_out)189 static int32_t tee_sm4_update(struct ctx_handle_t *ctx, const struct memref_t *data_in,
190     struct memref_t *data_out)
191 {
192     int32_t ret;
193 
194     EVP_CIPHER_CTX *sm4_ctx = (EVP_CIPHER_CTX *)(uintptr_t)ctx->ctx_buffer;
195     if (sm4_ctx == NULL) {
196         tloge("The sm4 cipher ctx is null");
197         return CRYPTO_BAD_PARAMETERS;
198     }
199 
200     uint8_t *in_buffer = (uint8_t *)(uintptr_t)data_in->buffer;
201     uint8_t *out_buffer = (uint8_t *)(uintptr_t)data_out->buffer;
202 
203     ret = sm4_update_params_check(ctx->alg_type, data_in->size, data_out->size);
204     if (ret != CRYPTO_SUCCESS) {
205         tloge("sm4 update parameter check failed\n");
206         return ret;
207     }
208 
209     if (data_out->size > INT32_MAX) {
210         tloge("data out size is too long\n");
211         return CRYPTO_BAD_PARAMETERS;
212     }
213     int32_t temp_dest_len = (int32_t)data_out->size;
214     if (ctx->direction == ENC_MODE)
215         ret = EVP_EncryptUpdate(sm4_ctx, out_buffer, &temp_dest_len, in_buffer, data_in->size);
216     else
217         ret = EVP_DecryptUpdate(sm4_ctx, out_buffer, &temp_dest_len, in_buffer, data_in->size);
218     if (ret != GMSSL_OK || temp_dest_len < 0) {
219         tloge("sm4 cipher update failed\n");
220         return get_soft_crypto_error(CRYPTO_BAD_PARAMETERS);
221     }
222     data_out->size = (uint32_t)temp_dest_len;
223     return CRYPTO_SUCCESS;
224 }
225 
sm4_cipher_update(struct ctx_handle_t * ctx,const struct memref_t * data_in,struct memref_t * data_out)226 int32_t sm4_cipher_update(struct ctx_handle_t *ctx, const struct memref_t *data_in,
227     struct memref_t *data_out)
228 {
229     bool check = (ctx == NULL || data_in == NULL || data_out == NULL ||
230         ((ctx->alg_type != TEE_ALG_SM4_CBC_PKCS7 || ctx->direction == ENC_MODE) && data_out->size < data_in->size));
231     if (check) {
232         tloge("input is null\n");
233         return CRYPTO_BAD_PARAMETERS;
234     }
235 
236     return tee_sm4_update(ctx, data_in, data_out);
237 }
238 
tee_sm4_do_final(struct ctx_handle_t * ctx,const struct memref_t * data_in,struct memref_t * data_out)239 static int32_t tee_sm4_do_final(struct ctx_handle_t *ctx, const struct memref_t *data_in,
240     struct memref_t *data_out)
241 {
242     int32_t ret;
243     int32_t update_len = 0;
244     uint32_t temp_len = data_out->size;
245     if (data_in->buffer != 0 && data_in->size != 0) {
246         ret = tee_sm4_update(ctx, data_in, data_out);
247         if (ret != CRYPTO_SUCCESS) {
248             tloge("sm4 update last block failed");
249             EVP_CIPHER_CTX_free((EVP_CIPHER_CTX *)(uintptr_t)(ctx->ctx_buffer));
250             ctx->ctx_buffer = 0;
251             return ret;
252         }
253         update_len = (int32_t)data_out->size;
254     }
255 
256     uint8_t *out_buffer = (uint8_t *)(uintptr_t)data_out->buffer;
257     int32_t final_len = temp_len - update_len;
258     if (ctx->direction == ENC_MODE)
259         ret = EVP_EncryptFinal_ex((EVP_CIPHER_CTX *)(uintptr_t)ctx->ctx_buffer,
260             out_buffer + update_len, &final_len);
261     else
262         ret = EVP_DecryptFinal_ex((EVP_CIPHER_CTX *)(uintptr_t)ctx->ctx_buffer,
263             out_buffer + update_len, &final_len);
264     EVP_CIPHER_CTX_free((EVP_CIPHER_CTX *)(uintptr_t)(ctx->ctx_buffer));
265     ctx->ctx_buffer = 0;
266     if (ret != 1) {
267         tloge("sm4 cipher final failed\n");
268         return get_soft_crypto_error(CRYPTO_BAD_PARAMETERS);
269     }
270     if (update_len > INT32_MAX - final_len) {
271         tloge("final len is invalid");
272         return CRYPTO_BAD_PARAMETERS;
273     }
274     data_out->size = (uint32_t)(update_len + final_len);
275     return CRYPTO_SUCCESS;
276 }
277 
sm4_cipher_do_final(struct ctx_handle_t * ctx,const struct memref_t * data_in,struct memref_t * data_out)278 int32_t sm4_cipher_do_final(struct ctx_handle_t *ctx, const struct memref_t *data_in,
279     struct memref_t *data_out)
280 {
281     bool check = (ctx == NULL || data_in == NULL || data_out == NULL || data_out->buffer == 0 ||
282                   data_out->size == 0 || data_out->size < data_in->size);
283     if (check) {
284         tloge("bad parameters\n");
285         return CRYPTO_BAD_PARAMETERS;
286     }
287 
288     return tee_sm4_do_final(ctx, data_in, data_out);
289 }
290 #endif // CRYPTO_SUPPORT_SOFT_SM4
291 
sm3_mac_init(struct ctx_handle_t * ctx,const struct symmerit_key_t * key)292 int32_t sm3_mac_init(struct ctx_handle_t *ctx, const struct symmerit_key_t *key)
293 {
294     bool check = (ctx == NULL || key == NULL || key->key_buffer == 0 || key->key_size == 0);
295     if (check)
296         return CRYPTO_BAD_PARAMETERS;
297 
298     HMAC_CTX *hmac_ctx = HMAC_CTX_new();
299     if (hmac_ctx == NULL) {
300         tloge("malloc failed!\n");
301         return get_soft_crypto_error(CRYPTO_NOT_SUPPORTED);
302     }
303 
304     if (HMAC_Init(hmac_ctx, (const unsigned char *)(uintptr_t)(key->key_buffer), key->key_size, EVP_sm3()) == 0) {
305         tloge("sm3 hmac failed");
306         HMAC_CTX_free(hmac_ctx);
307         return get_soft_crypto_error(CRYPTO_MAC_INVALID);
308     }
309     ctx->ctx_buffer = (uint64_t)(uintptr_t)hmac_ctx;
310     ctx->ctx_size = sizeof(*hmac_ctx);
311 
312     return CRYPTO_SUCCESS;
313 }
314 
sm3_mac_update(struct ctx_handle_t * ctx,const struct memref_t * data_in)315 int32_t sm3_mac_update(struct ctx_handle_t *ctx, const struct memref_t *data_in)
316 {
317     int32_t ret;
318     bool check = (ctx == NULL || ctx->ctx_buffer == 0 || data_in == NULL ||
319         data_in->buffer == 0 || data_in->size == 0);
320     if (check) {
321         tloge("bad params\n");
322         ret = CRYPTO_BAD_PARAMETERS;
323         goto out;
324     }
325 
326     if (HMAC_Update((HMAC_CTX *)(uintptr_t)(ctx->ctx_buffer),
327                     (const unsigned char *)(uintptr_t)(data_in->buffer), data_in->size) == 0) {
328         tloge("sm3 hmac failed");
329         ret = get_soft_crypto_error(CRYPTO_MAC_INVALID);
330         goto out;
331     }
332     return CRYPTO_SUCCESS;
333 out:
334     if (ctx != NULL) {
335         HMAC_CTX_free((HMAC_CTX *)(uintptr_t)ctx->ctx_buffer);
336         ctx->ctx_buffer = 0;
337     }
338     return ret;
339 }
340 
sm3_mac_computefinal(struct ctx_handle_t * ctx,struct memref_t * data_out)341 int32_t sm3_mac_computefinal(struct ctx_handle_t *ctx, struct memref_t *data_out)
342 {
343     int32_t ret;
344     if (ctx == NULL || ctx->ctx_buffer == 0)
345         return CRYPTO_BAD_PARAMETERS;
346 
347     bool check = (data_out == NULL || data_out->buffer == 0 || data_out->size < SM3_DIGEST_LENGTH);
348     if (check) {
349         tloge("context is NULL");
350         ret = CRYPTO_BAD_PARAMETERS;
351         goto out;
352     }
353 
354     uint32_t out_len = 0;
355     if (HMAC_Final((HMAC_CTX *)(uintptr_t)(ctx->ctx_buffer),
356                    (unsigned char *)(uintptr_t)data_out->buffer, &out_len) == 0) {
357         tloge("sm3 hmac failed");
358         ret = get_soft_crypto_error(CRYPTO_MAC_INVALID);
359         goto out;
360     }
361 
362     data_out->size = out_len;
363     ret = CRYPTO_SUCCESS;
364 out:
365     HMAC_CTX_free((HMAC_CTX *)(uintptr_t)ctx->ctx_buffer);
366     ctx->ctx_buffer = 0;
367     return ret;
368 }
369 
sm3_digest_init(struct ctx_handle_t * ctx)370 int32_t sm3_digest_init(struct ctx_handle_t *ctx)
371 {
372     if (ctx == NULL)
373         return CRYPTO_BAD_PARAMETERS;
374 
375     EVP_MD_CTX *sm3_ctx = EVP_MD_CTX_new();
376     if (sm3_ctx == NULL) {
377         tloge("malloc context failed!\n");
378         return get_soft_crypto_error(CRYPTO_ERROR_OUT_OF_MEMORY);
379     }
380 
381     if (EVP_DigestInit(sm3_ctx, EVP_sm3()) == 0) {
382         tloge("sm3 init failed");
383         EVP_MD_CTX_free(sm3_ctx);
384         return get_soft_crypto_error(CRYPTO_MAC_INVALID);
385     }
386 
387     ctx->ctx_buffer = (uint64_t)(uintptr_t)sm3_ctx;
388     ctx->ctx_size = sizeof(*sm3_ctx);
389 
390     return CRYPTO_SUCCESS;
391 }
392 
sm3_digest_update(struct ctx_handle_t * ctx,const struct memref_t * data_in)393 int32_t sm3_digest_update(struct ctx_handle_t *ctx, const struct memref_t *data_in)
394 {
395     int32_t ret;
396     bool check = (ctx == NULL || ctx->ctx_buffer == 0 || data_in == NULL ||
397         data_in->buffer == 0 || data_in->size == 0);
398     if (check) {
399         tloge("Invalid params\n");
400         ret = CRYPTO_BAD_PARAMETERS;
401         goto out;
402     }
403 
404     if (EVP_DigestUpdate((EVP_MD_CTX *)(uintptr_t)(ctx->ctx_buffer),
405                          (const unsigned char *)(uintptr_t)data_in->buffer, (size_t)data_in->size) == 0) {
406         tloge("sm3 hash update failed");
407         ret = CRYPTO_MAC_INVALID;
408         goto out;
409     }
410 
411     return CRYPTO_SUCCESS;
412 out:
413     if (ctx != NULL) {
414         TEE_Free((void *)(uintptr_t)ctx->ctx_buffer);
415         ctx->ctx_buffer = 0;
416     }
417     return ret;
418 }
419 
sm3_digest_dofinal(struct ctx_handle_t * ctx,struct memref_t * data_out)420 int32_t sm3_digest_dofinal(struct ctx_handle_t *ctx, struct memref_t *data_out)
421 {
422     int32_t ret;
423     bool check = (ctx == NULL || ctx->ctx_buffer == 0);
424     if (check)
425         return CRYPTO_BAD_PARAMETERS;
426 
427     check = (data_out == NULL || data_out->buffer == 0 || data_out->size < SM3_DIGEST_LENGTH);
428     if (check) {
429         tloge("context is NULL");
430         ret = CRYPTO_BAD_PARAMETERS;
431         goto out;
432     }
433 
434     if (EVP_DigestFinal((EVP_MD_CTX *)(uintptr_t)(ctx->ctx_buffer),
435                         (unsigned char *)(uintptr_t)(data_out->buffer), NULL) == 0) {
436         tloge("do sm3 hash failed");
437         ret = get_soft_crypto_error(CRYPTO_MAC_INVALID);
438         goto out;
439     }
440     data_out->size = SM3_DIGEST_LENGTH;
441 
442     ret = CRYPTO_SUCCESS;
443 
444 out:
445     EVP_MD_CTX_free((EVP_MD_CTX *)(uintptr_t)ctx->ctx_buffer);
446     ctx->ctx_buffer = 0;
447     return ret;
448 }
449 
copy_sm_buf_info(uint64_t * dst_buf,const uint64_t src_buf,uint32_t src_size)450 static int32_t copy_sm_buf_info(uint64_t *dst_buf, const uint64_t src_buf, uint32_t src_size)
451 {
452     TEE_Free((void *)(uintptr_t)*dst_buf);
453     *dst_buf = 0;
454     bool check = ((src_buf == 0) || (src_size == 0));
455     if (check)
456         return CRYPTO_SUCCESS;
457 
458     *dst_buf = (uint64_t)(uintptr_t)TEE_Malloc(src_size, TEE_MALLOC_FILL_ZERO);
459     if (*dst_buf == 0) {
460         tloge("dst_buf malloc failed\n");
461         return CRYPTO_ERROR_OUT_OF_MEMORY;
462     }
463     (void)memcpy_s((void *)(uintptr_t)*dst_buf, src_size, (void *)(uintptr_t)src_buf, src_size);
464 
465     return CRYPTO_SUCCESS;
466 }
467 
copy_sm4_operation(struct ctx_handle_t * dest,const struct ctx_handle_t * src)468 static int32_t copy_sm4_operation(struct ctx_handle_t *dest, const struct ctx_handle_t *src)
469 {
470     if (dest == NULL || src == NULL)
471         return CRYPTO_BAD_PARAMETERS;
472 
473     if (dest->ctx_buffer != 0) {
474         EVP_CIPHER_CTX_free((EVP_CIPHER_CTX *)(uintptr_t)(dest->ctx_buffer));
475         dest->ctx_buffer = 0;
476     }
477     if (src->ctx_buffer == 0)
478         return CRYPTO_SUCCESS;
479 
480     EVP_CIPHER_CTX *new_ctx = EVP_CIPHER_CTX_new();
481     if (new_ctx == NULL) {
482         tloge("New aes ctx failed");
483         return CRYPTO_ERROR_OUT_OF_MEMORY;
484     }
485     int32_t ret = EVP_CIPHER_CTX_copy(new_ctx, (EVP_CIPHER_CTX *)(uintptr_t)(src->ctx_buffer));
486     if (ret != GMSSL_OK) {
487         tloge("Copy aes ctx failed");
488         EVP_CIPHER_CTX_free(new_ctx);
489         return get_soft_crypto_error(CRYPTO_BAD_PARAMETERS);
490     }
491     dest->ctx_buffer = (uint64_t)(uintptr_t)new_ctx;
492 
493     return CRYPTO_SUCCESS;
494 }
495 
soft_copy_gmssl_info(struct ctx_handle_t * dest,const struct ctx_handle_t * src)496 int32_t soft_copy_gmssl_info(struct ctx_handle_t *dest, const struct ctx_handle_t *src)
497 {
498     bool check = (dest == NULL || src == NULL);
499     if (check) {
500         tloge("Invalid params!\n");
501         return CRYPTO_BAD_PARAMETERS;
502     }
503 
504     switch (src->alg_type) {
505     case TEE_ALG_SM3:
506         return copy_sm_buf_info(&(dest->ctx_buffer), src->ctx_buffer, sizeof(EVP_MD_CTX));
507     case TEE_ALG_HMAC_SM3:
508         return copy_sm_buf_info(&(dest->ctx_buffer), src->ctx_buffer, sizeof(HMAC_CTX));
509     case TEE_ALG_SM4_ECB_NOPAD:
510     case TEE_ALG_SM4_CBC_NOPAD:
511     case TEE_ALG_SM4_CBC_PKCS7:
512     case TEE_ALG_SM4_CTR:
513     case TEE_ALG_SM4_CFB128:
514     case TEE_ALG_SM4_GCM:
515         return copy_sm4_operation(dest, src);
516     default:
517         return CRYPTO_SUCCESS;
518     }
519 }
520 
free_sm4_context(uint64_t * ctx)521 void free_sm4_context(uint64_t *ctx)
522 {
523     bool check = (ctx == NULL || *ctx == 0);
524     if (check) {
525         tloge("Invalid params!\n");
526         return;
527     }
528 
529     EVP_CIPHER_CTX_free((EVP_CIPHER_CTX *)(uintptr_t)(*ctx));
530     *ctx = 0;
531 }
532 
crypto_sm3_hash(const struct memref_t * data_in,struct memref_t * data_out)533 int32_t crypto_sm3_hash(const struct memref_t *data_in, struct memref_t *data_out)
534 {
535     bool check = (data_in == NULL || data_out == NULL);
536     if (check)
537         return CRYPTO_BAD_PARAMETERS;
538 
539     struct ctx_handle_t ctx;
540     int32_t rc = sm3_digest_init(&ctx);
541     if (rc != CRYPTO_SUCCESS) {
542         tloge("sm3 hash init failed");
543         return get_soft_crypto_error(CRYPTO_ERROR_SECURITY);
544     }
545 
546     rc = sm3_digest_update(&ctx, data_in);
547     if (rc != CRYPTO_SUCCESS) {
548         tloge("sm3 update failed");
549         TEE_Free((void *)(uintptr_t)(ctx.ctx_buffer));
550         return rc;
551     }
552     rc = sm3_digest_dofinal(&ctx, data_out);
553     if (rc != CRYPTO_SUCCESS)
554         tloge("sm3 dofinal failed");
555 
556     return rc;
557 }
558 
crypto_sm3_hmac(const struct symmerit_key_t * key,const struct memref_t * data_in,struct memref_t * data_out)559 int32_t crypto_sm3_hmac(const struct symmerit_key_t *key, const struct memref_t *data_in, struct memref_t *data_out)
560 {
561     bool check = (key == NULL || data_in == NULL || data_out == NULL);
562     if (check)
563         return CRYPTO_BAD_PARAMETERS;
564 
565     struct ctx_handle_t ctx = {0};
566 
567     int32_t rc = sm3_mac_init(&ctx, key);
568     if (rc != CRYPTO_SUCCESS) {
569         tloge("sm3 hmac init failed");
570         return get_soft_crypto_error(CRYPTO_ERROR_SECURITY);
571     }
572 
573     rc = sm3_mac_update(&ctx, data_in);
574     if (rc != CRYPTO_SUCCESS) {
575         tloge("sm3 hmac init failed");
576         TEE_Free((void *)(uintptr_t)(ctx.ctx_buffer));
577         return get_soft_crypto_error(CRYPTO_ERROR_SECURITY);
578     }
579 
580     rc = sm3_mac_computefinal(&ctx, data_out);
581     if (rc != CRYPTO_SUCCESS)
582         tloge("sm3 hmac dofinal failed");
583 
584     return rc;
585 }
586