1 /***************************************************************************//**
2 * \file cy_crypto_core_ecc.h
3 * \version 2.120
4 *
5 * \brief
6 *  This file provides constant and parameters for the API for the ECC
7 *  in the Crypto driver.
8 *
9 ********************************************************************************
10 * \copyright
11 * Copyright (c) (2020-2022), Cypress Semiconductor Corporation (an Infineon company) or
12 * an affiliate of Cypress Semiconductor Corporation.
13 * SPDX-License-Identifier: Apache-2.0
14 *
15 * Licensed under the Apache License, Version 2.0 (the "License");
16 * you may not use this file except in compliance with the License.
17 * You may obtain a copy of the License at
18 *
19 *    http://www.apache.org/licenses/LICENSE-2.0
20 *
21 * Unless required by applicable law or agreed to in writing, software
22 * distributed under the License is distributed on an "AS IS" BASIS,
23 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 * See the License for the specific language governing permissions and
25 * limitations under the License.
26 *******************************************************************************/
27 
28 
29 #if !defined (CY_CRYPTO_CORE_ECC_H)
30 #define CY_CRYPTO_CORE_ECC_H
31 
32 #include "cy_device.h"
33 
34 #if defined (CY_IP_MXCRYPTO)
35 
36 #include "cy_crypto_common.h"
37 
38 #if defined(__cplusplus)
39 extern "C" {
40 #endif
41 
42 CY_MISRA_DEVIATE_BLOCK_START('MISRA C-2012 Rule 20.5', 5, \
43 'Since CY_CRYPTO_ECC_MAX_SIZE is decided by curve type, use of #undef will not make it ambiguous that which macros exist at a particular point within a translation unit.')
44 
45 #if (CPUSS_CRYPTO_VU == 1) && defined (CY_CRYPTO_CFG_ECP_C)
46 
47 typedef enum cy_en_red_mul_algs {
48     CY_CRYPTO_NIST_P_CURVE_SPECIFIC_RED_ALG = 0,
49     CY_CRYPTO_NIST_P_SHIFT_MUL_RED_ALG,
50     CY_CRYPTO_NIST_P_BARRETT_RED_ALG
51 } cy_en_crypto_ecc_red_mul_algs_t;
52 
53 
54 /** Structure defines a NIST GF(p) curve */
55 typedef struct {
56     /**  The curve ID */
57     cy_en_crypto_ecc_curve_id_t id;
58     /** The size of the curve in bits */
59     uint32_t size;
60     /** name of curve */
61     const char_t *name;
62     /** ECC calculation default algorithm */
63     cy_en_crypto_ecc_red_mul_algs_t algo;
64     /** The prime that defines the field the curve is in (encoded in hex) */
65     const uint8_t *prime;
66     /** Barrett coefficient for reduction modulo ECC prime (hex) */
67     const uint8_t *barrett_p;
68     /** The order of the curve (hex) */
69     const uint8_t *order;
70     /** Barrett coefficient for reduction modulo ECC order (hex) */
71     const uint8_t *barrett_o;
72     /** The x co-ordinate of the base point on the curve (hex) */
73     const uint8_t *Gx;
74     /** The y co-ordinate of the base point on the curve (hex) */
75     const uint8_t *Gy;
76 } cy_stc_crypto_ecc_dp_type;
77 
78 /** Structure defines a Edwards GF(p) curve */
79 typedef struct {
80     /**  The curve ID */
81     cy_en_crypto_ecc_curve_id_t id;
82     /** The size of the curve in bits */
83     uint32_t size;
84     /** The size of the barret_order coefs in bits */
85     uint32_t barret_osize;
86     /** The size of the barret_prime coefs in bits */
87     uint32_t barret_psize;
88     /** name of curve */
89     const char_t *name;
90     /** ECC calculation default algorithm */
91     cy_en_crypto_ecc_red_mul_algs_t algo;
92     /** The prime that defines the field the curve is in (encoded in hex) */
93     const uint8_t *prime;
94     /** Barrett coefficient for reduction modulo ECC prime (hex) */
95     const uint8_t *barrett_p;
96     /** The order of the curve (hex) */
97     const uint8_t *order;
98     /** Barrett coefficient for reduction modulo ECC order (hex) */
99     const uint8_t *barrett_o;
100    /** D (hex) */
101     const uint8_t *d;
102    /** A (hex) */
103     const uint8_t *a;
104    /** The x co-ordinate of the base point on the curve (hex) */
105     const uint8_t *Gx;
106     /** The y co-ordinate of the base point on the curve (hex) */
107     const uint8_t *Gy;
108 } cy_stc_crypto_edw_dp_type;
109 
110 cy_stc_crypto_ecc_dp_type *Cy_Crypto_Core_ECC_GetCurveParams(cy_en_crypto_ecc_curve_id_t curveId);
111 cy_en_crypto_status_t Cy_Crypto_Core_EDW_GetCurveParams(cy_stc_crypto_edw_dp_type *dp, cy_en_crypto_ecc_curve_id_t curveId);
112 
113 
114 #if defined(CY_CRYPTO_CFG_ECDSA_C)
115 /**
116 * \addtogroup group_crypto_lld_asymmetric_functions
117 * \{
118 */
119 
120 #if defined(CY_CRYPTO_CFG_ECDSA_GENKEY_C)
121 /** Pointer to a random number supplier function */
122 typedef int (*cy_func_get_random_data_t)(void *rndInfo, uint8_t *rndData, size_t rndSize);
123 
124 cy_en_crypto_status_t Cy_Crypto_Core_ECC_MakePrivateKey(CRYPTO_Type *base,
125         cy_en_crypto_ecc_curve_id_t curveID, uint8_t *key,
126         cy_func_get_random_data_t GetRandomDataFunc, void *randomDataInfo);
127 cy_en_crypto_status_t Cy_Crypto_Core_ECC_MakePublicKey(CRYPTO_Type *base,
128         cy_en_crypto_ecc_curve_id_t curveID,
129         const uint8_t *privateKey, cy_stc_crypto_ecc_key *publicKey);
130 cy_en_crypto_status_t Cy_Crypto_Core_ECC_MakeKeyPair(CRYPTO_Type *base,
131         cy_en_crypto_ecc_curve_id_t curveID,
132         cy_stc_crypto_ecc_key *key,
133         cy_func_get_random_data_t GetRandomDataFunc, void *randomDataInfo);
134 #endif /* defined(CY_CRYPTO_CFG_ECDSA_GENKEY_C) */
135 
136 #if defined(CY_CRYPTO_CFG_ECDSA_SIGN_C)
137 cy_en_crypto_status_t Cy_Crypto_Core_ECC_SignHash(CRYPTO_Type *base,
138                                     const uint8_t *hash,
139                                     uint32_t hashlen,
140                                     uint8_t *sig,
141                                     const cy_stc_crypto_ecc_key *key,
142                                     const uint8_t *messageKey);
143 #endif /* defined(CY_CRYPTO_CFG_ECDSA_SIGN_C) */
144 
145 #if defined(CY_CRYPTO_CFG_ECDSA_VERIFY_C)
146 cy_en_crypto_status_t Cy_Crypto_Core_ECC_VerifyHash(CRYPTO_Type *base,
147                                     const uint8_t *sig,
148                                     const uint8_t *hash,
149                                     uint32_t hashlen,
150                                     uint8_t *stat,
151                                     const cy_stc_crypto_ecc_key *key);
152 #endif /* defined(CY_CRYPTO_CFG_ECDSA_VERIFY_C) */
153 
154 #if defined(CY_CRYPTO_CFG_EDDSA_SIGN_C)
155 cy_en_crypto_status_t Cy_Crypto_Core_ED25519_Sign(CRYPTO_Type *base,
156                                     const uint8_t *hash,
157                                     uint32_t hashlen,
158                                     uint8_t *sig,const cy_stc_crypto_ecc_key *key,
159                                     cy_en_eddsa_sig_type_t sigType,
160                                     const uint8_t *sigctx,
161                                     uint32_t sigctx_len);
162 
163 cy_en_crypto_status_t Cy_Crypto_Core_ED25519_PointMultiplication(CRYPTO_Type *base,
164                                     cy_en_crypto_ecc_curve_id_t curveID,
165                                     const uint8_t *ecpGX,
166                                     const uint8_t *ecpGY,
167                                     const uint8_t *ecpD,
168                                     uint8_t *ecpQX,
169                                     uint8_t *ecpQY);
170 #endif /* defined(CY_CRYPTO_CFG_EDDSA_SIGN_C) */
171 
172 #if defined(CY_CRYPTO_CFG_EDDSA_VERIFY_C)
173 /** \cond INTERNAL */
174 cy_en_crypto_status_t Cy_Crypto_Core_ED25519_PointDecode(CRYPTO_Type *base,
175                                     cy_en_crypto_ecc_curve_id_t curveID,
176                                     const uint8_t *publicKey,
177                                     uint8_t *pubKey_x,
178                                     uint8_t *pubKey_y);
179 /** \endcond */
180 
181 cy_en_crypto_status_t Cy_Crypto_Core_ED25519_Verify(CRYPTO_Type *base,
182                                     uint8_t *sig,
183                                     const uint8_t *hash,
184                                     uint32_t hashlen,
185                                     const cy_stc_crypto_ecc_key *key,
186                                     uint32_t *stat,
187                                     cy_en_eddsa_sig_type_t sigType,
188                                     const uint8_t *sigctx,
189                                     uint32_t sigctx_len);
190 #endif /* defined(CY_CRYPTO_CFG_EDDSA_VERIFY_C) */
191 
192 #if defined (CY_CRYPTO_CFG_EDDSA_GENKEY_C)
193 cy_en_crypto_status_t Cy_Crypto_Core_ED25519_MakePublicKey(CRYPTO_Type *base,
194                                     cy_en_crypto_ecc_curve_id_t curveID,
195                                     const uint8_t *privateKey,
196                                     cy_stc_crypto_ecc_key *publicKey);
197 #endif /* defined(CY_CRYPTO_CFG_EDDSA_GENKEY_C) */
198 
199 /** \} group_crypto_lld_asymmetric_functions */
200 #endif /* defined(CY_CRYPTO_CFG_ECDSA_C) */
201 
202 /* Sizes for NIST P-curves */
203 #if defined(CY_CRYPTO_CFG_ECP_DP_SECP192R1_ENABLED)
204 #define CY_CRYPTO_ECC_P192_SIZE            (192u)      /* 2^192 - 2^64 - 1 */
205 #define CY_CRYPTO_ECC_P192_BYTE_SIZE       CY_CRYPTO_BYTE_SIZE_OF_BITS(CY_CRYPTO_ECC_P192_SIZE)
206 
207 #undef  CY_CRYPTO_ECC_MAX_SIZE
208 #define CY_CRYPTO_ECC_MAX_SIZE             CY_CRYPTO_ECC_P192_SIZE
209 #endif /* defined(CY_CRYPTO_CFG_ECP_DP_SECP192R1_ENABLED) */
210 
211 #if defined(CY_CRYPTO_CFG_ECP_DP_SECP224R1_ENABLED)
212 #define CY_CRYPTO_ECC_P224_SIZE            (224u)      /* 2^224 - 2^96  + 1 */
213 #define CY_CRYPTO_ECC_P224_BYTE_SIZE       CY_CRYPTO_BYTE_SIZE_OF_BITS(CY_CRYPTO_ECC_P224_SIZE)
214 
215 #undef  CY_CRYPTO_ECC_MAX_SIZE
216 #define CY_CRYPTO_ECC_MAX_SIZE             CY_CRYPTO_ECC_P224_SIZE
217 #endif /* defined(CY_CRYPTO_CFG_ECP_DP_SECP224R1_ENABLED) */
218 
219 #if defined(CY_CRYPTO_CFG_ECP_DP_SECP256R1_ENABLED)
220 #define CY_CRYPTO_ECC_P256_SIZE            (256u)      /* 2^256 - 2^224 + 2^192 + 2^96 - 1 */
221 #define CY_CRYPTO_ECC_P256_BYTE_SIZE       CY_CRYPTO_BYTE_SIZE_OF_BITS(CY_CRYPTO_ECC_P256_SIZE)
222 
223 #undef  CY_CRYPTO_ECC_MAX_SIZE
224 #define CY_CRYPTO_ECC_MAX_SIZE             CY_CRYPTO_ECC_P256_SIZE
225 #endif /* defined(CY_CRYPTO_CFG_ECP_DP_SECP256R1_ENABLED) */
226 
227 #if defined(CY_CRYPTO_CFG_ECP_DP_SECP384R1_ENABLED)
228 #define CY_CRYPTO_ECC_P384_SIZE            (384u)      /* 2^384 - 2^128 - 2^96 + 2^32 - 1 */
229 #define CY_CRYPTO_ECC_P384_BYTE_SIZE       CY_CRYPTO_BYTE_SIZE_OF_BITS(CY_CRYPTO_ECC_P384_SIZE)
230 
231 #undef  CY_CRYPTO_ECC_MAX_SIZE
232 #define CY_CRYPTO_ECC_MAX_SIZE             CY_CRYPTO_ECC_P384_SIZE
233 #endif /* defined(CY_CRYPTO_CFG_ECP_DP_SECP384R1_ENABLED) */
234 
235 #if defined(CY_CRYPTO_CFG_ECP_DP_SECP521R1_ENABLED)
236 #define CY_CRYPTO_ECC_P521_SIZE            (521u)      /* 2^521 - 1 */
237 #define CY_CRYPTO_ECC_P521_BYTE_SIZE       CY_CRYPTO_BYTE_SIZE_OF_BITS(CY_CRYPTO_ECC_P521_SIZE)
238 
239 #undef  CY_CRYPTO_ECC_MAX_SIZE
240 #define CY_CRYPTO_ECC_MAX_SIZE             CY_CRYPTO_ECC_P521_SIZE
241 #endif /* defined(CY_CRYPTO_CFG_ECP_DP_SECP521R1_ENABLED) */
242 
243 #if defined(CY_CRYPTO_CFG_ECP_DP_ED25519_ENABLED)
244 #define CY_CRYPTO_ECC_ED25519_SIZE         (255u)      /* 2^555 - 19 */
245 #define CY_CRYPTO_ECC_ED25519_BYTE_SIZE    CY_CRYPTO_BYTE_SIZE_OF_BITS(CY_CRYPTO_ECC_ED25519_SIZE)
246 #endif /* defined(CY_CRYPTO_CFG_ECP_DP_ED25519_ENABLED) */
247 
248 #if defined(CY_CRYPTO_ECC_MAX_SIZE)
249 #define CY_CRYPTO_ECC_MAX_BYTE_SIZE        CY_CRYPTO_BYTE_SIZE_OF_BITS(CY_CRYPTO_ECC_MAX_SIZE)
250 #endif /* defined(CY_CRYPTO_ECC_MAX_SIZE) */
251 
252 /* "Global" vector unit registers. */
253 #define VR_D                               10u
254 #define VR_S_X                             11u
255 #define VR_S_Y                             12u
256 #define VR_BARRETT                         13u
257 #define VR_P                               14u         /* polynomial */
258 
259 #endif /* (CPUSS_CRYPTO_VU == 1) && defined (CY_CRYPTO_CFG_ECP_C) */
260 
261 CY_MISRA_BLOCK_END('MISRA C-2012 Rule 20.5')
262 
263 #if defined(__cplusplus)
264 }
265 #endif
266 
267 #endif /* #if defined (CY_IP_MXCRYPTO) */
268 
269 #endif /* CY_CRYPTO_CORE_ECC_H  */
270 
271 
272 /* [] END OF FILE */
273