xref: /netbsd-src/crypto/external/bsd/openssl/dist/providers/common/digest_to_nid.c (revision b0d1725196a7921d003d2c66a14f186abda4176b)
1*b0d17251Schristos /*
2*b0d17251Schristos  * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
3*b0d17251Schristos  *
4*b0d17251Schristos  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*b0d17251Schristos  * this file except in compliance with the License.  You can obtain a copy
6*b0d17251Schristos  * in the file LICENSE in the source distribution or at
7*b0d17251Schristos  * https://www.openssl.org/source/license.html
8*b0d17251Schristos  */
9*b0d17251Schristos 
10*b0d17251Schristos #include "internal/deprecated.h"
11*b0d17251Schristos 
12*b0d17251Schristos #include <openssl/objects.h>
13*b0d17251Schristos #include <openssl/core_names.h>
14*b0d17251Schristos #include <openssl/evp.h>
15*b0d17251Schristos #include <openssl/core.h>
16*b0d17251Schristos #include "prov/securitycheck.h"
17*b0d17251Schristos #include "internal/nelem.h"
18*b0d17251Schristos 
19*b0d17251Schristos /*
20*b0d17251Schristos  * Internal library code deals with NIDs, so we need to translate from a name.
21*b0d17251Schristos  * We do so using EVP_MD_is_a(), and therefore need a name to NID map.
22*b0d17251Schristos  */
ossl_digest_md_to_nid(const EVP_MD * md,const OSSL_ITEM * it,size_t it_len)23*b0d17251Schristos int ossl_digest_md_to_nid(const EVP_MD *md, const OSSL_ITEM *it, size_t it_len)
24*b0d17251Schristos {
25*b0d17251Schristos     size_t i;
26*b0d17251Schristos 
27*b0d17251Schristos     if (md == NULL)
28*b0d17251Schristos         return NID_undef;
29*b0d17251Schristos 
30*b0d17251Schristos     for (i = 0; i < it_len; i++)
31*b0d17251Schristos         if (EVP_MD_is_a(md, it[i].ptr))
32*b0d17251Schristos             return (int)it[i].id;
33*b0d17251Schristos     return NID_undef;
34*b0d17251Schristos }
35*b0d17251Schristos 
36*b0d17251Schristos /*
37*b0d17251Schristos  * Retrieve one of the FIPS approved hash algorithms by nid.
38*b0d17251Schristos  * See FIPS 180-4 "Secure Hash Standard" and FIPS 202 - SHA-3.
39*b0d17251Schristos  */
ossl_digest_get_approved_nid(const EVP_MD * md)40*b0d17251Schristos int ossl_digest_get_approved_nid(const EVP_MD *md)
41*b0d17251Schristos {
42*b0d17251Schristos     static const OSSL_ITEM name_to_nid[] = {
43*b0d17251Schristos         { NID_sha1,      OSSL_DIGEST_NAME_SHA1      },
44*b0d17251Schristos         { NID_sha224,    OSSL_DIGEST_NAME_SHA2_224  },
45*b0d17251Schristos         { NID_sha256,    OSSL_DIGEST_NAME_SHA2_256  },
46*b0d17251Schristos         { NID_sha384,    OSSL_DIGEST_NAME_SHA2_384  },
47*b0d17251Schristos         { NID_sha512,    OSSL_DIGEST_NAME_SHA2_512  },
48*b0d17251Schristos         { NID_sha512_224, OSSL_DIGEST_NAME_SHA2_512_224 },
49*b0d17251Schristos         { NID_sha512_256, OSSL_DIGEST_NAME_SHA2_512_256 },
50*b0d17251Schristos         { NID_sha3_224,  OSSL_DIGEST_NAME_SHA3_224  },
51*b0d17251Schristos         { NID_sha3_256,  OSSL_DIGEST_NAME_SHA3_256  },
52*b0d17251Schristos         { NID_sha3_384,  OSSL_DIGEST_NAME_SHA3_384  },
53*b0d17251Schristos         { NID_sha3_512,  OSSL_DIGEST_NAME_SHA3_512  },
54*b0d17251Schristos     };
55*b0d17251Schristos 
56*b0d17251Schristos     return ossl_digest_md_to_nid(md, name_to_nid, OSSL_NELEM(name_to_nid));
57*b0d17251Schristos }
58