]> git.ipfire.org Git - thirdparty/strongswan.git/blob - src/libstrongswan/plugins/nonce/nonce_nonceg.c
nonce: Allow overriding the RNG quality used to generate nonces
[thirdparty/strongswan.git] / src / libstrongswan / plugins / nonce / nonce_nonceg.c
1 /*
2 * Copyright (C) 2012 Adrian-Ken Rueegsegger
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "nonce_nonceg.h"
17
18 #include <utils/debug.h>
19
20 typedef struct private_nonce_nonceg_t private_nonce_nonceg_t;
21
22 /**
23 * Private data of a nonce_nonceg_t object.
24 */
25 struct private_nonce_nonceg_t {
26
27 /**
28 * Public nonce_nonceg_t interface.
29 */
30 nonce_nonceg_t public;
31
32 /**
33 * Random number generator
34 */
35 rng_t* rng;
36 };
37
38 METHOD(nonce_gen_t, get_nonce, bool,
39 private_nonce_nonceg_t *this, size_t size, uint8_t *buffer)
40 {
41 return this->rng->get_bytes(this->rng, size, buffer);
42 }
43
44 METHOD(nonce_gen_t, allocate_nonce, bool,
45 private_nonce_nonceg_t *this, size_t size, chunk_t *chunk)
46 {
47 return this->rng->allocate_bytes(this->rng, size, chunk);
48 }
49
50 METHOD(nonce_gen_t, destroy, void,
51 private_nonce_nonceg_t *this)
52 {
53 DESTROY_IF(this->rng);
54 free(this);
55 }
56
57 /*
58 * Described in header.
59 */
60 nonce_nonceg_t *nonce_nonceg_create()
61 {
62 private_nonce_nonceg_t *this;
63
64 INIT(this,
65 .public = {
66 .nonce_gen = {
67 .get_nonce = _get_nonce,
68 .allocate_nonce = _allocate_nonce,
69 .destroy = _destroy,
70 },
71 },
72 );
73
74 this->rng = lib->crypto->create_rng(lib->crypto, NONCE_RNG_QUALITY);
75 if (!this->rng)
76 {
77 DBG1(DBG_LIB, "no RNG found for quality %N", rng_quality_names,
78 RNG_WEAK);
79 destroy(this);
80 return NULL;
81 }
82
83 return &this->public;
84 }