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