iv_gen: Provide external sequence number (IKE, ESP)
[strongswan.git] / src / libstrongswan / crypto / iv / iv_gen_rand.c
1 /*
2 * Copyright (C) 2013 Tobias Brunner
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 "iv_gen_rand.h"
17
18 #include <library.h>
19
20 typedef struct private_iv_gen_t private_iv_gen_t;
21
22 /**
23 * Private data of an iv_gen_t object.
24 */
25 struct private_iv_gen_t {
26
27 /**
28 * Public iv_gen_t interface.
29 */
30 iv_gen_t public;
31
32 /**
33 * rng_t object
34 */
35 rng_t *rng;
36 };
37
38 METHOD(iv_gen_t, get_iv, bool,
39 private_iv_gen_t *this, u_int64_t seq, size_t size, u_int8_t *buffer)
40 {
41 if (!this->rng)
42 {
43 return FALSE;
44 }
45 return this->rng->get_bytes(this->rng, size, buffer);
46 }
47
48 METHOD(iv_gen_t, allocate_iv, bool,
49 private_iv_gen_t *this, u_int64_t seq, size_t size, chunk_t *chunk)
50 {
51 if (!this->rng)
52 {
53 return FALSE;
54 }
55 return this->rng->allocate_bytes(this->rng, size, chunk);
56 }
57
58 METHOD(iv_gen_t, destroy, void,
59 private_iv_gen_t *this)
60 {
61 DESTROY_IF(this->rng);
62 free(this);
63 }
64
65 iv_gen_t *iv_gen_rand_create()
66 {
67 private_iv_gen_t *this;
68
69 INIT(this,
70 .public = {
71 .get_iv = _get_iv,
72 .allocate_iv = _allocate_iv,
73 .destroy = _destroy,
74 },
75 .rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK),
76 );
77
78 return &this->public;
79 }