1 /*
2  * Copyright (c) 2001-2020, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 
8 #include "cc_pal_log.h"
9 #include "cc_ecpki_error.h"
10 #include "mbedtls_cc_ec_mont_edw_error.h"
11 #include "mbedtls/ecp.h"
12 #include "ecp_common.h"
13 #include "cc_bitops.h"
14 
error_mapping_cc_to_mbedtls_ecc(CCError_t cc_error)15 int error_mapping_cc_to_mbedtls_ecc (CCError_t cc_error)
16 {
17     int ret;
18     switch (cc_error)
19     {
20 
21             case CC_ECPKI_ILLEGAL_DOMAIN_ID_ERROR:
22             case CC_ECPKI_BUILD_KEY_ILLEGAL_DOMAIN_ID_ERROR:
23             case CC_ECPKI_EXPORT_PUBL_KEY_ILLEGAL_DOMAIN_ID_ERROR:
24             case CC_ECPKI_BUILD_DOMAIN_ID_IS_NOT_VALID_ERROR:
25             case CC_ECDH_SVDP_DH_ILLEGAL_DOMAIN_ID_ERROR :
26             case CC_ECDSA_SIGN_INVALID_DOMAIN_ID_ERROR:
27             case CC_ECDSA_VERIFY_INVALID_DOMAIN_ID_ERROR:
28             case CC_ECPKI_INVALID_DOMAIN_ID_ERROR:
29             case CC_EC_MONT_IS_NOT_SUPPORTED:
30             case CC_EC_EDW_IS_NOT_SUPPORTED:
31 
32                 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
33                 break;
34 
35             case CC_ECPKI_INTERNAL_ERROR:
36             case CC_ECDH_SVDP_DH_PARTNER_PUBL_KEY_VALID_TAG_ERROR:
37             case CC_ECDH_SVDP_DH_USER_PRIV_KEY_VALID_TAG_ERROR:
38             case CC_ECDH_SVDP_DH_NOT_CONCENT_PUBL_AND_PRIV_DOMAIN_ID_ERROR:
39             case CC_ECDSA_SIGN_INVALID_IS_EPHEMER_KEY_INTERNAL_ERROR:
40             case CC_ECDSA_SIGN_USER_PRIV_KEY_VALIDATION_TAG_ERROR:
41             case CC_ECDSA_VERIFY_SIGNER_PUBL_KEY_VALIDATION_TAG_ERROR:
42             case CC_ECPKI_INVALID_PRIV_KEY_TAG_ERROR:
43             case CC_ECPKI_INVALID_PUBL_KEY_TAG_ERROR:
44             case CC_ECIES_INVALID_PUBL_KEY_TAG_ERROR:
45             case CC_ECIES_INVALID_PRIV_KEY_TAG_ERROR:
46             case CC_ECIES_INVALID_PRIV_KEY_VALUE_ERROR:
47 
48                 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
49                 break;
50 
51             case CC_ECDSA_VERIFY_INCONSISTENT_VERIFY_ERROR:
52             case CC_EC_EDW_SIGN_VERIFY_FAILED_ERROR:
53                 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
54                 break;
55 
56             case CC_ECPKI_DOMAIN_PTR_ERROR:
57             case CC_ECPKI_GEN_KEY_INVALID_PRIVATE_KEY_PTR_ERROR:
58             case CC_ECPKI_GEN_KEY_INVALID_PUBLIC_KEY_PTR_ERROR:
59             case CC_ECPKI_GEN_KEY_INVALID_TEMP_DATA_PTR_ERROR:
60             case CC_ECPKI_RND_CONTEXT_PTR_ERROR:
61             case CC_ECPKI_BUILD_KEY_INVALID_COMPRESSION_MODE_ERROR:
62             case CC_ECPKI_BUILD_KEY_INVALID_PRIV_KEY_IN_PTR_ERROR:
63             case CC_ECPKI_BUILD_KEY_INVALID_USER_PRIV_KEY_PTR_ERROR:
64             case CC_ECPKI_BUILD_KEY_INVALID_PRIV_KEY_SIZE_ERROR:
65             case CC_ECPKI_BUILD_KEY_INVALID_PRIV_KEY_DATA_ERROR:
66             case CC_ECPKI_BUILD_KEY_INVALID_PUBL_KEY_IN_PTR_ERROR:
67             case CC_ECPKI_BUILD_KEY_INVALID_USER_PUBL_KEY_PTR_ERROR:
68             case CC_ECPKI_BUILD_KEY_INVALID_PUBL_KEY_SIZE_ERROR:
69             case CC_ECPKI_BUILD_KEY_INVALID_PUBL_KEY_DATA_ERROR:
70             case CC_ECPKI_BUILD_KEY_INVALID_CHECK_MODE_ERROR:
71             case CC_ECPKI_BUILD_KEY_INVALID_TEMP_BUFF_PTR_ERROR:
72             case CC_ECPKI_EXPORT_PUBL_KEY_INVALID_USER_PUBL_KEY_PTR_ERROR:
73             case CC_ECPKI_EXPORT_PUBL_KEY_ILLEGAL_COMPRESSION_MODE_ERROR:
74             case CC_ECPKI_EXPORT_PUBL_KEY_INVALID_EXTERN_PUBL_KEY_PTR_ERROR:
75             case CC_ECPKI_EXPORT_PUBL_KEY_INVALID_PUBL_KEY_SIZE_PTR_ERROR:
76             case CC_ECPKI_EXPORT_PUBL_KEY_INVALID_PUBL_KEY_SIZE_ERROR:
77             case CC_ECPKI_EXPORT_PUBL_KEY_ILLEGAL_VALIDATION_TAG_ERROR:
78             case CC_ECPKI_EXPORT_PUBL_KEY_INVALID_PUBL_KEY_DATA_ERROR:
79             case CC_ECPKI_BUILD_DOMAIN_DOMAIN_PTR_ERROR:
80             case CC_ECPKI_BUILD_DOMAIN_EC_PARAMETR_PTR_ERROR:
81             case CC_ECPKI_BUILD_DOMAIN_EC_PARAMETR_SIZE_ERROR:
82             case CC_ECPKI_BUILD_DOMAIN_COFACTOR_PARAMS_ERROR:
83             case CC_ECPKI_BUILD_DOMAIN_SECURITY_STRENGTH_ERROR:
84             case CC_ECPKI_BUILD_SCA_RESIST_ILLEGAL_MODE_ERROR:
85             case CC_ECDH_SVDP_DH_INVALID_PARTNER_PUBL_KEY_PTR_ERROR:
86             case CC_ECDH_SVDP_DH_INVALID_USER_PRIV_KEY_PTR_ERROR:
87             case CC_ECDH_SVDP_DH_INVALID_SHARED_SECRET_VALUE_PTR_ERROR:
88             case CC_ECDH_SVDP_DH_INVALID_TEMP_DATA_PTR_ERROR:
89             case CC_ECDH_SVDP_DH_INVALID_SHARED_SECRET_VALUE_SIZE_PTR_ERROR:
90             case CC_ECDH_SVDP_DH_INVALID_SHARED_SECRET_VALUE_SIZE_ERROR:
91             case CC_ECDSA_SIGN_INVALID_USER_CONTEXT_PTR_ERROR:
92             case CC_ECDSA_SIGN_INVALID_USER_PRIV_KEY_PTR_ERROR:
93             case CC_ECDSA_SIGN_ILLEGAL_HASH_OP_MODE_ERROR:
94             case CC_ECDSA_SIGN_INVALID_MESSAGE_DATA_IN_PTR_ERROR:
95             case CC_ECDSA_SIGN_INVALID_MESSAGE_DATA_IN_SIZE_ERROR:
96             case CC_ECDSA_SIGN_USER_CONTEXT_VALIDATION_TAG_ERROR:
97             case CC_ECDSA_SIGN_INVALID_SIGNATURE_OUT_PTR_ERROR:
98             case CC_ECDSA_SIGN_INVALID_SIGNATURE_OUT_SIZE_PTR_ERROR:
99             case CC_ECDSA_SIGN_INVALID_SIGNATURE_OUT_SIZE_ERROR:
100             case CC_ECDSA_SIGN_INVALID_EPHEMERAL_KEY_PTR_ERROR:
101             case CC_ECDSA_SIGN_INVALID_RND_CONTEXT_PTR_ERROR:
102             case CC_ECDSA_SIGN_INVALID_RND_FUNCTION_PTR_ERROR:
103             case CC_ECDSA_SIGN_SIGNING_ERROR:
104             case CC_ECDSA_VERIFY_INVALID_USER_CONTEXT_PTR_ERROR:
105             case CC_ECDSA_VERIFY_INVALID_SIGNER_PUBL_KEY_PTR_ERROR:
106             case CC_ECDSA_VERIFY_ILLEGAL_HASH_OP_MODE_ERROR:
107             case CC_ECDSA_VERIFY_INVALID_SIGNATURE_IN_PTR_ERROR:
108             case CC_ECDSA_VERIFY_INVALID_SIGNATURE_SIZE_ERROR:
109             case CC_ECDSA_VERIFY_INVALID_MESSAGE_DATA_IN_PTR_ERROR:
110             case CC_ECDSA_VERIFY_INVALID_MESSAGE_DATA_IN_SIZE_ERROR:
111             case CC_ECDSA_VERIFY_USER_CONTEXT_VALIDATION_TAG_ERROR:
112             case CC_ECC_ILLEGAL_HASH_MODE_ERROR:
113             case CC_ECPKI_INVALID_RND_FUNC_PTR_ERROR:
114             case CC_ECPKI_INVALID_RND_CTX_PTR_ERROR:
115             case CC_ECPKI_INVALID_DATA_IN_PASSED_STRUCT_ERROR:
116             case CC_ECPKI_INVALID_BASE_POINT_PTR_ERROR:
117             case CC_ECIES_INVALID_PUBL_KEY_PTR_ERROR:
118             case CC_ECIES_INVALID_PRIV_KEY_PTR_ERROR:
119             case CC_ECIES_INVALID_KDF_DERIV_MODE_ERROR:
120             case CC_ECIES_INVALID_KDF_HASH_MODE_ERROR:
121             case CC_ECIES_INVALID_SECRET_KEY_PTR_ERROR:
122             case CC_ECIES_INVALID_SECRET_KEY_SIZE_ERROR:
123             case CC_ECIES_INVALID_CIPHER_DATA_PTR_ERROR:
124             case CC_ECIES_INVALID_CIPHER_DATA_SIZE_PTR_ERROR:
125             case CC_ECIES_INVALID_CIPHER_DATA_SIZE_ERROR:
126             case CC_ECIES_INVALID_TEMP_DATA_PTR_ERROR:
127             case CC_ECIES_INVALID_EPHEM_KEY_PAIR_PTR_ERROR:
128             case CC_EC_EDW_INVALID_INPUT_POINTER_ERROR:
129             case CC_EC_EDW_INVALID_INPUT_SIZE_ERROR:
130             case CC_EC_EDW_INVALID_SCALAR_SIZE_ERROR:
131             case CC_EC_EDW_INVALID_SCALAR_DATA_ERROR:
132             case CC_EC_EDW_RND_CONTEXT_PTR_INVALID_ERROR:
133             case CC_EC_EDW_RND_GEN_VECTOR_FUNC_ERROR:
134             case CC_EC_MONT_INVALID_INPUT_POINTER_ERROR:
135             case CC_EC_MONT_INVALID_INPUT_SIZE_ERROR:
136             case CC_EC_MONT_INVALID_DOMAIN_ID_ERROR:
137             case CC_ECEDW_INTERNAL_ERROR:
138             case CC_ECMONT_INTERNAL_ERROR:
139 
140                 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
141                 break;
142 
143             default:
144                 ret = -1;
145                 CC_PAL_LOG_ERR("Unknown CC_ERROR %d\r\n", cc_error);
146                 break;
147     }
148     CC_PAL_LOG_DEBUG("Converted CC_ERROR %d to MBEDTLS_ERR %d\r\n", cc_error, ret);
149     return ret;
150 }
151 
152 /* conversion from mbedtls group id (curve) to CC domain*/
ecp_grp_id_to_domain_id(const mbedtls_ecp_group_id id,CCEcpkiDomainID_t * domain_id)153 int ecp_grp_id_to_domain_id (const mbedtls_ecp_group_id id, CCEcpkiDomainID_t *domain_id)
154 {
155     switch (id)
156     {
157         case MBEDTLS_ECP_DP_SECP192R1:
158             *domain_id = CC_ECPKI_DomainID_secp192r1;
159             break;                          /*!< 192-bits NIST curve  */
160         case MBEDTLS_ECP_DP_SECP224R1:      /*!< 224-bits NIST curve  */
161             *domain_id = CC_ECPKI_DomainID_secp224r1;
162             break;
163         case MBEDTLS_ECP_DP_SECP256R1:      /*!< 256-bits NIST curve  */
164             *domain_id = CC_ECPKI_DomainID_secp256r1;
165             break;
166         case MBEDTLS_ECP_DP_SECP384R1:      /*!< 384-bits NIST curve  */
167             *domain_id = CC_ECPKI_DomainID_secp384r1;
168             break;
169         case MBEDTLS_ECP_DP_SECP521R1:      /*!< 521-bits NIST curve  */
170             *domain_id = CC_ECPKI_DomainID_secp521r1;
171             break;
172         case MBEDTLS_ECP_DP_SECP192K1:      /*!< 192-bits "Koblitz" curve */
173             *domain_id = CC_ECPKI_DomainID_secp192k1;
174             break;
175         case MBEDTLS_ECP_DP_SECP224K1:      /*!< 224-bits "Koblitz" curve */
176             *domain_id = CC_ECPKI_DomainID_secp224k1;
177             break;
178         case MBEDTLS_ECP_DP_SECP256K1:      /*!< 256-bits "Koblitz" curve */
179             *domain_id = CC_ECPKI_DomainID_secp256k1;
180             break;
181 
182         default:
183             CC_PAL_LOG_ERR("Error - ecp_grp_id_to_domain_id, group id %d is not supported\n", id);
184             *domain_id = CC_ECPKI_DomainIDLast;
185             return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
186     }
187     return (0);
188 }
189 
190