Coverage Report

Created: 2025-06-24 07:00

/src/boringssl/crypto/digest/digest_extra.cc
Line
Count
Source (jump to first uncovered line)
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/md4.h>
22
#include <openssl/md5.h>
23
#include <openssl/nid.h>
24
#include <openssl/obj.h>
25
26
#include "../asn1/internal.h"
27
#include "../fipsmodule/digest/internal.h"
28
#include "../internal.h"
29
30
31
struct nid_to_digest {
32
  int nid;
33
  const EVP_MD *(*md_func)(void);
34
  const char *short_name;
35
  const char *long_name;
36
};
37
38
static const struct nid_to_digest nid_to_digest_mapping[] = {
39
    {NID_md4, EVP_md4, SN_md4, LN_md4},
40
    {NID_md5, EVP_md5, SN_md5, LN_md5},
41
    {NID_sha1, EVP_sha1, SN_sha1, LN_sha1},
42
    {NID_sha224, EVP_sha224, SN_sha224, LN_sha224},
43
    {NID_sha256, EVP_sha256, SN_sha256, LN_sha256},
44
    {NID_sha384, EVP_sha384, SN_sha384, LN_sha384},
45
    {NID_sha512, EVP_sha512, SN_sha512, LN_sha512},
46
    {NID_sha512_256, EVP_sha512_256, SN_sha512_256, LN_sha512_256},
47
    {NID_md5_sha1, EVP_md5_sha1, SN_md5_sha1, LN_md5_sha1},
48
    // As a remnant of signing |EVP_MD|s, OpenSSL returned the corresponding
49
    // hash function when given a signature OID. To avoid unintended lax parsing
50
    // of hash OIDs, this is no longer supported for lookup by OID or NID.
51
    // Node.js, however, exposes |EVP_get_digestbyname|'s full behavior to
52
    // consumers so we retain it there.
53
    {NID_undef, EVP_sha1, SN_dsaWithSHA, LN_dsaWithSHA},
54
    {NID_undef, EVP_sha1, SN_dsaWithSHA1, LN_dsaWithSHA1},
55
    {NID_undef, EVP_sha1, SN_ecdsa_with_SHA1, NULL},
56
    {NID_undef, EVP_md5, SN_md5WithRSAEncryption, LN_md5WithRSAEncryption},
57
    {NID_undef, EVP_sha1, SN_sha1WithRSAEncryption, LN_sha1WithRSAEncryption},
58
    {NID_undef, EVP_sha224, SN_sha224WithRSAEncryption,
59
     LN_sha224WithRSAEncryption},
60
    {NID_undef, EVP_sha256, SN_sha256WithRSAEncryption,
61
     LN_sha256WithRSAEncryption},
62
    {NID_undef, EVP_sha384, SN_sha384WithRSAEncryption,
63
     LN_sha384WithRSAEncryption},
64
    {NID_undef, EVP_sha512, SN_sha512WithRSAEncryption,
65
     LN_sha512WithRSAEncryption},
66
};
67
68
1.05k
const EVP_MD *EVP_get_digestbynid(int nid) {
69
1.05k
  if (nid == NID_undef) {
70
    // Skip the |NID_undef| entries in |nid_to_digest_mapping|.
71
0
    return NULL;
72
0
  }
73
74
3.76k
  for (unsigned i = 0; i < OPENSSL_ARRAY_SIZE(nid_to_digest_mapping); i++) {
75
3.76k
    if (nid_to_digest_mapping[i].nid == nid) {
76
1.05k
      return nid_to_digest_mapping[i].md_func();
77
1.05k
    }
78
3.76k
  }
79
80
0
  return NULL;
81
1.05k
}
82
83
static const struct {
84
  uint8_t oid[9];
85
  uint8_t oid_len;
86
  int nid;
87
} kMDOIDs[] = {
88
    // 1.2.840.113549.2.4
89
    {{0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x04}, 8, NID_md4},
90
    // 1.2.840.113549.2.5
91
    {{0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x05}, 8, NID_md5},
92
    // 1.3.14.3.2.26
93
    {{0x2b, 0x0e, 0x03, 0x02, 0x1a}, 5, NID_sha1},
94
    // 2.16.840.1.101.3.4.2.1
95
    {{0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01}, 9, NID_sha256},
96
    // 2.16.840.1.101.3.4.2.2
97
    {{0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02}, 9, NID_sha384},
98
    // 2.16.840.1.101.3.4.2.3
99
    {{0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03}, 9, NID_sha512},
100
    // 2.16.840.1.101.3.4.2.4
101
    {{0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04}, 9, NID_sha224},
102
};
103
104
1.30k
static const EVP_MD *cbs_to_md(const CBS *cbs) {
105
5.50k
  for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kMDOIDs); i++) {
106
5.26k
    if (CBS_len(cbs) == kMDOIDs[i].oid_len &&
107
5.26k
        OPENSSL_memcmp(CBS_data(cbs), kMDOIDs[i].oid, kMDOIDs[i].oid_len) ==
108
1.98k
            0) {
109
1.05k
      return EVP_get_digestbynid(kMDOIDs[i].nid);
110
1.05k
    }
111
5.26k
  }
