xref: /netbsd-src/crypto/external/bsd/netpgp/dist/src/netpgpverify/sha1.c (revision dd98b26d9b747061a6a9c2243c42b44a36f58989)
1*dd98b26dSagc /*	$NetBSD: sha1.c,v 1.2 2016/06/14 20:47:08 agc Exp $	*/
225f78d91Sagc /*	$OpenBSD: sha1.c,v 1.9 1997/07/23 21:12:32 kstailey Exp $	*/
325f78d91Sagc 
425f78d91Sagc /*
525f78d91Sagc  * SHA-1 in C
625f78d91Sagc  * By Steve Reid <steve@edmweb.com>
725f78d91Sagc  * 100% Public Domain
825f78d91Sagc  *
925f78d91Sagc  * Test Vectors (from FIPS PUB 180-1)
1025f78d91Sagc  * "abc"
1125f78d91Sagc  *   A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
1225f78d91Sagc  * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
1325f78d91Sagc  *   84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
1425f78d91Sagc  * A million repetitions of "a"
1525f78d91Sagc  *   34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
1625f78d91Sagc  */
1725f78d91Sagc 
1825f78d91Sagc #define SHA1HANDSOFF		/* Copies data before messing with it. */
1925f78d91Sagc 
2025f78d91Sagc #include <string.h>
2125f78d91Sagc 
2225f78d91Sagc #include <sys/types.h>
2325f78d91Sagc 
2425f78d91Sagc #include "sha1.h"
2525f78d91Sagc 
2625f78d91Sagc #if !HAVE_SHA1_H
2725f78d91Sagc 
2825f78d91Sagc #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
2925f78d91Sagc 
3025f78d91Sagc /*
3125f78d91Sagc  * blk0() and blk() perform the initial expand.
3225f78d91Sagc  * I got the idea of expanding during the round function from SSLeay
3325f78d91Sagc  */
3425f78d91Sagc #if BYTE_ORDER == LITTLE_ENDIAN
3525f78d91Sagc # define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
3625f78d91Sagc     |(rol(block->l[i],8)&0x00FF00FF))
3725f78d91Sagc #else
3825f78d91Sagc # define blk0(i) block->l[i]
3925f78d91Sagc #endif
4025f78d91Sagc #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
4125f78d91Sagc     ^block->l[(i+2)&15]^block->l[i&15],1))
4225f78d91Sagc 
4325f78d91Sagc /*
4425f78d91Sagc  * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1
4525f78d91Sagc  */
4625f78d91Sagc #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
4725f78d91Sagc #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
4825f78d91Sagc #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
4925f78d91Sagc #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
5025f78d91Sagc #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
5125f78d91Sagc 
5225f78d91Sagc 
5325f78d91Sagc typedef union {
5425f78d91Sagc     uint8_t c[64];
5525f78d91Sagc     uint32_t l[16];
5625f78d91Sagc } CHAR64LONG16;
5725f78d91Sagc 
5825f78d91Sagc /* old sparc64 gcc could not compile this */
5925f78d91Sagc #undef SPARC64_GCC_WORKAROUND
6025f78d91Sagc #if defined(__sparc64__) && defined(__GNUC__) && __GNUC__ < 3
6125f78d91Sagc #define SPARC64_GCC_WORKAROUND
6225f78d91Sagc #endif
6325f78d91Sagc 
6425f78d91Sagc #ifdef SPARC64_GCC_WORKAROUND
6525f78d91Sagc void do_R01(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *);
6625f78d91Sagc void do_R2(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *);
6725f78d91Sagc void do_R3(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *);
6825f78d91Sagc void do_R4(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *);
6925f78d91Sagc 
7025f78d91Sagc #define nR0(v,w,x,y,z,i) R0(*v,*w,*x,*y,*z,i)
7125f78d91Sagc #define nR1(v,w,x,y,z,i) R1(*v,*w,*x,*y,*z,i)
7225f78d91Sagc #define nR2(v,w,x,y,z,i) R2(*v,*w,*x,*y,*z,i)
7325f78d91Sagc #define nR3(v,w,x,y,z,i) R3(*v,*w,*x,*y,*z,i)
7425f78d91Sagc #define nR4(v,w,x,y,z,i) R4(*v,*w,*x,*y,*z,i)
7525f78d91Sagc 
7625f78d91Sagc void
do_R01(uint32_t * a,uint32_t * b,uint32_t * c,uint32_t * d,uint32_t * e,CHAR64LONG16 * block)7725f78d91Sagc do_R01(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *block)
7825f78d91Sagc {
7925f78d91Sagc     nR0(a,b,c,d,e, 0); nR0(e,a,b,c,d, 1); nR0(d,e,a,b,c, 2); nR0(c,d,e,a,b, 3);
8025f78d91Sagc     nR0(b,c,d,e,a, 4); nR0(a,b,c,d,e, 5); nR0(e,a,b,c,d, 6); nR0(d,e,a,b,c, 7);
8125f78d91Sagc     nR0(c,d,e,a,b, 8); nR0(b,c,d,e,a, 9); nR0(a,b,c,d,e,10); nR0(e,a,b,c,d,11);
8225f78d91Sagc     nR0(d,e,a,b,c,12); nR0(c,d,e,a,b,13); nR0(b,c,d,e,a,14); nR0(a,b,c,d,e,15);
8325f78d91Sagc     nR1(e,a,b,c,d,16); nR1(d,e,a,b,c,17); nR1(c,d,e,a,b,18); nR1(b,c,d,e,a,19);
8425f78d91Sagc }
8525f78d91Sagc 
8625f78d91Sagc void
do_R2(uint32_t * a,uint32_t * b,uint32_t * c,uint32_t * d,uint32_t * e,CHAR64LONG16 * block)8725f78d91Sagc do_R2(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *block)
8825f78d91Sagc {
8925f78d91Sagc     nR2(a,b,c,d,e,20); nR2(e,a,b,c,d,21); nR2(d,e,a,b,c,22); nR2(c,d,e,a,b,23);
9025f78d91Sagc     nR2(b,c,d,e,a,24); nR2(a,b,c,d,e,25); nR2(e,a,b,c,d,26); nR2(d,e,a,b,c,27);
9125f78d91Sagc     nR2(c,d,e,a,b,28); nR2(b,c,d,e,a,29); nR2(a,b,c,d,e,30); nR2(e,a,b,c,d,31);
9225f78d91Sagc     nR2(d,e,a,b,c,32); nR2(c,d,e,a,b,33); nR2(b,c,d,e,a,34); nR2(a,b,c,d,e,35);
9325f78d91Sagc     nR2(e,a,b,c,d,36); nR2(d,e,a,b,c,37); nR2(c,d,e,a,b,38); nR2(b,c,d,e,a,39);
9425f78d91Sagc }
9525f78d91Sagc 
9625f78d91Sagc void
do_R3(uint32_t * a,uint32_t * b,uint32_t * c,uint32_t * d,uint32_t * e,CHAR64LONG16 * block)9725f78d91Sagc do_R3(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *block)
9825f78d91Sagc {
9925f78d91Sagc     nR3(a,b,c,d,e,40); nR3(e,a,b,c,d,41); nR3(d,e,a,b,c,42); nR3(c,d,e,a,b,43);
10025f78d91Sagc     nR3(b,c,d,e,a,44); nR3(a,b,c,d,e,45); nR3(e,a,b,c,d,46); nR3(d,e,a,b,c,47);
10125f78d91Sagc     nR3(c,d,e,a,b,48); nR3(b,c,d,e,a,49); nR3(a,b,c,d,e,50); nR3(e,a,b,c,d,51);
10225f78d91Sagc     nR3(d,e,a,b,c,52); nR3(c,d,e,a,b,53); nR3(b,c,d,e,a,54); nR3(a,b,c,d,e,55);
10325f78d91Sagc     nR3(e,a,b,c,d,56); nR3(d,e,a,b,c,57); nR3(c,d,e,a,b,58); nR3(b,c,d,e,a,59);
10425f78d91Sagc }
10525f78d91Sagc 
10625f78d91Sagc void
do_R4(uint32_t * a,uint32_t * b,uint32_t * c,uint32_t * d,uint32_t * e,CHAR64LONG16 * block)10725f78d91Sagc do_R4(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *block)
10825f78d91Sagc {
10925f78d91Sagc     nR4(a,b,c,d,e,60); nR4(e,a,b,c,d,61); nR4(d,e,a,b,c,62); nR4(c,d,e,a,b,63);
11025f78d91Sagc     nR4(b,c,d,e,a,64); nR4(a,b,c,d,e,65); nR4(e,a,b,c,d,66); nR4(d,e,a,b,c,67);
11125f78d91Sagc     nR4(c,d,e,a,b,68); nR4(b,c,d,e,a,69); nR4(a,b,c,d,e,70); nR4(e,a,b,c,d,71);
11225f78d91Sagc     nR4(d,e,a,b,c,72); nR4(c,d,e,a,b,73); nR4(b,c,d,e,a,74); nR4(a,b,c,d,e,75);
11325f78d91Sagc     nR4(e,a,b,c,d,76); nR4(d,e,a,b,c,77); nR4(c,d,e,a,b,78); nR4(b,c,d,e,a,79);
11425f78d91Sagc }
11525f78d91Sagc #endif
11625f78d91Sagc 
11725f78d91Sagc /*
11825f78d91Sagc  * Hash a single 512-bit block. This is the core of the algorithm.
11925f78d91Sagc  */
netpgpv_SHA1Transform(uint32_t state[5],const uint8_t buffer[64])120*dd98b26dSagc void netpgpv_SHA1Transform(uint32_t state[5], const uint8_t buffer[64])
12125f78d91Sagc {
12225f78d91Sagc     uint32_t a, b, c, d, e;
12325f78d91Sagc     CHAR64LONG16 *block;
12425f78d91Sagc 
12525f78d91Sagc #ifdef SHA1HANDSOFF
12625f78d91Sagc     CHAR64LONG16 workspace;
12725f78d91Sagc #endif
12825f78d91Sagc 
12925f78d91Sagc #ifdef SHA1HANDSOFF
13025f78d91Sagc     block = &workspace;
13125f78d91Sagc     (void)memcpy(block, buffer, 64);
13225f78d91Sagc #else
13325f78d91Sagc     block = (CHAR64LONG16 *)(void *)buffer;
13425f78d91Sagc #endif
13525f78d91Sagc 
13625f78d91Sagc     /* Copy context->state[] to working vars */
13725f78d91Sagc     a = state[0];
13825f78d91Sagc     b = state[1];
13925f78d91Sagc     c = state[2];
14025f78d91Sagc     d = state[3];
14125f78d91Sagc     e = state[4];
14225f78d91Sagc 
14325f78d91Sagc #ifdef SPARC64_GCC_WORKAROUND
14425f78d91Sagc     do_R01(&a, &b, &c, &d, &e, block);
14525f78d91Sagc     do_R2(&a, &b, &c, &d, &e, block);
14625f78d91Sagc     do_R3(&a, &b, &c, &d, &e, block);
14725f78d91Sagc     do_R4(&a, &b, &c, &d, &e, block);
14825f78d91Sagc #else
14925f78d91Sagc     /* 4 rounds of 20 operations each. Loop unrolled. */
15025f78d91Sagc     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);
15125f78d91Sagc     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);
15225f78d91Sagc     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);
15325f78d91Sagc     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);
15425f78d91Sagc     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);
15525f78d91Sagc     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);
15625f78d91Sagc     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);
15725f78d91Sagc     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);
15825f78d91Sagc     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);
15925f78d91Sagc     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);
16025f78d91Sagc     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);
16125f78d91Sagc     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);
16225f78d91Sagc     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);
16325f78d91Sagc     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);
16425f78d91Sagc     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);
16525f78d91Sagc     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);
16625f78d91Sagc     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);
16725f78d91Sagc     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);
16825f78d91Sagc     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);
16925f78d91Sagc     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);
17025f78d91Sagc #endif
17125f78d91Sagc 
17225f78d91Sagc     /* Add the working vars back into context.state[] */
17325f78d91Sagc     state[0] += a;
17425f78d91Sagc     state[1] += b;
17525f78d91Sagc     state[2] += c;
17625f78d91Sagc     state[3] += d;
17725f78d91Sagc     state[4] += e;
17825f78d91Sagc 
17925f78d91Sagc     /* Wipe variables */
18025f78d91Sagc     a = b = c = d = e = 0;
18125f78d91Sagc }
18225f78d91Sagc 
18325f78d91Sagc 
18425f78d91Sagc /*
18525f78d91Sagc  * SHA1Init - Initialize new context
18625f78d91Sagc  */
netpgpv_SHA1Init(NETPGPV_SHA1_CTX * context)187*dd98b26dSagc void netpgpv_SHA1Init(NETPGPV_SHA1_CTX *context)
18825f78d91Sagc {
18925f78d91Sagc 
19025f78d91Sagc     /* SHA1 initialization constants */
19125f78d91Sagc     context->state[0] = 0x67452301;
19225f78d91Sagc     context->state[1] = 0xEFCDAB89;
19325f78d91Sagc     context->state[2] = 0x98BADCFE;
19425f78d91Sagc     context->state[3] = 0x10325476;
19525f78d91Sagc     context->state[4] = 0xC3D2E1F0;
19625f78d91Sagc     context->count[0] = context->count[1] = 0;
19725f78d91Sagc }
19825f78d91Sagc 
19925f78d91Sagc 
20025f78d91Sagc /*
20125f78d91Sagc  * Run your data through this.
20225f78d91Sagc  */
netpgpv_SHA1Update(NETPGPV_SHA1_CTX * context,const uint8_t * data,unsigned int len)203*dd98b26dSagc void netpgpv_SHA1Update(NETPGPV_SHA1_CTX *context, const uint8_t *data, unsigned int len)
20425f78d91Sagc {
20525f78d91Sagc     unsigned int i, j;
20625f78d91Sagc 
20725f78d91Sagc     j = context->count[0];
20825f78d91Sagc     if ((context->count[0] += len << 3) < j)
20925f78d91Sagc 	context->count[1] += (len>>29)+1;
21025f78d91Sagc     j = (j >> 3) & 63;
21125f78d91Sagc     if ((j + len) > 63) {
21225f78d91Sagc 	(void)memcpy(&context->buffer[j], data, (i = 64-j));
213*dd98b26dSagc 	netpgpv_SHA1Transform(context->state, context->buffer);
21425f78d91Sagc 	for ( ; i + 63 < len; i += 64)
215*dd98b26dSagc 	    netpgpv_SHA1Transform(context->state, &data[i]);
21625f78d91Sagc 	j = 0;
21725f78d91Sagc     } else {
21825f78d91Sagc 	i = 0;
21925f78d91Sagc     }
22025f78d91Sagc     (void)memcpy(&context->buffer[j], &data[i], len - i);
22125f78d91Sagc }
22225f78d91Sagc 
22325f78d91Sagc 
22425f78d91Sagc /*
22525f78d91Sagc  * Add padding and return the message digest.
22625f78d91Sagc  */
netpgpv_SHA1Final(uint8_t digest[20],NETPGPV_SHA1_CTX * context)227*dd98b26dSagc void netpgpv_SHA1Final(uint8_t digest[20], NETPGPV_SHA1_CTX *context)
22825f78d91Sagc {
22925f78d91Sagc     unsigned int i;
23025f78d91Sagc     uint8_t finalcount[8];
23125f78d91Sagc 
23225f78d91Sagc     for (i = 0; i < 8; i++) {
23325f78d91Sagc 	finalcount[i] = (uint8_t)((context->count[(i >= 4 ? 0 : 1)]
23425f78d91Sagc 	 >> ((3-(i & 3)) * 8) ) & 255);	 /* Endian independent */
23525f78d91Sagc     }
236*dd98b26dSagc     netpgpv_SHA1Update(context, (const uint8_t *)"\200", 1);
23725f78d91Sagc     while ((context->count[0] & 504) != 448)
238*dd98b26dSagc 	netpgpv_SHA1Update(context, (const uint8_t *)"\0", 1);
239*dd98b26dSagc     netpgpv_SHA1Update(context, finalcount, 8);  /* Should cause a SHA1Transform() */
24025f78d91Sagc 
24125f78d91Sagc     if (digest) {
24225f78d91Sagc 	for (i = 0; i < 20; i++)
24325f78d91Sagc 	    digest[i] = (uint8_t)
24425f78d91Sagc 		((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
24525f78d91Sagc     }
24625f78d91Sagc }
24725f78d91Sagc 
24825f78d91Sagc #endif /* HAVE_SHA1_H */
249