hpke.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  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, 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 = kem;
  113. hpke->kdf = kdf;
  114. hpke->aead = 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 = 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 = 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 = 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 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, 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 = 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, 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(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, 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 = hpke->Npk;
  646. ephemeralPubKeySz = 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. return RNG_FAILURE_E;
  669. wc_ecc_set_rng((ecc_key*)ephemeralKey, rng);
  670. #endif
  671. ret = wc_ecc_shared_secret((ecc_key*)ephemeralKey,
  672. (ecc_key*)receiverKey, dh, &dh_len);
  673. #ifdef ECC_TIMING_RESISTANT
  674. wc_rng_free(rng);
  675. #endif
  676. break;
  677. #endif
  678. #if defined(HAVE_CURVE25519)
  679. case DHKEM_X25519_HKDF_SHA256:
  680. ret = wc_curve25519_shared_secret_ex((curve25519_key*)ephemeralKey,
  681. (curve25519_key*)receiverKey, dh, &dh_len,
  682. EC25519_LITTLE_ENDIAN);
  683. break;
  684. #endif
  685. case DHKEM_X448_HKDF_SHA512:
  686. /* TODO: Add X448 */
  687. default:
  688. ret = -1;
  689. break;
  690. }
  691. if (ret == 0) {
  692. /* serialize ephemeralKey into kemContext */
  693. ret = wc_HpkeSerializePublicKey(hpke, ephemeralKey,
  694. kemContext, &ephemeralPubKeySz);
  695. }
  696. if (ret == 0) {
  697. /* serialize pkR into kemContext */
  698. ret = wc_HpkeSerializePublicKey(hpke, receiverKey,
  699. kemContext + ephemeralPubKeySz, &receiverPubKeySz);
  700. }
  701. if (ret == 0) {
  702. /* compute the shared secret */
  703. ret = wc_HpkeExtractAndExpand(hpke, dh, dh_len, kemContext,
  704. hpke->Npk * 2, sharedSecret);
  705. }
  706. #ifdef WOLFSSL_SMALL_STACK
  707. XFREE(dh, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  708. XFREE(kemContext, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  709. #endif
  710. return ret;
  711. }
  712. /* setup the sender context with shared key, nonce and exporter secret */
  713. static int wc_HpkeSetupBaseSender(Hpke* hpke, HpkeBaseContext* context,
  714. void* ephemeralKey, void* receiverKey, byte* info, word32 infoSz)
  715. {
  716. int ret;
  717. #ifndef WOLFSSL_SMALL_STACK
  718. byte sharedSecret[HPKE_Nsecret_MAX];
  719. #else
  720. byte* sharedSecret;
  721. #endif
  722. if (hpke == NULL) {
  723. return BAD_FUNC_ARG;
  724. }
  725. #ifdef WOLFSSL_SMALL_STACK
  726. sharedSecret = (byte*)XMALLOC(hpke->Nsecret, hpke->heap,
  727. DYNAMIC_TYPE_TMP_BUFFER);
  728. #endif
  729. /* encap */
  730. ret = wc_HpkeEncap(hpke, ephemeralKey, receiverKey, sharedSecret);
  731. /* schedule */
  732. if (ret == 0) {
  733. ret = wc_HpkeKeyScheduleBase(hpke, context, sharedSecret, info,
  734. infoSz);
  735. }
  736. #ifdef WOLFSSL_SMALL_STACK
  737. XFREE(sharedSecret, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  738. #endif
  739. return ret;
  740. }
  741. /* encrypt a message using an hpke base context, return 0 or error */
  742. static int wc_HpkeContextSealBase(Hpke* hpke, HpkeBaseContext* context,
  743. byte* aad, word32 aadSz, byte* plaintext, word32 ptSz, byte* out)
  744. {
  745. int ret;
  746. byte nonce[HPKE_Nn_MAX];
  747. #ifndef WOLFSSL_SMALL_STACK
  748. Aes aes_key[1];
  749. #else
  750. Aes* aes_key;
  751. #endif
  752. if (hpke == NULL) {
  753. return BAD_FUNC_ARG;
  754. }
  755. #ifdef WOLFSSL_SMALL_STACK
  756. aes_key = (Aes*)XMALLOC(sizeof(Aes), hpke->heap, DYNAMIC_TYPE_AES);
  757. if (aes_key == NULL) {
  758. return MEMORY_E;
  759. }
  760. #endif
  761. ret = wc_AesInit(aes_key, hpke->heap, INVALID_DEVID);
  762. if (ret == 0) {
  763. ret = wc_HpkeContextComputeNonce(hpke, context, nonce);
  764. if (ret == 0) {
  765. ret = wc_AesGcmSetKey(aes_key, context->key, hpke->Nk);
  766. }
  767. if (ret == 0) {
  768. ret = wc_AesGcmEncrypt(aes_key, out, plaintext, ptSz, nonce,
  769. hpke->Nn, out + ptSz, hpke->Nt, aad, aadSz);
  770. }
  771. if (ret == 0) {
  772. context->seq++;
  773. }
  774. wc_AesFree(aes_key);
  775. }
  776. #ifdef WOLFSSL_SMALL_STACK
  777. XFREE(aes_key, hpke->heap, DYNAMIC_TYPE_AES);
  778. #endif
  779. return ret;
  780. }
  781. /* encrypt a message using the provided ephemeral and receiver kem keys */
  782. int wc_HpkeSealBase(Hpke* hpke, void* ephemeralKey, void* receiverKey,
  783. byte* info, word32 infoSz, byte* aad, word32 aadSz, byte* plaintext,
  784. word32 ptSz, byte* ciphertext)
  785. {
  786. int ret;
  787. #ifdef WOLFSSL_SMALL_STACK
  788. HpkeBaseContext* context;
  789. #else
  790. HpkeBaseContext context[1];
  791. #endif
  792. /* check that all the buffers are non NULL or optional with 0 length */
  793. if (hpke == NULL || ephemeralKey == NULL || receiverKey == NULL ||
  794. (info == NULL && infoSz != 0) || (aad == NULL && aadSz != 0) ||
  795. plaintext == NULL || ciphertext == NULL) {
  796. return BAD_FUNC_ARG;
  797. }
  798. #ifdef WOLFSSL_SMALL_STACK
  799. context = (HpkeBaseContext*)XMALLOC(sizeof(HpkeBaseContext), hpke->heap,
  800. DYNAMIC_TYPE_TMP_BUFFER);
  801. if (context == NULL) {
  802. return MEMORY_E;
  803. }
  804. #endif
  805. PRIVATE_KEY_UNLOCK();
  806. /* setup the context and pubKey */
  807. ret = wc_HpkeSetupBaseSender(hpke, context, ephemeralKey, receiverKey, info,
  808. infoSz);
  809. /* run seal using the context */
  810. if (ret == 0) {
  811. ret = wc_HpkeContextSealBase(hpke, context, aad, aadSz, plaintext,
  812. ptSz, ciphertext);
  813. }
  814. PRIVATE_KEY_LOCK();
  815. #ifdef WOLFSSL_SMALL_STACK
  816. XFREE(context, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  817. #endif
  818. return ret;
  819. }
  820. /* compute the shared secret from the ephemeral and receiver kem keys */
  821. static int wc_HpkeDecap(Hpke* hpke, void* receiverKey, const byte* pubKey,
  822. word16 pubKeySz, byte* sharedSecret)
  823. {
  824. int ret;
  825. #ifdef ECC_TIMING_RESISTANT
  826. WC_RNG* rng;
  827. #endif
  828. word32 dh_len;
  829. word16 receiverPubKeySz;
  830. void* ephemeralKey = NULL;
  831. #ifndef WOLFSSL_SMALL_STACK
  832. byte dh[HPKE_Ndh_MAX];
  833. byte kemContext[HPKE_Npk_MAX * 2];
  834. #else
  835. byte* dh = NULL;
  836. byte* kemContext = NULL;
  837. #endif
  838. if (hpke == NULL || receiverKey == NULL) {
  839. return BAD_FUNC_ARG;
  840. }
  841. receiverPubKeySz = hpke->Npk;
  842. #ifdef WOLFSSL_SMALL_STACK
  843. dh = (byte*)XMALLOC(hpke->Ndh, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  844. kemContext = (byte*)XMALLOC(hpke->Npk * 2, hpke->heap,
  845. DYNAMIC_TYPE_TMP_BUFFER);
  846. if (dh == NULL || kemContext == NULL) {
  847. XFREE(dh, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  848. XFREE(kemContext, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  849. return MEMORY_E;
  850. }
  851. #endif
  852. /* deserialize ephemeralKey from pubKey */
  853. ret = wc_HpkeDeserializePublicKey(hpke, &ephemeralKey, pubKey, pubKeySz);
  854. /* generate dh */
  855. dh_len = hpke->Ndh;
  856. if (ret == 0)
  857. switch (hpke->kem)
  858. {
  859. #if defined(HAVE_ECC)
  860. case DHKEM_P256_HKDF_SHA256:
  861. case DHKEM_P384_HKDF_SHA384:
  862. case DHKEM_P521_HKDF_SHA512:
  863. #ifdef ECC_TIMING_RESISTANT
  864. rng = wc_rng_new(NULL, 0, hpke->heap);
  865. if (rng == NULL)
  866. return RNG_FAILURE_E;
  867. wc_ecc_set_rng((ecc_key*)receiverKey, rng);
  868. #endif
  869. ret = wc_ecc_shared_secret((ecc_key*)receiverKey,
  870. (ecc_key*)ephemeralKey, dh, &dh_len);
  871. #ifdef ECC_TIMING_RESISTANT
  872. wc_rng_free(rng);
  873. #endif
  874. break;
  875. #endif
  876. #if defined(HAVE_CURVE25519)
  877. case DHKEM_X25519_HKDF_SHA256:
  878. ret = wc_curve25519_shared_secret_ex(
  879. (curve25519_key*)receiverKey, (curve25519_key*)ephemeralKey,
  880. dh, &dh_len, EC25519_LITTLE_ENDIAN);
  881. break;
  882. #endif
  883. case DHKEM_X448_HKDF_SHA512:
  884. /* TODO: Add X448 */
  885. default:
  886. ret = -1;
  887. break;
  888. }
  889. if (ephemeralKey != NULL)
  890. wc_HpkeFreeKey(hpke, hpke->kem, ephemeralKey, hpke->heap);
  891. if (ret == 0) {
  892. /* copy pubKey into kemContext */
  893. XMEMCPY(kemContext, pubKey, hpke->Npk);
  894. /* serialize pkR into kemContext */
  895. ret = wc_HpkeSerializePublicKey(hpke, receiverKey,
  896. kemContext + hpke->Npk, &receiverPubKeySz);
  897. }
  898. /* compute the shared secret */
  899. if (ret == 0) {
  900. ret = wc_HpkeExtractAndExpand(hpke, dh, dh_len, kemContext,
  901. hpke->Npk * 2, sharedSecret);
  902. }
  903. #ifdef WOLFSSL_SMALL_STACK
  904. XFREE(dh, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  905. XFREE(kemContext, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  906. #endif
  907. return ret;
  908. }
  909. /* setup an hpke base context for decrypting messages, return 0 or error */
  910. static int wc_HpkeSetupBaseReceiver(Hpke* hpke, HpkeBaseContext* context,
  911. void* receiverKey, const byte* pubKey, word16 pubKeySz, byte* info,
  912. word32 infoSz)
  913. {
  914. int ret;
  915. #ifndef WOLFSSL_SMALL_STACK
  916. byte sharedSecret[HPKE_Nsecret_MAX];
  917. #else
  918. byte* sharedSecret;
  919. #endif
  920. #ifdef WOLFSSL_SMALL_STACK
  921. sharedSecret = (byte*)XMALLOC(hpke->Nsecret, hpke->heap,
  922. DYNAMIC_TYPE_TMP_BUFFER);
  923. if (sharedSecret == NULL) {
  924. return MEMORY_E;
  925. }
  926. #endif
  927. /* decap */
  928. ret = wc_HpkeDecap(hpke, receiverKey, pubKey, pubKeySz, sharedSecret);
  929. /* schedule */
  930. if (ret == 0) {
  931. ret = wc_HpkeKeyScheduleBase(hpke, context, sharedSecret, info,
  932. infoSz);
  933. }
  934. #ifdef WOLFSSL_SMALL_STACK
  935. XFREE(sharedSecret, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  936. #endif
  937. return ret;
  938. }
  939. /* decrypt a message using a setup hpke context, return 0 or error */
  940. static int wc_HpkeContextOpenBase(Hpke* hpke, HpkeBaseContext* context,
  941. byte* aad, word32 aadSz, byte* ciphertext, word32 ctSz, byte* out)
  942. {
  943. int ret;
  944. byte nonce[HPKE_Nn_MAX];
  945. #ifndef WOLFSSL_SMALL_STACK
  946. Aes aes_key[1];
  947. #else
  948. Aes* aes_key;
  949. #endif
  950. if (hpke == NULL) {
  951. return BAD_FUNC_ARG;
  952. }
  953. #ifdef WOLFSSL_SMALL_STACK
  954. aes_key = (Aes*)XMALLOC(sizeof(Aes), hpke->heap, DYNAMIC_TYPE_AES);
  955. if (aes_key == NULL) {
  956. return MEMORY_E;
  957. }
  958. #endif
  959. ret = wc_HpkeContextComputeNonce(hpke, context, nonce);
  960. if (ret == 0)
  961. ret = wc_AesInit(aes_key, hpke->heap, INVALID_DEVID);
  962. if (ret == 0) {
  963. ret = wc_AesGcmSetKey(aes_key, context->key, hpke->Nk);
  964. if (ret == 0) {
  965. ret = wc_AesGcmDecrypt(aes_key, out, ciphertext, ctSz, nonce,
  966. hpke->Nn, ciphertext + ctSz, hpke->Nt, aad, aadSz);
  967. }
  968. if (ret == 0) {
  969. context->seq++;
  970. }
  971. wc_AesFree(aes_key);
  972. }
  973. #ifdef WOLFSSL_SMALL_STACK
  974. XFREE(aes_key, hpke->heap, DYNAMIC_TYPE_AES);
  975. #endif
  976. return ret;
  977. }
  978. /* decrypt a message using the receiver and encoded ephemeral key, return 0 or
  979. * error */
  980. int wc_HpkeOpenBase(Hpke* hpke, void* receiverKey, const byte* pubKey,
  981. word16 pubKeySz, byte* info, word32 infoSz, byte* aad, word32 aadSz,
  982. byte* ciphertext, word32 ctSz, byte* plaintext)
  983. {
  984. int ret;
  985. #ifndef WOLFSSL_SMALL_STACK
  986. HpkeBaseContext context[1];
  987. #else
  988. HpkeBaseContext* context;
  989. #endif
  990. /* check that all the buffer are non NULL or optional with 0 length */
  991. if (hpke == NULL || receiverKey == NULL || pubKey == NULL ||
  992. pubKeySz == 0 || (info == NULL && infoSz != 0) ||
  993. (aad == NULL && aadSz != 0) || plaintext == NULL ||
  994. ciphertext == NULL) {
  995. return BAD_FUNC_ARG;
  996. }
  997. #ifdef WOLFSSL_SMALL_STACK
  998. context = (HpkeBaseContext*)XMALLOC(sizeof(HpkeBaseContext), hpke->heap,
  999. DYNAMIC_TYPE_TMP_BUFFER);
  1000. if (context == NULL) {
  1001. return MEMORY_E;
  1002. }
  1003. #endif
  1004. PRIVATE_KEY_UNLOCK();
  1005. /* setup receiver */
  1006. ret = wc_HpkeSetupBaseReceiver(hpke, context, receiverKey, pubKey,
  1007. pubKeySz, info, infoSz);
  1008. if (ret == 0) {
  1009. /* open the ciphertext */
  1010. ret = wc_HpkeContextOpenBase(hpke, context, aad, aadSz, ciphertext,
  1011. ctSz, plaintext);
  1012. }
  1013. PRIVATE_KEY_LOCK();
  1014. #ifdef WOLFSSL_SMALL_STACK
  1015. XFREE(context, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  1016. #endif
  1017. return ret;
  1018. }
  1019. #endif /* HAVE_HPKE && (HAVE_ECC || HAVE_CURVE25519) && HAVE_AESGCM */