2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/core_names.h>
12 #include <openssl/crypto.h>
13 #include <openssl/evp.h>
14 #include <openssl/params.h>
15 #include "internal/sha3.h"
16 #include "internal/core_mkdigest.h"
17 #include "internal/provider_algs.h"
20 * Forward declaration of any unique methods implemented here. This is not strictly
21 * necessary for the compiler, but provides an assurance that the signatures
22 * of the functions in the dispatch table are correct.
24 static OSSL_OP_digest_init_fn keccak_init;
25 static OSSL_OP_digest_update_fn keccak_update;
26 static OSSL_OP_digest_final_fn keccak_final;
27 static OSSL_OP_digest_freectx_fn keccak_freectx;
28 static OSSL_OP_digest_dupctx_fn keccak_dupctx;
29 static OSSL_OP_digest_set_params_fn shake_set_params;
30 static sha3_absorb_fn generic_sha3_absorb;
31 static sha3_final_fn generic_sha3_final;
33 #if defined(OPENSSL_CPUID_OBJ) && defined(__s390__) && defined(KECCAK1600_ASM)
37 # include "s390x_arch.h"
39 # define S390_SHA3_CAPABLE(name) \
40 ((OPENSSL_s390xcap_P.kimd[0] & S390X_CAPBIT(S390X_##name)) && \
41 (OPENSSL_s390xcap_P.klmd[0] & S390X_CAPBIT(S390X_##name)))
45 static int keccak_init(void *vctx)
47 /* The newctx() handles most of the ctx fixed setup. */
48 sha3_reset((KECCAK1600_CTX *)vctx);
52 static int keccak_update(void *vctx, const unsigned char *inp, size_t len)
54 KECCAK1600_CTX *ctx = vctx;
55 const size_t bsz = ctx->block_size;
61 /* Is there anything in the buffer already ? */
62 if ((num = ctx->bufsz) != 0) {
63 /* Calculate how much space is left in the buffer */
65 /* If the new input does not fill the buffer then just add it */
67 memcpy(ctx->buf + num, inp, len);
71 /* otherwise fill up the buffer and absorb the buffer */
72 memcpy(ctx->buf + num, inp, rem);
73 /* Update the input pointer */
76 ctx->meth.absorb(ctx, ctx->buf, bsz);
79 /* Absorb the input - rem = leftover part of the input < blocksize) */
80 rem = ctx->meth.absorb(ctx, inp, len);
81 /* Copy the leftover bit of the input into the buffer */
83 memcpy(ctx->buf, inp + len - rem, rem);
89 static int keccak_final(void *vctx, unsigned char *out, size_t *outl,
93 KECCAK1600_CTX *ctx = vctx;
95 ret = ctx->meth.final(out, ctx);
101 * Generic software version of the absorb() and final().
103 static size_t generic_sha3_absorb(void *vctx, const void *inp, size_t len)
105 KECCAK1600_CTX *ctx = vctx;
107 return SHA3_absorb(ctx->A, inp, len, ctx->block_size);
110 static int generic_sha3_final(unsigned char *md, void *vctx)
112 return sha3_final(md, (KECCAK1600_CTX *)vctx);
115 static PROV_SHA3_METHOD sha3_generic_md =
121 #if defined(S390_SHA3)
123 static sha3_absorb_fn s390x_sha3_absorb;
124 static sha3_final_fn s390x_sha3_final;
125 static sha3_final_fn s390x_shake_final;
128 * The platform specific parts of the absorb() and final() for S390X.
130 static size_t s390x_sha3_absorb(void *vctx, const void *inp, size_t len)
132 KECCAK1600_CTX *ctx = vctx;
133 size_t rem = len % ctx->block_size;
135 s390x_kimd(inp, len - rem, ctx->pad, ctx->A);
139 static int s390x_sha3_final(unsigned char *md, void *vctx)
141 KECCAK1600_CTX *ctx = vctx;
143 s390x_klmd(ctx->buf, ctx->bufsz, NULL, 0, ctx->pad, ctx->A);
144 memcpy(md, ctx->A, ctx->md_size);
148 static int s390x_shake_final(unsigned char *md, void *vctx)
150 KECCAK1600_CTX *ctx = vctx;
152 s390x_klmd(ctx->buf, ctx->bufsz, md, ctx->md_size, ctx->pad, ctx->A);
156 static PROV_SHA3_METHOD sha3_s390x_md =
162 static PROV_SHA3_METHOD shake_s390x_md =
168 # define SHA3_SET_MD(uname, typ) \
169 if (S390_SHA3_CAPABLE(uname)) { \
170 ctx->pad = S390X_##uname; \
171 ctx->meth = typ##_s390x_md; \
173 ctx->meth = sha3_generic_md; \
176 # define SHA3_SET_MD(uname, typ) ctx->meth = sha3_generic_md;
177 #endif /* S390_SHA3 */
179 #define SHA3_newctx(typ, uname, name, bitlen, pad) \
180 static OSSL_OP_digest_newctx_fn name##_newctx; \
181 static void *name##_newctx(void *provctx) \
183 KECCAK1600_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); \
187 sha3_init(ctx, pad, bitlen); \
188 SHA3_SET_MD(uname, typ) \
192 #define KMAC_newctx(uname, bitlen, pad) \
193 static OSSL_OP_digest_newctx_fn uname##_newctx; \
194 static void *uname##_newctx(void *provctx) \
196 KECCAK1600_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); \
200 keccak_kmac_init(ctx, pad, bitlen); \
201 ctx->meth = sha3_generic_md; \
205 #define OSSL_FUNC_SHA3_DIGEST(name, bitlen, dgstsize, stparams) \
206 static OSSL_OP_digest_size_fn name##_size; \
207 static OSSL_OP_digest_block_size_fn name##_block_size; \
208 static size_t name##_block_size(void) \
210 return SHA3_BLOCKSIZE(bitlen); \
212 static size_t name##_size(void) \
216 const OSSL_DISPATCH name##_functions[] = { \
217 { OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))name##_newctx }, \
218 { OSSL_FUNC_DIGEST_INIT, (void (*)(void))keccak_init }, \
219 { OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))keccak_update }, \
220 { OSSL_FUNC_DIGEST_FINAL, (void (*)(void))keccak_final }, \
221 { OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))keccak_freectx }, \
222 { OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))keccak_dupctx }, \
223 { OSSL_FUNC_DIGEST_SIZE, (void (*)(void))name##_size }, \
224 { OSSL_FUNC_DIGEST_BLOCK_SIZE, (void (*)(void))name##_block_size }, \
225 { OSSL_FUNC_DIGEST_SET_PARAMS, (void (*)(void))stparams },\
226 OSSL_FUNC_DIGEST_CONSTRUCT_END
228 static void keccak_freectx(void *vctx)
230 KECCAK1600_CTX *ctx = (KECCAK1600_CTX *)vctx;
232 OPENSSL_clear_free(ctx, sizeof(*ctx));
235 static void *keccak_dupctx(void *ctx)
237 KECCAK1600_CTX *in = (KECCAK1600_CTX *)ctx;
238 KECCAK1600_CTX *ret = OPENSSL_malloc(sizeof(*ret));
244 static int shake_set_params(void *vctx, const OSSL_PARAM params[])
247 KECCAK1600_CTX *ctx = (KECCAK1600_CTX *)vctx;
249 if (ctx != NULL && params != NULL) {
250 p = OSSL_PARAM_locate_const(params, OSSL_DIGEST_PARAM_XOFLEN);
251 if (p != NULL && !OSSL_PARAM_get_size_t(p, &ctx->md_size))
255 return 0; /* Null Parameter */
258 #define SHA3(bitlen) \
259 SHA3_newctx(sha3, SHA3_##bitlen, sha3_##bitlen, bitlen, '\x06') \
260 OSSL_FUNC_SHA3_DIGEST(sha3_##bitlen, bitlen, SHA3_MDSIZE(bitlen), NULL)
262 #define SHAKE(bitlen) \
263 SHA3_newctx(shake, SHAKE_##bitlen, shake_##bitlen, bitlen, '\x1f') \
264 OSSL_FUNC_SHA3_DIGEST(shake_##bitlen, bitlen, SHA3_MDSIZE(bitlen), \
266 #define KMAC(bitlen) \
267 KMAC_newctx(keccak_kmac_##bitlen, bitlen, '\x04') \
268 OSSL_FUNC_SHA3_DIGEST(keccak_kmac_##bitlen, bitlen, KMAC_MDSIZE(bitlen), \