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 | #include <openssl/opensslconf.h> |
11 | |
12 | #include <openssl/obj_mac.h> |
13 | #include <openssl/sha.h> /* diverse SHA macros */ |
14 | #include "internal/sha3.h" /* KECCAK1600_WIDTH */ |
15 | #include "crypto/evp.h" |
16 | |
17 | static const EVP_MD sha1_md = { |
18 | NID_sha1, |
19 | NID_sha1WithRSAEncryption, |
20 | SHA_DIGEST_LENGTH, |
21 | EVP_MD_FLAG_DIGALGID_ABSENT, |
22 | NULL, |
23 | NULL, |
24 | NULL, |
25 | NULL, |
26 | NULL, |
27 | SHA_CBLOCK, |
28 | }; |
29 | |
30 | const EVP_MD *EVP_sha1(void) |
31 | { |
32 | return &sha1_md; |
33 | } |
34 | |
35 | static const EVP_MD sha224_md = { |
36 | NID_sha224, |
37 | NID_sha224WithRSAEncryption, |
38 | SHA224_DIGEST_LENGTH, |
39 | EVP_MD_FLAG_DIGALGID_ABSENT, |
40 | NULL, |
41 | NULL, |
42 | NULL, |
43 | NULL, |
44 | NULL, |
45 | SHA256_CBLOCK, |
46 | }; |
47 | |
48 | const EVP_MD *EVP_sha224(void) |
49 | { |
50 | return &sha224_md; |
51 | } |
52 | |
53 | static const EVP_MD sha256_md = { |
54 | NID_sha256, |
55 | NID_sha256WithRSAEncryption, |
56 | SHA256_DIGEST_LENGTH, |
57 | EVP_MD_FLAG_DIGALGID_ABSENT, |
58 | NULL, |
59 | NULL, |
60 | NULL, |
61 | NULL, |
62 | NULL, |
63 | SHA256_CBLOCK, |
64 | }; |
65 | |
66 | const EVP_MD *EVP_sha256(void) |
67 | { |
68 | return &sha256_md; |
69 | } |
70 | |
71 | static const EVP_MD sha512_224_md = { |
72 | NID_sha512_224, |
73 | NID_sha512_224WithRSAEncryption, |
74 | SHA224_DIGEST_LENGTH, |
75 | EVP_MD_FLAG_DIGALGID_ABSENT, |
76 | NULL, |
77 | NULL, |
78 | NULL, |
79 | NULL, |
80 | NULL, |
81 | SHA512_CBLOCK, |
82 | }; |
83 | |
84 | const EVP_MD *EVP_sha512_224(void) |
85 | { |
86 | return &sha512_224_md; |
87 | } |
88 | |
89 | static const EVP_MD sha512_256_md = { |
90 | NID_sha512_256, |
91 | NID_sha512_256WithRSAEncryption, |
92 | SHA256_DIGEST_LENGTH, |
93 | EVP_MD_FLAG_DIGALGID_ABSENT, |
94 | NULL, |
95 | NULL, |
96 | NULL, |
97 | NULL, |
98 | NULL, |
99 | SHA512_CBLOCK, |
100 | }; |
101 | |
102 | const EVP_MD *EVP_sha512_256(void) |
103 | { |
104 | return &sha512_256_md; |
105 | } |
106 | |
107 | static const EVP_MD sha384_md = { |
108 | NID_sha384, |
109 | NID_sha384WithRSAEncryption, |
110 | SHA384_DIGEST_LENGTH, |
111 | EVP_MD_FLAG_DIGALGID_ABSENT, |
112 | NULL, |
113 | NULL, |
114 | NULL, |
115 | NULL, |
116 | NULL, |
117 | SHA512_CBLOCK, |
118 | }; |
119 | |
120 | const EVP_MD *EVP_sha384(void) |
121 | { |
122 | return &sha384_md; |
123 | } |
124 | |
125 | static const EVP_MD sha512_md = { |
126 | NID_sha512, |
127 | NID_sha512WithRSAEncryption, |
128 | SHA512_DIGEST_LENGTH, |
129 | EVP_MD_FLAG_DIGALGID_ABSENT, |
130 | NULL, |
131 | NULL, |
132 | NULL, |
133 | NULL, |
134 | NULL, |
135 | SHA512_CBLOCK, |
136 | }; |
137 | |
138 | const EVP_MD *EVP_sha512(void) |
139 | { |
140 | return &sha512_md; |
141 | } |
142 | |
143 | # define EVP_MD_SHA3(bitlen) \ |
144 | const EVP_MD *EVP_sha3_##bitlen(void) \ |
145 | { \ |
146 | static const EVP_MD sha3_##bitlen##_md = { \ |
147 | NID_sha3_##bitlen, \ |
148 | NID_RSA_SHA3_##bitlen, \ |
149 | bitlen / 8, \ |
150 | EVP_MD_FLAG_DIGALGID_ABSENT, \ |
151 | NULL, \ |
152 | NULL, \ |
153 | NULL, \ |
154 | NULL, \ |
155 | NULL, \ |
156 | (KECCAK1600_WIDTH - bitlen * 2) / 8, \ |
157 | }; \ |
158 | return &sha3_##bitlen##_md; \ |
159 | } |
160 | # define EVP_MD_SHAKE(bitlen) \ |
161 | const EVP_MD *EVP_shake##bitlen(void) \ |
162 | { \ |
163 | static const EVP_MD shake##bitlen##_md = { \ |
164 | NID_shake##bitlen, \ |
165 | 0, \ |
166 | bitlen / 8, \ |
167 | EVP_MD_FLAG_XOF, \ |
168 | NULL, \ |
169 | NULL, \ |
170 | NULL, \ |
171 | NULL, \ |
172 | NULL, \ |
173 | (KECCAK1600_WIDTH - bitlen * 2) / 8, \ |
174 | }; \ |
175 | return &shake##bitlen##_md; \ |
176 | } |
177 | |
178 | EVP_MD_SHA3(224) |
179 | EVP_MD_SHA3(256) |
180 | EVP_MD_SHA3(384) |
181 | EVP_MD_SHA3(512) |
182 | |
183 | EVP_MD_SHAKE(128) |
184 | EVP_MD_SHAKE(256) |
185 | |