17b5038d7SDag-Erling Smørgrav /*
27b5038d7SDag-Erling Smørgrav * modified for ldns by Jelte Jansen, original taken from OpenBSD:
37b5038d7SDag-Erling Smørgrav *
47b5038d7SDag-Erling Smørgrav * SHA-1 in C
57b5038d7SDag-Erling Smørgrav * By Steve Reid <steve@edmweb.com>
67b5038d7SDag-Erling Smørgrav * 100% Public Domain
77b5038d7SDag-Erling Smørgrav *
87b5038d7SDag-Erling Smørgrav * Test Vectors (from FIPS PUB 180-1)
97b5038d7SDag-Erling Smørgrav * "abc"
107b5038d7SDag-Erling Smørgrav * A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
117b5038d7SDag-Erling Smørgrav * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
127b5038d7SDag-Erling Smørgrav * 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
137b5038d7SDag-Erling Smørgrav * A million repetitions of "a"
147b5038d7SDag-Erling Smørgrav * 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
157b5038d7SDag-Erling Smørgrav */
167b5038d7SDag-Erling Smørgrav
177b5038d7SDag-Erling Smørgrav /* #define LITTLE_ENDIAN * This should be #define'd already, if true. */
187b5038d7SDag-Erling Smørgrav
197b5038d7SDag-Erling Smørgrav #include <ldns/config.h>
207b5038d7SDag-Erling Smørgrav #include <ldns/ldns.h>
217b5038d7SDag-Erling Smørgrav #include <strings.h>
227b5038d7SDag-Erling Smørgrav
2317d15b25SDag-Erling Smørgrav #define SHA1HANDSOFF 1 /* Copies data before messing with it. */
247b5038d7SDag-Erling Smørgrav #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
257b5038d7SDag-Erling Smørgrav
267b5038d7SDag-Erling Smørgrav /* blk0() and blk() perform the initial expand. */
277b5038d7SDag-Erling Smørgrav /* I got the idea of expanding during the round function from SSLeay */
287b5038d7SDag-Erling Smørgrav #if BYTE_ORDER == LITTLE_ENDIAN
297b5038d7SDag-Erling Smørgrav #define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
307b5038d7SDag-Erling Smørgrav |(rol(block->l[i],8)&0x00FF00FF))
317b5038d7SDag-Erling Smørgrav #else
327b5038d7SDag-Erling Smørgrav #define blk0(i) block->l[i]
337b5038d7SDag-Erling Smørgrav #endif
347b5038d7SDag-Erling Smørgrav #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
357b5038d7SDag-Erling Smørgrav ^block->l[(i+2)&15]^block->l[i&15],1))
367b5038d7SDag-Erling Smørgrav
377b5038d7SDag-Erling Smørgrav /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
387b5038d7SDag-Erling Smørgrav #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
397b5038d7SDag-Erling Smørgrav #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
407b5038d7SDag-Erling Smørgrav #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
417b5038d7SDag-Erling Smørgrav #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
427b5038d7SDag-Erling Smørgrav #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
437b5038d7SDag-Erling Smørgrav
447b5038d7SDag-Erling Smørgrav /* Hash a single 512-bit block. This is the core of the algorithm. */
457b5038d7SDag-Erling Smørgrav
467b5038d7SDag-Erling Smørgrav void
ldns_sha1_transform(uint32_t state[5],const unsigned char buffer[LDNS_SHA1_BLOCK_LENGTH])477b5038d7SDag-Erling Smørgrav ldns_sha1_transform(uint32_t state[5], const unsigned char buffer[LDNS_SHA1_BLOCK_LENGTH])
487b5038d7SDag-Erling Smørgrav {
497b5038d7SDag-Erling Smørgrav uint32_t a, b, c, d, e;
507b5038d7SDag-Erling Smørgrav typedef union {
517b5038d7SDag-Erling Smørgrav unsigned char c[64];
527b5038d7SDag-Erling Smørgrav unsigned int l[16];
537b5038d7SDag-Erling Smørgrav } CHAR64LONG16;
547b5038d7SDag-Erling Smørgrav CHAR64LONG16* block;
557b5038d7SDag-Erling Smørgrav #ifdef SHA1HANDSOFF
567b5038d7SDag-Erling Smørgrav unsigned char workspace[LDNS_SHA1_BLOCK_LENGTH];
577b5038d7SDag-Erling Smørgrav
587b5038d7SDag-Erling Smørgrav block = (CHAR64LONG16 *)workspace;
597b5038d7SDag-Erling Smørgrav memmove(block, buffer, LDNS_SHA1_BLOCK_LENGTH);
607b5038d7SDag-Erling Smørgrav #else
617b5038d7SDag-Erling Smørgrav block = (CHAR64LONG16 *)buffer;
627b5038d7SDag-Erling Smørgrav #endif
637b5038d7SDag-Erling Smørgrav /* Copy context->state[] to working vars */
647b5038d7SDag-Erling Smørgrav a = state[0];
657b5038d7SDag-Erling Smørgrav b = state[1];
667b5038d7SDag-Erling Smørgrav c = state[2];
677b5038d7SDag-Erling Smørgrav d = state[3];
687b5038d7SDag-Erling Smørgrav e = state[4];
697b5038d7SDag-Erling Smørgrav
707b5038d7SDag-Erling Smørgrav /* 4 rounds of 20 operations each. Loop unrolled. */
717b5038d7SDag-Erling Smørgrav R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
727b5038d7SDag-Erling Smørgrav R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
737b5038d7SDag-Erling Smørgrav R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
747b5038d7SDag-Erling Smørgrav R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
757b5038d7SDag-Erling Smørgrav R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
767b5038d7SDag-Erling Smørgrav R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
777b5038d7SDag-Erling Smørgrav R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
787b5038d7SDag-Erling Smørgrav R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
797b5038d7SDag-Erling Smørgrav R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
807b5038d7SDag-Erling Smørgrav R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
817b5038d7SDag-Erling Smørgrav R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
827b5038d7SDag-Erling Smørgrav R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
837b5038d7SDag-Erling Smørgrav R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
847b5038d7SDag-Erling Smørgrav R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
857b5038d7SDag-Erling Smørgrav R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
867b5038d7SDag-Erling Smørgrav R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
877b5038d7SDag-Erling Smørgrav R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
887b5038d7SDag-Erling Smørgrav R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
897b5038d7SDag-Erling Smørgrav R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
907b5038d7SDag-Erling Smørgrav R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
917b5038d7SDag-Erling Smørgrav
927b5038d7SDag-Erling Smørgrav /* Add the working vars back into context.state[] */
937b5038d7SDag-Erling Smørgrav state[0] += a;
947b5038d7SDag-Erling Smørgrav state[1] += b;
957b5038d7SDag-Erling Smørgrav state[2] += c;
967b5038d7SDag-Erling Smørgrav state[3] += d;
977b5038d7SDag-Erling Smørgrav state[4] += e;
987b5038d7SDag-Erling Smørgrav /* Wipe variables */
997b5038d7SDag-Erling Smørgrav a = b = c = d = e = 0;
100*5afab0e5SDag-Erling Smørgrav (void)a;
1017b5038d7SDag-Erling Smørgrav }
1027b5038d7SDag-Erling Smørgrav
1037b5038d7SDag-Erling Smørgrav
1047b5038d7SDag-Erling Smørgrav /* SHA1Init - Initialize new context */
1057b5038d7SDag-Erling Smørgrav
1067b5038d7SDag-Erling Smørgrav void
ldns_sha1_init(ldns_sha1_ctx * context)1077b5038d7SDag-Erling Smørgrav ldns_sha1_init(ldns_sha1_ctx *context)
1087b5038d7SDag-Erling Smørgrav {
1097b5038d7SDag-Erling Smørgrav /* SHA1 initialization constants */
1107b5038d7SDag-Erling Smørgrav context->count = 0;
1117b5038d7SDag-Erling Smørgrav context->state[0] = 0x67452301;
1127b5038d7SDag-Erling Smørgrav context->state[1] = 0xEFCDAB89;
1137b5038d7SDag-Erling Smørgrav context->state[2] = 0x98BADCFE;
1147b5038d7SDag-Erling Smørgrav context->state[3] = 0x10325476;
1157b5038d7SDag-Erling Smørgrav context->state[4] = 0xC3D2E1F0;
1167b5038d7SDag-Erling Smørgrav }
1177b5038d7SDag-Erling Smørgrav
1187b5038d7SDag-Erling Smørgrav
1197b5038d7SDag-Erling Smørgrav /* Run your data through this. */
1207b5038d7SDag-Erling Smørgrav
1217b5038d7SDag-Erling Smørgrav void
ldns_sha1_update(ldns_sha1_ctx * context,const unsigned char * data,unsigned int len)1227b5038d7SDag-Erling Smørgrav ldns_sha1_update(ldns_sha1_ctx *context, const unsigned char *data, unsigned int len)
1237b5038d7SDag-Erling Smørgrav {
1247b5038d7SDag-Erling Smørgrav unsigned int i;
1257b5038d7SDag-Erling Smørgrav unsigned int j;
1267b5038d7SDag-Erling Smørgrav
1277b5038d7SDag-Erling Smørgrav j = (unsigned)(uint32_t)((context->count >> 3) & 63);
1287b5038d7SDag-Erling Smørgrav context->count += (len << 3);
1297b5038d7SDag-Erling Smørgrav if ((j + len) > 63) {
1307b5038d7SDag-Erling Smørgrav memmove(&context->buffer[j], data, (i = 64 - j));
1317b5038d7SDag-Erling Smørgrav ldns_sha1_transform(context->state, context->buffer);
1327b5038d7SDag-Erling Smørgrav for ( ; i + 63 < len; i += 64) {
1337b5038d7SDag-Erling Smørgrav ldns_sha1_transform(context->state, &data[i]);
1347b5038d7SDag-Erling Smørgrav }
1357b5038d7SDag-Erling Smørgrav j = 0;
1367b5038d7SDag-Erling Smørgrav }
1377b5038d7SDag-Erling Smørgrav else i = 0;
1387b5038d7SDag-Erling Smørgrav memmove(&context->buffer[j], &data[i], len - i);
1397b5038d7SDag-Erling Smørgrav }
1407b5038d7SDag-Erling Smørgrav
1417b5038d7SDag-Erling Smørgrav
1427b5038d7SDag-Erling Smørgrav /* Add padding and return the message digest. */
1437b5038d7SDag-Erling Smørgrav
1447b5038d7SDag-Erling Smørgrav void
ldns_sha1_final(unsigned char digest[LDNS_SHA1_DIGEST_LENGTH],ldns_sha1_ctx * context)1457b5038d7SDag-Erling Smørgrav ldns_sha1_final(unsigned char digest[LDNS_SHA1_DIGEST_LENGTH], ldns_sha1_ctx *context)
1467b5038d7SDag-Erling Smørgrav {
1477b5038d7SDag-Erling Smørgrav unsigned int i;
1487b5038d7SDag-Erling Smørgrav unsigned char finalcount[8];
1497b5038d7SDag-Erling Smørgrav
1507b5038d7SDag-Erling Smørgrav for (i = 0; i < 8; i++) {
1517b5038d7SDag-Erling Smørgrav finalcount[i] = (unsigned char)((context->count >>
1527b5038d7SDag-Erling Smørgrav ((7 - (i & 7)) * 8)) & 255); /* Endian independent */
1537b5038d7SDag-Erling Smørgrav }
1547b5038d7SDag-Erling Smørgrav ldns_sha1_update(context, (unsigned char *)"\200", 1);
1557b5038d7SDag-Erling Smørgrav while ((context->count & 504) != 448) {
1567b5038d7SDag-Erling Smørgrav ldns_sha1_update(context, (unsigned char *)"\0", 1);
1577b5038d7SDag-Erling Smørgrav }
1587b5038d7SDag-Erling Smørgrav ldns_sha1_update(context, finalcount, 8); /* Should cause a SHA1Transform() */
1597b5038d7SDag-Erling Smørgrav
1607b5038d7SDag-Erling Smørgrav if (digest != NULL)
1617b5038d7SDag-Erling Smørgrav for (i = 0; i < LDNS_SHA1_DIGEST_LENGTH; i++) {
1627b5038d7SDag-Erling Smørgrav digest[i] = (unsigned char)((context->state[i >> 2] >>
1637b5038d7SDag-Erling Smørgrav ((3 - (i & 3)) * 8)) & 255);
1647b5038d7SDag-Erling Smørgrav }
1657b5038d7SDag-Erling Smørgrav #ifdef SHA1HANDSOFF /* make SHA1Transform overwrite its own static vars */
1667b5038d7SDag-Erling Smørgrav ldns_sha1_transform(context->state, context->buffer);
1677b5038d7SDag-Erling Smørgrav #endif
1687b5038d7SDag-Erling Smørgrav }
1697b5038d7SDag-Erling Smørgrav
1707b5038d7SDag-Erling Smørgrav unsigned char *
ldns_sha1(const unsigned char * data,unsigned int data_len,unsigned char * digest)171*5afab0e5SDag-Erling Smørgrav ldns_sha1(const unsigned char *data, unsigned int data_len, unsigned char *digest)
1727b5038d7SDag-Erling Smørgrav {
1737b5038d7SDag-Erling Smørgrav ldns_sha1_ctx ctx;
1747b5038d7SDag-Erling Smørgrav ldns_sha1_init(&ctx);
1757b5038d7SDag-Erling Smørgrav ldns_sha1_update(&ctx, data, data_len);
1767b5038d7SDag-Erling Smørgrav ldns_sha1_final(digest, &ctx);
1777b5038d7SDag-Erling Smørgrav return digest;
1787b5038d7SDag-Erling Smørgrav }
179