2 * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
13 #include <openssl/evp.h>
14 #include <openssl/objects.h>
15 #include "internal/evp_int.h"
18 size_t SHA3_absorb(uint64_t A[5][5], const unsigned char *inp, size_t len,
20 void SHA3_squeeze(uint64_t A[5][5], unsigned char *out, size_t len, size_t r);
22 #define KECCAK1600_WIDTH 1600
26 size_t block_size; /* cached ctx->digest->block_size */
27 size_t md_size; /* output length, variable in XOF */
28 size_t num; /* used bytes in below buffer */
29 unsigned char buf[KECCAK1600_WIDTH / 8 - 32];
33 static int init(EVP_MD_CTX *evp_ctx, unsigned char pad)
35 KECCAK1600_CTX *ctx = evp_ctx->md_data;
36 size_t bsz = evp_ctx->digest->block_size;
38 if (bsz <= sizeof(ctx->buf)) {
39 memset(ctx->A, 0, sizeof(ctx->A));
42 ctx->block_size = bsz;
43 ctx->md_size = evp_ctx->digest->md_size;
52 static int sha3_init(EVP_MD_CTX *evp_ctx)
54 return init(evp_ctx, '\x06');
57 static int shake_init(EVP_MD_CTX *evp_ctx)
59 return init(evp_ctx, '\x1f');
62 static int sha3_update(EVP_MD_CTX *evp_ctx, const void *_inp, size_t len)
64 KECCAK1600_CTX *ctx = evp_ctx->md_data;
65 const unsigned char *inp = _inp;
66 size_t bsz = ctx->block_size;
69 if ((num = ctx->num) != 0) { /* process intermediate buffer? */
73 memcpy(ctx->buf + num, inp, len);
78 * We have enough data to fill or overflow the intermediate
79 * buffer. So we append |rem| bytes and process the block,
80 * leaving the rest for later processing...
82 memcpy(ctx->buf + num, inp, rem);
83 inp += rem, len -= rem;
84 (void)SHA3_absorb(ctx->A, ctx->buf, bsz, bsz);
86 /* ctx->buf is processed, ctx->num is guaranteed to be zero */
90 rem = SHA3_absorb(ctx->A, inp, len, bsz);
95 memcpy(ctx->buf, inp + len - rem, rem);
102 static int sha3_final(EVP_MD_CTX *evp_ctx, unsigned char *md)
104 KECCAK1600_CTX *ctx = evp_ctx->md_data;
105 size_t bsz = ctx->block_size;
106 size_t num = ctx->num;
109 * Pad the data with 10*1. Note that |num| can be |bsz - 1|
110 * in which case both byte operations below are performed on
113 memset(ctx->buf + num, 0, bsz - num);
114 ctx->buf[num] = ctx->pad;
115 ctx->buf[bsz - 1] |= 0x80;
117 (void)SHA3_absorb(ctx->A, ctx->buf, bsz, bsz);
119 SHA3_squeeze(ctx->A, md, ctx->md_size, bsz);
124 static int shake_ctrl(EVP_MD_CTX *evp_ctx, int cmd, int p1, void *p2)
126 KECCAK1600_CTX *ctx = evp_ctx->md_data;
129 case EVP_MD_CTRL_XOF_LEN:
137 #define EVP_MD_SHA3(bitlen) \
138 const EVP_MD *EVP_sha3_##bitlen(void) \
140 static const EVP_MD sha3_##bitlen##_md = { \
142 NID_RSA_SHA3_##bitlen, \
144 EVP_MD_FLAG_DIGALGID_ABSENT, \
150 (KECCAK1600_WIDTH - bitlen * 2) / 8, \
151 sizeof(KECCAK1600_CTX), \
153 return &sha3_##bitlen##_md; \
161 #define EVP_MD_SHAKE(bitlen) \
162 const EVP_MD *EVP_shake##bitlen(void) \
164 static const EVP_MD shake##bitlen##_md = { \
174 (KECCAK1600_WIDTH - bitlen * 2) / 8, \
175 sizeof(KECCAK1600_CTX), \
178 return &shake##bitlen##_md; \