summaryrefslogtreecommitdiff
path: root/lib/algorithms/ciphers.c
blob: 1318c88cafddb8b2069c2f37dd592eac9f890138 (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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*
 * 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>
#include "c-strcase.h"


/* Note that all algorithms are in CBC or STREAM modes.
 * Do not add any algorithms in other modes (avoid modified algorithms).
 * View first: "The order of encryption and authentication for
 * protecting communications" by Hugo Krawczyk - CRYPTO 2001
 *
 * On update, make sure to update MAX_CIPHER_BLOCK_SIZE, MAX_CIPHER_IV_SIZE,
 * and MAX_CIPHER_KEY_SIZE as well.
 * If any ciphers are removed, remove them from the back-end but
 * keep them in that list to allow backwards compatibility with applications
 * that specify them (they will be a no-op).
 */
static const cipher_entry_st algorithms[] = {
	{ .name = "AES-256-CBC",
	  .id = GNUTLS_CIPHER_AES_256_CBC,
	  .blocksize = 16,
	  .keysize = 32,
	  .type = CIPHER_BLOCK,
	  .explicit_iv = 16,
	  .cipher_iv = 16},
	{ .name = "AES-192-CBC",
	  .id = GNUTLS_CIPHER_AES_192_CBC,
	  .blocksize = 16,
	  .keysize = 24,
	  .type = CIPHER_BLOCK,
	  .explicit_iv = 16,
	  .cipher_iv = 16},
	{ .name = "AES-128-CBC",
	  .id = GNUTLS_CIPHER_AES_128_CBC,
	  .blocksize = 16,
	  .keysize = 16,
	  .type = CIPHER_BLOCK,
	  .explicit_iv = 16,
	  .cipher_iv = 16},
	{ .name = "AES-128-GCM",
	  .id = GNUTLS_CIPHER_AES_128_GCM,
	  .blocksize = 16,
	  .keysize = 16,
	  .type = CIPHER_AEAD,
	  .implicit_iv = 4,
	  .explicit_iv = 8,
	  .cipher_iv = 12,
	  .tagsize = 16},
	{ .name = "AES-256-GCM",
	  .id = GNUTLS_CIPHER_AES_256_GCM,
	  .blocksize = 16,
	  .keysize = 32,
	  .type = CIPHER_AEAD,
	  .implicit_iv = 4,
	  .explicit_iv = 8,
	  .cipher_iv = 12,
	  .tagsize = 16},
	{ .name = "AES-128-CCM",
	  .id = GNUTLS_CIPHER_AES_128_CCM,
	  .blocksize = 16,
	  .keysize = 16,
	  .type = CIPHER_AEAD,
	  .implicit_iv = 4,
	  .explicit_iv = 8,
	  .cipher_iv = 12,
	  .flags = GNUTLS_CIPHER_FLAG_ONLY_AEAD,
	  .tagsize = 16},
	{ .name = "AES-256-CCM",
	  .id = GNUTLS_CIPHER_AES_256_CCM,
	  .blocksize = 16,
	  .keysize = 32,
	  .type = CIPHER_AEAD,
	  .implicit_iv = 4,
	  .explicit_iv = 8,
	  .cipher_iv = 12,
	  .flags = GNUTLS_CIPHER_FLAG_ONLY_AEAD,
	  .tagsize = 16},
	{ .name = "AES-128-CCM-8",
	  .id = GNUTLS_CIPHER_AES_128_CCM_8,
	  .blocksize = 16,
	  .keysize = 16,
	  .type = CIPHER_AEAD,
	  .implicit_iv = 4,
	  .explicit_iv = 8,
	  .cipher_iv = 12,
	  .flags = GNUTLS_CIPHER_FLAG_ONLY_AEAD,
	  .tagsize = 8},
	{ .name = "AES-256-CCM-8",
	  .id = GNUTLS_CIPHER_AES_256_CCM_8,
	  .blocksize = 16,
	  .keysize = 32,
	  .type = CIPHER_AEAD,
	  .implicit_iv = 4,
	  .explicit_iv = 8,
	  .cipher_iv = 12,
	  .flags = GNUTLS_CIPHER_FLAG_ONLY_AEAD,
	  .tagsize = 8},
	{ .name = "ARCFOUR-128",
	  .id = GNUTLS_CIPHER_ARCFOUR_128,
	  .blocksize = 1,
	  .keysize = 16,
	  .type = CIPHER_STREAM,
	 0, 0, 0, 0},
	{ .name = "ESTREAM-SALSA20-256",
	  .id = GNUTLS_CIPHER_ESTREAM_SALSA20_256,
	  .blocksize = 64,
	  .keysize = 32,
	 .type = CIPHER_STREAM, 0, 0, 8, 0},
	{ .name = "SALSA20-256",
	  .id = GNUTLS_CIPHER_SALSA20_256,
	  .blocksize = 64,
	  .keysize = 32,
	  .type = CIPHER_STREAM,
	  .explicit_iv = 0,
	  .cipher_iv = 8},
	{ .name = "CHACHA20-32",
	  .id = GNUTLS_CIPHER_CHACHA20_32,
	  .blocksize = 64,
	  .keysize = 32,
	  .type = CIPHER_STREAM,
	  .explicit_iv = 0,
	  /* IV includes counter */
	  .cipher_iv = 16},
	{ .name = "CHACHA20-64",
	  .id = GNUTLS_CIPHER_CHACHA20_64,
	  .blocksize = 64,
	  .keysize = 32,
	  .type = CIPHER_STREAM,
	  .explicit_iv = 0,
	  /* IV includes counter */
	  .cipher_iv = 16},
	{ .name = "CAMELLIA-256-CBC",
	  .id = GNUTLS_CIPHER_CAMELLIA_256_CBC,
	  .blocksize = 16,
	  .keysize = 32,
	  .type = CIPHER_BLOCK,
	  .explicit_iv = 16,
	  .cipher_iv = 16},
	{ .name = "CAMELLIA-192-CBC",
	  .id = GNUTLS_CIPHER_CAMELLIA_192_CBC,
	  .blocksize = 16,
	  .keysize = 24,
	  .type = CIPHER_BLOCK,
	  .explicit_iv = 16,
	  .cipher_iv = 16},
	{ .name = "CAMELLIA-128-CBC",
	  .id = GNUTLS_CIPHER_CAMELLIA_128_CBC,
	  .blocksize = 16,
	  .keysize = 16,
	  .type = CIPHER_BLOCK,
	  .explicit_iv = 16,
	  .cipher_iv = 16},
	{ .name = "CHACHA20-POLY1305",
	  .id = GNUTLS_CIPHER_CHACHA20_POLY1305,
	  .blocksize = 64,
	  .keysize = 32,
	  .type = CIPHER_AEAD,
	  .implicit_iv = 12,
	  .explicit_iv = 0,
	  /* in chacha20 we don't need a rekey after 2^24 messages */
	  .flags = GNUTLS_CIPHER_FLAG_XOR_NONCE | GNUTLS_CIPHER_FLAG_NO_REKEY,
	  .cipher_iv = 12,
	  .tagsize = 16
	},
	{ .name = "CAMELLIA-128-GCM",
	  .id = GNUTLS_CIPHER_CAMELLIA_128_GCM,
	  .blocksize = 16,
	  .keysize = 16,
	  .type = CIPHER_AEAD, 4, 8, 12, 16},
	{ .name = "CAMELLIA-256-GCM",
	  .id = GNUTLS_CIPHER_CAMELLIA_256_GCM,
	  .blocksize = 16,
	  .keysize = 32,
	  .type = CIPHER_AEAD,
	  .implicit_iv = 4,
	  .explicit_iv = 8,
	  .cipher_iv = 12,
	  .tagsize = 16},
	{ .name = "GOST28147-TC26Z-CFB",
	  .id = GNUTLS_CIPHER_GOST28147_TC26Z_CFB,
	  .blocksize = 8,
	  .keysize = 32,
	  .type = CIPHER_STREAM,
	  .implicit_iv = 8,
	  .cipher_iv = 8},
	{ .name = "GOST28147-CPA-CFB",
	  .id = GNUTLS_CIPHER_GOST28147_CPA_CFB,
	  .blocksize = 8,
	  .keysize = 32,
	  .type = CIPHER_STREAM,
	  .implicit_iv = 8,
	  .cipher_iv = 8},
	{ .name = "GOST28147-CPB-CFB",
	  .id = GNUTLS_CIPHER_GOST28147_CPB_CFB,
	  .blocksize = 8,
	  .keysize = 32,
	  .type = CIPHER_STREAM,
	  .implicit_iv = 8,
	  .cipher_iv = 8},
	{ .name = "GOST28147-CPC-CFB",
	  .id = GNUTLS_CIPHER_GOST28147_CPC_CFB,
	  .blocksize = 8,
	  .keysize = 32,
	  .type = CIPHER_STREAM,
	  .implicit_iv = 8,
	  .cipher_iv = 8},
	{ .name = "GOST28147-CPD-CFB",
	  .id = GNUTLS_CIPHER_GOST28147_CPD_CFB,
	  .blocksize = 8,
	  .keysize = 32,
	  .type = CIPHER_STREAM,
	  .implicit_iv = 8,
	  .cipher_iv = 8},

	{ .name = "AES-128-CFB8",
	  .id = GNUTLS_CIPHER_AES_128_CFB8,
	  .blocksize = 16,
	  .keysize = 16,
	  .type = CIPHER_BLOCK,
	  .explicit_iv = 16,
	  .cipher_iv = 16},
	{ .name = "AES-192-CFB8",
	  .id = GNUTLS_CIPHER_AES_192_CFB8,
	  .blocksize = 16,
	  .keysize = 24,
	  .type = CIPHER_BLOCK,
	  .explicit_iv = 16,
	  .cipher_iv = 16},
	{ .name = "AES-256-CFB8",
	  .id = GNUTLS_CIPHER_AES_256_CFB8,
	  .blocksize = 16,
	  .keysize = 32,
	  .type = CIPHER_BLOCK,
	  .explicit_iv = 16,
	  .cipher_iv = 16},
	{ .name = "AES-128-XTS",
	  .id = GNUTLS_CIPHER_AES_128_XTS,
	  .blocksize = 16,
	  .keysize = 32,
	  .type = CIPHER_BLOCK,
	  .explicit_iv = 16,
	  .cipher_iv = 16},
	{ .name = "AES-256-XTS",
	  .id = GNUTLS_CIPHER_AES_256_XTS,
	  .blocksize = 16,
	  .keysize = 64,
	  .type = CIPHER_BLOCK,
	  .explicit_iv = 16,
	  .cipher_iv = 16},
	{ .name = "GOST28147-TC26Z-CNT",
	  .id = GNUTLS_CIPHER_GOST28147_TC26Z_CNT,
	  .blocksize = 8,
	  .keysize = 32,
	  .type = CIPHER_STREAM,
	  .implicit_iv = 8,
	  .cipher_iv = 8},
	{ .name = "3DES-CBC",
	  .id = GNUTLS_CIPHER_3DES_CBC,
	  .blocksize = 8,
	  .keysize = 24,
	  .type = CIPHER_BLOCK,
	  .explicit_iv = 8,
	  .cipher_iv = 8},
	{ .name = "DES-CBC",
	  .id = GNUTLS_CIPHER_DES_CBC,
	  .blocksize = 8,
	  .keysize = 8,
	  .type = CIPHER_BLOCK,
	  .explicit_iv = 8,
	  .cipher_iv = 8},
	{ .name = "ARCFOUR-40",
	  .id = GNUTLS_CIPHER_ARCFOUR_40,
	  .blocksize = 1,
	  .keysize = 5,
	  .type = CIPHER_STREAM},
	{ .name = "RC2-40",
	  .id = GNUTLS_CIPHER_RC2_40_CBC,
	  .blocksize = 8,
	  .keysize = 5,
	  .type = CIPHER_BLOCK,
	  .explicit_iv = 8,
	  .cipher_iv = 8},
	{ .name = "NULL",
	  .id = GNUTLS_CIPHER_NULL,
	  .blocksize = 1,
	  .keysize = 0,
	  .type = CIPHER_STREAM
	},
	{0, 0, 0, 0, 0, 0, 0}
};

#define GNUTLS_CIPHER_LOOP(b) \
	const cipher_entry_st *p; \
		for(p = algorithms; p->name != NULL; p++) { b ; }

#define GNUTLS_ALG_LOOP(a) \
			GNUTLS_CIPHER_LOOP( if(p->id == algorithm) { a; break; } )

/* CIPHER functions */

const cipher_entry_st *_gnutls_cipher_to_entry(gnutls_cipher_algorithm_t c)
{
	GNUTLS_CIPHER_LOOP(if (c == p->id) return p);

	return NULL;
}

/* Returns cipher entry even for ciphers that are not supported,
 * but are listed (e.g., deprecated ciphers).
 */
const cipher_entry_st *cipher_name_to_entry(const char *name)
{
	GNUTLS_CIPHER_LOOP(
		if (c_strcasecmp(p->name, name) == 0) {
			return p;
		}
	);

	return NULL;
}

/**
 * gnutls_cipher_get_block_size:
 * @algorithm: is an encryption algorithm
 *
 * Returns: the block size of the encryption algorithm.
 *
 * Since: 2.10.0
 **/
unsigned gnutls_cipher_get_block_size(gnutls_cipher_algorithm_t algorithm)
{
	size_t ret = 0;
	GNUTLS_ALG_LOOP(ret = p->blocksize);
	return ret;

}

/**
 * gnutls_cipher_get_tag_size:
 * @algorithm: is an encryption algorithm
 *
 * This function returns the tag size of an authenticated encryption
 * algorithm. For non-AEAD algorithms, it returns zero.
 *
 * Returns: the tag size of the authenticated encryption algorithm.
 *
 * Since: 3.2.2
 **/
unsigned gnutls_cipher_get_tag_size(gnutls_cipher_algorithm_t algorithm)
{
	return _gnutls_cipher_get_tag_size(cipher_to_entry(algorithm));
}

/**
 * gnutls_cipher_get_iv_size:
 * @algorithm: is an encryption algorithm
 *
 * This function returns the size of the initialization vector (IV) for the
 * provided algorithm. For algorithms with variable size IV (e.g., AES-CCM),
 * the returned size will be the one used by TLS.
 *
 * Returns: block size for encryption algorithm.
 *
 * Since: 3.2.0
 **/
unsigned gnutls_cipher_get_iv_size(gnutls_cipher_algorithm_t algorithm)
{
	size_t ret = 0;
	GNUTLS_ALG_LOOP(ret = p->cipher_iv);
	return ret;
}

/**
 * gnutls_cipher_get_key_size:
 * @algorithm: is an encryption algorithm
 *
 * This function returns the key size of the provided algorithm.
 *
 * Returns: length (in bytes) of the given cipher's key size, or 0 if
 *   the given cipher is invalid.
 **/
size_t gnutls_cipher_get_key_size(gnutls_cipher_algorithm_t algorithm)
{				/* In bytes */
	size_t ret = 0;
	GNUTLS_ALG_LOOP(ret = p->keysize);
	return ret;

}

/**
 * gnutls_cipher_get_name:
 * @algorithm: is an encryption algorithm
 *
 * Convert a #gnutls_cipher_algorithm_t type to a string.
 *
 * Returns: a pointer to a string that contains the name of the
 *   specified cipher, or %NULL.
 **/
const char *gnutls_cipher_get_name(gnutls_cipher_algorithm_t algorithm)
{
	const char *ret = NULL;

	/* avoid prefix */
	GNUTLS_ALG_LOOP(ret = p->name);

	return ret;
}

/**
 * gnutls_cipher_get_id:
 * @name: is a cipher algorithm name
 *
 * The names are compared in a case insensitive way.
 *
 * Returns: return a #gnutls_cipher_algorithm_t value corresponding to
 *   the specified cipher, or %GNUTLS_CIPHER_UNKNOWN on error.
 **/
gnutls_cipher_algorithm_t gnutls_cipher_get_id(const char *name)
{
	gnutls_cipher_algorithm_t ret = GNUTLS_CIPHER_UNKNOWN;

	GNUTLS_CIPHER_LOOP(
		if (c_strcasecmp(p->name, name) == 0) {
			if (p->id == GNUTLS_CIPHER_NULL || _gnutls_cipher_exists(p->id))
				ret = p->id;
			break;
		}
	);

	return ret;
}

/**
 * gnutls_cipher_list:
 *
 * Get a list of supported cipher algorithms.  Note that not
 * necessarily all ciphers are supported as TLS cipher suites.  For
 * example, DES is not supported as a cipher suite, but is supported
 * for other purposes (e.g., PKCS#8 or similar).
 *
 * This function is not thread safe.
 *
 * Returns: a (0)-terminated list of #gnutls_cipher_algorithm_t
 *   integers indicating the available ciphers.
 *
 **/
const gnutls_cipher_algorithm_t *gnutls_cipher_list(void)
{
	static gnutls_cipher_algorithm_t supported_ciphers[MAX_ALGOS] =
	    { 0 };

	if (supported_ciphers[0] == 0) {
		int i = 0;

		GNUTLS_CIPHER_LOOP(
			if (p->id == GNUTLS_CIPHER_NULL || _gnutls_cipher_exists(p->id))
				supported_ciphers[i++] = p->id;
		);
		supported_ciphers[i++] = 0;
	}

	return supported_ciphers;
}