Coverage Report

Created: 2026-01-09 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/providers/implementations/digests/sha2_prov.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
 * 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/byteorder.h>
17
#include <openssl/crypto.h>
18
#include <openssl/core_dispatch.h>
19
#include <openssl/evp.h>
20
#include <openssl/err.h>
21
#include <openssl/sha.h>
22
#include <openssl/params.h>
23
#include <openssl/proverr.h>
24
#include <openssl/core_names.h>
25
#include "prov/digestcommon.h"
26
#include "prov/implementations.h"
27
#include "crypto/sha.h"
28
#include "internal/common.h"
29
#include "providers/implementations/digests/sha2_prov.inc"
30
31
#define SHA2_FLAGS PROV_DIGEST_FLAG_ALGID_ABSENT
32
33
/* Special set_params method for SSL3 */
34
static int sha1_set_ctx_params(void *vctx, const OSSL_PARAM params[])
35
106
{
36
106
    struct sha1_set_ctx_params_st p;
37
106
    SHA_CTX *ctx = (SHA_CTX *)vctx;
38
39
106
    if (ossl_unlikely(ctx == NULL || !sha1_set_ctx_params_decoder(params, &p)))
40
0
        return 0;
41
42
106
    if (p.ssl3_ms != NULL)
43
0
        return ossl_sha1_ctrl(ctx, EVP_CTRL_SSL3_MASTER_SECRET,
44
0
            (int)p.ssl3_ms->data_size, p.ssl3_ms->data);
45
46
106
    return 1;
47
106
}
48
49
static const OSSL_PARAM *sha1_settable_ctx_params(ossl_unused void *ctx,
50
    ossl_unused void *provctx)
51
0
{
52
0
    return sha1_set_ctx_params_list;
53
0
}
54
55
static const unsigned char sha256magic[] = "SHA256v1";
56
0
#define SHA256MAGIC_LEN (sizeof(sha256magic) - 1)
57
#define SHA256_SERIALIZATION_LEN                      \
58
0
    (                                                 \
59
0
        SHA256MAGIC_LEN /* magic */                   \
60
0
        + sizeof(uint32_t) /* c->md_len */            \
61
0
        + sizeof(uint32_t) /* c->num */               \
62
0
        + sizeof(uint32_t) * 8 /* c->h */             \
63
0
        + sizeof(uint32_t) * 2 /* c->Nl + c->Nh */    \
64
0
        + sizeof(uint32_t) * SHA_LBLOCK /* c->data */ \
65
0
    )
66
67
static int SHA256_Serialize(SHA256_CTX *c, unsigned char *out,
68
    size_t *outlen)
69
0
{
70
0
    unsigned char *p;
71
0
    unsigned long i;
72
73
0
    if (out == NULL) {
74
0
        if (outlen == NULL)
75
0
            return 0;
76
77
0
        *outlen = SHA256_SERIALIZATION_LEN;
78
0
        return 1;
79
0
    }
80
81
0
    if (outlen != NULL && *outlen < SHA256_SERIALIZATION_LEN)
82
0
        return 0;
83
84
0
    p = out;
85
86
    /* Magic code */
87
0
    memcpy(p, sha256magic, SHA256MAGIC_LEN);
88
0
    p += SHA256MAGIC_LEN;
89
90
    /* md_len */
91
0
    p = OPENSSL_store_u32_le(p, c->md_len);
92
93
    /* num */
94
0
    p = OPENSSL_store_u32_le(p, c->num);
95
96
    /* h */
97
0
    for (i = 0; i < sizeof(c->h) / sizeof(SHA_LONG); i++)
98
0
        p = OPENSSL_store_u32_le(p, c->h[i]);
99
100
    /* Nl, Nh */
101
0
    p = OPENSSL_store_u32_le(p, c->Nl);
102
0
    p = OPENSSL_store_u32_le(p, c->Nh);
103
104
    /* data */
105
0
    for (i = 0; i < SHA_LBLOCK; i++)
106
0
        p = OPENSSL_store_u32_le(p, c->data[i]);
107
108
0
    if (outlen != NULL)
109
0
        *outlen = SHA256_SERIALIZATION_LEN;
110
111
0
    return 1;
112
0
}
113
114
/*
115
 * This function only performs basic input sanity checks and is not
116
 * built to handle malicious input data. Only trusted input should be
117
 * fed to this function
118
 */
119
static int SHA256_Deserialize(SHA256_CTX *c, const unsigned char *in,
120
    size_t inlen)
