curve448.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. /* curve448.c
  2. *
  3. * Copyright (C) 2006-2021 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, 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. if (ret == 0) {
  150. if (endian == EC448_BIG_ENDIAN) {
  151. /* put shared secret key in Big Endian format */
  152. for (i = 0; i < CURVE448_PUB_KEY_SIZE; i++) {
  153. out[i] = o[CURVE448_PUB_KEY_SIZE - i -1];
  154. }
  155. }
  156. else {
  157. /* put shared secret key in Little Endian format */
  158. XMEMCPY(out, o, CURVE448_PUB_KEY_SIZE);
  159. }
  160. *outLen = CURVE448_PUB_KEY_SIZE;
  161. }
  162. ForceZero(o, CURVE448_PUB_KEY_SIZE);
  163. return ret;
  164. }
  165. #endif /* HAVE_CURVE448_SHARED_SECRET */
  166. #ifdef HAVE_CURVE448_KEY_EXPORT
  167. /* Export the curve448 public key.
  168. * Public key encoded big-endian.
  169. *
  170. * key [in] Curve448 public key.
  171. * out [in] Array to hold public key.
  172. * outLen [in/out] On in, the number of bytes in array.
  173. * On out, the number bytes put into array.
  174. * returns BAD_FUNC_ARG when a parameter is NULL,
  175. * ECC_BAD_ARG_E when outLen is less than CURVE448_PUB_KEY_SIZE,
  176. * 0 otherwise.
  177. */
  178. int wc_curve448_export_public(curve448_key* key, byte* out, word32* outLen)
  179. {
  180. return wc_curve448_export_public_ex(key, out, outLen, EC448_BIG_ENDIAN);
  181. }
  182. /* Export the curve448 public key.
  183. *
  184. * key [in] Curve448 public key.
  185. * out [in] Array to hold public key.
  186. * outLen [in/out] On in, the number of bytes in array.
  187. * On out, the number bytes put into array.
  188. * endian [in] Endianness to use when encoding number in array.
  189. * returns BAD_FUNC_ARG when a parameter is NULL,
  190. * ECC_BAD_ARG_E when outLen is less than CURVE448_PUB_KEY_SIZE,
  191. * 0 otherwise.
  192. */
  193. int wc_curve448_export_public_ex(curve448_key* key, byte* out, word32* outLen,
  194. int endian)
  195. {
  196. int ret = 0;
  197. int i;
  198. if ((key == NULL) || (out == NULL) || (outLen == NULL)) {
  199. ret = BAD_FUNC_ARG;
  200. }
  201. /* check and set outgoing key size */
  202. if ((ret == 0) && (*outLen < CURVE448_PUB_KEY_SIZE)) {
  203. *outLen = CURVE448_PUB_KEY_SIZE;
  204. ret = ECC_BAD_ARG_E;
  205. }
  206. if (ret == 0) {
  207. /* calculate public if missing */
  208. if (!key->pubSet) {
  209. ret = wc_curve448_make_pub((int)sizeof(key->p), key->p,
  210. (int)sizeof(key->k), key->k);
  211. key->pubSet = (ret == 0);
  212. }
  213. }
  214. if (ret == 0) {
  215. *outLen = CURVE448_PUB_KEY_SIZE;
  216. if (endian == EC448_BIG_ENDIAN) {
  217. /* read keys in Big Endian format */
  218. for (i = 0; i < CURVE448_PUB_KEY_SIZE; i++) {
  219. out[i] = key->p[CURVE448_PUB_KEY_SIZE - i - 1];
  220. }
  221. }
  222. else {
  223. XMEMCPY(out, key->p, CURVE448_PUB_KEY_SIZE);
  224. }
  225. }
  226. return ret;
  227. }
  228. #endif /* HAVE_CURVE448_KEY_EXPORT */
  229. #ifdef HAVE_CURVE448_KEY_IMPORT
  230. /* Import a curve448 public key from a byte array.
  231. * Public key encoded in big-endian.
  232. *
  233. * in [in] Array holding public key.
  234. * inLen [in] Number of bytes of data in array.
  235. * key [in] Curve448 public key.
  236. * returns BAD_FUNC_ARG when a parameter is NULL,
  237. * ECC_BAD_ARG_E when inLen is less than CURVE448_PUB_KEY_SIZE,
  238. * 0 otherwise.
  239. */
  240. int wc_curve448_import_public(const byte* in, word32 inLen, curve448_key* key)
  241. {
  242. return wc_curve448_import_public_ex(in, inLen, key, EC448_BIG_ENDIAN);
  243. }
  244. /* Import a curve448 public key from a byte array.
  245. *
  246. * in [in] Array holding public key.
  247. * inLen [in] Number of bytes of data in array.
  248. * key [in] Curve448 public key.
  249. * endian [in] Endianness of encoded number in byte array.
  250. * returns BAD_FUNC_ARG when a parameter is NULL,
  251. * ECC_BAD_ARG_E when inLen is less than CURVE448_PUB_KEY_SIZE,
  252. * 0 otherwise.
  253. */
  254. int wc_curve448_import_public_ex(const byte* in, word32 inLen,
  255. curve448_key* key, int endian)
  256. {
  257. int ret = 0;
  258. int i;
  259. /* sanity check */
  260. if ((key == NULL) || (in == NULL)) {
  261. ret = BAD_FUNC_ARG;
  262. }
  263. /* check size of incoming keys */
  264. if ((ret == 0) && (inLen != CURVE448_PUB_KEY_SIZE)) {
  265. ret = ECC_BAD_ARG_E;
  266. }
  267. if (ret == 0) {
  268. if (endian == EC448_BIG_ENDIAN) {
  269. /* read keys in Big Endian format */
  270. for (i = 0; i < CURVE448_PUB_KEY_SIZE; i++) {
  271. key->p[i] = in[CURVE448_PUB_KEY_SIZE - i - 1];
  272. }
  273. }
  274. else
  275. XMEMCPY(key->p, in, inLen);
  276. key->pubSet = 1;
  277. }
  278. return ret;
  279. }
  280. /* Check the public key value (big or little endian)
  281. *
  282. * pub [in] Public key bytes.
  283. * pubSz [in] Size of public key in bytes.
  284. * endian [in] Public key bytes passed in as big-endian or little-endian.
  285. * returns BAD_FUNC_ARGS when pub is NULL,
  286. * ECC_BAD_ARG_E when key length is not 56 bytes, public key value is
  287. * zero or one;
  288. * BUFFER_E when size of public key is zero;
  289. * 0 otherwise.
  290. */
  291. int wc_curve448_check_public(const byte* pub, word32 pubSz, int endian)
  292. {
  293. int ret = 0;
  294. word32 i;
  295. if (pub == NULL) {
  296. ret = BAD_FUNC_ARG;
  297. }
  298. /* Check for empty key data */
  299. if ((ret == 0) && (pubSz == 0)) {
  300. ret = BUFFER_E;
  301. }
  302. /* Check key length */
  303. if ((ret == 0) && (pubSz != CURVE448_PUB_KEY_SIZE)) {
  304. ret = ECC_BAD_ARG_E;
  305. }
  306. if (ret == 0) {
  307. if (endian == EC448_LITTLE_ENDIAN) {
  308. /* Check for value of zero or one */
  309. for (i = pubSz - 1; i > 0; i--) {
  310. if (pub[i] != 0) {
  311. break;
  312. }
  313. }
  314. if ((i == 0) && (pub[0] == 0 || pub[0] == 1)) {
  315. return ECC_BAD_ARG_E;
  316. }
  317. }
  318. else {
  319. /* Check for value of zero or one */
  320. for (i = 0; i < pubSz-1; i++) {
  321. if (pub[i] != 0) {
  322. break;
  323. }
  324. }
  325. if ((i == pubSz - 1) && (pub[i] == 0 || pub[i] == 1)) {
  326. ret = ECC_BAD_ARG_E;
  327. }
  328. }
  329. }
  330. return ret;
  331. }
  332. #endif /* HAVE_CURVE448_KEY_IMPORT */
  333. #ifdef HAVE_CURVE448_KEY_EXPORT
  334. /* Export the curve448 private key raw form.
  335. * Private key encoded big-endian.
  336. *
  337. * key [in] Curve448 private key.
  338. * out [in] Array to hold private key.
  339. * outLen [in/out] On in, the number of bytes in array.
  340. * On out, the number bytes put into array.
  341. * returns BAD_FUNC_ARG when a parameter is NULL,
  342. * ECC_BAD_ARG_E when outLen is less than CURVE448_KEY_SIZE,
  343. * 0 otherwise.
  344. */
  345. int wc_curve448_export_private_raw(curve448_key* key, byte* out, word32* outLen)
  346. {
  347. return wc_curve448_export_private_raw_ex(key, out, outLen,
  348. EC448_BIG_ENDIAN);
  349. }
  350. /* Export the curve448 private key raw form.
  351. *
  352. * key [in] Curve448 private key.
  353. * out [in] Array to hold private key.
  354. * outLen [in/out] On in, the number of bytes in array.
  355. * On out, the number bytes put into array.
  356. * endian [in] Endianness to use when encoding number in array.
  357. * returns BAD_FUNC_ARG when a parameter is NULL,
  358. * ECC_BAD_ARG_E when outLen is less than CURVE448_KEY_SIZE,
  359. * 0 otherwise.
  360. */
  361. int wc_curve448_export_private_raw_ex(curve448_key* key, byte* out,
  362. word32* outLen, int endian)
  363. {
  364. int ret = 0;
  365. int i;
  366. /* sanity check */
  367. if ((key == NULL) || (out == NULL) || (outLen == NULL)) {
  368. ret = BAD_FUNC_ARG;
  369. }
  370. /* check size of outgoing buffer */
  371. if ((ret == 0) && (*outLen < CURVE448_KEY_SIZE)) {
  372. *outLen = CURVE448_KEY_SIZE;
  373. ret = ECC_BAD_ARG_E;
  374. }
  375. if (ret == 0) {
  376. *outLen = CURVE448_KEY_SIZE;
  377. if (endian == EC448_BIG_ENDIAN) {
  378. /* put the key in Big Endian format */
  379. for (i = 0; i < CURVE448_KEY_SIZE; i++) {
  380. out[i] = key->k[CURVE448_KEY_SIZE - i - 1];
  381. }
  382. }
  383. else {
  384. XMEMCPY(out, key->k, CURVE448_KEY_SIZE);
  385. }
  386. }
  387. return ret;
  388. }
  389. /* Export the curve448 private and public keys in raw form.
  390. * Private and public key encoded big-endian.
  391. *
  392. * key [in] Curve448 private key.
  393. * priv [in] Array to hold private key.
  394. * privSz [in/out] On in, the number of bytes in private key array.
  395. * On out, the number bytes put into private key array.
  396. * pub [in] Array to hold public key.
  397. * pubSz [in/out] On in, the number of bytes in public key array.
  398. * On out, the number bytes put into public key array.
  399. * returns BAD_FUNC_ARG when a parameter is NULL,
  400. * ECC_BAD_ARG_E when privSz is less than CURVE448_KEY_SIZE or pubSz is
  401. * less than CURVE448_PUB_KEY_SIZE,
  402. * 0 otherwise.
  403. */
  404. int wc_curve448_export_key_raw(curve448_key* key, byte* priv, word32 *privSz,
  405. byte* pub, word32 *pubSz)
  406. {
  407. return wc_curve448_export_key_raw_ex(key, priv, privSz, pub, pubSz,
  408. EC448_BIG_ENDIAN);
  409. }
  410. /* Export the curve448 private and public keys in raw form.
  411. *
  412. * key [in] Curve448 private key.
  413. * priv [in] Array to hold private key.
  414. * privSz [in/out] On in, the number of bytes in private key array.
  415. * On out, the number bytes put into private key array.
  416. * pub [in] Array to hold public key.
  417. * pubSz [in/out] On in, the number of bytes in public key array.
  418. * On out, the number bytes put into public key array.
  419. * endian [in] Endianness to use when encoding number in array.
  420. * returns BAD_FUNC_ARG when a parameter is NULL,
  421. * ECC_BAD_ARG_E when privSz is less than CURVE448_KEY_SIZE or pubSz is
  422. * less than CURVE448_PUB_KEY_SIZE,
  423. * 0 otherwise.
  424. */
  425. int wc_curve448_export_key_raw_ex(curve448_key* key, byte* priv, word32 *privSz,
  426. byte* pub, word32 *pubSz, int endian)
  427. {
  428. int ret;
  429. /* export private part */
  430. ret = wc_curve448_export_private_raw_ex(key, priv, privSz, endian);
  431. if (ret == 0) {
  432. /* export public part */
  433. ret = wc_curve448_export_public_ex(key, pub, pubSz, endian);
  434. }
  435. return ret;
  436. }
  437. #endif /* HAVE_CURVE448_KEY_EXPORT */
  438. #ifdef HAVE_CURVE448_KEY_IMPORT
  439. /* Import curve448 private and public keys from a byte arrays.
  440. * Private and public keys encoded in big-endian.
  441. *
  442. * piv [in] Array holding private key.
  443. * privSz [in] Number of bytes of data in private key array.
  444. * pub [in] Array holding public key.
  445. * pubSz [in] Number of bytes of data in public key array.
  446. * key [in] Curve448 private/public key.
  447. * returns BAD_FUNC_ARG when a parameter is NULL,
  448. * ECC_BAD_ARG_E when privSz is less than CURVE448_KEY_SIZE or pubSz is
  449. * less than CURVE448_PUB_KEY_SIZE,
  450. * 0 otherwise.
  451. */
  452. int wc_curve448_import_private_raw(const byte* priv, word32 privSz,
  453. const byte* pub, word32 pubSz,
  454. curve448_key* key)
  455. {
  456. return wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz, key,
  457. EC448_BIG_ENDIAN);
  458. }
  459. /* Import curve448 private and public keys from a byte arrays.
  460. *
  461. * piv [in] Array holding private key.
  462. * privSz [in] Number of bytes of data in private key array.
  463. * pub [in] Array holding public key.
  464. * pubSz [in] Number of bytes of data in public key array.
  465. * key [in] Curve448 private/public key.
  466. * endian [in] Endianness of encoded numbers in byte arrays.
  467. * returns BAD_FUNC_ARG when a parameter is NULL,
  468. * ECC_BAD_ARG_E when privSz is less than CURVE448_KEY_SIZE or pubSz is
  469. * less than CURVE448_PUB_KEY_SIZE,
  470. * 0 otherwise.
  471. */
  472. int wc_curve448_import_private_raw_ex(const byte* priv, word32 privSz,
  473. const byte* pub, word32 pubSz,
  474. curve448_key* key, int endian)
  475. {
  476. int ret;
  477. /* import private part */
  478. ret = wc_curve448_import_private_ex(priv, privSz, key, endian);
  479. if (ret == 0) {
  480. /* import public part */
  481. return wc_curve448_import_public_ex(pub, pubSz, key, endian);
  482. }
  483. return ret;
  484. }
  485. /* Import curve448 private key from a byte array.
  486. * Private key encoded in big-endian.
  487. *
  488. * piv [in] Array holding private key.
  489. * privSz [in] Number of bytes of data in private key array.
  490. * key [in] Curve448 private/public key.
  491. * returns BAD_FUNC_ARG when a parameter is NULL,
  492. * ECC_BAD_ARG_E when privSz is less than CURVE448_KEY_SIZE,
  493. * 0 otherwise.
  494. */
  495. int wc_curve448_import_private(const byte* priv, word32 privSz,
  496. curve448_key* key)
  497. {
  498. return wc_curve448_import_private_ex(priv, privSz, key, EC448_BIG_ENDIAN);
  499. }
  500. /* Import curve448 private key from a byte array.
  501. *
  502. * piv [in] Array holding private key.
  503. * privSz [in] Number of bytes of data in private key array.
  504. * key [in] Curve448 private/public key.
  505. * endian [in] Endianness of encoded number in byte array.
  506. * returns BAD_FUNC_ARG when a parameter is NULL,
  507. * ECC_BAD_ARG_E when privSz is less than CURVE448_KEY_SIZE,
  508. * 0 otherwise.
  509. */
  510. int wc_curve448_import_private_ex(const byte* priv, word32 privSz,
  511. curve448_key* key, int endian)
  512. {
  513. int ret = 0;
  514. int i;
  515. /* sanity check */
  516. if ((key == NULL) || (priv == NULL)) {
  517. ret = BAD_FUNC_ARG;
  518. }
  519. /* check size of incoming keys */
  520. if ((ret == 0) && ((int)privSz != CURVE448_KEY_SIZE)) {
  521. ret = ECC_BAD_ARG_E;
  522. }
  523. if (ret == 0) {
  524. if (endian == EC448_BIG_ENDIAN) {
  525. /* read the key in Big Endian format */
  526. for (i = 0; i < CURVE448_KEY_SIZE; i++) {
  527. key->k[i] = priv[CURVE448_KEY_SIZE - i - 1];
  528. }
  529. }
  530. else {
  531. XMEMCPY(key->k, priv, CURVE448_KEY_SIZE);
  532. }
  533. /* Clamp the key */
  534. key->k[0] &= 0xfc;
  535. key->k[CURVE448_KEY_SIZE-1] |= 0x80;
  536. key->privSet = 1;
  537. }
  538. return ret;
  539. }
  540. #endif /* HAVE_CURVE448_KEY_IMPORT */
  541. /* Initialize the curve448 key.
  542. *
  543. * key [in] Curve448 key object.
  544. * returns BAD_FUNC_ARG when key is NULL,
  545. * 0 otherwise.
  546. */
  547. int wc_curve448_init(curve448_key* key)
  548. {
  549. int ret = 0;
  550. if (key == NULL) {
  551. ret = BAD_FUNC_ARG;
  552. }
  553. if (ret == 0) {
  554. XMEMSET(key, 0, sizeof(*key));
  555. fe448_init();
  556. }
  557. return ret;
  558. }
  559. /* Clears the curve448 key data.
  560. *
  561. * key [in] Curve448 key object.
  562. */
  563. void wc_curve448_free(curve448_key* key)
  564. {
  565. if (key != NULL) {
  566. ForceZero(key->k, sizeof(key->k));
  567. XMEMSET(key->p, 0, sizeof(key->p));
  568. key->pubSet = 0;
  569. key->privSet = 0;
  570. }
  571. }
  572. /* Get the curve448 key's size.
  573. *
  574. * key [in] Curve448 key object.
  575. * returns 0 if key is NULL,
  576. * CURVE448_KEY_SIZE otherwise.
  577. */
  578. int wc_curve448_size(curve448_key* key)
  579. {
  580. int ret = 0;
  581. if (key != NULL) {
  582. ret = CURVE448_KEY_SIZE;
  583. }
  584. return ret;
  585. }
  586. #endif /* HAVE_CURVE448 */