Coverage Report

Created: 2025-12-10 06:24

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