xref: /netbsd-src/common/lib/libc/hash/sha1/sha1.c (revision 00f17ebc18da2891dcb34767549021de110b28aa)
1*00f17ebcSchristos /*	$NetBSD: sha1.c,v 1.7 2021/10/28 15:09:08 christos Exp $	*/
2275e8bb9Schristos /*	$OpenBSD: sha1.c,v 1.9 1997/07/23 21:12:32 kstailey Exp $	*/
3275e8bb9Schristos 
4275e8bb9Schristos /*
5275e8bb9Schristos  * SHA-1 in C
6275e8bb9Schristos  * By Steve Reid <steve@edmweb.com>
7275e8bb9Schristos  * 100% Public Domain
8275e8bb9Schristos  *
9275e8bb9Schristos  * Test Vectors (from FIPS PUB 180-1)
10275e8bb9Schristos  * "abc"
11275e8bb9Schristos  *   A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
12275e8bb9Schristos  * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
13275e8bb9Schristos  *   84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
14275e8bb9Schristos  * A million repetitions of "a"
15275e8bb9Schristos  *   34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
16275e8bb9Schristos  */
17275e8bb9Schristos 
18275e8bb9Schristos #define SHA1HANDSOFF		/* Copies data before messing with it. */
19275e8bb9Schristos 
20275e8bb9Schristos #include <sys/cdefs.h>
21d029b259Schristos 
22d029b259Schristos #if defined(_KERNEL) || defined(_STANDALONE)
23*00f17ebcSchristos __KERNEL_RCSID(0, "$NetBSD: sha1.c,v 1.7 2021/10/28 15:09:08 christos Exp $");
24d029b259Schristos 
25d029b259Schristos #include <lib/libkern/libkern.h>
26d029b259Schristos 
27d029b259Schristos #else
28d029b259Schristos 
29275e8bb9Schristos #if defined(LIBC_SCCS) && !defined(lint)
30*00f17ebcSchristos __RCSID("$NetBSD: sha1.c,v 1.7 2021/10/28 15:09:08 christos Exp $");
31275e8bb9Schristos #endif /* LIBC_SCCS and not lint */
32d029b259Schristos 
33275e8bb9Schristos #include "namespace.h"
34275e8bb9Schristos #include <assert.h>
35275e8bb9Schristos #include <string.h>
36d029b259Schristos 
37275e8bb9Schristos #endif
38275e8bb9Schristos 
39d029b259Schristos #include <sys/types.h>
40d029b259Schristos #include <sys/sha1.h>
41d029b259Schristos 
42d029b259Schristos 
43275e8bb9Schristos #if HAVE_NBTOOL_CONFIG_H
44275e8bb9Schristos #include "nbtool_config.h"
45275e8bb9Schristos #endif
46275e8bb9Schristos 
47275e8bb9Schristos #if !HAVE_SHA1_H
48275e8bb9Schristos 
49275e8bb9Schristos #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
50275e8bb9Schristos 
51275e8bb9Schristos /*
52275e8bb9Schristos  * blk0() and blk() perform the initial expand.
53275e8bb9Schristos  * I got the idea of expanding during the round function from SSLeay
54275e8bb9Schristos  */
55275e8bb9Schristos #if BYTE_ORDER == LITTLE_ENDIAN
56275e8bb9Schristos # define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
57275e8bb9Schristos     |(rol(block->l[i],8)&0x00FF00FF))
58275e8bb9Schristos #else
59275e8bb9Schristos # define blk0(i) block->l[i]
60275e8bb9Schristos #endif
61275e8bb9Schristos #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
62275e8bb9Schristos     ^block->l[(i+2)&15]^block->l[i&15],1))
63275e8bb9Schristos 
64275e8bb9Schristos /*
65275e8bb9Schristos  * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1
66275e8bb9Schristos  */
67275e8bb9Schristos #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
68275e8bb9Schristos #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
69275e8bb9Schristos #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
70275e8bb9Schristos #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
71275e8bb9Schristos #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
72275e8bb9Schristos 
73275e8bb9Schristos 
74b1aca4e8Sskrll #if !defined(_KERNEL) && !defined(_STANDALONE)
75b1aca4e8Sskrll #if defined(__weak_alias)
76275e8bb9Schristos __weak_alias(SHA1Transform,_SHA1Transform)
77275e8bb9Schristos __weak_alias(SHA1Init,_SHA1Init)
78275e8bb9Schristos __weak_alias(SHA1Update,_SHA1Update)
79275e8bb9Schristos __weak_alias(SHA1Final,_SHA1Final)
80275e8bb9Schristos #endif
81b1aca4e8Sskrll #endif
82275e8bb9Schristos 
83275e8bb9Schristos typedef union {
840c6bb30eSjoerg     uint8_t c[64];
850c6bb30eSjoerg     uint32_t l[16];
86275e8bb9Schristos } CHAR64LONG16;
87275e8bb9Schristos 
88275e8bb9Schristos /* old sparc64 gcc could not compile this */
89275e8bb9Schristos #undef SPARC64_GCC_WORKAROUND
90275e8bb9Schristos #if defined(__sparc64__) && defined(__GNUC__) && __GNUC__ < 3
91275e8bb9Schristos #define SPARC64_GCC_WORKAROUND
92275e8bb9Schristos #endif
93275e8bb9Schristos 
94275e8bb9Schristos #ifdef SPARC64_GCC_WORKAROUND
95267197ecSapb void do_R01(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *);
96267197ecSapb void do_R2(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *);
97267197ecSapb void do_R3(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *);
98267197ecSapb void do_R4(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *);
99275e8bb9Schristos 
100275e8bb9Schristos #define nR0(v,w,x,y,z,i) R0(*v,*w,*x,*y,*z,i)
101275e8bb9Schristos #define nR1(v,w,x,y,z,i) R1(*v,*w,*x,*y,*z,i)
102275e8bb9Schristos #define nR2(v,w,x,y,z,i) R2(*v,*w,*x,*y,*z,i)
103275e8bb9Schristos #define nR3(v,w,x,y,z,i) R3(*v,*w,*x,*y,*z,i)
104275e8bb9Schristos #define nR4(v,w,x,y,z,i) R4(*v,*w,*x,*y,*z,i)
105275e8bb9Schristos 
106275e8bb9Schristos void
do_R01(uint32_t * a,uint32_t * b,uint32_t * c,uint32_t * d,uint32_t * e,CHAR64LONG16 * block)107267197ecSapb do_R01(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *block)
108275e8bb9Schristos {
109275e8bb9Schristos     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);
110275e8bb9Schristos     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);
111275e8bb9Schristos     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);
112275e8bb9Schristos     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);
113275e8bb9Schristos     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);
114275e8bb9Schristos }
115275e8bb9Schristos 
116275e8bb9Schristos void
do_R2(uint32_t * a,uint32_t * b,uint32_t * c,uint32_t * d,uint32_t * e,CHAR64LONG16 * block)117267197ecSapb do_R2(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *block)
118275e8bb9Schristos {
119275e8bb9Schristos     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);
120275e8bb9Schristos     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);
121275e8bb9Schristos     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);
122275e8bb9Schristos     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);
123275e8bb9Schristos     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);
124275e8bb9Schristos }
125275e8bb9Schristos 
126275e8bb9Schristos void
do_R3(uint32_t * a,uint32_t * b,uint32_t * c,uint32_t * d,uint32_t * e,CHAR64LONG16 * block)127267197ecSapb do_R3(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *block)
128275e8bb9Schristos {
129275e8bb9Schristos     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);
130275e8bb9Schristos     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);
131275e8bb9Schristos     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);
132275e8bb9Schristos     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);
133275e8bb9Schristos     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);
134275e8bb9Schristos }
135275e8bb9Schristos 
136275e8bb9Schristos void
do_R4(uint32_t * a,uint32_t * b,uint32_t * c,uint32_t * d,uint32_t * e,CHAR64LONG16 * block)137267197ecSapb do_R4(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *block)
138275e8bb9Schristos {
139275e8bb9Schristos     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);
140275e8bb9Schristos     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);
141275e8bb9Schristos     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);
142275e8bb9Schristos     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);
143275e8bb9Schristos     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);
144275e8bb9Schristos }
145275e8bb9Schristos #endif
146275e8bb9Schristos 
147275e8bb9Schristos /*
148275e8bb9Schristos  * Hash a single 512-bit block. This is the core of the algorithm.
149275e8bb9Schristos  */
SHA1Transform(uint32_t state[5],const uint8_t buffer[64])1500c6bb30eSjoerg void SHA1Transform(uint32_t state[5], const uint8_t buffer[64])
151275e8bb9Schristos {
152267197ecSapb     uint32_t a, b, c, d, e;
153275e8bb9Schristos     CHAR64LONG16 *block;
154275e8bb9Schristos 
155275e8bb9Schristos #ifdef SHA1HANDSOFF
156275e8bb9Schristos     CHAR64LONG16 workspace;
157275e8bb9Schristos #endif
158275e8bb9Schristos 
159275e8bb9Schristos     _DIAGASSERT(buffer != 0);
160275e8bb9Schristos     _DIAGASSERT(state != 0);
161275e8bb9Schristos 
162275e8bb9Schristos #ifdef SHA1HANDSOFF
163275e8bb9Schristos     block = &workspace;
164275e8bb9Schristos     (void)memcpy(block, buffer, 64);
165275e8bb9Schristos #else
166275e8bb9Schristos     block = (CHAR64LONG16 *)(void *)buffer;
167275e8bb9Schristos #endif
168275e8bb9Schristos 
169275e8bb9Schristos     /* Copy context->state[] to working vars */
170275e8bb9Schristos     a = state[0];
171275e8bb9Schristos     b = state[1];
172275e8bb9Schristos     c = state[2];
173275e8bb9Schristos     d = state[3];
174275e8bb9Schristos     e = state[4];
175275e8bb9Schristos 
176275e8bb9Schristos #ifdef SPARC64_GCC_WORKAROUND
177275e8bb9Schristos     do_R01(&a, &b, &c, &d, &e, block);
178275e8bb9Schristos     do_R2(&a, &b, &c, &d, &e, block);
179275e8bb9Schristos     do_R3(&a, &b, &c, &d, &e, block);
180275e8bb9Schristos     do_R4(&a, &b, &c, &d, &e, block);
181275e8bb9Schristos #else
182275e8bb9Schristos     /* 4 rounds of 20 operations each. Loop unrolled. */
183275e8bb9Schristos     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);
184275e8bb9Schristos     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);
185275e8bb9Schristos     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);
186275e8bb9Schristos     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);
187275e8bb9Schristos     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);
188275e8bb9Schristos     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);
189275e8bb9Schristos     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);
190275e8bb9Schristos     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);
191275e8bb9Schristos     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);
192275e8bb9Schristos     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);
193275e8bb9Schristos     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);
194275e8bb9Schristos     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);
195275e8bb9Schristos     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);
196275e8bb9Schristos     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);
197275e8bb9Schristos     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);
198275e8bb9Schristos     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);
199275e8bb9Schristos     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);
200275e8bb9Schristos     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);
201275e8bb9Schristos     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);
202275e8bb9Schristos     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);
203275e8bb9Schristos #endif
204275e8bb9Schristos 
205275e8bb9Schristos     /* Add the working vars back into context.state[] */
206275e8bb9Schristos     state[0] += a;
207275e8bb9Schristos     state[1] += b;
208275e8bb9Schristos     state[2] += c;
209275e8bb9Schristos     state[3] += d;
210275e8bb9Schristos     state[4] += e;
211275e8bb9Schristos 
212275e8bb9Schristos     /* Wipe variables */
213275e8bb9Schristos     a = b = c = d = e = 0;
214275e8bb9Schristos }
215275e8bb9Schristos 
216275e8bb9Schristos 
217275e8bb9Schristos /*
218275e8bb9Schristos  * SHA1Init - Initialize new context
219275e8bb9Schristos  */
SHA1Init(SHA1_CTX * context)22096276685Scegger void SHA1Init(SHA1_CTX *context)
221275e8bb9Schristos {
222275e8bb9Schristos 
223275e8bb9Schristos     _DIAGASSERT(context != 0);
224275e8bb9Schristos 
225275e8bb9Schristos     /* SHA1 initialization constants */
226275e8bb9Schristos     context->state[0] = 0x67452301;
227275e8bb9Schristos     context->state[1] = 0xEFCDAB89;
228275e8bb9Schristos     context->state[2] = 0x98BADCFE;
229275e8bb9Schristos     context->state[3] = 0x10325476;
230275e8bb9Schristos     context->state[4] = 0xC3D2E1F0;
231275e8bb9Schristos     context->count[0] = context->count[1] = 0;
232275e8bb9Schristos }
233275e8bb9Schristos 
234275e8bb9Schristos 
235275e8bb9Schristos /*
236275e8bb9Schristos  * Run your data through this.
237275e8bb9Schristos  */
SHA1Update(SHA1_CTX * context,const uint8_t * data,unsigned int len)2380c6bb30eSjoerg void SHA1Update(SHA1_CTX *context, const uint8_t *data, unsigned int len)
239275e8bb9Schristos {
2400c6bb30eSjoerg     unsigned int i, j;
241275e8bb9Schristos 
242275e8bb9Schristos     _DIAGASSERT(context != 0);
243275e8bb9Schristos     _DIAGASSERT(data != 0);
244275e8bb9Schristos 
245275e8bb9Schristos     j = context->count[0];
246275e8bb9Schristos     if ((context->count[0] += len << 3) < j)
247275e8bb9Schristos 	context->count[1] += (len>>29)+1;
248275e8bb9Schristos     j = (j >> 3) & 63;
249275e8bb9Schristos     if ((j + len) > 63) {
250275e8bb9Schristos 	(void)memcpy(&context->buffer[j], data, (i = 64-j));
251275e8bb9Schristos 	SHA1Transform(context->state, context->buffer);
252275e8bb9Schristos 	for ( ; i + 63 < len; i += 64)
253275e8bb9Schristos 	    SHA1Transform(context->state, &data[i]);
254275e8bb9Schristos 	j = 0;
255275e8bb9Schristos     } else {
256275e8bb9Schristos 	i = 0;
257275e8bb9Schristos     }
258275e8bb9Schristos     (void)memcpy(&context->buffer[j], &data[i], len - i);
259275e8bb9Schristos }
260275e8bb9Schristos 
261275e8bb9Schristos 
262275e8bb9Schristos /*
263275e8bb9Schristos  * Add padding and return the message digest.
264275e8bb9Schristos  */
SHA1Final(uint8_t digest[SHA1_DIGEST_LENGTH],SHA1_CTX * context)265*00f17ebcSchristos void SHA1Final(uint8_t digest[SHA1_DIGEST_LENGTH], SHA1_CTX *context)
266275e8bb9Schristos {
2670c6bb30eSjoerg     unsigned int i;
2680c6bb30eSjoerg     uint8_t finalcount[8];
269275e8bb9Schristos 
270275e8bb9Schristos     _DIAGASSERT(digest != 0);
271275e8bb9Schristos     _DIAGASSERT(context != 0);
272275e8bb9Schristos 
273275e8bb9Schristos     for (i = 0; i < 8; i++) {
2740c6bb30eSjoerg 	finalcount[i] = (uint8_t)((context->count[(i >= 4 ? 0 : 1)]
275275e8bb9Schristos 	 >> ((3-(i & 3)) * 8) ) & 255);	 /* Endian independent */
276275e8bb9Schristos     }
2770c6bb30eSjoerg     SHA1Update(context, (const uint8_t *)"\200", 1);
278275e8bb9Schristos     while ((context->count[0] & 504) != 448)
2790c6bb30eSjoerg 	SHA1Update(context, (const uint8_t *)"\0", 1);
280275e8bb9Schristos     SHA1Update(context, finalcount, 8);  /* Should cause a SHA1Transform() */
281275e8bb9Schristos 
282275e8bb9Schristos     if (digest) {
283275e8bb9Schristos 	for (i = 0; i < 20; i++)
2840c6bb30eSjoerg 	    digest[i] = (uint8_t)
285275e8bb9Schristos 		((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
286275e8bb9Schristos     }
287275e8bb9Schristos }
288275e8bb9Schristos 
289275e8bb9Schristos #endif /* HAVE_SHA1_H */
290