Coverage Report

Created: 2026-05-11 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/digest/digest_extra.cc
Line
Count
Source
1
// Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include <openssl/digest.h>
16
17
#include <string.h>
18
19
#include <openssl/blake2.h>
20
#include <openssl/bytestring.h>
21
#include <openssl/evp_errors.h>
22
#include <openssl/md4.h>
23
#include <openssl/md5.h>
24
#include <openssl/nid.h>
25
#include <openssl/obj.h>
26
#include <openssl/sha.h>
27
#include <openssl/span.h>
28
29
#include "../asn1/internal.h"
30
#include "../fipsmodule/digest/internal.h"
31
#include "../internal.h"
32
33
34
struct nid_to_digest {
35
  int nid;
36
  const EVP_MD *(*md_func)();
37
  const char *short_name;
38
  const char *long_name;
39
};
40
41
static const struct nid_to_digest nid_to_digest_mapping[] = {
42
    {NID_md4, EVP_md4, SN_md4, LN_md4},
43
    {NID_md5, EVP_md5, SN_md5, LN_md5},
44
    {NID_sha1, EVP_sha1, SN_sha1, LN_sha1},
45
    {NID_sha224, EVP_sha224, SN_sha224, LN_sha224},
46
    {NID_sha256, EVP_sha256, SN_sha256, LN_sha256},
47
    {NID_sha384, EVP_sha384, SN_sha384, LN_sha384},
48
    {NID_sha512, EVP_sha512, SN_sha512, LN_sha512},
49
    {NID_sha512_256, EVP_sha512_256, SN_sha512_256, LN_sha512_256},
50
    {NID_md5_sha1, EVP_md5_sha1, SN_md5_sha1, LN_md5_sha1},
51
    // As a remnant of signing |EVP_MD|s, OpenSSL returned the corresponding
52
    // hash function when given a signature OID. To avoid unintended lax parsing
53
    // of hash OIDs, this is no longer supported for lookup by OID or NID.
54
    // Node.js, however, exposes |EVP_get_digestbyname|'s full behavior to
55
    // consumers so we retain it there.
56
    {NID_undef, EVP_sha1, SN_dsaWithSHA, LN_dsaWithSHA},
57
    {NID_undef, EVP_sha1, SN_dsaWithSHA1, LN_dsaWithSHA1},
58
    {NID_undef, EVP_sha1, SN_ecdsa_with_SHA1, nullptr},
59
    {NID_undef, EVP_md5, SN_md5WithRSAEncryption, LN_md5WithRSAEncryption},
60
    {NID_undef, EVP_sha1, SN_sha1WithRSAEncryption, LN_sha1WithRSAEncryption},
61
    {NID_undef, EVP_sha224, SN_sha224WithRSAEncryption,
62
     LN_sha224WithRSAEncryption},
63
    {NID_undef, EVP_sha256, SN_sha256WithRSAEncryption,
64
     LN_sha256WithRSAEncryption},
65
    {NID_undef, EVP_sha384, SN_sha384WithRSAEncryption,
66
     LN_sha384WithRSAEncryption},
67
    {NID_undef, EVP_sha512, SN_sha512WithRSAEncryption,
68
     LN_sha512WithRSAEncryption},
69
};
70
71
993
const EVP_MD *EVP_get_digestbynid(int nid) {
72
993
  if (nid == NID_undef) {
73
    // Skip the |NID_undef| entries in |nid_to_digest_mapping|.
74
0
    return nullptr;
75
0
  }
76
77
4.11k
  for (const auto &mapping : nid_to_digest_mapping) {
78
4.11k
    if (mapping.nid == nid) {
79
993
      return mapping.md_func();
80
993
    }
81
4.11k
  }
82
83
0
  return nullptr;
84
993
}
85
86
static const struct {
87
  uint8_t oid[9];
88
  uint8_t oid_len;
89
  int nid;
90
} kMDOIDs[] = {
91
    // 1.2.840.113549.2.4
92
    {{0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x04}, 8, NID_md4},
93
    // 1.2.840.113549.2.5
94
    {{0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x05}, 8, NID_md5},
95
    // 1.3.14.3.2.26
96
    {{0x2b, 0x0e, 0x03, 0x02, 0x1a}, 5, NID_sha1},
97
    // 2.16.840.1.101.3.4.2.1
98
    {{0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01}, 9, NID_sha256},
99
    // 2.16.840.1.101.3.4.2.2
100
    {{0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02}, 9, NID_sha384},
101
    // 2.16.840.1.101.3.4.2.3
102
    {{0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03}, 9, NID_sha512},
103
    // 2.16.840.1.101.3.4.2.4
104
    {{0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04}, 9, NID_sha224},
105
};
106
107
1.43k
static int cbs_to_digest_nid(const CBS *cbs) {
108
6.02k
  for (const auto &md : kMDOIDs) {
109
6.02k
    if (bssl::Span<const uint8_t>(*cbs) ==
110
6.02k
        bssl::Span(md.oid).first(md.oid_len)) {
111
1.22k
      return md.nid;
112
1.22k
    }
113
6.02k
  }
114
202
  return NID_undef;
115
1.43k
}
116
117
0
const EVP_MD *EVP_get_digestbyobj(const ASN1_OBJECT *obj) {
118
0
  int nid = obj->nid;
119
0
  if (nid == NID_undef) {
120
    // Handle objects with no saved NID. Note we don't use |OBJ_obj2nid| here to
121
    // avoid pulling in the OID table.
122
0
    CBS cbs;
123
0
    CBS_init(&cbs, OBJ_get0_data(obj), OBJ_length(obj));
124
0
    nid = cbs_to_digest_nid(&cbs);
125
0
  }
126
127
0
  return nid == NID_undef ? nullptr : EVP_get_digestbynid(nid);
128
0
}
129
130
1.66k
int EVP_parse_digest_algorithm_nid(CBS *cbs) {
131
1.66k
  CBS algorithm, oid;
132
1.66k
  if (!CBS_get_asn1(cbs, &algorithm, CBS_ASN1_SEQUENCE) ||
133
1.48k
      !CBS_get_asn1(&algorithm, &oid, CBS_ASN1_OBJECT)) {
134
229
    OPENSSL_PUT_ERROR(DIGEST, DIGEST_R_DECODE_ERROR);
135
229
    return NID_undef;
136
229
  }
137
138
1.43k
  int ret = cbs_to_digest_nid(&oid);
139
1.43k
  if (ret == NID_undef) {
140
202
    OPENSSL_PUT_ERROR(DIGEST, DIGEST_R_UNKNOWN_HASH);
141
202
    return NID_undef;
142
202
  }
143
144
  // The parameters, if present, must be NULL. Historically, whether the NULL
145
  // was included or omitted was not well-specified. When parsing an
146
  // AlgorithmIdentifier, we allow both. (Note this code is not used when
147
  // verifying RSASSA-PKCS1-v1_5 signatures.)
148
1.22k
  if (CBS_len(&algorithm) > 0) {
149
612
    CBS param;
150
612
    if (!CBS_get_asn1(&algorithm, &param, CBS_ASN1_NULL) ||
151
586
        CBS_len(&param) != 0 ||  //
152
530
        CBS_len(&algorithm) != 0) {
153
161
      OPENSSL_PUT_ERROR(DIGEST, DIGEST_R_DECODE_ERROR);
154
161
      return NID_undef;
155
161
    }
156
612
  }
157
158
1.06k
  return ret;
159
1.22k
}
160
161
1.54k
const EVP_MD *EVP_parse_digest_algorithm(CBS *cbs) {
162
1.54k
  int nid = EVP_parse_digest_algorithm_nid(cbs);
163
1.54k
  if (nid == NID_undef) {
164
551
    return nullptr;
165
551
  }
166
993
  return EVP_get_digestbynid(nid);
167
1.54k
}
168
169
static int marshal_digest_algorithm(CBB *cbb, const EVP_MD *md,
170
0
                                    bool with_null) {
171
0
  CBB algorithm, oid, null;
172
0
  if (!CBB_add_asn1(cbb, &algorithm, CBS_ASN1_SEQUENCE) ||
173
0
      !CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT)) {
174
0
    return 0;
175
0
  }
