1 /* USER CODE BEGIN Header */
2 /**
3 ******************************************************************************
4 * File Name :
5 * Description :
6 ******************************************************************************
7 * @attention
8 *
9 * Copyright (c) 2024 STMicroelectronics.
10 * All rights reserved.
11 *
12 * This software is licensed under terms that can be found in the LICENSE file
13 * in the root directory of this software component.
14 * If no LICENSE file comes with this software, it is provided AS-IS.
15 *
16 ******************************************************************************
17 */
18 /* USER CODE END Header */
19
20 #include "app_common.h"
21 #include "stm32wbaxx_ll_pka.h"
22
23 /*****************************************************************************/
24
25 static const uint32_t HW_PKA_P256_gfp[8] =
26 {
27 0xFFFFFFFF, /* LSB */
28 0xFFFFFFFF,
29 0xFFFFFFFF,
30 0x00000000,
31 0x00000000,
32 0x00000000,
33 0x00000001,
34 0xFFFFFFFF,
35 };
36
37 static const uint32_t HW_PKA_P256_r2[8] =
38 {
39 0x00000002, /* LSB */
40 0x00000005,
41 0x00000003,
42 0xFFFFFFFE,
43 0xFFFFFFF9,
44 0xFFFFFFFB,
45 0xFFFFFFFC,
46 0xFFFFFFFC,
47 };
48
49 static const uint32_t HW_PKA_P256_p_x[8] =
50 {
51 0xD898C296, /* LSB */
52 0xF4A13945,
53 0x2DEB33A0,
54 0x77037D81,
55 0x63A440F2,
56 0xF8BCE6E5,
57 0xE12C4247,
58 0x6B17D1F2,
59 };
60
61 static const uint32_t HW_PKA_P256_p_y[8] =
62 {
63 0x37BF51F5, /* LSB */
64 0xCBB64068,
65 0x6B315ECE,
66 0x2BCE3357,
67 0x7C0F9E16,
68 0x8EE7EB4A,
69 0xFE1A7F9B,
70 0x4FE342E2,
71 };
72
73 static const uint32_t HW_PKA_P256_a[8] =
74 {
75 0x00000003, /* LSB */
76 0x00000000,
77 0x00000000,
78 0x00000000,
79 0x00000000,
80 0x00000000,
81 0x00000000,
82 0x00000000,
83 };
84
85 static const uint32_t HW_PKA_P256_b[8] =
86 {
87 0x27D2604B, /* LSB */
88 0x3BCE3C3E,
89 0xCC53B0F6,
90 0x651D06B0,
91 0x769886BC,
92 0xB3EBBD55,
93 0xAA3A93E7,
94 0x5AC635D8,
95 };
96
97 static const uint32_t HW_PKA_P256_n[8] =
98 {
99 0XFC632551, /* LSB */
100 0XF3B9CAC2,
101 0XA7179E84,
102 0XBCE6FAAD,
103 0XFFFFFFFF,
104 0XFFFFFFFF,
105 0X00000000,
106 0XFFFFFFFF
107 };
108
109 /*****************************************************************************/
110
HW_PKA_P256_StartRangeCheck(const uint32_t * coord)111 void HW_PKA_P256_StartRangeCheck( const uint32_t* coord )
112 {
113 /* Set the muber of bits of P */
114 HW_PKA_WriteSingleInput( PKA_COMPARISON_IN_OP_NB_BITS, 256 );
115
116 /* Set the coordinate */
117 HW_PKA_WriteOperand( PKA_COMPARISON_IN_OP1, 8, coord );
118
119 /* Set the modulus value p */
120 HW_PKA_WriteOperand( PKA_COMPARISON_IN_OP2, 8, HW_PKA_P256_gfp );
121
122 /* Start PKA hardware */
123 HW_PKA_Start( LL_PKA_MODE_COMPARISON );
124 }
125
126 /*****************************************************************************/
127
HW_PKA_P256_IsRangeCheckOk(void)128 uint32_t HW_PKA_P256_IsRangeCheckOk( void )
129 {
130 return (HW_PKA_ReadSingleOutput( PKA_COMPARISON_OUT_RESULT ) == 0x916AUL);
131 }
132
133 /*****************************************************************************/
134
HW_PKA_P256_StartPointCheck(const uint32_t * x,const uint32_t * y)135 void HW_PKA_P256_StartPointCheck( const uint32_t* x,
136 const uint32_t* y )
137 {
138 /* Set the muber of bits of p */
139 HW_PKA_WriteSingleInput( PKA_POINT_CHECK_IN_MOD_NB_BITS, 256 );
140
141 /* Set the coefficient a sign */
142 HW_PKA_WriteSingleInput( PKA_POINT_CHECK_IN_A_COEFF_SIGN, 1 );
143
144 /* Set the coefficient |a| */
145 HW_PKA_WriteOperand( PKA_POINT_CHECK_IN_A_COEFF, 8, HW_PKA_P256_a );
146
147 /* Set the coefficient b */
148 HW_PKA_WriteOperand( PKA_POINT_CHECK_IN_B_COEFF, 8, HW_PKA_P256_b );
149
150 /* Set the modulus value p */
151 HW_PKA_WriteOperand( PKA_POINT_CHECK_IN_MOD_GF, 8, HW_PKA_P256_gfp );
152
153 /* Set the point coordinate x */
154 HW_PKA_WriteOperand( PKA_POINT_CHECK_IN_INITIAL_POINT_X, 8, x );
155
156 /* Set the point coordinate y */
157 HW_PKA_WriteOperand( PKA_POINT_CHECK_IN_INITIAL_POINT_Y, 8, y );
158
159 /* Set the Montgomery parameter */
160 HW_PKA_WriteOperand( PKA_POINT_CHECK_IN_MONTGOMERY_PARAM,
161 8, HW_PKA_P256_r2 );
162
163 /* Start PKA hardware */
164 HW_PKA_Start( LL_PKA_MODE_POINT_CHECK );
165 }
166
167 /*****************************************************************************/
168
HW_PKA_P256_IsPointCheckOk(void)169 uint32_t HW_PKA_P256_IsPointCheckOk( void )
170 {
171 return (HW_PKA_ReadSingleOutput( PKA_POINT_CHECK_OUT_ERROR ) == 0xD60DUL);
172 }
173
174 /*****************************************************************************/
175
HW_PKA_P256_StartEccScalarMul(const uint32_t * k,const uint32_t * p_x,const uint32_t * p_y)176 void HW_PKA_P256_StartEccScalarMul( const uint32_t* k,
177 const uint32_t* p_x,
178 const uint32_t* p_y )
179 {
180 /* Set the scalar multiplier k length */
181 HW_PKA_WriteSingleInput( PKA_ECC_SCALAR_MUL_IN_EXP_NB_BITS, 256 );
182
183 /* Set the modulus length */
184 HW_PKA_WriteSingleInput( PKA_ECC_SCALAR_MUL_IN_OP_NB_BITS, 256 );
185
186 /* Set the coefficient a sign */
187 HW_PKA_WriteSingleInput( PKA_ECC_SCALAR_MUL_IN_A_COEFF_SIGN, 1 );
188
189 /* Set the coefficient |a| */
190 HW_PKA_WriteOperand( PKA_ECC_SCALAR_MUL_IN_A_COEFF, 8, HW_PKA_P256_a );
191
192 /* Set the coefficient b */
193 HW_PKA_WriteOperand( PKA_ECC_SCALAR_MUL_IN_B_COEFF, 8, HW_PKA_P256_b );
194
195 /* Set the modulus value p */
196 HW_PKA_WriteOperand( PKA_ECC_SCALAR_MUL_IN_MOD_GF, 8, HW_PKA_P256_gfp );
197
198 /* Set the scalar multiplier k */
199 HW_PKA_WriteOperand( PKA_ECC_SCALAR_MUL_IN_K, 8, k );
200
201 /* Set the point P coordinate x */
202 HW_PKA_WriteOperand( PKA_ECC_SCALAR_MUL_IN_INITIAL_POINT_X,
203 8, p_x ? p_x : HW_PKA_P256_p_x );
204
205 /* Set the point P coordinate y */
206 HW_PKA_WriteOperand( PKA_ECC_SCALAR_MUL_IN_INITIAL_POINT_Y,
207 8, p_y ? p_y : HW_PKA_P256_p_y );
208
209 /* Set the prime order n */
210 HW_PKA_WriteOperand( PKA_ECC_SCALAR_MUL_IN_N_PRIME_ORDER,
211 8, HW_PKA_P256_n );
212
213 /* Start PKA hardware */
214 HW_PKA_Start( LL_PKA_MODE_ECC_MUL );
215 }
216
217 /*****************************************************************************/
218
HW_PKA_P256_ReadEccScalarMul(uint32_t * p_x,uint32_t * p_y)219 void HW_PKA_P256_ReadEccScalarMul( uint32_t* p_x,
220 uint32_t* p_y )
221 {
222 /* Read the output point X */
223 if ( p_x )
224 {
225 HW_PKA_ReadResult( PKA_ECC_SCALAR_MUL_OUT_RESULT_X, 8, p_x );
226 }
227
228 /* Read the output point Y as the second half of the result */
229 if ( p_y )
230 {
231 HW_PKA_ReadResult( PKA_ECC_SCALAR_MUL_OUT_RESULT_Y, 8, p_y );
232 }
233 }
234
235 /*****************************************************************************/
236