Logo AND Algorithmique Numérique Distribuée

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