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_ctx_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, blksize, dgstsize, flags, \
207 static OSSL_OP_digest_get_params_fn name##_get_params; \
208 static int name##_get_params(OSSL_PARAM params[]) \
210 OSSL_PARAM *p = NULL; \
212 p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_BLOCK_SIZE); \
213 if (p != NULL && !OSSL_PARAM_set_int(p, (blksize))) \
215 p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_SIZE); \
216 if (p != NULL && !OSSL_PARAM_set_int(p, (dgstsize))) \
218 p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_FLAGS); \
219 if (p != NULL && !OSSL_PARAM_set_ulong(p, (flags))) \
223 const OSSL_DISPATCH name##_functions[] = { \
224 { OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))name##_newctx }, \
225 { OSSL_FUNC_DIGEST_INIT, (void (*)(void))keccak_init }, \
226 { OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))keccak_update }, \
227 { OSSL_FUNC_DIGEST_FINAL, (void (*)(void))keccak_final }, \
228 { OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))keccak_freectx }, \
229 { OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))keccak_dupctx }, \
230 { OSSL_FUNC_DIGEST_GET_PARAMS, (void (*)(void))name##_get_params }, \
231 { OSSL_FUNC_DIGEST_CTX_SET_PARAMS, (void (*)(void))stparams }, \
232 OSSL_FUNC_DIGEST_CONSTRUCT_END
234 static void keccak_freectx(void *vctx
)
236 KECCAK1600_CTX
*ctx
= (KECCAK1600_CTX
*)vctx
;
238 OPENSSL_clear_free(ctx
, sizeof(*ctx
));
241 static void *keccak_dupctx(void *ctx
)
243 KECCAK1600_CTX
*in
= (KECCAK1600_CTX
*)ctx
;
244 KECCAK1600_CTX
*ret
= OPENSSL_malloc(sizeof(*ret
));
250 static int shake_set_params(void *vctx
, const OSSL_PARAM params
[])
253 KECCAK1600_CTX
*ctx
= (KECCAK1600_CTX
*)vctx
;
255 if (ctx
!= NULL
&& params
!= NULL
) {
256 p
= OSSL_PARAM_locate_const(params
, OSSL_DIGEST_PARAM_XOFLEN
);
257 if (p
!= NULL
&& !OSSL_PARAM_get_size_t(p
, &ctx
->md_size
))
261 return 0; /* Null Parameter */
264 #define SHA3(bitlen) \
265 SHA3_newctx(sha3, SHA3_##bitlen, sha3_##bitlen, bitlen, '\x06') \
266 OSSL_FUNC_SHA3_DIGEST(sha3_##bitlen, bitlen, \
267 SHA3_BLOCKSIZE(bitlen), SHA3_MDSIZE(bitlen), \
268 EVP_MD_FLAG_DIGALGID_ABSENT, NULL)
270 #define SHAKE(bitlen) \
271 SHA3_newctx(shake, SHAKE_##bitlen, shake_##bitlen, bitlen, '\x1f') \
272 OSSL_FUNC_SHA3_DIGEST(shake_##bitlen, bitlen, \
273 SHA3_BLOCKSIZE(bitlen), SHA3_MDSIZE(bitlen), \
274 EVP_MD_FLAG_XOF, shake_set_params)
275 #define KMAC(bitlen) \
276 KMAC_newctx(keccak_kmac_##bitlen, bitlen, '\x04') \
277 OSSL_FUNC_SHA3_DIGEST(keccak_kmac_##bitlen, bitlen, \
278 SHA3_BLOCKSIZE(bitlen), KMAC_MDSIZE(bitlen), \
279 EVP_MD_FLAG_XOF, shake_set_params)