176
177
0
  bool found = false;
178
0
  int nid = EVP_MD_type(md);
179
0
  for (const auto &mdoid : kMDOIDs) {
180
0
    if (nid == mdoid.nid) {
181
0
      if (!CBB_add_bytes(&oid, mdoid.oid, mdoid.oid_len)) {
182
0
        return 0;
183
0
      }
184
0
      found = true;
185
0
      break;
186
0
    }
187
0
  }
188
189
0
  if (!found) {
190
0
    OPENSSL_PUT_ERROR(DIGEST, DIGEST_R_UNKNOWN_HASH);
191
0
    return 0;
192
0
  }
193
194
0
  if ((with_null && !CBB_add_asn1(&algorithm, &null, CBS_ASN1_NULL)) ||  //
195
0
      !CBB_flush(cbb)) {
196
0
    return 0;
197
0
  }
198
199
0
  return 1;
200
0
}
201
202
0
int EVP_marshal_digest_algorithm(CBB *cbb, const EVP_MD *md) {
203
0
  return marshal_digest_algorithm(cbb, md, /*with_null=*/true);
204
0
}
205
206
0
int EVP_marshal_digest_algorithm_no_params(CBB *cbb, const EVP_MD *md) {
207
0
  return marshal_digest_algorithm(cbb, md, /*with_null=*/false);
208
0
}
209
210
0
const EVP_MD *EVP_get_digestbyname(const char *name) {
211
0
  for (const auto &mapping : nid_to_digest_mapping) {
212
0
    const char *short_name = mapping.short_name;
213
0
    const char *long_name = mapping.long_name;
214
0
    if ((short_name && strcmp(short_name, name) == 0) ||
215
0
        (long_name && strcmp(long_name, name) == 0)) {
216
0
      return mapping.md_func();
217
0
    }
218
0
  }
219
220
0
  return nullptr;
221
0
}
222
223
EVP_MD *EVP_MD_fetch(OSSL_LIB_CTX *libctx, const char *name,
224
0
                     const char *propq) {
225
0
  EVP_MD *ret = const_cast<EVP_MD *>(EVP_get_digestbyname(name));
226
0
  if (ret == nullptr) {
227
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
228
0
  }
229
0
  return ret;
230
0
}
231
232
0
int EVP_MD_up_ref(EVP_MD *md) { return 1; }
233
234
0
void EVP_MD_free(EVP_MD *md) {}
235
236
int EVP_Q_digest(OSSL_LIB_CTX *libctx, const char *name, const char *propq,
237
0
                 const void *in, size_t in_len, uint8_t *out, size_t *out_len) {
238
0
  const EVP_MD *md = EVP_MD_fetch(libctx, name, propq);
239
0
  if (md == nullptr) {
240
0
    return 0;
241
0
  }
242
0
  unsigned len_u;
243
0
  if (!EVP_Digest(in, in_len, out, &len_u, md, nullptr)) {
244
0
    return 0;
245
0
  }
246
0
  *out_len = len_u;
247
0
  return 1;
248
0
}
249
250
0
static void blake2b256_init(EVP_MD_CTX *ctx) {
251
0
  BLAKE2B256_Init(reinterpret_cast<BLAKE2B_CTX *>(ctx->md_data));
252
0
}
253
254
0
static void blake2b256_update(EVP_MD_CTX *ctx, const void *data, size_t len) {
255
0
  BLAKE2B256_Update(reinterpret_cast<BLAKE2B_CTX *>(ctx->md_data), data, len);
256
0
}
257
258
0
static void blake2b256_final(EVP_MD_CTX *ctx, uint8_t *md) {
259
0
  BLAKE2B256_Final(md, reinterpret_cast<BLAKE2B_CTX *>(ctx->md_data));
260
0
}
261
262
static const EVP_MD evp_md_blake2b256 = {
263
    NID_undef,       BLAKE2B256_DIGEST_LENGTH, 0,
264
    blake2b256_init, blake2b256_update,        blake2b256_final,
265
    BLAKE2B_CBLOCK,  sizeof(BLAKE2B_CTX),
266
};
267
268
0
const EVP_MD *EVP_blake2b256() { return &evp_md_blake2b256; }
269
270
static_assert(sizeof(BLAKE2B_CTX) <= EVP_MAX_MD_DATA_SIZE);
271
272
273
26.6k
static void md4_init(EVP_MD_CTX *ctx) {
274
26.6k
  BSSL_CHECK(MD4_Init(reinterpret_cast<MD4_CTX *>(ctx->md_data)));
275
26.6k
}
276
277
26.8k
static void md4_update(EVP_MD_CTX *ctx, const void *data, size_t count) {
278
26.8k
  BSSL_CHECK(
279
26.8k
      MD4_Update(reinterpret_cast<MD4_CTX *>(ctx->md_data), data, count));
280
26.8k
}
281
282
26.6k
static void md4_final(EVP_MD_CTX *ctx, uint8_t *out) {
283
26.6k
  BSSL_CHECK(MD4_Final(out, reinterpret_cast<MD4_CTX *>(ctx->md_data)));
284
26.6k
}
285
286
static const EVP_MD evp_md_md4 = {
287
    NID_md4,            //
288
    MD4_DIGEST_LENGTH,  //
289
    0,
290
    md4_init,
291
    md4_update,
292
    md4_final,
293
    64,
294
    sizeof(MD4_CTX),
295
};
296
297
121
const EVP_MD *EVP_md4() { return &evp_md_md4; }
298
299
static_assert(sizeof(MD4_CTX) <= EVP_MAX_MD_DATA_SIZE);
300
301
302
46.6k
static void md5_init(EVP_MD_CTX *ctx) {
303
46.6k
  BSSL_CHECK(MD5_Init(reinterpret_cast<MD5_CTX *>(ctx->md_data)));
304
46.6k
}
305
306
269k
static void md5_update(EVP_MD_CTX *ctx, const void *data, size_t count) {
307
269k
  BSSL_CHECK(
308
269k
      MD5_Update(reinterpret_cast<MD5_CTX *>(ctx->md_data), data, count));
309
269k
}
310
311
157k
static void md5_final(EVP_MD_CTX *ctx, uint8_t *out) {
312
157k
  BSSL_CHECK(MD5_Final(out, reinterpret_cast<MD5_CTX *>(ctx->md_data)));
313
157k
}
314
315
static const EVP_MD evp_md_md5 = {
316
    NID_md5,    MD5_DIGEST_LENGTH, 0,  md5_init,
317
    md5_update, md5_final,         64, sizeof(MD5_CTX),
318
};
319
320
9.42k
const EVP_MD *EVP_md5() { return &evp_md_md5; }
321
322
static_assert(sizeof(MD5_CTX) <= EVP_MAX_MD_DATA_SIZE);
323
324
325
typedef struct {
326
  MD5_CTX md5;
327
  SHA_CTX sha1;
328
} MD5_SHA1_CTX;
329
330
9.52k
static void md5_sha1_init(EVP_MD_CTX *md_ctx) {
331
9.52k
  MD5_SHA1_CTX *ctx = reinterpret_cast<MD5_SHA1_CTX *>(md_ctx->md_data);
332
9.52k
  BSSL_CHECK(MD5_Init(&ctx->md5) && SHA1_Init(&ctx->sha1));
333
9.52k
}
334
335
static void md5_sha1_update(EVP_MD_CTX *md_ctx, const void *data,
336
43.2k
                            size_t count) {
337
43.2k
  MD5_SHA1_CTX *ctx = reinterpret_cast<MD5_SHA1_CTX *>(md_ctx->md_data);
338
43.2k
  BSSL_CHECK(MD5_Update(&ctx->md5, data, count) &&
339
43.2k
             SHA1_Update(&ctx->sha1, data, count));
340
43.2k
}
341
342
12.3k
static void md5_sha1_final(EVP_MD_CTX *md_ctx, uint8_t *out) {
343
12.3k
  MD5_SHA1_CTX *ctx = reinterpret_cast<MD5_SHA1_CTX *>(md_ctx->md_data);
344
12.3k
  BSSL_CHECK(MD5_Final(out, &ctx->md5) &&
345
12.3k
             SHA1_Final(out + MD5_DIGEST_LENGTH, &ctx->sha1));
346
12.3k
}
347
348
const EVP_MD evp_md_md5_sha1 = {
349
    NID_md5_sha1,
350
    MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
351
    0,
352
    md5_sha1_init,
353
    md5_sha1_update,
354
    md5_sha1_final,
355
    64,
356
    sizeof(MD5_SHA1_CTX),
357
};
358
359
163k
const EVP_MD *EVP_md5_sha1() { return &evp_md_md5_sha1; }
360
361
static_assert(sizeof(MD5_SHA1_CTX) <= EVP_MAX_MD_DATA_SIZE);