Nonce: Let get_nonce, allocate_nonce return boolean
[strongswan.git] / src / libstrongswan / plugins / nonce / nonce_nonceg.c
1 /*
2 * Copyright (C) 2012 Adrian-Ken Rueegsegger
3 * 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 <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, u_int8_t *buffer)
40 {
41 this->rng->get_bytes(this->rng, size, buffer);
42 return TRUE;
43 }
44
45 METHOD(nonce_gen_t, allocate_nonce, bool,
46 private_nonce_nonceg_t *this, size_t size, chunk_t *chunk)
47 {
48 this->rng->allocate_bytes(this->rng, size, chunk);
49 return TRUE;
50 }
51
52 METHOD(nonce_gen_t, destroy, void,
53 private_nonce_nonceg_t *this)
54 {
55 DESTROY_IF(this->rng);
56 free(this);
57 }
58
59 /*
60 * Described in header.
61 */
62 nonce_nonceg_t *nonce_nonceg_create()
63 {
64 private_nonce_nonceg_t *this;
65
66 INIT(this,
67 .public = {
68 .nonce_gen = {
69 .get_nonce = _get_nonce,
70 .allocate_nonce = _allocate_nonce,
71 .destroy = _destroy,
72 },
73 },
74 );
75
76 this->rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
77 if (!this->rng)
78 {
79 DBG1(DBG_LIB, "no RNG found for quality %N", rng_quality_names,
80 RNG_WEAK);
81 destroy(this);
82 return NULL;
83 }
84
85 return &this->public;
86 }