1 /**************************************************************************/
2 /* ---------------------------------------------------------------------- */
3 /* Copyright (C) 2006 - 2008 FRESCOR consortium partners: */
5 /* Universidad de Cantabria, SPAIN */
6 /* University of York, UK */
7 /* Scuola Superiore Sant'Anna, ITALY */
8 /* Kaiserslautern University, GERMANY */
9 /* Univ. Politécnica Valencia, SPAIN */
10 /* Czech Technical University in Prague, CZECH REPUBLIC */
12 /* Thales Communication S.A. FRANCE */
13 /* Visual Tools S.A. SPAIN */
14 /* Rapita Systems Ltd UK */
17 /* See http://www.frescor.org for a link to partners' websites */
19 /* FRESCOR project (FP6/2005/IST/5-034026) is funded */
20 /* in part by the European Union Sixth Framework Programme */
21 /* The European Union is not liable of any use that may be */
22 /* made of this code. */
25 /* This file is part of FORB (Frescor Object Request Broker) */
27 /* FORB is free software; you can redistribute it and/or modify it */
28 /* under terms of the GNU General Public License as published by the */
29 /* Free Software Foundation; either version 2, or (at your option) any */
30 /* later version. FORB is distributed in the hope that it will be */
31 /* useful, but WITHOUT ANY WARRANTY; without even the implied warranty */
32 /* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
33 /* General Public License for more details. You should have received a */
34 /* copy of the GNU General Public License along with FORB; see file */
35 /* COPYING. If not, write to the Free Software Foundation, 675 Mass Ave, */
36 /* Cambridge, MA 02139, USA. */
38 /* As a special exception, including FORB header files in a file, */
39 /* instantiating FORB generics or templates, or linking other files */
40 /* with FORB objects to produce an executable application, does not */
41 /* by itself cause the resulting executable application to be covered */
42 /* by the GNU General Public License. This exception does not */
43 /* however invalidate any other reasons why the executable file might be */
44 /* covered by the GNU Public License. */
45 /**************************************************************************/
49 By Steve Reid <steve@edmweb.com>
52 Test Vectors (from FIPS PUB 180-1)
54 A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
55 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
56 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
57 A million repetitions of "a"
58 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
61 /* #define LITTLE_ENDIAN * This should be #define'd if true. */
62 /* #define SHA1HANDSOFF * Copies data before messing with it. */
64 /* #include <stdio.h> */
69 /* Extract endinaning from glibc */
73 #if __BYTE_ORDER == __LITTLE_ENDIAN
78 void SHA1Transform(uint32_t state[5], uint8_t buffer[64]);
79 void SHA1Init(SHA1_CTX* context);
80 void SHA1Update(SHA1_CTX* context, uint8_t* data, unsigned int len);
81 void SHA1Final(uint8_t digest[20], SHA1_CTX* context);
83 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
85 /* blk0() and blk() perform the initial expand. */
86 /* I got the idea of expanding during the round function from SSLeay */
88 #define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
89 |(rol(block->l[i],8)&0x00FF00FF))
91 #define blk0(i) block->l[i]
93 #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
94 ^block->l[(i+2)&15]^block->l[i&15],1))
96 /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
97 #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
98 #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
99 #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
100 #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
101 #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
104 /* Hash a single 512-bit block. This is the core of the algorithm. */
106 void SHA1Transform(uint32_t state[5], uint8_t buffer[64])
108 uint32_t a, b, c, d, e;
115 static uint8_t workspace[64];
116 block = (CHAR64LONG16*)workspace;
117 memcpy(block, buffer, 64);
119 block = (CHAR64LONG16*)buffer;
121 /* Copy context->state[] to working vars */
127 /* 4 rounds of 20 operations each. Loop unrolled. */
128 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);
129 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);
130 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);
131 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);
132 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);
133 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);
134 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);
135 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);
136 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);
137 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);
138 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);
139 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);
140 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);
141 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);
142 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);
143 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);
144 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);
145 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);
146 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);
147 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);
148 /* Add the working vars back into context.state[] */
155 a = b = c = d = e = 0;
159 /* SHA1Init - Initialize new context */
161 void SHA1Init(SHA1_CTX* context)
163 /* SHA1 initialization constants */
164 context->state[0] = 0x67452301;
165 context->state[1] = 0xEFCDAB89;
166 context->state[2] = 0x98BADCFE;
167 context->state[3] = 0x10325476;
168 context->state[4] = 0xC3D2E1F0;
169 context->count[0] = context->count[1] = 0;
173 /* Run your data through this. */
175 void SHA1Update(SHA1_CTX* context, uint8_t* data, unsigned int len)
179 j = (context->count[0] >> 3) & 63;
180 if ((context->count[0] += len << 3) < (len << 3)) context->count[1]++;
181 context->count[1] += (len >> 29);
182 if ((j + len) > 63) {
183 memcpy(&context->buffer[j], data, (i = 64-j));
184 SHA1Transform(context->state, context->buffer);
185 for ( ; i + 63 < len; i += 64) {
186 SHA1Transform(context->state, &data[i]);
191 memcpy(&context->buffer[j], &data[i], len - i);
195 /* Add padding and return the message digest. */
197 void SHA1Final(uint8_t digest[20], SHA1_CTX* context)
200 uint8_t finalcount[8];
202 for (i = 0; i < 8; i++) {
203 finalcount[i] = (uint8_t)((context->count[(i >= 4 ? 0 : 1)]
204 >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */
206 SHA1Update(context, (uint8_t *)"\200", 1);
207 while ((context->count[0] & 504) != 448) {
208 SHA1Update(context, (uint8_t *)"\0", 1);
210 SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */
211 for (i = 0; i < 20; i++) {
212 digest[i] = (uint8_t)
213 ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
217 memset(context->buffer, 0, 64);
218 memset(context->state, 0, 20);
219 memset(context->count, 0, 8);
220 memset(&finalcount, 0, 8);
221 #ifdef SHA1HANDSOFF /* make SHA1Transform overwrite it's own static vars */
222 SHA1Transform(context->state, context->buffer);
227 /*************************************************************/
232 int main(int argc, char** argv)
236 uint8_t digest[20], buffer[16384];
240 puts("Public domain SHA-1 implementation - by Steve Reid <steve@edmweb.com>");
241 puts("Produces the SHA-1 hash of a file, or stdin if no file is specified.");
248 if (!(file = fopen(argv[1], "rb"))) {
249 fputs("Unable to open file.", stderr);
254 while (!feof(file)) { /* note: what if ferror(file) */
255 i = fread(buffer, 1, 16384, file);
256 SHA1Update(&context, buffer, i);
258 SHA1Final(digest, &context);
260 for (i = 0; i < 5; i++) {
261 for (j = 0; j < 4; j++) {
262 printf("%02X", digest[i*4+j]);