hpke.c 32 KB

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