hpke_test.c 74 KB

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