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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
|
/*
* Copyright (C) 2008 Free Software Foundation
*
* Author: Nikos Mavrogiannopoulos
*
* This file is part of GNUTLS.
*
* The GNUTLS library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA
*
*/
#include <gnutls_errors.h>
#include <gnutls_int.h>
#include <gnutls/crypto.h>
#include <crypto.h>
#include <gnutls_mpi.h>
#include <gnutls_pk.h>
#include <random.h>
#include <gnutls_cipher_int.h>
typedef struct algo_list
{
int algorithm;
int priority;
void *alg_data;
struct algo_list *next;
} algo_list;
#define cipher_list algo_list
#define mac_list algo_list
#define digest_list algo_list
static int
_algo_register (algo_list * al, int algorithm, int priority, void *s)
{
algo_list *cl;
algo_list *last_cl = al;
/* look if there is any cipher with lowest priority. In that case do not add.
*/
cl = al;
while (cl && cl->alg_data)
{
if (cl->algorithm == algorithm)
{
if (cl->priority < priority)
{
gnutls_assert ();
return GNUTLS_E_CRYPTO_ALREADY_REGISTERED;
}
else
{
/* the current has higher priority -> overwrite */
cl->algorithm = algorithm;
cl->priority = priority;
cl->alg_data = s;
return 0;
}
}
cl = cl->next;
if (cl)
last_cl = cl;
}
cl = gnutls_calloc (1, sizeof (cipher_list));
if (cl == NULL)
{
gnutls_assert ();
return GNUTLS_E_MEMORY_ERROR;
}
last_cl->algorithm = algorithm;
last_cl->priority = priority;
last_cl->alg_data = s;
last_cl->next = cl;
return 0;
}
static void *
_get_algo (algo_list * al, int algo)
{
cipher_list *cl;
/* look if there is any cipher with lowest priority. In that case do not add.
*/
cl = al;
while (cl && cl->alg_data)
{
if (cl->algorithm == algo)
{
return cl->alg_data;
}
cl = cl->next;
}
return NULL;
}
static cipher_list glob_cl = { GNUTLS_CIPHER_NULL, 0, NULL, NULL };
static mac_list glob_ml = { GNUTLS_MAC_NULL, 0, NULL, NULL };
static digest_list glob_dl = { GNUTLS_MAC_NULL, 0, NULL, NULL };
static void
_deregister (algo_list * cl)
{
algo_list *next;
next = cl->next;
cl->next = NULL;
cl = next;
while (cl)
{
next = cl->next;
gnutls_free (cl);
cl = next;
}
}
void
_gnutls_crypto_deregister (void)
{
_deregister (&glob_cl);
_deregister (&glob_ml);
_deregister (&glob_dl);
}
/**
* gnutls_crypto_single_cipher_register2 - register a cipher algorithm
* @algorithm: is the gnutls algorithm identifier
* @priority: is the priority of the algorithm
* @version: should be set to %GNUTLS_CRYPTO_API_VERSION
* @s: is a structure holding new cipher's data
*
* This function will register a cipher algorithm to be used by
* gnutls. Any algorithm registered will override the included
* algorithms and by convention kernel implemented algorithms have
* priority of 90. The algorithm with the lowest priority will be
* used by gnutls.
*
* This function should be called before gnutls_global_init().
*
* For simplicity you can use the convenience
* gnutls_crypto_single_cipher_register() macro.
*
* Returns: %GNUTLS_E_SUCCESS on success, otherwise an error.
*
* Since: 2.6.0
**/
int
gnutls_crypto_single_cipher_register2 (gnutls_cipher_algorithm_t algorithm,
int priority, int version,
const gnutls_crypto_single_cipher_st * s)
{
if (version != GNUTLS_CRYPTO_API_VERSION)
{
gnutls_assert ();
return GNUTLS_E_UNIMPLEMENTED_FEATURE;
}
return _algo_register (&glob_cl, algorithm, priority, s);
}
gnutls_crypto_single_cipher_st *
_gnutls_get_crypto_cipher (gnutls_cipher_algorithm_t algo)
{
return _get_algo (&glob_cl, algo);
}
/**
* gnutls_crypto_rnd_register2 - register a random generator
* @priority: is the priority of the generator
* @version: should be set to %GNUTLS_CRYPTO_API_VERSION
* @s: is a structure holding new generator's data
*
* This function will register a random generator to be used by
* gnutls. Any generator registered will override the included
* generator and by convention kernel implemented generators have
* priority of 90. The generator with the lowest priority will be
* used by gnutls.
*
* This function should be called before gnutls_global_init().
*
* For simplicity you can use the convenience
* gnutls_crypto_rnd_register() macro.
*
* Returns: %GNUTLS_E_SUCCESS on success, otherwise an error.
*
* Since: 2.6.0
**/
int
gnutls_crypto_rnd_register2 (int priority, int version,
const gnutls_crypto_rnd_st * s)
{
if (version != GNUTLS_CRYPTO_API_VERSION)
{
gnutls_assert ();
return GNUTLS_E_UNIMPLEMENTED_FEATURE;
}
if (crypto_rnd_prio > priority)
{
memcpy (&_gnutls_rnd_ops, s, sizeof (*s));
crypto_rnd_prio = priority;
return 0;
}
return GNUTLS_E_CRYPTO_ALREADY_REGISTERED;
}
/**
* gnutls_crypto_single_mac_register2 - register a MAC algorithm
* @algorithm: is the gnutls algorithm identifier
* @priority: is the priority of the algorithm
* @version: should be set to %GNUTLS_CRYPTO_API_VERSION
* @s: is a structure holding new algorithms's data
*
* This function will register a MAC algorithm to be used by gnutls.
* Any algorithm registered will override the included algorithms and
* by convention kernel implemented algorithms have priority of 90.
* The algorithm with the lowest priority will be used by gnutls.
*
* This function should be called before gnutls_global_init().
*
* For simplicity you can use the convenience
* gnutls_crypto_single_mac_register() macro.
*
* Returns: %GNUTLS_E_SUCCESS on success, otherwise an error.
*
* Since: 2.6.0
**/
int
gnutls_crypto_single_mac_register2 (gnutls_mac_algorithm_t algorithm,
int priority, int version,
const gnutls_crypto_single_mac_st * s)
{
if (version != GNUTLS_CRYPTO_API_VERSION)
{
gnutls_assert ();
return GNUTLS_E_UNIMPLEMENTED_FEATURE;
}
return _algo_register (&glob_ml, algorithm, priority, s);
}
gnutls_crypto_single_mac_st *
_gnutls_get_crypto_mac (gnutls_mac_algorithm_t algo)
{
return _get_algo (&glob_ml, algo);
}
/**
* gnutls_crypto_single_digest_register2 - register a digest algorithm
* @algorithm: is the gnutls algorithm identifier
* @priority: is the priority of the algorithm
* @version: should be set to %GNUTLS_CRYPTO_API_VERSION
* @s: is a structure holding new algorithms's data
*
* This function will register a digest (hash) algorithm to be used by
* gnutls. Any algorithm registered will override the included
* algorithms and by convention kernel implemented algorithms have
* priority of 90. The algorithm with the lowest priority will be
* used by gnutls.
*
* This function should be called before gnutls_global_init().
*
* For simplicity you can use the convenience
* gnutls_crypto_single_digest_register() macro.
*
* Returns: %GNUTLS_E_SUCCESS on success, otherwise an error.
*
* Since: 2.6.0
**/
int
gnutls_crypto_single_digest_register2 (gnutls_digest_algorithm_t algorithm,
int priority, int version,
const gnutls_crypto_single_digest_st * s)
{
if (version != GNUTLS_CRYPTO_API_VERSION)
{
gnutls_assert ();
return GNUTLS_E_UNIMPLEMENTED_FEATURE;
}
return _algo_register (&glob_dl, algorithm, priority, s);
}
gnutls_crypto_single_digest_st *
_gnutls_get_crypto_digest (gnutls_digest_algorithm_t algo)
{
return _get_algo (&glob_dl, algo);
}
/**
* gnutls_crypto_bigint_register2 - register a bigint interface
* @priority: is the priority of the interface
* @version: should be set to %GNUTLS_CRYPTO_API_VERSION
* @s: is a structure holding new interface's data
*
* This function will register an interface for gnutls to operate
* on big integers. Any interface registered will override
* the included interface. The interface with the lowest
* priority will be used by gnutls.
*
* Note that the bigint interface must interoperate with the public
* key interface. Thus if this interface is updated the
* gnutls_crypto_pk_register() should also be used.
*
* This function should be called before gnutls_global_init().
*
* For simplicity you can use the convenience gnutls_crypto_bigint_register()
* macro.
*
* Returns: %GNUTLS_E_SUCCESS on success, otherwise an error.
*
* Since: 2.6.0
**/
int
gnutls_crypto_bigint_register2 (int priority, int version,
const gnutls_crypto_bigint_st * s)
{
if (version != GNUTLS_CRYPTO_API_VERSION)
{
gnutls_assert ();
return GNUTLS_E_UNIMPLEMENTED_FEATURE;
}
if (crypto_bigint_prio > priority)
{
memcpy (&_gnutls_mpi_ops, s, sizeof (*s));
crypto_bigint_prio = priority;
return 0;
}
return GNUTLS_E_CRYPTO_ALREADY_REGISTERED;
}
/**
* gnutls_crypto_pk_register2 - register a public key interface
* @priority: is the priority of the interface
* @version: should be set to %GNUTLS_CRYPTO_API_VERSION
* @s: is a structure holding new interface's data
*
* This function will register an interface for gnutls to operate
* on public key operations. Any interface registered will override
* the included interface. The interface with the lowest
* priority will be used by gnutls.
*
* Note that the bigint interface must interoperate with the bigint
* interface. Thus if this interface is updated the
* gnutls_crypto_bigint_register() should also be used.
*
* This function should be called before gnutls_global_init().
*
* For simplicity you can use the convenience gnutls_crypto_pk_register()
* macro.
*
* Returns: %GNUTLS_E_SUCCESS on success, otherwise an error.
*
* Since: 2.6.0
**/
int
gnutls_crypto_pk_register2 (int priority, int version,
const gnutls_crypto_pk_st * s)
{
if (version != GNUTLS_CRYPTO_API_VERSION)
{
gnutls_assert ();
return GNUTLS_E_UNIMPLEMENTED_FEATURE;
}
if (crypto_pk_prio > priority)
{
memcpy (&_gnutls_pk_ops, s, sizeof (*s));
crypto_pk_prio = priority;
return 0;
}
return GNUTLS_E_CRYPTO_ALREADY_REGISTERED;
}
/**
* gnutls_crypto_cipher_register2 - register a cipher interface
* @priority: is the priority of the cipher interface
* @version: should be set to %GNUTLS_CRYPTO_API_VERSION
* @s: is a structure holding new interface's data
*
* This function will register a cipher interface to be used by
* gnutls. Any interface registered will override the included engine
* and by convention kernel implemented interfaces should have
* priority of 90. The interface with the lowest priority will be used
* by gnutls.
*
* This function should be called before gnutls_global_init().
*
* For simplicity you can use the convenience
* gnutls_crypto_cipher_register() macro.
*
* Returns: %GNUTLS_E_SUCCESS on success, otherwise an error.
*
* Since: 2.6.0
**/
int
gnutls_crypto_cipher_register2 (int priority, int version,
const gnutls_crypto_cipher_st * s)
{
if (version != GNUTLS_CRYPTO_API_VERSION)
{
gnutls_assert ();
return GNUTLS_E_UNIMPLEMENTED_FEATURE;
}
if (crypto_cipher_prio > priority)
{
memcpy (&_gnutls_cipher_ops, s, sizeof (*s));
crypto_cipher_prio = priority;
return 0;
}
return GNUTLS_E_CRYPTO_ALREADY_REGISTERED;
}
/**
* gnutls_crypto_mac_register2 - register a mac interface
* @priority: is the priority of the mac interface
* @version: should be set to %GNUTLS_CRYPTO_API_VERSION
* @s: is a structure holding new interface's data
*
* This function will register a mac interface to be used by
* gnutls. Any interface registered will override the included engine
* and by convention kernel implemented interfaces should have
* priority of 90. The interface with the lowest priority will be used
* by gnutls.
*
* This function should be called before gnutls_global_init().
*
* For simplicity you can use the convenience
* gnutls_crypto_digest_register() macro.
*
* Returns: %GNUTLS_E_SUCCESS on success, otherwise an error.
*
* Since: 2.6.0
**/
int
gnutls_crypto_mac_register2 (int priority, int version,
const gnutls_crypto_mac_st * s)
{
if (version != GNUTLS_CRYPTO_API_VERSION)
{
gnutls_assert ();
return GNUTLS_E_UNIMPLEMENTED_FEATURE;
}
if (crypto_mac_prio > priority)
{
memcpy (&_gnutls_mac_ops, s, sizeof (*s));
crypto_mac_prio = priority;
return 0;
}
return GNUTLS_E_CRYPTO_ALREADY_REGISTERED;
}
/**
* gnutls_crypto_digest_register2 - register a digest interface
* @priority: is the priority of the digest interface
* @version: should be set to %GNUTLS_CRYPTO_API_VERSION
* @s: is a structure holding new interface's data
*
* This function will register a digest interface to be used by
* gnutls. Any interface registered will override the included engine
* and by convention kernel implemented interfaces should have
* priority of 90. The interface with the lowest priority will be used
* by gnutls.
*
* This function should be called before gnutls_global_init().
*
* For simplicity you can use the convenience
* gnutls_crypto_digest_register() macro.
*
* Returns: %GNUTLS_E_SUCCESS on success, otherwise an error.
*
* Since: 2.6.0
**/
int
gnutls_crypto_digest_register2 (int priority, int version,
const gnutls_crypto_digest_st * s)
{
if (version != GNUTLS_CRYPTO_API_VERSION)
{
gnutls_assert ();
return GNUTLS_E_UNIMPLEMENTED_FEATURE;
}
if (crypto_digest_prio > priority)
{
memcpy (&_gnutls_digest_ops, s, sizeof (*s));
crypto_digest_prio = priority;
return 0;
}
return GNUTLS_E_CRYPTO_ALREADY_REGISTERED;
}
|