Add a return value to prf_t.allocate_bytes()
[strongswan.git] / src / libstrongswan / crypto / prfs / mac_prf.c
1 /*
2 * Copyright (C) 2012 Tobias Brunner
3 * Copyright (C) 2005-2006 Martin Willi
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #include "mac_prf.h"
19
20 typedef struct private_prf_t private_prf_t;
21
22 /**
23 * Private data of a mac_prf_t object.
24 */
25 struct private_prf_t {
26
27 /**
28 * Public interface
29 */
30 prf_t public;
31
32 /**
33 * MAC to use
34 */
35 mac_t *mac;
36 };
37
38 METHOD(prf_t, get_bytes, bool,
39 private_prf_t *this, chunk_t seed, u_int8_t *buffer)
40 {
41 this->mac->get_mac(this->mac, seed, buffer);
42 return TRUE;
43 }
44
45 METHOD(prf_t, allocate_bytes, bool,
46 private_prf_t *this, chunk_t seed, chunk_t *chunk)
47 {
48 if (!chunk)
49 {
50 this->mac->get_mac(this->mac, seed, NULL);
51 }
52 else
53 {
54 *chunk = chunk_alloc(this->mac->get_mac_size(this->mac));
55 this->mac->get_mac(this->mac, seed, chunk->ptr);
56 }
57 return TRUE;
58 }
59
60 METHOD(prf_t, get_block_size, size_t,
61 private_prf_t *this)
62 {
63 return this->mac->get_mac_size(this->mac);
64 }
65
66 METHOD(prf_t, get_key_size, size_t,
67 private_prf_t *this)
68 {
69 /* IKEv2 uses MAC size as key size */
70 return this->mac->get_mac_size(this->mac);
71 }
72
73 METHOD(prf_t, set_key, void,
74 private_prf_t *this, chunk_t key)
75 {
76 this->mac->set_key(this->mac, key);
77 }
78
79 METHOD(prf_t, destroy, void,
80 private_prf_t *this)
81 {
82 this->mac->destroy(this->mac);
83 free(this);
84 }
85
86 /*
87 * Described in header.
88 */
89 prf_t *mac_prf_create(mac_t *mac)
90 {
91 private_prf_t *this;
92
93 INIT(this,
94 .public = {
95 .get_bytes = _get_bytes,
96 .allocate_bytes = _allocate_bytes,
97 .get_block_size = _get_block_size,
98 .get_key_size = _get_key_size,
99 .set_key = _set_key,
100 .destroy = _destroy,
101 },
102 .mac = mac,
103 );
104
105 return &this->public;
106 }