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