libipsec: Properly initialize variables when creating AEAD wrapper
[strongswan.git] / src / libipsec / esp_context.c
1 /*
2 * Copyright (C) 2012-2013 Tobias Brunner
3 * Copyright (C) 2012 Giuliano Grassi
4 * Copyright (C) 2012 Ralf Sager
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 <limits.h>
19 #include <stdint.h>
20
21 #include "esp_context.h"
22
23 #include <library.h>
24 #include <utils/debug.h>
25
26 /**
27 * Should be a multiple of 8
28 */
29 #define ESP_DEFAULT_WINDOW_SIZE 128
30
31 typedef struct private_esp_context_t private_esp_context_t;
32
33 /**
34 * Private additions to esp_context_t.
35 */
36 struct private_esp_context_t {
37
38 /**
39 * Public members
40 */
41 esp_context_t public;
42
43 /**
44 * AEAD wrapper or method to encrypt/decrypt/authenticate ESP packets
45 */
46 aead_t *aead;
47
48 /**
49 * The highest sequence number that was successfully verified
50 * and authenticated, or assigned in an outbound context
51 */
52 u_int32_t last_seqno;
53
54 /**
55 * The bit in the window of the highest authenticated sequence number
56 */
57 u_int seqno_index;
58
59 /**
60 * The size of the anti-replay window (in bits)
61 */
62 u_int window_size;
63
64 /**
65 * The anti-replay window buffer
66 */
67 chunk_t window;
68
69 /**
70 * TRUE in case of an inbound ESP context
71 */
72 bool inbound;
73 };
74
75 /**
76 * Set or unset a bit in the window.
77 */
78 static inline void set_window_bit(private_esp_context_t *this,
79 u_int index, bool set)
80 {
81 u_int i = index / CHAR_BIT;
82
83 if (set)
84 {
85 this->window.ptr[i] |= 1 << (index % CHAR_BIT);
86 }
87 else
88 {
89 this->window.ptr[i] &= ~(1 << (index % CHAR_BIT));
90 }
91 }
92
93 /**
94 * Get a bit from the window.
95 */
96 static inline bool get_window_bit(private_esp_context_t *this, u_int index)
97 {
98 u_int i = index / CHAR_BIT;
99
100 return this->window.ptr[i] & (1 << index % CHAR_BIT);
101 }
102
103 /**
104 * Returns TRUE if the supplied seqno is not already marked in the window
105 */
106 static bool check_window(private_esp_context_t *this, u_int32_t seqno)
107 {
108 u_int offset;
109
110 offset = this->last_seqno - seqno;
111 offset = (this->seqno_index - offset) % this->window_size;
112 return !get_window_bit(this, offset);
113 }
114
115 METHOD(esp_context_t, verify_seqno, bool,
116 private_esp_context_t *this, u_int32_t seqno)
117 {
118 if (!this->inbound)
119 {
120 return FALSE;
121 }
122
123 if (seqno > this->last_seqno)
124 { /* |----------------------------------------|
125 * <---------^ ^ or <---------^ ^
126 * WIN H S WIN H S
127 */
128 return TRUE;
129 }
130 else if (seqno > 0 && this->window_size > this->last_seqno - seqno)
131 { /* |----------------------------------------|
132 * <---------^ or <---------^
133 * WIN ^ H WIN ^ H
134 * S S
135 */
136 return check_window(this, seqno);
137 }
138 else
139 { /* |----------------------------------------|
140 * ^ <---------^
141 * S WIN H
142 */
143 return FALSE;
144 }
145 }
146
147 METHOD(esp_context_t, set_authenticated_seqno, void,
148 private_esp_context_t *this, u_int32_t seqno)
149 {
150 u_int i, shift;
151
152 if (!this->inbound)
153 {
154 return;
155 }
156
157 if (seqno > this->last_seqno)
158 { /* shift the window to the new highest authenticated seqno */
159 shift = seqno - this->last_seqno;
160 shift = shift < this->window_size ? shift : this->window_size;
161 for (i = 0; i < shift; ++i)
162 {
163 this->seqno_index = (this->seqno_index + 1) % this->window_size;
164 set_window_bit(this, this->seqno_index, FALSE);
165 }
166 set_window_bit(this, this->seqno_index, TRUE);
167 this->last_seqno = seqno;
168 }
169 else
170 { /* seqno is inside the window, set the corresponding window bit */
171 i = this->last_seqno - seqno;
172 set_window_bit(this, (this->seqno_index - i) % this->window_size, TRUE);
173 }
174 }
175
176 METHOD(esp_context_t, get_seqno, u_int32_t,
177 private_esp_context_t *this)
178 {
179 return this->last_seqno;
180 }
181
182 METHOD(esp_context_t, next_seqno, bool,
183 private_esp_context_t *this, u_int32_t *seqno)
184 {
185 if (this->inbound || this->last_seqno == UINT32_MAX)
186 { /* inbound or segno would cycle */
187 return FALSE;
188 }
189 *seqno = ++this->last_seqno;
190 return TRUE;
191 }
192
193 METHOD(esp_context_t, get_aead, aead_t*,
194 private_esp_context_t *this)
195 {
196 return this->aead;
197 }
198
199 METHOD(esp_context_t, destroy, void,
200 private_esp_context_t *this)
201 {
202 chunk_free(&this->window);
203 DESTROY_IF(this->aead);
204 free(this);
205 }
206
207 /**
208 * Create an AEAD algorithm
209 */
210 static bool create_aead(private_esp_context_t *this, int alg,
211 chunk_t key)
212 {
213 switch (alg)
214 {
215 case ENCR_AES_GCM_ICV8:
216 case ENCR_AES_GCM_ICV12:
217 case ENCR_AES_GCM_ICV16:
218 /* the key includes a 4 byte salt */
219 this->aead = lib->crypto->create_aead(lib->crypto, alg, key.len-4);
220 break;
221 default:
222 break;
223 }
224 if (!this->aead)
225 {
226 DBG1(DBG_ESP, "failed to create ESP context: unsupported AEAD "
227 "algorithm %N", encryption_algorithm_names, alg);
228 return FALSE;
229 }
230 if (!this->aead->set_key(this->aead, key))
231 {
232 DBG1(DBG_ESP, "failed to create ESP context: setting AEAD key failed");
233 return FALSE;
234 }
235 return TRUE;
236 }
237
238 /**
239 * Create AEAD wrapper around traditional encryption/integrity algorithms
240 */
241 static bool create_traditional(private_esp_context_t *this, int enc_alg,
242 chunk_t enc_key, int int_alg, chunk_t int_key)
243 {
244 crypter_t *crypter = NULL;
245 signer_t *signer = NULL;
246
247 crypter = lib->crypto->create_crypter(lib->crypto, enc_alg, enc_key.len);
248 if (!crypter)
249 {
250 DBG1(DBG_ESP, "failed to create ESP context: unsupported encryption "
251 "algorithm %N", encryption_algorithm_names, enc_alg);
252 goto failed;
253 }
254 if (!crypter->set_key(crypter, enc_key))
255 {
256 DBG1(DBG_ESP, "failed to create ESP context: setting encryption key "
257 "failed");
258 goto failed;
259 }
260
261 signer = lib->crypto->create_signer(lib->crypto, int_alg);
262 if (!signer)
263 {
264 DBG1(DBG_ESP, "failed to create ESP context: unsupported integrity "
265 "algorithm %N", integrity_algorithm_names, int_alg);
266 goto failed;
267 }
268 if (!signer->set_key(signer, int_key))
269 {
270 DBG1(DBG_ESP, "failed to create ESP context: setting signature key "
271 "failed");
272 goto failed;
273 }
274 this->aead = aead_create(crypter, signer);
275 return TRUE;
276
277 failed:
278 DESTROY_IF(crypter);
279 DESTROY_IF(signer);
280 return FALSE;
281 }
282
283 /**
284 * Described in header.
285 */
286 esp_context_t *esp_context_create(int enc_alg, chunk_t enc_key,
287 int int_alg, chunk_t int_key, bool inbound)
288 {
289 private_esp_context_t *this;
290
291 INIT(this,
292 .public = {
293 .get_aead = _get_aead,
294 .get_seqno = _get_seqno,
295 .next_seqno = _next_seqno,
296 .verify_seqno = _verify_seqno,
297 .set_authenticated_seqno = _set_authenticated_seqno,
298 .destroy = _destroy,
299 },
300 .inbound = inbound,
301 .window_size = ESP_DEFAULT_WINDOW_SIZE,
302 );
303
304 if (encryption_algorithm_is_aead(enc_alg))
305 {
306 if (!create_aead(this, enc_alg, enc_key))
307 {
308 destroy(this);
309 return NULL;
310 }
311 }
312 else
313 {
314 if (!create_traditional(this, enc_alg, enc_key, int_alg, int_key))
315 {
316 destroy(this);
317 return NULL;
318 }
319 }
320
321 if (inbound)
322 {
323 this->window = chunk_alloc(this->window_size / CHAR_BIT + 1);
324 memset(this->window.ptr, 0, this->window.len);
325 }
326 return &this->public;
327 }