blob: b57805f95564ab1603ca29658eaa6e0d7acb5e8d [file] [log] [blame]
Rob Landley509dd142007-11-24 21:25:18 -06001/*
Rob Landley0a22c5c2007-11-24 21:33:23 -06002 * Based on the public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
3 * from http://www.mirrors.wiretapped.net/security/cryptography/hashes/sha1/
4 */
Rob Landley509dd142007-11-24 21:25:18 -06005
Rob Landley0a22c5c2007-11-24 21:33:23 -06006/* #define LITTLE_ENDIAN * This should be #define'd if true. */
Rob Landley509dd142007-11-24 21:25:18 -06007/* #define SHA1HANDSOFF * Copies data before messing with it. */
Rob Landley0a22c5c2007-11-24 21:33:23 -06008#define LITTLE_ENDIAN
Rob Landley509dd142007-11-24 21:25:18 -06009
10#include <stdio.h>
11#include <string.h>
Rob Landley0a22c5c2007-11-24 21:33:23 -060012#include <inttypes.h>
Rob Landley509dd142007-11-24 21:25:18 -060013
Rob Landley0a22c5c2007-11-24 21:33:23 -060014struct sha1 {
15 uint32_t state[5];
Rob Landleyf1980822007-11-24 22:02:07 -060016 uint64_t count;
17 union {
18 unsigned char c[64];
19 uint32_t i[16];
20 } buffer;
Rob Landley0a22c5c2007-11-24 21:33:23 -060021};
Rob Landley509dd142007-11-24 21:25:18 -060022
Rob Landley0a22c5c2007-11-24 21:33:23 -060023void sha1_init(struct sha1 *this);
Rob Landleyf1980822007-11-24 22:02:07 -060024void sha1_transform(struct sha1 *this);
Rob Landley0a22c5c2007-11-24 21:33:23 -060025void sha1_update(struct sha1 *this, unsigned char *data, unsigned int len);
26void sha1_final(struct sha1 *this, unsigned char digest[20]);
Rob Landley509dd142007-11-24 21:25:18 -060027
28#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
29
30/* blk0() and blk() perform the initial expand. */
31/* I got the idea of expanding during the round function from SSLeay */
32#ifdef LITTLE_ENDIAN
Rob Landleyf1980822007-11-24 22:02:07 -060033#define blk0(i) (block[i] = (rol(block[i],24)&0xFF00FF00) \
34 |(rol(block[i],8)&0x00FF00FF))
Rob Landley509dd142007-11-24 21:25:18 -060035#else
Rob Landleyf1980822007-11-24 22:02:07 -060036#define blk0(i) block[i]
Rob Landley509dd142007-11-24 21:25:18 -060037#endif
Rob Landleyf1980822007-11-24 22:02:07 -060038#define blk(i) (block[i&15] = rol(block[(i+13)&15]^block[(i+8)&15] \
39 ^block[(i+2)&15]^block[i&15],1))
Rob Landley509dd142007-11-24 21:25:18 -060040
41/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
42#define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
43#define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
44#define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
45#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
46#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
47
Rob Landley0a22c5c2007-11-24 21:33:23 -060048void printy(unsigned char *this)
49{
50 int i;
51
52 for (i = 0; i < 20; i++) printf("%02x", this[i]);
53 putchar('\n');
54}
Rob Landley509dd142007-11-24 21:25:18 -060055
56/* Hash a single 512-bit block. This is the core of the algorithm. */
57
Rob Landleyf1980822007-11-24 22:02:07 -060058void sha1_transform(struct sha1 *this)
Rob Landley509dd142007-11-24 21:25:18 -060059{
Rob Landley0a22c5c2007-11-24 21:33:23 -060060 unsigned int a, b, c, d, e;
Rob Landleyf1980822007-11-24 22:02:07 -060061 uint32_t *block = this->buffer.i;
Rob Landley0a22c5c2007-11-24 21:33:23 -060062
Rob Landleyb911de32007-11-24 21:26:56 -060063 /* Copy context->state[] to working vars */
Rob Landleyf1980822007-11-24 22:02:07 -060064 a = this->state[0];
65 b = this->state[1];
66 c = this->state[2];
67 d = this->state[3];
68 e = this->state[4];
Rob Landleyb911de32007-11-24 21:26:56 -060069 /* 4 rounds of 20 operations each. Loop unrolled. */
70 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);
71 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);
72 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);
73 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);
74 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);
75 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);
76 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);
77 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);
78 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);
79 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);
80 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);
81 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);
82 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);
83 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);
84 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);
85 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);
86 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);
87 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);
88 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);
89 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);
90 /* Add the working vars back into context.state[] */
Rob Landleyf1980822007-11-24 22:02:07 -060091 this->state[0] += a;
92 this->state[1] += b;
93 this->state[2] += c;
94 this->state[3] += d;
95 this->state[4] += e;
96printy(this->state);
Rob Landleyb911de32007-11-24 21:26:56 -060097 /* Wipe variables */
98 a = b = c = d = e = 0;
Rob Landley509dd142007-11-24 21:25:18 -060099}
100
101
102/* SHA1Init - Initialize new context */
103
Rob Landley0a22c5c2007-11-24 21:33:23 -0600104void sha1_init(struct sha1 *this)
Rob Landley509dd142007-11-24 21:25:18 -0600105{
Rob Landleyb911de32007-11-24 21:26:56 -0600106 /* SHA1 initialization constants */
Rob Landley0a22c5c2007-11-24 21:33:23 -0600107 this->state[0] = 0x67452301;
108 this->state[1] = 0xEFCDAB89;
109 this->state[2] = 0x98BADCFE;
110 this->state[3] = 0x10325476;
111 this->state[4] = 0xC3D2E1F0;
Rob Landleyf1980822007-11-24 22:02:07 -0600112 this->count = 0;
Rob Landley509dd142007-11-24 21:25:18 -0600113}
114
Rob Landley0a22c5c2007-11-24 21:33:23 -0600115/* Run your data through this function. */
Rob Landley509dd142007-11-24 21:25:18 -0600116
Rob Landley0a22c5c2007-11-24 21:33:23 -0600117void sha1_update(struct sha1 *this, unsigned char *data, unsigned int len)
Rob Landley509dd142007-11-24 21:25:18 -0600118{
Rob Landley0a22c5c2007-11-24 21:33:23 -0600119 unsigned int i, j;
Rob Landley509dd142007-11-24 21:25:18 -0600120
Rob Landleyf1980822007-11-24 22:02:07 -0600121 j = this->count & 63;
122 this->count += len;
Rob Landley509dd142007-11-24 21:25:18 -0600123
Rob Landleyf1980822007-11-24 22:02:07 -0600124 // Enough data to process a frame?
125 if ((j + len) > 63) {
126 i = 64-j;
127 memcpy(this->buffer.c + j, data, i);
128 sha1_transform(this);
129 for ( ; i + 63 < len; i += 64) {
130 memcpy(this->buffer.c, data + i, 64);
131 sha1_transform(this);
132 }
133 j = 0;
134 } else i = 0;
135 // Grab remaining chunk
136 memcpy(this->buffer.c + j, data + i, len - i);
137}
Rob Landley509dd142007-11-24 21:25:18 -0600138
139/* Add padding and return the message digest. */
140
Rob Landley0a22c5c2007-11-24 21:33:23 -0600141void sha1_final(struct sha1 *this, unsigned char digest[20])
Rob Landley509dd142007-11-24 21:25:18 -0600142{
Rob Landleyf1980822007-11-24 22:02:07 -0600143 uint64_t count = this->count << 3;
144 unsigned int i;
145 unsigned char buf;
Rob Landley509dd142007-11-24 21:25:18 -0600146
Rob Landleyf1980822007-11-24 22:02:07 -0600147 // End the message by appending a "1" bit to the data, ending with the
148 // message size (in bits, big endian), and adding enough zero bits in
149 // between to pad to the end of the next 64-byte frame. Since our input
150 // up to now has been in whole bytes, we can deal with bytes here too.
151
152 buf = 0x80;
153 do {
154 sha1_update(this, &buf, 1);
155 buf = 0;
156 } while ((this->count & 63) != 56);
157 for (i = 0; i < 8; i++)
158 this->buffer.c[56+i] = count >> (8*(7-i));
159 sha1_transform(this);
160
Rob Landleyb911de32007-11-24 21:26:56 -0600161 for (i = 0; i < 20; i++) {
162 digest[i] = (unsigned char)
Rob Landley0a22c5c2007-11-24 21:33:23 -0600163 ((this->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
Rob Landleyb911de32007-11-24 21:26:56 -0600164 }
165 /* Wipe variables */
Rob Landleyf1980822007-11-24 22:02:07 -0600166 i = 0;
Rob Landley509dd142007-11-24 21:25:18 -0600167}
168
169
170/*************************************************************/
171
172
173int main(int argc, char** argv)
174{
Rob Landley0a22c5c2007-11-24 21:33:23 -0600175 int i, j;
176 struct sha1 this;
177 unsigned char digest[20], buffer[16384];
178 FILE* file;
Rob Landley509dd142007-11-24 21:25:18 -0600179
Rob Landleyb911de32007-11-24 21:26:56 -0600180 if (argc < 2) {
181 file = stdin;
182 }
183 else {
184 if (!(file = fopen(argv[1], "rb"))) {
185 fputs("Unable to open file.", stderr);
186 exit(-1);
187 }
Rob Landley0a22c5c2007-11-24 21:33:23 -0600188 }
189 sha1_init(&this);
Rob Landleyb911de32007-11-24 21:26:56 -0600190 while (!feof(file)) { /* note: what if ferror(file) */
191 i = fread(buffer, 1, 16384, file);
Rob Landley0a22c5c2007-11-24 21:33:23 -0600192 sha1_update(&this, buffer, i);
Rob Landleyb911de32007-11-24 21:26:56 -0600193 }
Rob Landley0a22c5c2007-11-24 21:33:23 -0600194 sha1_final(&this, digest);
Rob Landleyb911de32007-11-24 21:26:56 -0600195 fclose(file);
Rob Landley0a22c5c2007-11-24 21:33:23 -0600196 printy(digest);
Rob Landleyb911de32007-11-24 21:26:56 -0600197 exit(0);
Rob Landley509dd142007-11-24 21:25:18 -0600198}