The introduced SHA1_NOFINAL hasher was not sufficient for EAP-AKA,
[strongswan.git] / src / libstrongswan / plugins / sha1 / sha1_hasher.c
1 /*
2 * Copyright (C) 2005-2006 Martin Willi
3 * Copyright (C) 2005 Jan Hutter
4 * Hochschule fuer Technik Rapperswil
5 *
6 * Ported from Steve Reid's <steve@edmweb.com> implementation
7 * "SHA1 in C" found in strongSwan.
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 * for more details.
18 *
19 * $Id$
20 */
21
22 #include <string.h>
23 #include <arpa/inet.h>
24
25 #include "sha1_hasher.h"
26
27 /*
28 * ugly macro stuff
29 */
30 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
31
32 #if BYTE_ORDER == LITTLE_ENDIAN
33 #define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) |(rol(block->l[i],8)&0x00FF00FF))
34 #elif BYTE_ORDER == BIG_ENDIAN
35 #define blk0(i) block->l[i]
36 #else
37 #error "Endianness not defined!"
38 #endif
39 #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] ^block->l[(i+2)&15]^block->l[i&15],1))
40
41 /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
42 #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
43 #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
44 #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
45 #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
46 #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
47
48
49 typedef struct private_sha1_hasher_t private_sha1_hasher_t;
50 typedef struct private_sha1_keyed_prf_t private_sha1_keyed_prf_t;
51
52 /**
53 * Private data structure with hasing context.
54 */
55 struct private_sha1_hasher_t {
56 /**
57 * Public interface for this hasher.
58 */
59 sha1_hasher_t public;
60
61 /*
62 * State of the hasher.
63 */
64 u_int32_t state[5];
65 u_int32_t count[2];
66 u_int8_t buffer[64];
67 };
68
69 /**
70 * Private data structure with keyed prf context.
71 */
72 struct private_sha1_keyed_prf_t {
73 /**
74 * public prf interface
75 */
76 sha1_keyed_prf_t public;
77
78 /**
79 * internal used hasher
80 */
81 private_sha1_hasher_t *hasher;
82 };
83
84 /*
85 * Hash a single 512-bit block. This is the core of the algorithm. *
86 */
87 static void SHA1Transform(u_int32_t state[5], const unsigned char buffer[64])
88 {
89 u_int32_t a, b, c, d, e;
90 typedef union {
91 u_int8_t c[64];
92 u_int32_t l[16];
93 } CHAR64LONG16;
94 CHAR64LONG16 block[1]; /* use array to appear as a pointer */
95 memcpy(block, buffer, 64);
96
97 /* Copy context->state[] to working vars */
98 a = state[0];
99 b = state[1];
100 c = state[2];
101 d = state[3];
102 e = state[4];
103 /* 4 rounds of 20 operations each. Loop unrolled. */
104 R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
105 R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
106 R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
107 R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
108 R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
109 R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
110 R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
111 R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
112 R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
113 R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
114 R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
115 R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
116 R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
117 R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
118 R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
119 R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
120 R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
121 R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
122 R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
123 R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
124 /* Add the working vars back into context.state[] */
125 state[0] += a;
126 state[1] += b;
127 state[2] += c;
128 state[3] += d;
129 state[4] += e;
130 /* Wipe variables */
131 a = b = c = d = e = 0;
132 memset(block, '\0', sizeof(block));
133 }
134
135 /*
136 * Run your data through this.
137 */
138 static void SHA1Update(private_sha1_hasher_t* this, u_int8_t *data, u_int32_t len)
139 {
140 u_int32_t i;
141 u_int32_t j;
142
143 j = this->count[0];
144 if ((this->count[0] += len << 3) < j)
145 {
146 this->count[1]++;
147 }
148 this->count[1] += (len>>29);
149 j = (j >> 3) & 63;
150 if ((j + len) > 63)
151 {
152 memcpy(&this->buffer[j], data, (i = 64-j));
153 SHA1Transform(this->state, this->buffer);
154 for ( ; i + 63 < len; i += 64)
155 {
156 SHA1Transform(this->state, &data[i]);
157 }
158 j = 0;
159 }
160 else
161 {
162 i = 0;
163 }
164 memcpy(&this->buffer[j], &data[i], len - i);
165 }
166
167
168 /*
169 * Add padding and return the message digest.
170 */
171 static void SHA1Final(private_sha1_hasher_t *this, u_int8_t *digest)
172 {
173 u_int32_t i;
174 u_int8_t finalcount[8];
175 u_int8_t c;
176
177 for (i = 0; i < 8; i++)
178 {
179 finalcount[i] = (u_int8_t)((this->count[(i >= 4 ? 0 : 1)]
180 >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */
181 }
182 c = 0200;
183 SHA1Update(this, &c, 1);
184 while ((this->count[0] & 504) != 448)
185 {
186 c = 0000;
187 SHA1Update(this, &c, 1);
188 }
189 SHA1Update(this, finalcount, 8); /* Should cause a SHA1Transform() */
190 for (i = 0; i < 20; i++)
191 {
192 digest[i] = (u_int8_t)((this->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
193 }
194 }
195
196 /**
197 * Implementation of hasher_t.reset.
198 */
199 static void reset(private_sha1_hasher_t *this)
200 {
201 this->state[0] = 0x67452301;
202 this->state[1] = 0xEFCDAB89;
203 this->state[2] = 0x98BADCFE;
204 this->state[3] = 0x10325476;
205 this->state[4] = 0xC3D2E1F0;
206 this->count[0] = 0;
207 this->count[1] = 0;
208 }
209
210 /**
211 * Implementation of hasher_t.get_hash.
212 */
213 static void get_hash(private_sha1_hasher_t *this, chunk_t chunk, u_int8_t *buffer)
214 {
215 SHA1Update(this, chunk.ptr, chunk.len);
216 if (buffer != NULL)
217 {
218 SHA1Final(this, buffer);
219 reset(this);
220 }
221 }
222
223 /**
224 * Implementation of hasher_t.allocate_hash.
225 */
226 static void allocate_hash(private_sha1_hasher_t *this, chunk_t chunk, chunk_t *hash)
227 {
228 SHA1Update(this, chunk.ptr, chunk.len);
229 if (hash != NULL)
230 {
231 hash->ptr = malloc(HASH_SIZE_SHA1);
232 hash->len = HASH_SIZE_SHA1;
233
234 SHA1Final(this, hash->ptr);
235 reset(this);
236 }
237 }
238
239 /**
240 * Implementation of hasher_t.get_hash_size.
241 */
242 static size_t get_hash_size(private_sha1_hasher_t *this)
243 {
244 return HASH_SIZE_SHA1;
245 }
246
247 /**
248 * Implementation of hasher_t.destroy.
249 */
250 static void destroy(private_sha1_hasher_t *this)
251 {
252 free(this);
253 }
254
255 /*
256 * Described in header.
257 */
258 sha1_hasher_t *sha1_hasher_create(hash_algorithm_t algo)
259 {
260 private_sha1_hasher_t *this;
261 if (algo != HASH_SHA1)
262 {
263 return NULL;
264 }
265 this = malloc_thing(private_sha1_hasher_t);
266 this->public.hasher_interface.get_hash = (void (*) (hasher_t*, chunk_t, u_int8_t*))get_hash;
267 this->public.hasher_interface.allocate_hash = (void (*) (hasher_t*, chunk_t, chunk_t*))allocate_hash;
268 this->public.hasher_interface.get_hash_size = (size_t (*) (hasher_t*))get_hash_size;
269 this->public.hasher_interface.reset = (void (*) (hasher_t*))reset;
270 this->public.hasher_interface.destroy = (void (*) (hasher_t*))destroy;
271
272 /* initialize */
273 reset(this);
274
275 return &(this->public);
276 }
277
278 /**
279 * Implementation of prf_t.get_bytes.
280 */
281 static void get_bytes(private_sha1_keyed_prf_t *this, chunk_t seed, u_int8_t *bytes)
282 {
283 u_int32_t *hash = (u_int32_t*)bytes;
284
285 SHA1Update(this->hasher, seed.ptr, seed.len);
286
287 hash[0] = htonl(this->hasher->state[0]);
288 hash[1] = htonl(this->hasher->state[1]);
289 hash[2] = htonl(this->hasher->state[2]);
290 hash[3] = htonl(this->hasher->state[3]);
291 hash[4] = htonl(this->hasher->state[4]);
292 }
293
294 /**
295 * Implementation of prf_t.get_block_size.
296 */
297 static size_t get_block_size(private_sha1_keyed_prf_t *this)
298 {
299 return HASH_SIZE_SHA1;
300 }
301
302 /**
303 * Implementation of prf_t.allocate_bytes.
304 */
305 static void allocate_bytes(private_sha1_keyed_prf_t *this, chunk_t seed, chunk_t *chunk)
306 {
307 *chunk = chunk_alloc(HASH_SIZE_SHA1);
308 get_bytes(this, seed, chunk->ptr);
309 }
310
311 /**
312 * Implementation of prf_t.get_key_size.
313 */
314 static size_t get_key_size(private_sha1_keyed_prf_t *this)
315 {
316 return sizeof(this->hasher->state);
317 }
318
319 /**
320 * Implementation of prf_t.set_key.
321 */
322 static void set_key(private_sha1_keyed_prf_t *this, chunk_t key)
323 {
324 int i, rounds;
325 u_int32_t *iv = (u_int32_t*)key.ptr;
326
327 reset(this->hasher);
328 rounds = min(key.len/sizeof(u_int32_t), sizeof(this->hasher->state));
329 for (i = 0; i < rounds; i++)
330 {
331 this->hasher->state[i] ^= htonl(iv[i]);
332 }
333 }
334
335 /**
336 * Implementation of prf_t.destroy.
337 */
338 static void destroy_p(private_sha1_keyed_prf_t *this)
339 {
340 destroy(this->hasher);
341 free(this);
342 }
343
344 /**
345 * see header
346 */
347 sha1_keyed_prf_t *sha1_keyed_prf_create(pseudo_random_function_t algo)
348 {
349 private_sha1_keyed_prf_t *this;
350 if (algo != PRF_KEYED_SHA1)
351 {
352 return NULL;
353 }
354 this = malloc_thing(private_sha1_keyed_prf_t);
355 this->public.prf_interface.get_bytes = (void (*) (prf_t *,chunk_t,u_int8_t*))get_bytes;
356 this->public.prf_interface.allocate_bytes = (void (*) (prf_t*,chunk_t,chunk_t*))allocate_bytes;
357 this->public.prf_interface.get_block_size = (size_t (*) (prf_t*))get_block_size;
358 this->public.prf_interface.get_key_size = (size_t (*) (prf_t*))get_key_size;
359 this->public.prf_interface.set_key = (void (*) (prf_t *,chunk_t))set_key;
360 this->public.prf_interface.destroy = (void (*) (prf_t *))destroy_p;
361
362 this->hasher = (private_sha1_hasher_t*)sha1_hasher_create(HASH_SHA1);
363
364 return &(this->public);
365 }
366