1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2020 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 <openssl/core.h>
11e1051a39Sopenharmony_ci#include <openssl/core_names.h>
12e1051a39Sopenharmony_ci#include <openssl/evp.h>
13e1051a39Sopenharmony_ci#include <openssl/obj_mac.h>
14e1051a39Sopenharmony_ci#include "internal/nelem.h"
15e1051a39Sopenharmony_ci#include "crypto/rsa.h"
16e1051a39Sopenharmony_ci
17e1051a39Sopenharmony_cistatic int meth2nid(const void *meth,
18e1051a39Sopenharmony_ci                    int (*meth_is_a)(const void *meth, const char *name),
19e1051a39Sopenharmony_ci                    const OSSL_ITEM *items, size_t items_n)
20e1051a39Sopenharmony_ci{
21e1051a39Sopenharmony_ci    size_t i;
22e1051a39Sopenharmony_ci
23e1051a39Sopenharmony_ci    if (meth != NULL)
24e1051a39Sopenharmony_ci        for (i = 0; i < items_n; i++)
25e1051a39Sopenharmony_ci            if (meth_is_a(meth, items[i].ptr))
26e1051a39Sopenharmony_ci                return (int)items[i].id;
27e1051a39Sopenharmony_ci    return NID_undef;
28e1051a39Sopenharmony_ci}
29e1051a39Sopenharmony_ci
30e1051a39Sopenharmony_cistatic const char *nid2name(int meth, const OSSL_ITEM *items, size_t items_n)
31e1051a39Sopenharmony_ci{
32e1051a39Sopenharmony_ci    size_t i;
33e1051a39Sopenharmony_ci
34e1051a39Sopenharmony_ci    for (i = 0; i < items_n; i++)
35e1051a39Sopenharmony_ci        if (meth == (int)items[i].id)
36e1051a39Sopenharmony_ci            return items[i].ptr;
37e1051a39Sopenharmony_ci    return NULL;
38e1051a39Sopenharmony_ci}
39e1051a39Sopenharmony_ci
40e1051a39Sopenharmony_ci/*
41e1051a39Sopenharmony_ci * The list of permitted hash functions are taken from
42e1051a39Sopenharmony_ci * https://tools.ietf.org/html/rfc8017#appendix-A.2.1:
43e1051a39Sopenharmony_ci *
44e1051a39Sopenharmony_ci * OAEP-PSSDigestAlgorithms    ALGORITHM-IDENTIFIER ::= {
45e1051a39Sopenharmony_ci *     { OID id-sha1       PARAMETERS NULL }|
46e1051a39Sopenharmony_ci *     { OID id-sha224     PARAMETERS NULL }|
47e1051a39Sopenharmony_ci *     { OID id-sha256     PARAMETERS NULL }|
48e1051a39Sopenharmony_ci *     { OID id-sha384     PARAMETERS NULL }|
49e1051a39Sopenharmony_ci *     { OID id-sha512     PARAMETERS NULL }|
50e1051a39Sopenharmony_ci *     { OID id-sha512-224 PARAMETERS NULL }|
51e1051a39Sopenharmony_ci *     { OID id-sha512-256 PARAMETERS NULL },
52e1051a39Sopenharmony_ci *     ...  -- Allows for future expansion --
53e1051a39Sopenharmony_ci * }
54e1051a39Sopenharmony_ci */
55e1051a39Sopenharmony_cistatic const OSSL_ITEM oaeppss_name_nid_map[] = {
56e1051a39Sopenharmony_ci    { NID_sha1,         OSSL_DIGEST_NAME_SHA1         },
57e1051a39Sopenharmony_ci    { NID_sha224,       OSSL_DIGEST_NAME_SHA2_224     },
58e1051a39Sopenharmony_ci    { NID_sha256,       OSSL_DIGEST_NAME_SHA2_256     },
59e1051a39Sopenharmony_ci    { NID_sha384,       OSSL_DIGEST_NAME_SHA2_384     },
60e1051a39Sopenharmony_ci    { NID_sha512,       OSSL_DIGEST_NAME_SHA2_512     },
61e1051a39Sopenharmony_ci    { NID_sha512_224,   OSSL_DIGEST_NAME_SHA2_512_224 },
62e1051a39Sopenharmony_ci    { NID_sha512_256,   OSSL_DIGEST_NAME_SHA2_512_256 },
63e1051a39Sopenharmony_ci};
64e1051a39Sopenharmony_ci
65e1051a39Sopenharmony_cistatic int md_is_a(const void *md, const char *name)
66e1051a39Sopenharmony_ci{
67e1051a39Sopenharmony_ci    return EVP_MD_is_a(md, name);
68e1051a39Sopenharmony_ci}
69e1051a39Sopenharmony_ci
70e1051a39Sopenharmony_ciint ossl_rsa_oaeppss_md2nid(const EVP_MD *md)
71e1051a39Sopenharmony_ci{
72e1051a39Sopenharmony_ci    return meth2nid(md, md_is_a,
73e1051a39Sopenharmony_ci                    oaeppss_name_nid_map, OSSL_NELEM(oaeppss_name_nid_map));
74e1051a39Sopenharmony_ci}
75e1051a39Sopenharmony_ci
76e1051a39Sopenharmony_ciconst char *ossl_rsa_oaeppss_nid2name(int md)
77e1051a39Sopenharmony_ci{
78e1051a39Sopenharmony_ci    return nid2name(md, oaeppss_name_nid_map, OSSL_NELEM(oaeppss_name_nid_map));
79e1051a39Sopenharmony_ci}
80e1051a39Sopenharmony_ci
81e1051a39Sopenharmony_ciconst char *ossl_rsa_mgf_nid2name(int mgf)
82e1051a39Sopenharmony_ci{
83e1051a39Sopenharmony_ci    if (mgf == NID_mgf1)
84e1051a39Sopenharmony_ci        return SN_mgf1;
85e1051a39Sopenharmony_ci    return NULL;
86e1051a39Sopenharmony_ci}
87