112
113
247
  return NULL;
114
1.30k
}
115
116
0
const EVP_MD *EVP_get_digestbyobj(const ASN1_OBJECT *obj) {
117
  // Handle objects with no corresponding OID. Note we don't use |OBJ_obj2nid|
118
  // here to avoid pulling in the OID table.
119
0
  if (obj->nid != NID_undef) {
120
0
    return EVP_get_digestbynid(obj->nid);
121
0
  }
122
123
0
  CBS cbs;
124
0
  CBS_init(&cbs, OBJ_get0_data(obj), OBJ_length(obj));
125
0
  return cbs_to_md(&cbs);
126
0
}
127
128
1.49k
const EVP_MD *EVP_parse_digest_algorithm(CBS *cbs) {
129
1.49k
  CBS algorithm, oid;
130
1.49k
  if (!CBS_get_asn1(cbs, &algorithm, CBS_ASN1_SEQUENCE) ||
131
1.49k
      !CBS_get_asn1(&algorithm, &oid, CBS_ASN1_OBJECT)) {
132
191
    OPENSSL_PUT_ERROR(DIGEST, DIGEST_R_DECODE_ERROR);
133
191
    return NULL;
134
191
  }
135
136
1.30k
  const EVP_MD *ret = cbs_to_md(&oid);
137
1.30k
  if (ret == NULL) {
138
247
    OPENSSL_PUT_ERROR(DIGEST, DIGEST_R_UNKNOWN_HASH);
139
247
    return NULL;
140
247
  }
141
142
  // The parameters, if present, must be NULL. Historically, whether the NULL
143
  // was included or omitted was not well-specified. When parsing an
144
  // AlgorithmIdentifier, we allow both. (Note this code is not used when
145
  // verifying RSASSA-PKCS1-v1_5 signatures.)
146
1.05k
  if (CBS_len(&algorithm) > 0) {
147
378
    CBS param;
148
378
    if (!CBS_get_asn1(&algorithm, &param, CBS_ASN1_NULL) ||
149
378
        CBS_len(&param) != 0 ||  //
150
378
        CBS_len(&algorithm) != 0) {
151
158
      OPENSSL_PUT_ERROR(DIGEST, DIGEST_R_DECODE_ERROR);
152
158
      return NULL;
153
158
    }
154
378
  }
155
156
900
  return ret;
157
1.05k
}
158
159
static int marshal_digest_algorithm(CBB *cbb, const EVP_MD *md,
160
0
                                    bool with_null) {
161
0
  CBB algorithm, oid, null;
162
0
  if (!CBB_add_asn1(cbb, &algorithm, CBS_ASN1_SEQUENCE) ||
163
0
      !CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT)) {
164
0
    return 0;
165
0
  }
