curve448.c 22 KB

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