4 * @brief Implementation of hasher_sha_t.
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
12 * Ported from Steve Reid's <steve@edmweb.com> implementation
13 * "SHA1 in C" found in strongSwan.
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>.
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
28 #include "sha1_hasher.h"
30 #include <definitions.h>
32 #define BLOCK_SIZE_SHA1 20
37 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
39 #if BYTE_ORDER == LITTLE_ENDIAN
40 #define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) |(rol(block->l[i],8)&0x00FF00FF))
41 #elif BYTE_ORDER == BIG_ENDIAN
42 #define blk0(i) block->l[i]
44 #error "Endianness not defined!"
46 #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))
48 /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
49 #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
50 #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
51 #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
52 #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
53 #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
56 typedef struct private_sha1_hasher_t private_sha1_hasher_t
;
59 * Private data structure with hasing context.
61 struct private_sha1_hasher_t
{
63 * Public interface for this hasher.
68 * State of the hasher.
76 * Hash a single 512-bit block. This is the core of the algorithm. *
78 static void SHA1Transform(u_int32_t state
[5], const unsigned char buffer
[64])
80 u_int32_t a
, b
, c
, d
, e
;
85 CHAR64LONG16 block
[1]; /* use array to appear as a pointer */
86 memcpy(block
, buffer
, 64);
88 /* Copy context->state[] to working vars */
94 /* 4 rounds of 20 operations each. Loop unrolled. */
95 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);
96 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);
97 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);
98 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);
99 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);
100 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);
101 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);
102 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);
103 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);
104 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);
105 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);
106 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);
107 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);
108 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);
109 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);
110 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);
111 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);
112 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);
113 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);
114 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);
115 /* Add the working vars back into context.state[] */
122 a
= b
= c
= d
= e
= 0;
123 memset(block
, '\0', sizeof(block
));
127 * Run your data through this.
129 static void SHA1Update(private_sha1_hasher_t
* this, u_int8_t
*data
, u_int32_t len
)
135 if ((this->count
[0] += len
<< 3) < j
)
139 this->count
[1] += (len
>>29);
143 memcpy(&this->buffer
[j
], data
, (i
= 64-j
));
144 SHA1Transform(this->state
, this->buffer
);
145 for ( ; i
+ 63 < len
; i
+= 64)
147 SHA1Transform(this->state
, &data
[i
]);
155 memcpy(&this->buffer
[j
], &data
[i
], len
- i
);
160 * Add padding and return the message digest.
162 static void SHA1Final(private_sha1_hasher_t
*this, u_int8_t
*digest
)
165 u_int8_t finalcount
[8];
168 for (i
= 0; i
< 8; i
++)
170 finalcount
[i
] = (u_int8_t
)((this->count
[(i
>= 4 ? 0 : 1)]
171 >> ((3-(i
& 3)) * 8) ) & 255); /* Endian independent */
174 SHA1Update(this, &c
, 1);
175 while ((this->count
[0] & 504) != 448)
178 SHA1Update(this, &c
, 1);
180 SHA1Update(this, finalcount
, 8); /* Should cause a SHA1Transform() */
181 for (i
= 0; i
< 20; i
++)
183 digest
[i
] = (u_int8_t
)((this->state
[i
>>2] >> ((3-(i
& 3)) * 8) ) & 255);
189 * Implementation of hasher_t.get_hash.
191 static void get_hash(private_sha1_hasher_t
*this, chunk_t chunk
, u_int8_t
*buffer
)
193 SHA1Update(this, chunk
.ptr
, chunk
.len
);
196 SHA1Final(this, buffer
);
197 this->public.hasher_interface
.reset(&(this->public.hasher_interface
));
203 * Implementation of hasher_t.allocate_hash.
205 static void allocate_hash(private_sha1_hasher_t
*this, chunk_t chunk
, chunk_t
*hash
)
207 chunk_t allocated_hash
;
209 SHA1Update(this, chunk
.ptr
, chunk
.len
);
212 allocated_hash
.ptr
= malloc(BLOCK_SIZE_SHA1
);
213 allocated_hash
.len
= BLOCK_SIZE_SHA1
;
215 SHA1Final(this, allocated_hash
.ptr
);
216 this->public.hasher_interface
.reset(&(this->public.hasher_interface
));
218 *hash
= allocated_hash
;
223 * Implementation of hasher_t.get_hash_size.
225 static size_t get_hash_size(private_sha1_hasher_t
*this)
227 return BLOCK_SIZE_SHA1
;
231 * Implementation of hasher_t.reset.
233 static void reset(private_sha1_hasher_t
*this)
235 this->state
[0] = 0x67452301;
236 this->state
[1] = 0xEFCDAB89;
237 this->state
[2] = 0x98BADCFE;
238 this->state
[3] = 0x10325476;
239 this->state
[4] = 0xC3D2E1F0;
244 * Implementation of hasher_t.destroy.
246 static void destroy(private_sha1_hasher_t
*this)
253 * Described in header.
255 sha1_hasher_t
*sha1_hasher_create()
257 private_sha1_hasher_t
*this = malloc_thing(private_sha1_hasher_t
);
259 this->public.hasher_interface
.get_hash
= (void (*) (hasher_t
*, chunk_t
, u_int8_t
*))get_hash
;
260 this->public.hasher_interface
.allocate_hash
= (void (*) (hasher_t
*, chunk_t
, chunk_t
*))allocate_hash
;
261 this->public.hasher_interface
.get_hash_size
= (size_t (*) (hasher_t
*))get_hash_size
;
262 this->public.hasher_interface
.reset
= (void (*) (hasher_t
*))reset
;
263 this->public.hasher_interface
.destroy
= (void (*) (hasher_t
*))destroy
;
266 this->public.hasher_interface
.reset(&(this->public.hasher_interface
));
268 return &(this->public);