1 /**
2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
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 * Description: ecp harden adapt functions.
15 *
16 * Create: 2024-07-18
17 */
18
19 #include "mbedtls_harden_adapt_api.h"
20 #include "dfx.h"
21 #include "securec.h"
22
23 static mbedtls_alt_ecp_harden_func g_ecp_func;
24
mbedtls_alt_ecp_mul(mbedtls_alt_ecp_curve_type curve_type,const mbedtls_alt_ecp_data * k,const mbedtls_alt_ecp_point * p,const mbedtls_alt_ecp_point * r)25 int mbedtls_alt_ecp_mul(mbedtls_alt_ecp_curve_type curve_type, const mbedtls_alt_ecp_data *k,
26 const mbedtls_alt_ecp_point *p, const mbedtls_alt_ecp_point *r)
27 {
28 int ret = -1;
29 mbedtls_harden_log_func_enter();
30 if (g_ecp_func.ecp_mul == NULL) {
31 mbedtls_printf("Error: ecp_mul unregister!\n");
32 return -1;
33 }
34 ret = g_ecp_func.ecp_mul(curve_type, k, p, r);
35 mbedtls_harden_log_func_exit();
36 return ret;
37 }
38
mbedtls_alt_ecdsa_verify(mbedtls_alt_ecp_curve_type curve_type,const unsigned char * hash,unsigned int hash_len,const unsigned char * pub_x,const unsigned char * pub_y,const unsigned char * sig_r,const unsigned char * sig_s,unsigned klen)39 int mbedtls_alt_ecdsa_verify(mbedtls_alt_ecp_curve_type curve_type,
40 const unsigned char *hash, unsigned int hash_len,
41 const unsigned char *pub_x, const unsigned char *pub_y,
42 const unsigned char *sig_r, const unsigned char *sig_s, unsigned klen)
43 {
44 int ret = -1;
45 mbedtls_harden_log_func_enter();
46 if (g_ecp_func.ecdsa_verify == NULL) {
47 mbedtls_printf("Error: ecdsa_verify unregister!\n");
48 return -1;
49 }
50 ret = g_ecp_func.ecdsa_verify(curve_type, hash, hash_len, pub_x, pub_y, sig_r, sig_s, klen);
51 mbedtls_harden_log_func_exit();
52 return ret;
53 }
54
mbedtls_alt_ecp_register(const mbedtls_alt_ecp_harden_func * ecp_func)55 void mbedtls_alt_ecp_register(const mbedtls_alt_ecp_harden_func *ecp_func)
56 {
57 if (ecp_func == NULL) {
58 return;
59 }
60 (void)memcpy_s(&g_ecp_func, sizeof(mbedtls_alt_ecp_harden_func), ecp_func, sizeof(mbedtls_alt_ecp_harden_func));
61 }