sha3_prov.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /*
  2. * Copyright 2019-2023 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 <string.h>
  10. #include <openssl/core_names.h>
  11. #include <openssl/crypto.h>
  12. #include <openssl/evp.h>
  13. #include <openssl/params.h>
  14. #include <openssl/err.h>
  15. #include <openssl/proverr.h>
  16. #include "internal/numbers.h"
  17. #include "internal/sha3.h"
  18. #include "prov/digestcommon.h"
  19. #include "prov/implementations.h"
  20. #define SHA3_FLAGS PROV_DIGEST_FLAG_ALGID_ABSENT
  21. #define SHAKE_FLAGS PROV_DIGEST_FLAG_XOF
  22. #define KMAC_FLAGS PROV_DIGEST_FLAG_XOF
  23. /*
  24. * Forward declaration of any unique methods implemented here. This is not strictly
  25. * necessary for the compiler, but provides an assurance that the signatures
  26. * of the functions in the dispatch table are correct.
  27. */
  28. static OSSL_FUNC_digest_init_fn keccak_init;
  29. static OSSL_FUNC_digest_init_fn keccak_init_params;
  30. static OSSL_FUNC_digest_update_fn keccak_update;
  31. static OSSL_FUNC_digest_final_fn keccak_final;
  32. static OSSL_FUNC_digest_freectx_fn keccak_freectx;
  33. static OSSL_FUNC_digest_dupctx_fn keccak_dupctx;
  34. static OSSL_FUNC_digest_squeeze_fn shake_squeeze;
  35. static OSSL_FUNC_digest_set_ctx_params_fn shake_set_ctx_params;
  36. static OSSL_FUNC_digest_settable_ctx_params_fn shake_settable_ctx_params;
  37. static sha3_absorb_fn generic_sha3_absorb;
  38. static sha3_final_fn generic_sha3_final;
  39. static sha3_squeeze_fn generic_sha3_squeeze;
  40. #if defined(OPENSSL_CPUID_OBJ) && defined(__s390__) && defined(KECCAK1600_ASM)
  41. /*
  42. * IBM S390X support
  43. */
  44. # include "s390x_arch.h"
  45. # define S390_SHA3 1
  46. # define S390_SHA3_CAPABLE(name) \
  47. ((OPENSSL_s390xcap_P.kimd[0] & S390X_CAPBIT(S390X_##name)) && \
  48. (OPENSSL_s390xcap_P.klmd[0] & S390X_CAPBIT(S390X_##name)))
  49. #endif
  50. static int keccak_init(void *vctx, ossl_unused const OSSL_PARAM params[])
  51. {
  52. if (!ossl_prov_is_running())
  53. return 0;
  54. /* The newctx() handles most of the ctx fixed setup. */
  55. ossl_sha3_reset((KECCAK1600_CTX *)vctx);
  56. return 1;
  57. }
  58. static int keccak_init_params(void *vctx, const OSSL_PARAM params[])
  59. {
  60. return keccak_init(vctx, NULL)
  61. && shake_set_ctx_params(vctx, params);
  62. }
  63. static int keccak_update(void *vctx, const unsigned char *inp, size_t len)
  64. {
  65. KECCAK1600_CTX *ctx = vctx;
  66. const size_t bsz = ctx->block_size;
  67. size_t num, rem;
  68. if (len == 0)
  69. return 1;
  70. /* Is there anything in the buffer already ? */
  71. if ((num = ctx->bufsz) != 0) {
  72. /* Calculate how much space is left in the buffer */
  73. rem = bsz - num;
  74. /* If the new input does not fill the buffer then just add it */
  75. if (len < rem) {
  76. memcpy(ctx->buf + num, inp, len);
  77. ctx->bufsz += len;
  78. return 1;
  79. }
  80. /* otherwise fill up the buffer and absorb the buffer */
  81. memcpy(ctx->buf + num, inp, rem);
  82. /* Update the input pointer */
  83. inp += rem;
  84. len -= rem;
  85. ctx->meth.absorb(ctx, ctx->buf, bsz);
  86. ctx->bufsz = 0;
  87. }
  88. /* Absorb the input - rem = leftover part of the input < blocksize) */
  89. rem = ctx->meth.absorb(ctx, inp, len);
  90. /* Copy the leftover bit of the input into the buffer */
  91. if (rem) {
  92. memcpy(ctx->buf, inp + len - rem, rem);
  93. ctx->bufsz = rem;
  94. }
  95. return 1;
  96. }
  97. static int keccak_final(void *vctx, unsigned char *out, size_t *outl,
  98. size_t outlen)
  99. {
  100. int ret = 1;
  101. KECCAK1600_CTX *ctx = vctx;
  102. if (!ossl_prov_is_running())
  103. return 0;
  104. if (ctx->md_size == SIZE_MAX) {
  105. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH);
  106. return 0;
  107. }
  108. if (outlen > 0)
  109. ret = ctx->meth.final(ctx, out, ctx->md_size);
  110. *outl = ctx->md_size;
  111. return ret;
  112. }
  113. static int shake_squeeze(void *vctx, unsigned char *out, size_t *outl,
  114. size_t outlen)
  115. {
  116. int ret = 1;
  117. KECCAK1600_CTX *ctx = vctx;
  118. if (!ossl_prov_is_running())
  119. return 0;
  120. if (ctx->meth.squeeze == NULL)
  121. return 0;
  122. if (outlen > 0)
  123. ret = ctx->meth.squeeze(ctx, out, outlen);
  124. *outl = outlen;
  125. return ret;
  126. }
  127. /*-
  128. * Generic software version of the absorb() and final().
  129. */
  130. static size_t generic_sha3_absorb(void *vctx, const void *inp, size_t len)
  131. {
  132. KECCAK1600_CTX *ctx = vctx;
  133. if (!(ctx->xof_state == XOF_STATE_INIT ||
  134. ctx->xof_state == XOF_STATE_ABSORB))
  135. return 0;
  136. ctx->xof_state = XOF_STATE_ABSORB;
  137. return SHA3_absorb(ctx->A, inp, len, ctx->block_size);
  138. }
  139. static int generic_sha3_final(void *vctx, unsigned char *out, size_t outlen)
  140. {
  141. return ossl_sha3_final((KECCAK1600_CTX *)vctx, out, outlen);
  142. }
  143. static int generic_sha3_squeeze(void *vctx, unsigned char *out, size_t outlen)
  144. {
  145. return ossl_sha3_squeeze((KECCAK1600_CTX *)vctx, out, outlen);
  146. }
  147. static PROV_SHA3_METHOD sha3_generic_md =
  148. {
  149. generic_sha3_absorb,
  150. generic_sha3_final,
  151. NULL
  152. };
  153. static PROV_SHA3_METHOD shake_generic_md =
  154. {
  155. generic_sha3_absorb,
  156. generic_sha3_final,
  157. generic_sha3_squeeze
  158. };
  159. #if defined(S390_SHA3)
  160. static sha3_absorb_fn s390x_sha3_absorb;
  161. static sha3_final_fn s390x_sha3_final;
  162. static sha3_final_fn s390x_shake_final;
  163. /*-
  164. * The platform specific parts of the absorb() and final() for S390X.
  165. */
  166. static size_t s390x_sha3_absorb(void *vctx, const void *inp, size_t len)
  167. {
  168. KECCAK1600_CTX *ctx = vctx;
  169. size_t rem = len % ctx->block_size;
  170. if (!(ctx->xof_state == XOF_STATE_INIT ||
  171. ctx->xof_state == XOF_STATE_ABSORB))
  172. return 0;
  173. ctx->xof_state = XOF_STATE_ABSORB;
  174. s390x_kimd(inp, len - rem, ctx->pad, ctx->A);
  175. return rem;
  176. }
  177. static int s390x_sha3_final(void *vctx, unsigned char *out, size_t outlen)
  178. {
  179. KECCAK1600_CTX *ctx = vctx;
  180. if (!ossl_prov_is_running())
  181. return 0;
  182. if (!(ctx->xof_state == XOF_STATE_INIT ||
  183. ctx->xof_state == XOF_STATE_ABSORB))
  184. return 0;
  185. ctx->xof_state = XOF_STATE_FINAL;
  186. s390x_klmd(ctx->buf, ctx->bufsz, NULL, 0, ctx->pad, ctx->A);
  187. memcpy(out, ctx->A, outlen);
  188. return 1;
  189. }
  190. static int s390x_shake_final(void *vctx, unsigned char *out, size_t outlen)
  191. {
  192. KECCAK1600_CTX *ctx = vctx;
  193. if (!ossl_prov_is_running())
  194. return 0;
  195. if (!(ctx->xof_state == XOF_STATE_INIT ||
  196. ctx->xof_state == XOF_STATE_ABSORB))
  197. return 0;
  198. ctx->xof_state = XOF_STATE_FINAL;
  199. s390x_klmd(ctx->buf, ctx->bufsz, out, outlen, ctx->pad, ctx->A);
  200. return 1;
  201. }
  202. static int s390x_shake_squeeze(void *vctx, unsigned char *out, size_t outlen)
  203. {
  204. KECCAK1600_CTX *ctx = vctx;
  205. size_t len;
  206. if (!ossl_prov_is_running())
  207. return 0;
  208. if (ctx->xof_state == XOF_STATE_FINAL)
  209. return 0;
  210. /*
  211. * On the first squeeze call, finish the absorb process (incl. padding).
  212. */
  213. if (ctx->xof_state != XOF_STATE_SQUEEZE) {
  214. ctx->xof_state = XOF_STATE_SQUEEZE;
  215. s390x_klmd(ctx->buf, ctx->bufsz, out, outlen, ctx->pad, ctx->A);
  216. ctx->bufsz = outlen % ctx->block_size;
  217. /* reuse ctx->bufsz to count bytes squeezed from current sponge */
  218. return 1;
  219. }
  220. ctx->xof_state = XOF_STATE_SQUEEZE;
  221. if (ctx->bufsz != 0) {
  222. len = ctx->block_size - ctx->bufsz;
  223. if (outlen < len)
  224. len = outlen;
  225. memcpy(out, (char *)ctx->A + ctx->bufsz, len);
  226. out += len;
  227. outlen -= len;
  228. ctx->bufsz += len;
  229. if (ctx->bufsz == ctx->block_size)
  230. ctx->bufsz = 0;
  231. }
  232. if (outlen == 0)
  233. return 1;
  234. s390x_klmd(NULL, 0, out, outlen, ctx->pad | S390X_KLMD_PS, ctx->A);
  235. ctx->bufsz = outlen % ctx->block_size;
  236. return 1;
  237. }
  238. static int s390x_keccakc_final(void *vctx, unsigned char *out, size_t outlen,
  239. int padding)
  240. {
  241. KECCAK1600_CTX *ctx = vctx;
  242. size_t bsz = ctx->block_size;
  243. size_t num = ctx->bufsz;
  244. size_t needed = outlen;
  245. if (!ossl_prov_is_running())
  246. return 0;
  247. if (!(ctx->xof_state == XOF_STATE_INIT ||
  248. ctx->xof_state == XOF_STATE_ABSORB))
  249. return 0;
  250. ctx->xof_state = XOF_STATE_FINAL;
  251. if (outlen == 0)
  252. return 1;
  253. memset(ctx->buf + num, 0, bsz - num);
  254. ctx->buf[num] = padding;
  255. ctx->buf[bsz - 1] |= 0x80;
  256. s390x_kimd(ctx->buf, bsz, ctx->pad, ctx->A);
  257. num = needed > bsz ? bsz : needed;
  258. memcpy(out, ctx->A, num);
  259. needed -= num;
  260. if (needed > 0)
  261. s390x_klmd(NULL, 0, out + bsz, needed, ctx->pad | S390X_KLMD_PS, ctx->A);
  262. return 1;
  263. }
  264. static int s390x_keccak_final(void *vctx, unsigned char *out, size_t outlen)
  265. {
  266. return s390x_keccakc_final(vctx, out, outlen, 0x01);
  267. }
  268. static int s390x_kmac_final(void *vctx, unsigned char *out, size_t outlen)
  269. {
  270. return s390x_keccakc_final(vctx, out, outlen, 0x04);
  271. }
  272. static int s390x_keccakc_squeeze(void *vctx, unsigned char *out, size_t outlen,
  273. int padding)
  274. {
  275. KECCAK1600_CTX *ctx = vctx;
  276. size_t len;
  277. if (!ossl_prov_is_running())
  278. return 0;
  279. if (ctx->xof_state == XOF_STATE_FINAL)
  280. return 0;
  281. /*
  282. * On the first squeeze call, finish the absorb process
  283. * by adding the trailing padding and then doing
  284. * a final absorb.
  285. */
  286. if (ctx->xof_state != XOF_STATE_SQUEEZE) {
  287. len = ctx->block_size - ctx->bufsz;
  288. memset(ctx->buf + ctx->bufsz, 0, len);
  289. ctx->buf[ctx->bufsz] = padding;
  290. ctx->buf[ctx->block_size - 1] |= 0x80;
  291. s390x_kimd(ctx->buf, ctx->block_size, ctx->pad, ctx->A);
  292. ctx->bufsz = 0;
  293. /* reuse ctx->bufsz to count bytes squeezed from current sponge */
  294. }
  295. if (ctx->bufsz != 0 || ctx->xof_state != XOF_STATE_SQUEEZE) {
  296. len = ctx->block_size - ctx->bufsz;
  297. if (outlen < len)
  298. len = outlen;
  299. memcpy(out, (char *)ctx->A + ctx->bufsz, len);
  300. out += len;
  301. outlen -= len;
  302. ctx->bufsz += len;
  303. if (ctx->bufsz == ctx->block_size)
  304. ctx->bufsz = 0;
  305. }
  306. ctx->xof_state = XOF_STATE_SQUEEZE;
  307. if (outlen == 0)
  308. return 1;
  309. s390x_klmd(NULL, 0, out, outlen, ctx->pad | S390X_KLMD_PS, ctx->A);
  310. ctx->bufsz = outlen % ctx->block_size;
  311. return 1;
  312. }
  313. static int s390x_keccak_squeeze(void *vctx, unsigned char *out, size_t outlen)
  314. {
  315. return s390x_keccakc_squeeze(vctx, out, outlen, 0x01);
  316. }
  317. static int s390x_kmac_squeeze(void *vctx, unsigned char *out, size_t outlen)
  318. {
  319. return s390x_keccakc_squeeze(vctx, out, outlen, 0x04);
  320. }
  321. static PROV_SHA3_METHOD sha3_s390x_md =
  322. {
  323. s390x_sha3_absorb,
  324. s390x_sha3_final,
  325. NULL,
  326. };
  327. static PROV_SHA3_METHOD keccak_s390x_md =
  328. {
  329. s390x_sha3_absorb,
  330. s390x_keccak_final,
  331. s390x_keccak_squeeze,
  332. };
  333. static PROV_SHA3_METHOD shake_s390x_md =
  334. {
  335. s390x_sha3_absorb,
  336. s390x_shake_final,
  337. s390x_shake_squeeze,
  338. };
  339. static PROV_SHA3_METHOD kmac_s390x_md =
  340. {
  341. s390x_sha3_absorb,
  342. s390x_kmac_final,
  343. s390x_kmac_squeeze,
  344. };
  345. # define SHAKE_SET_MD(uname, typ) \
  346. if (S390_SHA3_CAPABLE(uname)) { \
  347. ctx->pad = S390X_##uname; \
  348. ctx->meth = typ##_s390x_md; \
  349. } else { \
  350. ctx->meth = shake_generic_md; \
  351. }
  352. # define SHA3_SET_MD(uname, typ) \
  353. if (S390_SHA3_CAPABLE(uname)) { \
  354. ctx->pad = S390X_##uname; \
  355. ctx->meth = typ##_s390x_md; \
  356. } else { \
  357. ctx->meth = sha3_generic_md; \
  358. }
  359. # define KMAC_SET_MD(bitlen) \
  360. if (S390_SHA3_CAPABLE(SHAKE_##bitlen)) { \
  361. ctx->pad = S390X_SHAKE_##bitlen; \
  362. ctx->meth = kmac_s390x_md; \
  363. } else { \
  364. ctx->meth = sha3_generic_md; \
  365. }
  366. #elif defined(__aarch64__) && defined(KECCAK1600_ASM)
  367. # include "arm_arch.h"
  368. static sha3_absorb_fn armsha3_sha3_absorb;
  369. size_t SHA3_absorb_cext(uint64_t A[5][5], const unsigned char *inp, size_t len,
  370. size_t r);
  371. /*-
  372. * Hardware-assisted ARMv8.2 SHA3 extension version of the absorb()
  373. */
  374. static size_t armsha3_sha3_absorb(void *vctx, const void *inp, size_t len)
  375. {
  376. KECCAK1600_CTX *ctx = vctx;
  377. return SHA3_absorb_cext(ctx->A, inp, len, ctx->block_size);
  378. }
  379. static PROV_SHA3_METHOD sha3_ARMSHA3_md =
  380. {
  381. armsha3_sha3_absorb,
  382. generic_sha3_final
  383. };
  384. static PROV_SHA3_METHOD shake_ARMSHA3_md =
  385. {
  386. armsha3_sha3_absorb,
  387. generic_sha3_final,
  388. generic_sha3_squeeze
  389. };
  390. # define SHAKE_SET_MD(uname, typ) \
  391. if (OPENSSL_armcap_P & ARMV8_HAVE_SHA3_AND_WORTH_USING) { \
  392. ctx->meth = shake_ARMSHA3_md; \
  393. } else { \
  394. ctx->meth = shake_generic_md; \
  395. }
  396. # define SHA3_SET_MD(uname, typ) \
  397. if (OPENSSL_armcap_P & ARMV8_HAVE_SHA3_AND_WORTH_USING) { \
  398. ctx->meth = sha3_ARMSHA3_md; \
  399. } else { \
  400. ctx->meth = sha3_generic_md; \
  401. }
  402. # define KMAC_SET_MD(bitlen) \
  403. if (OPENSSL_armcap_P & ARMV8_HAVE_SHA3_AND_WORTH_USING) { \
  404. ctx->meth = sha3_ARMSHA3_md; \
  405. } else { \
  406. ctx->meth = sha3_generic_md; \
  407. }
  408. #else
  409. # define SHA3_SET_MD(uname, typ) ctx->meth = sha3_generic_md;
  410. # define KMAC_SET_MD(bitlen) ctx->meth = sha3_generic_md;
  411. # define SHAKE_SET_MD(uname, typ) ctx->meth = shake_generic_md;
  412. #endif /* S390_SHA3 */
  413. #define SHA3_newctx(typ, uname, name, bitlen, pad) \
  414. static OSSL_FUNC_digest_newctx_fn name##_newctx; \
  415. static void *name##_newctx(void *provctx) \
  416. { \
  417. KECCAK1600_CTX *ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) \
  418. : NULL; \
  419. \
  420. if (ctx == NULL) \
  421. return NULL; \
  422. ossl_sha3_init(ctx, pad, bitlen); \
  423. SHA3_SET_MD(uname, typ) \
  424. return ctx; \
  425. }
  426. #define SHAKE_newctx(typ, uname, name, bitlen, mdlen, pad) \
  427. static OSSL_FUNC_digest_newctx_fn name##_newctx; \
  428. static void *name##_newctx(void *provctx) \
  429. { \
  430. KECCAK1600_CTX *ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx))\
  431. : NULL; \
  432. \
  433. if (ctx == NULL) \
  434. return NULL; \
  435. ossl_keccak_init(ctx, pad, bitlen, mdlen); \
  436. if (mdlen == 0) \
  437. ctx->md_size = SIZE_MAX; \
  438. SHAKE_SET_MD(uname, typ) \
  439. return ctx; \
  440. }
  441. #define KMAC_newctx(uname, bitlen, pad) \
  442. static OSSL_FUNC_digest_newctx_fn uname##_newctx; \
  443. static void *uname##_newctx(void *provctx) \
  444. { \
  445. KECCAK1600_CTX *ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) \
  446. : NULL; \
  447. \
  448. if (ctx == NULL) \
  449. return NULL; \
  450. ossl_keccak_init(ctx, pad, bitlen, 2 * bitlen); \
  451. KMAC_SET_MD(bitlen) \
  452. return ctx; \
  453. }
  454. #define PROV_FUNC_SHA3_DIGEST_COMMON(name, bitlen, blksize, dgstsize, flags) \
  455. PROV_FUNC_DIGEST_GET_PARAM(name, blksize, dgstsize, flags) \
  456. const OSSL_DISPATCH ossl_##name##_functions[] = { \
  457. { OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))name##_newctx }, \
  458. { OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))keccak_update }, \
  459. { OSSL_FUNC_DIGEST_FINAL, (void (*)(void))keccak_final }, \
  460. { OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))keccak_freectx }, \
  461. { OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))keccak_dupctx }, \
  462. PROV_DISPATCH_FUNC_DIGEST_GET_PARAMS(name)
  463. #define PROV_FUNC_SHA3_DIGEST(name, bitlen, blksize, dgstsize, flags) \
  464. PROV_FUNC_SHA3_DIGEST_COMMON(name, bitlen, blksize, dgstsize, flags), \
  465. { OSSL_FUNC_DIGEST_INIT, (void (*)(void))keccak_init }, \
  466. PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_END
  467. #define PROV_FUNC_SHAKE_DIGEST(name, bitlen, blksize, dgstsize, flags) \
  468. PROV_FUNC_SHA3_DIGEST_COMMON(name, bitlen, blksize, dgstsize, flags), \
  469. { OSSL_FUNC_DIGEST_SQUEEZE, (void (*)(void))shake_squeeze }, \
  470. { OSSL_FUNC_DIGEST_INIT, (void (*)(void))keccak_init_params }, \
  471. { OSSL_FUNC_DIGEST_SET_CTX_PARAMS, (void (*)(void))shake_set_ctx_params }, \
  472. { OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS, \
  473. (void (*)(void))shake_settable_ctx_params }, \
  474. PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_END
  475. static void keccak_freectx(void *vctx)
  476. {
  477. KECCAK1600_CTX *ctx = (KECCAK1600_CTX *)vctx;
  478. OPENSSL_clear_free(ctx, sizeof(*ctx));
  479. }
  480. static void *keccak_dupctx(void *ctx)
  481. {
  482. KECCAK1600_CTX *in = (KECCAK1600_CTX *)ctx;
  483. KECCAK1600_CTX *ret = ossl_prov_is_running() ? OPENSSL_malloc(sizeof(*ret))
  484. : NULL;
  485. if (ret != NULL)
  486. *ret = *in;
  487. return ret;
  488. }
  489. static const OSSL_PARAM known_shake_settable_ctx_params[] = {
  490. {OSSL_DIGEST_PARAM_XOFLEN, OSSL_PARAM_UNSIGNED_INTEGER, NULL, 0, 0},
  491. OSSL_PARAM_END
  492. };
  493. static const OSSL_PARAM *shake_settable_ctx_params(ossl_unused void *ctx,
  494. ossl_unused void *provctx)
  495. {
  496. return known_shake_settable_ctx_params;
  497. }
  498. static int shake_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  499. {
  500. const OSSL_PARAM *p;
  501. KECCAK1600_CTX *ctx = (KECCAK1600_CTX *)vctx;
  502. if (ctx == NULL)
  503. return 0;
  504. if (params == NULL)
  505. return 1;
  506. p = OSSL_PARAM_locate_const(params, OSSL_DIGEST_PARAM_XOFLEN);
  507. if (p != NULL && !OSSL_PARAM_get_size_t(p, &ctx->md_size)) {
  508. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  509. return 0;
  510. }
  511. return 1;
  512. }
  513. #define IMPLEMENT_SHA3_functions(bitlen) \
  514. SHA3_newctx(sha3, SHA3_##bitlen, sha3_##bitlen, bitlen, '\x06') \
  515. PROV_FUNC_SHA3_DIGEST(sha3_##bitlen, bitlen, \
  516. SHA3_BLOCKSIZE(bitlen), SHA3_MDSIZE(bitlen), \
  517. SHA3_FLAGS)
  518. #define IMPLEMENT_KECCAK_functions(bitlen) \
  519. SHA3_newctx(keccak, KECCAK_##bitlen, keccak_##bitlen, bitlen, '\x01') \
  520. PROV_FUNC_SHA3_DIGEST(keccak_##bitlen, bitlen, \
  521. SHA3_BLOCKSIZE(bitlen), SHA3_MDSIZE(bitlen), \
  522. SHA3_FLAGS)
  523. #define IMPLEMENT_SHAKE_functions(bitlen) \
  524. SHAKE_newctx(shake, SHAKE_##bitlen, shake_##bitlen, bitlen, \
  525. 0 /* no default md length */, '\x1f') \
  526. PROV_FUNC_SHAKE_DIGEST(shake_##bitlen, bitlen, \
  527. SHA3_BLOCKSIZE(bitlen), 0, \
  528. SHAKE_FLAGS)
  529. #define IMPLEMENT_KMAC_functions(bitlen) \
  530. KMAC_newctx(keccak_kmac_##bitlen, bitlen, '\x04') \
  531. PROV_FUNC_SHAKE_DIGEST(keccak_kmac_##bitlen, bitlen, \
  532. SHA3_BLOCKSIZE(bitlen), KMAC_MDSIZE(bitlen), \
  533. KMAC_FLAGS)
  534. /* ossl_sha3_224_functions */
  535. IMPLEMENT_SHA3_functions(224)
  536. /* ossl_sha3_256_functions */
  537. IMPLEMENT_SHA3_functions(256)
  538. /* ossl_sha3_384_functions */
  539. IMPLEMENT_SHA3_functions(384)
  540. /* ossl_sha3_512_functions */
  541. IMPLEMENT_SHA3_functions(512)
  542. /* ossl_keccak_224_functions */
  543. IMPLEMENT_KECCAK_functions(224)
  544. /* ossl_keccak_256_functions */
  545. IMPLEMENT_KECCAK_functions(256)
  546. /* ossl_keccak_384_functions */
  547. IMPLEMENT_KECCAK_functions(384)
  548. /* ossl_keccak_512_functions */
  549. IMPLEMENT_KECCAK_functions(512)
  550. /* ossl_shake_128_functions */
  551. IMPLEMENT_SHAKE_functions(128)
  552. /* ossl_shake_256_functions */
  553. IMPLEMENT_SHAKE_functions(256)
  554. /* ossl_keccak_kmac_128_functions */
  555. IMPLEMENT_KMAC_functions(128)
  556. /* ossl_keccak_kmac_256_functions */
  557. IMPLEMENT_KMAC_functions(256)