Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
0530feea4975c7ceb1081b23b769f63b857bd477
[simgrid.git] / src / xbt / xbt_sha.c
1 /* $Id$ */
2 /* xbt_sha.c - SHA1 hash function */
3
4 /* Initial version part of iksemel (XML parser for Jabber)
5  *   Copyright (C) 2000-2003 Gurer Ozen <madcat@e-kolay.net>. All right reserved.
6  *   Distributed under LGPL v2.1, February 1999.
7  */
8
9 /* Adapted to fit into SimGrid by Martin Quinson.
10  *   Copyright (c) 2008 Martin Quinson. All rights reserved.
11  *   Distributed under LGPL v2.1, February 1999.
12  */
13
14 /* This program is free software; you can redistribute it and/or modify it
15  * under the terms of the license (GNU LGPL) which comes with this package. */
16
17 #include "xbt/sysdep.h"
18 #include "xbt/hash.h"
19
20 struct s_xbt_sha_ {
21   unsigned int hash[5];
22   unsigned int buf[80];
23   int blen;
24   unsigned int lenhi, lenlo;
25 };
26 static void sha_calculate(xbt_sha_t sha);
27
28 /* ************** */
29 /* User Interface */
30 /* ************** */
31
32 /** @brief constructor */
33 xbt_sha_t xbt_sha_new(void)
34 {
35   xbt_sha_t sha;
36
37   sha = xbt_new(s_xbt_sha_t, 1);
38   xbt_sha_reset(sha);
39
40   return sha;
41 }
42
43 /** @brief destructor */
44 void xbt_sha_free(xbt_sha_t sha)
45 {
46   free(sha);
47 }
48
49 void xbt_sha_reset(xbt_sha_t sha)
50 {
51   memset(sha, 0, sizeof(s_xbt_sha_t));
52   sha->hash[0] = 0x67452301;
53   sha->hash[1] = 0xefcdab89;
54   sha->hash[2] = 0x98badcfe;
55   sha->hash[3] = 0x10325476;
56   sha->hash[4] = 0xc3d2e1f0;
57 }
58
59 /* @brief Add some more data to the buffer */
60 void xbt_sha_feed(xbt_sha_t sha, const unsigned char *data, size_t len)
61 {
62   int i;
63
64   for (i = 0; i < len; i++) {
65     sha->buf[sha->blen / 4] <<= 8;
66     sha->buf[sha->blen / 4] |= (unsigned int) data[i];
67     if ((++sha->blen) % 64 == 0) {
68       sha_calculate(sha);
69       sha->blen = 0;
70     }
71     sha->lenlo += 8;
72     sha->lenhi += (sha->lenlo < 8);
73   }
74 }
75
76 /* finalize computation before displaying the result */
77 static void xbt_sha_finalize(xbt_sha_t sha)
78 {
79   unsigned char pad[8];
80   unsigned char padc;
81
82   pad[0] = (unsigned char) ((sha->lenhi >> 24) & 0xff);
83   pad[1] = (unsigned char) ((sha->lenhi >> 16) & 0xff);
84   pad[2] = (unsigned char) ((sha->lenhi >> 8) & 0xff);
85   pad[3] = (unsigned char) (sha->lenhi & 0xff);
86   pad[4] = (unsigned char) ((sha->lenlo >> 24) & 0xff);
87   pad[5] = (unsigned char) ((sha->lenlo >> 16) & 0xff);
88   pad[6] = (unsigned char) ((sha->lenlo >> 8) & 0xff);
89   pad[7] = (unsigned char) (sha->lenlo & 255);
90
91   padc = 0x80;
92   xbt_sha_feed(sha, &padc, 1);
93
94   padc = 0x00;
95   while (sha->blen != 56)
96     xbt_sha_feed(sha, &padc, 1);
97
98   xbt_sha_feed(sha, pad, 8);
99 }
100
101 /** @brief returns the sha hash into a newly allocated buffer (+ reset sha object) */
102 char *xbt_sha_read(xbt_sha_t sha)
103 {
104   char *res = xbt_malloc(40);
105   xbt_sha_print(sha, res);
106   return res;
107 }
108
109 /** @brief copy the content sha hash into the @a hash pre-allocated string (and reset buffer) */
110 void xbt_sha_print(xbt_sha_t sha, char *hash)
111 {
112   int i;
113
114   xbt_sha_finalize(sha);
115   for (i = 0; i < 5; i++) {
116     sprintf(hash, "%08x", sha->hash[i]);
117     hash += 8;
118   }
119 }
120
121
122 /** @brief simply compute a SHA1 hash and copy it to the provided buffer */
123 void xbt_sha(const char *data, char *hash)
124 {
125   s_xbt_sha_t sha;
126
127   xbt_sha_reset(&sha);
128   xbt_sha_feed(&sha, (const unsigned char *) data, strlen(data));
129
130   xbt_sha_print(&sha, hash);
131 }
132
133 /* ********************* */
134 /* Core of the algorithm */
135 /* ********************* */
136
137 #define SRL(x,y) (((x) << (y)) | ((x) >> (32-(y))))
138 #define SHA(a,b,f,c) \
139   for (i= (a) ; i<= (b) ; i++) { \
140     TMP = SRL(A,5) + ( (f) ) + E + sha->buf[i] + (c) ; \
141     E = D; \
142     D = C; \
143     C = SRL(B,30); \
144     B = A; \
145     A = TMP; \
146   }
147
148 static void sha_calculate(xbt_sha_t sha)
149 {
150   int i;
151   unsigned int A, B, C, D, E, TMP;
152
153   for (i = 16; i < 80; i++)
154     sha->buf[i] =
155       SRL(sha->buf[i - 3] ^ sha->buf[i - 8] ^ sha->
156           buf[i - 14] ^ sha->buf[i - 16], 1);
157
158   A = sha->hash[0];
159   B = sha->hash[1];
160   C = sha->hash[2];
161   D = sha->hash[3];
162   E = sha->hash[4];
163
164   SHA(0, 19, ((C ^ D) & B) ^ D, 0x5a827999);
165   SHA(20, 39, B ^ C ^ D, 0x6ed9eba1);
166   SHA(40, 59, (B & C) | (D & (B | C)), 0x8f1bbcdc);
167   SHA(60, 79, B ^ C ^ D, 0xca62c1d6);
168
169   sha->hash[0] += A;
170   sha->hash[1] += B;
171   sha->hash[2] += C;
172   sha->hash[3] += D;
173   sha->hash[4] += E;
174 }
175
176 /* ************* */
177 /* Testing stuff */
178 /* ************* */
179 #ifdef SIMGRID_TEST
180 #include "xbt/hash.h"
181 #include "portable.h"           /* hexa_str */
182
183 static char *mycmp(const char *p1, const char *p2, size_t n)
184 {
185   int i;
186
187   for (i = 0; i < n; i++) {
188     if (p1[i] != p2[i]) {
189       return bprintf("Differs on %d -- Ox%x", i, p1[i]);
190     }
191   }
192   return xbt_strdup("");
193 }
194
195 static void test_sha(const char *clear, const char *hashed)
196 {
197   char hash[41];
198   xbt_sha(clear, hash);
199
200   xbt_test_add1("==== Test with '%s'", clear);
201   xbt_test_assert3(!memcmp(hash, hashed, 40), "Wrong sha: %40s!=%40s (%s)",
202                    hash, hashed, mycmp(hash, hashed, 40));
203 }
204
205 XBT_TEST_SUITE("hash", "Various hash functions");
206
207 XBT_LOG_NEW_DEFAULT_CATEGORY(hash, "Tests of various hash functions ");
208
209
210 XBT_TEST_UNIT("sha", test_crypto_sha, "Test of the sha algorithm")
211 {
212   /* Empty string as test vector */
213   test_sha("", "da39a3ee5e6b4b0d3255bfef95601890afd80709");
214
215   /* Some pangram as test vector */
216   test_sha("The quick brown fox jumps over the lazy dog",
217            "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12");
218   test_sha("Woven silk pyjamas exchanged for blue quartz",
219            "da3aff337c810c6470db4dbf0f205c8afc31c442");
220   test_sha("Pack my box with five dozen liquor jugs",
221            "373ba8be29d4d95708bf7cd43038f4e409dcb439");
222
223 }
224 #endif /* SIMGRID_TEST */