2 /* xbt_sha.c - SHA1 hash function */
4 /* Initial version part of iksemel (XML parser for Jabber)
5 * Copyright (C) 2000-2003 Gurer Ozen <madcat@e-kolay.net>. All right reserved. */
7 /* Adapted to fit into SimGrid by Martin Quinson.
8 Copyright (c) 2008 Martin Quinson. All rights reserved. */
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. */
13 #include "xbt/sysdep.h"
20 unsigned int lenhi, lenlo;
22 static void sha_calculate (xbt_sha_t sha);
28 /** @brief constructor */
29 xbt_sha_t xbt_sha_new (void) {
32 sha = xbt_new(s_xbt_sha_t,1);
38 /** @brief destructor */
39 void xbt_sha_free (xbt_sha_t sha) {
43 void xbt_sha_reset (xbt_sha_t sha) {
44 memset (sha, 0, sizeof (s_xbt_sha_t));
45 sha->hash[0] = 0x67452301;
46 sha->hash[1] = 0xefcdab89;
47 sha->hash[2] = 0x98badcfe;
48 sha->hash[3] = 0x10325476;
49 sha->hash[4] = 0xc3d2e1f0;
52 /* @brief Add some more data to the buffer */
53 void xbt_sha_feed (xbt_sha_t sha, const unsigned char *data, size_t len) {
56 for (i=0; i<len; i++) {
57 sha->buf[sha->blen / 4] <<= 8;
58 sha->buf[sha->blen / 4] |= (unsigned int)data[i];
59 if ((++sha->blen) % 64 == 0) {
64 sha->lenhi += (sha->lenlo < 8);
68 /* finalize computation before displaying the result */
69 static void xbt_sha_finalize (xbt_sha_t sha) {
73 pad[0] = (unsigned char)((sha->lenhi >> 24) & 0xff);
74 pad[1] = (unsigned char)((sha->lenhi >> 16) & 0xff);
75 pad[2] = (unsigned char)((sha->lenhi >> 8) & 0xff);
76 pad[3] = (unsigned char)(sha->lenhi & 0xff);
77 pad[4] = (unsigned char)((sha->lenlo >> 24) & 0xff);
78 pad[5] = (unsigned char)((sha->lenlo >> 16) & 0xff);
79 pad[6] = (unsigned char)((sha->lenlo >> 8) & 0xff);
80 pad[7] = (unsigned char)(sha->lenlo & 255);
83 xbt_sha_feed (sha, &padc, 1);
86 while (sha->blen != 56)
87 xbt_sha_feed (sha, &padc, 1);
89 xbt_sha_feed (sha, pad, 8);
92 /** @brief returns the sha hash into a newly allocated buffer (+ reset sha object) */
93 char *xbt_sha_read(xbt_sha_t sha) {
94 char *res = xbt_malloc(40);
95 xbt_sha_print(sha,res);
99 /** @brief copy the content sha hash into the @a hash pre-allocated string (and reset buffer) */
100 void xbt_sha_print (xbt_sha_t sha, char *hash) {
103 xbt_sha_finalize(sha);
104 for (i=0; i<5; i++) {
105 sprintf (hash, "%08x", sha->hash[i]);
111 /** @brief simply compute a SHA1 hash and copy it to the provided buffer */
112 void xbt_sha (const char *data, char *hash) {
115 xbt_sha_reset (&sha);
116 xbt_sha_feed (&sha, (const unsigned char*)data, strlen (data));
118 xbt_sha_print (&sha, hash);
121 /* ********************* */
122 /* Core of the algorithm */
123 /* ********************* */
125 #define SRL(x,y) (((x) << (y)) | ((x) >> (32-(y))))
126 #define SHA(a,b,f,c) \
127 for (i= (a) ; i<= (b) ; i++) { \
128 TMP = SRL(A,5) + ( (f) ) + E + sha->buf[i] + (c) ; \
136 static void sha_calculate (xbt_sha_t sha)
139 unsigned int A, B, C, D, E, TMP;
141 for (i=16; i<80; i++)
142 sha->buf[i] = SRL (sha->buf[i-3] ^ sha->buf[i-8] ^ sha->buf[i-14] ^ sha->buf[i-16], 1);
150 SHA (0, 19, ((C^D)&B)^D, 0x5a827999);
151 SHA (20, 39, B^C^D, 0x6ed9eba1);
152 SHA (40, 59, (B&C)|(D&(B|C)), 0x8f1bbcdc);
153 SHA (60, 79, B^C^D, 0xca62c1d6);
166 #include "xbt/hash.h"
167 #include "portable.h" /* hexa_str */
169 static char* mycmp(const char *p1, const char *p2,size_t n) {
172 for (i=0; i<n; i++) {
173 if (p1[i] != p2[i]) {
174 return bprintf("Differs on %d -- Ox%x",i,p1[i]);
177 return xbt_strdup("");
180 static void test_sha(const char *clear,const char *hashed) {
184 xbt_test_add1("==== Test with '%s'",clear);
185 xbt_test_assert3(!memcmp(hash, hashed, 40), "Wrong sha: %40s!=%40s (%s)",
186 hash,hashed,mycmp(hash,hashed,40));
189 XBT_TEST_SUITE("hash","Various hash functions");
191 XBT_LOG_NEW_DEFAULT_CATEGORY(hash,"Tests of various hash functions ");
194 XBT_TEST_UNIT("sha",test_crypto_sha,"Test of the sha algorithm") {
195 /* Empty string as test vector */
196 test_sha("", "da39a3ee5e6b4b0d3255bfef95601890afd80709");
198 /* Some pangram as test vector */
199 test_sha("The quick brown fox jumps over the lazy dog", "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12");
200 test_sha("Woven silk pyjamas exchanged for blue quartz","da3aff337c810c6470db4dbf0f205c8afc31c442");
201 test_sha("Pack my box with five dozen liquor jugs", "373ba8be29d4d95708bf7cd43038f4e409dcb439");
204 #endif /* SIMGRID_TEST */