1 /*
2 * Copyright 2020-2024 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 <string.h> /* strcmp */
11 #include <openssl/dh.h>
12 #include "internal/nelem.h"
13 #include "crypto/dh.h"
14
15 typedef struct dh_name2id_st {
16 const char *name;
17 int id;
18 int type;
19 } DH_GENTYPE_NAME2ID;
20
21 /* Indicates that the paramgen_type can be used for either DH or DHX */
22 #define TYPE_ANY -1
23 #ifndef OPENSSL_NO_DH
24 # define TYPE_DH DH_FLAG_TYPE_DH
25 # define TYPE_DHX DH_FLAG_TYPE_DHX
26 #else
27 # define TYPE_DH 0
28 # define TYPE_DHX 0
29 #endif
30
31 static const DH_GENTYPE_NAME2ID dhtype2id[] = {
32 { "group", DH_PARAMGEN_TYPE_GROUP, TYPE_ANY },
33 { "generator", DH_PARAMGEN_TYPE_GENERATOR, TYPE_DH },
34 { "fips186_4", DH_PARAMGEN_TYPE_FIPS_186_4, TYPE_DHX },
35 { "fips186_2", DH_PARAMGEN_TYPE_FIPS_186_2, TYPE_DHX },
36 };
37
ossl_dh_gen_type_id2name(int id)38 const char *ossl_dh_gen_type_id2name(int id)
39 {
40 size_t i;
41
42 for (i = 0; i < OSSL_NELEM(dhtype2id); ++i) {
43 if (dhtype2id[i].id == id)
44 return dhtype2id[i].name;
45 }
46 return NULL;
47 }
48
49 #ifndef OPENSSL_NO_DH
ossl_dh_gen_type_name2id(const char * name,int type)50 int ossl_dh_gen_type_name2id(const char *name, int type)
51 {
52 size_t i;
53
54 for (i = 0; i < OSSL_NELEM(dhtype2id); ++i) {
55 if ((dhtype2id[i].type == TYPE_ANY
56 || type == dhtype2id[i].type)
57 && strcmp(dhtype2id[i].name, name) == 0)
58 return dhtype2id[i].id;
59 }
60 return -1;
61 }
62 #endif
63