- fixed member order
[strongswan.git] / Source / charon / transforms / hashers / hasher_sha1.c
1 /**
2 * @file hasher_sha1.c
3 *
4 * @brief Implementation of hasher_sha_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * Ported from Steve Reid's <steve@edmweb.com> implementation
13 * "SHA1 in C" found in strongSwan.
14 *
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 2 of the License, or (at your
18 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
19 *
20 * This program is distributed in the hope that it will be useful, but
21 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 * for more details.
24 */
25
26 #include "hasher_sha1.h"
27
28 #include <definitions.h>
29 #include <utils/allocator.h>
30
31 #define BLOCK_SIZE_SHA1 20
32
33 /*
34 * ugly macro stuff
35 */
36 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
37
38 #if BYTE_ORDER == LITTLE_ENDIAN
39 #define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) |(rol(block->l[i],8)&0x00FF00FF))
40 #elif BYTE_ORDER == BIG_ENDIAN
41 #define blk0(i) block->l[i]
42 #else
43 #error "Endianness not defined!"
44 #endif
45 #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))
46
47 /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
48 #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
49 #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
50 #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
51 #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
52 #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
53
54
55 typedef struct private_hasher_sha1_t private_hasher_sha1_t;
56
57 /**
58 * private data structure with hasing context
59 */
60 struct private_hasher_sha1_t {
61 /**
62 * public interface for this hasher
63 */
64 hasher_sha1_t public;
65
66 /*
67 * state of the hasher
68 */
69 u_int32_t state[5];
70 u_int32_t count[2];
71 u_int8_t buffer[64];
72 };
73
74 /*
75 * Hash a single 512-bit block. This is the core of the algorithm. *
76 */
77 void SHA1Transform(u_int32_t state[5], const unsigned char buffer[64])
78 {
79 u_int32_t a, b, c, d, e;
80 typedef union {
81 u_int8_t c[64];
82 u_int32_t l[16];
83 } CHAR64LONG16;
84 CHAR64LONG16 block[1]; /* use array to appear as a pointer */
85 memcpy(block, buffer, 64);
86
87 /* Copy context->state[] to working vars */
88 a = state[0];
89 b = state[1];
90 c = state[2];
91 d = state[3];
92 e = state[4];
93 /* 4 rounds of 20 operations each. Loop unrolled. */
94 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);
95 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);
96 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);
97 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);
98 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);
99 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);
100 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);
101 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);
102 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);
103 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);
104 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);
105 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);
106 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);
107 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);
108 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);
109 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);
110 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);
111 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);
112 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);
113 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);
114 /* Add the working vars back into context.state[] */
115 state[0] += a;
116 state[1] += b;
117 state[2] += c;
118 state[3] += d;
119 state[4] += e;
120 /* Wipe variables */
121 a = b = c = d = e = 0;
122 memset(block, '\0', sizeof(block));
123 }
124
125 /*
126 * Run your data through this.
127 */
128 void SHA1Update(private_hasher_sha1_t* this, u_int8_t *data, u_int32_t len)
129 {
130 u_int32_t i;
131 u_int32_t j;
132
133 j = this->count[0];
134 if ((this->count[0] += len << 3) < j)
135 {
136 this->count[1]++;
137 }
138 this->count[1] += (len>>29);
139 j = (j >> 3) & 63;
140 if ((j + len) > 63)
141 {
142 memcpy(&this->buffer[j], data, (i = 64-j));
143 SHA1Transform(this->state, this->buffer);
144 for ( ; i + 63 < len; i += 64)
145 {
146 SHA1Transform(this->state, &data[i]);
147 }
148 j = 0;
149 }
150 else
151 {
152 i = 0;
153 }
154 memcpy(&this->buffer[j], &data[i], len - i);
155 }
156
157
158 /*
159 * Add padding and return the message digest.
160 */
161 void SHA1Final(private_hasher_sha1_t *this, u_int8_t *digest)
162 {
163 u_int32_t i;
164 u_int8_t finalcount[8];
165 u_int8_t c;
166
167 for (i = 0; i < 8; i++)
168 {
169 finalcount[i] = (u_int8_t)((this->count[(i >= 4 ? 0 : 1)]
170 >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */
171 }
172 c = 0200;
173 SHA1Update(this, &c, 1);
174 while ((this->count[0] & 504) != 448)
175 {
176 c = 0000;
177 SHA1Update(this, &c, 1);
178 }
179 SHA1Update(this, finalcount, 8); /* Should cause a SHA1Transform() */
180 for (i = 0; i < 20; i++)
181 {
182 digest[i] = (u_int8_t)((this->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
183 }
184 }
185
186
187 /**
188 * implementation of hasher_t.get_hash for sha1
189 */
190 static status_t get_hash(private_hasher_sha1_t *this, chunk_t chunk, u_int8_t *buffer)
191 {
192 SHA1Update(this, chunk.ptr, chunk.len);
193 if (buffer != NULL)
194 {
195 SHA1Final(this, buffer);
196 this->public.hasher_interface.reset(&(this->public.hasher_interface));
197 }
198 return SUCCESS;
199 }
200
201
202 /**
203 * implementation of hasher_t.allocate_hash for sha1
204 */
205 static status_t allocate_hash(private_hasher_sha1_t *this, chunk_t chunk, chunk_t *hash)
206 {
207 chunk_t allocated_hash;
208
209 SHA1Update(this, chunk.ptr, chunk.len);
210 if (hash != NULL)
211 {
212 allocated_hash.ptr = allocator_alloc(BLOCK_SIZE_SHA1);
213 allocated_hash.len = BLOCK_SIZE_SHA1;
214 if (allocated_hash.ptr == NULL)
215 {
216 return OUT_OF_RES;
217 }
218 SHA1Final(this, allocated_hash.ptr);
219 this->public.hasher_interface.reset(&(this->public.hasher_interface));
220
221 *hash = allocated_hash;
222 }
223
224 return SUCCESS;
225 }
226
227 /**
228 * implementation of hasher_t.get_block_size for sha1
229 */
230 static size_t get_block_size(private_hasher_sha1_t *this)
231 {
232 return BLOCK_SIZE_SHA1;
233 }
234
235 /**
236 * implementation of hasher_t.reset for sha1
237 */
238 static status_t reset(private_hasher_sha1_t *this)
239 {
240 this->state[0] = 0x67452301;
241 this->state[1] = 0xEFCDAB89;
242 this->state[2] = 0x98BADCFE;
243 this->state[3] = 0x10325476;
244 this->state[4] = 0xC3D2E1F0;
245 this->count[0] = 0;
246 this->count[1] = 0;
247 return SUCCESS;
248 }
249 /**
250 * implementation of hasher_t.destroy for sha1
251 */
252 static status_t destroy(private_hasher_sha1_t *this)
253 {
254 allocator_free(this);
255 return SUCCESS;
256 }
257
258
259 /*
260 * Described in header
261 */
262 hasher_sha1_t *hasher_sha1_create()
263 {
264 private_hasher_sha1_t *this = allocator_alloc_thing(private_hasher_sha1_t);
265 if (this == NULL)
266 {
267 return NULL;
268 }
269
270 this->public.hasher_interface.get_hash = (status_t (*) (hasher_t*, chunk_t, u_int8_t*))get_hash;
271 this->public.hasher_interface.allocate_hash = (status_t (*) (hasher_t*, chunk_t, chunk_t*))allocate_hash;
272 this->public.hasher_interface.get_block_size = (size_t (*) (hasher_t*))get_block_size;
273 this->public.hasher_interface.reset = (size_t (*) (hasher_t*))reset;
274 this->public.hasher_interface.destroy = (size_t (*) (hasher_t*))destroy;
275
276 /* initialize */
277 this->public.hasher_interface.reset(&(this->public.hasher_interface));
278
279 return &(this->public);
280 }