1/*
2 * Copyright 2020-2021 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
15typedef 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
31static const DH_GENTYPE_NAME2ID dhtype2id[] =
32{
33    { "group", DH_PARAMGEN_TYPE_GROUP, TYPE_ANY },
34    { "generator", DH_PARAMGEN_TYPE_GENERATOR, TYPE_DH },
35    { "fips186_4", DH_PARAMGEN_TYPE_FIPS_186_4, TYPE_DHX },
36    { "fips186_2", DH_PARAMGEN_TYPE_FIPS_186_2, TYPE_DHX },
37};
38
39const char *ossl_dh_gen_type_id2name(int id)
40{
41    size_t i;
42
43    for (i = 0; i < OSSL_NELEM(dhtype2id); ++i) {
44        if (dhtype2id[i].id == id)
45            return dhtype2id[i].name;
46    }
47    return NULL;
48}
49
50#ifndef OPENSSL_NO_DH
51int ossl_dh_gen_type_name2id(const char *name, int type)
52{
53    size_t i;
54
55    for (i = 0; i < OSSL_NELEM(dhtype2id); ++i) {
56        if ((dhtype2id[i].type == TYPE_ANY
57             || type == dhtype2id[i].type)
58            && strcmp(dhtype2id[i].name, name) == 0)
59            return dhtype2id[i].id;
60    }
61    return -1;
62}
63#endif
64