digest_extra.c
Go to the documentation of this file.
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to. The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  * notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  * notice, this list of conditions and the following disclaimer in the
29  * documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  * must display the following acknowledgement:
32  * "This product includes cryptographic software written by
33  * Eric Young (eay@cryptsoft.com)"
34  * The word 'cryptographic' can be left out if the rouines from the library
35  * being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  * the apps directory (application code) you must include an acknowledgement:
38  * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed. i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.] */
56 
57 #include <openssl/digest.h>
58 
59 #include <string.h>
60 
61 #include <openssl/blake2.h>
62 #include <openssl/bytestring.h>
63 #include <openssl/obj.h>
64 #include <openssl/nid.h>
65 
66 #include "../asn1/internal.h"
67 #include "../internal.h"
68 #include "../fipsmodule/digest/internal.h"
69 
70 
71 struct nid_to_digest {
72  int nid;
73  const EVP_MD* (*md_func)(void);
74  const char *short_name;
75  const char *long_name;
76 };
77 
78 static const struct nid_to_digest nid_to_digest_mapping[] = {
88  // As a remnant of signing |EVP_MD|s, OpenSSL returned the corresponding
89  // hash function when given a signature OID. To avoid unintended lax parsing
90  // of hash OIDs, this is no longer supported for lookup by OID or NID.
91  // Node.js, however, exposes |EVP_get_digestbyname|'s full behavior to
92  // consumers so we retain it there.
106 };
107 
109  if (nid == NID_undef) {
110  // Skip the |NID_undef| entries in |nid_to_digest_mapping|.
111  return NULL;
112  }
113 
114  for (unsigned i = 0; i < OPENSSL_ARRAY_SIZE(nid_to_digest_mapping); i++) {
115  if (nid_to_digest_mapping[i].nid == nid) {
116  return nid_to_digest_mapping[i].md_func();
117  }
118  }
119 
120  return NULL;
121 }
122 
123 static const struct {
126  int nid;
127 } kMDOIDs[] = {
128  // 1.2.840.113549.2.4
129  { {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x04}, 8, NID_md4 },
130  // 1.2.840.113549.2.5
131  { {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x05}, 8, NID_md5 },
132  // 1.3.14.3.2.26
133  { {0x2b, 0x0e, 0x03, 0x02, 0x1a}, 5, NID_sha1 },
134  // 2.16.840.1.101.3.4.2.1
135  { {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01}, 9, NID_sha256 },
136  // 2.16.840.1.101.3.4.2.2
137  { {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02}, 9, NID_sha384 },
138  // 2.16.840.1.101.3.4.2.3
139  { {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03}, 9, NID_sha512 },
140  // 2.16.840.1.101.3.4.2.4
141  { {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04}, 9, NID_sha224 },
142 };
143 
144 static const EVP_MD *cbs_to_md(const CBS *cbs) {
145  for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kMDOIDs); i++) {
146  if (CBS_len(cbs) == kMDOIDs[i].oid_len &&
148  0) {
149  return EVP_get_digestbynid(kMDOIDs[i].nid);
150  }
151  }
152 
153  return NULL;
154 }
155 
157  // Handle objects with no corresponding OID. Note we don't use |OBJ_obj2nid|
158  // here to avoid pulling in the OID table.
159  if (obj->nid != NID_undef) {
160  return EVP_get_digestbynid(obj->nid);
161  }
162 
163  CBS cbs;
165  return cbs_to_md(&cbs);
166 }
167 
169  CBS algorithm, oid;
170  if (!CBS_get_asn1(cbs, &algorithm, CBS_ASN1_SEQUENCE) ||
171  !CBS_get_asn1(&algorithm, &oid, CBS_ASN1_OBJECT)) {
173  return NULL;
174  }
175 
176  const EVP_MD *ret = cbs_to_md(&oid);
177  if (ret == NULL) {
179  return NULL;
180  }
181 
182  // The parameters, if present, must be NULL. Historically, whether the NULL
183  // was included or omitted was not well-specified. When parsing an
184  // AlgorithmIdentifier, we allow both. (Note this code is not used when
185  // verifying RSASSA-PKCS1-v1_5 signatures.)
186  if (CBS_len(&algorithm) > 0) {
187  CBS param;
188  if (!CBS_get_asn1(&algorithm, &param, CBS_ASN1_NULL) ||
189  CBS_len(&param) != 0 ||
190  CBS_len(&algorithm) != 0) {
192  return NULL;
193  }
194  }
195 
196  return ret;
197 }
198 
200  CBB algorithm, oid, null;
201  if (!CBB_add_asn1(cbb, &algorithm, CBS_ASN1_SEQUENCE) ||
202  !CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT)) {
204  return 0;
205  }
206 
207  int found = 0;
208  int nid = EVP_MD_type(md);
209  for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kMDOIDs); i++) {
210  if (nid == kMDOIDs[i].nid) {
211  if (!CBB_add_bytes(&oid, kMDOIDs[i].oid, kMDOIDs[i].oid_len)) {
213  return 0;
214  }
215  found = 1;
216  break;
217  }
218  }
219 
220  if (!found) {
222  return 0;
223  }
224 
225  if (!CBB_add_asn1(&algorithm, &null, CBS_ASN1_NULL) ||
226  !CBB_flush(cbb)) {
228  return 0;
229  }
230 
231  return 1;
232 }
233 
234 const EVP_MD *EVP_get_digestbyname(const char *name) {
235  for (unsigned i = 0; i < OPENSSL_ARRAY_SIZE(nid_to_digest_mapping); i++) {
236  const char *short_name = nid_to_digest_mapping[i].short_name;
237  const char *long_name = nid_to_digest_mapping[i].long_name;
238  if ((short_name && strcmp(short_name, name) == 0) ||
239  (long_name && strcmp(long_name, name) == 0)) {
240  return nid_to_digest_mapping[i].md_func();
241  }
242  }
243 
244  return NULL;
245 }
246 
247 static void blake2b256_init(EVP_MD_CTX *ctx) { BLAKE2B256_Init(ctx->md_data); }
248 
249 static void blake2b256_update(EVP_MD_CTX *ctx, const void *data, size_t len) {
250  BLAKE2B256_Update(ctx->md_data, data, len);
251 }
252 
254  BLAKE2B256_Final(md, ctx->md_data);
255 }
256 
257 static const EVP_MD evp_md_blake2b256 = {
258  NID_undef,
260  0,
265  sizeof(BLAKE2B_CTX),
266 };
267 
268 const EVP_MD *EVP_blake2b256(void) { return &evp_md_blake2b256; }
nid_to_digest::long_name
const char * long_name
Definition: digest_extra.c:75
obj
OPENSSL_EXPORT const ASN1_OBJECT * obj
Definition: x509.h:1671
NID_md5_sha1
#define NID_md5_sha1
Definition: nid.h:603
NID_md4
#define NID_md4
Definition: nid.h:1254
EVP_MD_type
#define EVP_MD_type
Definition: boringssl_prefix_symbols.h:1580
CBB_flush
#define CBB_flush
Definition: boringssl_prefix_symbols.h:1045
EVP_md5_sha1
const OPENSSL_EXPORT EVP_MD * EVP_md5_sha1(void)
EVP_sha512
const OPENSSL_EXPORT EVP_MD * EVP_sha512(void)
LN_sha224WithRSAEncryption
#define LN_sha224WithRSAEncryption
Definition: nid.h:2987
cbs_st
Definition: bytestring.h:39
OPENSSL_memcmp
static int OPENSSL_memcmp(const void *s1, const void *s2, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:811
SN_sha1WithRSAEncryption
#define SN_sha1WithRSAEncryption
Definition: nid.h:375
ctx
Definition: benchmark-async.c:30
OBJ_get0_data
#define OBJ_get0_data
Definition: boringssl_prefix_symbols.h:1850
env_md_st
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/digest/internal.h:67
CBS_data
#define CBS_data
Definition: boringssl_prefix_symbols.h:1057
LN_sha224
#define LN_sha224
Definition: nid.h:3007
EVP_sha384
const OPENSSL_EXPORT EVP_MD * EVP_sha384(void)
OBJ_length
#define OBJ_length
Definition: boringssl_prefix_symbols.h:1851
OPENSSL_PUT_ERROR
#define OPENSSL_PUT_ERROR(library, reason)
Definition: err.h:423
BLAKE2B256_Init
#define BLAKE2B256_Init
Definition: boringssl_prefix_symbols.h:875
string.h
OPENSSL_ARRAY_SIZE
#define OPENSSL_ARRAY_SIZE(array)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:179
LN_md5_sha1
#define LN_md5_sha1
Definition: nid.h:602
SN_sha384
#define SN_sha384
Definition: nid.h:2996
EVP_marshal_digest_algorithm
int EVP_marshal_digest_algorithm(CBB *cbb, const EVP_MD *md)
Definition: digest_extra.c:199
LN_dsaWithSHA1
#define LN_dsaWithSHA1
Definition: nid.h:597
CBS_len
#define CBS_len
Definition: boringssl_prefix_symbols.h:1089
blake2.h
SN_md5WithRSAEncryption
#define SN_md5WithRSAEncryption
Definition: nid.h:122
blake2b256_update
static void blake2b256_update(EVP_MD_CTX *ctx, const void *data, size_t len)
Definition: digest_extra.c:249
setup.name
name
Definition: setup.py:542
CBS_get_asn1
#define CBS_get_asn1
Definition: boringssl_prefix_symbols.h:1061
BLAKE2B256_Update
#define BLAKE2B256_Update
Definition: boringssl_prefix_symbols.h:876
cbs
const CBS * cbs
Definition: third_party/boringssl-with-bazel/src/crypto/trust_token/internal.h:107
nid_to_digest_mapping
static const struct nid_to_digest nid_to_digest_mapping[]
Definition: digest_extra.c:78
CBS_init
#define CBS_init
Definition: boringssl_prefix_symbols.h:1085
SN_dsaWithSHA
#define SN_dsaWithSHA
Definition: nid.h:380
NID_sha384
#define NID_sha384
Definition: nid.h:2998
EVP_sha256
const OPENSSL_EXPORT EVP_MD * EVP_sha256(void)
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
SN_sha256
#define SN_sha256
Definition: nid.h:2991
blake2b256_init
static void blake2b256_init(EVP_MD_CTX *ctx)
Definition: digest_extra.c:247
env_md_ctx_st
Definition: digest.h:306
asn1_object_st
Definition: third_party/boringssl-with-bazel/src/crypto/asn1/internal.h:102
CBS_ASN1_NULL
#define CBS_ASN1_NULL
Definition: bytestring.h:210
bytestring.h
SN_sha224WithRSAEncryption
#define SN_sha224WithRSAEncryption
Definition: nid.h:2986
LN_sha384WithRSAEncryption
#define LN_sha384WithRSAEncryption
Definition: nid.h:2977
LN_sha1
#define LN_sha1
Definition: nid.h:371
asn1_object_st::nid
int nid
Definition: third_party/boringssl-with-bazel/src/crypto/asn1/internal.h:104
evp_md_blake2b256
static const EVP_MD evp_md_blake2b256
Definition: digest_extra.c:257
oid
uint8_t oid[9]
Definition: digest_extra.c:124
kMDOIDs
static const struct @347 kMDOIDs[]
DIGEST_R_UNKNOWN_HASH
#define DIGEST_R_UNKNOWN_HASH
Definition: digest.h:346
SN_sha224
#define SN_sha224
Definition: nid.h:3006
gen_stats_data.found
bool found
Definition: gen_stats_data.py:61
SN_sha512
#define SN_sha512
Definition: nid.h:3001
LN_md5WithRSAEncryption
#define LN_md5WithRSAEncryption
Definition: nid.h:123
NID_sha256
#define NID_sha256
Definition: nid.h:2993
NID_sha512_256
#define NID_sha512_256
Definition: nid.h:4251
LN_sha512_256
#define LN_sha512_256
Definition: nid.h:4250
cbs_to_md
static const EVP_MD * cbs_to_md(const CBS *cbs)
Definition: digest_extra.c:144
CBB_add_asn1
#define CBB_add_asn1
Definition: boringssl_prefix_symbols.h:1019
EVP_parse_digest_algorithm
const EVP_MD * EVP_parse_digest_algorithm(CBS *cbs)
Definition: digest_extra.c:168
nid_to_digest
Definition: digest_extra.c:71
SN_sha1
#define SN_sha1
Definition: nid.h:370
BLAKE2B256_DIGEST_LENGTH
#define BLAKE2B256_DIGEST_LENGTH
Definition: blake2.h:25
NID_undef
#define NID_undef
Definition: nid.h:85
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
LN_sha512WithRSAEncryption
#define LN_sha512WithRSAEncryption
Definition: nid.h:2982
NID_md5
#define NID_md5
Definition: nid.h:105
blake2b256_final
static void blake2b256_final(EVP_MD_CTX *ctx, uint8_t *md)
Definition: digest_extra.c:253
NID_sha1
#define NID_sha1
Definition: nid.h:372
EVP_get_digestbynid
const EVP_MD * EVP_get_digestbynid(int nid)
Definition: digest_extra.c:108
NID_sha512
#define NID_sha512
Definition: nid.h:3003
SN_dsaWithSHA1
#define SN_dsaWithSHA1
Definition: nid.h:596
SN_sha512_256
#define SN_sha512_256
Definition: nid.h:4249
NID_sha224
#define NID_sha224
Definition: nid.h:3008
CBB_add_bytes
#define CBB_add_bytes
Definition: boringssl_prefix_symbols.h:1025
benchmark.md
md
Definition: benchmark.py:86
nid.h
nid_to_digest::md_func
const EVP_MD *(* md_func)(void)
Definition: digest_extra.c:73
digest.h
SN_sha512WithRSAEncryption
#define SN_sha512WithRSAEncryption
Definition: nid.h:2981
LN_sha256WithRSAEncryption
#define LN_sha256WithRSAEncryption
Definition: nid.h:2972
LN_md5
#define LN_md5
Definition: nid.h:104
SN_sha256WithRSAEncryption
#define SN_sha256WithRSAEncryption
Definition: nid.h:2971
BLAKE2B_CBLOCK
#define BLAKE2B_CBLOCK
Definition: blake2.h:26
LN_sha512
#define LN_sha512
Definition: nid.h:3002
CBS_ASN1_OBJECT
#define CBS_ASN1_OBJECT
Definition: bytestring.h:211
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
EVP_get_digestbyname
const EVP_MD * EVP_get_digestbyname(const char *name)
Definition: digest_extra.c:234
EVP_sha1
const OPENSSL_EXPORT EVP_MD * EVP_sha1(void)
nid_to_digest::nid
int nid
Definition: digest_extra.c:72
BLAKE2B256_Final
#define BLAKE2B256_Final
Definition: boringssl_prefix_symbols.h:874
EVP_get_digestbyobj
const EVP_MD * EVP_get_digestbyobj(const ASN1_OBJECT *obj)
Definition: digest_extra.c:156
SN_md5_sha1
#define SN_md5_sha1
Definition: nid.h:601
BLAKE2B_CTX
struct blake2b_state_st BLAKE2B_CTX
Definition: base.h:383
LN_sha256
#define LN_sha256
Definition: nid.h:2992
SN_md4
#define SN_md4
Definition: nid.h:1252
obj.h
LN_md4
#define LN_md4
Definition: nid.h:1253
nid_to_digest::short_name
const char * short_name
Definition: digest_extra.c:74
SN_md5
#define SN_md5
Definition: nid.h:103
EVP_blake2b256
const EVP_MD * EVP_blake2b256(void)
Definition: digest_extra.c:268
EVP_md4
const OPENSSL_EXPORT EVP_MD * EVP_md4(void)
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
EVP_md5
const OPENSSL_EXPORT EVP_MD * EVP_md5(void)
oid_len
uint8_t oid_len
Definition: digest_extra.c:125
SN_sha384WithRSAEncryption
#define SN_sha384WithRSAEncryption
Definition: nid.h:2976
CBS_ASN1_SEQUENCE
#define CBS_ASN1_SEQUENCE
Definition: bytestring.h:214
DIGEST_R_DECODE_ERROR
#define DIGEST_R_DECODE_ERROR
Definition: digest.h:345
nid
int nid
Definition: digest_extra.c:126
EVP_sha512_256
const OPENSSL_EXPORT EVP_MD * EVP_sha512_256(void)
LN_sha1WithRSAEncryption
#define LN_sha1WithRSAEncryption
Definition: nid.h:376
SN_ecdsa_with_SHA1
#define SN_ecdsa_with_SHA1
Definition: nid.h:1917
EVP_sha224
const OPENSSL_EXPORT EVP_MD * EVP_sha224(void)
LN_sha384
#define LN_sha384
Definition: nid.h:2997
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
ERR_R_MALLOC_FAILURE
#define ERR_R_MALLOC_FAILURE
Definition: err.h:371
cbb_st
Definition: bytestring.h:375
LN_dsaWithSHA
#define LN_dsaWithSHA
Definition: nid.h:381


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:12