hpke_test.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921
  1. /*
  2. * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/evp.h>
  10. #include <openssl/core_names.h>
  11. #include <openssl/rand.h>
  12. #include <openssl/hpke.h>
  13. #include "testutil.h"
  14. /* a size to use for stack buffers */
  15. #define OSSL_HPKE_TSTSIZE 512
  16. static OSSL_LIB_CTX *testctx = NULL;
  17. static OSSL_PROVIDER *nullprov = NULL;
  18. static OSSL_PROVIDER *deflprov = NULL;
  19. static char *testpropq = "provider=default";
  20. static int verbose = 0;
  21. typedef struct {
  22. int mode;
  23. OSSL_HPKE_SUITE suite;
  24. const unsigned char *ikmE;
  25. size_t ikmElen;
  26. const unsigned char *expected_pkEm;
  27. size_t expected_pkEmlen;
  28. const unsigned char *ikmR;
  29. size_t ikmRlen;
  30. const unsigned char *expected_pkRm;
  31. size_t expected_pkRmlen;
  32. const unsigned char *expected_skRm;
  33. size_t expected_skRmlen;
  34. const unsigned char *expected_secret;
  35. size_t expected_secretlen;
  36. const unsigned char *ksinfo;
  37. size_t ksinfolen;
  38. const unsigned char *ikmAuth;
  39. size_t ikmAuthlen;
  40. const unsigned char *psk;
  41. size_t psklen;
  42. const char *pskid; /* want terminating NUL here */
  43. } TEST_BASEDATA;
  44. typedef struct
  45. {
  46. int seq;
  47. const unsigned char *pt;
  48. size_t ptlen;
  49. const unsigned char *aad;
  50. size_t aadlen;
  51. const unsigned char *expected_ct;
  52. size_t expected_ctlen;
  53. } TEST_AEADDATA;
  54. typedef struct
  55. {
  56. const unsigned char *context;
  57. size_t contextlen;
  58. const unsigned char *expected_secret;
  59. size_t expected_secretlen;
  60. } TEST_EXPORTDATA;
  61. /**
  62. * @brief Test that an EVP_PKEY encoded public key matches the supplied buffer
  63. * @param pkey is the EVP_PKEY we want to check
  64. * @param pub is the expected public key buffer
  65. * @param publen is the length of the above
  66. * @return 1 for good, 0 for bad
  67. */
  68. static int cmpkey(const EVP_PKEY *pkey,
  69. const unsigned char *pub, size_t publen)
  70. {
  71. unsigned char pubbuf[256];
  72. size_t pubbuflen = 0;
  73. int erv = 0;
  74. if (!TEST_true(publen <= sizeof(pubbuf)))
  75. return 0;
  76. erv = EVP_PKEY_get_octet_string_param(pkey,
  77. OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
  78. pubbuf, sizeof(pubbuf), &pubbuflen);
  79. if (!TEST_true(erv))
  80. return 0;
  81. if (pub != NULL && !TEST_mem_eq(pubbuf, pubbuflen, pub, publen))
  82. return 0;
  83. return 1;
  84. }
  85. static int do_testhpke(const TEST_BASEDATA *base,
  86. const TEST_AEADDATA *aead, size_t aeadsz,
  87. const TEST_EXPORTDATA *export, size_t exportsz)
  88. {
  89. OSSL_LIB_CTX *libctx = testctx;
  90. const char *propq = testpropq;
  91. OSSL_HPKE_CTX *sealctx = NULL, *openctx = NULL;
  92. unsigned char ct[256];
  93. unsigned char enc[256];
  94. unsigned char ptout[256];
  95. size_t ptoutlen = sizeof(ptout);
  96. size_t enclen = sizeof(enc);
  97. size_t ctlen = sizeof(ct);
  98. unsigned char pub[OSSL_HPKE_TSTSIZE];
  99. size_t publen = sizeof(pub);
  100. EVP_PKEY *privE = NULL;
  101. unsigned char authpub[OSSL_HPKE_TSTSIZE];
  102. size_t authpublen = sizeof(authpub);
  103. EVP_PKEY *authpriv = NULL;
  104. unsigned char rpub[OSSL_HPKE_TSTSIZE];
  105. size_t rpublen = sizeof(pub);
  106. EVP_PKEY *privR = NULL;
  107. int ret = 0;
  108. size_t i;
  109. uint64_t lastseq = 0;
  110. if (!TEST_true(OSSL_HPKE_keygen(base->suite, pub, &publen, &privE,
  111. base->ikmE, base->ikmElen, libctx, propq)))
  112. goto end;
  113. if (!TEST_true(cmpkey(privE, base->expected_pkEm, base->expected_pkEmlen)))
  114. goto end;
  115. if (!TEST_ptr(sealctx = OSSL_HPKE_CTX_new(base->mode, base->suite,
  116. libctx, propq)))
  117. goto end;
  118. if (!TEST_true(OSSL_HPKE_CTX_set1_ikme(sealctx, base->ikmE, base->ikmElen)))
  119. goto end;
  120. if (base->mode == OSSL_HPKE_MODE_AUTH
  121. || base->mode == OSSL_HPKE_MODE_PSKAUTH) {
  122. if (!TEST_true(base->ikmAuth != NULL && base->ikmAuthlen > 0))
  123. goto end;
  124. if (!TEST_true(OSSL_HPKE_keygen(base->suite,
  125. authpub, &authpublen, &authpriv,
  126. base->ikmAuth, base->ikmAuthlen,
  127. libctx, propq)))
  128. goto end;
  129. if (!TEST_true(OSSL_HPKE_CTX_set1_authpriv(sealctx, authpriv)))
  130. goto end;
  131. }
  132. if (!TEST_true(OSSL_HPKE_keygen(base->suite, rpub, &rpublen, &privR,
  133. base->ikmR, base->ikmRlen, libctx, propq)))
  134. goto end;
  135. if (!TEST_true(cmpkey(privR, base->expected_pkRm, base->expected_pkRmlen)))
  136. goto end;
  137. if (base->mode == OSSL_HPKE_MODE_PSK
  138. || base->mode == OSSL_HPKE_MODE_PSKAUTH) {
  139. if (!TEST_true(OSSL_HPKE_CTX_set1_psk(sealctx, base->pskid,
  140. base->psk, base->psklen)))
  141. goto end;
  142. }
  143. if (!TEST_true(OSSL_HPKE_encap(sealctx, enc, &enclen,
  144. rpub, rpublen,
  145. base->ksinfo, base->ksinfolen)))
  146. goto end;
  147. if (!TEST_true(cmpkey(privE, enc, enclen)))
  148. goto end;
  149. for (i = 0; i < aeadsz; ++i) {
  150. ctlen = sizeof(ct);
  151. memset(ct, 0, ctlen);
  152. if (!TEST_true(OSSL_HPKE_seal(sealctx, ct, &ctlen,
  153. aead[i].aad, aead[i].aadlen,
  154. aead[i].pt, aead[i].ptlen)))
  155. goto end;
  156. if (!TEST_mem_eq(ct, ctlen, aead[i].expected_ct,
  157. aead[i].expected_ctlen))
  158. goto end;
  159. if (!TEST_true(OSSL_HPKE_CTX_get_seq(sealctx, &lastseq)))
  160. goto end;
  161. if (lastseq != (uint64_t)(i + 1))
  162. goto end;
  163. }
  164. if (!TEST_ptr(openctx = OSSL_HPKE_CTX_new(base->mode, base->suite,
  165. libctx, propq)))
  166. goto end;
  167. if (base->mode == OSSL_HPKE_MODE_PSK
  168. || base->mode == OSSL_HPKE_MODE_PSKAUTH) {
  169. if (!TEST_true(base->pskid != NULL && base->psk != NULL
  170. && base->psklen > 0))
  171. goto end;
  172. if (!TEST_true(OSSL_HPKE_CTX_set1_psk(openctx, base->pskid,
  173. base->psk, base->psklen)))
  174. goto end;
  175. }
  176. if (base->mode == OSSL_HPKE_MODE_AUTH
  177. || base->mode == OSSL_HPKE_MODE_PSKAUTH) {
  178. if (!TEST_true(OSSL_HPKE_CTX_set1_authpub(openctx,
  179. authpub, authpublen)))
  180. goto end;
  181. }
  182. if (!TEST_true(OSSL_HPKE_decap(openctx, enc, enclen, privR,
  183. base->ksinfo, base->ksinfolen)))
  184. goto end;
  185. for (i = 0; i < aeadsz; ++i) {
  186. ptoutlen = sizeof(ptout);
  187. memset(ptout, 0, ptoutlen);
  188. if (!TEST_true(OSSL_HPKE_open(openctx, ptout, &ptoutlen,
  189. aead[i].aad, aead[i].aadlen,
  190. aead[i].expected_ct,
  191. aead[i].expected_ctlen)))
  192. goto end;
  193. if (!TEST_mem_eq(aead[i].pt, aead[i].ptlen, ptout, ptoutlen))
  194. goto end;
  195. /* check the sequence is being incremented as expected */
  196. if (!TEST_true(OSSL_HPKE_CTX_get_seq(openctx, &lastseq)))
  197. goto end;
  198. if (lastseq != (uint64_t)(i + 1))
  199. goto end;
  200. }
  201. /* check exporters */
  202. for (i = 0; i < exportsz; ++i) {
  203. size_t len = export[i].expected_secretlen;
  204. unsigned char eval[OSSL_HPKE_TSTSIZE];
  205. if (len > sizeof(eval))
  206. goto end;
  207. /* export with too long label should fail */
  208. if (!TEST_false(OSSL_HPKE_export(sealctx, eval, len,
  209. export[i].context, -1)))
  210. goto end;
  211. /* good export call */
  212. if (!TEST_true(OSSL_HPKE_export(sealctx, eval, len,
  213. export[i].context,
  214. export[i].contextlen)))
  215. goto end;
  216. if (!TEST_mem_eq(eval, len, export[i].expected_secret,
  217. export[i].expected_secretlen))
  218. goto end;
  219. /* check seal fails if export only mode */
  220. if (aeadsz == 0) {
  221. if (!TEST_false(OSSL_HPKE_seal(sealctx, ct, &ctlen,
  222. NULL, 0, ptout, ptoutlen)))
  223. goto end;
  224. }
  225. }
  226. ret = 1;
  227. end:
  228. OSSL_HPKE_CTX_free(sealctx);
  229. OSSL_HPKE_CTX_free(openctx);
  230. EVP_PKEY_free(privE);
  231. EVP_PKEY_free(privR);
  232. EVP_PKEY_free(authpriv);
  233. return ret;
  234. }
  235. static const unsigned char pt[] = {
  236. 0x42, 0x65, 0x61, 0x75, 0x74, 0x79, 0x20, 0x69,
  237. 0x73, 0x20, 0x74, 0x72, 0x75, 0x74, 0x68, 0x2c,
  238. 0x20, 0x74, 0x72, 0x75, 0x74, 0x68, 0x20, 0x62,
  239. 0x65, 0x61, 0x75, 0x74, 0x79
  240. };
  241. static const unsigned char ksinfo[] = {
  242. 0x4f, 0x64, 0x65, 0x20, 0x6f, 0x6e, 0x20, 0x61,
  243. 0x20, 0x47, 0x72, 0x65, 0x63, 0x69, 0x61, 0x6e,
  244. 0x20, 0x55, 0x72, 0x6e
  245. };
  246. /*
  247. * static const char *pskid = "Ennyn Durin aran Moria";
  248. */
  249. static const unsigned char pskid[] = {
  250. 0x45, 0x6e, 0x6e, 0x79, 0x6e, 0x20, 0x44, 0x75,
  251. 0x72, 0x69, 0x6e, 0x20, 0x61, 0x72, 0x61, 0x6e,
  252. 0x20, 0x4d, 0x6f, 0x72, 0x69, 0x61, 0x00
  253. };
  254. static const unsigned char psk[] = {
  255. 0x02, 0x47, 0xfd, 0x33, 0xb9, 0x13, 0x76, 0x0f,
  256. 0xa1, 0xfa, 0x51, 0xe1, 0x89, 0x2d, 0x9f, 0x30,
  257. 0x7f, 0xbe, 0x65, 0xeb, 0x17, 0x1e, 0x81, 0x32,
  258. 0xc2, 0xaf, 0x18, 0x55, 0x5a, 0x73, 0x8b, 0x82
  259. };
  260. /* these need to be "outside" the function below to keep check-ansi CI happy */
  261. static const unsigned char first_ikme[] = {
  262. 0x78, 0x62, 0x8c, 0x35, 0x4e, 0x46, 0xf3, 0xe1,
  263. 0x69, 0xbd, 0x23, 0x1b, 0xe7, 0xb2, 0xff, 0x1c,
  264. 0x77, 0xaa, 0x30, 0x24, 0x60, 0xa2, 0x6d, 0xbf,
  265. 0xa1, 0x55, 0x15, 0x68, 0x4c, 0x00, 0x13, 0x0b
  266. };
  267. static const unsigned char first_ikmr[] = {
  268. 0xd4, 0xa0, 0x9d, 0x09, 0xf5, 0x75, 0xfe, 0xf4,
  269. 0x25, 0x90, 0x5d, 0x2a, 0xb3, 0x96, 0xc1, 0x44,
  270. 0x91, 0x41, 0x46, 0x3f, 0x69, 0x8f, 0x8e, 0xfd,
  271. 0xb7, 0xac, 0xcf, 0xaf, 0xf8, 0x99, 0x50, 0x98
  272. };
  273. static const unsigned char first_ikmepub[] = {
  274. 0x0a, 0xd0, 0x95, 0x0d, 0x9f, 0xb9, 0x58, 0x8e,
  275. 0x59, 0x69, 0x0b, 0x74, 0xf1, 0x23, 0x7e, 0xcd,
  276. 0xf1, 0xd7, 0x75, 0xcd, 0x60, 0xbe, 0x2e, 0xca,
  277. 0x57, 0xaf, 0x5a, 0x4b, 0x04, 0x71, 0xc9, 0x1b,
  278. };
  279. static const unsigned char first_ikmrpub[] = {
  280. 0x9f, 0xed, 0x7e, 0x8c, 0x17, 0x38, 0x75, 0x60,
  281. 0xe9, 0x2c, 0xc6, 0x46, 0x2a, 0x68, 0x04, 0x96,
  282. 0x57, 0x24, 0x6a, 0x09, 0xbf, 0xa8, 0xad, 0xe7,
  283. 0xae, 0xfe, 0x58, 0x96, 0x72, 0x01, 0x63, 0x66
  284. };
  285. static const unsigned char first_ikmrpriv[] = {
  286. 0xc5, 0xeb, 0x01, 0xeb, 0x45, 0x7f, 0xe6, 0xc6,
  287. 0xf5, 0x75, 0x77, 0xc5, 0x41, 0x3b, 0x93, 0x15,
  288. 0x50, 0xa1, 0x62, 0xc7, 0x1a, 0x03, 0xac, 0x8d,
  289. 0x19, 0x6b, 0xab, 0xbd, 0x4e, 0x5c, 0xe0, 0xfd
  290. };
  291. static const unsigned char first_expected_shared_secret[] = {
  292. 0x72, 0x76, 0x99, 0xf0, 0x09, 0xff, 0xe3, 0xc0,
  293. 0x76, 0x31, 0x50, 0x19, 0xc6, 0x96, 0x48, 0x36,
  294. 0x6b, 0x69, 0x17, 0x14, 0x39, 0xbd, 0x7d, 0xd0,
  295. 0x80, 0x77, 0x43, 0xbd, 0xe7, 0x69, 0x86, 0xcd
  296. };
  297. static const unsigned char first_aad0[] = {
  298. 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x2d, 0x30
  299. };
  300. static const unsigned char first_ct0[] = {
  301. 0xe5, 0x2c, 0x6f, 0xed, 0x7f, 0x75, 0x8d, 0x0c,
  302. 0xf7, 0x14, 0x56, 0x89, 0xf2, 0x1b, 0xc1, 0xbe,
  303. 0x6e, 0xc9, 0xea, 0x09, 0x7f, 0xef, 0x4e, 0x95,
  304. 0x94, 0x40, 0x01, 0x2f, 0x4f, 0xeb, 0x73, 0xfb,
  305. 0x61, 0x1b, 0x94, 0x61, 0x99, 0xe6, 0x81, 0xf4,
  306. 0xcf, 0xc3, 0x4d, 0xb8, 0xea
  307. };
  308. static const unsigned char first_aad1[] = {
  309. 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x2d, 0x31
  310. };
  311. static const unsigned char first_ct1[] = {
  312. 0x49, 0xf3, 0xb1, 0x9b, 0x28, 0xa9, 0xea, 0x9f,
  313. 0x43, 0xe8, 0xc7, 0x12, 0x04, 0xc0, 0x0d, 0x4a,
  314. 0x49, 0x0e, 0xe7, 0xf6, 0x13, 0x87, 0xb6, 0x71,
  315. 0x9d, 0xb7, 0x65, 0xe9, 0x48, 0x12, 0x3b, 0x45,
  316. 0xb6, 0x16, 0x33, 0xef, 0x05, 0x9b, 0xa2, 0x2c,
  317. 0xd6, 0x24, 0x37, 0xc8, 0xba
  318. };
  319. static const unsigned char first_aad2[] = {
  320. 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x2d, 0x32
  321. };
  322. static const unsigned char first_ct2[] = {
  323. 0x25, 0x7c, 0xa6, 0xa0, 0x84, 0x73, 0xdc, 0x85,
  324. 0x1f, 0xde, 0x45, 0xaf, 0xd5, 0x98, 0xcc, 0x83,
  325. 0xe3, 0x26, 0xdd, 0xd0, 0xab, 0xe1, 0xef, 0x23,
  326. 0xba, 0xa3, 0xba, 0xa4, 0xdd, 0x8c, 0xde, 0x99,
  327. 0xfc, 0xe2, 0xc1, 0xe8, 0xce, 0x68, 0x7b, 0x0b,
  328. 0x47, 0xea, 0xd1, 0xad, 0xc9
  329. };
  330. static const unsigned char first_export1[] = {
  331. 0xdf, 0xf1, 0x7a, 0xf3, 0x54, 0xc8, 0xb4, 0x16,
  332. 0x73, 0x56, 0x7d, 0xb6, 0x25, 0x9f, 0xd6, 0x02,
  333. 0x99, 0x67, 0xb4, 0xe1, 0xaa, 0xd1, 0x30, 0x23,
  334. 0xc2, 0xae, 0x5d, 0xf8, 0xf4, 0xf4, 0x3b, 0xf6
  335. };
  336. static const unsigned char first_context2[] = { 0x00 };
  337. static const unsigned char first_export2[] = {
  338. 0x6a, 0x84, 0x72, 0x61, 0xd8, 0x20, 0x7f, 0xe5,
  339. 0x96, 0xbe, 0xfb, 0x52, 0x92, 0x84, 0x63, 0x88,
  340. 0x1a, 0xb4, 0x93, 0xda, 0x34, 0x5b, 0x10, 0xe1,
  341. 0xdc, 0xc6, 0x45, 0xe3, 0xb9, 0x4e, 0x2d, 0x95
  342. };
  343. static const unsigned char first_context3[] = {
  344. 0x54, 0x65, 0x73, 0x74, 0x43, 0x6f, 0x6e, 0x74,
  345. 0x65, 0x78, 0x74
  346. };
  347. static const unsigned char first_export3[] = {
  348. 0x8a, 0xff, 0x52, 0xb4, 0x5a, 0x1b, 0xe3, 0xa7,
  349. 0x34, 0xbc, 0x7a, 0x41, 0xe2, 0x0b, 0x4e, 0x05,
  350. 0x5a, 0xd4, 0xc4, 0xd2, 0x21, 0x04, 0xb0, 0xc2,
  351. 0x02, 0x85, 0xa7, 0xc4, 0x30, 0x24, 0x01, 0xcd
  352. };
  353. static int x25519kdfsha256_hkdfsha256_aes128gcm_psk_test(void)
  354. {
  355. const TEST_BASEDATA pskdata = {
  356. /* "X25519", NULL, "SHA256", "SHA256", "AES-128-GCM", */
  357. OSSL_HPKE_MODE_PSK,
  358. {
  359. OSSL_HPKE_KEM_ID_X25519,
  360. OSSL_HPKE_KDF_ID_HKDF_SHA256,
  361. OSSL_HPKE_AEAD_ID_AES_GCM_128
  362. },
  363. first_ikme, sizeof(first_ikme),
  364. first_ikmepub, sizeof(first_ikmepub),
  365. first_ikmr, sizeof(first_ikmr),
  366. first_ikmrpub, sizeof(first_ikmrpub),
  367. first_ikmrpriv, sizeof(first_ikmrpriv),
  368. first_expected_shared_secret, sizeof(first_expected_shared_secret),
  369. ksinfo, sizeof(ksinfo),
  370. NULL, 0, /* No Auth */
  371. psk, sizeof(psk), (char *) pskid
  372. };
  373. const TEST_AEADDATA aeaddata[] = {
  374. {
  375. 0,
  376. pt, sizeof(pt),
  377. first_aad0, sizeof(first_aad0),
  378. first_ct0, sizeof(first_ct0)
  379. },
  380. {
  381. 1,
  382. pt, sizeof(pt),
  383. first_aad1, sizeof(first_aad1),
  384. first_ct1, sizeof(first_ct1)
  385. },
  386. {
  387. 2,
  388. pt, sizeof(pt),
  389. first_aad2, sizeof(first_aad2),
  390. first_ct2, sizeof(first_ct2)
  391. }
  392. };
  393. const TEST_EXPORTDATA exportdata[] = {
  394. { NULL, 0, first_export1, sizeof(first_export1) },
  395. { first_context2, sizeof(first_context2),
  396. first_export2, sizeof(first_export2) },
  397. { first_context3, sizeof(first_context3),
  398. first_export3, sizeof(first_export3) },
  399. };
  400. return do_testhpke(&pskdata, aeaddata, OSSL_NELEM(aeaddata),
  401. exportdata, OSSL_NELEM(exportdata));
  402. }
  403. static const unsigned char second_ikme[] = {
  404. 0x72, 0x68, 0x60, 0x0d, 0x40, 0x3f, 0xce, 0x43,
  405. 0x15, 0x61, 0xae, 0xf5, 0x83, 0xee, 0x16, 0x13,
  406. 0x52, 0x7c, 0xff, 0x65, 0x5c, 0x13, 0x43, 0xf2,
  407. 0x98, 0x12, 0xe6, 0x67, 0x06, 0xdf, 0x32, 0x34
  408. };
  409. static const unsigned char second_ikmepub[] = {
  410. 0x37, 0xfd, 0xa3, 0x56, 0x7b, 0xdb, 0xd6, 0x28,
  411. 0xe8, 0x86, 0x68, 0xc3, 0xc8, 0xd7, 0xe9, 0x7d,
  412. 0x1d, 0x12, 0x53, 0xb6, 0xd4, 0xea, 0x6d, 0x44,
  413. 0xc1, 0x50, 0xf7, 0x41, 0xf1, 0xbf, 0x44, 0x31,
  414. };
  415. static const unsigned char second_ikmr[] = {
  416. 0x6d, 0xb9, 0xdf, 0x30, 0xaa, 0x07, 0xdd, 0x42,
  417. 0xee, 0x5e, 0x81, 0x81, 0xaf, 0xdb, 0x97, 0x7e,
  418. 0x53, 0x8f, 0x5e, 0x1f, 0xec, 0x8a, 0x06, 0x22,
  419. 0x3f, 0x33, 0xf7, 0x01, 0x3e, 0x52, 0x50, 0x37
  420. };
  421. static const unsigned char second_ikmrpub[] = {
  422. 0x39, 0x48, 0xcf, 0xe0, 0xad, 0x1d, 0xdb, 0x69,
  423. 0x5d, 0x78, 0x0e, 0x59, 0x07, 0x71, 0x95, 0xda,
  424. 0x6c, 0x56, 0x50, 0x6b, 0x02, 0x73, 0x29, 0x79,
  425. 0x4a, 0xb0, 0x2b, 0xca, 0x80, 0x81, 0x5c, 0x4d
  426. };
  427. static const unsigned char second_ikmrpriv[] = {
  428. 0x46, 0x12, 0xc5, 0x50, 0x26, 0x3f, 0xc8, 0xad,
  429. 0x58, 0x37, 0x5d, 0xf3, 0xf5, 0x57, 0xaa, 0xc5,
  430. 0x31, 0xd2, 0x68, 0x50, 0x90, 0x3e, 0x55, 0xa9,
  431. 0xf2, 0x3f, 0x21, 0xd8, 0x53, 0x4e, 0x8a, 0xc8
  432. };
  433. static const unsigned char second_expected_shared_secret[] = {
  434. 0xfe, 0x0e, 0x18, 0xc9, 0xf0, 0x24, 0xce, 0x43,
  435. 0x79, 0x9a, 0xe3, 0x93, 0xc7, 0xe8, 0xfe, 0x8f,
  436. 0xce, 0x9d, 0x21, 0x88, 0x75, 0xe8, 0x22, 0x7b,
  437. 0x01, 0x87, 0xc0, 0x4e, 0x7d, 0x2e, 0xa1, 0xfc
  438. };
  439. static const unsigned char second_aead0[] = {
  440. 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x2d, 0x30
  441. };
  442. static const unsigned char second_ct0[] = {
  443. 0xf9, 0x38, 0x55, 0x8b, 0x5d, 0x72, 0xf1, 0xa2,
  444. 0x38, 0x10, 0xb4, 0xbe, 0x2a, 0xb4, 0xf8, 0x43,
  445. 0x31, 0xac, 0xc0, 0x2f, 0xc9, 0x7b, 0xab, 0xc5,
  446. 0x3a, 0x52, 0xae, 0x82, 0x18, 0xa3, 0x55, 0xa9,
  447. 0x6d, 0x87, 0x70, 0xac, 0x83, 0xd0, 0x7b, 0xea,
  448. 0x87, 0xe1, 0x3c, 0x51, 0x2a
  449. };
  450. static const unsigned char second_aead1[] = {
  451. 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x2d, 0x31
  452. };
  453. static const unsigned char second_ct1[] = {
  454. 0xaf, 0x2d, 0x7e, 0x9a, 0xc9, 0xae, 0x7e, 0x27,
  455. 0x0f, 0x46, 0xba, 0x1f, 0x97, 0x5b, 0xe5, 0x3c,
  456. 0x09, 0xf8, 0xd8, 0x75, 0xbd, 0xc8, 0x53, 0x54,
  457. 0x58, 0xc2, 0x49, 0x4e, 0x8a, 0x6e, 0xab, 0x25,
  458. 0x1c, 0x03, 0xd0, 0xc2, 0x2a, 0x56, 0xb8, 0xca,
  459. 0x42, 0xc2, 0x06, 0x3b, 0x84
  460. };
  461. static const unsigned char second_export1[] = {
  462. 0x38, 0x53, 0xfe, 0x2b, 0x40, 0x35, 0x19, 0x5a,
  463. 0x57, 0x3f, 0xfc, 0x53, 0x85, 0x6e, 0x77, 0x05,
  464. 0x8e, 0x15, 0xd9, 0xea, 0x06, 0x4d, 0xe3, 0xe5,
  465. 0x9f, 0x49, 0x61, 0xd0, 0x09, 0x52, 0x50, 0xee
  466. };
  467. static const unsigned char second_context2[] = { 0x00 };
  468. static const unsigned char second_export2[] = {
  469. 0x2e, 0x8f, 0x0b, 0x54, 0x67, 0x3c, 0x70, 0x29,
  470. 0x64, 0x9d, 0x4e, 0xb9, 0xd5, 0xe3, 0x3b, 0xf1,
  471. 0x87, 0x2c, 0xf7, 0x6d, 0x62, 0x3f, 0xf1, 0x64,
  472. 0xac, 0x18, 0x5d, 0xa9, 0xe8, 0x8c, 0x21, 0xa5
  473. };
  474. static const unsigned char second_context3[] = {
  475. 0x54, 0x65, 0x73, 0x74, 0x43, 0x6f, 0x6e, 0x74,
  476. 0x65, 0x78, 0x74
  477. };
  478. static const unsigned char second_export3[] = {
  479. 0xe9, 0xe4, 0x30, 0x65, 0x10, 0x2c, 0x38, 0x36,
  480. 0x40, 0x1b, 0xed, 0x8c, 0x3c, 0x3c, 0x75, 0xae,
  481. 0x46, 0xbe, 0x16, 0x39, 0x86, 0x93, 0x91, 0xd6,
  482. 0x2c, 0x61, 0xf1, 0xec, 0x7a, 0xf5, 0x49, 0x31
  483. };
  484. static int x25519kdfsha256_hkdfsha256_aes128gcm_base_test(void)
  485. {
  486. const TEST_BASEDATA basedata = {
  487. OSSL_HPKE_MODE_BASE,
  488. {
  489. OSSL_HPKE_KEM_ID_X25519,
  490. OSSL_HPKE_KDF_ID_HKDF_SHA256,
  491. OSSL_HPKE_AEAD_ID_AES_GCM_128
  492. },
  493. second_ikme, sizeof(second_ikme),
  494. second_ikmepub, sizeof(second_ikmepub),
  495. second_ikmr, sizeof(second_ikmr),
  496. second_ikmrpub, sizeof(second_ikmrpub),
  497. second_ikmrpriv, sizeof(second_ikmrpriv),
  498. second_expected_shared_secret, sizeof(second_expected_shared_secret),
  499. ksinfo, sizeof(ksinfo),
  500. NULL, 0, /* no auth ikm */
  501. NULL, 0, NULL /* no psk */
  502. };
  503. const TEST_AEADDATA aeaddata[] = {
  504. {
  505. 0,
  506. pt, sizeof(pt),
  507. second_aead0, sizeof(second_aead0),
  508. second_ct0, sizeof(second_ct0)
  509. },
  510. {
  511. 1,
  512. pt, sizeof(pt),
  513. second_aead1, sizeof(second_aead1),
  514. second_ct1, sizeof(second_ct1)
  515. }
  516. };
  517. const TEST_EXPORTDATA exportdata[] = {
  518. { NULL, 0, second_export1, sizeof(second_export1) },
  519. { second_context2, sizeof(second_context2),
  520. second_export2, sizeof(second_export2) },
  521. { second_context3, sizeof(second_context3),
  522. second_export3, sizeof(second_export3) },
  523. };
  524. return do_testhpke(&basedata, aeaddata, OSSL_NELEM(aeaddata),
  525. exportdata, OSSL_NELEM(exportdata));
  526. }
  527. static const unsigned char third_ikme[] = {
  528. 0x42, 0x70, 0xe5, 0x4f, 0xfd, 0x08, 0xd7, 0x9d,
  529. 0x59, 0x28, 0x02, 0x0a, 0xf4, 0x68, 0x6d, 0x8f,
  530. 0x6b, 0x7d, 0x35, 0xdb, 0xe4, 0x70, 0x26, 0x5f,
  531. 0x1f, 0x5a, 0xa2, 0x28, 0x16, 0xce, 0x86, 0x0e
  532. };
  533. static const unsigned char third_ikmepub[] = {
  534. 0x04, 0xa9, 0x27, 0x19, 0xc6, 0x19, 0x5d, 0x50,
  535. 0x85, 0x10, 0x4f, 0x46, 0x9a, 0x8b, 0x98, 0x14,
  536. 0xd5, 0x83, 0x8f, 0xf7, 0x2b, 0x60, 0x50, 0x1e,
  537. 0x2c, 0x44, 0x66, 0xe5, 0xe6, 0x7b, 0x32, 0x5a,
  538. 0xc9, 0x85, 0x36, 0xd7, 0xb6, 0x1a, 0x1a, 0xf4,
  539. 0xb7, 0x8e, 0x5b, 0x7f, 0x95, 0x1c, 0x09, 0x00,
  540. 0xbe, 0x86, 0x3c, 0x40, 0x3c, 0xe6, 0x5c, 0x9b,
  541. 0xfc, 0xb9, 0x38, 0x26, 0x57, 0x22, 0x2d, 0x18,
  542. 0xc4,
  543. };
  544. static const unsigned char third_ikmr[] = {
  545. 0x66, 0x8b, 0x37, 0x17, 0x1f, 0x10, 0x72, 0xf3,
  546. 0xcf, 0x12, 0xea, 0x8a, 0x23, 0x6a, 0x45, 0xdf,
  547. 0x23, 0xfc, 0x13, 0xb8, 0x2a, 0xf3, 0x60, 0x9a,
  548. 0xd1, 0xe3, 0x54, 0xf6, 0xef, 0x81, 0x75, 0x50
  549. };
  550. static const unsigned char third_ikmrpub[] = {
  551. 0x04, 0xfe, 0x8c, 0x19, 0xce, 0x09, 0x05, 0x19,
  552. 0x1e, 0xbc, 0x29, 0x8a, 0x92, 0x45, 0x79, 0x25,
  553. 0x31, 0xf2, 0x6f, 0x0c, 0xec, 0xe2, 0x46, 0x06,
  554. 0x39, 0xe8, 0xbc, 0x39, 0xcb, 0x7f, 0x70, 0x6a,
  555. 0x82, 0x6a, 0x77, 0x9b, 0x4c, 0xf9, 0x69, 0xb8,
  556. 0xa0, 0xe5, 0x39, 0xc7, 0xf6, 0x2f, 0xb3, 0xd3,
  557. 0x0a, 0xd6, 0xaa, 0x8f, 0x80, 0xe3, 0x0f, 0x1d,
  558. 0x12, 0x8a, 0xaf, 0xd6, 0x8a, 0x2c, 0xe7, 0x2e,
  559. 0xa0
  560. };
  561. static const unsigned char third_ikmrpriv[] = {
  562. 0xf3, 0xce, 0x7f, 0xda, 0xe5, 0x7e, 0x1a, 0x31,
  563. 0x0d, 0x87, 0xf1, 0xeb, 0xbd, 0xe6, 0xf3, 0x28,
  564. 0xbe, 0x0a, 0x99, 0xcd, 0xbc, 0xad, 0xf4, 0xd6,
  565. 0x58, 0x9c, 0xf2, 0x9d, 0xe4, 0xb8, 0xff, 0xd2
  566. };
  567. static const unsigned char third_expected_shared_secret[] = {
  568. 0xc0, 0xd2, 0x6a, 0xea, 0xb5, 0x36, 0x60, 0x9a,
  569. 0x57, 0x2b, 0x07, 0x69, 0x5d, 0x93, 0x3b, 0x58,
  570. 0x9d, 0xcf, 0x36, 0x3f, 0xf9, 0xd9, 0x3c, 0x93,
  571. 0xad, 0xea, 0x53, 0x7a, 0xea, 0xbb, 0x8c, 0xb8
  572. };
  573. static const unsigned char third_aead0[] = {
  574. 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x2d, 0x30
  575. };
  576. static const unsigned char third_ct0[] = {
  577. 0x5a, 0xd5, 0x90, 0xbb, 0x8b, 0xaa, 0x57, 0x7f,
  578. 0x86, 0x19, 0xdb, 0x35, 0xa3, 0x63, 0x11, 0x22,
  579. 0x6a, 0x89, 0x6e, 0x73, 0x42, 0xa6, 0xd8, 0x36,
  580. 0xd8, 0xb7, 0xbc, 0xd2, 0xf2, 0x0b, 0x6c, 0x7f,
  581. 0x90, 0x76, 0xac, 0x23, 0x2e, 0x3a, 0xb2, 0x52,
  582. 0x3f, 0x39, 0x51, 0x34, 0x34
  583. };
  584. static const unsigned char third_aead1[] = {
  585. 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x2d, 0x31
  586. };
  587. static const unsigned char third_ct1[] = {
  588. 0xfa, 0x6f, 0x03, 0x7b, 0x47, 0xfc, 0x21, 0x82,
  589. 0x6b, 0x61, 0x01, 0x72, 0xca, 0x96, 0x37, 0xe8,
  590. 0x2d, 0x6e, 0x58, 0x01, 0xeb, 0x31, 0xcb, 0xd3,
  591. 0x74, 0x82, 0x71, 0xaf, 0xfd, 0x4e, 0xcb, 0x06,
  592. 0x64, 0x6e, 0x03, 0x29, 0xcb, 0xdf, 0x3c, 0x3c,
  593. 0xd6, 0x55, 0xb2, 0x8e, 0x82
  594. };
  595. static const unsigned char third_export1[] = {
  596. 0x5e, 0x9b, 0xc3, 0xd2, 0x36, 0xe1, 0x91, 0x1d,
  597. 0x95, 0xe6, 0x5b, 0x57, 0x6a, 0x8a, 0x86, 0xd4,
  598. 0x78, 0xfb, 0x82, 0x7e, 0x8b, 0xdf, 0xe7, 0x7b,
  599. 0x74, 0x1b, 0x28, 0x98, 0x90, 0x49, 0x0d, 0x4d
  600. };
  601. static const unsigned char third_context2[] = { 0x00 };
  602. static const unsigned char third_export2[] = {
  603. 0x6c, 0xff, 0x87, 0x65, 0x89, 0x31, 0xbd, 0xa8,
  604. 0x3d, 0xc8, 0x57, 0xe6, 0x35, 0x3e, 0xfe, 0x49,
  605. 0x87, 0xa2, 0x01, 0xb8, 0x49, 0x65, 0x8d, 0x9b,
  606. 0x04, 0x7a, 0xab, 0x4c, 0xf2, 0x16, 0xe7, 0x96
  607. };
  608. static const unsigned char third_context3[] = {
  609. 0x54, 0x65, 0x73, 0x74, 0x43, 0x6f, 0x6e, 0x74,
  610. 0x65, 0x78, 0x74
  611. };
  612. static const unsigned char third_export3[] = {
  613. 0xd8, 0xf1, 0xea, 0x79, 0x42, 0xad, 0xbb, 0xa7,
  614. 0x41, 0x2c, 0x6d, 0x43, 0x1c, 0x62, 0xd0, 0x13,
  615. 0x71, 0xea, 0x47, 0x6b, 0x82, 0x3e, 0xb6, 0x97,
  616. 0xe1, 0xf6, 0xe6, 0xca, 0xe1, 0xda, 0xb8, 0x5a
  617. };
  618. static int P256kdfsha256_hkdfsha256_aes128gcm_base_test(void)
  619. {
  620. const TEST_BASEDATA basedata = {
  621. OSSL_HPKE_MODE_BASE,
  622. {
  623. OSSL_HPKE_KEM_ID_P256,
  624. OSSL_HPKE_KDF_ID_HKDF_SHA256,
  625. OSSL_HPKE_AEAD_ID_AES_GCM_128
  626. },
  627. third_ikme, sizeof(third_ikme),
  628. third_ikmepub, sizeof(third_ikmepub),
  629. third_ikmr, sizeof(third_ikmr),
  630. third_ikmrpub, sizeof(third_ikmrpub),
  631. third_ikmrpriv, sizeof(third_ikmrpriv),
  632. third_expected_shared_secret, sizeof(third_expected_shared_secret),
  633. ksinfo, sizeof(ksinfo),
  634. NULL, 0, /* no auth */
  635. NULL, 0, NULL /* PSK stuff */
  636. };
  637. const TEST_AEADDATA aeaddata[] = {
  638. {
  639. 0,
  640. pt, sizeof(pt),
  641. third_aead0, sizeof(third_aead0),
  642. third_ct0, sizeof(third_ct0)
  643. },
  644. {
  645. 1,
  646. pt, sizeof(pt),
  647. third_aead1, sizeof(third_aead1),
  648. third_ct1, sizeof(third_ct1)
  649. }
  650. };
  651. const TEST_EXPORTDATA exportdata[] = {
  652. { NULL, 0, third_export1, sizeof(third_export1) },
  653. { third_context2, sizeof(third_context2),
  654. third_export2, sizeof(third_export2) },
  655. { third_context3, sizeof(third_context3),
  656. third_export3, sizeof(third_export3) },
  657. };
  658. return do_testhpke(&basedata, aeaddata, OSSL_NELEM(aeaddata),
  659. exportdata, OSSL_NELEM(exportdata));
  660. }
  661. static const unsigned char fourth_ikme[] = {
  662. 0x55, 0xbc, 0x24, 0x5e, 0xe4, 0xef, 0xda, 0x25,
  663. 0xd3, 0x8f, 0x2d, 0x54, 0xd5, 0xbb, 0x66, 0x65,
  664. 0x29, 0x1b, 0x99, 0xf8, 0x10, 0x8a, 0x8c, 0x4b,
  665. 0x68, 0x6c, 0x2b, 0x14, 0x89, 0x3e, 0xa5, 0xd9
  666. };
  667. static const unsigned char fourth_ikmepub[] = {
  668. 0xe5, 0xe8, 0xf9, 0xbf, 0xff, 0x6c, 0x2f, 0x29,
  669. 0x79, 0x1f, 0xc3, 0x51, 0xd2, 0xc2, 0x5c, 0xe1,
  670. 0x29, 0x9a, 0xa5, 0xea, 0xca, 0x78, 0xa7, 0x57,
  671. 0xc0, 0xb4, 0xfb, 0x4b, 0xcd, 0x83, 0x09, 0x18
  672. };
  673. static const unsigned char fourth_ikmr[] = {
  674. 0x68, 0x3a, 0xe0, 0xda, 0x1d, 0x22, 0x18, 0x1e,
  675. 0x74, 0xed, 0x2e, 0x50, 0x3e, 0xbf, 0x82, 0x84,
  676. 0x0d, 0xeb, 0x1d, 0x5e, 0x87, 0x2c, 0xad, 0xe2,
  677. 0x0f, 0x4b, 0x45, 0x8d, 0x99, 0x78, 0x3e, 0x31
  678. };
  679. static const unsigned char fourth_ikmrpub[] = {
  680. 0x19, 0x41, 0x41, 0xca, 0x6c, 0x3c, 0x3b, 0xeb,
  681. 0x47, 0x92, 0xcd, 0x97, 0xba, 0x0e, 0xa1, 0xfa,
  682. 0xff, 0x09, 0xd9, 0x84, 0x35, 0x01, 0x23, 0x45,
  683. 0x76, 0x6e, 0xe3, 0x3a, 0xae, 0x2d, 0x76, 0x64
  684. };
  685. static const unsigned char fourth_ikmrpriv[] = {
  686. 0x33, 0xd1, 0x96, 0xc8, 0x30, 0xa1, 0x2f, 0x9a,
  687. 0xc6, 0x5d, 0x6e, 0x56, 0x5a, 0x59, 0x0d, 0x80,
  688. 0xf0, 0x4e, 0xe9, 0xb1, 0x9c, 0x83, 0xc8, 0x7f,
  689. 0x2c, 0x17, 0x0d, 0x97, 0x2a, 0x81, 0x28, 0x48
  690. };
  691. static const unsigned char fourth_expected_shared_secret[] = {
  692. 0xe8, 0x17, 0x16, 0xce, 0x8f, 0x73, 0x14, 0x1d,
  693. 0x4f, 0x25, 0xee, 0x90, 0x98, 0xef, 0xc9, 0x68,
  694. 0xc9, 0x1e, 0x5b, 0x8c, 0xe5, 0x2f, 0xff, 0xf5,
  695. 0x9d, 0x64, 0x03, 0x9e, 0x82, 0x91, 0x8b, 0x66
  696. };
  697. static const unsigned char fourth_export1[] = {
  698. 0x7a, 0x36, 0x22, 0x1b, 0xd5, 0x6d, 0x50, 0xfb,
  699. 0x51, 0xee, 0x65, 0xed, 0xfd, 0x98, 0xd0, 0x6a,
  700. 0x23, 0xc4, 0xdc, 0x87, 0x08, 0x5a, 0xa5, 0x86,
  701. 0x6c, 0xb7, 0x08, 0x72, 0x44, 0xbd, 0x2a, 0x36
  702. };
  703. static const unsigned char fourth_context2[] = { 0x00 };
  704. static const unsigned char fourth_export2[] = {
  705. 0xd5, 0x53, 0x5b, 0x87, 0x09, 0x9c, 0x6c, 0x3c,
  706. 0xe8, 0x0d, 0xc1, 0x12, 0xa2, 0x67, 0x1c, 0x6e,
  707. 0xc8, 0xe8, 0x11, 0xa2, 0xf2, 0x84, 0xf9, 0x48,
  708. 0xce, 0xc6, 0xdd, 0x17, 0x08, 0xee, 0x33, 0xf0
  709. };
  710. static const unsigned char fourth_context3[] = {
  711. 0x54, 0x65, 0x73, 0x74, 0x43, 0x6f, 0x6e, 0x74,
  712. 0x65, 0x78, 0x74
  713. };
  714. static const unsigned char fourth_export3[] = {
  715. 0xff, 0xaa, 0xbc, 0x85, 0xa7, 0x76, 0x13, 0x6c,
  716. 0xa0, 0xc3, 0x78, 0xe5, 0xd0, 0x84, 0xc9, 0x14,
  717. 0x0a, 0xb5, 0x52, 0xb7, 0x8f, 0x03, 0x9d, 0x2e,
  718. 0x87, 0x75, 0xf2, 0x6e, 0xff, 0xf4, 0xc7, 0x0e
  719. };
  720. static int export_only_test(void)
  721. {
  722. /* based on RFC9180 A.7 */
  723. const TEST_BASEDATA basedata = {
  724. OSSL_HPKE_MODE_BASE,
  725. {
  726. OSSL_HPKE_KEM_ID_X25519,
  727. OSSL_HPKE_KDF_ID_HKDF_SHA256,
  728. OSSL_HPKE_AEAD_ID_EXPORTONLY
  729. },
  730. fourth_ikme, sizeof(fourth_ikme),
  731. fourth_ikmepub, sizeof(fourth_ikmepub),
  732. fourth_ikmr, sizeof(fourth_ikmr),
  733. fourth_ikmrpub, sizeof(fourth_ikmrpub),
  734. fourth_ikmrpriv, sizeof(fourth_ikmrpriv),
  735. fourth_expected_shared_secret, sizeof(fourth_expected_shared_secret),
  736. ksinfo, sizeof(ksinfo),
  737. NULL, 0, /* no auth */
  738. NULL, 0, NULL /* PSK stuff */
  739. };
  740. const TEST_EXPORTDATA exportdata[] = {
  741. { NULL, 0, fourth_export1, sizeof(fourth_export1) },
  742. { fourth_context2, sizeof(fourth_context2),
  743. fourth_export2, sizeof(fourth_export2) },
  744. { fourth_context3, sizeof(fourth_context3),
  745. fourth_export3, sizeof(fourth_export3) },
  746. };
  747. return do_testhpke(&basedata, NULL, 0,
  748. exportdata, OSSL_NELEM(exportdata));
  749. }
  750. /*
  751. * Randomly toss a coin
  752. */
  753. #define COIN_IS_HEADS (test_random() % 2)
  754. /* tables of HPKE modes and suite values */
  755. static int hpke_mode_list[] = {
  756. OSSL_HPKE_MODE_BASE,
  757. OSSL_HPKE_MODE_PSK,
  758. OSSL_HPKE_MODE_AUTH,
  759. OSSL_HPKE_MODE_PSKAUTH
  760. };
  761. static uint16_t hpke_kem_list[] = {
  762. OSSL_HPKE_KEM_ID_P256,
  763. OSSL_HPKE_KEM_ID_P384,
  764. OSSL_HPKE_KEM_ID_P521,
  765. OSSL_HPKE_KEM_ID_X25519,
  766. OSSL_HPKE_KEM_ID_X448
  767. };
  768. static uint16_t hpke_kdf_list[] = {
  769. OSSL_HPKE_KDF_ID_HKDF_SHA256,
  770. OSSL_HPKE_KDF_ID_HKDF_SHA384,
  771. OSSL_HPKE_KDF_ID_HKDF_SHA512
  772. };
  773. static uint16_t hpke_aead_list[] = {
  774. OSSL_HPKE_AEAD_ID_AES_GCM_128,
  775. OSSL_HPKE_AEAD_ID_AES_GCM_256,
  776. OSSL_HPKE_AEAD_ID_CHACHA_POLY1305
  777. };
  778. /*
  779. * Strings that can be used with names or IANA codepoints.
  780. * Note that the initial entries from these lists should
  781. * match the lists above, i.e. kem_str_list[0] and
  782. * hpke_kem_list[0] should refer to the same KEM. We use
  783. * that for verbose output via TEST_note() below.
  784. * Subsequent entries are only used for tests of
  785. * OSSL_HPKE_str2suite()
  786. */
  787. static const char *mode_str_list[] = {
  788. "base", "psk", "auth", "pskauth"
  789. };
  790. static const char *kem_str_list[] = {
  791. "P-256", "P-384", "P-521", "x25519", "x448",
  792. "0x10", "0x11", "0x12", "0x20", "0x21",
  793. "16", "17", "18", "32", "33"
  794. };
  795. static const char *kdf_str_list[] = {
  796. "hkdf-sha256", "hkdf-sha384", "hkdf-sha512",
  797. "0x1", "0x01", "0x2", "0x02", "0x3", "0x03",
  798. "1", "2", "3"
  799. };
  800. static const char *aead_str_list[] = {
  801. "aes-128-gcm", "aes-256-gcm", "chacha20-poly1305", "exporter",
  802. "0x1", "0x01", "0x2", "0x02", "0x3", "0x03",
  803. "1", "2", "3",
  804. "0xff", "255"
  805. };
  806. /* table of bogus strings that better not work */
  807. static const char *bogus_suite_strs[] = {
  808. "3,33,3",
  809. "bogus,bogus,bogus",
  810. "bogus,33,3,1,bogus",
  811. "bogus,33,3,1",
  812. "bogus,bogus",
  813. "bogus",
  814. /* one bad token */
  815. "0x10,0x01,bogus",
  816. "0x10,bogus,0x01",
  817. "bogus,0x02,0x01",
  818. /* in reverse order */
  819. "aes-256-gcm,hkdf-sha512,x25519",
  820. /* surplus separators */
  821. ",,0x10,0x01,0x02",
  822. "0x10,,0x01,0x02",
  823. "0x10,0x01,,0x02",
  824. /* embedded NUL chars */
  825. "0x10,\00x01,,0x02",
  826. "0x10,\0""0x01,0x02",
  827. "0x10\0,0x01,0x02",
  828. "0x10,0x01\0,0x02",
  829. "0x10,0x01,\0""0x02",
  830. /* embedded whitespace */
  831. " aes-256-gcm,hkdf-sha512,x25519",
  832. "aes-256-gcm, hkdf-sha512,x25519",
  833. "aes-256-gcm ,hkdf-sha512,x25519",
  834. "aes-256-gcm,hkdf-sha512, x25519",
  835. "aes-256-gcm,hkdf-sha512 ,x25519",
  836. "aes-256-gcm,hkdf-sha512,x25519 ",
  837. /* good value followed by extra stuff */
  838. "0x10,0x01,0x02,",
  839. "0x10,0x01,0x02,,,",
  840. "0x10,0x01,0x01,0x02",
  841. "0x10,0x01,0x01,blah",
  842. "0x10,0x01,0x01 0x02",
  843. /* too few but good tokens */
  844. "0x10,0x01",
  845. "0x10",
  846. /* empty things */
  847. NULL,
  848. "",
  849. ",",
  850. ",,"
  851. };
  852. /**
  853. * @brief round-trips, generating keys, encrypt and decrypt
  854. *
  855. * This iterates over all mode and ciphersuite options trying
  856. * a key gen, encrypt and decrypt for each. The aad, info, and
  857. * seq inputs are randomly set or omitted each time. EVP and
  858. * non-EVP key generation are randomly selected.
  859. *
  860. * @return 1 for success, other otherwise
  861. */
  862. static int test_hpke_modes_suites(void)
  863. {
  864. int overallresult = 1;
  865. size_t mind = 0; /* index into hpke_mode_list */
  866. size_t kemind = 0; /* index into hpke_kem_list */
  867. size_t kdfind = 0; /* index into hpke_kdf_list */
  868. size_t aeadind = 0; /* index into hpke_aead_list */
  869. /* iterate over the different modes */
  870. for (mind = 0; mind < OSSL_NELEM(hpke_mode_list); mind++) {
  871. int hpke_mode = hpke_mode_list[mind];
  872. size_t aadlen = OSSL_HPKE_TSTSIZE;
  873. unsigned char aad[OSSL_HPKE_TSTSIZE];
  874. unsigned char *aadp = NULL;
  875. size_t infolen = 32;
  876. unsigned char info[32];
  877. unsigned char *infop = NULL;
  878. unsigned char lpsk[32];
  879. unsigned char *pskp = NULL;
  880. char lpskid[32];
  881. size_t psklen = 32;
  882. char *pskidp = NULL;
  883. EVP_PKEY *privp = NULL;
  884. OSSL_HPKE_SUITE hpke_suite = OSSL_HPKE_SUITE_DEFAULT;
  885. size_t plainlen = OSSL_HPKE_TSTSIZE;
  886. unsigned char plain[OSSL_HPKE_TSTSIZE];
  887. uint64_t startseq = 0;
  888. OSSL_HPKE_CTX *rctx = NULL;
  889. OSSL_HPKE_CTX *ctx = NULL;
  890. memset(plain, 0x00, OSSL_HPKE_TSTSIZE);
  891. strcpy((char *)plain, "a message not in a bottle");
  892. plainlen = strlen((char *)plain);
  893. /*
  894. * Randomly try with/without info, aad, seq. Given mode and suite
  895. * combos, and this being run even a few times, we'll exercise many
  896. * code paths fairly quickly. We don't really care what the values
  897. * are but it'll be easier to debug if they're known, so we set 'em.
  898. */
  899. if (COIN_IS_HEADS) {
  900. aadp = aad;
  901. memset(aad, 'a', aadlen);
  902. } else {
  903. aadlen = 0;
  904. }
  905. if (COIN_IS_HEADS) {
  906. infop = info;
  907. memset(info, 'i', infolen);
  908. } else {
  909. infolen = 0;
  910. }
  911. if (hpke_mode == OSSL_HPKE_MODE_PSK
  912. || hpke_mode == OSSL_HPKE_MODE_PSKAUTH) {
  913. pskp = lpsk;
  914. memset(lpsk, 'P', psklen);
  915. pskidp = lpskid;
  916. memset(lpskid, 'I', psklen - 1);
  917. lpskid[psklen - 1] = '\0';
  918. } else {
  919. psklen = 0;
  920. }
  921. for (kemind = 0; /* iterate over the kems, kdfs and aeads */
  922. overallresult == 1 && kemind < OSSL_NELEM(hpke_kem_list);
  923. kemind++) {
  924. uint16_t kem_id = hpke_kem_list[kemind];
  925. size_t authpublen = OSSL_HPKE_TSTSIZE;
  926. unsigned char authpub[OSSL_HPKE_TSTSIZE];
  927. unsigned char *authpubp = NULL;
  928. EVP_PKEY *authpriv = NULL;
  929. hpke_suite.kem_id = kem_id;
  930. if (hpke_mode == OSSL_HPKE_MODE_AUTH
  931. || hpke_mode == OSSL_HPKE_MODE_PSKAUTH) {
  932. if (TEST_true(OSSL_HPKE_keygen(hpke_suite, authpub, &authpublen,
  933. &authpriv, NULL, 0,
  934. testctx, NULL)) != 1) {
  935. overallresult = 0;
  936. }
  937. authpubp = authpub;
  938. } else {
  939. authpublen = 0;
  940. }
  941. for (kdfind = 0;
  942. overallresult == 1 && kdfind < OSSL_NELEM(hpke_kdf_list);
  943. kdfind++) {
  944. uint16_t kdf_id = hpke_kdf_list[kdfind];
  945. hpke_suite.kdf_id = kdf_id;
  946. for (aeadind = 0;
  947. overallresult == 1
  948. && aeadind < OSSL_NELEM(hpke_aead_list);
  949. aeadind++) {
  950. uint16_t aead_id = hpke_aead_list[aeadind];
  951. size_t publen = OSSL_HPKE_TSTSIZE;
  952. unsigned char pub[OSSL_HPKE_TSTSIZE];
  953. size_t senderpublen = OSSL_HPKE_TSTSIZE;
  954. unsigned char senderpub[OSSL_HPKE_TSTSIZE];
  955. size_t cipherlen = OSSL_HPKE_TSTSIZE;
  956. unsigned char cipher[OSSL_HPKE_TSTSIZE];
  957. size_t clearlen = OSSL_HPKE_TSTSIZE;
  958. unsigned char clear[OSSL_HPKE_TSTSIZE];
  959. hpke_suite.aead_id = aead_id;
  960. if (!TEST_true(OSSL_HPKE_keygen(hpke_suite,
  961. pub, &publen, &privp,
  962. NULL, 0, testctx, NULL)))
  963. overallresult = 0;
  964. if (!TEST_ptr(ctx = OSSL_HPKE_CTX_new(hpke_mode, hpke_suite,
  965. testctx, NULL)))
  966. overallresult = 0;
  967. if (hpke_mode == OSSL_HPKE_MODE_PSK
  968. || hpke_mode == OSSL_HPKE_MODE_PSKAUTH) {
  969. if (!TEST_true(OSSL_HPKE_CTX_set1_psk(ctx, pskidp,
  970. pskp, psklen)))
  971. overallresult = 0;
  972. }
  973. if (hpke_mode == OSSL_HPKE_MODE_AUTH
  974. || hpke_mode == OSSL_HPKE_MODE_PSKAUTH) {
  975. if (!TEST_true(OSSL_HPKE_CTX_set1_authpriv(ctx,
  976. authpriv)))
  977. overallresult = 0;
  978. }
  979. if (COIN_IS_HEADS) {
  980. RAND_bytes_ex(testctx,
  981. (unsigned char *) &startseq,
  982. sizeof(startseq),
  983. RAND_DRBG_STRENGTH);
  984. if (!TEST_true(OSSL_HPKE_CTX_set_seq(ctx, startseq)))
  985. overallresult = 0;
  986. } else {
  987. startseq = 0;
  988. }
  989. if (!TEST_true(OSSL_HPKE_encap(ctx, senderpub,
  990. &senderpublen,
  991. pub, publen,
  992. infop, infolen)))
  993. overallresult = 0;
  994. /* throw in a call with a too-short cipherlen */
  995. cipherlen = 15;
  996. if (!TEST_false(OSSL_HPKE_seal(ctx, cipher, &cipherlen,
  997. aadp, aadlen,
  998. plain, plainlen)))
  999. overallresult = 0;
  1000. /* fix back real cipherlen */
  1001. cipherlen = OSSL_HPKE_TSTSIZE;
  1002. if (!TEST_true(OSSL_HPKE_seal(ctx, cipher, &cipherlen,
  1003. aadp, aadlen,
  1004. plain, plainlen)))
  1005. overallresult = 0;
  1006. OSSL_HPKE_CTX_free(ctx);
  1007. memset(clear, 0, clearlen);
  1008. if (!TEST_ptr(rctx = OSSL_HPKE_CTX_new(hpke_mode,
  1009. hpke_suite,
  1010. testctx, NULL)))
  1011. overallresult = 0;
  1012. if (hpke_mode == OSSL_HPKE_MODE_PSK
  1013. || hpke_mode == OSSL_HPKE_MODE_PSKAUTH) {
  1014. if (!TEST_true(OSSL_HPKE_CTX_set1_psk(rctx, pskidp,
  1015. pskp, psklen)))
  1016. overallresult = 0;
  1017. }
  1018. if (hpke_mode == OSSL_HPKE_MODE_AUTH
  1019. || hpke_mode == OSSL_HPKE_MODE_PSKAUTH) {
  1020. /* check a borked p256 key */
  1021. if (hpke_suite.kem_id == OSSL_HPKE_KEM_ID_P256) {
  1022. /* set to fail decode of authpub this time */
  1023. if (!TEST_false(OSSL_HPKE_CTX_set1_authpub(rctx,
  1024. authpub,
  1025. 10
  1026. )))
  1027. overallresult = 0;
  1028. }
  1029. if (!TEST_true(OSSL_HPKE_CTX_set1_authpub(rctx,
  1030. authpubp,
  1031. authpublen)))
  1032. overallresult = 0;
  1033. }
  1034. if (startseq != 0) {
  1035. if (!TEST_true(OSSL_HPKE_CTX_set_seq(rctx, startseq)))
  1036. overallresult = 0;
  1037. }
  1038. if (!TEST_true(OSSL_HPKE_decap(rctx, senderpub,
  1039. senderpublen, privp,
  1040. infop, infolen)))
  1041. overallresult = 0;
  1042. /* throw in a call with a too-short clearlen */
  1043. clearlen = 15;
  1044. if (!TEST_false(OSSL_HPKE_open(rctx, clear, &clearlen,
  1045. aadp, aadlen, cipher,
  1046. cipherlen)))
  1047. overallresult = 0;
  1048. /* fix up real clearlen again */
  1049. clearlen = OSSL_HPKE_TSTSIZE;
  1050. if (!TEST_true(OSSL_HPKE_open(rctx, clear, &clearlen,
  1051. aadp, aadlen, cipher,
  1052. cipherlen)))
  1053. overallresult = 0;
  1054. OSSL_HPKE_CTX_free(rctx);
  1055. EVP_PKEY_free(privp);
  1056. privp = NULL;
  1057. /* check output */
  1058. if (!TEST_mem_eq(clear, clearlen, plain, plainlen)) {
  1059. overallresult = 0;
  1060. }
  1061. if (verbose || overallresult != 1) {
  1062. const char *res = NULL;
  1063. res = (overallresult == 1 ? "worked" : "failed");
  1064. TEST_note("HPKE %s for mode: %s/0x%02x, "\
  1065. "kem: %s/0x%02x, kdf: %s/0x%02x, "\
  1066. "aead: %s/0x%02x", res,
  1067. mode_str_list[mind], (int) mind,
  1068. kem_str_list[kemind], kem_id,
  1069. kdf_str_list[kdfind], kdf_id,
  1070. aead_str_list[aeadind], aead_id);
  1071. }
  1072. }
  1073. }
  1074. EVP_PKEY_free(authpriv);
  1075. }
  1076. }
  1077. return overallresult;
  1078. }
  1079. /**
  1080. * @brief check roundtrip for export
  1081. * @return 1 for success, other otherwise
  1082. */
  1083. static int test_hpke_export(void)
  1084. {
  1085. int erv = 0;
  1086. EVP_PKEY *privp = NULL;
  1087. unsigned char pub[OSSL_HPKE_TSTSIZE];
  1088. size_t publen = sizeof(pub);
  1089. int hpke_mode = OSSL_HPKE_MODE_BASE;
  1090. OSSL_HPKE_SUITE hpke_suite = OSSL_HPKE_SUITE_DEFAULT;
  1091. OSSL_HPKE_CTX *ctx = NULL;
  1092. OSSL_HPKE_CTX *rctx = NULL;
  1093. unsigned char exp[32];
  1094. unsigned char exp2[32];
  1095. unsigned char rexp[32];
  1096. unsigned char rexp2[32];
  1097. unsigned char plain[] = "quick brown fox";
  1098. size_t plainlen = sizeof(plain);
  1099. unsigned char enc[OSSL_HPKE_TSTSIZE];
  1100. size_t enclen = sizeof(enc);
  1101. unsigned char cipher[OSSL_HPKE_TSTSIZE];
  1102. size_t cipherlen = sizeof(cipher);
  1103. unsigned char clear[OSSL_HPKE_TSTSIZE];
  1104. size_t clearlen = sizeof(clear);
  1105. char *estr = "foo";
  1106. if (!TEST_true(OSSL_HPKE_keygen(hpke_suite, pub, &publen, &privp,
  1107. NULL, 0, testctx, NULL)))
  1108. goto end;
  1109. if (!TEST_ptr(ctx = OSSL_HPKE_CTX_new(hpke_mode, hpke_suite,
  1110. testctx, NULL)))
  1111. goto end;
  1112. /* a few error cases 1st */
  1113. if (!TEST_false(OSSL_HPKE_export(NULL, exp, sizeof(exp),
  1114. (unsigned char *)estr, strlen(estr))))
  1115. goto end;
  1116. /* ctx before encap should fail too */
  1117. if (!TEST_false(OSSL_HPKE_export(ctx, exp, sizeof(exp),
  1118. (unsigned char *)estr, strlen(estr))))
  1119. goto end;
  1120. if (!TEST_true(OSSL_HPKE_encap(ctx, enc, &enclen, pub, publen, NULL, 0)))
  1121. goto end;
  1122. if (!TEST_true(OSSL_HPKE_seal(ctx, cipher, &cipherlen, NULL, 0,
  1123. plain, plainlen)))
  1124. goto end;
  1125. /* now for real */
  1126. if (!TEST_true(OSSL_HPKE_export(ctx, exp, sizeof(exp),
  1127. (unsigned char *)estr, strlen(estr))))
  1128. goto end;
  1129. /* check a 2nd call with same input gives same output */
  1130. if (!TEST_true(OSSL_HPKE_export(ctx, exp2, sizeof(exp2),
  1131. (unsigned char *)estr, strlen(estr))))
  1132. goto end;
  1133. if (!TEST_mem_eq(exp, sizeof(exp), exp2, sizeof(exp2)))
  1134. goto end;
  1135. if (!TEST_ptr(rctx = OSSL_HPKE_CTX_new(hpke_mode, hpke_suite,
  1136. testctx, NULL)))
  1137. goto end;
  1138. if (!TEST_true(OSSL_HPKE_decap(rctx, enc, enclen, privp, NULL, 0)))
  1139. goto end;
  1140. if (!TEST_true(OSSL_HPKE_open(rctx, clear, &clearlen, NULL, 0,
  1141. cipher, cipherlen)))
  1142. goto end;
  1143. if (!TEST_true(OSSL_HPKE_export(rctx, rexp, sizeof(rexp),
  1144. (unsigned char *)estr, strlen(estr))))
  1145. goto end;
  1146. /* check a 2nd call with same input gives same output */
  1147. if (!TEST_true(OSSL_HPKE_export(rctx, rexp2, sizeof(rexp2),
  1148. (unsigned char *)estr, strlen(estr))))
  1149. goto end;
  1150. if (!TEST_mem_eq(rexp, sizeof(rexp), rexp2, sizeof(rexp2)))
  1151. goto end;
  1152. if (!TEST_mem_eq(exp, sizeof(exp), rexp, sizeof(rexp)))
  1153. goto end;
  1154. erv = 1;
  1155. end:
  1156. OSSL_HPKE_CTX_free(ctx);
  1157. OSSL_HPKE_CTX_free(rctx);
  1158. EVP_PKEY_free(privp);
  1159. return erv;
  1160. }
  1161. /**
  1162. * @brief Check mapping from strings to HPKE suites
  1163. * @return 1 for success, other otherwise
  1164. */
  1165. static int test_hpke_suite_strs(void)
  1166. {
  1167. int overallresult = 1;
  1168. int kemind = 0;
  1169. int kdfind = 0;
  1170. int aeadind = 0;
  1171. int sind = 0;
  1172. char sstr[128];
  1173. OSSL_HPKE_SUITE stirred;
  1174. char giant[2048];
  1175. size_t suitesize;
  1176. size_t ptr_suitesize;
  1177. for (kemind = 0; kemind != OSSL_NELEM(kem_str_list); kemind++) {
  1178. for (kdfind = 0; kdfind != OSSL_NELEM(kdf_str_list); kdfind++) {
  1179. for (aeadind = 0; aeadind != OSSL_NELEM(aead_str_list); aeadind++) {
  1180. snprintf(sstr, 128, "%s,%s,%s", kem_str_list[kemind],
  1181. kdf_str_list[kdfind], aead_str_list[aeadind]);
  1182. if (TEST_true(OSSL_HPKE_str2suite(sstr, &stirred)) != 1) {
  1183. if (verbose)
  1184. TEST_note("Unexpected str2suite fail for :%s",
  1185. bogus_suite_strs[sind]);
  1186. overallresult = 0;
  1187. }
  1188. }
  1189. }
  1190. }
  1191. for (sind = 0; sind != OSSL_NELEM(bogus_suite_strs); sind++) {
  1192. if (TEST_false(OSSL_HPKE_str2suite(bogus_suite_strs[sind],
  1193. &stirred)) != 1) {
  1194. if (verbose)
  1195. TEST_note("OSSL_HPKE_str2suite didn't fail for bogus[%d]:%s",
  1196. sind, bogus_suite_strs[sind]);
  1197. overallresult = 0;
  1198. }
  1199. }
  1200. /* check a few errors */
  1201. if (!TEST_false(OSSL_HPKE_str2suite("", &stirred)))
  1202. overallresult = 0;
  1203. if (!TEST_false(OSSL_HPKE_str2suite(NULL, &stirred)))
  1204. overallresult = 0;
  1205. if (!TEST_false(OSSL_HPKE_str2suite("", NULL)))
  1206. overallresult = 0;
  1207. memset(giant, 'A', sizeof(giant) - 1);
  1208. giant[sizeof(giant) - 1] = '\0';
  1209. if (!TEST_false(OSSL_HPKE_str2suite(giant, &stirred)))
  1210. overallresult = 0;
  1211. /* we'll check the size of a suite just to see what we get */
  1212. suitesize = sizeof(stirred);
  1213. ptr_suitesize = sizeof(&stirred);
  1214. if (verbose) {
  1215. TEST_note("Size of OSSL_HPKE_SUITE is %d, size of ptr is %d",
  1216. (int) suitesize, (int) ptr_suitesize);
  1217. }
  1218. return overallresult;
  1219. }
  1220. /**
  1221. * @brief try the various GREASEy APIs
  1222. * @return 1 for success, other otherwise
  1223. */
  1224. static int test_hpke_grease(void)
  1225. {
  1226. int overallresult = 1;
  1227. OSSL_HPKE_SUITE g_suite;
  1228. unsigned char g_pub[OSSL_HPKE_TSTSIZE];
  1229. size_t g_pub_len = OSSL_HPKE_TSTSIZE;
  1230. unsigned char g_cipher[OSSL_HPKE_TSTSIZE];
  1231. size_t g_cipher_len = 266;
  1232. size_t clearlen = 128;
  1233. size_t expanded = 0;
  1234. size_t enclen = 0;
  1235. size_t ikmelen = 0;
  1236. memset(&g_suite, 0, sizeof(OSSL_HPKE_SUITE));
  1237. /* GREASEing */
  1238. /* check too short for public value */
  1239. g_pub_len = 10;
  1240. if (TEST_false(OSSL_HPKE_get_grease_value(testctx, NULL, NULL, &g_suite,
  1241. g_pub, &g_pub_len,
  1242. g_cipher, g_cipher_len)) != 1) {
  1243. overallresult = 0;
  1244. }
  1245. /* reset to work */
  1246. g_pub_len = OSSL_HPKE_TSTSIZE;
  1247. if (TEST_true(OSSL_HPKE_get_grease_value(testctx, NULL, NULL, &g_suite,
  1248. g_pub, &g_pub_len,
  1249. g_cipher, g_cipher_len)) != 1) {
  1250. overallresult = 0;
  1251. }
  1252. /* expansion */
  1253. expanded = OSSL_HPKE_get_ciphertext_size(g_suite, clearlen);
  1254. if (!TEST_size_t_gt(expanded, clearlen)) {
  1255. overallresult = 0;
  1256. }
  1257. enclen = OSSL_HPKE_get_public_encap_size(g_suite);
  1258. if (!TEST_size_t_ne(enclen, 0))
  1259. overallresult = 0;
  1260. /* not really GREASE but we'll check ikmelen thing */
  1261. ikmelen = OSSL_HPKE_get_recommended_ikmelen(g_suite);
  1262. if (!TEST_size_t_ne(ikmelen, 0))
  1263. overallresult = 0;
  1264. return overallresult;
  1265. }
  1266. /*
  1267. * Make a set of calls with odd parameters
  1268. */
  1269. static int test_hpke_oddcalls(void)
  1270. {
  1271. int erv = 0;
  1272. EVP_PKEY *privp = NULL;
  1273. unsigned char pub[OSSL_HPKE_TSTSIZE];
  1274. size_t publen = sizeof(pub);
  1275. int hpke_mode = OSSL_HPKE_MODE_BASE;
  1276. int bad_mode = 0xbad;
  1277. OSSL_HPKE_SUITE hpke_suite = OSSL_HPKE_SUITE_DEFAULT;
  1278. OSSL_HPKE_SUITE bad_suite = { 0xbad, 0xbad, 0xbad };
  1279. OSSL_HPKE_CTX *ctx = NULL;
  1280. OSSL_HPKE_CTX *rctx = NULL;
  1281. unsigned char plain[] = "quick brown fox";
  1282. size_t plainlen = sizeof(plain);
  1283. unsigned char enc[OSSL_HPKE_TSTSIZE];
  1284. size_t enclen = sizeof(enc);
  1285. unsigned char cipher[OSSL_HPKE_TSTSIZE];
  1286. size_t cipherlen = sizeof(cipher);
  1287. unsigned char clear[OSSL_HPKE_TSTSIZE];
  1288. size_t clearlen = sizeof(clear);
  1289. unsigned char fake_ikm[OSSL_HPKE_TSTSIZE];
  1290. char *badpropq = "yeah, this won't work";
  1291. uint64_t lseq = 0;
  1292. char giant_pskid[OSSL_HPKE_MAX_PARMLEN + 10];
  1293. unsigned char info[OSSL_HPKE_TSTSIZE];
  1294. /* many of the calls below are designed to get better test coverage */
  1295. /* NULL ctx calls */
  1296. OSSL_HPKE_CTX_free(NULL);
  1297. if (!TEST_false(OSSL_HPKE_CTX_set_seq(NULL, 1)))
  1298. goto end;
  1299. if (!TEST_false(OSSL_HPKE_CTX_get_seq(NULL, &lseq)))
  1300. goto end;
  1301. if (!TEST_false(OSSL_HPKE_CTX_set1_authpub(NULL, pub, publen)))
  1302. goto end;
  1303. if (!TEST_false(OSSL_HPKE_CTX_set1_authpriv(NULL, privp)))
  1304. goto end;
  1305. if (!TEST_false(OSSL_HPKE_CTX_set1_ikme(NULL, NULL, 0)))
  1306. goto end;
  1307. if (!TEST_false(OSSL_HPKE_CTX_set1_psk(NULL, NULL, NULL, 0)))
  1308. goto end;
  1309. /* make/break ctx */
  1310. if (!TEST_ptr(ctx = OSSL_HPKE_CTX_new(hpke_mode, hpke_suite,
  1311. testctx, "foo")))
  1312. goto end;
  1313. OSSL_HPKE_CTX_free(ctx);
  1314. ctx = NULL;
  1315. /* bad suite calls */
  1316. hpke_suite.aead_id = 0xbad;
  1317. if (!TEST_false(OSSL_HPKE_suite_check(hpke_suite)))
  1318. goto end;
  1319. hpke_suite.aead_id = OSSL_HPKE_AEAD_ID_AES_GCM_128;
  1320. if (!TEST_false(OSSL_HPKE_suite_check(bad_suite)))
  1321. goto end;
  1322. if (!TEST_false(OSSL_HPKE_get_recommended_ikmelen(bad_suite)))
  1323. goto end;
  1324. if (!TEST_false(OSSL_HPKE_get_public_encap_size(bad_suite)))
  1325. goto end;
  1326. if (!TEST_false(OSSL_HPKE_get_ciphertext_size(bad_suite, 0)))
  1327. goto end;
  1328. if (!TEST_false(OSSL_HPKE_keygen(bad_suite, pub, &publen, &privp,
  1329. NULL, 0, testctx, badpropq)))
  1330. goto end;
  1331. if (!TEST_false(OSSL_HPKE_keygen(bad_suite, pub, &publen, &privp,
  1332. NULL, 0, testctx, NULL)))
  1333. goto end;
  1334. /* dodgy keygen calls */
  1335. /* no pub */
  1336. if (!TEST_false(OSSL_HPKE_keygen(hpke_suite, NULL, &publen, &privp,
  1337. NULL, 0, testctx, NULL)))
  1338. goto end;
  1339. /* ikmlen but NULL ikm */
  1340. if (!TEST_false(OSSL_HPKE_keygen(hpke_suite, pub, &publen, &privp,
  1341. NULL, 80, testctx, NULL)))
  1342. goto end;
  1343. /* zero ikmlen but ikm */
  1344. if (!TEST_false(OSSL_HPKE_keygen(hpke_suite, pub, &publen, &privp,
  1345. fake_ikm, 0, testctx, NULL)))
  1346. goto end;
  1347. /* GIANT ikmlen */
  1348. if (!TEST_false(OSSL_HPKE_keygen(hpke_suite, pub, &publen, &privp,
  1349. fake_ikm, -1, testctx, NULL)))
  1350. goto end;
  1351. /* short publen */
  1352. publen = 10;
  1353. if (!TEST_false(OSSL_HPKE_keygen(hpke_suite, pub, &publen, &privp,
  1354. NULL, 0, testctx, NULL)))
  1355. goto end;
  1356. publen = sizeof(pub);
  1357. /* encap/decap with NULLs */
  1358. if (!TEST_false(OSSL_HPKE_encap(NULL, NULL, NULL, NULL, 0, NULL, 0)))
  1359. goto end;
  1360. if (!TEST_false(OSSL_HPKE_decap(NULL, NULL, 0, NULL, NULL, 0)))
  1361. goto end;
  1362. /*
  1363. * run through a sender/recipient set of calls but with
  1364. * failing calls interspersed whenever possible
  1365. */
  1366. /* good keygen */
  1367. if (!TEST_true(OSSL_HPKE_keygen(hpke_suite, pub, &publen, &privp,
  1368. NULL, 0, testctx, NULL)))
  1369. goto end;
  1370. /* a psk context with no psk => encap fail */
  1371. if (!TEST_ptr(ctx = OSSL_HPKE_CTX_new(OSSL_HPKE_MODE_PSK, hpke_suite,
  1372. testctx, NULL)))
  1373. goto end;
  1374. /* set bad length psk */
  1375. if (!TEST_false(OSSL_HPKE_CTX_set1_psk(ctx, "foo",
  1376. (unsigned char *)"bar", -1)))
  1377. goto end;
  1378. /* set bad length pskid */
  1379. memset(giant_pskid, 'A', sizeof(giant_pskid) - 1);
  1380. giant_pskid[sizeof(giant_pskid) - 1] = '\0';
  1381. if (!TEST_false(OSSL_HPKE_CTX_set1_psk(ctx, giant_pskid,
  1382. (unsigned char *)"bar", 3)))
  1383. goto end;
  1384. /* still no psk really set so encap fails */
  1385. if (!TEST_false(OSSL_HPKE_encap(ctx, enc, &enclen, pub, publen, NULL, 0)))
  1386. goto end;
  1387. OSSL_HPKE_CTX_free(ctx);
  1388. /* bad suite */
  1389. if (!TEST_ptr_null(ctx = OSSL_HPKE_CTX_new(hpke_mode, bad_suite,
  1390. testctx, NULL)))
  1391. goto end;
  1392. /* bad mode */
  1393. if (!TEST_ptr_null(ctx = OSSL_HPKE_CTX_new(bad_mode, hpke_suite,
  1394. testctx, NULL)))
  1395. goto end;
  1396. /* make good ctx */
  1397. if (!TEST_ptr(ctx = OSSL_HPKE_CTX_new(hpke_mode, hpke_suite,
  1398. testctx, NULL)))
  1399. goto end;
  1400. /* too long ikm */
  1401. if (!TEST_false(OSSL_HPKE_CTX_set1_ikme(ctx, fake_ikm, -1)))
  1402. goto end;
  1403. /* zero length ikm */
  1404. if (!TEST_false(OSSL_HPKE_CTX_set1_ikme(ctx, fake_ikm, 0)))
  1405. goto end;
  1406. /* NULL authpub */
  1407. if (!TEST_false(OSSL_HPKE_CTX_set1_authpub(ctx, NULL, 0)))
  1408. goto end;
  1409. /* NULL auth priv */
  1410. if (!TEST_false(OSSL_HPKE_CTX_set1_authpriv(ctx, NULL)))
  1411. goto end;
  1412. /* priv good, but mode is bad */
  1413. if (!TEST_false(OSSL_HPKE_CTX_set1_authpriv(ctx, privp)))
  1414. goto end;
  1415. /* bad mode for psk */
  1416. if (!TEST_false(OSSL_HPKE_CTX_set1_psk(ctx, "foo",
  1417. (unsigned char *)"bar", 3)))
  1418. goto end;
  1419. /* seal before encap */
  1420. if (!TEST_false(OSSL_HPKE_seal(ctx, cipher, &cipherlen, NULL, 0,
  1421. plain, plainlen)))
  1422. goto end;
  1423. /* encap with dodgy public */
  1424. if (!TEST_false(OSSL_HPKE_encap(ctx, enc, &enclen, pub, 1, NULL, 0)))
  1425. goto end;
  1426. /* encap with too big info */
  1427. if (!TEST_false(OSSL_HPKE_encap(ctx, enc, &enclen, pub, 1, info, -1)))
  1428. goto end;
  1429. /* good encap */
  1430. if (!TEST_true(OSSL_HPKE_encap(ctx, enc, &enclen, pub, publen, NULL, 0)))
  1431. goto end;
  1432. /* second encap fail */
  1433. if (!TEST_false(OSSL_HPKE_encap(ctx, enc, &enclen, pub, publen, NULL, 0)))
  1434. goto end;
  1435. plainlen = 0;
  1436. /* should fail for no plaintext */
  1437. if (!TEST_false(OSSL_HPKE_seal(ctx, cipher, &cipherlen, NULL, 0,
  1438. plain, plainlen)))
  1439. goto end;
  1440. /* the sequence ought not have been incremented, so good to start over */
  1441. plainlen = sizeof(plain);
  1442. /* seq wrap around test */
  1443. if (!TEST_true(OSSL_HPKE_CTX_set_seq(ctx, -1)))
  1444. goto end;
  1445. if (!TEST_false(OSSL_HPKE_seal(ctx, cipher, &cipherlen, NULL, 0,
  1446. plain, plainlen)))
  1447. goto end;
  1448. /* reset seq */
  1449. if (!TEST_true(OSSL_HPKE_CTX_set_seq(ctx, 0)))
  1450. goto end;
  1451. /* working seal */
  1452. if (!TEST_true(OSSL_HPKE_seal(ctx, cipher, &cipherlen, NULL, 0,
  1453. plain, plainlen)))
  1454. goto end;
  1455. /* receiver side */
  1456. /* decap fail with psk mode but no psk set */
  1457. if (!TEST_ptr(rctx = OSSL_HPKE_CTX_new(OSSL_HPKE_MODE_PSK, hpke_suite,
  1458. testctx, NULL)))
  1459. goto end;
  1460. if (!TEST_false(OSSL_HPKE_decap(rctx, enc, enclen, privp, NULL, 0)))
  1461. goto end;
  1462. /* done with PSK mode */
  1463. OSSL_HPKE_CTX_free(rctx);
  1464. /* back good calls for base mode */
  1465. if (!TEST_ptr(rctx = OSSL_HPKE_CTX_new(hpke_mode, hpke_suite,
  1466. testctx, NULL)))
  1467. goto end;
  1468. /* open before decap */
  1469. if (!TEST_false(OSSL_HPKE_open(rctx, clear, &clearlen, NULL, 0,
  1470. cipher, cipherlen)))
  1471. goto end;
  1472. /* decap with info too long */
  1473. if (!TEST_false(OSSL_HPKE_decap(rctx, enc, enclen, privp, info, -1)))
  1474. goto end;
  1475. /* good decap */
  1476. if (!TEST_true(OSSL_HPKE_decap(rctx, enc, enclen, privp, NULL, 0)))
  1477. goto end;
  1478. /* second decap fail */
  1479. if (!TEST_false(OSSL_HPKE_decap(rctx, enc, enclen, privp, NULL, 0)))
  1480. goto end;
  1481. /* no space for recovered clear */
  1482. clearlen = 0;
  1483. if (!TEST_false(OSSL_HPKE_open(rctx, clear, &clearlen, NULL, 0,
  1484. cipher, cipherlen)))
  1485. goto end;
  1486. clearlen = OSSL_HPKE_TSTSIZE;
  1487. /* seq wrap around test */
  1488. if (!TEST_true(OSSL_HPKE_CTX_set_seq(rctx, -1)))
  1489. goto end;
  1490. if (!TEST_false(OSSL_HPKE_open(rctx, clear, &clearlen, NULL, 0,
  1491. cipher, cipherlen)))
  1492. goto end;
  1493. if (!TEST_true(OSSL_HPKE_CTX_set_seq(rctx, 0)))
  1494. goto end;
  1495. if (!TEST_true(OSSL_HPKE_open(rctx, clear, &clearlen, NULL, 0,
  1496. cipher, cipherlen)))
  1497. goto end;
  1498. if (!TEST_mem_eq(plain, plainlen, clear, clearlen))
  1499. goto end;
  1500. erv = 1;
  1501. end:
  1502. OSSL_HPKE_CTX_free(ctx);
  1503. OSSL_HPKE_CTX_free(rctx);
  1504. EVP_PKEY_free(privp);
  1505. return erv;
  1506. }
  1507. /* from RFC 9180 Appendix A.1.1 */
  1508. static const unsigned char ikm25519[] = {
  1509. 0x72, 0x68, 0x60, 0x0d, 0x40, 0x3f, 0xce, 0x43,
  1510. 0x15, 0x61, 0xae, 0xf5, 0x83, 0xee, 0x16, 0x13,
  1511. 0x52, 0x7c, 0xff, 0x65, 0x5c, 0x13, 0x43, 0xf2,
  1512. 0x98, 0x12, 0xe6, 0x67, 0x06, 0xdf, 0x32, 0x34
  1513. };
  1514. static const unsigned char pub25519[] = {
  1515. 0x37, 0xfd, 0xa3, 0x56, 0x7b, 0xdb, 0xd6, 0x28,
  1516. 0xe8, 0x86, 0x68, 0xc3, 0xc8, 0xd7, 0xe9, 0x7d,
  1517. 0x1d, 0x12, 0x53, 0xb6, 0xd4, 0xea, 0x6d, 0x44,
  1518. 0xc1, 0x50, 0xf7, 0x41, 0xf1, 0xbf, 0x44, 0x31
  1519. };
  1520. /* from RFC9180 Appendix A.3.1 */
  1521. static const unsigned char ikmp256[] = {
  1522. 0x42, 0x70, 0xe5, 0x4f, 0xfd, 0x08, 0xd7, 0x9d,
  1523. 0x59, 0x28, 0x02, 0x0a, 0xf4, 0x68, 0x6d, 0x8f,
  1524. 0x6b, 0x7d, 0x35, 0xdb, 0xe4, 0x70, 0x26, 0x5f,
  1525. 0x1f, 0x5a, 0xa2, 0x28, 0x16, 0xce, 0x86, 0x0e
  1526. };
  1527. static const unsigned char pubp256[] = {
  1528. 0x04, 0xa9, 0x27, 0x19, 0xc6, 0x19, 0x5d, 0x50,
  1529. 0x85, 0x10, 0x4f, 0x46, 0x9a, 0x8b, 0x98, 0x14,
  1530. 0xd5, 0x83, 0x8f, 0xf7, 0x2b, 0x60, 0x50, 0x1e,
  1531. 0x2c, 0x44, 0x66, 0xe5, 0xe6, 0x7b, 0x32, 0x5a,
  1532. 0xc9, 0x85, 0x36, 0xd7, 0xb6, 0x1a, 0x1a, 0xf4,
  1533. 0xb7, 0x8e, 0x5b, 0x7f, 0x95, 0x1c, 0x09, 0x00,
  1534. 0xbe, 0x86, 0x3c, 0x40, 0x3c, 0xe6, 0x5c, 0x9b,
  1535. 0xfc, 0xb9, 0x38, 0x26, 0x57, 0x22, 0x2d, 0x18,
  1536. 0xc4
  1537. };
  1538. /*
  1539. * A test vector that exercises the counter iteration
  1540. * for p256. This was contributed by Ilari L. on the
  1541. * CFRG list, see the mail archive:
  1542. * https://mailarchive.ietf.org/arch/msg/cfrg/4zwl_y5YN6OU9oeWZOMHNOlOa2w/
  1543. */
  1544. static const unsigned char ikmiter[] = {
  1545. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1546. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1547. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1548. 0x00, 0x00, 0x00, 0x03, 0x01, 0x38, 0xb5, 0xec
  1549. };
  1550. static const unsigned char pubiter[] = {
  1551. 0x04, 0x7d, 0x0c, 0x87, 0xff, 0xd5, 0xd1, 0x45,
  1552. 0x54, 0xa7, 0x51, 0xdf, 0xa3, 0x99, 0x26, 0xa9,
  1553. 0xe3, 0x0e, 0x7c, 0x3c, 0x65, 0x62, 0x4f, 0x4b,
  1554. 0x5f, 0xb3, 0xad, 0x7a, 0xa4, 0xda, 0xc2, 0x4a,
  1555. 0xd8, 0xf5, 0xbe, 0xd0, 0xe8, 0x6e, 0xb8, 0x84,
  1556. 0x1c, 0xe4, 0x89, 0x2e, 0x0f, 0xc3, 0x87, 0xbb,
  1557. 0xdb, 0xfe, 0x16, 0x0d, 0x58, 0x9c, 0x89, 0x2d,
  1558. 0xd4, 0xb1, 0x46, 0x4a, 0xc3, 0x51, 0xc5, 0x6f,
  1559. 0xb6
  1560. };
  1561. /* from RFC9180 Appendix A.6.1 */
  1562. static const unsigned char ikmp521[] = {
  1563. 0x7f, 0x06, 0xab, 0x82, 0x15, 0x10, 0x5f, 0xc4,
  1564. 0x6a, 0xce, 0xeb, 0x2e, 0x3d, 0xc5, 0x02, 0x8b,
  1565. 0x44, 0x36, 0x4f, 0x96, 0x04, 0x26, 0xeb, 0x0d,
  1566. 0x8e, 0x40, 0x26, 0xc2, 0xf8, 0xb5, 0xd7, 0xe7,
  1567. 0xa9, 0x86, 0x68, 0x8f, 0x15, 0x91, 0xab, 0xf5,
  1568. 0xab, 0x75, 0x3c, 0x35, 0x7a, 0x5d, 0x6f, 0x04,
  1569. 0x40, 0x41, 0x4b, 0x4e, 0xd4, 0xed, 0xe7, 0x13,
  1570. 0x17, 0x77, 0x2a, 0xc9, 0x8d, 0x92, 0x39, 0xf7,
  1571. 0x09, 0x04
  1572. };
  1573. static const unsigned char pubp521[] = {
  1574. 0x04, 0x01, 0x38, 0xb3, 0x85, 0xca, 0x16, 0xbb,
  1575. 0x0d, 0x5f, 0xa0, 0xc0, 0x66, 0x5f, 0xbb, 0xd7,
  1576. 0xe6, 0x9e, 0x3e, 0xe2, 0x9f, 0x63, 0x99, 0x1d,
  1577. 0x3e, 0x9b, 0x5f, 0xa7, 0x40, 0xaa, 0xb8, 0x90,
  1578. 0x0a, 0xae, 0xed, 0x46, 0xed, 0x73, 0xa4, 0x90,
  1579. 0x55, 0x75, 0x84, 0x25, 0xa0, 0xce, 0x36, 0x50,
  1580. 0x7c, 0x54, 0xb2, 0x9c, 0xc5, 0xb8, 0x5a, 0x5c,
  1581. 0xee, 0x6b, 0xae, 0x0c, 0xf1, 0xc2, 0x1f, 0x27,
  1582. 0x31, 0xec, 0xe2, 0x01, 0x3d, 0xc3, 0xfb, 0x7c,
  1583. 0x8d, 0x21, 0x65, 0x4b, 0xb1, 0x61, 0xb4, 0x63,
  1584. 0x96, 0x2c, 0xa1, 0x9e, 0x8c, 0x65, 0x4f, 0xf2,
  1585. 0x4c, 0x94, 0xdd, 0x28, 0x98, 0xde, 0x12, 0x05,
  1586. 0x1f, 0x1e, 0xd0, 0x69, 0x22, 0x37, 0xfb, 0x02,
  1587. 0xb2, 0xf8, 0xd1, 0xdc, 0x1c, 0x73, 0xe9, 0xb3,
  1588. 0x66, 0xb5, 0x29, 0xeb, 0x43, 0x6e, 0x98, 0xa9,
  1589. 0x96, 0xee, 0x52, 0x2a, 0xef, 0x86, 0x3d, 0xd5,
  1590. 0x73, 0x9d, 0x2f, 0x29, 0xb0
  1591. };
  1592. static int test_hpke_random_suites(void)
  1593. {
  1594. OSSL_HPKE_SUITE def_suite = OSSL_HPKE_SUITE_DEFAULT;
  1595. OSSL_HPKE_SUITE suite = OSSL_HPKE_SUITE_DEFAULT;
  1596. OSSL_HPKE_SUITE suite2 = { 0xff01, 0xff02, 0xff03 };
  1597. unsigned char enc[200];
  1598. size_t enclen = sizeof(enc);
  1599. unsigned char ct[500];
  1600. size_t ctlen = sizeof(ct);
  1601. /* test with NULL/0 inputs */
  1602. if (!TEST_false(OSSL_HPKE_get_grease_value(testctx, NULL, NULL, NULL,
  1603. NULL, NULL, NULL, 0)))
  1604. return 0;
  1605. enclen = 10;
  1606. if (!TEST_false(OSSL_HPKE_get_grease_value(testctx, NULL, &def_suite,
  1607. &suite2, enc, &enclen,
  1608. ct, ctlen)))
  1609. return 0;
  1610. enclen = sizeof(enc); /* reset, 'cause get_grease() will have set */
  1611. /* test with a should-be-good suite */
  1612. if (!TEST_true(OSSL_HPKE_get_grease_value(testctx, NULL, &def_suite,
  1613. &suite2, enc, &enclen,
  1614. ct, ctlen)))
  1615. return 0;
  1616. /* no suggested suite */
  1617. enclen = sizeof(enc); /* reset, 'cause get_grease() will have set */
  1618. if (!TEST_true(OSSL_HPKE_get_grease_value(testctx, NULL, NULL, &suite2,
  1619. enc, &enclen, ct, ctlen)))
  1620. return 0;
  1621. /* suggested suite with P-521, just to be sure we hit long values */
  1622. enclen = sizeof(enc); /* reset, 'cause get_grease() will have set */
  1623. suite.kem_id = OSSL_HPKE_KEM_ID_P521;
  1624. if (!TEST_true(OSSL_HPKE_get_grease_value(testctx, NULL, &suite, &suite2,
  1625. enc, &enclen, ct, ctlen)))
  1626. return 0;
  1627. enclen = sizeof(enc);
  1628. ctlen = 2; /* too-short cttext (can't fit an aead tag) */
  1629. if (!TEST_false(OSSL_HPKE_get_grease_value(testctx, NULL, NULL, &suite2,
  1630. enc, &enclen, ct, ctlen)))
  1631. return 0;
  1632. ctlen = sizeof(ct);
  1633. enclen = sizeof(enc);
  1634. suite.kem_id = OSSL_HPKE_KEM_ID_X25519; /* back to default */
  1635. suite.aead_id = 0x1234; /* bad aead */
  1636. if (!TEST_false(OSSL_HPKE_get_grease_value(testctx, NULL, &suite, &suite2,
  1637. enc, &enclen, ct, ctlen)))
  1638. return 0;
  1639. enclen = sizeof(enc);
  1640. suite.aead_id = def_suite.aead_id; /* good aead */
  1641. suite.kdf_id = 0x3451; /* bad kdf */
  1642. if (!TEST_false(OSSL_HPKE_get_grease_value(testctx, NULL, &suite, &suite2,
  1643. enc, &enclen, ct, ctlen)))
  1644. return 0;
  1645. enclen = sizeof(enc);
  1646. suite.kdf_id = def_suite.kdf_id; /* good kdf */
  1647. suite.kem_id = 0x4517; /* bad kem */
  1648. if (!TEST_false(OSSL_HPKE_get_grease_value(testctx, NULL, &suite, &suite2,
  1649. enc, &enclen, ct, ctlen)))
  1650. return 0;
  1651. return 1;
  1652. }
  1653. /*
  1654. * @brief generate a key pair from initial key material (ikm) and check public
  1655. * @param kem_id the KEM to use (RFC9180 code point)
  1656. * @ikm is the initial key material buffer
  1657. * @ikmlen is the length of ikm
  1658. * @pub is the public key buffer
  1659. * @publen is the length of the public key
  1660. * @return 1 for good, other otherwise
  1661. *
  1662. * This calls OSSL_HPKE_keygen specifying only the IKM, then
  1663. * compares the key pair values with the already-known values
  1664. * that were input.
  1665. */
  1666. static int test_hpke_one_ikm_gen(uint16_t kem_id,
  1667. const unsigned char *ikm, size_t ikmlen,
  1668. const unsigned char *pub, size_t publen)
  1669. {
  1670. OSSL_HPKE_SUITE hpke_suite = OSSL_HPKE_SUITE_DEFAULT;
  1671. unsigned char lpub[OSSL_HPKE_TSTSIZE];
  1672. size_t lpublen = OSSL_HPKE_TSTSIZE;
  1673. EVP_PKEY *sk = NULL;
  1674. hpke_suite.kem_id = kem_id;
  1675. if (!TEST_true(OSSL_HPKE_keygen(hpke_suite, lpub, &lpublen, &sk,
  1676. ikm, ikmlen, testctx, NULL)))
  1677. return 0;
  1678. if (!TEST_ptr(sk))
  1679. return 0;
  1680. EVP_PKEY_free(sk);
  1681. if (!TEST_mem_eq(pub, publen, lpub, lpublen))
  1682. return 0;
  1683. return 1;
  1684. }
  1685. /*
  1686. * @brief test some uses of IKM produce the expected public keys
  1687. */
  1688. static int test_hpke_ikms(void)
  1689. {
  1690. int res = 1;
  1691. res = test_hpke_one_ikm_gen(OSSL_HPKE_KEM_ID_X25519,
  1692. ikm25519, sizeof(ikm25519),
  1693. pub25519, sizeof(pub25519));
  1694. if (res != 1)
  1695. return res;
  1696. res = test_hpke_one_ikm_gen(OSSL_HPKE_KEM_ID_P521,
  1697. ikmp521, sizeof(ikmp521),
  1698. pubp521, sizeof(pubp521));
  1699. if (res != 1)
  1700. return res;
  1701. res = test_hpke_one_ikm_gen(OSSL_HPKE_KEM_ID_P256,
  1702. ikmp256, sizeof(ikmp256),
  1703. pubp256, sizeof(pubp256));
  1704. if (res != 1)
  1705. return res;
  1706. res = test_hpke_one_ikm_gen(OSSL_HPKE_KEM_ID_P256,
  1707. ikmiter, sizeof(ikmiter),
  1708. pubiter, sizeof(pubiter));
  1709. if (res != 1)
  1710. return res;
  1711. return res;
  1712. }
  1713. /*
  1714. * Test that use of a compressed format auth public key works
  1715. * We'll do a typical round-trip for auth mode but provide the
  1716. * auth public key in compressed form. That should work.
  1717. */
  1718. static int test_hpke_compressed(void)
  1719. {
  1720. int erv = 0;
  1721. EVP_PKEY *privp = NULL;
  1722. unsigned char pub[OSSL_HPKE_TSTSIZE];
  1723. size_t publen = sizeof(pub);
  1724. EVP_PKEY *authpriv = NULL;
  1725. unsigned char authpub[OSSL_HPKE_TSTSIZE];
  1726. size_t authpublen = sizeof(authpub);
  1727. int hpke_mode = OSSL_HPKE_MODE_AUTH;
  1728. OSSL_HPKE_SUITE hpke_suite = OSSL_HPKE_SUITE_DEFAULT;
  1729. OSSL_HPKE_CTX *ctx = NULL;
  1730. OSSL_HPKE_CTX *rctx = NULL;
  1731. unsigned char plain[] = "quick brown fox";
  1732. size_t plainlen = sizeof(plain);
  1733. unsigned char enc[OSSL_HPKE_TSTSIZE];
  1734. size_t enclen = sizeof(enc);
  1735. unsigned char cipher[OSSL_HPKE_TSTSIZE];
  1736. size_t cipherlen = sizeof(cipher);
  1737. unsigned char clear[OSSL_HPKE_TSTSIZE];
  1738. size_t clearlen = sizeof(clear);
  1739. hpke_suite.kem_id = OSSL_HPKE_KEM_ID_P256;
  1740. /* generate auth key pair */
  1741. if (!TEST_true(OSSL_HPKE_keygen(hpke_suite, authpub, &authpublen, &authpriv,
  1742. NULL, 0, testctx, NULL)))
  1743. goto end;
  1744. /* now get the compressed form public key */
  1745. if (!TEST_true(EVP_PKEY_set_utf8_string_param(authpriv,
  1746. OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT,
  1747. OSSL_PKEY_EC_POINT_CONVERSION_FORMAT_COMPRESSED)))
  1748. goto end;
  1749. if (!TEST_true(EVP_PKEY_get_octet_string_param(authpriv,
  1750. OSSL_PKEY_PARAM_PUB_KEY,
  1751. authpub,
  1752. sizeof(authpub),
  1753. &authpublen)))
  1754. goto end;
  1755. /* sender side as usual */
  1756. if (!TEST_true(OSSL_HPKE_keygen(hpke_suite, pub, &publen, &privp,
  1757. NULL, 0, testctx, NULL)))
  1758. goto end;
  1759. if (!TEST_ptr(ctx = OSSL_HPKE_CTX_new(hpke_mode, hpke_suite,
  1760. testctx, NULL)))
  1761. goto end;
  1762. if (!TEST_true(OSSL_HPKE_CTX_set1_authpriv(ctx, authpriv)))
  1763. goto end;
  1764. if (!TEST_true(OSSL_HPKE_encap(ctx, enc, &enclen, pub, publen, NULL, 0)))
  1765. goto end;
  1766. if (!TEST_true(OSSL_HPKE_seal(ctx, cipher, &cipherlen, NULL, 0,
  1767. plain, plainlen)))
  1768. goto end;
  1769. /* receiver side providing compressed form of auth public */
  1770. if (!TEST_ptr(rctx = OSSL_HPKE_CTX_new(hpke_mode, hpke_suite,
  1771. testctx, NULL)))
  1772. goto end;
  1773. if (!TEST_true(OSSL_HPKE_CTX_set1_authpub(rctx, authpub, authpublen)))
  1774. goto end;
  1775. if (!TEST_true(OSSL_HPKE_decap(rctx, enc, enclen, privp, NULL, 0)))
  1776. goto end;
  1777. if (!TEST_true(OSSL_HPKE_open(rctx, clear, &clearlen, NULL, 0,
  1778. cipher, cipherlen)))
  1779. goto end;
  1780. erv = 1;
  1781. end:
  1782. EVP_PKEY_free(privp);
  1783. EVP_PKEY_free(authpriv);
  1784. OSSL_HPKE_CTX_free(ctx);
  1785. OSSL_HPKE_CTX_free(rctx);
  1786. return erv;
  1787. }
  1788. typedef enum OPTION_choice {
  1789. OPT_ERR = -1,
  1790. OPT_EOF = 0,
  1791. OPT_VERBOSE,
  1792. OPT_TEST_ENUM
  1793. } OPTION_CHOICE;
  1794. const OPTIONS *test_get_options(void)
  1795. {
  1796. static const OPTIONS test_options[] = {
  1797. OPT_TEST_OPTIONS_DEFAULT_USAGE,
  1798. { "v", OPT_VERBOSE, '-', "Enable verbose mode" },
  1799. { OPT_HELP_STR, 1, '-', "Run HPKE tests\n" },
  1800. { NULL }
  1801. };
  1802. return test_options;
  1803. }
  1804. int setup_tests(void)
  1805. {
  1806. OPTION_CHOICE o;
  1807. while ((o = opt_next()) != OPT_EOF) {
  1808. switch (o) {
  1809. case OPT_VERBOSE:
  1810. verbose = 1; /* Print progress dots */
  1811. break;
  1812. case OPT_TEST_CASES:
  1813. break;
  1814. default:
  1815. return 0;
  1816. }
  1817. }
  1818. if (!test_get_libctx(&testctx, &nullprov, NULL, &deflprov, "default"))
  1819. return 0;
  1820. ADD_TEST(x25519kdfsha256_hkdfsha256_aes128gcm_base_test);
  1821. ADD_TEST(x25519kdfsha256_hkdfsha256_aes128gcm_psk_test);
  1822. ADD_TEST(P256kdfsha256_hkdfsha256_aes128gcm_base_test);
  1823. ADD_TEST(export_only_test);
  1824. ADD_TEST(test_hpke_export);
  1825. ADD_TEST(test_hpke_modes_suites);
  1826. ADD_TEST(test_hpke_suite_strs);
  1827. ADD_TEST(test_hpke_grease);
  1828. ADD_TEST(test_hpke_ikms);
  1829. ADD_TEST(test_hpke_random_suites);
  1830. ADD_TEST(test_hpke_oddcalls);
  1831. ADD_TEST(test_hpke_compressed);
  1832. return 1;
  1833. }
  1834. void cleanup_tests(void)
  1835. {
  1836. OSSL_PROVIDER_unload(deflprov);
  1837. OSSL_PROVIDER_unload(nullprov);
  1838. OSSL_LIB_CTX_free(testctx);
  1839. }