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
10/*
11 * All SHA low level APIs are deprecated for public use, but still ok for
12 * internal use.
13 */
14#include "internal/deprecated.h"
15
16#include <openssl/sha.h>         /* diverse SHA macros */
17#include "internal/sha3.h"       /* KECCAK1600_WIDTH */
18#include "crypto/evp.h"
19/* Used by legacy methods */
20#include "crypto/sha.h"
21#include "legacy_meth.h"
22#include "evp_local.h"
23
24/*-
25 * LEGACY methods for SHA.
26 * These only remain to support engines that can get these methods.
27 * Hardware support for SHA3 has been removed from these legacy cases.
28 */
29#define IMPLEMENT_LEGACY_EVP_MD_METH_SHA3(nm, fn, tag)                         \
30static int nm##_init(EVP_MD_CTX *ctx)                                          \
31{                                                                              \
32    return fn##_init(EVP_MD_CTX_get0_md_data(ctx), tag, ctx->digest->md_size * 8); \
33}                                                                              \
34static int nm##_update(EVP_MD_CTX *ctx, const void *data, size_t count)        \
35{                                                                              \
36    return fn##_update(EVP_MD_CTX_get0_md_data(ctx), data, count);             \
37}                                                                              \
38static int nm##_final(EVP_MD_CTX *ctx, unsigned char *md)                      \
39{                                                                              \
40    return fn##_final(md, EVP_MD_CTX_get0_md_data(ctx));                       \
41}
42#define IMPLEMENT_LEGACY_EVP_MD_METH_SHAKE(nm, fn, tag)                        \
43static int nm##_init(EVP_MD_CTX *ctx)                                          \
44{                                                                              \
45    return fn##_init(EVP_MD_CTX_get0_md_data(ctx), tag, ctx->digest->md_size * 8); \
46}                                                                              \
47
48#define sha512_224_Init    sha512_224_init
49#define sha512_256_Init    sha512_256_init
50
51#define sha512_224_Update  SHA512_Update
52#define sha512_224_Final   SHA512_Final
53#define sha512_256_Update  SHA512_Update
54#define sha512_256_Final   SHA512_Final
55
56IMPLEMENT_LEGACY_EVP_MD_METH(sha1, SHA1)
57IMPLEMENT_LEGACY_EVP_MD_METH(sha224, SHA224)
58IMPLEMENT_LEGACY_EVP_MD_METH(sha256, SHA256)
59IMPLEMENT_LEGACY_EVP_MD_METH(sha384, SHA384)
60IMPLEMENT_LEGACY_EVP_MD_METH(sha512, SHA512)
61IMPLEMENT_LEGACY_EVP_MD_METH(sha512_224_int, sha512_224)
62IMPLEMENT_LEGACY_EVP_MD_METH(sha512_256_int, sha512_256)
63IMPLEMENT_LEGACY_EVP_MD_METH_SHA3(sha3_int, ossl_sha3, '\x06')
64IMPLEMENT_LEGACY_EVP_MD_METH_SHAKE(shake, ossl_sha3, '\x1f')
65
66static int sha1_int_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
67{
68    return ossl_sha1_ctrl(ctx != NULL ? EVP_MD_CTX_get0_md_data(ctx) : NULL,
69                          cmd, p1, p2);
70}
71
72static int shake_ctrl(EVP_MD_CTX *evp_ctx, int cmd, int p1, void *p2)
73{
74    KECCAK1600_CTX *ctx;
75
76    if (evp_ctx == NULL)
77        return 0;
78    ctx = evp_ctx->md_data;
79
80    switch (cmd) {
81    case EVP_MD_CTRL_XOF_LEN:
82        ctx->md_size = p1;
83        return 1;
84    default:
85        return 0;
86    }
87}
88
89
90
91static const EVP_MD sha1_md = {
92    NID_sha1,
93    NID_sha1WithRSAEncryption,
94    SHA_DIGEST_LENGTH,
95    EVP_MD_FLAG_DIGALGID_ABSENT,
96    EVP_ORIG_GLOBAL,
97    LEGACY_EVP_MD_METH_TABLE(sha1_init, sha1_update, sha1_final, sha1_int_ctrl,
98                             SHA_CBLOCK),
99};
100
101const EVP_MD *EVP_sha1(void)
102{
103    return &sha1_md;
104}
105
106static const EVP_MD sha224_md = {
107    NID_sha224,
108    NID_sha224WithRSAEncryption,
109    SHA224_DIGEST_LENGTH,
110    EVP_MD_FLAG_DIGALGID_ABSENT,
111    EVP_ORIG_GLOBAL,
112    LEGACY_EVP_MD_METH_TABLE(sha224_init, sha224_update, sha224_final, NULL,
113                             SHA256_CBLOCK),
114};
115
116const EVP_MD *EVP_sha224(void)
117{
118    return &sha224_md;
119}
120
121static const EVP_MD sha256_md = {
122    NID_sha256,
123    NID_sha256WithRSAEncryption,
124    SHA256_DIGEST_LENGTH,
125    EVP_MD_FLAG_DIGALGID_ABSENT,
126    EVP_ORIG_GLOBAL,
127    LEGACY_EVP_MD_METH_TABLE(sha256_init, sha256_update, sha256_final, NULL,
128                             SHA256_CBLOCK),
129};
130
131const EVP_MD *EVP_sha256(void)
132{
133    return &sha256_md;
134}
135
136static const EVP_MD sha512_224_md = {
137    NID_sha512_224,
138    NID_sha512_224WithRSAEncryption,
139    SHA224_DIGEST_LENGTH,
140    EVP_MD_FLAG_DIGALGID_ABSENT,
141    EVP_ORIG_GLOBAL,
142    LEGACY_EVP_MD_METH_TABLE(sha512_224_int_init, sha512_224_int_update,
143                             sha512_224_int_final, NULL, SHA512_CBLOCK),
144};
145
146const EVP_MD *EVP_sha512_224(void)
147{
148    return &sha512_224_md;
149}
150
151static const EVP_MD sha512_256_md = {
152    NID_sha512_256,
153    NID_sha512_256WithRSAEncryption,
154    SHA256_DIGEST_LENGTH,
155    EVP_MD_FLAG_DIGALGID_ABSENT,
156    EVP_ORIG_GLOBAL,
157    LEGACY_EVP_MD_METH_TABLE(sha512_256_int_init, sha512_256_int_update,
158                             sha512_256_int_final, NULL, SHA512_CBLOCK),
159};
160
161const EVP_MD *EVP_sha512_256(void)
162{
163    return &sha512_256_md;
164}
165
166static const EVP_MD sha384_md = {
167    NID_sha384,
168    NID_sha384WithRSAEncryption,
169    SHA384_DIGEST_LENGTH,
170    EVP_MD_FLAG_DIGALGID_ABSENT,
171    EVP_ORIG_GLOBAL,
172    LEGACY_EVP_MD_METH_TABLE(sha384_init, sha384_update, sha384_final, NULL,
173                             SHA512_CBLOCK),
174};
175
176const EVP_MD *EVP_sha384(void)
177{
178    return &sha384_md;
179}
180
181static const EVP_MD sha512_md = {
182    NID_sha512,
183    NID_sha512WithRSAEncryption,
184    SHA512_DIGEST_LENGTH,
185    EVP_MD_FLAG_DIGALGID_ABSENT,
186    EVP_ORIG_GLOBAL,
187    LEGACY_EVP_MD_METH_TABLE(sha512_init, sha512_update, sha512_final, NULL,
188                             SHA512_CBLOCK),
189};
190
191const EVP_MD *EVP_sha512(void)
192{
193    return &sha512_md;
194}
195
196#define EVP_MD_SHA3(bitlen)                                                    \
197const EVP_MD *EVP_sha3_##bitlen(void)                                          \
198{                                                                              \
199    static const EVP_MD sha3_##bitlen##_md = {                                 \
200        NID_sha3_##bitlen,                                                     \
201        NID_RSA_SHA3_##bitlen,                                                 \
202        bitlen / 8,                                                            \
203        EVP_MD_FLAG_DIGALGID_ABSENT,                                           \
204        EVP_ORIG_GLOBAL,                                                       \
205        LEGACY_EVP_MD_METH_TABLE(sha3_int_init, sha3_int_update,               \
206                                 sha3_int_final, NULL,                         \
207                                 (KECCAK1600_WIDTH - bitlen * 2) / 8),         \
208    };                                                                         \
209    return &sha3_##bitlen##_md;                                                \
210}
211#define EVP_MD_SHAKE(bitlen)                                                   \
212const EVP_MD *EVP_shake##bitlen(void)                                          \
213{                                                                              \
214    static const EVP_MD shake##bitlen##_md = {                                 \
215        NID_shake##bitlen,                                                     \
216        0,                                                                     \
217        bitlen / 8,                                                            \
218        EVP_MD_FLAG_XOF,                                                       \
219        EVP_ORIG_GLOBAL,                                                       \
220        LEGACY_EVP_MD_METH_TABLE(shake_init, sha3_int_update, sha3_int_final,  \
221                        shake_ctrl, (KECCAK1600_WIDTH - bitlen * 2) / 8),      \
222    };                                                                         \
223    return &shake##bitlen##_md;                                                \
224}
225
226EVP_MD_SHA3(224)
227EVP_MD_SHA3(256)
228EVP_MD_SHA3(384)
229EVP_MD_SHA3(512)
230
231EVP_MD_SHAKE(128)
232EVP_MD_SHAKE(256)
233