s/qmail 4.2.29a
Next generation secure email transport
Loading...
Searching...
No Matches
sha1.c
Go to the documentation of this file.
1/*
2SHA-1 in C
3By Steve Reid <sreid@sea-to-sky.net>
4100% Public Domain
5
6-----------------
7Modified 7/98
8By James H. Brown <jbrown@burgoyne.com>
9Still 100% Public Domain
10
11-----------------
12Adopted for s/qmail 2/2020
13feh
14Still 100% Public Domain; though requiring fehQlibs-14
15
16*/
17
18#include <string.h>
19#include "sha1.h"
20#include "byte.h"
21
22// #define SHA1HANDSOFF
23
24#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
25
26/* blk0() and blk() perform the initial expand. */
27/* I got the idea of expanding during the round function from SSLeay */
28/* FIXME: can we do this in an endian-proof way? */
29#ifndef WORDS_BIGENDIAN
30#define blk0(i) (block->l[i] = (rol(block->l[i],24) & 0xFF00FF00) \
31 | (rol(block->l[i],8) & 0x00FF00FF))
32#else
33#define blk0(i) block->l[i]
34#endif
35#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
36 ^block->l[(i+2)&15]^block->l[i&15],1))
37
38/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
39#define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
40#define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
41#define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
42#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
43#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
44
45/* Hash a single 512-bit block. This is the core of the algorithm. */
46
47void sha1_transform(uint32_t state[5],const uint8_t buffer[SHA1_BLOCKSIZE])
48{
49 uint32_t a, b, c, d, e;
50 typedef union {
51 uint8_t c[SHA1_BLOCKSIZE];
52 uint32_t l[16];
53 } CHAR64LONG16;
54 CHAR64LONG16 *block;
55
56#ifdef SHA1HANDSOFF
57 static uint8_t workspace[SHA1_BLOCKSIZE];
58
59 block = (CHAR64LONG16 *) workspace;
60 byte_copy(block,SHA1_BLOCKSIZE,buffer);
61#else
62 block = (CHAR64LONG16 *) buffer;
63#endif
64
65 /* Copy context->state[] to working vars */
66 a = state[0];
67 b = state[1];
68 c = state[2];
69 d = state[3];
70 e = state[4];
71
72 /* 4 rounds of 20 operations each. Loop unrolled. */
73 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);
74 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);
75 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);
76 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);
77 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);
78 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);
79 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);
80 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);
81 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);
82 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);
83 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);
84 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);
85 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);
86 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);
87 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);
88 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);
89 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);
90 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);
91 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);
92 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);
93
94 /* Add the working vars back into context.state[] */
95 state[0] += a;
96 state[1] += b;
97 state[2] += c;
98 state[3] += d;
99 state[4] += e;
100
101 /* Wipe variables */
102 a = b = c = d = e = 0;
103
104#ifdef SHA1HANDSOFF
105 byte_zero(block,64);
106#endif
107}
108
109/* SHA1Init - Initialize new context */
110
111void sha1_init(sha1_ctx *context)
112{
113 /* SHA1 initialization constants */
114 context->state[0] = 0x67452301;
115 context->state[1] = 0xEFCDAB89;
116 context->state[2] = 0x98BADCFE;
117 context->state[3] = 0x10325476;
118 context->state[4] = 0xC3D2E1F0;
119 context->count[0] = context->count[1] = 0;
120}
121
122/* Run your data through this. */
123
124void sha1_update(sha1_ctx *context,const uint8_t *data,uint32_t len)
125{
126 uint32_t i, j;
127
128 j = (context->count[0] >> 3) & 63;
129 if ((context->count[0] += len << 3) < (len << 3))
130 context->count[1]++;
131 context->count[1] += (len >> 29);
132 if ((j + len) > 63) {
133 byte_copy(&context->buffer[j],(i = 64 - j),data);
134 sha1_transform(context->state,context->buffer);
135 for (; i + 63 < len; i += 64) {
136 sha1_transform(context->state,data + i);
137 }
138 j = 0;
139 } else
140 i = 0;
141 byte_copy(&context->buffer[j],len - i,&data[i]);
142}
143
144/* Add padding and return the message digest. */
145
146void sha1_final(uint8_t digest[SHA1_DIGESTSIZE],sha1_ctx *context)
147{
148 uint32_t i;
149 uint8_t finalcount[8];
150
151 for (i = 0; i < 8; i++) {
152 finalcount[i] = (uint8_t)((context->count[(i >= 4 ? 0 : 1)]
153 >> ((3 - (i & 3)) * 8)) & 255); /* Endian independent */
154 }
155 sha1_update(context,(uint8_t *) "\200",1);
156
157 while ((context->count[0] & 504) != 448)
158 sha1_update(context,(uint8_t *) "\0",1);
159
160 sha1_update(context,finalcount,8); /* Should cause a SHA1_Transform() */
161
162 for (i = 0; i < SHA1_DIGESTSIZE; i++)
163 digest[i] = (uint8_t) ((context->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
164
165 /* Wipe variables */
166 i = 0;
167 byte_zero(context->buffer,64);
168 byte_zero(context->state,20);
169 byte_zero(context->count,8);
170 byte_zero(finalcount,8);
171
172#ifdef SHA1HANDSOFF /* make SHA1Transform overwrite its own static vars */
173 sha1_transform(context->state,context->buffer);
174#endif
175}
176
177void sha1_hash(char *hash,const char *str,uint32_t len)
178{
179 sha1_ctx context;
180 int i;
181
182 sha1_init(&context);
183 for (i = 0; i < len; i++)
184 sha1_update(&context,(uint8_t *)str + i,1);
185
186 sha1_final((uint8_t *)hash,&context);
187 hash[20] = '\0';
188}
buffer b
Definition: auto-gid.c:10
stralloc data
Definition: fastforward.c:118
void c(char *, char *, char *, int, int, int)
Definition: install.c:57
stralloc hash
Definition: qmail-dksign.c:243
int j
Definition: qmail-send.c:920
struct del * d[CHANNELS]
Definition: qmail-send.c:720
#define R1(v, w, x, y, z, i)
Definition: sha1.c:40
#define R2(v, w, x, y, z, i)
Definition: sha1.c:41
#define R0(v, w, x, y, z, i)
Definition: sha1.c:39
void sha1_init(sha1_ctx *context)
Definition: sha1.c:111
void sha1_hash(char *hash, const char *str, uint32_t len)
Definition: sha1.c:177
void sha1_transform(uint32_t state[5], const uint8_t buffer[SHA1_BLOCKSIZE])
Definition: sha1.c:47
#define R3(v, w, x, y, z, i)
Definition: sha1.c:42
void sha1_update(sha1_ctx *context, const uint8_t *data, uint32_t len)
Definition: sha1.c:124
void sha1_final(uint8_t digest[SHA1_DIGESTSIZE], sha1_ctx *context)
Definition: sha1.c:146
#define R4(v, w, x, y, z, i)
Definition: sha1.c:43
#define SHA1_BLOCKSIZE
Definition: sha1.h:15
#define SHA1_DIGESTSIZE
Definition: sha1.h:16
Definition: sha1.h:19
uint32_t count[2]
Definition: sha1.h:21
uint32_t state[5]
Definition: sha1.h:20
uint8_t buffer[SHA1_BLOCKSIZE]
Definition: sha1.h:22