ext_kyber.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. /* ext_kyber.c
  2. *
  3. * Copyright (C) 2006-2023 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. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #include <wolfssl/wolfcrypt/error-crypt.h>
  26. #include <wolfssl/wolfcrypt/logging.h>
  27. #ifdef WOLFSSL_HAVE_KYBER
  28. #include <wolfssl/wolfcrypt/ext_kyber.h>
  29. #ifdef NO_INLINE
  30. #include <wolfssl/wolfcrypt/misc.h>
  31. #else
  32. #define WOLFSSL_MISC_INCLUDED
  33. #include <wolfcrypt/src/misc.c>
  34. #endif
  35. #if defined (HAVE_LIBOQS)
  36. static const char* OQS_ID2name(int id) {
  37. switch (id) {
  38. case KYBER_LEVEL1: return OQS_KEM_alg_kyber_512;
  39. case KYBER_LEVEL3: return OQS_KEM_alg_kyber_768;
  40. case KYBER_LEVEL5: return OQS_KEM_alg_kyber_1024;
  41. default: break;
  42. }
  43. return NULL;
  44. }
  45. int ext_kyber_enabled(int id)
  46. {
  47. const char * name = OQS_ID2name(id);
  48. return OQS_KEM_alg_is_enabled(name);
  49. }
  50. #endif
  51. /******************************************************************************/
  52. /* Initializer and cleanup functions. */
  53. /**
  54. * Initialize the Kyber key.
  55. *
  56. * @param [in] type Type of key: KYBER512, KYBER768, KYBER1024.
  57. * @param [out] key Kyber key object to initialize.
  58. * @param [in] heap Dynamic memory hint.
  59. * @param [in] devId Device Id.
  60. * @return 0 on success.
  61. * @return BAD_FUNC_ARG when key is NULL or type is unrecognized.
  62. * @return NOT_COMPILED_IN when key type is not supported.
  63. */
  64. int wc_KyberKey_Init(int type, KyberKey* key, void* heap, int devId)
  65. {
  66. int ret = 0;
  67. /* Validate key. */
  68. if (key == NULL) {
  69. ret = BAD_FUNC_ARG;
  70. }
  71. if (ret == 0) {
  72. /* Validate type. */
  73. switch (type) {
  74. case KYBER_LEVEL1:
  75. #ifdef HAVE_LIBOQS
  76. case KYBER_LEVEL3:
  77. case KYBER_LEVEL5:
  78. #endif /* HAVE_LIBOQS */
  79. break;
  80. default:
  81. /* No other values supported. */
  82. ret = BAD_FUNC_ARG;
  83. break;
  84. }
  85. }
  86. if (ret == 0) {
  87. /* Zero out all data. */
  88. XMEMSET(key, 0, sizeof(*key));
  89. /* Keep type for parameters. */
  90. key->type = type;
  91. }
  92. (void)devId;
  93. (void)heap;
  94. return ret;
  95. }
  96. /**
  97. * Free the Kyber key object.
  98. *
  99. * @param [in, out] key Kyber key object to dispose of.
  100. */
  101. void wc_KyberKey_Free(KyberKey* key)
  102. {
  103. if (key != NULL) {
  104. /* Ensure all private data is zeroed. */
  105. ForceZero(key, sizeof(*key));
  106. }
  107. }
  108. /******************************************************************************/
  109. /* Data size getters. */
  110. /**
  111. * Get the size in bytes of encoded private key for the key.
  112. *
  113. * @param [in] key Kyber key object.
  114. * @param [out] len Length of encoded private key in bytes.
  115. * @return 0 on success.
  116. * @return BAD_FUNC_ARG when key or len is NULL.
  117. * @return NOT_COMPILED_IN when key type is not supported.
  118. */
  119. int wc_KyberKey_PrivateKeySize(KyberKey* key, word32* len)
  120. {
  121. int ret = 0;
  122. /* Validate parameters. */
  123. if ((key == NULL) || (len == NULL)) {
  124. ret = BAD_FUNC_ARG;
  125. }
  126. #ifdef HAVE_LIBOQS
  127. /* NOTE: SHAKE and AES variants have the same length private key. */
  128. if (ret == 0) {
  129. switch (key->type) {
  130. case KYBER_LEVEL1:
  131. *len = OQS_KEM_kyber_512_length_secret_key;
  132. break;
  133. case KYBER_LEVEL3:
  134. *len = OQS_KEM_kyber_768_length_secret_key;
  135. break;
  136. case KYBER_LEVEL5:
  137. *len = OQS_KEM_kyber_1024_length_secret_key;
  138. break;
  139. default:
  140. /* No other values supported. */
  141. ret = BAD_FUNC_ARG;
  142. break;
  143. }
  144. }
  145. #endif /* HAVE_LIBOQS */
  146. #ifdef HAVE_PQM4
  147. (void)key;
  148. if (ret == 0) {
  149. *len = PQM4_PRIVATE_KEY_LENGTH;
  150. }
  151. #endif /* HAVE_PQM4 */
  152. return ret;
  153. }
  154. /**
  155. * Get the size in bytes of encoded public key for the key.
  156. *
  157. * @param [in] key Kyber key object.
  158. * @param [out] len Length of encoded public key in bytes.
  159. * @return 0 on success.
  160. * @return BAD_FUNC_ARG when key or len is NULL.
  161. * @return NOT_COMPILED_IN when key type is not supported.
  162. */
  163. int wc_KyberKey_PublicKeySize(KyberKey* key, word32* len)
  164. {
  165. int ret = 0;
  166. /* Validate parameters. */
  167. if ((key == NULL) || (len == NULL)) {
  168. ret = BAD_FUNC_ARG;
  169. }
  170. #ifdef HAVE_LIBOQS
  171. /* NOTE: SHAKE and AES variants have the same length public key. */
  172. if (ret == 0) {
  173. switch (key->type) {
  174. case KYBER_LEVEL1:
  175. *len = OQS_KEM_kyber_512_length_public_key;
  176. break;
  177. case KYBER_LEVEL3:
  178. *len = OQS_KEM_kyber_768_length_public_key;
  179. break;
  180. case KYBER_LEVEL5:
  181. *len = OQS_KEM_kyber_1024_length_public_key;
  182. break;
  183. default:
  184. /* No other values supported. */
  185. ret = BAD_FUNC_ARG;
  186. break;
  187. }
  188. }
  189. #endif /* HAVE_LIBOQS */
  190. #ifdef HAVE_PQM4
  191. (void)key;
  192. if (ret == 0) {
  193. *len = PQM4_PUBLIC_KEY_LENGTH;
  194. }
  195. #endif /* HAVE_PQM4 */
  196. return ret;
  197. }
  198. /**
  199. * Get the size in bytes of cipher text for key.
  200. *
  201. * @param [in] key Kyber key object.
  202. * @param [out] len Length of cipher text in bytes.
  203. * @return 0 on success.
  204. * @return BAD_FUNC_ARG when key or len is NULL.
  205. * @return NOT_COMPILED_IN when key type is not supported.
  206. */
  207. int wc_KyberKey_CipherTextSize(KyberKey* key, word32* len)
  208. {
  209. int ret = 0;
  210. /* Validate parameters. */
  211. if ((key == NULL) || (len == NULL)) {
  212. ret = BAD_FUNC_ARG;
  213. }
  214. #ifdef HAVE_LIBOQS
  215. /* NOTE: SHAKE and AES variants have the same length ciphertext. */
  216. if (ret == 0) {
  217. switch (key->type) {
  218. case KYBER_LEVEL1:
  219. *len = OQS_KEM_kyber_512_length_ciphertext;
  220. break;
  221. case KYBER_LEVEL3:
  222. *len = OQS_KEM_kyber_768_length_ciphertext;
  223. break;
  224. case KYBER_LEVEL5:
  225. *len = OQS_KEM_kyber_1024_length_ciphertext;
  226. break;
  227. default:
  228. /* No other values supported. */
  229. ret = BAD_FUNC_ARG;
  230. break;
  231. }
  232. }
  233. #endif /* HAVE_LIBOQS */
  234. #ifdef HAVE_PQM4
  235. (void)key;
  236. if (ret == 0) {
  237. *len = PQM4_CIPHERTEXT_LENGTH;
  238. }
  239. #endif /* HAVE_PQM4 */
  240. return ret;
  241. }
  242. /**
  243. * Size of a shared secret in bytes. Always KYBER_SS_SZ.
  244. *
  245. * @param [in] key Kyber key object. Not used.
  246. * @param [out] Size of the shared secret created with a Kyber key.
  247. * @return 0 on success.
  248. * @return 0 to indicate success.
  249. */
  250. int wc_KyberKey_SharedSecretSize(KyberKey* key, word32* len)
  251. {
  252. (void)key;
  253. /* Validate parameters. */
  254. if (len == NULL) {
  255. return BAD_FUNC_ARG;
  256. }
  257. *len = KYBER_SS_SZ;
  258. return 0;
  259. }
  260. /******************************************************************************/
  261. /* Cryptographic operations. */
  262. /**
  263. * Make a Kyber key object using a random number generator.
  264. *
  265. * NOTE: rng is ignored. OQS and PQM4 don't use our RNG.
  266. *
  267. * @param [in, out] key Kyber key ovject.
  268. * @param [in] rng Random number generator.
  269. * @return 0 on success.
  270. * @return BAD_FUNC_ARG when key or rng is NULL.
  271. * @return MEMORY_E when dynamic memory allocation failed.
  272. */
  273. int wc_KyberKey_MakeKey(KyberKey* key, WC_RNG* rng)
  274. {
  275. int ret = 0;
  276. #ifdef HAVE_LIBOQS
  277. const char* algName = NULL;
  278. OQS_KEM *kem = NULL;
  279. #endif
  280. (void)rng;
  281. /* Validate parameter. */
  282. if (key == NULL) {
  283. return BAD_FUNC_ARG;
  284. }
  285. #ifdef HAVE_LIBOQS
  286. if (ret == 0) {
  287. algName = OQS_ID2name(key->type);
  288. if (algName == NULL) {
  289. ret = BAD_FUNC_ARG;
  290. }
  291. }
  292. if (ret == 0) {
  293. algName = OQS_ID2name(key->type);
  294. if (algName == NULL) {
  295. ret = BAD_FUNC_ARG;
  296. }
  297. }
  298. if (ret == 0) {
  299. kem = OQS_KEM_new(algName);
  300. if (kem == NULL) {
  301. ret = BAD_FUNC_ARG;
  302. }
  303. }
  304. if (ret == 0) {
  305. if (OQS_KEM_keypair(kem, key->pub, key->priv) !=
  306. OQS_SUCCESS) {
  307. ret = BAD_FUNC_ARG;
  308. }
  309. }
  310. OQS_KEM_free(kem);
  311. #endif /* HAVE_LIBOQS */
  312. #ifdef HAVE_PQM4
  313. if (ret == 0) {
  314. if (crypto_kem_keypair(key->pub, key->priv) != 0) {
  315. WOLFSSL_MSG("PQM4 keygen failure");
  316. ret = BAD_FUNC_ARG;
  317. }
  318. }
  319. #endif /* HAVE_PQM4 */
  320. if (ret != 0) {
  321. ForceZero(key, sizeof(*key));
  322. }
  323. return ret;
  324. }
  325. /**
  326. * Make a Kyber key object using random data.
  327. *
  328. * @param [in, out] key Kyber key ovject.
  329. * @param [in] rng Random number generator.
  330. * @return 0 on success.
  331. * @return BAD_FUNC_ARG when key or rand is NULL.
  332. * @return BUFFER_E when length is not KYBER_MAKEKEY_RAND_SZ.
  333. * @return NOT_COMPILED_IN when key type is not supported.
  334. * @return MEMORY_E when dynamic memory allocation failed.
  335. */
  336. int wc_KyberKey_MakeKeyWithRandom(KyberKey* key, const unsigned char* rand,
  337. int len)
  338. {
  339. (void)rand;
  340. (void)len;
  341. /* OQS and PQM4 don't support external randomness. */
  342. return wc_KyberKey_MakeKey(key, NULL);
  343. }
  344. /**
  345. * Encapsulate with random number generator and derive secret.
  346. *
  347. * @param [in] key Kyber key object.
  348. * @param [out] ct Cipher text.
  349. * @param [out] ss Shared secret generated.
  350. * @param [in] rng Random number generator.
  351. * @return 0 on success.
  352. * @return BAD_FUNC_ARG when key, ct, ss or RNG is NULL.
  353. * @return NOT_COMPILED_IN when key type is not supported.
  354. * @return MEMORY_E when dynamic memory allocation failed.
  355. */
  356. int wc_KyberKey_Encapsulate(KyberKey* key, unsigned char* ct, unsigned char* ss,
  357. WC_RNG* rng)
  358. {
  359. int ret = 0;
  360. #ifdef HAVE_LIBOQS
  361. const char * algName = NULL;
  362. OQS_KEM *kem = NULL;
  363. #endif
  364. (void)rng;
  365. /* Validate parameters. */
  366. if ((key == NULL) || (ct == NULL) || (ss == NULL)) {
  367. ret = BAD_FUNC_ARG;
  368. }
  369. #ifdef HAVE_LIBOQS
  370. if (ret == 0) {
  371. algName = OQS_ID2name(key->type);
  372. if (algName == NULL) {
  373. ret = BAD_FUNC_ARG;
  374. }
  375. }
  376. if (ret == 0) {
  377. kem = OQS_KEM_new(algName);
  378. if (kem == NULL) {
  379. ret = BAD_FUNC_ARG;
  380. }
  381. }
  382. if (ret == 0) {
  383. if (OQS_KEM_encaps(kem, ct, ss, key->pub) != OQS_SUCCESS) {
  384. ret = BAD_FUNC_ARG;
  385. }
  386. }
  387. OQS_KEM_free(kem);
  388. #endif /* HAVE_LIBOQS */
  389. #ifdef HAVE_PQM4
  390. if (ret == 0) {
  391. if (crypto_kem_enc(ct, ss, key->pub) != 0) {
  392. WOLFSSL_MSG("PQM4 Encapsulation failure.");
  393. ret = BAD_FUNC_ARG;
  394. }
  395. }
  396. #endif /* HAVE_PQM4 */
  397. return ret;
  398. }
  399. /**
  400. * Encapsulate with random data and derive secret.
  401. *
  402. * @param [out] ct Cipher text.
  403. * @param [out] ss Shared secret generated.
  404. * @param [in] rand Random data.
  405. * @param [in] len Random data.
  406. * @return 0 on success.
  407. * @return BAD_FUNC_ARG when key, ct, ss or RNG is NULL.
  408. * @return BUFFER_E when len is not KYBER_ENC_RAND_SZ.
  409. * @return NOT_COMPILED_IN when key type is not supported.
  410. * @return MEMORY_E when dynamic memory allocation failed.
  411. */
  412. int wc_KyberKey_EncapsulateWithRandom(KyberKey* key, unsigned char* ct,
  413. unsigned char* ss, const unsigned char* rand, int len)
  414. {
  415. (void)rand;
  416. (void)len;
  417. /* OQS and PQM4 don't support external randomness. */
  418. return wc_KyberKey_Encapsulate(key, ct, ss, NULL);
  419. }
  420. /**
  421. * Decapsulate the cipher text to calculate the shared secret.
  422. *
  423. * Validates the cipher text by encapsulating and comparing with data passed in.
  424. *
  425. * @param [in] key Kyber key object.
  426. * @param [out] ss Shared secret.
  427. * @param [in] ct Cipher text.
  428. * @param [in] len Length of cipher text.
  429. * @return 0 on success.
  430. * @return BAD_FUNC_ARG when key, ss or cr are NULL.
  431. * @return NOT_COMPILED_IN when key type is not supported.
  432. * @return BUFFER_E when len is not the length of cipher text for the key type.
  433. * @return MEMORY_E when dynamic memory allocation failed.
  434. */
  435. int wc_KyberKey_Decapsulate(KyberKey* key, unsigned char* ss,
  436. const unsigned char* ct, word32 len)
  437. {
  438. int ret = 0;
  439. word32 ctlen = 0;
  440. #ifdef HAVE_LIBOQS
  441. const char * algName = NULL;
  442. OQS_KEM *kem = NULL;
  443. #endif
  444. /* Validate parameters. */
  445. if ((key == NULL) || (ss == NULL) || (ct == NULL)) {
  446. ret = BAD_FUNC_ARG;
  447. }
  448. if (ret == 0) {
  449. ret = wc_KyberKey_CipherTextSize(key, &ctlen);
  450. }
  451. if ((ret == 0) && (len != ctlen)) {
  452. ret = BUFFER_E;
  453. }
  454. #ifdef HAVE_LIBOQS
  455. if (ret == 0) {
  456. algName = OQS_ID2name(key->type);
  457. if (algName == NULL) {
  458. ret = BAD_FUNC_ARG;
  459. }
  460. }
  461. if (ret == 0) {
  462. kem = OQS_KEM_new(algName);
  463. if (kem == NULL) {
  464. ret = BAD_FUNC_ARG;
  465. }
  466. }
  467. if (ret == 0) {
  468. if (OQS_KEM_decaps(kem, ss, ct, key->priv) != OQS_SUCCESS) {
  469. ret = BAD_FUNC_ARG;
  470. }
  471. }
  472. OQS_KEM_free(kem);
  473. #endif /* HAVE_LIBOQS */
  474. #ifdef HAVE_PQM4
  475. if (ret == 0) {
  476. if (crypto_kem_dec(ss, ct, key->priv) != 0) {
  477. WOLFSSL_MSG("PQM4 Decapsulation failure.");
  478. ret = BAD_FUNC_ARG;
  479. }
  480. }
  481. #endif /* HAVE_PQM4 */
  482. return ret;
  483. }
  484. /******************************************************************************/
  485. /* Encoding and decoding functions. */
  486. /**
  487. * Decode the private key.
  488. *
  489. * We store the whole thing in the private key buffer. Note this means we cannot
  490. * do the encapsulation operation with the private key. But generally speaking
  491. * this is never done.
  492. *
  493. * @param [in, out] key Kyber key object.
  494. * @param [in] in Buffer holding encoded key.
  495. * @param [in] len Length of data in buffer.
  496. * @return 0 on success.
  497. * @return BAD_FUNC_ARG when key or in is NULL.
  498. * @return NOT_COMPILED_IN when key type is not supported.
  499. * @return BUFFER_E when len is not the correct size.
  500. */
  501. int wc_KyberKey_DecodePrivateKey(KyberKey* key, unsigned char* in, word32 len)
  502. {
  503. int ret = 0;
  504. word32 privLen = 0;
  505. /* Validate parameters. */
  506. if ((key == NULL) || (in == NULL)) {
  507. ret = BAD_FUNC_ARG;
  508. }
  509. if (ret == 0) {
  510. ret = wc_KyberKey_PrivateKeySize(key, &privLen);
  511. }
  512. /* Ensure the data is the correct length for the key type. */
  513. if ((ret == 0) && (len != privLen)) {
  514. ret = BUFFER_E;
  515. }
  516. if (ret == 0) {
  517. XMEMCPY(key->priv, in, privLen);
  518. }
  519. return ret;
  520. }
  521. /**
  522. * Decode public key.
  523. *
  524. * We store the whole thing in the public key buffer.
  525. *
  526. * @param [in, out] key Kyber key object.
  527. * @param [in] in Buffer holding encoded key.
  528. * @param [in] len Length of data in buffer.
  529. * @return 0 on success.
  530. * @return BAD_FUNC_ARG when key or in is NULL.
  531. * @return NOT_COMPILED_IN when key type is not supported.
  532. * @return BUFFER_E when len is not the correct size.
  533. */
  534. int wc_KyberKey_DecodePublicKey(KyberKey* key, unsigned char* in, word32 len)
  535. {
  536. int ret = 0;
  537. word32 pubLen = 0;
  538. /* Validate parameters. */
  539. if ((key == NULL) || (in == NULL)) {
  540. ret = BAD_FUNC_ARG;
  541. }
  542. if (ret == 0) {
  543. ret = wc_KyberKey_PublicKeySize(key, &pubLen);
  544. }
  545. /* Ensure the data is the correct length for the key type. */
  546. if ((ret == 0) && (len != pubLen)) {
  547. ret = BUFFER_E;
  548. }
  549. if (ret == 0) {
  550. XMEMCPY(key->pub, in, pubLen);
  551. }
  552. return ret;
  553. }
  554. /**
  555. * Encode the private key.
  556. *
  557. * We stored it as a blob so we can just copy it over.
  558. *
  559. * @param [in] key Kyber key object.
  560. * @param [out] out Buffer to hold data.
  561. * @param [in] len Size of buffer in bytes.
  562. * @return 0 on success.
  563. * @return BAD_FUNC_ARG when key or out is NULL or private/public key not
  564. * available.
  565. * @return NOT_COMPILED_IN when key type is not supported.
  566. */
  567. int wc_KyberKey_EncodePrivateKey(KyberKey* key, unsigned char* out, word32 len)
  568. {
  569. int ret = 0;
  570. unsigned int privLen = 0;
  571. if ((key == NULL) || (out == NULL)) {
  572. ret = BAD_FUNC_ARG;
  573. }
  574. if (ret == 0) {
  575. ret = wc_KyberKey_PrivateKeySize(key, &privLen);
  576. }
  577. /* Check buffer is big enough for encoding. */
  578. if ((ret == 0) && (len != privLen)) {
  579. ret = BUFFER_E;
  580. }
  581. if (ret == 0) {
  582. XMEMCPY(out, key->priv, privLen);
  583. }
  584. return ret;
  585. }
  586. /**
  587. * Encode the public key.
  588. *
  589. * We stored it as a blob so we can just copy it over.
  590. *
  591. * @param [in] key Kyber key object.
  592. * @param [out] out Buffer to hold data.
  593. * @param [in] len Size of buffer in bytes.
  594. * @return 0 on success.
  595. * @return BAD_FUNC_ARG when key or out is NULL or public key not available.
  596. * @return NOT_COMPILED_IN when key type is not supported.
  597. */
  598. int wc_KyberKey_EncodePublicKey(KyberKey* key, unsigned char* out, word32 len)
  599. {
  600. int ret = 0;
  601. unsigned int pubLen = 0;
  602. if ((key == NULL) || (out == NULL)) {
  603. ret = BAD_FUNC_ARG;
  604. }
  605. if (ret == 0) {
  606. ret = wc_KyberKey_PublicKeySize(key, &pubLen);
  607. }
  608. /* Check buffer is big enough for encoding. */
  609. if ((ret == 0) && (len != pubLen)) {
  610. ret = BUFFER_E;
  611. }
  612. if (ret == 0) {
  613. XMEMCPY(out, key->pub, pubLen);
  614. }
  615. return ret;
  616. }
  617. #endif /* WOLFSSL_HAVE_KYBER */