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