falcon.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. /* falcon.c
  2. *
  3. * Copyright (C) 2006-2022 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. /* Based on ed448.c and Reworked for Falcon by Anthony Hu. */
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25. /* in case user set HAVE_PQC there */
  26. #include <wolfssl/wolfcrypt/settings.h>
  27. #include <wolfssl/wolfcrypt/asn.h>
  28. #if defined(HAVE_PQC) && defined(HAVE_FALCON)
  29. #ifdef HAVE_LIBOQS
  30. #include <oqs/oqs.h>
  31. #endif
  32. #include <wolfssl/wolfcrypt/falcon.h>
  33. #include <wolfssl/wolfcrypt/error-crypt.h>
  34. #ifdef NO_INLINE
  35. #include <wolfssl/wolfcrypt/misc.h>
  36. #else
  37. #define WOLFSSL_MISC_INCLUDED
  38. #include <wolfcrypt/src/misc.c>
  39. #endif
  40. /* Sign the message using the falcon private key.
  41. *
  42. * in [in] Message to sign.
  43. * inLen [in] Length of the message in bytes.
  44. * out [in] Buffer to write signature into.
  45. * outLen [in/out] On in, size of buffer.
  46. * On out, the length of the signature in bytes.
  47. * key [in] Falcon key to use when signing
  48. * returns BAD_FUNC_ARG when a parameter is NULL or public key not set,
  49. * BUFFER_E when outLen is less than FALCON_LEVEL1_SIG_SIZE,
  50. * 0 otherwise.
  51. */
  52. int wc_falcon_sign_msg(const byte* in, word32 inLen,
  53. byte* out, word32 *outLen,
  54. falcon_key* key)
  55. {
  56. int ret = 0;
  57. #ifdef HAVE_LIBOQS
  58. OQS_SIG *oqssig = NULL;
  59. size_t localOutLen = 0;
  60. /* sanity check on arguments */
  61. if ((in == NULL) || (out == NULL) || (outLen == NULL) || (key == NULL)) {
  62. ret = BAD_FUNC_ARG;
  63. }
  64. if ((ret == 0) && (!key->prvKeySet)) {
  65. ret = BAD_FUNC_ARG;
  66. }
  67. if (ret == 0) {
  68. if (key->level == 1) {
  69. oqssig = OQS_SIG_new(OQS_SIG_alg_falcon_512);
  70. }
  71. else if (key->level == 5) {
  72. oqssig = OQS_SIG_new(OQS_SIG_alg_falcon_1024);
  73. }
  74. if (oqssig == NULL) {
  75. ret = SIG_TYPE_E;
  76. }
  77. }
  78. /* check and set up out length */
  79. if (ret == 0) {
  80. if ((key->level == 1) && (*outLen < FALCON_LEVEL1_SIG_SIZE)) {
  81. *outLen = FALCON_LEVEL1_SIG_SIZE;
  82. ret = BUFFER_E;
  83. }
  84. else if ((key->level == 5) && (*outLen < FALCON_LEVEL5_SIG_SIZE)) {
  85. *outLen = FALCON_LEVEL5_SIG_SIZE;
  86. ret = BUFFER_E;
  87. }
  88. localOutLen = *outLen;
  89. }
  90. if ((ret == 0) &&
  91. (OQS_SIG_sign(oqssig, out, &localOutLen, in, inLen, key->k)
  92. == OQS_ERROR)) {
  93. ret = BAD_FUNC_ARG;
  94. }
  95. if (ret == 0) {
  96. *outLen = (word32)localOutLen;
  97. }
  98. if (oqssig != NULL) {
  99. OQS_SIG_free(oqssig);
  100. }
  101. #else
  102. ret = NOT_COMPILED_IN;
  103. #endif
  104. return ret;
  105. }
  106. /* Verify the message using the falcon public key.
  107. *
  108. * sig [in] Signature to verify.
  109. * sigLen [in] Size of signature in bytes.
  110. * msg [in] Message to verify.
  111. * msgLen [in] Length of the message in bytes.
  112. * res [out] *res is set to 1 on successful verification.
  113. * key [in] Falcon key to use to verify.
  114. * returns BAD_FUNC_ARG when a parameter is NULL or contextLen is zero when and
  115. * BUFFER_E when sigLen is less than FALCON_LEVEL1_SIG_SIZE,
  116. * 0 otherwise.
  117. */
  118. int wc_falcon_verify_msg(const byte* sig, word32 sigLen, const byte* msg,
  119. word32 msgLen, int* res, falcon_key* key)
  120. {
  121. int ret = 0;
  122. #ifdef HAVE_LIBOQS
  123. OQS_SIG *oqssig = NULL;
  124. if (key == NULL || sig == NULL || msg == NULL || res == NULL) {
  125. ret = BAD_FUNC_ARG;
  126. }
  127. if ((ret == 0) && (!key->pubKeySet)) {
  128. ret = BAD_FUNC_ARG;
  129. }
  130. if (ret == 0) {
  131. if (key->level == 1) {
  132. oqssig = OQS_SIG_new(OQS_SIG_alg_falcon_512);
  133. }
  134. else if (key->level == 5) {
  135. oqssig = OQS_SIG_new(OQS_SIG_alg_falcon_1024);
  136. }
  137. if (oqssig == NULL) {
  138. ret = SIG_TYPE_E;
  139. }
  140. }
  141. if ((ret == 0) &&
  142. (OQS_SIG_verify(oqssig, msg, msgLen, sig, sigLen, key->p)
  143. == OQS_ERROR)) {
  144. ret = SIG_VERIFY_E;
  145. }
  146. if (ret == 0) {
  147. *res = 1;
  148. }
  149. if (oqssig != NULL) {
  150. OQS_SIG_free(oqssig);
  151. }
  152. #else
  153. ret = NOT_COMPILED_IN;
  154. #endif
  155. return ret;
  156. }
  157. /* Initialize the falcon private/public key.
  158. *
  159. * key [in] Falcon key.
  160. * returns BAD_FUNC_ARG when key is NULL
  161. */
  162. int wc_falcon_init(falcon_key* key)
  163. {
  164. if (key == NULL) {
  165. return BAD_FUNC_ARG;
  166. }
  167. ForceZero(key, sizeof(key));
  168. return 0;
  169. }
  170. /* Set the level of the falcon private/public key.
  171. *
  172. * key [out] Falcon key.
  173. * level [in] Either 1 or 5.
  174. * returns BAD_FUNC_ARG when key is NULL or level is not 1 and not 5.
  175. */
  176. int wc_falcon_set_level(falcon_key* key, byte level)
  177. {
  178. if (key == NULL) {
  179. return BAD_FUNC_ARG;
  180. }
  181. if (level != 1 && level != 5) {
  182. return BAD_FUNC_ARG;
  183. }
  184. key->level = level;
  185. key->pubKeySet = 0;
  186. key->prvKeySet = 0;
  187. return 0;
  188. }
  189. /* Get the level of the falcon private/public key.
  190. *
  191. * key [in] Falcon key.
  192. * level [out] The level.
  193. * returns BAD_FUNC_ARG when key is NULL or level has not been set.
  194. */
  195. int wc_falcon_get_level(falcon_key* key, byte* level)
  196. {
  197. if (key == NULL || level == NULL) {
  198. return BAD_FUNC_ARG;
  199. }
  200. if (key->level != 1 && key->level != 5) {
  201. return BAD_FUNC_ARG;
  202. }
  203. *level = key->level;
  204. return 0;
  205. }
  206. /* Clears the falcon key data
  207. *
  208. * key [in] Falcon key.
  209. */
  210. void wc_falcon_free(falcon_key* key)
  211. {
  212. if (key != NULL) {
  213. ForceZero(key, sizeof(key));
  214. }
  215. }
  216. /* Export the falcon public key.
  217. *
  218. * key [in] Falcon public key.
  219. * out [in] Array to hold public key.
  220. * outLen [in/out] On in, the number of bytes in array.
  221. * On out, the number bytes put into array.
  222. * returns BAD_FUNC_ARG when a parameter is NULL,
  223. * BUFFER_E when outLen is less than FALCON_LEVEL1_PUB_KEY_SIZE,
  224. * 0 otherwise.
  225. */
  226. int wc_falcon_export_public(falcon_key* key,
  227. byte* out, word32* outLen)
  228. {
  229. /* sanity check on arguments */
  230. if ((key == NULL) || (out == NULL) || (outLen == NULL)) {
  231. return BAD_FUNC_ARG;
  232. }
  233. if ((key->level != 1) && (key->level != 5)) {
  234. return BAD_FUNC_ARG;
  235. }
  236. if (!key->pubKeySet) {
  237. return BAD_FUNC_ARG;
  238. }
  239. /* check and set up out length */
  240. if ((key->level == 1) && (*outLen < FALCON_LEVEL1_PUB_KEY_SIZE)) {
  241. *outLen = FALCON_LEVEL1_PUB_KEY_SIZE;
  242. return BUFFER_E;
  243. }
  244. else if ((key->level == 5) && (*outLen < FALCON_LEVEL5_PUB_KEY_SIZE)) {
  245. *outLen = FALCON_LEVEL5_PUB_KEY_SIZE;
  246. return BUFFER_E;
  247. }
  248. if (key->level == 1) {
  249. *outLen = FALCON_LEVEL1_PUB_KEY_SIZE;
  250. XMEMCPY(out, key->p, FALCON_LEVEL1_PUB_KEY_SIZE);
  251. }
  252. else if (key->level == 5) {
  253. *outLen = FALCON_LEVEL5_PUB_KEY_SIZE;
  254. XMEMCPY(out, key->p, FALCON_LEVEL5_PUB_KEY_SIZE);
  255. }
  256. return 0;
  257. }
  258. /* Import a falcon public key from a byte array.
  259. * Public key encoded in big-endian.
  260. *
  261. * in [in] Array holding public key.
  262. * inLen [in] Number of bytes of data in array.
  263. * key [in] Falcon public key.
  264. * returns BAD_FUNC_ARG when a parameter is NULL or key format is not supported,
  265. * 0 otherwise.
  266. */
  267. int wc_falcon_import_public(const byte* in, word32 inLen,
  268. falcon_key* key)
  269. {
  270. /* sanity check on arguments */
  271. if ((in == NULL) || (key == NULL)) {
  272. return BAD_FUNC_ARG;
  273. }
  274. if ((key->level != 1) && (key->level != 5)) {
  275. return BAD_FUNC_ARG;
  276. }
  277. if ((key->level == 1) && (inLen != FALCON_LEVEL1_PUB_KEY_SIZE)) {
  278. return BAD_FUNC_ARG;
  279. }
  280. else if ((key->level == 5) && (inLen != FALCON_LEVEL5_PUB_KEY_SIZE)) {
  281. return BAD_FUNC_ARG;
  282. }
  283. XMEMCPY(key->p, in, inLen);
  284. key->pubKeySet = 1;
  285. return 0;
  286. }
  287. static int parse_private_key(const byte* priv, word32 privSz,
  288. byte** out, word32 *outSz,
  289. falcon_key* key) {
  290. word32 idx = 0;
  291. int ret = 0;
  292. int length = 0;
  293. /* sanity check on arguments */
  294. if ((priv == NULL) || (key == NULL)) {
  295. return BAD_FUNC_ARG;
  296. }
  297. if ((key->level != 1) && (key->level != 5)) {
  298. return BAD_FUNC_ARG;
  299. }
  300. /* At this point, it is still a PKCS8 private key. */
  301. if ((ret = ToTraditionalInline(priv, &idx, privSz)) < 0) {
  302. return ret;
  303. }
  304. /* Now it is a octet_string(concat(priv,pub)) */
  305. if ((ret = GetOctetString(priv, &idx, &length, privSz)) < 0) {
  306. return ret;
  307. }
  308. *out = (byte *)priv + idx;
  309. *outSz = privSz - idx;
  310. /* And finally it is concat(priv,pub). Key size check. */
  311. if ((key->level == 1) && (*outSz != FALCON_LEVEL1_KEY_SIZE +
  312. FALCON_LEVEL1_PUB_KEY_SIZE)) {
  313. return BAD_FUNC_ARG;
  314. }
  315. else if ((key->level == 5) && (*outSz != FALCON_LEVEL5_KEY_SIZE +
  316. FALCON_LEVEL5_PUB_KEY_SIZE)) {
  317. return BAD_FUNC_ARG;
  318. }
  319. return 0;
  320. }
  321. /* Import a falcon private key from a byte array.
  322. *
  323. * priv [in] Array holding private key.
  324. * privSz [in] Number of bytes of data in array.
  325. * key [in] Falcon private key.
  326. * returns BAD_FUNC_ARG when a parameter is NULL or privSz is less than
  327. * FALCON_LEVEL1_KEY_SIZE,
  328. * 0 otherwise.
  329. */
  330. int wc_falcon_import_private_only(const byte* priv, word32 privSz,
  331. falcon_key* key)
  332. {
  333. int ret = 0;
  334. byte *newPriv = NULL;
  335. word32 newPrivSz = 0;
  336. if ((ret = parse_private_key(priv, privSz, &newPriv, &newPrivSz, key))
  337. != 0) {
  338. return ret;
  339. }
  340. if (key->level == 1) {
  341. XMEMCPY(key->k, newPriv, FALCON_LEVEL1_KEY_SIZE);
  342. }
  343. else if (key->level == 5) {
  344. XMEMCPY(key->k, newPriv, FALCON_LEVEL5_KEY_SIZE);
  345. }
  346. key->prvKeySet = 1;
  347. return 0;
  348. }
  349. /* Import a falcon private and public keys from byte array(s).
  350. *
  351. * priv [in] Array holding private key or private+public keys
  352. * privSz [in] Number of bytes of data in private key array.
  353. * pub [in] Array holding public key (or NULL).
  354. * pubSz [in] Number of bytes of data in public key array (or 0).
  355. * key [in] Falcon private/public key.
  356. * returns BAD_FUNC_ARG when a required parameter is NULL or an invalid
  357. * combination of keys/lengths is supplied, 0 otherwise.
  358. */
  359. int wc_falcon_import_private_key(const byte* priv, word32 privSz,
  360. const byte* pub, word32 pubSz,
  361. falcon_key* key)
  362. {
  363. int ret = 0;
  364. byte *newPriv = NULL;
  365. word32 newPrivSz = 0;
  366. if ((ret = parse_private_key(priv, privSz, &newPriv, &newPrivSz, key))
  367. != 0) {
  368. return ret;
  369. }
  370. if (pub == NULL) {
  371. if (pubSz != 0) {
  372. return BAD_FUNC_ARG;
  373. }
  374. if ((newPrivSz != FALCON_LEVEL1_PRV_KEY_SIZE) &&
  375. (newPrivSz != FALCON_LEVEL5_PRV_KEY_SIZE)) {
  376. return BAD_FUNC_ARG;
  377. }
  378. if (key->level == 1) {
  379. pub = newPriv + FALCON_LEVEL1_KEY_SIZE;
  380. pubSz = FALCON_LEVEL1_PUB_KEY_SIZE;
  381. }
  382. else if (key->level == 5) {
  383. pub = newPriv + FALCON_LEVEL5_KEY_SIZE;
  384. pubSz = FALCON_LEVEL5_PUB_KEY_SIZE;
  385. }
  386. }
  387. else if ((pubSz != FALCON_LEVEL1_PUB_KEY_SIZE) &&
  388. (pubSz != FALCON_LEVEL5_PUB_KEY_SIZE)) {
  389. return BAD_FUNC_ARG;
  390. }
  391. /* import public key */
  392. ret = wc_falcon_import_public(pub, pubSz, key);
  393. if (ret == 0) {
  394. /* make the private key (priv + pub) */
  395. if (key->level == 1) {
  396. XMEMCPY(key->k, newPriv, FALCON_LEVEL1_KEY_SIZE);
  397. }
  398. else if (key->level == 5) {
  399. XMEMCPY(key->k, newPriv, FALCON_LEVEL5_KEY_SIZE);
  400. }
  401. key->prvKeySet = 1;
  402. }
  403. return ret;
  404. }
  405. /* Export the falcon private key.
  406. *
  407. * key [in] Falcon private key.
  408. * out [in] Array to hold private key.
  409. * outLen [in/out] On in, the number of bytes in array.
  410. * On out, the number bytes put into array.
  411. * returns BAD_FUNC_ARG when a parameter is NULL,
  412. * BUFFER_E when outLen is less than FALCON_LEVEL1_KEY_SIZE,
  413. * 0 otherwise.
  414. */
  415. int wc_falcon_export_private_only(falcon_key* key, byte* out, word32* outLen)
  416. {
  417. /* sanity checks on arguments */
  418. if ((key == NULL) || (out == NULL) || (outLen == NULL)) {
  419. return BAD_FUNC_ARG;
  420. }
  421. if ((key->level != 1) && (key->level != 5)) {
  422. return BAD_FUNC_ARG;
  423. }
  424. /* check and set up out length */
  425. if ((key->level == 1) && (*outLen < FALCON_LEVEL1_KEY_SIZE)) {
  426. *outLen = FALCON_LEVEL1_KEY_SIZE;
  427. return BUFFER_E;
  428. }
  429. else if ((key->level == 5) && (*outLen < FALCON_LEVEL5_KEY_SIZE)) {
  430. *outLen = FALCON_LEVEL5_KEY_SIZE;
  431. return BUFFER_E;
  432. }
  433. if (key->level == 1) {
  434. *outLen = FALCON_LEVEL1_KEY_SIZE;
  435. }
  436. else if (key->level == 5) {
  437. *outLen = FALCON_LEVEL5_KEY_SIZE;
  438. }
  439. XMEMCPY(out, key->k, *outLen);
  440. return 0;
  441. }
  442. /* Export the falcon private and public key.
  443. *
  444. * key [in] Falcon private/public key.
  445. * out [in] Array to hold private and public key.
  446. * outLen [in/out] On in, the number of bytes in array.
  447. * On out, the number bytes put into array.
  448. * returns BAD_FUNC_ARG when a parameter is NULL,
  449. * BUFFER_E when outLen is less than FALCON_LEVEL1_PRV_KEY_SIZE,
  450. * 0 otherwise.
  451. */
  452. int wc_falcon_export_private(falcon_key* key, byte* out, word32* outLen)
  453. {
  454. /* sanity checks on arguments */
  455. if ((key == NULL) || (out == NULL) || (outLen == NULL)) {
  456. return BAD_FUNC_ARG;
  457. }
  458. if ((key->level != 1) && (key->level != 5)) {
  459. return BAD_FUNC_ARG;
  460. }
  461. if ((key->level == 1) && (*outLen < FALCON_LEVEL1_PRV_KEY_SIZE)) {
  462. *outLen = FALCON_LEVEL1_PRV_KEY_SIZE;
  463. return BUFFER_E;
  464. }
  465. else if ((key->level == 5) && (*outLen < FALCON_LEVEL5_PRV_KEY_SIZE)) {
  466. *outLen = FALCON_LEVEL5_PRV_KEY_SIZE;
  467. return BUFFER_E;
  468. }
  469. if (key->level == 1) {
  470. *outLen = FALCON_LEVEL1_PRV_KEY_SIZE;
  471. XMEMCPY(out, key->k, FALCON_LEVEL1_PRV_KEY_SIZE);
  472. XMEMCPY(out + FALCON_LEVEL1_PRV_KEY_SIZE, key->p,
  473. FALCON_LEVEL1_PUB_KEY_SIZE);
  474. }
  475. else if (key->level == 5) {
  476. *outLen = FALCON_LEVEL5_PRV_KEY_SIZE;
  477. XMEMCPY(out, key->k, FALCON_LEVEL5_PRV_KEY_SIZE);
  478. XMEMCPY(out + FALCON_LEVEL5_PRV_KEY_SIZE, key->p,
  479. FALCON_LEVEL5_PUB_KEY_SIZE);
  480. }
  481. return 0;
  482. }
  483. /* Export the falcon private and public key.
  484. *
  485. * key [in] Falcon private/public key.
  486. * priv [in] Array to hold private key.
  487. * privSz [in/out] On in, the number of bytes in private key array.
  488. * pub [in] Array to hold public key.
  489. * pubSz [in/out] On in, the number of bytes in public key array.
  490. * On out, the number bytes put into array.
  491. * returns BAD_FUNC_ARG when a parameter is NULL,
  492. * BUFFER_E when privSz is less than FALCON_LEVEL1_PRV_KEY_SIZE or pubSz is less
  493. * than FALCON_LEVEL1_PUB_KEY_SIZE,
  494. * 0 otherwise.
  495. */
  496. int wc_falcon_export_key(falcon_key* key, byte* priv, word32 *privSz,
  497. byte* pub, word32 *pubSz)
  498. {
  499. int ret = 0;
  500. /* export private part */
  501. ret = wc_falcon_export_private(key, priv, privSz);
  502. if (ret == 0) {
  503. /* export public part */
  504. ret = wc_falcon_export_public(key, pub, pubSz);
  505. }
  506. return ret;
  507. }
  508. /* Check the public key of the falcon key matches the private key.
  509. *
  510. * key [in] Falcon private/public key.
  511. * returns BAD_FUNC_ARG when key is NULL,
  512. * PUBLIC_KEY_E when the public key is not set or doesn't match,
  513. * other -ve value on hash failure,
  514. * 0 otherwise.
  515. */
  516. int wc_falcon_check_key(falcon_key* key)
  517. {
  518. if (key == NULL) {
  519. return BAD_FUNC_ARG;
  520. }
  521. /* Assume everything is fine. */
  522. return 0;
  523. }
  524. /* Returns the size of a falcon private key.
  525. *
  526. * key [in] Falcon private/public key.
  527. * returns BAD_FUNC_ARG when key is NULL,
  528. * FALCON_LEVEL1_KEY_SIZE otherwise.
  529. */
  530. int wc_falcon_size(falcon_key* key)
  531. {
  532. if (key == NULL) {
  533. return BAD_FUNC_ARG;
  534. }
  535. if (key->level == 1) {
  536. return FALCON_LEVEL1_KEY_SIZE;
  537. }
  538. else if (key->level == 5) {
  539. return FALCON_LEVEL5_KEY_SIZE;
  540. }
  541. return BAD_FUNC_ARG;
  542. }
  543. /* Returns the size of a falcon private plus public key.
  544. *
  545. * key [in] Falcon private/public key.
  546. * returns BAD_FUNC_ARG when key is NULL,
  547. * FALCON_LEVEL1_PRV_KEY_SIZE otherwise.
  548. */
  549. int wc_falcon_priv_size(falcon_key* key)
  550. {
  551. if (key == NULL) {
  552. return BAD_FUNC_ARG;
  553. }
  554. if (key->level == 1) {
  555. return FALCON_LEVEL1_PRV_KEY_SIZE;
  556. }
  557. else if (key->level == 5) {
  558. return FALCON_LEVEL5_PRV_KEY_SIZE;
  559. }
  560. return BAD_FUNC_ARG;
  561. }
  562. /* Returns the size of a falcon public key.
  563. *
  564. * key [in] Falcon private/public key.
  565. * returns BAD_FUNC_ARG when key is NULL,
  566. * FALCON_LEVEL1_PUB_KEY_SIZE otherwise.
  567. */
  568. int wc_falcon_pub_size(falcon_key* key)
  569. {
  570. if (key == NULL) {
  571. return BAD_FUNC_ARG;
  572. }
  573. if (key->level == 1) {
  574. return FALCON_LEVEL1_PUB_KEY_SIZE;
  575. }
  576. else if (key->level == 5) {
  577. return FALCON_LEVEL5_PUB_KEY_SIZE;
  578. }
  579. return BAD_FUNC_ARG;
  580. }
  581. /* Returns the size of a falcon signature.
  582. *
  583. * key [in] Falcon private/public key.
  584. * returns BAD_FUNC_ARG when key is NULL,
  585. * FALCON_LEVEL1_SIG_SIZE otherwise.
  586. */
  587. int wc_falcon_sig_size(falcon_key* key)
  588. {
  589. if (key == NULL) {
  590. return BAD_FUNC_ARG;
  591. }
  592. if (key->level == 1) {
  593. return FALCON_LEVEL1_SIG_SIZE;
  594. }
  595. else if (key->level == 5) {
  596. return FALCON_LEVEL5_SIG_SIZE;
  597. }
  598. return BAD_FUNC_ARG;
  599. }
  600. int wc_Falcon_PrivateKeyDecode(const byte* input, word32* inOutIdx,
  601. falcon_key* key, word32 inSz)
  602. {
  603. int ret = 0;
  604. byte privKey[FALCON_MAX_KEY_SIZE], pubKey[FALCON_MAX_PUB_KEY_SIZE];
  605. word32 privKeyLen = (word32)sizeof(privKey);
  606. word32 pubKeyLen = (word32)sizeof(pubKey);
  607. int keytype = 0;
  608. if (input == NULL || inOutIdx == NULL || key == NULL || inSz == 0) {
  609. return BAD_FUNC_ARG;
  610. }
  611. if (key->level == 1) {
  612. keytype = FALCON_LEVEL1k;
  613. }
  614. else if (key->level == 5) {
  615. keytype = FALCON_LEVEL5k;
  616. }
  617. else {
  618. return BAD_FUNC_ARG;
  619. }
  620. ret = DecodeAsymKey(input, inOutIdx, inSz, privKey, &privKeyLen,
  621. pubKey, &pubKeyLen, keytype);
  622. if (ret == 0) {
  623. if (pubKeyLen == 0) {
  624. ret = wc_falcon_import_private_only(input, inSz, key);
  625. }
  626. else {
  627. ret = wc_falcon_import_private_key(privKey, privKeyLen,
  628. pubKey, pubKeyLen, key);
  629. }
  630. }
  631. return ret;
  632. }
  633. int wc_Falcon_PublicKeyDecode(const byte* input, word32* inOutIdx,
  634. falcon_key* key, word32 inSz)
  635. {
  636. int ret = 0;
  637. byte pubKey[FALCON_MAX_PUB_KEY_SIZE];
  638. word32 pubKeyLen = (word32)sizeof(pubKey);
  639. int keytype = 0;
  640. if (input == NULL || inOutIdx == NULL || key == NULL || inSz == 0) {
  641. return BAD_FUNC_ARG;
  642. }
  643. if (key->level == 1) {
  644. keytype = FALCON_LEVEL1k;
  645. }
  646. else if (key->level == 5) {
  647. keytype = FALCON_LEVEL5k;
  648. }
  649. else {
  650. return BAD_FUNC_ARG;
  651. }
  652. ret = DecodeAsymKeyPublic(input, inOutIdx, inSz, pubKey, &pubKeyLen,
  653. keytype);
  654. if (ret == 0) {
  655. ret = wc_falcon_import_public(pubKey, pubKeyLen, key);
  656. }
  657. return ret;
  658. }
  659. #ifdef WC_ENABLE_ASYM_KEY_EXPORT
  660. /* Encode the public part of an Falcon key in DER.
  661. *
  662. * Pass NULL for output to get the size of the encoding.
  663. *
  664. * @param [in] key Falcon key object.
  665. * @param [out] output Buffer to put encoded data in.
  666. * @param [in] outLen Size of buffer in bytes.
  667. * @param [in] withAlg Whether to use SubjectPublicKeyInfo format.
  668. * @return Size of encoded data in bytes on success.
  669. * @return BAD_FUNC_ARG when key is NULL.
  670. * @return MEMORY_E when dynamic memory allocation failed.
  671. */
  672. int wc_Falcon_PublicKeyToDer(falcon_key* key, byte* output, word32 inLen,
  673. int withAlg)
  674. {
  675. int ret;
  676. byte pubKey[FALCON_MAX_PUB_KEY_SIZE];
  677. word32 pubKeyLen = (word32)sizeof(pubKey);
  678. int keytype = 0;
  679. if (key == NULL || output == NULL) {
  680. return BAD_FUNC_ARG;
  681. }
  682. if (key->level == 1) {
  683. keytype = FALCON_LEVEL1k;
  684. }
  685. else if (key->level == 5) {
  686. keytype = FALCON_LEVEL5k;
  687. }
  688. else {
  689. return BAD_FUNC_ARG;
  690. }
  691. ret = wc_falcon_export_public(key, pubKey, &pubKeyLen);
  692. if (ret == 0) {
  693. ret = SetAsymKeyDerPublic(pubKey, pubKeyLen, output, inLen, keytype,
  694. withAlg);
  695. }
  696. return ret;
  697. }
  698. #endif
  699. int wc_Falcon_KeyToDer(falcon_key* key, byte* output, word32 inLen)
  700. {
  701. if (key == NULL) {
  702. return BAD_FUNC_ARG;
  703. }
  704. if (key->level == 1) {
  705. return SetAsymKeyDer(key->k, FALCON_LEVEL1_KEY_SIZE, key->p,
  706. FALCON_LEVEL1_KEY_SIZE, output, inLen,
  707. FALCON_LEVEL1k);
  708. }
  709. else if (key->level == 5) {
  710. return SetAsymKeyDer(key->k, FALCON_LEVEL5_KEY_SIZE, key->p,
  711. FALCON_LEVEL5_KEY_SIZE, output, inLen,
  712. FALCON_LEVEL5k);
  713. }
  714. return BAD_FUNC_ARG;
  715. }
  716. int wc_Falcon_PrivateKeyToDer(falcon_key* key, byte* output, word32 inLen)
  717. {
  718. if (key == NULL) {
  719. return BAD_FUNC_ARG;
  720. }
  721. if (key->level == 1) {
  722. return SetAsymKeyDer(key->k, FALCON_LEVEL1_KEY_SIZE, NULL, 0, output,
  723. inLen, FALCON_LEVEL1k);
  724. }
  725. else if (key->level == 5) {
  726. return SetAsymKeyDer(key->k, FALCON_LEVEL5_KEY_SIZE, NULL, 0, output,
  727. inLen, FALCON_LEVEL5k);
  728. }
  729. return BAD_FUNC_ARG;
  730. }
  731. #endif /* HAVE_PQC && HAVE_FALCON */