summaryrefslogtreecommitdiffstats
path: root/lib/algorithms/secparams.c
blob: efd1f4753026206811e53b48ae20a5d68661211c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
 * Copyright (C) 2011-2012 Free Software Foundation, Inc.
 *
 * Author: Nikos Mavrogiannopoulos
 *
 * This file is part of GnuTLS.
 *
 * The GnuTLS is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
 *
 */

#include "gnutls_int.h"
#include <algorithms.h>
#include "errors.h"
#include <x509/common.h>

typedef struct {
	const char *name;
	gnutls_sec_param_t sec_param;
	unsigned int bits;	/* security level */
	unsigned int pk_bits;	/* DH, RSA, SRP */
	unsigned int dsa_bits;	/* bits for DSA. Handled differently since
				 * choice of key size in DSA is political.
				 */
	unsigned int subgroup_bits;	/* subgroup bits */
	unsigned int ecc_bits;	/* bits for ECC keys */
} gnutls_sec_params_entry;

static const gnutls_sec_params_entry sec_params[] = {
	{"Insecure", GNUTLS_SEC_PARAM_INSECURE, 0, 0, 0, 0, 0},
	{"Export", GNUTLS_SEC_PARAM_EXPORT, 42, 512, 0, 84, 0},
	{"Very weak", GNUTLS_SEC_PARAM_VERY_WEAK, 64, 767, 0, 128, 0},
	{"Weak", GNUTLS_SEC_PARAM_WEAK, 72, 1008, 1008, 160, 160},
#ifdef ENABLE_FIPS140
	{"Low", GNUTLS_SEC_PARAM_LOW, 80, 1024, 1024, 160, 160},
	{"Legacy", GNUTLS_SEC_PARAM_LEGACY, 96, 1024, 1024, 192, 192},
	{"Medium", GNUTLS_SEC_PARAM_MEDIUM, 112, 2048, 2048, 224, 224},
	{"High", GNUTLS_SEC_PARAM_HIGH, 128, 3072, 3072, 256, 256},
#else
	{"Low", GNUTLS_SEC_PARAM_LOW, 80, 1024, 1024, 160, 160}, /* ENISA-LEGACY */
	{"Legacy", GNUTLS_SEC_PARAM_LEGACY, 96, 1776, 2048, 192, 192},
	{"Medium", GNUTLS_SEC_PARAM_MEDIUM, 112, 2048, 2048, 256, 224},
	{"High", GNUTLS_SEC_PARAM_HIGH, 128, 3072, 3072, 256, 256},
#endif
	{"Ultra", GNUTLS_SEC_PARAM_ULTRA, 192, 8192, 8192, 384, 384},
	{"Future", GNUTLS_SEC_PARAM_FUTURE, 256, 15360, 15360, 512, 512},
	{NULL, 0, 0, 0, 0, 0}
};

#define GNUTLS_SEC_PARAM_LOOP(b) \
	{ const gnutls_sec_params_entry *p; \
		for(p = sec_params; p->name != NULL; p++) { b ; } }

/**
 * gnutls_sec_param_to_pk_bits:
 * @algo: is a public key algorithm
 * @param: is a security parameter
 *
 * When generating private and public key pairs a difficult question
 * is which size of "bits" the modulus will be in RSA and the group size
 * in DSA. The easy answer is 1024, which is also wrong. This function
 * will convert a human understandable security parameter to an
 * appropriate size for the specific algorithm.
 *
 * Returns: The number of bits, or (0).
 *
 * Since: 2.12.0
 **/
unsigned int
gnutls_sec_param_to_pk_bits(gnutls_pk_algorithm_t algo,
			    gnutls_sec_param_t param)
{
	unsigned int ret = 0;

	/* handle DSA differently */
	GNUTLS_SEC_PARAM_LOOP(
	if (p->sec_param == param) {
		if (algo == GNUTLS_PK_DSA)
			ret = p->dsa_bits;
		else if (IS_EC(algo)||IS_GOSTEC(algo))
			ret = p->ecc_bits;
		else
			ret = p->pk_bits;
		break;
	}
	);
	return ret;
}

/**
 * gnutls_sec_param_to_symmetric_bits:
 * @algo: is a public key algorithm
 * @param: is a security parameter
 *
 * This function will return the number of bits that correspond to
 * symmetric cipher strength for the given security parameter.
 *
 * Returns: The number of bits, or (0).
 *
 * Since: 3.3.0
 **/
unsigned int
gnutls_sec_param_to_symmetric_bits(gnutls_sec_param_t param)
{
	unsigned int ret = 0;

	/* handle DSA differently */
	GNUTLS_SEC_PARAM_LOOP(
	if (p->sec_param == param) {
		ret = p->bits; break;
	}
	);
	return ret;
}

/* Returns the corresponding size for subgroup bits (q),
 * given the group bits (p).
 */
unsigned int _gnutls_pk_bits_to_subgroup_bits(unsigned int pk_bits)
{
	unsigned int ret = 0;

	GNUTLS_SEC_PARAM_LOOP(
		ret = p->subgroup_bits;
		if (p->pk_bits >= pk_bits)
			break;
	);
	return ret;
}

/* Returns a corresponding SHA algorithm size for the
 * public key bits given. It is based on the NIST mappings.
 */
gnutls_digest_algorithm_t _gnutls_pk_bits_to_sha_hash(unsigned int pk_bits)
{
	GNUTLS_SEC_PARAM_LOOP(
		if (p->pk_bits >= pk_bits) {
			if (p->bits <= 128)
				return GNUTLS_DIG_SHA256;
			else if (p->bits <= 192)
				return GNUTLS_DIG_SHA384;
			else
				return GNUTLS_DIG_SHA512;
		}
	);
	return GNUTLS_DIG_SHA256;
}

/**
 * gnutls_sec_param_get_name:
 * @param: is a security parameter
 *
 * Convert a #gnutls_sec_param_t value to a string.
 *
 * Returns: a pointer to a string that contains the name of the
 *   specified security level, or %NULL.
 *
 * Since: 2.12.0
 **/
const char *gnutls_sec_param_get_name(gnutls_sec_param_t param)
{
	const char *ret = "Unknown";

	GNUTLS_SEC_PARAM_LOOP(
		if (p->sec_param == param) {
			ret = p->name;
			break;
		}
	);

	return ret;
}

/**
 * gnutls_pk_bits_to_sec_param:
 * @algo: is a public key algorithm
 * @bits: is the number of bits
 *
 * This is the inverse of gnutls_sec_param_to_pk_bits(). Given an algorithm
 * and the number of bits, it will return the security parameter. This is
 * a rough indication.
 *
 * Returns: The security parameter.
 *
 * Since: 2.12.0
 **/
gnutls_sec_param_t
gnutls_pk_bits_to_sec_param(gnutls_pk_algorithm_t algo, unsigned int bits)
{
	gnutls_sec_param_t ret = GNUTLS_SEC_PARAM_INSECURE;

	if (bits == 0)
		return GNUTLS_SEC_PARAM_UNKNOWN;

	if (IS_EC(algo)||IS_GOSTEC(algo)) {
		GNUTLS_SEC_PARAM_LOOP(
			if (p->ecc_bits > bits) {
				break;
			}
			ret = p->sec_param;
		);
	} else {
		GNUTLS_SEC_PARAM_LOOP(
			if (p->pk_bits > bits) {
			      break;
			}
			ret = p->sec_param;
		);
	}

	return ret;
}