hpke.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  1. /* hpke.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. /* The HPKE supports ECC and X25519 with AES GCM only.
  22. * TODO: Add X448 and ChaCha20
  23. */
  24. #ifdef HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27. #include <wolfssl/wolfcrypt/settings.h>
  28. #if defined(HAVE_HPKE) && (defined(HAVE_ECC) || defined(HAVE_CURVE25519)) && \
  29. defined(HAVE_AESGCM)
  30. #include <wolfssl/wolfcrypt/error-crypt.h>
  31. #include <wolfssl/wolfcrypt/ecc.h>
  32. #include <wolfssl/wolfcrypt/curve25519.h>
  33. #include <wolfssl/wolfcrypt/curve448.h>
  34. #include <wolfssl/wolfcrypt/hmac.h>
  35. #include <wolfssl/wolfcrypt/hash.h>
  36. #include <wolfssl/wolfcrypt/sha256.h>
  37. #include <wolfssl/wolfcrypt/sha512.h>
  38. #include <wolfssl/wolfcrypt/aes.h>
  39. #include <wolfssl/wolfcrypt/hpke.h>
  40. #ifdef NO_INLINE
  41. #include <wolfssl/wolfcrypt/misc.h>
  42. #else
  43. #define WOLFSSL_MISC_INCLUDED
  44. #include <wolfcrypt/src/misc.c>
  45. #endif
  46. const int hpkeSupportedKem[HPKE_SUPPORTED_KEM_LEN] = {
  47. DHKEM_P256_HKDF_SHA256,
  48. DHKEM_P384_HKDF_SHA384,
  49. DHKEM_P521_HKDF_SHA512,
  50. DHKEM_X25519_HKDF_SHA256,
  51. };
  52. const int hpkeSupportedKdf[HPKE_SUPPORTED_KDF_LEN] = {
  53. HKDF_SHA256,
  54. HKDF_SHA384,
  55. HKDF_SHA512,
  56. };
  57. const int hpkeSupportedAead[HPKE_SUPPORTED_AEAD_LEN] = {
  58. HPKE_AES_128_GCM,
  59. HPKE_AES_256_GCM,
  60. };
  61. static const char* KEM_STR = "KEM";
  62. static const int KEM_STR_LEN = 3;
  63. static const char* HPKE_STR = "HPKE";
  64. static const int HPKE_STR_LEN = 4;
  65. static const char* HPKE_VERSION_STR = "HPKE-v1";
  66. static const int HPKE_VERSION_STR_LEN = 7;
  67. static const char* EAE_PRK_LABEL_STR = "eae_prk";
  68. static const int EAE_PRK_LABEL_STR_LEN = 7;
  69. static const char* SHARED_SECRET_LABEL_STR = "shared_secret";
  70. static const int SHARED_SECRET_LABEL_STR_LEN = 13;
  71. static const char* PSK_ID_HASH_LABEL_STR = "psk_id_hash";
  72. static const int PSK_ID_HASH_LABEL_STR_LEN = 11;
  73. static const char* INFO_HASH_LABEL_STR = "info_hash";
  74. static const int INFO_HASH_LABEL_STR_LEN = 9;
  75. static const char* SECRET_LABEL_STR = "secret";
  76. static const int SECRET_LABEL_STR_LEN = 6;
  77. static const char* KEY_LABEL_STR = "key";
  78. static const int KEY_LABEL_STR_LEN = 3;
  79. static const char* BASE_NONCE_LABEL_STR = "base_nonce";
  80. static const int BASE_NONCE_LABEL_STR_LEN = 10;
  81. static const char* EXP_LABEL_STR = "exp";
  82. static const int EXP_LABEL_STR_LEN = 3;
  83. /* encode n as a byte string with length w, return 0 or error */
  84. static int I2OSP(int n, int w, byte* out)
  85. {
  86. int i;
  87. if (w <= 0 || w > 32) {
  88. return MP_VAL;
  89. }
  90. /* if width is less than int max check that n is less than w bytes max */
  91. /* if width is greater than int max check that n is less than int max */
  92. if ((w < 4 && n > ((1 << (w * 8)) - 1)) || (w >= 4 && n > 0x7fffffff)) {
  93. return MP_VAL;
  94. }
  95. /* make sure the byte string is cleared */
  96. XMEMSET(out, 0, (size_t)w);
  97. for (i = 0; i < w && n > 0; i++) {
  98. out[w-(i + 1)] = (byte)n;
  99. n >>= 8;
  100. }
  101. return 0;
  102. }
  103. /* initialize the hpke struct with the desired ciphersuites, return 0 or error*/
  104. int wc_HpkeInit(Hpke* hpke, int kem, int kdf, int aead, void* heap)
  105. {
  106. int ret;
  107. byte* id;
  108. if (hpke == NULL || kem == 0 || kdf == 0 || aead == 0) {
  109. return BAD_FUNC_ARG;
  110. }
  111. XMEMSET(hpke, 0, sizeof(*hpke));
  112. hpke->kem = (word32)kem;
  113. hpke->kdf = (word32)kdf;
  114. hpke->aead = (word32)aead;
  115. hpke->heap = heap;
  116. /* set kem_suite_id */
  117. id = hpke->kem_suite_id;
  118. XMEMCPY(id, KEM_STR, KEM_STR_LEN);
  119. id += KEM_STR_LEN;
  120. ret = I2OSP(kem, 2, id);
  121. /* set hpke_suite_id */
  122. id = hpke->hpke_suite_id;
  123. XMEMCPY(id, HPKE_STR, HPKE_STR_LEN);
  124. id += HPKE_STR_LEN;
  125. if (ret == 0) {
  126. ret = I2OSP(kem, 2, id);
  127. id += 2;
  128. }
  129. if (ret == 0) {
  130. ret = I2OSP(kdf, 2, id);
  131. id += 2;
  132. }
  133. if (ret == 0) {
  134. ret = I2OSP(aead, 2, id);
  135. }
  136. if (ret == 0) {
  137. switch (kem) {
  138. #if defined(HAVE_ECC)
  139. #if defined(WOLFSSL_SHA224) || !defined(NO_SHA256)
  140. case DHKEM_P256_HKDF_SHA256:
  141. hpke->curve_id = ECC_SECP256R1;
  142. hpke->Nsecret = WC_SHA256_DIGEST_SIZE;
  143. hpke->Nh = WC_SHA256_DIGEST_SIZE;
  144. hpke->Ndh = (word32)wc_ecc_get_curve_size_from_id(hpke->curve_id);
  145. hpke->Npk = 1 + hpke->Ndh * 2;
  146. break;
  147. #endif
  148. #ifdef WOLFSSL_SHA384
  149. case DHKEM_P384_HKDF_SHA384:
  150. hpke->curve_id = ECC_SECP384R1;
  151. hpke->Nsecret = WC_SHA384_DIGEST_SIZE;
  152. hpke->Nh = WC_SHA384_DIGEST_SIZE;
  153. hpke->Ndh = (word32)wc_ecc_get_curve_size_from_id(hpke->curve_id);
  154. hpke->Npk = 1 + hpke->Ndh * 2;
  155. break;
  156. #endif
  157. #if defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)
  158. case DHKEM_P521_HKDF_SHA512:
  159. hpke->curve_id = ECC_SECP521R1;
  160. hpke->Nsecret = WC_SHA512_DIGEST_SIZE;
  161. hpke->Nh = WC_SHA512_DIGEST_SIZE;
  162. hpke->Ndh = (word32)wc_ecc_get_curve_size_from_id(hpke->curve_id);
  163. hpke->Npk = 1 + hpke->Ndh * 2;
  164. break;
  165. #endif
  166. #endif
  167. #if defined(HAVE_CURVE25519) &&\
  168. (defined(WOLFSSL_SHA224) || !defined(NO_SHA256))
  169. case DHKEM_X25519_HKDF_SHA256:
  170. hpke->Nsecret = WC_SHA256_DIGEST_SIZE;
  171. hpke->Nh = WC_SHA256_DIGEST_SIZE;
  172. hpke->Ndh = CURVE25519_KEYSIZE;
  173. hpke->Npk = CURVE25519_PUB_KEY_SIZE;
  174. break;
  175. #endif
  176. #if defined(HAVE_CURVE448) &&\
  177. (defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512))
  178. case DHKEM_X448_HKDF_SHA512:
  179. hpke->Nsecret = WC_SHA512_DIGEST_SIZE;
  180. hpke->Nh = WC_SHA512_DIGEST_SIZE;
  181. /* size of x448 shared secret */
  182. hpke->Ndh = 64;
  183. hpke->Npk = CURVE448_PUB_KEY_SIZE;
  184. ret = BAD_FUNC_ARG; /* TODO: Add X448 */
  185. break;
  186. #endif
  187. default:
  188. ret = BAD_FUNC_ARG;
  189. break;
  190. }
  191. }
  192. if (ret == 0) {
  193. switch (kdf) {
  194. case HKDF_SHA256:
  195. hpke->kdf_digest = WC_SHA256;
  196. break;
  197. case HKDF_SHA384:
  198. hpke->kdf_digest = WC_SHA384;
  199. break;
  200. case HKDF_SHA512:
  201. hpke->kdf_digest = WC_SHA512;
  202. break;
  203. default:
  204. ret = BAD_FUNC_ARG;
  205. break;
  206. }
  207. }
  208. if (ret == 0) {
  209. switch (aead) {
  210. case HPKE_AES_128_GCM:
  211. hpke->Nk = AES_128_KEY_SIZE;
  212. hpke->Nn = GCM_NONCE_MID_SZ;
  213. hpke->Nt = AES_BLOCK_SIZE;
  214. break;
  215. case HPKE_AES_256_GCM:
  216. hpke->Nk = AES_256_KEY_SIZE;
  217. hpke->Nn = GCM_NONCE_MID_SZ;
  218. hpke->Nt = AES_BLOCK_SIZE;
  219. break;
  220. default:
  221. ret = BAD_FUNC_ARG;
  222. break;
  223. }
  224. }
  225. if ((int)hpke->Ndh < 0) {
  226. return (int)hpke->Ndh;
  227. }
  228. return ret;
  229. }
  230. /* generate a keypair for use with the supplied hpke kem method, return 0 or
  231. * error */
  232. int wc_HpkeGenerateKeyPair(Hpke* hpke, void** keypair, WC_RNG* rng)
  233. {
  234. int ret = 0;
  235. if (hpke == NULL || keypair == NULL || rng == NULL)
  236. return BAD_FUNC_ARG;
  237. switch (hpke->kem) {
  238. #if defined(HAVE_ECC)
  239. case DHKEM_P256_HKDF_SHA256:
  240. *keypair = wc_ecc_key_new(hpke->heap);
  241. if (*keypair != NULL)
  242. ret = wc_ecc_make_key_ex(rng, 32, (ecc_key*)*keypair,
  243. ECC_SECP256R1);
  244. break;
  245. case DHKEM_P384_HKDF_SHA384:
  246. *keypair = wc_ecc_key_new(hpke->heap);
  247. if (*keypair != NULL)
  248. ret = wc_ecc_make_key_ex(rng, 48, (ecc_key*)*keypair,
  249. ECC_SECP384R1);
  250. break;
  251. case DHKEM_P521_HKDF_SHA512:
  252. *keypair = wc_ecc_key_new(hpke->heap);
  253. if (*keypair != NULL)
  254. ret = wc_ecc_make_key_ex(rng, 66, (ecc_key*)*keypair,
  255. ECC_SECP521R1);
  256. break;
  257. #endif
  258. #if defined(HAVE_CURVE25519)
  259. case DHKEM_X25519_HKDF_SHA256:
  260. *keypair = XMALLOC(sizeof(curve25519_key), hpke->heap,
  261. DYNAMIC_TYPE_CURVE25519);
  262. if (*keypair != NULL) {
  263. ret = wc_curve25519_init_ex((curve25519_key*)*keypair,
  264. hpke->heap, INVALID_DEVID);
  265. if (ret == 0)
  266. ret = wc_curve25519_make_key(rng, 32,
  267. (curve25519_key*)*keypair);
  268. }
  269. break;
  270. #endif
  271. case DHKEM_X448_HKDF_SHA512:
  272. /* TODO: Add X448 */
  273. default:
  274. ret = BAD_FUNC_ARG;
  275. break;
  276. }
  277. if (ret == 0 && *keypair == NULL)
  278. ret = MEMORY_E;
  279. if (ret != 0 && *keypair != NULL) {
  280. wc_HpkeFreeKey(hpke, (word16)hpke->kem, *keypair, hpke->heap);
  281. *keypair = NULL;
  282. }
  283. return ret;
  284. }
  285. /* encode the provided kem key into a byte string, return 0 or error */
  286. int wc_HpkeSerializePublicKey(Hpke* hpke, void* key, byte* out, word16* outSz)
  287. {
  288. int ret;
  289. word32 tmpOutSz;
  290. if (hpke == NULL || key == NULL || out == NULL || outSz == NULL) {
  291. return BAD_FUNC_ARG;
  292. }
  293. tmpOutSz = *outSz;
  294. switch (hpke->kem)
  295. {
  296. #if defined(HAVE_ECC)
  297. case DHKEM_P256_HKDF_SHA256:
  298. case DHKEM_P384_HKDF_SHA384:
  299. case DHKEM_P521_HKDF_SHA512:
  300. /* export x963 uncompressed */
  301. ret = wc_ecc_export_x963_ex((ecc_key*)key, out, &tmpOutSz, 0);
  302. break;
  303. #endif
  304. #if defined(HAVE_CURVE25519)
  305. case DHKEM_X25519_HKDF_SHA256:
  306. ret = wc_curve25519_export_public_ex((curve25519_key*)key, out,
  307. &tmpOutSz, EC25519_LITTLE_ENDIAN);
  308. break;
  309. #endif
  310. case DHKEM_X448_HKDF_SHA512:
  311. default:
  312. ret = -1;
  313. break;
  314. }
  315. *outSz = (word16)tmpOutSz;
  316. return ret;
  317. }
  318. /* load a serialized kem key into a wolfcrypt key struct depending on the kem */
  319. int wc_HpkeDeserializePublicKey(Hpke* hpke, void** key, const byte* in,
  320. word16 inSz)
  321. {
  322. int ret = 0;
  323. if (hpke == NULL || key == NULL || in == NULL) {
  324. return BAD_FUNC_ARG;
  325. }
  326. if (inSz < (word32)hpke->Npk) {
  327. return BUFFER_E;
  328. }
  329. switch (hpke->kem)
  330. {
  331. #if defined(HAVE_ECC)
  332. case DHKEM_P256_HKDF_SHA256:
  333. case DHKEM_P384_HKDF_SHA384:
  334. case DHKEM_P521_HKDF_SHA512:
  335. /* init the ecc key */
  336. *key = wc_ecc_key_new(hpke->heap);
  337. if (*key != NULL) {
  338. /* import the x963 key */
  339. ret = wc_ecc_import_x963_ex(in, inSz, (ecc_key*)*key,
  340. hpke->curve_id);
  341. }
  342. break;
  343. #endif
  344. #if defined(HAVE_CURVE25519)
  345. case DHKEM_X25519_HKDF_SHA256:
  346. *key = XMALLOC(sizeof(curve25519_key), hpke->heap,
  347. DYNAMIC_TYPE_CURVE25519);
  348. if (*key != NULL) {
  349. ret = wc_curve25519_init_ex((curve25519_key*)*key, hpke->heap,
  350. INVALID_DEVID);
  351. if (ret == 0)
  352. ret = wc_curve25519_import_public_ex(in, inSz,
  353. (curve25519_key*)*key, EC25519_LITTLE_ENDIAN);
  354. }
  355. break;
  356. #endif
  357. case DHKEM_X448_HKDF_SHA512:
  358. default:
  359. ret = -1;
  360. break;
  361. }
  362. if (ret == 0 && *key == NULL)
  363. ret = MEMORY_E;
  364. if (ret != 0 && *key != NULL) {
  365. wc_HpkeFreeKey(hpke, (word16)hpke->kem, *key, hpke->heap);
  366. *key = NULL;
  367. }
  368. return ret;
  369. }
  370. /* free a kem key */
  371. void wc_HpkeFreeKey(Hpke* hpke, word16 kem, void* keypair, void* heap)
  372. {
  373. switch (kem)
  374. {
  375. #if defined(HAVE_ECC)
  376. case DHKEM_P256_HKDF_SHA256:
  377. case DHKEM_P384_HKDF_SHA384:
  378. case DHKEM_P521_HKDF_SHA512:
  379. wc_ecc_key_free((ecc_key*)keypair);
  380. break;
  381. #endif
  382. #if defined(HAVE_CURVE25519)
  383. case DHKEM_X25519_HKDF_SHA256:
  384. wc_curve25519_free((curve25519_key*)keypair);
  385. XFREE(keypair, heap, DYNAMIC_TYPE_CURVE25519);
  386. break;
  387. #endif
  388. case DHKEM_X448_HKDF_SHA512:
  389. /* TODO: Add X448 */
  390. default:
  391. break;
  392. }
  393. (void)hpke;
  394. (void)heap;
  395. }
  396. static int wc_HpkeLabeledExtract(Hpke* hpke, byte* suite_id,
  397. word32 suite_id_len, byte* salt, word32 salt_len, byte* label,
  398. word32 label_len, byte* ikm, word32 ikm_len, byte* out)
  399. {
  400. int ret;
  401. byte* labeled_ikm_p;
  402. #ifndef WOLFSSL_SMALL_STACK
  403. byte labeled_ikm[MAX_HPKE_LABEL_SZ];
  404. #else
  405. byte* labeled_ikm;
  406. #endif
  407. if (hpke == NULL) {
  408. return BAD_FUNC_ARG;
  409. }
  410. #ifdef WOLFSSL_SMALL_STACK
  411. labeled_ikm = (byte*)XMALLOC(MAX_HPKE_LABEL_SZ, hpke->heap,
  412. DYNAMIC_TYPE_TMP_BUFFER);
  413. if (labeled_ikm == NULL) {
  414. return MEMORY_E;
  415. }
  416. #endif
  417. /* concat the labeled_ikm */
  418. /* version */
  419. XMEMCPY(labeled_ikm, HPKE_VERSION_STR, HPKE_VERSION_STR_LEN);
  420. labeled_ikm_p = labeled_ikm + HPKE_VERSION_STR_LEN;
  421. /* suite_id */
  422. XMEMCPY(labeled_ikm_p, suite_id, suite_id_len);
  423. labeled_ikm_p += suite_id_len;
  424. /* label */
  425. XMEMCPY(labeled_ikm_p, label, label_len);
  426. labeled_ikm_p += label_len;
  427. /* ikm */
  428. if (ikm_len != 0) {
  429. XMEMCPY(labeled_ikm_p, ikm, ikm_len);
  430. labeled_ikm_p += ikm_len;
  431. }
  432. /* call extract */
  433. PRIVATE_KEY_UNLOCK();
  434. ret = wc_HKDF_Extract(hpke->kdf_digest, salt, salt_len, labeled_ikm,
  435. (word32)(size_t)(labeled_ikm_p - labeled_ikm), out);
  436. PRIVATE_KEY_LOCK();
  437. #ifdef WOLFSSL_SMALL_STACK
  438. XFREE(labeled_ikm, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  439. #endif
  440. return ret;
  441. }
  442. /* do hkdf expand with the format specified in the hpke rfc, return 0 or
  443. * error */
  444. static int wc_HpkeLabeledExpand(Hpke* hpke, byte* suite_id, word32 suite_id_len,
  445. byte* prk, word32 prk_len, byte* label, word32 label_len, byte* info,
  446. word32 infoSz, word32 L, byte* out)
  447. {
  448. int ret;
  449. byte* labeled_info_p;
  450. #ifndef WOLFSSL_SMALL_STACK
  451. byte labeled_info[MAX_HPKE_LABEL_SZ];
  452. #else
  453. byte* labeled_info;
  454. #endif
  455. if (hpke == NULL) {
  456. return BAD_FUNC_ARG;
  457. }
  458. #ifdef WOLFSSL_SMALL_STACK
  459. labeled_info = (byte*)XMALLOC(MAX_HPKE_LABEL_SZ, hpke->heap,
  460. DYNAMIC_TYPE_TMP_BUFFER);
  461. if (labeled_info == NULL) {
  462. return MEMORY_E;
  463. }
  464. #endif
  465. /* copy length */
  466. ret = I2OSP((int)L, 2, labeled_info);
  467. labeled_info_p = labeled_info + 2;
  468. if (ret == 0) {
  469. /* version */
  470. XMEMCPY(labeled_info_p, HPKE_VERSION_STR, HPKE_VERSION_STR_LEN);
  471. labeled_info_p += HPKE_VERSION_STR_LEN;
  472. /* suite_id */
  473. XMEMCPY(labeled_info_p, suite_id, suite_id_len);
  474. labeled_info_p += suite_id_len;
  475. /* label */
  476. XMEMCPY(labeled_info_p, label, label_len);
  477. labeled_info_p += label_len;
  478. /* info */
  479. XMEMCPY(labeled_info_p, info, infoSz);
  480. labeled_info_p += infoSz;
  481. /* call expand */
  482. PRIVATE_KEY_UNLOCK();
  483. ret = wc_HKDF_Expand(hpke->kdf_digest,
  484. prk, prk_len,
  485. labeled_info, (word32)(size_t)(labeled_info_p - labeled_info),
  486. out, L);
  487. PRIVATE_KEY_LOCK();
  488. }
  489. #ifdef WOLFSSL_SMALL_STACK
  490. XFREE(labeled_info, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  491. #endif
  492. return ret;
  493. }
  494. /* compute the current nonce from the base nonce using the sequence value,
  495. * return 0 or error */
  496. static int wc_HpkeContextComputeNonce(Hpke* hpke, HpkeBaseContext* context,
  497. byte* out)
  498. {
  499. int ret;
  500. byte seq_bytes[HPKE_Nn_MAX];
  501. /* convert the sequence into a byte string with the same length as the
  502. * nonce */
  503. ret = I2OSP(context->seq, (int)hpke->Nn, seq_bytes);
  504. if (ret == 0) {
  505. xorbufout(out, context->base_nonce, seq_bytes, hpke->Nn);
  506. }
  507. return ret;
  508. }
  509. /* call extract and expand as specified in the hpke rfc, return 0 or error */
  510. static int wc_HpkeExtractAndExpand( Hpke* hpke, byte* dh, word32 dh_len,
  511. byte* kemContext, word32 kem_context_length, byte* sharedSecret)
  512. {
  513. int ret;
  514. /* max length is the largest hmac digest possible */
  515. #ifndef WOLFSSL_SMALL_STACK
  516. byte eae_prk[WC_MAX_DIGEST_SIZE];
  517. #else
  518. byte* eae_prk;
  519. #endif
  520. if (hpke == NULL) {
  521. return BAD_FUNC_ARG;
  522. }
  523. #ifdef WOLFSSL_SMALL_STACK
  524. eae_prk = (byte*)XMALLOC(WC_MAX_DIGEST_SIZE, hpke->heap,
  525. DYNAMIC_TYPE_DIGEST);
  526. if (eae_prk == NULL) {
  527. return MEMORY_E;
  528. }
  529. #endif
  530. /* extract */
  531. ret = wc_HpkeLabeledExtract(hpke, hpke->kem_suite_id,
  532. sizeof( hpke->kem_suite_id ), NULL, 0, (byte*)EAE_PRK_LABEL_STR,
  533. EAE_PRK_LABEL_STR_LEN, dh, dh_len, eae_prk);
  534. /* expand */
  535. if ( ret == 0 )
  536. ret = wc_HpkeLabeledExpand(hpke, hpke->kem_suite_id,
  537. sizeof( hpke->kem_suite_id ), eae_prk, hpke->Nh,
  538. (byte*)SHARED_SECRET_LABEL_STR, SHARED_SECRET_LABEL_STR_LEN,
  539. kemContext, kem_context_length, hpke->Nsecret, sharedSecret);
  540. #ifdef WOLFSSL_SMALL_STACK
  541. XFREE(eae_prk, hpke->heap, DYNAMIC_TYPE_DIGEST);
  542. #endif
  543. return ret;
  544. }
  545. /* derive the key, nonce and exporter secret and store them in the context
  546. * struct, return 0 or error */
  547. static int wc_HpkeKeyScheduleBase(Hpke* hpke, HpkeBaseContext* context,
  548. byte* sharedSecret, byte* info, word32 infoSz)
  549. {
  550. int ret;
  551. #ifndef WOLFSSL_SMALL_STACK
  552. /* 1 for mode and WC_MAX_DIGEST_SIZE times 2 for psk_id_hash and */
  553. /* info_hash */
  554. byte key_schedule_context[1 + 2 * WC_MAX_DIGEST_SIZE];
  555. /* maximum size of secret is largest hash of extract */
  556. byte secret[WC_MAX_DIGEST_SIZE];
  557. #else
  558. byte* key_schedule_context = NULL;
  559. byte* secret = NULL;
  560. #endif
  561. if (hpke == NULL) {
  562. return BAD_FUNC_ARG;
  563. }
  564. #ifdef WOLFSSL_SMALL_STACK
  565. key_schedule_context = (byte*)XMALLOC((1 + 2 * WC_MAX_DIGEST_SIZE),
  566. hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  567. secret = (byte*)XMALLOC(WC_MAX_DIGEST_SIZE, hpke->heap,
  568. DYNAMIC_TYPE_DIGEST);
  569. if (key_schedule_context == NULL || secret == NULL) {
  570. XFREE(key_schedule_context, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  571. XFREE(secret, hpke->heap, DYNAMIC_TYPE_DIGEST);
  572. return MEMORY_E;
  573. }
  574. #endif
  575. /* set the sequence to 0 */
  576. context->seq = 0;
  577. /* 0 for mode */
  578. key_schedule_context[0] = 0;
  579. /* extract psk_id, which for base is null */
  580. ret = wc_HpkeLabeledExtract(hpke, hpke->hpke_suite_id,
  581. sizeof( hpke->hpke_suite_id ), NULL, 0, (byte*)PSK_ID_HASH_LABEL_STR,
  582. PSK_ID_HASH_LABEL_STR_LEN, NULL, 0, key_schedule_context + 1);
  583. /* extract info */
  584. if (ret == 0) {
  585. ret = wc_HpkeLabeledExtract(hpke, hpke->hpke_suite_id,
  586. sizeof( hpke->hpke_suite_id ), NULL, 0, (byte*)INFO_HASH_LABEL_STR,
  587. INFO_HASH_LABEL_STR_LEN, info, infoSz,
  588. key_schedule_context + 1 + hpke->Nh);
  589. }
  590. /* extract secret */
  591. if (ret == 0) {
  592. ret = wc_HpkeLabeledExtract(hpke, hpke->hpke_suite_id,
  593. sizeof( hpke->hpke_suite_id ), sharedSecret, hpke->Nsecret,
  594. (byte*)SECRET_LABEL_STR, SECRET_LABEL_STR_LEN, NULL, 0, secret);
  595. }
  596. /* expand key */
  597. if (ret == 0)
  598. ret = wc_HpkeLabeledExpand(hpke, hpke->hpke_suite_id,
  599. sizeof( hpke->hpke_suite_id ), secret, hpke->Nh,
  600. (byte*)KEY_LABEL_STR, KEY_LABEL_STR_LEN, key_schedule_context,
  601. 1 + 2 * hpke->Nh, hpke->Nk, context->key);
  602. /* expand nonce */
  603. if (ret == 0) {
  604. ret = wc_HpkeLabeledExpand(hpke, hpke->hpke_suite_id,
  605. sizeof( hpke->hpke_suite_id ), secret, hpke->Nh,
  606. (byte*)BASE_NONCE_LABEL_STR, BASE_NONCE_LABEL_STR_LEN,
  607. key_schedule_context, 1 + 2 * hpke->Nh, hpke->Nn,
  608. context->base_nonce);
  609. }
  610. /* expand exporter_secret */
  611. if (ret == 0) {
  612. ret = wc_HpkeLabeledExpand(hpke, hpke->hpke_suite_id,
  613. sizeof( hpke->hpke_suite_id ), secret, hpke->Nh,
  614. (byte*)EXP_LABEL_STR, EXP_LABEL_STR_LEN, key_schedule_context,
  615. 1 + 2 * hpke->Nh, hpke->Nh, context->exporter_secret);
  616. }
  617. #ifdef WOLFSSL_SMALL_STACK
  618. XFREE(key_schedule_context, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  619. XFREE(secret, hpke->heap, DYNAMIC_TYPE_DIGEST);
  620. #endif
  621. return ret;
  622. }
  623. /* compute the shared secret from the ephemeral and receiver kem keys */
  624. static int wc_HpkeEncap(Hpke* hpke, void* ephemeralKey, void* receiverKey,
  625. byte* sharedSecret)
  626. {
  627. int ret;
  628. #ifdef ECC_TIMING_RESISTANT
  629. WC_RNG* rng;
  630. #endif
  631. word32 dh_len;
  632. word16 receiverPubKeySz;
  633. word16 ephemeralPubKeySz;
  634. #ifndef WOLFSSL_SMALL_STACK
  635. byte dh[HPKE_Ndh_MAX];
  636. byte kemContext[HPKE_Npk_MAX * 2];
  637. #else
  638. byte* dh = NULL;
  639. byte* kemContext = NULL;
  640. #endif
  641. if (hpke == NULL || ephemeralKey == NULL || receiverKey == NULL ||
  642. sharedSecret == NULL) {
  643. return BAD_FUNC_ARG;
  644. }
  645. receiverPubKeySz = (word16)hpke->Npk;
  646. ephemeralPubKeySz = (word16)hpke->Npk;
  647. #ifdef WOLFSSL_SMALL_STACK
  648. dh = (byte*)XMALLOC(hpke->Ndh, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  649. kemContext = (byte*)XMALLOC(hpke->Npk * 2, hpke->heap,
  650. DYNAMIC_TYPE_TMP_BUFFER);
  651. if (dh == NULL || kemContext == NULL) {
  652. XFREE(dh, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  653. XFREE(kemContext, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  654. return MEMORY_E;
  655. }
  656. #endif
  657. /* generate dh */
  658. dh_len = hpke->Ndh;
  659. switch (hpke->kem)
  660. {
  661. #if defined(HAVE_ECC)
  662. case DHKEM_P256_HKDF_SHA256:
  663. case DHKEM_P384_HKDF_SHA384:
  664. case DHKEM_P521_HKDF_SHA512:
  665. #ifdef ECC_TIMING_RESISTANT
  666. rng = wc_rng_new(NULL, 0, hpke->heap);
  667. if (rng == NULL) {
  668. ret = RNG_FAILURE_E;
  669. break;
  670. }
  671. wc_ecc_set_rng((ecc_key*)ephemeralKey, rng);
  672. #endif
  673. ret = wc_ecc_shared_secret((ecc_key*)ephemeralKey,
  674. (ecc_key*)receiverKey, dh, &dh_len);
  675. #ifdef ECC_TIMING_RESISTANT
  676. wc_rng_free(rng);
  677. #endif
  678. break;
  679. #endif
  680. #if defined(HAVE_CURVE25519)
  681. case DHKEM_X25519_HKDF_SHA256:
  682. ret = wc_curve25519_shared_secret_ex((curve25519_key*)ephemeralKey,
  683. (curve25519_key*)receiverKey, dh, &dh_len,
  684. EC25519_LITTLE_ENDIAN);
  685. break;
  686. #endif
  687. case DHKEM_X448_HKDF_SHA512:
  688. /* TODO: Add X448 */
  689. default:
  690. ret = -1;
  691. break;
  692. }
  693. if (ret == 0) {
  694. /* serialize ephemeralKey into kemContext */
  695. ret = wc_HpkeSerializePublicKey(hpke, ephemeralKey,
  696. kemContext, &ephemeralPubKeySz);
  697. }
  698. if (ret == 0) {
  699. /* serialize pkR into kemContext */
  700. ret = wc_HpkeSerializePublicKey(hpke, receiverKey,
  701. kemContext + ephemeralPubKeySz, &receiverPubKeySz);
  702. }
  703. if (ret == 0) {
  704. /* compute the shared secret */
  705. ret = wc_HpkeExtractAndExpand(hpke, dh, dh_len, kemContext,
  706. hpke->Npk * 2, sharedSecret);
  707. }
  708. #ifdef WOLFSSL_SMALL_STACK
  709. XFREE(dh, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  710. XFREE(kemContext, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  711. #endif
  712. return ret;
  713. }
  714. /* setup the sender context with shared key, nonce and exporter secret */
  715. static int wc_HpkeSetupBaseSender(Hpke* hpke, HpkeBaseContext* context,
  716. void* ephemeralKey, void* receiverKey, byte* info, word32 infoSz)
  717. {
  718. int ret;
  719. #ifndef WOLFSSL_SMALL_STACK
  720. byte sharedSecret[HPKE_Nsecret_MAX];
  721. #else
  722. byte* sharedSecret;
  723. #endif
  724. if (hpke == NULL) {
  725. return BAD_FUNC_ARG;
  726. }
  727. #ifdef WOLFSSL_SMALL_STACK
  728. sharedSecret = (byte*)XMALLOC(hpke->Nsecret, hpke->heap,
  729. DYNAMIC_TYPE_TMP_BUFFER);
  730. #endif
  731. /* encap */
  732. ret = wc_HpkeEncap(hpke, ephemeralKey, receiverKey, sharedSecret);
  733. /* schedule */
  734. if (ret == 0) {
  735. ret = wc_HpkeKeyScheduleBase(hpke, context, sharedSecret, info,
  736. infoSz);
  737. }
  738. #ifdef WOLFSSL_SMALL_STACK
  739. XFREE(sharedSecret, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  740. #endif
  741. return ret;
  742. }
  743. /* encrypt a message using an hpke base context, return 0 or error */
  744. static int wc_HpkeContextSealBase(Hpke* hpke, HpkeBaseContext* context,
  745. byte* aad, word32 aadSz, byte* plaintext, word32 ptSz, byte* out)
  746. {
  747. int ret;
  748. byte nonce[HPKE_Nn_MAX];
  749. #ifndef WOLFSSL_SMALL_STACK
  750. Aes aes_key[1];
  751. #else
  752. Aes* aes_key;
  753. #endif
  754. if (hpke == NULL) {
  755. return BAD_FUNC_ARG;
  756. }
  757. #ifdef WOLFSSL_SMALL_STACK
  758. aes_key = (Aes*)XMALLOC(sizeof(Aes), hpke->heap, DYNAMIC_TYPE_AES);
  759. if (aes_key == NULL) {
  760. return MEMORY_E;
  761. }
  762. #endif
  763. ret = wc_AesInit(aes_key, hpke->heap, INVALID_DEVID);
  764. if (ret == 0) {
  765. ret = wc_HpkeContextComputeNonce(hpke, context, nonce);
  766. if (ret == 0) {
  767. ret = wc_AesGcmSetKey(aes_key, context->key, hpke->Nk);
  768. }
  769. if (ret == 0) {
  770. ret = wc_AesGcmEncrypt(aes_key, out, plaintext, ptSz, nonce,
  771. hpke->Nn, out + ptSz, hpke->Nt, aad, aadSz);
  772. }
  773. if (ret == 0) {
  774. context->seq++;
  775. }
  776. wc_AesFree(aes_key);
  777. }
  778. #ifdef WOLFSSL_SMALL_STACK
  779. XFREE(aes_key, hpke->heap, DYNAMIC_TYPE_AES);
  780. #endif
  781. return ret;
  782. }
  783. /* encrypt a message using the provided ephemeral and receiver kem keys */
  784. int wc_HpkeSealBase(Hpke* hpke, void* ephemeralKey, void* receiverKey,
  785. byte* info, word32 infoSz, byte* aad, word32 aadSz, byte* plaintext,
  786. word32 ptSz, byte* ciphertext)
  787. {
  788. int ret;
  789. #ifdef WOLFSSL_SMALL_STACK
  790. HpkeBaseContext* context;
  791. #else
  792. HpkeBaseContext context[1];
  793. #endif
  794. /* check that all the buffers are non NULL or optional with 0 length */
  795. if (hpke == NULL || ephemeralKey == NULL || receiverKey == NULL ||
  796. (info == NULL && infoSz != 0) || (aad == NULL && aadSz != 0) ||
  797. plaintext == NULL || ciphertext == NULL) {
  798. return BAD_FUNC_ARG;
  799. }
  800. #ifdef WOLFSSL_SMALL_STACK
  801. context = (HpkeBaseContext*)XMALLOC(sizeof(HpkeBaseContext), hpke->heap,
  802. DYNAMIC_TYPE_TMP_BUFFER);
  803. if (context == NULL) {
  804. return MEMORY_E;
  805. }
  806. #endif
  807. PRIVATE_KEY_UNLOCK();
  808. /* setup the context and pubKey */
  809. ret = wc_HpkeSetupBaseSender(hpke, context, ephemeralKey, receiverKey, info,
  810. infoSz);
  811. /* run seal using the context */
  812. if (ret == 0) {
  813. ret = wc_HpkeContextSealBase(hpke, context, aad, aadSz, plaintext,
  814. ptSz, ciphertext);
  815. }
  816. PRIVATE_KEY_LOCK();
  817. #ifdef WOLFSSL_SMALL_STACK
  818. XFREE(context, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  819. #endif
  820. return ret;
  821. }
  822. /* compute the shared secret from the ephemeral and receiver kem keys */
  823. static int wc_HpkeDecap(Hpke* hpke, void* receiverKey, const byte* pubKey,
  824. word16 pubKeySz, byte* sharedSecret)
  825. {
  826. int ret;
  827. #ifdef ECC_TIMING_RESISTANT
  828. WC_RNG* rng;
  829. #endif
  830. word32 dh_len;
  831. word16 receiverPubKeySz;
  832. void* ephemeralKey = NULL;
  833. #ifndef WOLFSSL_SMALL_STACK
  834. byte dh[HPKE_Ndh_MAX];
  835. byte kemContext[HPKE_Npk_MAX * 2];
  836. #else
  837. byte* dh = NULL;
  838. byte* kemContext = NULL;
  839. #endif
  840. if (hpke == NULL || receiverKey == NULL) {
  841. return BAD_FUNC_ARG;
  842. }
  843. receiverPubKeySz = (word16)hpke->Npk;
  844. #ifdef WOLFSSL_SMALL_STACK
  845. dh = (byte*)XMALLOC(hpke->Ndh, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  846. kemContext = (byte*)XMALLOC(hpke->Npk * 2, hpke->heap,
  847. DYNAMIC_TYPE_TMP_BUFFER);
  848. if (dh == NULL || kemContext == NULL) {
  849. XFREE(dh, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  850. XFREE(kemContext, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  851. return MEMORY_E;
  852. }
  853. #endif
  854. /* deserialize ephemeralKey from pubKey */
  855. ret = wc_HpkeDeserializePublicKey(hpke, &ephemeralKey, pubKey, pubKeySz);
  856. /* generate dh */
  857. dh_len = hpke->Ndh;
  858. if (ret == 0)
  859. switch (hpke->kem)
  860. {
  861. #if defined(HAVE_ECC)
  862. case DHKEM_P256_HKDF_SHA256:
  863. case DHKEM_P384_HKDF_SHA384:
  864. case DHKEM_P521_HKDF_SHA512:
  865. #ifdef ECC_TIMING_RESISTANT
  866. rng = wc_rng_new(NULL, 0, hpke->heap);
  867. if (rng == NULL)
  868. return RNG_FAILURE_E;
  869. wc_ecc_set_rng((ecc_key*)receiverKey, rng);
  870. #endif
  871. ret = wc_ecc_shared_secret((ecc_key*)receiverKey,
  872. (ecc_key*)ephemeralKey, dh, &dh_len);
  873. #ifdef ECC_TIMING_RESISTANT
  874. wc_rng_free(rng);
  875. #endif
  876. break;
  877. #endif
  878. #if defined(HAVE_CURVE25519)
  879. case DHKEM_X25519_HKDF_SHA256:
  880. ret = wc_curve25519_shared_secret_ex(
  881. (curve25519_key*)receiverKey, (curve25519_key*)ephemeralKey,
  882. dh, &dh_len, EC25519_LITTLE_ENDIAN);
  883. break;
  884. #endif
  885. case DHKEM_X448_HKDF_SHA512:
  886. /* TODO: Add X448 */
  887. default:
  888. ret = -1;
  889. break;
  890. }
  891. if (ephemeralKey != NULL)
  892. wc_HpkeFreeKey(hpke, (word16)hpke->kem, ephemeralKey, hpke->heap);
  893. if (ret == 0) {
  894. /* copy pubKey into kemContext */
  895. XMEMCPY(kemContext, pubKey, hpke->Npk);
  896. /* serialize pkR into kemContext */
  897. ret = wc_HpkeSerializePublicKey(hpke, receiverKey,
  898. kemContext + hpke->Npk, &receiverPubKeySz);
  899. }
  900. /* compute the shared secret */
  901. if (ret == 0) {
  902. ret = wc_HpkeExtractAndExpand(hpke, dh, dh_len, kemContext,
  903. hpke->Npk * 2, sharedSecret);
  904. }
  905. #ifdef WOLFSSL_SMALL_STACK
  906. XFREE(dh, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  907. XFREE(kemContext, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  908. #endif
  909. return ret;
  910. }
  911. /* setup an hpke base context for decrypting messages, return 0 or error */
  912. static int wc_HpkeSetupBaseReceiver(Hpke* hpke, HpkeBaseContext* context,
  913. void* receiverKey, const byte* pubKey, word16 pubKeySz, byte* info,
  914. word32 infoSz)
  915. {
  916. int ret;
  917. #ifndef WOLFSSL_SMALL_STACK
  918. byte sharedSecret[HPKE_Nsecret_MAX];
  919. #else
  920. byte* sharedSecret;
  921. #endif
  922. #ifdef WOLFSSL_SMALL_STACK
  923. sharedSecret = (byte*)XMALLOC(hpke->Nsecret, hpke->heap,
  924. DYNAMIC_TYPE_TMP_BUFFER);
  925. if (sharedSecret == NULL) {
  926. return MEMORY_E;
  927. }
  928. #endif
  929. /* decap */
  930. ret = wc_HpkeDecap(hpke, receiverKey, pubKey, pubKeySz, sharedSecret);
  931. /* schedule */
  932. if (ret == 0) {
  933. ret = wc_HpkeKeyScheduleBase(hpke, context, sharedSecret, info,
  934. infoSz);
  935. }
  936. #ifdef WOLFSSL_SMALL_STACK
  937. XFREE(sharedSecret, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  938. #endif
  939. return ret;
  940. }
  941. /* decrypt a message using a setup hpke context, return 0 or error */
  942. static int wc_HpkeContextOpenBase(Hpke* hpke, HpkeBaseContext* context,
  943. byte* aad, word32 aadSz, byte* ciphertext, word32 ctSz, byte* out)
  944. {
  945. int ret;
  946. byte nonce[HPKE_Nn_MAX];
  947. #ifndef WOLFSSL_SMALL_STACK
  948. Aes aes_key[1];
  949. #else
  950. Aes* aes_key;
  951. #endif
  952. if (hpke == NULL) {
  953. return BAD_FUNC_ARG;
  954. }
  955. XMEMSET(nonce, 0, sizeof(nonce));
  956. #ifdef WOLFSSL_SMALL_STACK
  957. aes_key = (Aes*)XMALLOC(sizeof(Aes), hpke->heap, DYNAMIC_TYPE_AES);
  958. if (aes_key == NULL) {
  959. return MEMORY_E;
  960. }
  961. #endif
  962. ret = wc_HpkeContextComputeNonce(hpke, context, nonce);
  963. if (ret == 0)
  964. ret = wc_AesInit(aes_key, hpke->heap, INVALID_DEVID);
  965. if (ret == 0) {
  966. ret = wc_AesGcmSetKey(aes_key, context->key, hpke->Nk);
  967. if (ret == 0) {
  968. ret = wc_AesGcmDecrypt(aes_key, out, ciphertext, ctSz, nonce,
  969. hpke->Nn, ciphertext + ctSz, hpke->Nt, aad, aadSz);
  970. }
  971. if (ret == 0) {
  972. context->seq++;
  973. }
  974. wc_AesFree(aes_key);
  975. }
  976. #ifdef WOLFSSL_SMALL_STACK
  977. XFREE(aes_key, hpke->heap, DYNAMIC_TYPE_AES);
  978. #endif
  979. return ret;
  980. }
  981. /* decrypt a message using the receiver and encoded ephemeral key, return 0 or
  982. * error */
  983. int wc_HpkeOpenBase(Hpke* hpke, void* receiverKey, const byte* pubKey,
  984. word16 pubKeySz, byte* info, word32 infoSz, byte* aad, word32 aadSz,
  985. byte* ciphertext, word32 ctSz, byte* plaintext)
  986. {
  987. int ret;
  988. #ifndef WOLFSSL_SMALL_STACK
  989. HpkeBaseContext context[1];
  990. #else
  991. HpkeBaseContext* context;
  992. #endif
  993. /* check that all the buffer are non NULL or optional with 0 length */
  994. if (hpke == NULL || receiverKey == NULL || pubKey == NULL ||
  995. pubKeySz == 0 || (info == NULL && infoSz != 0) ||
  996. (aad == NULL && aadSz != 0) || plaintext == NULL ||
  997. ciphertext == NULL) {
  998. return BAD_FUNC_ARG;
  999. }
  1000. #ifdef WOLFSSL_SMALL_STACK
  1001. context = (HpkeBaseContext*)XMALLOC(sizeof(HpkeBaseContext), hpke->heap,
  1002. DYNAMIC_TYPE_TMP_BUFFER);
  1003. if (context == NULL) {
  1004. return MEMORY_E;
  1005. }
  1006. #endif
  1007. PRIVATE_KEY_UNLOCK();
  1008. /* setup receiver */
  1009. ret = wc_HpkeSetupBaseReceiver(hpke, context, receiverKey, pubKey,
  1010. pubKeySz, info, infoSz);
  1011. if (ret == 0) {
  1012. /* open the ciphertext */
  1013. ret = wc_HpkeContextOpenBase(hpke, context, aad, aadSz, ciphertext,
  1014. ctSz, plaintext);
  1015. }
  1016. PRIVATE_KEY_LOCK();
  1017. #ifdef WOLFSSL_SMALL_STACK
  1018. XFREE(context, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  1019. #endif
  1020. return ret;
  1021. }
  1022. #endif /* HAVE_HPKE && (HAVE_ECC || HAVE_CURVE25519) && HAVE_AESGCM */