Logo AND Algorithmique Numérique Distribuée

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