Check and forward syscall errors in AF_ALG
[strongswan.git] / src / libstrongswan / plugins / af_alg / af_alg_prf.c
1 /*
2 * Copyright (C) 2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
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 "af_alg_prf.h"
17 #include "af_alg_ops.h"
18
19 typedef struct private_af_alg_prf_t private_af_alg_prf_t;
20
21 /**
22 * Private data of a af_alg_prf_t object.
23 */
24 struct private_af_alg_prf_t {
25
26 /**
27 * Public af_alg_prf_t interface.
28 */
29 af_alg_prf_t public;
30
31 /**
32 * AF_ALG operations
33 */
34 af_alg_ops_t *ops;
35
36 /**
37 * Size of the PRF output
38 */
39 size_t block_size;
40
41 /**
42 * Default key size
43 */
44 size_t key_size;
45
46 /**
47 * Using an XCBC algorithm?
48 */
49 bool xcbc;
50 };
51
52 /**
53 * Algorithm database
54 */
55 static struct {
56 pseudo_random_function_t id;
57 char *name;
58 size_t block_size;
59 bool xcbc;
60 } algs[AF_ALG_PRF] = {
61 {PRF_HMAC_SHA1, "hmac(sha1)", 20, FALSE, },
62 {PRF_HMAC_SHA2_256, "hmac(sha256)", 32, FALSE, },
63 {PRF_HMAC_MD5, "hmac(md5)", 16, FALSE, },
64 {PRF_HMAC_SHA2_384, "hmac(sha384)", 48, FALSE, },
65 {PRF_HMAC_SHA2_512, "hmac(sha512)", 64, FALSE, },
66 {PRF_AES128_XCBC, "xcbc(aes)", 16, TRUE, },
67 {PRF_CAMELLIA128_XCBC, "xcbc(camellia)", 16, TRUE, },
68 };
69
70 /**
71 * See header.
72 */
73 void af_alg_prf_probe(plugin_feature_t *features, int *pos)
74 {
75 af_alg_ops_t *ops;
76 int i;
77
78 for (i = 0; i < countof(algs); i++)
79 {
80 ops = af_alg_ops_create("hash", algs[i].name);
81 if (ops)
82 {
83 ops->destroy(ops);
84 features[(*pos)++] = PLUGIN_PROVIDE(PRF, algs[i].id);
85 }
86 }
87 }
88
89 /**
90 * Get the kernel algorithm string and block size for our identifier
91 */
92 static size_t lookup_alg(pseudo_random_function_t algo, char **name, bool *xcbc)
93 {
94 int i;
95
96 for (i = 0; i < countof(algs); i++)
97 {
98 if (algs[i].id == algo)
99 {
100 *name = algs[i].name;
101 *xcbc = algs[i].xcbc;
102 return algs[i].block_size;
103 }
104 }
105 return 0;
106 }
107
108 METHOD(prf_t, get_bytes, bool,
109 private_af_alg_prf_t *this, chunk_t seed, u_int8_t *buffer)
110 {
111 return this->ops->hash(this->ops, seed, buffer, this->block_size);
112 }
113
114 METHOD(prf_t, allocate_bytes, bool,
115 private_af_alg_prf_t *this, chunk_t seed, chunk_t *chunk)
116 {
117 if (chunk)
118 {
119 *chunk = chunk_alloc(this->block_size);
120 return get_bytes(this, seed, chunk->ptr);
121 }
122 return get_bytes(this, seed, NULL);
123 }
124
125 METHOD(prf_t, get_block_size, size_t,
126 private_af_alg_prf_t *this)
127 {
128 return this->block_size;
129 }
130
131 METHOD(prf_t, get_key_size, size_t,
132 private_af_alg_prf_t *this)
133 {
134 return this->block_size;
135 }
136
137 METHOD(prf_t, set_key, bool,
138 private_af_alg_prf_t *this, chunk_t key)
139 {
140 char buf[this->block_size];
141
142 if (this->xcbc)
143 {
144 /* The kernel currently does not support variable length XCBC keys,
145 * do RFC4434 key padding/reduction manually. */
146 if (key.len < this->block_size)
147 {
148 memset(buf, 0, this->block_size);
149 memcpy(buf, key.ptr, key.len);
150 key = chunk_from_thing(buf);
151 }
152 else if (key.len > this->block_size)
153 {
154 memset(buf, 0, this->block_size);
155 if (!this->ops->set_key(this->ops, chunk_from_thing(buf)) ||
156 !this->ops->hash(this->ops, key, buf, this->block_size))
157 {
158 return FALSE;
159 }
160 key = chunk_from_thing(buf);
161 }
162 }
163 return this->ops->set_key(this->ops, key);
164 }
165
166 METHOD(prf_t, destroy, void,
167 private_af_alg_prf_t *this)
168 {
169 this->ops->destroy(this->ops);
170 free(this);
171 }
172
173 /*
174 * Described in header.
175 */
176 af_alg_prf_t *af_alg_prf_create(pseudo_random_function_t algo)
177 {
178 private_af_alg_prf_t *this;
179 size_t block_size;
180 bool xcbc;
181 char *name;
182
183 block_size = lookup_alg(algo, &name, &xcbc);
184 if (!block_size)
185 { /* not supported by kernel */
186 return NULL;
187 }
188
189 INIT(this,
190 .public = {
191 .prf = {
192 .get_bytes = _get_bytes,
193 .allocate_bytes = _allocate_bytes,
194 .get_block_size = _get_block_size,
195 .get_key_size = _get_key_size,
196 .set_key = _set_key,
197 .destroy = _destroy,
198 },
199 },
200 .ops = af_alg_ops_create("hash", name),
201 .block_size = block_size,
202 .xcbc = xcbc,
203 );
204 if (!this->ops)
205 {
206 free(this);
207 return NULL;
208 }
209 return &this->public;
210 }