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