]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/legacy_sha.c
KEYMGMT: Require both get_params and gettable_params, or none
[thirdparty/openssl.git] / crypto / evp / legacy_sha.c
1 /*
2 * Copyright 2019 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) \
30 static int nm##_init(EVP_MD_CTX *ctx) \
31 { \
32 return fn##_init(EVP_MD_CTX_md_data(ctx), tag, ctx->digest->md_size * 8); \
33 } \
34 static int nm##_update(EVP_MD_CTX *ctx, const void *data, size_t count) \
35 { \
36 return fn##_update(EVP_MD_CTX_md_data(ctx), data, count); \
37 } \
38 static int nm##_final(EVP_MD_CTX *ctx, unsigned char *md) \
39 { \
40 return fn##_final(md, EVP_MD_CTX_md_data(ctx)); \
41 }
42 #define IMPLEMENT_LEGACY_EVP_MD_METH_SHAKE(nm, fn, tag) \
43 static int nm##_init(EVP_MD_CTX *ctx) \
44 { \
45 return fn##_init(EVP_MD_CTX_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
56 IMPLEMENT_LEGACY_EVP_MD_METH(sha1, SHA1)
57 IMPLEMENT_LEGACY_EVP_MD_METH(sha224, SHA224)
58 IMPLEMENT_LEGACY_EVP_MD_METH(sha256, SHA256)
59 IMPLEMENT_LEGACY_EVP_MD_METH(sha384, SHA384)
60 IMPLEMENT_LEGACY_EVP_MD_METH(sha512, SHA512)
61 IMPLEMENT_LEGACY_EVP_MD_METH(sha512_224_int, sha512_224)
62 IMPLEMENT_LEGACY_EVP_MD_METH(sha512_256_int, sha512_256)
63 IMPLEMENT_LEGACY_EVP_MD_METH_SHA3(sha3_int, sha3, '\x06')
64 IMPLEMENT_LEGACY_EVP_MD_METH_SHAKE(shake, sha3, '\x1f')
65
66 static int sha1_int_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
67 {
68 return sha1_ctrl(ctx != NULL ? EVP_MD_CTX_md_data(ctx) : NULL, cmd, p1, p2);
69 }
70
71 static int shake_ctrl(EVP_MD_CTX *evp_ctx, int cmd, int p1, void *p2)
72 {
73 KECCAK1600_CTX *ctx = evp_ctx->md_data;
74
75 switch (cmd) {
76 case EVP_MD_CTRL_XOF_LEN:
77 ctx->md_size = p1;
78 return 1;
79 default:
80 return 0;
81 }
82 }
83
84
85
86 static const EVP_MD sha1_md = {
87 NID_sha1,
88 NID_sha1WithRSAEncryption,
89 SHA_DIGEST_LENGTH,
90 EVP_MD_FLAG_DIGALGID_ABSENT,
91 LEGACY_EVP_MD_METH_TABLE(sha1_init, sha1_update, sha1_final, sha1_int_ctrl,
92 SHA_CBLOCK),
93 };
94
95 const EVP_MD *EVP_sha1(void)
96 {
97 return &sha1_md;
98 }
99
100 static const EVP_MD sha224_md = {
101 NID_sha224,
102 NID_sha224WithRSAEncryption,
103 SHA224_DIGEST_LENGTH,
104 EVP_MD_FLAG_DIGALGID_ABSENT,
105 LEGACY_EVP_MD_METH_TABLE(sha224_init, sha224_update, sha224_final, NULL,
106 SHA256_CBLOCK),
107 };
108
109 const EVP_MD *EVP_sha224(void)
110 {
111 return &sha224_md;
112 }
113
114 static const EVP_MD sha256_md = {
115 NID_sha256,
116 NID_sha256WithRSAEncryption,
117 SHA256_DIGEST_LENGTH,
118 EVP_MD_FLAG_DIGALGID_ABSENT,
119 LEGACY_EVP_MD_METH_TABLE(sha256_init, sha256_update, sha256_final, NULL,
120 SHA256_CBLOCK),
121 };
122
123 const EVP_MD *EVP_sha256(void)
124 {
125 return &sha256_md;
126 }
127
128 static const EVP_MD sha512_224_md = {
129 NID_sha512_224,
130 NID_sha512_224WithRSAEncryption,
131 SHA224_DIGEST_LENGTH,
132 EVP_MD_FLAG_DIGALGID_ABSENT,
133 LEGACY_EVP_MD_METH_TABLE(sha512_224_int_init, sha512_224_int_update,
134 sha512_224_int_final, NULL, SHA512_CBLOCK),
135 };
136
137 const EVP_MD *EVP_sha512_224(void)
138 {
139 return &sha512_224_md;
140 }
141
142 static const EVP_MD sha512_256_md = {
143 NID_sha512_256,
144 NID_sha512_256WithRSAEncryption,
145 SHA256_DIGEST_LENGTH,
146 EVP_MD_FLAG_DIGALGID_ABSENT,
147 LEGACY_EVP_MD_METH_TABLE(sha512_256_int_init, sha512_256_int_update,
148 sha512_256_int_final, NULL, SHA512_CBLOCK),
149 };
150
151 const EVP_MD *EVP_sha512_256(void)
152 {
153 return &sha512_256_md;
154 }
155
156 static const EVP_MD sha384_md = {
157 NID_sha384,
158 NID_sha384WithRSAEncryption,
159 SHA384_DIGEST_LENGTH,
160 EVP_MD_FLAG_DIGALGID_ABSENT,
161 LEGACY_EVP_MD_METH_TABLE(sha384_init, sha384_update, sha384_final, NULL,
162 SHA512_CBLOCK),
163 };
164
165 const EVP_MD *EVP_sha384(void)
166 {
167 return &sha384_md;
168 }
169
170 static const EVP_MD sha512_md = {
171 NID_sha512,
172 NID_sha512WithRSAEncryption,
173 SHA512_DIGEST_LENGTH,
174 EVP_MD_FLAG_DIGALGID_ABSENT,
175 LEGACY_EVP_MD_METH_TABLE(sha512_init, sha512_update, sha512_final, NULL,
176 SHA512_CBLOCK),
177 };
178
179 const EVP_MD *EVP_sha512(void)
180 {
181 return &sha512_md;
182 }
183
184 #define EVP_MD_SHA3(bitlen) \
185 const EVP_MD *EVP_sha3_##bitlen(void) \
186 { \
187 static const EVP_MD sha3_##bitlen##_md = { \
188 NID_sha3_##bitlen, \
189 NID_RSA_SHA3_##bitlen, \
190 bitlen / 8, \
191 EVP_MD_FLAG_DIGALGID_ABSENT, \
192 LEGACY_EVP_MD_METH_TABLE(sha3_int_init, sha3_int_update, \
193 sha3_int_final, NULL, \
194 (KECCAK1600_WIDTH - bitlen * 2) / 8), \
195 }; \
196 return &sha3_##bitlen##_md; \
197 }
198 #define EVP_MD_SHAKE(bitlen) \
199 const EVP_MD *EVP_shake##bitlen(void) \
200 { \
201 static const EVP_MD shake##bitlen##_md = { \
202 NID_shake##bitlen, \
203 0, \
204 bitlen / 8, \
205 EVP_MD_FLAG_XOF, \
206 LEGACY_EVP_MD_METH_TABLE(shake_init, sha3_int_update, sha3_int_final, \
207 shake_ctrl, (KECCAK1600_WIDTH - bitlen * 2) / 8), \
208 }; \
209 return &shake##bitlen##_md; \
210 }
211
212 EVP_MD_SHA3(224)
213 EVP_MD_SHA3(256)
214 EVP_MD_SHA3(384)
215 EVP_MD_SHA3(512)
216
217 EVP_MD_SHAKE(128)
218 EVP_MD_SHAKE(256)