90a16dbb8b9795b911a82727b0eb9db4f7f169e1
[apps/madmutt.git] / lib-hash / sha1.c
1 /*
2  SHA-1 in C
3
4  By Steve Reid <steve@edmweb.com>, with small changes to make it
5  fit into mutt by Thomas Roessler <roessler@does-not-exist.org>.
6
7  100% Public Domain.
8
9  Test Vectors (from FIPS PUB 180-1)
10  "abc"
11  A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
12  "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
13  84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
14  A million repetitions of "a"
15  34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
16 */
17
18 #include <lib-lib/lib-lib.h>
19 #define SHA1HANDSOFF
20
21 #include "sha1.h"
22
23 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
24
25 /* blk0() and blk() perform the initial expand. */
26 /* I got the idea of expanding during the round function from SSLeay */
27 #ifdef WORDS_BIGENDIAN
28 #  define blk0(i) block->l[i]
29 #else
30 #  define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
31                     |(rol(block->l[i],8)&0x00FF00FF))
32 #endif
33
34 #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
35     ^block->l[(i+2)&15]^block->l[i&15],1))
36
37 /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
38 #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
39 #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
40 #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
41 #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
42 #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
43
44
45 /* Hash a single 512-bit block. This is the core of the algorithm. */
46
47 void SHA1Transform (uint32_t state[5], const unsigned char buffer[64])
48 {
49   uint32_t a, b, c, d, e;
50   typedef union {
51     unsigned char c[64];
52     uint32_t l[16];
53   } CHAR64LONG16;
54
55 #ifdef SHA1HANDSOFF
56   CHAR64LONG16 block[1];        /* use array to appear as a pointer */
57
58   memcpy (block, buffer, 64);
59 #else
60   /* The following had better never be used because it causes the
61    * pointer-to-const buffer to be cast into a pointer to non-const.
62    * And the result is written through.  I threw a "const" in, hoping
63    * this will cause a diagnostic.
64    */
65   CHAR64LONG16 *block = (const CHAR64LONG16 *) buffer;
66 #endif
67   /* Copy context->state[] to working vars */
68   a = state[0];
69   b = state[1];
70   c = state[2];
71   d = state[3];
72   e = state[4];
73   /* 4 rounds of 20 operations each. Loop unrolled. */
74   R0 (a, b, c, d, e, 0);
75   R0 (e, a, b, c, d, 1);
76   R0 (d, e, a, b, c, 2);
77   R0 (c, d, e, a, b, 3);
78   R0 (b, c, d, e, a, 4);
79   R0 (a, b, c, d, e, 5);
80   R0 (e, a, b, c, d, 6);
81   R0 (d, e, a, b, c, 7);
82   R0 (c, d, e, a, b, 8);
83   R0 (b, c, d, e, a, 9);
84   R0 (a, b, c, d, e, 10);
85   R0 (e, a, b, c, d, 11);
86   R0 (d, e, a, b, c, 12);
87   R0 (c, d, e, a, b, 13);
88   R0 (b, c, d, e, a, 14);
89   R0 (a, b, c, d, e, 15);
90   R1 (e, a, b, c, d, 16);
91   R1 (d, e, a, b, c, 17);
92   R1 (c, d, e, a, b, 18);
93   R1 (b, c, d, e, a, 19);
94   R2 (a, b, c, d, e, 20);
95   R2 (e, a, b, c, d, 21);
96   R2 (d, e, a, b, c, 22);
97   R2 (c, d, e, a, b, 23);
98   R2 (b, c, d, e, a, 24);
99   R2 (a, b, c, d, e, 25);
100   R2 (e, a, b, c, d, 26);
101   R2 (d, e, a, b, c, 27);
102   R2 (c, d, e, a, b, 28);
103   R2 (b, c, d, e, a, 29);
104   R2 (a, b, c, d, e, 30);
105   R2 (e, a, b, c, d, 31);
106   R2 (d, e, a, b, c, 32);
107   R2 (c, d, e, a, b, 33);
108   R2 (b, c, d, e, a, 34);
109   R2 (a, b, c, d, e, 35);
110   R2 (e, a, b, c, d, 36);
111   R2 (d, e, a, b, c, 37);
112   R2 (c, d, e, a, b, 38);
113   R2 (b, c, d, e, a, 39);
114   R3 (a, b, c, d, e, 40);
115   R3 (e, a, b, c, d, 41);
116   R3 (d, e, a, b, c, 42);
117   R3 (c, d, e, a, b, 43);
118   R3 (b, c, d, e, a, 44);
119   R3 (a, b, c, d, e, 45);
120   R3 (e, a, b, c, d, 46);
121   R3 (d, e, a, b, c, 47);
122   R3 (c, d, e, a, b, 48);
123   R3 (b, c, d, e, a, 49);
124   R3 (a, b, c, d, e, 50);
125   R3 (e, a, b, c, d, 51);
126   R3 (d, e, a, b, c, 52);
127   R3 (c, d, e, a, b, 53);
128   R3 (b, c, d, e, a, 54);
129   R3 (a, b, c, d, e, 55);
130   R3 (e, a, b, c, d, 56);
131   R3 (d, e, a, b, c, 57);
132   R3 (c, d, e, a, b, 58);
133   R3 (b, c, d, e, a, 59);
134   R4 (a, b, c, d, e, 60);
135   R4 (e, a, b, c, d, 61);
136   R4 (d, e, a, b, c, 62);
137   R4 (c, d, e, a, b, 63);
138   R4 (b, c, d, e, a, 64);
139   R4 (a, b, c, d, e, 65);
140   R4 (e, a, b, c, d, 66);
141   R4 (d, e, a, b, c, 67);
142   R4 (c, d, e, a, b, 68);
143   R4 (b, c, d, e, a, 69);
144   R4 (a, b, c, d, e, 70);
145   R4 (e, a, b, c, d, 71);
146   R4 (d, e, a, b, c, 72);
147   R4 (c, d, e, a, b, 73);
148   R4 (b, c, d, e, a, 74);
149   R4 (a, b, c, d, e, 75);
150   R4 (e, a, b, c, d, 76);
151   R4 (d, e, a, b, c, 77);
152   R4 (c, d, e, a, b, 78);
153   R4 (b, c, d, e, a, 79);
154   /* Add the working vars back into context.state[] */
155   state[0] += a;
156   state[1] += b;
157   state[2] += c;
158   state[3] += d;
159   state[4] += e;
160   /* Wipe variables */
161   a = b = c = d = e = 0;
162 #ifdef SHA1HANDSOFF
163   memset (block, '\0', sizeof (block));
164 #endif
165 }
166
167
168 /* SHA1Init - Initialize new context */
169
170 void SHA1Init (SHA1_CTX * context)
171 {
172   /* SHA1 initialization constants */
173   context->state[0] = 0x67452301;
174   context->state[1] = 0xEFCDAB89;
175   context->state[2] = 0x98BADCFE;
176   context->state[3] = 0x10325476;
177   context->state[4] = 0xC3D2E1F0;
178   context->count[0] = context->count[1] = 0;
179 }
180
181
182 /* Run your data through this. */
183
184 void SHA1Update (SHA1_CTX * context, const unsigned char *data, uint32_t len)
185 {
186   uint32_t i;
187   uint32_t j;
188
189   j = context->count[0];
190   if ((context->count[0] += len << 3) < j)
191     context->count[1]++;
192   context->count[1] += (len >> 29);
193   j = (j >> 3) & 63;
194   if ((j + len) > 63) {
195     memcpy (&context->buffer[j], data, (i = 64 - j));
196     SHA1Transform (context->state, context->buffer);
197     for (; i + 63 < len; i += 64) {
198       SHA1Transform (context->state, &data[i]);
199     }
200     j = 0;
201   }
202   else
203     i = 0;
204   memcpy (&context->buffer[j], &data[i], len - i);
205 }
206
207
208 /* Add padding and return the message digest. */
209
210 void SHA1Final (unsigned char digest[20], SHA1_CTX * context)
211 {
212   unsigned i;
213   unsigned char finalcount[8];
214   unsigned char c;
215
216   for (i = 0; i < 8; i++) {
217     finalcount[i] = (unsigned char) ((context->count[(i >= 4 ? 0 : 1)]
218                                       >> ((3 - (i & 3)) * 8)) & 255);   /* Endian independent */
219   }
220   c = 0200;
221   SHA1Update (context, &c, 1);
222   while ((context->count[0] & 504) != 448) {
223     c = 0000;
224     SHA1Update (context, &c, 1);
225   }
226   SHA1Update (context, finalcount, 8);  /* Should cause a SHA1Transform() */
227   for (i = 0; i < 20; i++) {
228     digest[i] = (unsigned char)
229       ((context->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
230   }
231   /* Wipe variables */
232   memset (context, '\0', sizeof (*context));
233   memset (&finalcount, '\0', sizeof (finalcount));
234 }