blob: e83694854c684588e04e25c057a3ba1d4b63ce20 [file] [log] [blame]
sewardjde4a1d02002-03-22 01:27:54 +00001
2/*
3Stick the enclosed text in a file "sha1.test.c".
4
5Here is a sample run:
6 hugh $ cc sha1.test.c
7 hugh $ valgrind ./a.out
8 ==26189== valgrind-20020227, a memory error detector for x86 GNU/Linux.
njn0e1b5142003-04-15 14:58:06 +00009 ==26189== Copyright (C) 2000-200x, and GNU GPL'd, by Julian Seward.
sewardjde4a1d02002-03-22 01:27:54 +000010 ==26189== For more details, rerun with: -v
11 ==26189==
12
13 valgrind: vg_main.c:495 (vgPlain_create_translation_for): Assertion `trans_size > 0 && trans_size < 65536' failed.
14 Please report this bug to me at: jseward@acm.org
15
16 hugh $ valgrind --instrument=no ./a.out
17 ==26209== valgrind-20020227, a memory error detector for x86 GNU/Linux.
njn0e1b5142003-04-15 14:58:06 +000018 ==26209== Copyright (C) 2000-200x, and GNU GPL'd, by Julian Seward.
sewardjde4a1d02002-03-22 01:27:54 +000019 ==26209== For more details, rerun with: -v
20 ==26209==
21 ==26209==
22
23*/
24
25/* ================ sha1.c ================ */
26/*
27SHA-1 in C
28By Steve Reid <steve@edmweb.com>
29100% Public Domain
30
31Test Vectors (from FIPS PUB 180-1)
32"abc"
33 A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
34"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
35 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
36A million repetitions of "a"
37 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
38*/
39
40/* #define LITTLE_ENDIAN * This should be #define'd already, if true. */
41/* #define SHA1HANDSOFF * Copies data before messing with it. */
42
43#define SHA1HANDSOFF
44
45#include <string.h>
46#include <sys/types.h> /* for u_int*_t */
47
48/* ================ sha1.h ================ */
49/*
50SHA-1 in C
51By Steve Reid <steve@edmweb.com>
52100% Public Domain
53*/
54
55typedef struct {
56 u_int32_t state[5];
57 u_int32_t count[2];
58 unsigned char buffer[64];
59} SHA1_CTX;
60
61void SHA1Transform(u_int32_t state[5], const unsigned char buffer[64]);
62void SHA1Init(SHA1_CTX* context);
63void SHA1Update(SHA1_CTX* context, const unsigned char* data, u_int32_t len);
64void SHA1Final(unsigned char digest[20], SHA1_CTX* context);
65/* ================ end of sha1.h ================ */
66#include <endian.h>
67
68#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
69
70/* blk0() and blk() perform the initial expand. */
71/* I got the idea of expanding during the round function from SSLeay */
72#if BYTE_ORDER == LITTLE_ENDIAN
73#define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
74 |(rol(block->l[i],8)&0x00FF00FF))
75#elif BYTE_ORDER == BIG_ENDIAN
76#define blk0(i) block->l[i]
77#else
78#error "Endianness not defined!"
79#endif
80#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
81 ^block->l[(i+2)&15]^block->l[i&15],1))
82
83/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
84#define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
85#define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
86#define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
87#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
88#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
89
90
91/* Hash a single 512-bit block. This is the core of the algorithm. */
92
93void SHA1Transform(u_int32_t state[5], const unsigned char buffer[64])
94{
95u_int32_t a, b, c, d, e;
96typedef union {
97 unsigned char c[64];
98 u_int32_t l[16];
99} CHAR64LONG16;
100#ifdef SHA1HANDSOFF
101CHAR64LONG16 block[1]; /* use array to appear as a pointer */
102 memcpy(block, buffer, 64);
103#else
104 /* The following had better never be used because it causes the
105 * pointer-to-const buffer to be cast into a pointer to non-const.
106 * And the result is written through. I threw a "const" in, hoping
107 * this will cause a diagnostic.
108 */
109CHAR64LONG16* block = (const CHAR64LONG16*)buffer;
110#endif
111 /* Copy context->state[] to working vars */
112 a = state[0];
113 b = state[1];
114 c = state[2];
115 d = state[3];
116 e = state[4];
117 /* 4 rounds of 20 operations each. Loop unrolled. */
118 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);
119 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);
120 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);
121 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);
122 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);
123 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);
124 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);
125 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);
126 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);
127 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);
128 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);
129 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);
130 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);
131 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);
132 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);
133 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);
134 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);
135 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);
136 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);
137 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);
138 /* Add the working vars back into context.state[] */
139 state[0] += a;
140 state[1] += b;
141 state[2] += c;
142 state[3] += d;
143 state[4] += e;
144 /* Wipe variables */
145 a = b = c = d = e = 0;
146#ifdef SHA1HANDSOFF
147 memset(block, '\0', sizeof(block));
148#endif
149}
150
151
152/* SHA1Init - Initialize new context */
153
154void SHA1Init(SHA1_CTX* context)
155{
156 /* SHA1 initialization constants */
157 context->state[0] = 0x67452301;
158 context->state[1] = 0xEFCDAB89;
159 context->state[2] = 0x98BADCFE;
160 context->state[3] = 0x10325476;
161 context->state[4] = 0xC3D2E1F0;
162 context->count[0] = context->count[1] = 0;
163}
164
165
166/* Run your data through this. */
167
168void SHA1Update(SHA1_CTX* context, const unsigned char* data, u_int32_t len)
169{
170u_int32_t i;
171u_int32_t j;
172
173 j = context->count[0];
174 if ((context->count[0] += len << 3) < j)
175 context->count[1]++;
176 context->count[1] += (len>>29);
177 j = (j >> 3) & 63;
178 if ((j + len) > 63) {
179 memcpy(&context->buffer[j], data, (i = 64-j));
180 SHA1Transform(context->state, context->buffer);
181 for ( ; i + 63 < len; i += 64) {
182 SHA1Transform(context->state, &data[i]);
183 }
184 j = 0;
185 }
186 else i = 0;
187 memcpy(&context->buffer[j], &data[i], len - i);
188}
189
190
191/* Add padding and return the message digest. */
192
193void SHA1Final(unsigned char digest[20], SHA1_CTX* context)
194{
195unsigned i;
196unsigned char finalcount[8];
197unsigned char c;
198
199#if 0 /* untested "improvement" by DHR */
200 /* Convert context->count to a sequence of bytes
201 * in finalcount. Second element first, but
202 * big-endian order within element.
203 * But we do it all backwards.
204 */
205 unsigned char *fcp = &finalcount[8];
206
207 for (i = 0; i < 2; i++)
208 {
209 u_int32_t t = context->count[i];
210 int j;
211
212 for (j = 0; j < 4; t >>= 8, j++)
213 *--fcp = (unsigned char) t
214 }
215#else
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#endif
221 c = 0200;
222 SHA1Update(context, &c, 1);
223 while ((context->count[0] & 504) != 448) {
224 c = 0000;
225 SHA1Update(context, &c, 1);
226 }
227 SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */
228 for (i = 0; i < 20; i++) {
229 digest[i] = (unsigned char)
230 ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
231 }
232 /* Wipe variables */
233 memset(context, '\0', sizeof(*context));
234 memset(&finalcount, '\0', sizeof(finalcount));
235}
236/* ================ end of sha1.c ================ */
237
238int
239main(int argc, char **argv)
240{
241 SHA1_CTX ctx;
242 unsigned char hash[20];
243
244 SHA1Init(&ctx);
245 SHA1Update(&ctx, "abc", 3);
246 SHA1Final(hash, &ctx);
247 return 0;
248}
249
250