hpke.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  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. #ifdef WOLFSSL_SMALL_STACK
  956. aes_key = (Aes*)XMALLOC(sizeof(Aes), hpke->heap, DYNAMIC_TYPE_AES);
  957. if (aes_key == NULL) {
  958. return MEMORY_E;
  959. }
  960. #endif
  961. ret = wc_HpkeContextComputeNonce(hpke, context, nonce);
  962. if (ret == 0)
  963. ret = wc_AesInit(aes_key, hpke->heap, INVALID_DEVID);
  964. if (ret == 0) {
  965. ret = wc_AesGcmSetKey(aes_key, context->key, hpke->Nk);
  966. if (ret == 0) {
  967. ret = wc_AesGcmDecrypt(aes_key, out, ciphertext, ctSz, nonce,
  968. hpke->Nn, ciphertext + ctSz, hpke->Nt, aad, aadSz);
  969. }
  970. if (ret == 0) {
  971. context->seq++;
  972. }
  973. wc_AesFree(aes_key);
  974. }
  975. #ifdef WOLFSSL_SMALL_STACK
  976. XFREE(aes_key, hpke->heap, DYNAMIC_TYPE_AES);
  977. #endif
  978. return ret;
  979. }
  980. /* decrypt a message using the receiver and encoded ephemeral key, return 0 or
  981. * error */
  982. int wc_HpkeOpenBase(Hpke* hpke, void* receiverKey, const byte* pubKey,
  983. word16 pubKeySz, byte* info, word32 infoSz, byte* aad, word32 aadSz,
  984. byte* ciphertext, word32 ctSz, byte* plaintext)
  985. {
  986. int ret;
  987. #ifndef WOLFSSL_SMALL_STACK
  988. HpkeBaseContext context[1];
  989. #else
  990. HpkeBaseContext* context;
  991. #endif
  992. /* check that all the buffer are non NULL or optional with 0 length */
  993. if (hpke == NULL || receiverKey == NULL || pubKey == NULL ||
  994. pubKeySz == 0 || (info == NULL && infoSz != 0) ||
  995. (aad == NULL && aadSz != 0) || plaintext == NULL ||
  996. ciphertext == NULL) {
  997. return BAD_FUNC_ARG;
  998. }
  999. #ifdef WOLFSSL_SMALL_STACK
  1000. context = (HpkeBaseContext*)XMALLOC(sizeof(HpkeBaseContext), hpke->heap,
  1001. DYNAMIC_TYPE_TMP_BUFFER);
  1002. if (context == NULL) {
  1003. return MEMORY_E;
  1004. }
  1005. #endif
  1006. PRIVATE_KEY_UNLOCK();
  1007. /* setup receiver */
  1008. ret = wc_HpkeSetupBaseReceiver(hpke, context, receiverKey, pubKey,
  1009. pubKeySz, info, infoSz);
  1010. if (ret == 0) {
  1011. /* open the ciphertext */
  1012. ret = wc_HpkeContextOpenBase(hpke, context, aad, aadSz, ciphertext,
  1013. ctSz, plaintext);
  1014. }
  1015. PRIVATE_KEY_LOCK();
  1016. #ifdef WOLFSSL_SMALL_STACK
  1017. XFREE(context, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  1018. #endif
  1019. return ret;
  1020. }
  1021. #endif /* HAVE_HPKE && (HAVE_ECC || HAVE_CURVE25519) && HAVE_AESGCM */