]> rtime.felk.cvut.cz Git - frescor/forb.git/blob - src/sha1.c
forb: Split forb_port_destroy() to stop and destroy phases
[frescor/forb.git] / src / sha1.c
1 /**************************************************************************/
2 /* ---------------------------------------------------------------------- */
3 /* Copyright (C) 2006 - 2008 FRESCOR consortium partners:                 */
4 /*                                                                        */
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                */
11 /*   ENEA                                   SWEDEN                        */
12 /*   Thales Communication S.A.              FRANCE                        */
13 /*   Visual Tools S.A.                      SPAIN                         */
14 /*   Rapita Systems Ltd                     UK                            */
15 /*   Evidence                               ITALY                         */
16 /*                                                                        */
17 /*   See http://www.frescor.org for a link to partners' websites          */
18 /*                                                                        */
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.                                               */
23 /*                                                                        */
24 /*                                                                        */
25 /*  This file is part of FORB (Frescor Object Request Broker)             */
26 /*                                                                        */
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.                                              */
37 /*                                                                        */
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 /**************************************************************************/
46
47 /*
48 SHA-1 in C
49 By Steve Reid <steve@edmweb.com>
50 100% Public Domain
51
52 Test Vectors (from FIPS PUB 180-1)
53 "abc"
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
59 */
60
61 /* #define LITTLE_ENDIAN * This should be #define'd if true. */
62 /* #define SHA1HANDSOFF * Copies data before messing with it. */
63
64 /* #include <stdio.h> */
65 #include <inttypes.h>
66 #include <string.h>
67 #include "sha1.h"
68
69 /* Extract endinaning from glibc */
70 #include <endian.h>
71 #undef LITTLE_ENDIAN
72 #ifdef __BYTE_ORDER
73 #if __BYTE_ORDER == __LITTLE_ENDIAN
74 #define LITTLE_ENDIAN
75 #endif
76 #endif
77
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);
82
83 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
84
85 /* blk0() and blk() perform the initial expand. */
86 /* I got the idea of expanding during the round function from SSLeay */
87 #ifdef LITTLE_ENDIAN
88 #define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
89     |(rol(block->l[i],8)&0x00FF00FF))
90 #else
91 #define blk0(i) block->l[i]
92 #endif
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))
95
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);
102
103
104 /* Hash a single 512-bit block. This is the core of the algorithm. */
105
106 void SHA1Transform(uint32_t state[5], uint8_t buffer[64])
107 {
108 uint32_t a, b, c, d, e;
109 typedef union {
110     uint8_t c[64];
111     uint32_t l[16];
112 } CHAR64LONG16;
113 CHAR64LONG16* block;
114 #ifdef SHA1HANDSOFF
115 static uint8_t workspace[64];
116     block = (CHAR64LONG16*)workspace;
117     memcpy(block, buffer, 64);
118 #else
119     block = (CHAR64LONG16*)buffer;
120 #endif
121     /* Copy context->state[] to working vars */
122     a = state[0];
123     b = state[1];
124     c = state[2];
125     d = state[3];
126     e = state[4];
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[] */
149     state[0] += a;
150     state[1] += b;
151     state[2] += c;
152     state[3] += d;
153     state[4] += e;
154     /* Wipe variables */
155     a = b = c = d = e = 0;
156 }
157
158
159 /* SHA1Init - Initialize new context */
160
161 void SHA1Init(SHA1_CTX* context)
162 {
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;
170 }
171
172
173 /* Run your data through this. */
174
175 void SHA1Update(SHA1_CTX* context, uint8_t* data, unsigned int len)
176 {
177 unsigned int i, j;
178
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]);
187         }
188         j = 0;
189     }
190     else i = 0;
191     memcpy(&context->buffer[j], &data[i], len - i);
192 }
193
194
195 /* Add padding and return the message digest. */
196
197 void SHA1Final(uint8_t digest[20], SHA1_CTX* context)
198 {
199 unsigned long i, j;
200 uint8_t finalcount[8];
201
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 */
205     }
206     SHA1Update(context, (uint8_t *)"\200", 1);
207     while ((context->count[0] & 504) != 448) {
208         SHA1Update(context, (uint8_t *)"\0", 1);
209     }
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);
214     }
215     /* Wipe variables */
216     i = j = 0;
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);
223 #endif
224 }
225
226
227 /*************************************************************/
228
229 #if 0
230 #include <stdio.h>
231
232 int main(int argc, char** argv)
233 {
234 int i, j;
235 SHA1_CTX context;
236 uint8_t digest[20], buffer[16384];
237 FILE* file;
238
239     if (argc > 2) {
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.");
242         exit(0);
243     }
244     if (argc < 2) {
245         file = stdin;
246     }
247     else {
248         if (!(file = fopen(argv[1], "rb"))) {
249             fputs("Unable to open file.", stderr);
250             exit(-1);
251         }
252     } 
253     SHA1Init(&context);
254     while (!feof(file)) {  /* note: what if ferror(file) */
255         i = fread(buffer, 1, 16384, file);
256         SHA1Update(&context, buffer, i);
257     }
258     SHA1Final(digest, &context);
259     fclose(file);
260     for (i = 0; i < 5; i++) {
261         for (j = 0; j < 4; j++) {
262             printf("%02X", digest[i*4+j]);
263         }
264         putchar(' ');
265     }
266     putchar('\n');
267     exit(0);
268 }
269 #endif