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