dilithium.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. /* dilithium.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 Dilithium 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_DILITHIUM)
  29. #ifdef HAVE_LIBOQS
  30. #include <oqs/oqs.h>
  31. #endif
  32. #include <wolfssl/wolfcrypt/dilithium.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 dilithium 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] Dilithium 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 DILITHIUM_LEVEL2_SIG_SIZE,
  50. * 0 otherwise.
  51. */
  52. int wc_dilithium_sign_msg(const byte* in, word32 inLen,
  53. byte* out, word32 *outLen,
  54. dilithium_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->sym == SHAKE_VARIANT) && (key->level == 2)) {
  69. oqssig = OQS_SIG_new(OQS_SIG_alg_dilithium_2);
  70. }
  71. else if ((key->sym == SHAKE_VARIANT) && (key->level == 3)) {
  72. oqssig = OQS_SIG_new(OQS_SIG_alg_dilithium_3);
  73. }
  74. else if ((key->sym == SHAKE_VARIANT) && (key->level == 5)) {
  75. oqssig = OQS_SIG_new(OQS_SIG_alg_dilithium_5);
  76. }
  77. else if ((key->sym == AES_VARIANT) && (key->level == 2)) {
  78. oqssig = OQS_SIG_new(OQS_SIG_alg_dilithium_2_aes);
  79. }
  80. else if ((key->sym == AES_VARIANT) && (key->level == 3)) {
  81. oqssig = OQS_SIG_new(OQS_SIG_alg_dilithium_3_aes);
  82. }
  83. else if ((key->sym == AES_VARIANT) && (key->level == 5)) {
  84. oqssig = OQS_SIG_new(OQS_SIG_alg_dilithium_5_aes);
  85. }
  86. if (oqssig == NULL) {
  87. ret = SIG_TYPE_E;
  88. }
  89. }
  90. /* check and set up out length */
  91. if (ret == 0) {
  92. if ((key->level == 2) && (*outLen < DILITHIUM_LEVEL2_SIG_SIZE)) {
  93. *outLen = DILITHIUM_LEVEL2_SIG_SIZE;
  94. ret = BUFFER_E;
  95. }
  96. else if ((key->level == 3) && (*outLen < DILITHIUM_LEVEL3_SIG_SIZE)) {
  97. *outLen = DILITHIUM_LEVEL3_SIG_SIZE;
  98. ret = BUFFER_E;
  99. }
  100. else if ((key->level == 5) && (*outLen < DILITHIUM_LEVEL5_SIG_SIZE)) {
  101. *outLen = DILITHIUM_LEVEL5_SIG_SIZE;
  102. ret = BUFFER_E;
  103. }
  104. localOutLen = *outLen;
  105. }
  106. if ((ret == 0) &&
  107. (OQS_SIG_sign(oqssig, out, &localOutLen, in, inLen, key->k)
  108. == OQS_ERROR)) {
  109. ret = BAD_FUNC_ARG;
  110. }
  111. if (ret == 0) {
  112. *outLen = (word32)localOutLen;
  113. }
  114. if (oqssig != NULL) {
  115. OQS_SIG_free(oqssig);
  116. }
  117. #else
  118. ret = NOT_COMPILED_IN;
  119. #endif
  120. return ret;
  121. }
  122. /* Verify the message using the dilithium public key.
  123. *
  124. * sig [in] Signature to verify.
  125. * sigLen [in] Size of signature in bytes.
  126. * msg [in] Message to verify.
  127. * msgLen [in] Length of the message in bytes.
  128. * res [out] *res is set to 1 on successful verification.
  129. * key [in] Dilithium key to use to verify.
  130. * returns BAD_FUNC_ARG when a parameter is NULL or contextLen is zero when and
  131. * BUFFER_E when sigLen is less than DILITHIUM_LEVEL2_SIG_SIZE,
  132. * 0 otherwise.
  133. */
  134. int wc_dilithium_verify_msg(const byte* sig, word32 sigLen, const byte* msg,
  135. word32 msgLen, int* res, dilithium_key* key)
  136. {
  137. int ret = 0;
  138. #ifdef HAVE_LIBOQS
  139. OQS_SIG *oqssig = NULL;
  140. if (key == NULL || sig == NULL || msg == NULL || res == NULL) {
  141. ret = BAD_FUNC_ARG;
  142. }
  143. if ((ret == 0) && (!key->pubKeySet)) {
  144. ret = BAD_FUNC_ARG;
  145. }
  146. if (ret == 0) {
  147. if ((key->sym == SHAKE_VARIANT) && (key->level == 2)) {
  148. oqssig = OQS_SIG_new(OQS_SIG_alg_dilithium_2);
  149. }
  150. else if ((key->sym == SHAKE_VARIANT) && (key->level == 3)) {
  151. oqssig = OQS_SIG_new(OQS_SIG_alg_dilithium_3);
  152. }
  153. else if ((key->sym == SHAKE_VARIANT) && (key->level == 5)) {
  154. oqssig = OQS_SIG_new(OQS_SIG_alg_dilithium_5);
  155. }
  156. else if ((key->sym == AES_VARIANT) && (key->level == 2)) {
  157. oqssig = OQS_SIG_new(OQS_SIG_alg_dilithium_2_aes);
  158. }
  159. else if ((key->sym == AES_VARIANT) && (key->level == 3)) {
  160. oqssig = OQS_SIG_new(OQS_SIG_alg_dilithium_3_aes);
  161. }
  162. else if ((key->sym == AES_VARIANT) && (key->level == 5)) {
  163. oqssig = OQS_SIG_new(OQS_SIG_alg_dilithium_5_aes);
  164. }
  165. if (oqssig == NULL) {
  166. ret = SIG_TYPE_E;
  167. }
  168. }
  169. if ((ret == 0) &&
  170. (OQS_SIG_verify(oqssig, msg, msgLen, sig, sigLen, key->p)
  171. == OQS_ERROR)) {
  172. ret = SIG_VERIFY_E;
  173. }
  174. if (ret == 0) {
  175. *res = 1;
  176. }
  177. if (oqssig != NULL) {
  178. OQS_SIG_free(oqssig);
  179. }
  180. #else
  181. ret = NOT_COMPILED_IN;
  182. #endif
  183. return ret;
  184. }
  185. /* Initialize the dilithium private/public key.
  186. *
  187. * key [in] Dilithium key.
  188. * returns BAD_FUNC_ARG when key is NULL
  189. */
  190. int wc_dilithium_init(dilithium_key* key)
  191. {
  192. if (key == NULL) {
  193. return BAD_FUNC_ARG;
  194. }
  195. ForceZero(key, sizeof(key));
  196. return 0;
  197. }
  198. /* Set the level of the dilithium private/public key.
  199. *
  200. * key [out] Dilithium key.
  201. * level [in] Either 2,3 or 5.
  202. * sym [in] Either SHAKE_VARIANT or AES_VARIANT.
  203. * returns BAD_FUNC_ARG when key is NULL or level or sym are bad values.
  204. */
  205. int wc_dilithium_set_level_and_sym(dilithium_key* key, byte level, byte sym)
  206. {
  207. if (key == NULL) {
  208. return BAD_FUNC_ARG;
  209. }
  210. if (level != 2 && level != 3 && level != 5) {
  211. return BAD_FUNC_ARG;
  212. }
  213. if (sym != SHAKE_VARIANT && sym != AES_VARIANT) {
  214. return BAD_FUNC_ARG;
  215. }
  216. key->level = level;
  217. key->sym = sym;
  218. key->pubKeySet = 0;
  219. key->prvKeySet = 0;
  220. return 0;
  221. }
  222. /* Get the level and symmetric variant of the dilithium private/public key.
  223. *
  224. * key [in] Dilithium key.
  225. * level [out] The level.
  226. * sym [out] The symetric variant. SHAKE_VARIANT or AES_VARIANT.
  227. * returns BAD_FUNC_ARG when key is NULL or level has not been set.
  228. */
  229. int wc_dilithium_get_level_and_sym(dilithium_key* key, byte* level, byte* sym)
  230. {
  231. if (key == NULL || level == NULL) {
  232. return BAD_FUNC_ARG;
  233. }
  234. if (key->level != 2 && key->level != 3 && key->level != 5) {
  235. return BAD_FUNC_ARG;
  236. }
  237. if (key->sym != SHAKE_VARIANT && key->sym != AES_VARIANT) {
  238. return BAD_FUNC_ARG;
  239. }
  240. *level = key->level;
  241. *sym = key->sym;
  242. return 0;
  243. }
  244. /* Clears the dilithium key data
  245. *
  246. * key [in] Dilithium key.
  247. */
  248. void wc_dilithium_free(dilithium_key* key)
  249. {
  250. if (key != NULL) {
  251. ForceZero(key, sizeof(key));
  252. }
  253. }
  254. /* Export the dilithium public key.
  255. *
  256. * key [in] Dilithium public key.
  257. * out [in] Array to hold public key.
  258. * outLen [in/out] On in, the number of bytes in array.
  259. * On out, the number bytes put into array.
  260. * returns BAD_FUNC_ARG when a parameter is NULL,
  261. * BUFFER_E when outLen is less than DILITHIUM_LEVEL2_PUB_KEY_SIZE,
  262. * 0 otherwise.
  263. */
  264. int wc_dilithium_export_public(dilithium_key* key,
  265. byte* out, word32* outLen)
  266. {
  267. /* sanity check on arguments */
  268. if ((key == NULL) || (out == NULL) || (outLen == NULL)) {
  269. return BAD_FUNC_ARG;
  270. }
  271. if ((key->level != 1) && (key->level != 5)) {
  272. return BAD_FUNC_ARG;
  273. }
  274. if (!key->pubKeySet) {
  275. return BAD_FUNC_ARG;
  276. }
  277. /* check and set up out length */
  278. if ((key->level == 2) && (*outLen < DILITHIUM_LEVEL2_PUB_KEY_SIZE)) {
  279. *outLen = DILITHIUM_LEVEL2_PUB_KEY_SIZE;
  280. return BUFFER_E;
  281. }
  282. else if ((key->level == 3) && (*outLen < DILITHIUM_LEVEL3_PUB_KEY_SIZE)) {
  283. *outLen = DILITHIUM_LEVEL3_PUB_KEY_SIZE;
  284. return BUFFER_E;
  285. }
  286. else if ((key->level == 5) && (*outLen < DILITHIUM_LEVEL5_PUB_KEY_SIZE)) {
  287. *outLen = DILITHIUM_LEVEL5_PUB_KEY_SIZE;
  288. return BUFFER_E;
  289. }
  290. if (key->level == 2) {
  291. *outLen = DILITHIUM_LEVEL2_PUB_KEY_SIZE;
  292. XMEMCPY(out, key->p, DILITHIUM_LEVEL2_PUB_KEY_SIZE);
  293. }
  294. else if (key->level == 3) {
  295. *outLen = DILITHIUM_LEVEL3_PUB_KEY_SIZE;
  296. XMEMCPY(out, key->p, DILITHIUM_LEVEL3_PUB_KEY_SIZE);
  297. }
  298. else if (key->level == 5) {
  299. *outLen = DILITHIUM_LEVEL5_PUB_KEY_SIZE;
  300. XMEMCPY(out, key->p, DILITHIUM_LEVEL5_PUB_KEY_SIZE);
  301. }
  302. return 0;
  303. }
  304. /* Import a dilithium public key from a byte array.
  305. * Public key encoded in big-endian.
  306. *
  307. * in [in] Array holding public key.
  308. * inLen [in] Number of bytes of data in array.
  309. * key [in] Dilithium public key.
  310. * returns BAD_FUNC_ARG when a parameter is NULL or key format is not supported,
  311. * 0 otherwise.
  312. */
  313. int wc_dilithium_import_public(const byte* in, word32 inLen,
  314. dilithium_key* key)
  315. {
  316. /* sanity check on arguments */
  317. if ((in == NULL) || (key == NULL)) {
  318. return BAD_FUNC_ARG;
  319. }
  320. if ((key->level != 2) && (key->level != 3) && (key->level != 5)) {
  321. return BAD_FUNC_ARG;
  322. }
  323. if (key->sym != SHAKE_VARIANT && key->sym != AES_VARIANT) {
  324. return BAD_FUNC_ARG;
  325. }
  326. if ((key->level == 2) && (inLen != DILITHIUM_LEVEL2_PUB_KEY_SIZE)) {
  327. return BAD_FUNC_ARG;
  328. }
  329. else if ((key->level == 3) && (inLen != DILITHIUM_LEVEL3_PUB_KEY_SIZE)) {
  330. return BAD_FUNC_ARG;
  331. }
  332. else if ((key->level == 5) && (inLen != DILITHIUM_LEVEL5_PUB_KEY_SIZE)) {
  333. return BAD_FUNC_ARG;
  334. }
  335. XMEMCPY(key->p, in, inLen);
  336. key->pubKeySet = 1;
  337. return 0;
  338. }
  339. static int parse_private_key(const byte* priv, word32 privSz,
  340. byte** out, word32 *outSz,
  341. dilithium_key* key) {
  342. word32 idx = 0;
  343. int ret = 0;
  344. int length = 0;
  345. /* sanity check on arguments */
  346. if ((priv == NULL) || (key == NULL)) {
  347. return BAD_FUNC_ARG;
  348. }
  349. if ((key->level != 2) && (key->level != 3) && (key->level != 5)) {
  350. return BAD_FUNC_ARG;
  351. }
  352. if (key->sym != SHAKE_VARIANT && key->sym != AES_VARIANT) {
  353. return BAD_FUNC_ARG;
  354. }
  355. /* At this point, it is still a PKCS8 private key. */
  356. if ((ret = ToTraditionalInline(priv, &idx, privSz)) < 0) {
  357. return ret;
  358. }
  359. /* Now it is a octet_string(concat(priv,pub)) */
  360. if ((ret = GetOctetString(priv, &idx, &length, privSz)) < 0) {
  361. return ret;
  362. }
  363. *out = (byte *)priv + idx;
  364. *outSz = privSz - idx;
  365. /* And finally it is concat(priv,pub). Key size check. */
  366. if ((key->level == 2) && (*outSz != DILITHIUM_LEVEL2_KEY_SIZE +
  367. DILITHIUM_LEVEL2_PUB_KEY_SIZE)) {
  368. return BAD_FUNC_ARG;
  369. }
  370. else if ((key->level == 3) && (*outSz != DILITHIUM_LEVEL3_KEY_SIZE +
  371. DILITHIUM_LEVEL3_PUB_KEY_SIZE)) {
  372. return BAD_FUNC_ARG;
  373. }
  374. else if ((key->level == 5) && (*outSz != DILITHIUM_LEVEL5_KEY_SIZE +
  375. DILITHIUM_LEVEL5_PUB_KEY_SIZE)) {
  376. return BAD_FUNC_ARG;
  377. }
  378. return 0;
  379. }
  380. /* Import a dilithium private key from a byte array.
  381. *
  382. * priv [in] Array holding private key.
  383. * privSz [in] Number of bytes of data in array.
  384. * key [in] Dilithium private key.
  385. * returns BAD_FUNC_ARG when a parameter is NULL or privSz is less than
  386. * DILITHIUM_LEVEL2_KEY_SIZE,
  387. * 0 otherwise.
  388. */
  389. int wc_dilithium_import_private_only(const byte* priv, word32 privSz,
  390. dilithium_key* key)
  391. {
  392. int ret = 0;
  393. byte *newPriv = NULL;
  394. word32 newPrivSz = 0;
  395. if ((ret = parse_private_key(priv, privSz, &newPriv, &newPrivSz, key))
  396. != 0) {
  397. return ret;
  398. }
  399. if (key->level == 2) {
  400. XMEMCPY(key->k, newPriv, DILITHIUM_LEVEL2_KEY_SIZE);
  401. }
  402. else if (key->level == 3) {
  403. XMEMCPY(key->k, newPriv, DILITHIUM_LEVEL3_KEY_SIZE);
  404. }
  405. else if (key->level == 5) {
  406. XMEMCPY(key->k, newPriv, DILITHIUM_LEVEL5_KEY_SIZE);
  407. }
  408. key->prvKeySet = 1;
  409. return 0;
  410. }
  411. /* Import a dilithium private and public keys from byte array(s).
  412. *
  413. * priv [in] Array holding private key or private+public keys
  414. * privSz [in] Number of bytes of data in private key array.
  415. * pub [in] Array holding public key (or NULL).
  416. * pubSz [in] Number of bytes of data in public key array (or 0).
  417. * key [in] Dilithium private/public key.
  418. * returns BAD_FUNC_ARG when a required parameter is NULL or an invalid
  419. * combination of keys/lengths is supplied, 0 otherwise.
  420. */
  421. int wc_dilithium_import_private_key(const byte* priv, word32 privSz,
  422. const byte* pub, word32 pubSz,
  423. dilithium_key* key)
  424. {
  425. int ret = 0;
  426. byte *newPriv = NULL;
  427. word32 newPrivSz = 0;
  428. if ((ret = parse_private_key(priv, privSz, &newPriv, &newPrivSz, key))
  429. != 0) {
  430. return ret;
  431. }
  432. if (pub == NULL) {
  433. if (pubSz != 0) {
  434. return BAD_FUNC_ARG;
  435. }
  436. if ((newPrivSz != DILITHIUM_LEVEL2_PRV_KEY_SIZE) &&
  437. (newPrivSz != DILITHIUM_LEVEL3_PRV_KEY_SIZE) &&
  438. (newPrivSz != DILITHIUM_LEVEL5_PRV_KEY_SIZE)) {
  439. return BAD_FUNC_ARG;
  440. }
  441. if (key->level == 2) {
  442. pub = newPriv + DILITHIUM_LEVEL2_KEY_SIZE;
  443. pubSz = DILITHIUM_LEVEL2_PUB_KEY_SIZE;
  444. }
  445. else if (key->level == 3) {
  446. pub = newPriv + DILITHIUM_LEVEL3_KEY_SIZE;
  447. pubSz = DILITHIUM_LEVEL3_PUB_KEY_SIZE;
  448. }
  449. else if (key->level == 5) {
  450. pub = newPriv + DILITHIUM_LEVEL5_KEY_SIZE;
  451. pubSz = DILITHIUM_LEVEL5_PUB_KEY_SIZE;
  452. }
  453. }
  454. else if ((pubSz != DILITHIUM_LEVEL2_PUB_KEY_SIZE) &&
  455. (pubSz != DILITHIUM_LEVEL3_PUB_KEY_SIZE) &&
  456. (pubSz != DILITHIUM_LEVEL5_PUB_KEY_SIZE)) {
  457. return BAD_FUNC_ARG;
  458. }
  459. /* import public key */
  460. ret = wc_dilithium_import_public(pub, pubSz, key);
  461. if (ret == 0) {
  462. /* make the private key (priv + pub) */
  463. if (key->level == 2) {
  464. XMEMCPY(key->k, newPriv, DILITHIUM_LEVEL2_KEY_SIZE);
  465. }
  466. else if (key->level == 3) {
  467. XMEMCPY(key->k, newPriv, DILITHIUM_LEVEL3_KEY_SIZE);
  468. }
  469. else if (key->level == 5) {
  470. XMEMCPY(key->k, newPriv, DILITHIUM_LEVEL5_KEY_SIZE);
  471. }
  472. key->prvKeySet = 1;
  473. }
  474. return ret;
  475. }
  476. /* Export the dilithium private key.
  477. *
  478. * key [in] Dilithium private key.
  479. * out [in] Array to hold private key.
  480. * outLen [in/out] On in, the number of bytes in array.
  481. * On out, the number bytes put into array.
  482. * returns BAD_FUNC_ARG when a parameter is NULL,
  483. * BUFFER_E when outLen is less than DILITHIUM_LEVEL2_KEY_SIZE,
  484. * 0 otherwise.
  485. */
  486. int wc_dilithium_export_private_only(dilithium_key* key, byte* out, word32* outLen)
  487. {
  488. /* sanity checks on arguments */
  489. if ((key == NULL) || (out == NULL) || (outLen == NULL)) {
  490. return BAD_FUNC_ARG;
  491. }
  492. if ((key->level != 2) && (key->level != 3) && (key->level != 5)) {
  493. return BAD_FUNC_ARG;
  494. }
  495. if (key->sym != SHAKE_VARIANT && key->sym != AES_VARIANT) {
  496. return BAD_FUNC_ARG;
  497. }
  498. /* check and set up out length */
  499. if ((key->level == 2) && (*outLen < DILITHIUM_LEVEL2_KEY_SIZE)) {
  500. *outLen = DILITHIUM_LEVEL2_KEY_SIZE;
  501. return BUFFER_E;
  502. }
  503. else if ((key->level == 3) && (*outLen < DILITHIUM_LEVEL3_KEY_SIZE)) {
  504. *outLen = DILITHIUM_LEVEL3_KEY_SIZE;
  505. return BUFFER_E;
  506. }
  507. else if ((key->level == 5) && (*outLen < DILITHIUM_LEVEL5_KEY_SIZE)) {
  508. *outLen = DILITHIUM_LEVEL5_KEY_SIZE;
  509. return BUFFER_E;
  510. }
  511. if (key->level == 2) {
  512. *outLen = DILITHIUM_LEVEL2_KEY_SIZE;
  513. }
  514. else if (key->level == 3) {
  515. *outLen = DILITHIUM_LEVEL3_KEY_SIZE;
  516. }
  517. else if (key->level == 5) {
  518. *outLen = DILITHIUM_LEVEL5_KEY_SIZE;
  519. }
  520. XMEMCPY(out, key->k, *outLen);
  521. return 0;
  522. }
  523. /* Export the dilithium private and public key.
  524. *
  525. * key [in] Dilithium private/public key.
  526. * out [in] Array to hold private and public key.
  527. * outLen [in/out] On in, the number of bytes in array.
  528. * On out, the number bytes put into array.
  529. * returns BAD_FUNC_ARG when a parameter is NULL,
  530. * BUFFER_E when outLen is less than DILITHIUM_LEVEL2_PRV_KEY_SIZE,
  531. * 0 otherwise.
  532. */
  533. int wc_dilithium_export_private(dilithium_key* key, byte* out, word32* outLen)
  534. {
  535. /* sanity checks on arguments */
  536. if ((key == NULL) || (out == NULL) || (outLen == NULL)) {
  537. return BAD_FUNC_ARG;
  538. }
  539. if ((key->level != 2) && (key->level != 3) && (key->level != 5)) {
  540. return BAD_FUNC_ARG;
  541. }
  542. if (key->sym != SHAKE_VARIANT && key->sym != AES_VARIANT) {
  543. return BAD_FUNC_ARG;
  544. }
  545. if ((key->level == 2) && (*outLen < DILITHIUM_LEVEL2_PRV_KEY_SIZE)) {
  546. *outLen = DILITHIUM_LEVEL2_PRV_KEY_SIZE;
  547. return BUFFER_E;
  548. }
  549. else if ((key->level == 3) && (*outLen < DILITHIUM_LEVEL3_PRV_KEY_SIZE)) {
  550. *outLen = DILITHIUM_LEVEL3_PRV_KEY_SIZE;
  551. return BUFFER_E;
  552. }
  553. else if ((key->level == 5) && (*outLen < DILITHIUM_LEVEL5_PRV_KEY_SIZE)) {
  554. *outLen = DILITHIUM_LEVEL5_PRV_KEY_SIZE;
  555. return BUFFER_E;
  556. }
  557. if (key->level == 2) {
  558. *outLen = DILITHIUM_LEVEL2_PRV_KEY_SIZE;
  559. XMEMCPY(out, key->k, DILITHIUM_LEVEL2_PRV_KEY_SIZE);
  560. XMEMCPY(out + DILITHIUM_LEVEL2_PRV_KEY_SIZE, key->p,
  561. DILITHIUM_LEVEL2_PUB_KEY_SIZE);
  562. }
  563. else if (key->level == 3) {
  564. *outLen = DILITHIUM_LEVEL3_PRV_KEY_SIZE;
  565. XMEMCPY(out, key->k, DILITHIUM_LEVEL3_PRV_KEY_SIZE);
  566. XMEMCPY(out + DILITHIUM_LEVEL3_PRV_KEY_SIZE, key->p,
  567. DILITHIUM_LEVEL3_PUB_KEY_SIZE);
  568. }
  569. else if (key->level == 5) {
  570. *outLen = DILITHIUM_LEVEL5_PRV_KEY_SIZE;
  571. XMEMCPY(out, key->k, DILITHIUM_LEVEL5_PRV_KEY_SIZE);
  572. XMEMCPY(out + DILITHIUM_LEVEL5_PRV_KEY_SIZE, key->p,
  573. DILITHIUM_LEVEL5_PUB_KEY_SIZE);
  574. }
  575. return 0;
  576. }
  577. /* Export the dilithium private and public key.
  578. *
  579. * key [in] Dilithium private/public key.
  580. * priv [in] Array to hold private key.
  581. * privSz [in/out] On in, the number of bytes in private key array.
  582. * pub [in] Array to hold public key.
  583. * pubSz [in/out] On in, the number of bytes in public key array.
  584. * On out, the number bytes put into array.
  585. * returns BAD_FUNC_ARG when a parameter is NULL,
  586. * BUFFER_E when privSz is less than DILITHIUM_LEVEL2_PRV_KEY_SIZE or pubSz is less
  587. * than DILITHIUM_LEVEL2_PUB_KEY_SIZE,
  588. * 0 otherwise.
  589. */
  590. int wc_dilithium_export_key(dilithium_key* key, byte* priv, word32 *privSz,
  591. byte* pub, word32 *pubSz)
  592. {
  593. int ret = 0;
  594. /* export private part */
  595. ret = wc_dilithium_export_private(key, priv, privSz);
  596. if (ret == 0) {
  597. /* export public part */
  598. ret = wc_dilithium_export_public(key, pub, pubSz);
  599. }
  600. return ret;
  601. }
  602. /* Check the public key of the dilithium key matches the private key.
  603. *
  604. * key [in] Dilithium private/public key.
  605. * returns BAD_FUNC_ARG when key is NULL,
  606. * PUBLIC_KEY_E when the public key is not set or doesn't match,
  607. * other -ve value on hash failure,
  608. * 0 otherwise.
  609. */
  610. int wc_dilithium_check_key(dilithium_key* key)
  611. {
  612. if (key == NULL) {
  613. return BAD_FUNC_ARG;
  614. }
  615. /* Assume everything is fine. */
  616. return 0;
  617. }
  618. /* Returns the size of a dilithium private key.
  619. *
  620. * key [in] Dilithium private/public key.
  621. * returns BAD_FUNC_ARG when key is NULL,
  622. * DILITHIUM_LEVEL2_KEY_SIZE otherwise.
  623. */
  624. int wc_dilithium_size(dilithium_key* key)
  625. {
  626. if (key == NULL) {
  627. return BAD_FUNC_ARG;
  628. }
  629. if (key->level == 2) {
  630. return DILITHIUM_LEVEL2_KEY_SIZE;
  631. }
  632. else if (key->level == 3) {
  633. return DILITHIUM_LEVEL3_KEY_SIZE;
  634. }
  635. else if (key->level == 5) {
  636. return DILITHIUM_LEVEL5_KEY_SIZE;
  637. }
  638. return BAD_FUNC_ARG;
  639. }
  640. /* Returns the size of a dilithium private plus public key.
  641. *
  642. * key [in] Dilithium private/public key.
  643. * returns BAD_FUNC_ARG when key is NULL,
  644. * DILITHIUM_LEVEL2_PRV_KEY_SIZE otherwise.
  645. */
  646. int wc_dilithium_priv_size(dilithium_key* key)
  647. {
  648. if (key == NULL) {
  649. return BAD_FUNC_ARG;
  650. }
  651. if (key->level == 2) {
  652. return DILITHIUM_LEVEL2_PRV_KEY_SIZE;
  653. }
  654. else if (key->level == 3) {
  655. return DILITHIUM_LEVEL3_PRV_KEY_SIZE;
  656. }
  657. else if (key->level == 5) {
  658. return DILITHIUM_LEVEL5_PRV_KEY_SIZE;
  659. }
  660. return BAD_FUNC_ARG;
  661. }
  662. /* Returns the size of a dilithium public key.
  663. *
  664. * key [in] Dilithium private/public key.
  665. * returns BAD_FUNC_ARG when key is NULL,
  666. * DILITHIUM_LEVEL2_PUB_KEY_SIZE otherwise.
  667. */
  668. int wc_dilithium_pub_size(dilithium_key* key)
  669. {
  670. if (key == NULL) {
  671. return BAD_FUNC_ARG;
  672. }
  673. if (key->level == 2) {
  674. return DILITHIUM_LEVEL2_PUB_KEY_SIZE;
  675. }
  676. else if (key->level == 3) {
  677. return DILITHIUM_LEVEL3_PUB_KEY_SIZE;
  678. }
  679. else if (key->level == 5) {
  680. return DILITHIUM_LEVEL5_PUB_KEY_SIZE;
  681. }
  682. return BAD_FUNC_ARG;
  683. }
  684. /* Returns the size of a dilithium signature.
  685. *
  686. * key [in] Dilithium private/public key.
  687. * returns BAD_FUNC_ARG when key is NULL,
  688. * DILITHIUM_LEVEL2_SIG_SIZE otherwise.
  689. */
  690. int wc_dilithium_sig_size(dilithium_key* key)
  691. {
  692. if (key == NULL) {
  693. return BAD_FUNC_ARG;
  694. }
  695. if (key->level == 2) {
  696. return DILITHIUM_LEVEL2_SIG_SIZE;
  697. }
  698. else if (key->level == 3) {
  699. return DILITHIUM_LEVEL3_SIG_SIZE;
  700. }
  701. else if (key->level == 5) {
  702. return DILITHIUM_LEVEL5_SIG_SIZE;
  703. }
  704. return BAD_FUNC_ARG;
  705. }
  706. int wc_Dilithium_PrivateKeyDecode(const byte* input, word32* inOutIdx,
  707. dilithium_key* key, word32 inSz)
  708. {
  709. int ret = 0;
  710. byte privKey[DILITHIUM_MAX_KEY_SIZE], pubKey[DILITHIUM_MAX_PUB_KEY_SIZE];
  711. word32 privKeyLen = (word32)sizeof(privKey);
  712. word32 pubKeyLen = (word32)sizeof(pubKey);
  713. int keytype = 0;
  714. if (input == NULL || inOutIdx == NULL || key == NULL || inSz == 0) {
  715. return BAD_FUNC_ARG;
  716. }
  717. if ((key->level == 2) && (key->sym == SHAKE_VARIANT)) {
  718. keytype = DILITHIUM_LEVEL2k;
  719. }
  720. else if ((key->level == 3) && (key->sym == SHAKE_VARIANT)) {
  721. keytype = DILITHIUM_LEVEL3k;
  722. }
  723. else if ((key->level == 5) && (key->sym == SHAKE_VARIANT)) {
  724. keytype = DILITHIUM_LEVEL5k;
  725. }
  726. if ((key->level == 2) && (key->sym == AES_VARIANT)) {
  727. keytype = DILITHIUM_AES_LEVEL2k;
  728. }
  729. else if ((key->level == 3) && (key->sym == AES_VARIANT)) {
  730. keytype = DILITHIUM_AES_LEVEL3k;
  731. }
  732. else if ((key->level == 5) && (key->sym == AES_VARIANT)) {
  733. keytype = DILITHIUM_AES_LEVEL5k;
  734. }
  735. else {
  736. return BAD_FUNC_ARG;
  737. }
  738. ret = DecodeAsymKey(input, inOutIdx, inSz, privKey, &privKeyLen,
  739. pubKey, &pubKeyLen, keytype);
  740. if (ret == 0) {
  741. if (pubKeyLen == 0) {
  742. ret = wc_dilithium_import_private_only(input, inSz, key);
  743. }
  744. else {
  745. ret = wc_dilithium_import_private_key(privKey, privKeyLen,
  746. pubKey, pubKeyLen, key);
  747. }
  748. }
  749. return ret;
  750. }
  751. int wc_Dilithium_PublicKeyDecode(const byte* input, word32* inOutIdx,
  752. dilithium_key* key, word32 inSz)
  753. {
  754. int ret = 0;
  755. byte pubKey[DILITHIUM_MAX_PUB_KEY_SIZE];
  756. word32 pubKeyLen = (word32)sizeof(pubKey);
  757. int keytype = 0;
  758. if (input == NULL || inOutIdx == NULL || key == NULL || inSz == 0) {
  759. return BAD_FUNC_ARG;
  760. }
  761. if ((key->level == 2) && (key->sym == SHAKE_VARIANT)) {
  762. keytype = DILITHIUM_LEVEL2k;
  763. }
  764. else if ((key->level == 3) && (key->sym == SHAKE_VARIANT)) {
  765. keytype = DILITHIUM_LEVEL3k;
  766. }
  767. else if ((key->level == 5) && (key->sym == SHAKE_VARIANT)) {
  768. keytype = DILITHIUM_LEVEL5k;
  769. }
  770. if ((key->level == 2) && (key->sym == AES_VARIANT)) {
  771. keytype = DILITHIUM_AES_LEVEL2k;
  772. }
  773. else if ((key->level == 3) && (key->sym == AES_VARIANT)) {
  774. keytype = DILITHIUM_AES_LEVEL3k;
  775. }
  776. else if ((key->level == 5) && (key->sym == AES_VARIANT)) {
  777. keytype = DILITHIUM_AES_LEVEL5k;
  778. }
  779. else {
  780. return BAD_FUNC_ARG;
  781. }
  782. ret = DecodeAsymKeyPublic(input, inOutIdx, inSz, pubKey, &pubKeyLen,
  783. keytype);
  784. if (ret == 0) {
  785. ret = wc_dilithium_import_public(pubKey, pubKeyLen, key);
  786. }
  787. return ret;
  788. }
  789. #ifdef WC_ENABLE_ASYM_KEY_EXPORT
  790. /* Encode the public part of an Dilithium key in DER.
  791. *
  792. * Pass NULL for output to get the size of the encoding.
  793. *
  794. * @param [in] key Dilithium key object.
  795. * @param [out] output Buffer to put encoded data in.
  796. * @param [in] outLen Size of buffer in bytes.
  797. * @param [in] withAlg Whether to use SubjectPublicKeyInfo format.
  798. * @return Size of encoded data in bytes on success.
  799. * @return BAD_FUNC_ARG when key is NULL.
  800. * @return MEMORY_E when dynamic memory allocation failed.
  801. */
  802. int wc_Dilithium_PublicKeyToDer(dilithium_key* key, byte* output, word32 inLen,
  803. int withAlg)
  804. {
  805. int ret;
  806. byte pubKey[DILITHIUM_MAX_PUB_KEY_SIZE];
  807. word32 pubKeyLen = (word32)sizeof(pubKey);
  808. int keytype = 0;
  809. if (key == NULL || output == NULL) {
  810. return BAD_FUNC_ARG;
  811. }
  812. if ((key->level == 2) && (key->sym == SHAKE_VARIANT)) {
  813. keytype = DILITHIUM_LEVEL2k;
  814. }
  815. else if ((key->level == 3) && (key->sym == SHAKE_VARIANT)) {
  816. keytype = DILITHIUM_LEVEL3k;
  817. }
  818. else if ((key->level == 5) && (key->sym == SHAKE_VARIANT)) {
  819. keytype = DILITHIUM_LEVEL5k;
  820. }
  821. if ((key->level == 2) && (key->sym == AES_VARIANT)) {
  822. keytype = DILITHIUM_AES_LEVEL2k;
  823. }
  824. else if ((key->level == 3) && (key->sym == AES_VARIANT)) {
  825. keytype = DILITHIUM_AES_LEVEL3k;
  826. }
  827. else if ((key->level == 5) && (key->sym == AES_VARIANT)) {
  828. keytype = DILITHIUM_AES_LEVEL5k;
  829. }
  830. else {
  831. return BAD_FUNC_ARG;
  832. }
  833. ret = wc_dilithium_export_public(key, pubKey, &pubKeyLen);
  834. if (ret == 0) {
  835. ret = SetAsymKeyDerPublic(pubKey, pubKeyLen, output, inLen, keytype,
  836. withAlg);
  837. }
  838. return ret;
  839. }
  840. #endif
  841. int wc_Dilithium_KeyToDer(dilithium_key* key, byte* output, word32 inLen)
  842. {
  843. if (key == NULL) {
  844. return BAD_FUNC_ARG;
  845. }
  846. if ((key->level == 2) && (key->sym == SHAKE_VARIANT)) {
  847. return SetAsymKeyDer(key->k, DILITHIUM_LEVEL2_KEY_SIZE, key->p,
  848. DILITHIUM_LEVEL2_KEY_SIZE, output, inLen,
  849. DILITHIUM_LEVEL2k);
  850. }
  851. else if ((key->level == 3) && (key->sym == SHAKE_VARIANT)) {
  852. return SetAsymKeyDer(key->k, DILITHIUM_LEVEL3_KEY_SIZE, key->p,
  853. DILITHIUM_LEVEL3_KEY_SIZE, output, inLen,
  854. DILITHIUM_LEVEL3k);
  855. }
  856. else if ((key->level == 5) && (key->sym == SHAKE_VARIANT)) {
  857. return SetAsymKeyDer(key->k, DILITHIUM_LEVEL5_KEY_SIZE, key->p,
  858. DILITHIUM_LEVEL5_KEY_SIZE, output, inLen,
  859. DILITHIUM_LEVEL5k);
  860. }
  861. else if ((key->level == 2) && (key->sym == AES_VARIANT)) {
  862. return SetAsymKeyDer(key->k, DILITHIUM_LEVEL2_KEY_SIZE, key->p,
  863. DILITHIUM_LEVEL2_KEY_SIZE, output, inLen,
  864. DILITHIUM_AES_LEVEL2k);
  865. }
  866. else if ((key->level == 3) && (key->sym == AES_VARIANT)) {
  867. return SetAsymKeyDer(key->k, DILITHIUM_LEVEL3_KEY_SIZE, key->p,
  868. DILITHIUM_LEVEL3_KEY_SIZE, output, inLen,
  869. DILITHIUM_AES_LEVEL3k);
  870. }
  871. else if ((key->level == 5) && (key->sym == AES_VARIANT)) {
  872. return SetAsymKeyDer(key->k, DILITHIUM_LEVEL5_KEY_SIZE, key->p,
  873. DILITHIUM_LEVEL5_KEY_SIZE, output, inLen,
  874. DILITHIUM_AES_LEVEL5k);
  875. }
  876. return BAD_FUNC_ARG;
  877. }
  878. int wc_Dilithium_PrivateKeyToDer(dilithium_key* key, byte* output, word32 inLen)
  879. {
  880. if (key == NULL) {
  881. return BAD_FUNC_ARG;
  882. }
  883. if ((key->level == 2) && (key->sym == SHAKE_VARIANT)) {
  884. return SetAsymKeyDer(key->k, DILITHIUM_LEVEL2_KEY_SIZE, NULL, 0, output,
  885. inLen, DILITHIUM_LEVEL2k);
  886. }
  887. else if ((key->level == 3) && (key->sym == SHAKE_VARIANT)) {
  888. return SetAsymKeyDer(key->k, DILITHIUM_LEVEL3_KEY_SIZE, NULL, 0, output,
  889. inLen, DILITHIUM_LEVEL3k);
  890. }
  891. else if ((key->level == 5) && (key->sym == SHAKE_VARIANT)) {
  892. return SetAsymKeyDer(key->k, DILITHIUM_LEVEL5_KEY_SIZE, NULL, 0, output,
  893. inLen, DILITHIUM_LEVEL5k);
  894. }
  895. else if ((key->level == 2) && (key->sym == AES_VARIANT)) {
  896. return SetAsymKeyDer(key->k, DILITHIUM_LEVEL2_KEY_SIZE, NULL, 0, output,
  897. inLen, DILITHIUM_AES_LEVEL2k);
  898. }
  899. else if ((key->level == 3) && (key->sym == AES_VARIANT)) {
  900. return SetAsymKeyDer(key->k, DILITHIUM_LEVEL3_KEY_SIZE, NULL, 0, output,
  901. inLen, DILITHIUM_AES_LEVEL3k);
  902. }
  903. else if ((key->level == 5) && (key->sym == AES_VARIANT)) {
  904. return SetAsymKeyDer(key->k, DILITHIUM_LEVEL5_KEY_SIZE, NULL, 0, output,
  905. inLen, DILITHIUM_AES_LEVEL5k);
  906. }
  907. return BAD_FUNC_ARG;
  908. }
  909. #endif /* HAVE_PQC && HAVE_DILITHIUM */