121
0
{
122
0
    const unsigned char *p;
123
0
    uint32_t val;
124
0
    unsigned long i;
125
126
0
    if (c == NULL || in == NULL || inlen != SHA256_SERIALIZATION_LEN)
127
0
        return 0;
128
129
    /* Magic code check */
130
0
    if (memcmp(in, sha256magic, SHA256MAGIC_LEN) != 0)
131
0
        return 0;
132
133
0
    p = in + SHA256MAGIC_LEN;
134
135
    /* md_len check */
136
0
    p = OPENSSL_load_u32_le(&val, p);
137
0
    if ((unsigned int)val != c->md_len) {
138
0
        return 0;
139
0
    }
140
141
    /* num check */
142
0
    p = OPENSSL_load_u32_le(&val, p);
143
0
    if (val >= sizeof(c->data))
144
0
        return 0;
145
0
    c->num = (unsigned int)val;
146
147
    /* h */
148
0
    for (i = 0; i < (sizeof(c->h) / sizeof(SHA_LONG)); i++) {
149
0
        p = OPENSSL_load_u32_le(&val, p);
150
0
        c->h[i] = (SHA_LONG)val;
151
0
    }
152
153
    /* Nl, Nh */
154
0
    p = OPENSSL_load_u32_le(&val, p);
155
0
    c->Nl = (SHA_LONG)val;
156
0
    p = OPENSSL_load_u32_le(&val, p);
157
0
    c->Nh = (SHA_LONG)val;
158
159
    /* data */
160
0
    for (i = 0; i < SHA_LBLOCK; i++) {
161
0
        p = OPENSSL_load_u32_le(&val, p);
162
0
        c->data[i] = (SHA_LONG)val;
163
0
    }
164
165
0
    return 1;
166
0
}
167
168
static const unsigned char sha512magic[] = "SHA512v1";
169
0
#define SHA512MAGIC_LEN (sizeof(sha512magic) - 1)
170
#define SHA512_SERIALIZATION_LEN                   \
171
0
    (                                              \
172
0
        SHA512MAGIC_LEN /* magic */                \
173
0
        + sizeof(uint32_t) /* c->md_len */         \
174
0
        + sizeof(uint32_t) /* c->num */            \
175
0
        + sizeof(uint64_t) * 8 /* c->h */          \
176
0
        + sizeof(uint64_t) * 2 /* c->Nl + c->Nh */ \
177
0
        + SHA512_CBLOCK /* c->u.d/c->u.p */        \
178
0
    )
179
180
static int SHA512_Serialize(SHA512_CTX *c, unsigned char *out,
181
    size_t *outlen)
182
0
{
183
0
    unsigned char *p;
184
0
    unsigned long i;
185
186
0
    if (out == NULL) {
187
0
        if (outlen == NULL)
188
0
            return 0;
189
190
0
        *outlen = SHA512_SERIALIZATION_LEN;
191
0
        return 1;
192
0
    }
193
194
0
    if (outlen != NULL && *outlen < SHA512_SERIALIZATION_LEN)
195
0
        return 0;
196
197
0
    p = out;
198
199
    /* Magic code */
200
0
    memcpy(p, sha512magic, SHA512MAGIC_LEN);
201
0
    p += SHA512MAGIC_LEN;
202
203
    /* md_len */
204
0
    p = OPENSSL_store_u32_le(p, c->md_len);
205
206
    /* num */
207
0
    p = OPENSSL_store_u32_le(p, c->num);
208
209
    /* h */
210
0
    for (i = 0; i < sizeof(c->h) / sizeof(SHA_LONG64); i++)
211
0
        p = OPENSSL_store_u64_le(p, c->h[i]);
212
213
    /* Nl, Nh */
214
0
    p = OPENSSL_store_u64_le(p, c->Nl);
215
0
    p = OPENSSL_store_u64_le(p, c->Nh);
216
217
    /* data */
218
0
    memcpy(p, c->u.p, SHA512_CBLOCK);
219
0
    p += SHA512_CBLOCK;
220
221
0
    if (outlen != NULL)
222
0
        *outlen = SHA512_SERIALIZATION_LEN;
223
224
0
    return 1;
225
0
}
226
227
/*
228
 * This function only performs basic input sanity checks and is not
229
 * built to handle malicious input data. Only trusted input should be
230
 * fed to this function
231
 */
232
static int SHA512_Deserialize(SHA512_CTX *c, const unsigned char *in,
233
    size_t inlen)
234
0
{
235
0
    const unsigned char *p;
236
0
    uint32_t val32;
237
0
    uint64_t val;
238
0
    unsigned long i;
239
240
0
    if (c == NULL || in == NULL || inlen != SHA512_SERIALIZATION_LEN)
241
0
        return 0;
242
243
    /* Magic code */
244
0
    if (memcmp(in, sha512magic, SHA512MAGIC_LEN) != 0)
245
0
        return 0;
246
247
0
    p = in + SHA512MAGIC_LEN;
248
249
    /* md_len check */
250
0
    p = OPENSSL_load_u32_le(&val32, p);
251
0
    if ((unsigned int)val32 != c->md_len)
252
0
        return 0;
253
254
    /* num check */
255
0
    p = OPENSSL_load_u32_le(&val32, p);
256
0
    if (val32 >= sizeof(c->u.d))
257
0
        return 0;
258
0
    c->num = (unsigned int)val32;
259
260
    /* h */
261
0
    for (i = 0; i < (sizeof(c->h) / sizeof(SHA_LONG64)); i++) {
262
0
        p = OPENSSL_load_u64_le(&val, p);
263
0
        c->h[i] = (SHA_LONG64)val;
264
0
    }
265
266
    /* Nl, Nh */
267
0
    p = OPENSSL_load_u64_le(&val, p);
268
0
    c->Nl = (SHA_LONG64)val;
269
0
    p = OPENSSL_load_u64_le(&val, p);
270
0
    c->Nh = (SHA_LONG64)val;
271
272
    /* data */
273
0
    memcpy(c->u.p, p, SHA512_CBLOCK);
274
0
    p += SHA512_CBLOCK;
275
276
0
    return 1;
277
0
}
278
279
/* ossl_sha1_functions */
280
53
IMPLEMENT_digest_functions_with_settable_ctx(
Unexecuted instantiation: sha2_prov.c:sha1_newctx
Unexecuted instantiation: sha2_prov.c:sha1_dupctx
281
53
    sha1, SHA_CTX, SHA_CBLOCK, SHA_DIGEST_LENGTH, SHA2_FLAGS,
282
53
    SHA1_Init, SHA1_Update, SHA1_Final,
283
53
    sha1_settable_ctx_params, sha1_set_ctx_params)