166
167
0
  bool found = false;
168
0
  int nid = EVP_MD_type(md);
169
0
  for (const auto &mdoid : kMDOIDs) {
170
0
    if (nid == mdoid.nid) {
171
0
      if (!CBB_add_bytes(&oid, mdoid.oid, mdoid.oid_len)) {
172
0
        return 0;
173
0
      }
174
0
      found = true;
175
0
      break;
176
0
    }
177
0
  }
178
179
0
  if (!found) {
180
0
    OPENSSL_PUT_ERROR(DIGEST, DIGEST_R_UNKNOWN_HASH);
181
0
    return 0;
182
0
  }
183
184
0
  if ((with_null && !CBB_add_asn1(&algorithm, &null, CBS_ASN1_NULL)) ||  //
185
0
      !CBB_flush(cbb)) {
186
0
    return 0;
187
0
  }
188
189
0
  return 1;
190
0
}
191
192
0
int EVP_marshal_digest_algorithm(CBB *cbb, const EVP_MD *md) {
193
0
  return marshal_digest_algorithm(cbb, md, /*with_null=*/true);
194
0
}
195
196
0
int EVP_marshal_digest_algorithm_no_params(CBB *cbb, const EVP_MD *md) {
197
0
  return marshal_digest_algorithm(cbb, md, /*with_null=*/false);
198
0
}
199
200
0
const EVP_MD *EVP_get_digestbyname(const char *name) {
201
0
  for (unsigned i = 0; i < OPENSSL_ARRAY_SIZE(nid_to_digest_mapping); i++) {
202
0
    const char *short_name = nid_to_digest_mapping[i].short_name;
203
0
    const char *long_name = nid_to_digest_mapping[i].long_name;
204
0
    if ((short_name && strcmp(short_name, name) == 0) ||
205
0
        (long_name && strcmp(long_name, name) == 0)) {
206
0
      return nid_to_digest_mapping[i].md_func();
207
0
    }
208
0
  }
209
210
0
  return NULL;
211
0
}
212
213
0
static void blake2b256_init(EVP_MD_CTX *ctx) {
214
0
  BLAKE2B256_Init(reinterpret_cast<BLAKE2B_CTX *>(ctx->md_data));
215
0
}
216
217
0
static void blake2b256_update(EVP_MD_CTX *ctx, const void *data, size_t len) {
218
0
  BLAKE2B256_Update(reinterpret_cast<BLAKE2B_CTX *>(ctx->md_data), data, len);
219
0
}
220
221
0
static void blake2b256_final(EVP_MD_CTX *ctx, uint8_t *md) {
222
0
  BLAKE2B256_Final(md, reinterpret_cast<BLAKE2B_CTX *>(ctx->md_data));
223
0
}
224
225
static const EVP_MD evp_md_blake2b256 = {
226
    NID_undef,       BLAKE2B256_DIGEST_LENGTH, 0,
227
    blake2b256_init, blake2b256_update,        blake2b256_final,
228
    BLAKE2B_CBLOCK,  sizeof(BLAKE2B_CTX),
229
};
230
231
0
const EVP_MD *EVP_blake2b256(void) { return &evp_md_blake2b256; }
232
233
static_assert(sizeof(BLAKE2B_CTX) <= EVP_MAX_MD_DATA_SIZE);
234
235
236
23.9k
static void md4_init(EVP_MD_CTX *ctx) {
237
23.9k
  BSSL_CHECK(MD4_Init(reinterpret_cast<MD4_CTX *>(ctx->md_data)));
238
23.9k
}
239
240
24.2k
static void md4_update(EVP_MD_CTX *ctx, const void *data, size_t count) {
241
24.2k
  BSSL_CHECK(
242
24.2k
      MD4_Update(reinterpret_cast<MD4_CTX *>(ctx->md_data), data, count));
243
24.2k
}
244
245
23.9k
static void md4_final(EVP_MD_CTX *ctx, uint8_t *out) {
246
23.9k
  BSSL_CHECK(MD4_Final(out, reinterpret_cast<MD4_CTX *>(ctx->md_data)));
247
23.9k
}
248
249
static const EVP_MD evp_md_md4 = {
250
    NID_md4,            //
251
    MD4_DIGEST_LENGTH,  //
252
    0,
253
    md4_init,
254
    md4_update,
255
    md4_final,
256
    64,
257
    sizeof(MD4_CTX),
258
};
259
260
165
const EVP_MD *EVP_md4(void) { return &evp_md_md4; }
261
262
static_assert(sizeof(MD4_CTX) <= EVP_MAX_MD_DATA_SIZE);
263
264
265
37.1k
static void md5_init(EVP_MD_CTX *ctx) {
266
37.1k
  BSSL_CHECK(MD5_Init(reinterpret_cast<MD5_CTX *>(ctx->md_data)));
267
37.1k
}
268
269
232k
static void md5_update(EVP_MD_CTX *ctx, const void *data, size_t count) {
270
232k
  BSSL_CHECK(
271
232k
      MD5_Update(reinterpret_cast<MD5_CTX *>(ctx->md_data), data, count));
272
232k
}
273
274
134k
static void md5_final(EVP_MD_CTX *ctx, uint8_t *out) {
275
134k
  BSSL_CHECK(MD5_Final(out, reinterpret_cast<MD5_CTX *>(ctx->md_data)));
276
134k
}
277
278
static const EVP_MD evp_md_md5 = {
279
    NID_md5,    MD5_DIGEST_LENGTH, 0,  md5_init,
280
    md5_update, md5_final,         64, sizeof(MD5_CTX),
281
};
282
283
8.41k
const EVP_MD *EVP_md5(void) { return &evp_md_md5; }
284
285
static_assert(sizeof(MD5_CTX) <= EVP_MAX_MD_DATA_SIZE);
286
287
288
typedef struct {
289
  MD5_CTX md5;
290
  SHA_CTX sha1;
291
} MD5_SHA1_CTX;
292
293
8.71k
static void md5_sha1_init(EVP_MD_CTX *md_ctx) {
294
8.71k
  MD5_SHA1_CTX *ctx = reinterpret_cast<MD5_SHA1_CTX *>(md_ctx->md_data);
295
8.71k
  BSSL_CHECK(MD5_Init(&ctx->md5) && SHA1_Init(&ctx->sha1));
296
8.71k
}
297
298
static void md5_sha1_update(EVP_MD_CTX *md_ctx, const void *data,
299
38.8k
                            size_t count) {
300
38.8k
  MD5_SHA1_CTX *ctx = reinterpret_cast<MD5_SHA1_CTX *>(md_ctx->md_data);
301
38.8k
  BSSL_CHECK(MD5_Update(&ctx->md5, data, count) &&
302
38.8k
             SHA1_Update(&ctx->sha1, data, count));
303
38.8k
}
304
305
10.8k
static void md5_sha1_final(EVP_MD_CTX *md_ctx, uint8_t *out) {
306
10.8k
  MD5_SHA1_CTX *ctx = reinterpret_cast<MD5_SHA1_CTX *>(md_ctx->md_data);
307
10.8k
  BSSL_CHECK(MD5_Final(out, &ctx->md5) &&
308
10.8k
             SHA1_Final(out + MD5_DIGEST_LENGTH, &ctx->sha1));
309
10.8k
}
310
311
const EVP_MD evp_md_md5_sha1 = {
312
    NID_md5_sha1,
313
    MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
314
    0,
315
    md5_sha1_init,
316
    md5_sha1_update,
317
    md5_sha1_final,
318
    64,
319
    sizeof(MD5_SHA1_CTX),
320
};
321
322
217k
const EVP_MD *EVP_md5_sha1(void) { return &evp_md_md5_sha1; }
323
324
static_assert(sizeof(MD5_SHA1_CTX) <= EVP_MAX_MD_DATA_SIZE);