sthen 03d3f8319b import ikesemel 1.2
iksemel is an LGPL-licensed XML parser library mainly designed for
Jabber applications.

ok steven@
2007-09-04 15:05:27 +00:00

142 lines
3.2 KiB
Plaintext

$OpenBSD: patch-src_sha_c,v 1.1.1.1 2007/09/04 15:05:27 sthen Exp $
--- src/sha.c.orig Wed Apr 28 20:35:57 2004
+++ src/sha.c Thu Jul 26 16:28:32 2007
@@ -4,17 +4,14 @@
** modify it under the terms of GNU Lesser General Public License.
*/
+#include <sys/types.h>
+#include <sha1.h>
+
#include "common.h"
#include "iksemel.h"
-static void sha_buffer (iksha *sha, const unsigned char *data, int len);
-static void sha_calculate (iksha *sha);
-
struct iksha_struct {
- unsigned long hash[5];
- unsigned long buf[80];
- int blen;
- unsigned long lenhi, lenlo;
+ SHA1_CTX context;
};
iksha *
@@ -32,51 +29,20 @@ void
iks_sha_reset (iksha *sha)
{
memset (sha, 0, sizeof (iksha));
- sha->hash[0] = 0x67452301L;
- sha->hash[1] = 0xefcdab89L;
- sha->hash[2] = 0x98badcfeL;
- sha->hash[3] = 0x10325476L;
- sha->hash[4] = 0xc3d2e1f0L;
+ SHA1Init(&sha->context);
}
void
iks_sha_hash (iksha *sha, const unsigned char *data, size_t len, int finish)
{
- unsigned char pad[8];
- unsigned char padc;
-
- if (data && len != 0) sha_buffer (sha, data, len);
- if (!finish) return;
-
- pad[0] = (unsigned char)((sha->lenhi >> 24) & 0xff);
- pad[1] = (unsigned char)((sha->lenhi >> 16) & 0xff);
- pad[2] = (unsigned char)((sha->lenhi >> 8) & 0xff);
- pad[3] = (unsigned char)(sha->lenhi & 0xff);
- pad[4] = (unsigned char)((sha->lenlo >> 24) & 0xff);
- pad[5] = (unsigned char)((sha->lenlo >> 16) & 0xff);
- pad[6] = (unsigned char)((sha->lenlo >> 8) & 0xff);
- pad[7] = (unsigned char)(sha->lenlo & 255);
-
- padc = 0x80;
- sha_buffer (sha, &padc, 1);
-
- padc = 0x00;
- while (sha->blen != 56)
- sha_buffer (sha, &padc, 1);
-
- sha_buffer (sha, pad, 8);
+ if (data && len != 0)
+ SHA1Update(&sha->context, data, len);
}
void
iks_sha_print (iksha *sha, char *hash)
{
- int i;
-
- for (i=0; i<5; i++)
- {
- sprintf (hash, "%08lx", sha->hash[i]);
- hash += 8;
- }
+ SHA1End(&sha->context, hash);
}
void
@@ -94,59 +60,4 @@ iks_sha (const char *data, char *hash)
iks_sha_hash (sha, data, strlen (data), 1);
iks_sha_print (sha, hash);
iks_free (sha);
-}
-
-static void
-sha_buffer (iksha *sha, const unsigned char *data, int len)
-{
- int i;
-
- for (i=0; i<len; i++) {
- sha->buf[sha->blen / 4] <<= 8;
- sha->buf[sha->blen / 4] |= (unsigned long)data[i];
- if ((++sha->blen) % 64 == 0) {
- sha_calculate (sha);
- sha->blen = 0;
- }
- sha->lenlo += 8;
- sha->lenhi += (sha->lenlo < 8);
- }
-}
-
-#define SRL(x,y) (((x) << (y)) | ((x) >> (32-(y))))
-#define SHA(a,b,f,c) \
- for (i= (a) ; i<= (b) ; i++) { \
- TMP = SRL(A,5) + ( (f) ) + E + sha->buf[i] + (c) ; \
- E = D; \
- D = C; \
- C = SRL(B,30); \
- B = A; \
- A = TMP; \
- }
-
-static void
-sha_calculate (iksha *sha)
-{
- int i;
- unsigned long A, B, C, D, E, TMP;
-
- for (i=16; i<80; i++)
- sha->buf[i] = SRL (sha->buf[i-3] ^ sha->buf[i-8] ^ sha->buf[i-14] ^ sha->buf[i-16], 1);
-
- A = sha->hash[0];
- B = sha->hash[1];
- C = sha->hash[2];
- D = sha->hash[3];
- E = sha->hash[4];
-
- SHA (0, 19, ((C^D)&B)^D, 0x5a827999L);
- SHA (20, 39, B^C^D, 0x6ed9eba1L);
- SHA (40, 59, (B&C)|(D&(B|C)), 0x8f1bbcdcL);
- SHA (60, 79, B^C^D, 0xca62c1d6L);
-
- sha->hash[0] += A;
- sha->hash[1] += B;
- sha->hash[2] += C;
- sha->hash[3] += D;
- sha->hash[4] += E;
}