284
53
285
53
/* ossl_sha224_functions */
286
53
IMPLEMENT_digest_functions_with_serialize(sha224, SHA256_CTX,
Unexecuted instantiation: sha2_prov.c:sha224_newctx
Unexecuted instantiation: sha2_prov.c:sha224_dupctx
287
27
    SHA256_CBLOCK, SHA224_DIGEST_LENGTH,
288
27
    SHA2_FLAGS, SHA224_Init,
289
27
    SHA224_Update, SHA224_Final,
290
27
    SHA256_Serialize, SHA256_Deserialize)
291
27
292
27
/* ossl_sha256_functions */
293
14.2k
IMPLEMENT_digest_functions_with_serialize(sha256, SHA256_CTX,
Unexecuted instantiation: sha2_prov.c:sha256_newctx
Unexecuted instantiation: sha2_prov.c:sha256_dupctx
294
14.2k
    SHA256_CBLOCK, SHA256_DIGEST_LENGTH,
295
14.2k
    SHA2_FLAGS, SHA256_Init,
296
14.2k
    SHA256_Update, SHA256_Final,
297
14.2k
    SHA256_Serialize, SHA256_Deserialize)
298
14.2k
/* ossl_sha256_192_internal_functions */
299
14.2k
IMPLEMENT_digest_functions_with_serialize(sha256_192_internal, SHA256_CTX,
Unexecuted instantiation: sha2_prov.c:sha256_192_internal_newctx
Unexecuted instantiation: sha2_prov.c:sha256_192_internal_dupctx
300
1
    SHA256_CBLOCK, SHA256_192_DIGEST_LENGTH,
301
1
    SHA2_FLAGS, ossl_sha256_192_init,
302
1
    SHA256_Update, SHA256_Final,
303
1
    SHA256_Serialize, SHA256_Deserialize)
304
1
/* ossl_sha384_functions */
305
13
IMPLEMENT_digest_functions_with_serialize(sha384, SHA512_CTX,
Unexecuted instantiation: sha2_prov.c:sha384_newctx
Unexecuted instantiation: sha2_prov.c:sha384_dupctx
306
13
    SHA512_CBLOCK, SHA384_DIGEST_LENGTH,
307
13
    SHA2_FLAGS, SHA384_Init,
308
13
    SHA384_Update, SHA384_Final,
309
13
    SHA512_Serialize, SHA512_Deserialize)
310
13
311
13
/* ossl_sha512_functions */
312
3.26k
IMPLEMENT_digest_functions_with_serialize(sha512, SHA512_CTX,
Unexecuted instantiation: sha2_prov.c:sha512_newctx
Unexecuted instantiation: sha2_prov.c:sha512_dupctx
313
3.26k
    SHA512_CBLOCK, SHA512_DIGEST_LENGTH,
314
3.26k
    SHA2_FLAGS, SHA512_Init,
315
3.26k
    SHA512_Update, SHA512_Final,
316
3.26k
    SHA512_Serialize, SHA512_Deserialize)
317
3.26k
318
3.26k
/* ossl_sha512_224_functions */
319
3.26k
IMPLEMENT_digest_functions_with_serialize(sha512_224, SHA512_CTX,
Unexecuted instantiation: sha2_prov.c:sha512_224_newctx
Unexecuted instantiation: sha2_prov.c:sha512_224_dupctx
320
1
    SHA512_CBLOCK, SHA224_DIGEST_LENGTH,
321
1
    SHA2_FLAGS, sha512_224_init,
322
1
    SHA512_Update, SHA512_Final,
323
1
    SHA512_Serialize, SHA512_Deserialize)
324
1
325
1
/* ossl_sha512_256_functions */
326
IMPLEMENT_digest_functions_with_serialize(sha512_256, SHA512_CTX,
Unexecuted instantiation: sha2_prov.c:sha512_256_newctx
Unexecuted instantiation: sha2_prov.c:sha512_256_dupctx
327
    SHA512_CBLOCK, SHA256_DIGEST_LENGTH,
328
    SHA2_FLAGS, sha512_256_init,
329
    SHA512_Update, SHA512_Final,
330
    SHA512_Serialize, SHA512_Deserialize)