/src/wolfssl-sp-math-all/wolfcrypt/src/curve448.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* curve448.c |
2 | | * |
3 | | * Copyright (C) 2006-2025 wolfSSL Inc. |
4 | | * |
5 | | * This file is part of wolfSSL. |
6 | | * |
7 | | * wolfSSL is free software; you can redistribute it and/or modify |
8 | | * it under the terms of the GNU General Public License as published by |
9 | | * the Free Software Foundation; either version 3 of the License, or |
10 | | * (at your option) any later version. |
11 | | * |
12 | | * wolfSSL is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU General Public License |
18 | | * along with this program; if not, write to the Free Software |
19 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
20 | | */ |
21 | | |
22 | | /* Implemented to: RFC 7748 */ |
23 | | |
24 | | /* Based On Daniel J Bernstein's curve25519 Public Domain ref10 work. |
25 | | * Reworked for curve448 by Sean Parkinson. |
26 | | */ |
27 | | |
28 | | #include <wolfssl/wolfcrypt/libwolfssl_sources.h> |
29 | | |
30 | | #ifdef HAVE_CURVE448 |
31 | | |
32 | | #include <wolfssl/wolfcrypt/curve448.h> |
33 | | #ifdef NO_INLINE |
34 | | #include <wolfssl/wolfcrypt/misc.h> |
35 | | #else |
36 | | #define WOLFSSL_MISC_INCLUDED |
37 | | #include <wolfcrypt/src/misc.c> |
38 | | #endif |
39 | | |
40 | | int wc_curve448_make_pub(int public_size, byte* pub, int private_size, |
41 | | const byte* priv) |
42 | 798 | { |
43 | 798 | int ret; |
44 | 798 | unsigned char basepoint[CURVE448_KEY_SIZE] = {5}; |
45 | | |
46 | 798 | if ((pub == NULL) || (priv == NULL)) { |
47 | 0 | return ECC_BAD_ARG_E; |
48 | 0 | } |
49 | 798 | if ((public_size != CURVE448_PUB_KEY_SIZE) || |
50 | 798 | (private_size != CURVE448_KEY_SIZE)) { |
51 | 0 | return ECC_BAD_ARG_E; |
52 | 0 | } |
53 | | |
54 | 798 | fe448_init(); |
55 | | |
56 | | /* compute public key */ |
57 | 798 | ret = curve448(pub, priv, basepoint); |
58 | | |
59 | 798 | return ret; |
60 | 798 | } |
61 | | |
62 | | |
63 | | /* Make a new curve448 private/public key. |
64 | | * |
65 | | * rng [in] Random number generator. |
66 | | * keysize [in] Size of the key to generate. |
67 | | * key [in] Curve448 key object. |
68 | | * returns BAD_FUNC_ARG when rng or key are NULL, |
69 | | * ECC_BAD_ARG_E when keysize is not CURVE448_KEY_SIZE, |
70 | | * 0 otherwise. |
71 | | */ |
72 | | int wc_curve448_make_key(WC_RNG* rng, int keysize, curve448_key* key) |
73 | 870 | { |
74 | 870 | int ret = 0; |
75 | | |
76 | 870 | if ((key == NULL) || (rng == NULL)) { |
77 | 0 | ret = BAD_FUNC_ARG; |
78 | 0 | } |
79 | | |
80 | | /* currently only a key size of 56 bytes is used */ |
81 | 870 | if ((ret == 0) && (keysize != CURVE448_KEY_SIZE)) { |
82 | 0 | ret = ECC_BAD_ARG_E; |
83 | 0 | } |
84 | | |
85 | 870 | if (ret == 0) { |
86 | | /* random number for private key */ |
87 | 870 | ret = wc_RNG_GenerateBlock(rng, key->k, (word32)keysize); |
88 | 870 | } |
89 | 870 | if (ret == 0) { |
90 | 798 | key->privSet = 1; |
91 | | |
92 | | /* clamp private */ |
93 | 798 | key->k[0] &= 0xfc; |
94 | 798 | key->k[CURVE448_KEY_SIZE-1] |= 0x80; |
95 | | |
96 | | /* compute public */ |
97 | 798 | ret = wc_curve448_make_pub((int)sizeof(key->p), key->p, |
98 | 798 | (int)sizeof(key->k), key->k); |
99 | 798 | if (ret == 0) { |
100 | 798 | key->pubSet = 1; |
101 | 798 | } |
102 | 0 | else { |
103 | 0 | ForceZero(key->k, sizeof(key->k)); |
104 | 0 | XMEMSET(key->p, 0, sizeof(key->p)); |
105 | 0 | } |
106 | 798 | } |
107 | | |
108 | 870 | return ret; |
109 | 870 | } |
110 | | |
111 | | #ifdef HAVE_CURVE448_SHARED_SECRET |
112 | | |
113 | | /* Calculate the shared secret from the private key and peer's public key. |
114 | | * Calculation over curve448. |
115 | | * Secret encoded big-endian. |
116 | | * |
117 | | * private_key [in] Curve448 private key. |
118 | | * public_key [in] Curve448 public key. |
119 | | * out [in] Array to hold shared secret. |
120 | | * outLen [in/out] On in, the number of bytes in array. |
121 | | * On out, the number bytes put into array. |
122 | | * returns BAD_FUNC_ARG when a parameter is NULL or outLen is less than |
123 | | * CURVE448_KEY_SIZE, |
124 | | * 0 otherwise. |
125 | | */ |
126 | | int wc_curve448_shared_secret(curve448_key* private_key, |
127 | | curve448_key* public_key, |
128 | | byte* out, word32* outLen) |
129 | 0 | { |
130 | 0 | return wc_curve448_shared_secret_ex(private_key, public_key, out, outLen, |
131 | 0 | EC448_BIG_ENDIAN); |
132 | 0 | } |
133 | | |
134 | | /* Calculate the shared secret from the private key and peer's public key. |
135 | | * Calculation over curve448. |
136 | | * |
137 | | * private_key [in] Curve448 private key. |
138 | | * public_key [in] Curve448 public key. |
139 | | * out [in] Array to hold shared secret. |
140 | | * outLen [in/out] On in, the number of bytes in array. |
141 | | * On out, the number bytes put into array. |
142 | | * endian [in] Endianness to use when encoding number in array. |
143 | | * returns BAD_FUNC_ARG when a parameter is NULL or outLen is less than |
144 | | * CURVE448_PUB_KEY_SIZE, |
145 | | * 0 otherwise. |
146 | | */ |
147 | | int wc_curve448_shared_secret_ex(curve448_key* private_key, |
148 | | curve448_key* public_key, |
149 | | byte* out, word32* outLen, int endian) |
150 | 38 | { |
151 | 38 | unsigned char o[CURVE448_PUB_KEY_SIZE]; |
152 | 38 | int ret = 0; |
153 | 38 | int i; |
154 | | |
155 | | /* sanity check */ |
156 | 38 | if ((private_key == NULL) || (public_key == NULL) || (out == NULL) || |
157 | 38 | (outLen == NULL) || (*outLen < CURVE448_PUB_KEY_SIZE)) { |
158 | 0 | ret = BAD_FUNC_ARG; |
159 | 0 | } |
160 | | /* make sure we have a populated private and public key */ |
161 | 38 | if (ret == 0 && (!private_key->privSet || !public_key->pubSet)) { |
162 | 0 | ret = ECC_BAD_ARG_E; |
163 | 0 | } |
164 | | |
165 | 38 | if (ret == 0) { |
166 | 38 | ret = curve448(o, private_key->k, public_key->p); |
167 | 38 | } |
168 | | #ifdef WOLFSSL_ECDHX_SHARED_NOT_ZERO |
169 | | if (ret == 0) { |
170 | | byte t = 0; |
171 | | for (i = 0; i < CURVE448_PUB_KEY_SIZE; i++) { |
172 | | t |= o[i]; |
173 | | } |
174 | | if (t == 0) { |
175 | | ret = ECC_OUT_OF_RANGE_E; |
176 | | } |
177 | | } |
178 | | #endif |
179 | 38 | if (ret == 0) { |
180 | 38 | if (endian == EC448_BIG_ENDIAN) { |
181 | | /* put shared secret key in Big Endian format */ |
182 | 0 | for (i = 0; i < CURVE448_PUB_KEY_SIZE; i++) { |
183 | 0 | out[i] = o[CURVE448_PUB_KEY_SIZE - i -1]; |
184 | 0 | } |
185 | 0 | } |
186 | 38 | else { |
187 | | /* put shared secret key in Little Endian format */ |
188 | 38 | XMEMCPY(out, o, CURVE448_PUB_KEY_SIZE); |
189 | 38 | } |
190 | | |
191 | 38 | *outLen = CURVE448_PUB_KEY_SIZE; |
192 | 38 | } |
193 | | |
194 | 38 | ForceZero(o, CURVE448_PUB_KEY_SIZE); |
195 | | |
196 | 38 | return ret; |
197 | 38 | } |
198 | | |
199 | | #endif /* HAVE_CURVE448_SHARED_SECRET */ |
200 | | |
201 | | #ifdef HAVE_CURVE448_KEY_EXPORT |
202 | | |
203 | | /* Export the curve448 public key. |
204 | | * Public key encoded big-endian. |
205 | | * |
206 | | * key [in] Curve448 public key. |
207 | | * out [in] Array to hold public key. |
208 | | * outLen [in/out] On in, the number of bytes in array. |
209 | | * On out, the number bytes put into array. |
210 | | * returns BAD_FUNC_ARG when a parameter is NULL, |
211 | | * ECC_BAD_ARG_E when outLen is less than CURVE448_PUB_KEY_SIZE, |
212 | | * 0 otherwise. |
213 | | */ |
214 | | int wc_curve448_export_public(curve448_key* key, byte* out, word32* outLen) |
215 | 1 | { |
216 | 1 | return wc_curve448_export_public_ex(key, out, outLen, EC448_BIG_ENDIAN); |
217 | 1 | } |
218 | | |
219 | | /* Export the curve448 public key. |
220 | | * |
221 | | * key [in] Curve448 public key. |
222 | | * out [in] Array to hold public key. |
223 | | * outLen [in/out] On in, the number of bytes in array. |
224 | | * On out, the number bytes put into array. |
225 | | * endian [in] Endianness to use when encoding number in array. |
226 | | * returns BAD_FUNC_ARG when a parameter is NULL, |
227 | | * ECC_BAD_ARG_E when outLen is less than CURVE448_PUB_KEY_SIZE, |
228 | | * 0 otherwise. |
229 | | */ |
230 | | int wc_curve448_export_public_ex(curve448_key* key, byte* out, word32* outLen, |
231 | | int endian) |
232 | 669 | { |
233 | 669 | int ret = 0; |
234 | | |
235 | 669 | if ((key == NULL) || (out == NULL) || (outLen == NULL)) { |
236 | 20 | ret = BAD_FUNC_ARG; |
237 | 20 | } |
238 | | |
239 | | /* check and set outgoing key size */ |
240 | 669 | if ((ret == 0) && (*outLen < CURVE448_PUB_KEY_SIZE)) { |
241 | 6 | *outLen = CURVE448_PUB_KEY_SIZE; |
242 | 6 | ret = ECC_BAD_ARG_E; |
243 | 6 | } |
244 | 669 | if (ret == 0) { |
245 | | /* calculate public if missing */ |
246 | 643 | if (!key->pubSet) { |
247 | 0 | ret = wc_curve448_make_pub((int)sizeof(key->p), key->p, |
248 | 0 | (int)sizeof(key->k), key->k); |
249 | 0 | key->pubSet = (ret == 0); |
250 | 0 | } |
251 | 643 | } |
252 | 669 | if (ret == 0) { |
253 | 643 | *outLen = CURVE448_PUB_KEY_SIZE; |
254 | 643 | if (endian == EC448_BIG_ENDIAN) { |
255 | 1 | int i; |
256 | | /* read keys in Big Endian format */ |
257 | 57 | for (i = 0; i < CURVE448_PUB_KEY_SIZE; i++) { |
258 | 56 | out[i] = key->p[CURVE448_PUB_KEY_SIZE - i - 1]; |
259 | 56 | } |
260 | 1 | } |
261 | 642 | else { |
262 | 642 | XMEMCPY(out, key->p, CURVE448_PUB_KEY_SIZE); |
263 | 642 | } |
264 | 643 | } |
265 | | |
266 | 669 | return ret; |
267 | 669 | } |
268 | | |
269 | | #endif /* HAVE_CURVE448_KEY_EXPORT */ |
270 | | |
271 | | #ifdef HAVE_CURVE448_KEY_IMPORT |
272 | | |
273 | | /* Import a curve448 public key from a byte array. |
274 | | * Public key encoded in big-endian. |
275 | | * |
276 | | * in [in] Array holding public key. |
277 | | * inLen [in] Number of bytes of data in array. |
278 | | * key [in] Curve448 public key. |
279 | | * returns BAD_FUNC_ARG when a parameter is NULL, |
280 | | * ECC_BAD_ARG_E when inLen is less than CURVE448_PUB_KEY_SIZE, |
281 | | * 0 otherwise. |
282 | | */ |
283 | | int wc_curve448_import_public(const byte* in, word32 inLen, curve448_key* key) |
284 | 27 | { |
285 | 27 | return wc_curve448_import_public_ex(in, inLen, key, EC448_BIG_ENDIAN); |
286 | 27 | } |
287 | | |
288 | | /* Import a curve448 public key from a byte array. |
289 | | * |
290 | | * in [in] Array holding public key. |
291 | | * inLen [in] Number of bytes of data in array. |
292 | | * key [in] Curve448 public key. |
293 | | * endian [in] Endianness of encoded number in byte array. |
294 | | * returns BAD_FUNC_ARG when a parameter is NULL, |
295 | | * ECC_BAD_ARG_E when inLen is less than CURVE448_PUB_KEY_SIZE, |
296 | | * 0 otherwise. |
297 | | */ |
298 | | int wc_curve448_import_public_ex(const byte* in, word32 inLen, |
299 | | curve448_key* key, int endian) |
300 | 159 | { |
301 | 159 | int ret = 0; |
302 | | |
303 | | /* sanity check */ |
304 | 159 | if ((key == NULL) || (in == NULL)) { |
305 | 1 | ret = BAD_FUNC_ARG; |
306 | 1 | } |
307 | | |
308 | | /* check size of incoming keys */ |
309 | 159 | if ((ret == 0) && (inLen != CURVE448_PUB_KEY_SIZE)) { |
310 | 26 | ret = ECC_BAD_ARG_E; |
311 | 26 | } |
312 | | |
313 | 159 | if (ret == 0) { |
314 | 132 | if (endian == EC448_BIG_ENDIAN) { |
315 | 1 | int i; |
316 | | /* read keys in Big Endian format */ |
317 | 57 | for (i = 0; i < CURVE448_PUB_KEY_SIZE; i++) { |
318 | 56 | key->p[i] = in[CURVE448_PUB_KEY_SIZE - i - 1]; |
319 | 56 | } |
320 | 1 | } |
321 | 131 | else |
322 | 131 | XMEMCPY(key->p, in, inLen); |
323 | 132 | key->pubSet = 1; |
324 | 132 | } |
325 | | |
326 | 159 | return ret; |
327 | 159 | } |
328 | | |
329 | | /* Check the public key value (big or little endian) |
330 | | * |
331 | | * pub [in] Public key bytes. |
332 | | * pubSz [in] Size of public key in bytes. |
333 | | * endian [in] Public key bytes passed in as big-endian or little-endian. |
334 | | * returns BAD_FUNC_ARGS when pub is NULL, |
335 | | * ECC_BAD_ARG_E when key length is not 56 bytes, public key value is |
336 | | * zero or one; |
337 | | * BUFFER_E when size of public key is zero; |
338 | | * 0 otherwise. |
339 | | */ |
340 | | int wc_curve448_check_public(const byte* pub, word32 pubSz, int endian) |
341 | 632 | { |
342 | 632 | int ret = 0; |
343 | | |
344 | 632 | if (pub == NULL) { |
345 | 0 | ret = BAD_FUNC_ARG; |
346 | 0 | } |
347 | | |
348 | | /* Check for empty key data */ |
349 | 632 | if ((ret == 0) && (pubSz == 0)) { |
350 | 7 | ret = BUFFER_E; |
351 | 7 | } |
352 | | |
353 | | /* Check key length */ |
354 | 632 | if ((ret == 0) && (pubSz != CURVE448_PUB_KEY_SIZE)) { |
355 | 90 | ret = ECC_BAD_ARG_E; |
356 | 90 | } |
357 | | |
358 | 632 | if (ret == 0) { |
359 | 535 | word32 i; |
360 | | |
361 | 535 | if (endian == EC448_LITTLE_ENDIAN) { |
362 | | /* Check for value of zero or one */ |
363 | 1.61k | for (i = CURVE448_PUB_KEY_SIZE - 1; i > 0; i--) { |
364 | 1.59k | if (pub[i] != 0) { |
365 | 200 | break; |
366 | 200 | } |
367 | 1.59k | } |
368 | 220 | if ((i == 0) && (pub[0] == 0 || pub[0] == 1)) { |
369 | 8 | return ECC_BAD_ARG_E; |
370 | 8 | } |
371 | | /* Check for order-1 or higher */ |
372 | 3.06k | for (i = CURVE448_PUB_KEY_SIZE - 1; i > 28; i--) { |
373 | 2.97k | if (pub[i] != 0xff) { |
374 | 120 | break; |
375 | 120 | } |
376 | 2.97k | } |
377 | 212 | if ((i == 28) && (pub[i] == 0xff)) { |
378 | 5 | return ECC_BAD_ARG_E; |
379 | 5 | } |
380 | 207 | if ((i == 28) && (pub[i] == 0xfe)) { |
381 | 660 | for (--i; i > 0; i--) { |
382 | 644 | if (pub[i] != 0xff) { |
383 | 49 | break; |
384 | 49 | } |
385 | 644 | } |
386 | 65 | if ((i == 0) && (pub[i] >= 0xfe)) { |
387 | 4 | return ECC_BAD_ARG_E; |
388 | 4 | } |
389 | 65 | } |
390 | 207 | } |
391 | 315 | else { |
392 | | /* Check for value of zero or one */ |
393 | 1.21k | for (i = 0; i < CURVE448_PUB_KEY_SIZE-1; i++) { |
394 | 1.20k | if (pub[i] != 0) { |
395 | 304 | break; |
396 | 304 | } |
397 | 1.20k | } |
398 | 315 | if ((i == CURVE448_PUB_KEY_SIZE - 1) && |
399 | 315 | (pub[i] == 0 || pub[i] == 1)) { |
400 | 2 | ret = ECC_BAD_ARG_E; |
401 | 2 | } |
402 | | /* Check for order-1 or higher */ |
403 | 1.03k | for (i = 0; i < 27; i++) { |
404 | 1.01k | if (pub[i] != 0xff) { |
405 | 294 | break; |
406 | 294 | } |
407 | 1.01k | } |
408 | 315 | if ((i == 27) && (pub[i] == 0xff)) { |
409 | 1 | return ECC_BAD_ARG_E; |
410 | 1 | } |
411 | 314 | if ((i == 27) && (pub[i] == 0xfe)) { |
412 | 15 | for (++i; i < CURVE448_PUB_KEY_SIZE - 1; i--) { |
413 | 15 | if (pub[i] != 0xff) { |
414 | 13 | break; |
415 | 13 | } |
416 | 15 | } |
417 | 13 | if ((i == CURVE448_PUB_KEY_SIZE) && (pub[i] >= 0xfe)) { |
418 | 0 | return ECC_BAD_ARG_E; |
419 | 0 | } |
420 | 13 | } |
421 | 314 | } |
422 | 535 | } |
423 | | |
424 | 614 | return ret; |
425 | 632 | } |
426 | | |
427 | | #endif /* HAVE_CURVE448_KEY_IMPORT */ |
428 | | |
429 | | |
430 | | #ifdef HAVE_CURVE448_KEY_EXPORT |
431 | | |
432 | | /* Export the curve448 private key raw form. |
433 | | * Private key encoded big-endian. |
434 | | * |
435 | | * key [in] Curve448 private key. |
436 | | * out [in] Array to hold private key. |
437 | | * outLen [in/out] On in, the number of bytes in array. |
438 | | * On out, the number bytes put into array. |
439 | | * returns BAD_FUNC_ARG when a parameter is NULL, |
440 | | * ECC_BAD_ARG_E when outLen is less than CURVE448_KEY_SIZE, |
441 | | * 0 otherwise. |
442 | | */ |
443 | | int wc_curve448_export_private_raw(curve448_key* key, byte* out, word32* outLen) |
444 | 0 | { |
445 | 0 | return wc_curve448_export_private_raw_ex(key, out, outLen, |
446 | 0 | EC448_BIG_ENDIAN); |
447 | 0 | } |
448 | | |
449 | | /* Export the curve448 private key raw form. |
450 | | * |
451 | | * key [in] Curve448 private key. |
452 | | * out [in] Array to hold private key. |
453 | | * outLen [in/out] On in, the number of bytes in array. |
454 | | * On out, the number bytes put into array. |
455 | | * endian [in] Endianness to use when encoding number in array. |
456 | | * returns BAD_FUNC_ARG when a parameter is NULL, |
457 | | * ECC_BAD_ARG_E when outLen is less than CURVE448_KEY_SIZE, |
458 | | * 0 otherwise. |
459 | | */ |
460 | | int wc_curve448_export_private_raw_ex(curve448_key* key, byte* out, |
461 | | word32* outLen, int endian) |
462 | 239 | { |
463 | 239 | int ret = 0; |
464 | | |
465 | | /* sanity check */ |
466 | 239 | if ((key == NULL) || (out == NULL) || (outLen == NULL)) { |
467 | 68 | ret = BAD_FUNC_ARG; |
468 | 68 | } |
469 | | |
470 | | /* check size of outgoing buffer */ |
471 | 239 | if ((ret == 0) && (*outLen < CURVE448_KEY_SIZE)) { |
472 | 18 | *outLen = CURVE448_KEY_SIZE; |
473 | 18 | ret = ECC_BAD_ARG_E; |
474 | 18 | } |
475 | 239 | if (ret == 0) { |
476 | 153 | *outLen = CURVE448_KEY_SIZE; |
477 | | |
478 | 153 | if (endian == EC448_BIG_ENDIAN) { |
479 | 0 | int i; |
480 | | /* put the key in Big Endian format */ |
481 | 0 | for (i = 0; i < CURVE448_KEY_SIZE; i++) { |
482 | 0 | out[i] = key->k[CURVE448_KEY_SIZE - i - 1]; |
483 | 0 | } |
484 | 0 | } |
485 | 153 | else { |
486 | 153 | XMEMCPY(out, key->k, CURVE448_KEY_SIZE); |
487 | 153 | } |
488 | 153 | } |
489 | | |
490 | 239 | return ret; |
491 | 239 | } |
492 | | |
493 | | /* Export the curve448 private and public keys in raw form. |
494 | | * Private and public key encoded big-endian. |
495 | | * |
496 | | * key [in] Curve448 private key. |
497 | | * priv [in] Array to hold private key. |
498 | | * privSz [in/out] On in, the number of bytes in private key array. |
499 | | * On out, the number bytes put into private key array. |
500 | | * pub [in] Array to hold public key. |
501 | | * pubSz [in/out] On in, the number of bytes in public key array. |
502 | | * On out, the number bytes put into public key array. |
503 | | * returns BAD_FUNC_ARG when a parameter is NULL, |
504 | | * ECC_BAD_ARG_E when privSz is less than CURVE448_KEY_SIZE or pubSz is |
505 | | * less than CURVE448_PUB_KEY_SIZE, |
506 | | * 0 otherwise. |
507 | | */ |
508 | | int wc_curve448_export_key_raw(curve448_key* key, byte* priv, word32 *privSz, |
509 | | byte* pub, word32 *pubSz) |
510 | 0 | { |
511 | 0 | return wc_curve448_export_key_raw_ex(key, priv, privSz, pub, pubSz, |
512 | 0 | EC448_BIG_ENDIAN); |
513 | 0 | } |
514 | | |
515 | | /* Export the curve448 private and public keys in raw form. |
516 | | * |
517 | | * key [in] Curve448 private key. |
518 | | * priv [in] Array to hold private key. |
519 | | * privSz [in/out] On in, the number of bytes in private key array. |
520 | | * On out, the number bytes put into private key array. |
521 | | * pub [in] Array to hold public key. |
522 | | * pubSz [in/out] On in, the number of bytes in public key array. |
523 | | * On out, the number bytes put into public key array. |
524 | | * endian [in] Endianness to use when encoding number in array. |
525 | | * returns BAD_FUNC_ARG when a parameter is NULL, |
526 | | * ECC_BAD_ARG_E when privSz is less than CURVE448_KEY_SIZE or pubSz is |
527 | | * less than CURVE448_PUB_KEY_SIZE, |
528 | | * 0 otherwise. |
529 | | */ |
530 | | int wc_curve448_export_key_raw_ex(curve448_key* key, byte* priv, word32 *privSz, |
531 | | byte* pub, word32 *pubSz, int endian) |
532 | 0 | { |
533 | 0 | int ret; |
534 | | |
535 | | /* export private part */ |
536 | 0 | ret = wc_curve448_export_private_raw_ex(key, priv, privSz, endian); |
537 | 0 | if (ret == 0) { |
538 | | /* export public part */ |
539 | 0 | ret = wc_curve448_export_public_ex(key, pub, pubSz, endian); |
540 | 0 | } |
541 | |
|
542 | 0 | return ret; |
543 | 0 | } |
544 | | |
545 | | #endif /* HAVE_CURVE448_KEY_EXPORT */ |
546 | | |
547 | | #ifdef HAVE_CURVE448_KEY_IMPORT |
548 | | |
549 | | /* Import curve448 private and public keys from a byte arrays. |
550 | | * Private and public keys encoded in big-endian. |
551 | | * |
552 | | * piv [in] Array holding private key. |
553 | | * privSz [in] Number of bytes of data in private key array. |
554 | | * pub [in] Array holding public key. |
555 | | * pubSz [in] Number of bytes of data in public key array. |
556 | | * key [in] Curve448 private/public key. |
557 | | * returns BAD_FUNC_ARG when a parameter is NULL, |
558 | | * ECC_BAD_ARG_E when privSz is less than CURVE448_KEY_SIZE or pubSz is |
559 | | * less than CURVE448_PUB_KEY_SIZE, |
560 | | * 0 otherwise. |
561 | | */ |
562 | | int wc_curve448_import_private_raw(const byte* priv, word32 privSz, |
563 | | const byte* pub, word32 pubSz, |
564 | | curve448_key* key) |
565 | 4 | { |
566 | 4 | return wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz, key, |
567 | 4 | EC448_BIG_ENDIAN); |
568 | 4 | } |
569 | | |
570 | | /* Import curve448 private and public keys from a byte arrays. |
571 | | * |
572 | | * piv [in] Array holding private key. |
573 | | * privSz [in] Number of bytes of data in private key array. |
574 | | * pub [in] Array holding public key. |
575 | | * pubSz [in] Number of bytes of data in public key array. |
576 | | * key [in] Curve448 private/public key. |
577 | | * endian [in] Endianness of encoded numbers in byte arrays. |
578 | | * returns BAD_FUNC_ARG when a parameter is NULL, |
579 | | * ECC_BAD_ARG_E when privSz is less than CURVE448_KEY_SIZE or pubSz is |
580 | | * less than CURVE448_PUB_KEY_SIZE, |
581 | | * 0 otherwise. |
582 | | */ |
583 | | int wc_curve448_import_private_raw_ex(const byte* priv, word32 privSz, |
584 | | const byte* pub, word32 pubSz, |
585 | | curve448_key* key, int endian) |
586 | 4 | { |
587 | 4 | int ret; |
588 | | |
589 | | /* import private part */ |
590 | 4 | ret = wc_curve448_import_private_ex(priv, privSz, key, endian); |
591 | 4 | if (ret == 0) { |
592 | | /* import public part */ |
593 | 1 | return wc_curve448_import_public_ex(pub, pubSz, key, endian); |
594 | 1 | } |
595 | | |
596 | 3 | return ret; |
597 | 4 | } |
598 | | |
599 | | /* Import curve448 private key from a byte array. |
600 | | * Private key encoded in big-endian. |
601 | | * |
602 | | * piv [in] Array holding private key. |
603 | | * privSz [in] Number of bytes of data in private key array. |
604 | | * key [in] Curve448 private/public key. |
605 | | * returns BAD_FUNC_ARG when a parameter is NULL, |
606 | | * ECC_BAD_ARG_E when privSz is less than CURVE448_KEY_SIZE, |
607 | | * 0 otherwise. |
608 | | */ |
609 | | int wc_curve448_import_private(const byte* priv, word32 privSz, |
610 | | curve448_key* key) |
611 | 0 | { |
612 | 0 | return wc_curve448_import_private_ex(priv, privSz, key, EC448_BIG_ENDIAN); |
613 | 0 | } |
614 | | |
615 | | /* Import curve448 private key from a byte array. |
616 | | * |
617 | | * piv [in] Array holding private key. |
618 | | * privSz [in] Number of bytes of data in private key array. |
619 | | * key [in] Curve448 private/public key. |
620 | | * endian [in] Endianness of encoded number in byte array. |
621 | | * returns BAD_FUNC_ARG when a parameter is NULL, |
622 | | * ECC_BAD_ARG_E when privSz is less than CURVE448_KEY_SIZE, |
623 | | * 0 otherwise. |
624 | | */ |
625 | | int wc_curve448_import_private_ex(const byte* priv, word32 privSz, |
626 | | curve448_key* key, int endian) |
627 | 4 | { |
628 | 4 | int ret = 0; |
629 | | |
630 | | /* sanity check */ |
631 | 4 | if ((key == NULL) || (priv == NULL)) { |
632 | 0 | ret = BAD_FUNC_ARG; |
633 | 0 | } |
634 | | |
635 | | /* check size of incoming keys */ |
636 | 4 | if ((ret == 0) && ((int)privSz != CURVE448_KEY_SIZE)) { |
637 | 3 | ret = ECC_BAD_ARG_E; |
638 | 3 | } |
639 | | |
640 | 4 | if (ret == 0) { |
641 | 1 | if (endian == EC448_BIG_ENDIAN) { |
642 | 1 | int i; |
643 | | /* read the key in Big Endian format */ |
644 | 57 | for (i = 0; i < CURVE448_KEY_SIZE; i++) { |
645 | 56 | key->k[i] = priv[CURVE448_KEY_SIZE - i - 1]; |
646 | 56 | } |
647 | 1 | } |
648 | 0 | else { |
649 | 0 | XMEMCPY(key->k, priv, CURVE448_KEY_SIZE); |
650 | 0 | } |
651 | | |
652 | | /* Clamp the key */ |
653 | 1 | key->k[0] &= 0xfc; |
654 | 1 | key->k[CURVE448_KEY_SIZE-1] |= 0x80; |
655 | | |
656 | 1 | key->privSet = 1; |
657 | 1 | } |
658 | | |
659 | 4 | return ret; |
660 | 4 | } |
661 | | |
662 | | #endif /* HAVE_CURVE448_KEY_IMPORT */ |
663 | | |
664 | | |
665 | | /* Initialize the curve448 key. |
666 | | * |
667 | | * key [in] Curve448 key object. |
668 | | * returns BAD_FUNC_ARG when key is NULL, |
669 | | * 0 otherwise. |
670 | | */ |
671 | | int wc_curve448_init(curve448_key* key) |
672 | 1.07k | { |
673 | 1.07k | int ret = 0; |
674 | | |
675 | 1.07k | if (key == NULL) { |
676 | 0 | ret = BAD_FUNC_ARG; |
677 | 0 | } |
678 | | |
679 | 1.07k | if (ret == 0) { |
680 | 1.07k | XMEMSET(key, 0, sizeof(*key)); |
681 | | |
682 | 1.07k | fe448_init(); |
683 | | |
684 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
685 | | wc_MemZero_Add("wc_curve448_init key->k", &key->k, CURVE448_KEY_SIZE); |
686 | | #endif |
687 | 1.07k | } |
688 | | |
689 | 1.07k | return ret; |
690 | 1.07k | } |
691 | | |
692 | | |
693 | | /* Clears the curve448 key data. |
694 | | * |
695 | | * key [in] Curve448 key object. |
696 | | */ |
697 | | void wc_curve448_free(curve448_key* key) |
698 | 1.13k | { |
699 | 1.13k | if (key != NULL) { |
700 | 1.07k | ForceZero(key->k, sizeof(key->k)); |
701 | 1.07k | XMEMSET(key->p, 0, sizeof(key->p)); |
702 | 1.07k | key->pubSet = 0; |
703 | 1.07k | key->privSet = 0; |
704 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
705 | | wc_MemZero_Check(key, sizeof(curve448_key)); |
706 | | #endif |
707 | 1.07k | } |
708 | 1.13k | } |
709 | | |
710 | | |
711 | | /* Get the curve448 key's size. |
712 | | * |
713 | | * key [in] Curve448 key object. |
714 | | * returns 0 if key is NULL, |
715 | | * CURVE448_KEY_SIZE otherwise. |
716 | | */ |
717 | | int wc_curve448_size(curve448_key* key) |
718 | 0 | { |
719 | 0 | int ret = 0; |
720 | |
|
721 | 0 | if (key != NULL) { |
722 | 0 | ret = CURVE448_KEY_SIZE; |
723 | 0 | } |
724 | |
|
725 | 0 | return ret; |
726 | 0 | } |
727 | | |
728 | | #endif /* HAVE_CURVE448 */ |
729 | | |