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