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