Rob Landley | 509dd14 | 2007-11-24 21:25:18 -0600 | [diff] [blame] | 1 | /* |
Rob Landley | 0a22c5c | 2007-11-24 21:33:23 -0600 | [diff] [blame] | 2 | * 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 Landley | 509dd14 | 2007-11-24 21:25:18 -0600 | [diff] [blame] | 5 | |
Rob Landley | 0a22c5c | 2007-11-24 21:33:23 -0600 | [diff] [blame] | 6 | /* #define LITTLE_ENDIAN * This should be #define'd if true. */ |
Rob Landley | 509dd14 | 2007-11-24 21:25:18 -0600 | [diff] [blame] | 7 | /* #define SHA1HANDSOFF * Copies data before messing with it. */ |
Rob Landley | 0a22c5c | 2007-11-24 21:33:23 -0600 | [diff] [blame] | 8 | #define LITTLE_ENDIAN |
Rob Landley | 509dd14 | 2007-11-24 21:25:18 -0600 | [diff] [blame] | 9 | |
| 10 | #include <stdio.h> |
| 11 | #include <string.h> |
Rob Landley | 0a22c5c | 2007-11-24 21:33:23 -0600 | [diff] [blame] | 12 | #include <inttypes.h> |
Rob Landley | 509dd14 | 2007-11-24 21:25:18 -0600 | [diff] [blame] | 13 | |
Rob Landley | 0a22c5c | 2007-11-24 21:33:23 -0600 | [diff] [blame] | 14 | struct sha1 { |
| 15 | uint32_t state[5]; |
Rob Landley | f198082 | 2007-11-24 22:02:07 -0600 | [diff] [blame^] | 16 | uint64_t count; |
| 17 | union { |
| 18 | unsigned char c[64]; |
| 19 | uint32_t i[16]; |
| 20 | } buffer; |
Rob Landley | 0a22c5c | 2007-11-24 21:33:23 -0600 | [diff] [blame] | 21 | }; |
Rob Landley | 509dd14 | 2007-11-24 21:25:18 -0600 | [diff] [blame] | 22 | |
Rob Landley | 0a22c5c | 2007-11-24 21:33:23 -0600 | [diff] [blame] | 23 | void sha1_init(struct sha1 *this); |
Rob Landley | f198082 | 2007-11-24 22:02:07 -0600 | [diff] [blame^] | 24 | void sha1_transform(struct sha1 *this); |
Rob Landley | 0a22c5c | 2007-11-24 21:33:23 -0600 | [diff] [blame] | 25 | void sha1_update(struct sha1 *this, unsigned char *data, unsigned int len); |
| 26 | void sha1_final(struct sha1 *this, unsigned char digest[20]); |
Rob Landley | 509dd14 | 2007-11-24 21:25:18 -0600 | [diff] [blame] | 27 | |
| 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 Landley | f198082 | 2007-11-24 22:02:07 -0600 | [diff] [blame^] | 33 | #define blk0(i) (block[i] = (rol(block[i],24)&0xFF00FF00) \ |
| 34 | |(rol(block[i],8)&0x00FF00FF)) |
Rob Landley | 509dd14 | 2007-11-24 21:25:18 -0600 | [diff] [blame] | 35 | #else |
Rob Landley | f198082 | 2007-11-24 22:02:07 -0600 | [diff] [blame^] | 36 | #define blk0(i) block[i] |
Rob Landley | 509dd14 | 2007-11-24 21:25:18 -0600 | [diff] [blame] | 37 | #endif |
Rob Landley | f198082 | 2007-11-24 22:02:07 -0600 | [diff] [blame^] | 38 | #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 Landley | 509dd14 | 2007-11-24 21:25:18 -0600 | [diff] [blame] | 40 | |
| 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 Landley | 0a22c5c | 2007-11-24 21:33:23 -0600 | [diff] [blame] | 48 | void 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 Landley | 509dd14 | 2007-11-24 21:25:18 -0600 | [diff] [blame] | 55 | |
| 56 | /* Hash a single 512-bit block. This is the core of the algorithm. */ |
| 57 | |
Rob Landley | f198082 | 2007-11-24 22:02:07 -0600 | [diff] [blame^] | 58 | void sha1_transform(struct sha1 *this) |
Rob Landley | 509dd14 | 2007-11-24 21:25:18 -0600 | [diff] [blame] | 59 | { |
Rob Landley | 0a22c5c | 2007-11-24 21:33:23 -0600 | [diff] [blame] | 60 | unsigned int a, b, c, d, e; |
Rob Landley | f198082 | 2007-11-24 22:02:07 -0600 | [diff] [blame^] | 61 | uint32_t *block = this->buffer.i; |
Rob Landley | 0a22c5c | 2007-11-24 21:33:23 -0600 | [diff] [blame] | 62 | |
Rob Landley | b911de3 | 2007-11-24 21:26:56 -0600 | [diff] [blame] | 63 | /* Copy context->state[] to working vars */ |
Rob Landley | f198082 | 2007-11-24 22:02:07 -0600 | [diff] [blame^] | 64 | 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 Landley | b911de3 | 2007-11-24 21:26:56 -0600 | [diff] [blame] | 69 | /* 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 Landley | f198082 | 2007-11-24 22:02:07 -0600 | [diff] [blame^] | 91 | this->state[0] += a; |
| 92 | this->state[1] += b; |
| 93 | this->state[2] += c; |
| 94 | this->state[3] += d; |
| 95 | this->state[4] += e; |
| 96 | printy(this->state); |
Rob Landley | b911de3 | 2007-11-24 21:26:56 -0600 | [diff] [blame] | 97 | /* Wipe variables */ |
| 98 | a = b = c = d = e = 0; |
Rob Landley | 509dd14 | 2007-11-24 21:25:18 -0600 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | |
| 102 | /* SHA1Init - Initialize new context */ |
| 103 | |
Rob Landley | 0a22c5c | 2007-11-24 21:33:23 -0600 | [diff] [blame] | 104 | void sha1_init(struct sha1 *this) |
Rob Landley | 509dd14 | 2007-11-24 21:25:18 -0600 | [diff] [blame] | 105 | { |
Rob Landley | b911de3 | 2007-11-24 21:26:56 -0600 | [diff] [blame] | 106 | /* SHA1 initialization constants */ |
Rob Landley | 0a22c5c | 2007-11-24 21:33:23 -0600 | [diff] [blame] | 107 | 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 Landley | f198082 | 2007-11-24 22:02:07 -0600 | [diff] [blame^] | 112 | this->count = 0; |
Rob Landley | 509dd14 | 2007-11-24 21:25:18 -0600 | [diff] [blame] | 113 | } |
| 114 | |
Rob Landley | 0a22c5c | 2007-11-24 21:33:23 -0600 | [diff] [blame] | 115 | /* Run your data through this function. */ |
Rob Landley | 509dd14 | 2007-11-24 21:25:18 -0600 | [diff] [blame] | 116 | |
Rob Landley | 0a22c5c | 2007-11-24 21:33:23 -0600 | [diff] [blame] | 117 | void sha1_update(struct sha1 *this, unsigned char *data, unsigned int len) |
Rob Landley | 509dd14 | 2007-11-24 21:25:18 -0600 | [diff] [blame] | 118 | { |
Rob Landley | 0a22c5c | 2007-11-24 21:33:23 -0600 | [diff] [blame] | 119 | unsigned int i, j; |
Rob Landley | 509dd14 | 2007-11-24 21:25:18 -0600 | [diff] [blame] | 120 | |
Rob Landley | f198082 | 2007-11-24 22:02:07 -0600 | [diff] [blame^] | 121 | j = this->count & 63; |
| 122 | this->count += len; |
Rob Landley | 509dd14 | 2007-11-24 21:25:18 -0600 | [diff] [blame] | 123 | |
Rob Landley | f198082 | 2007-11-24 22:02:07 -0600 | [diff] [blame^] | 124 | // 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 Landley | 509dd14 | 2007-11-24 21:25:18 -0600 | [diff] [blame] | 138 | |
| 139 | /* Add padding and return the message digest. */ |
| 140 | |
Rob Landley | 0a22c5c | 2007-11-24 21:33:23 -0600 | [diff] [blame] | 141 | void sha1_final(struct sha1 *this, unsigned char digest[20]) |
Rob Landley | 509dd14 | 2007-11-24 21:25:18 -0600 | [diff] [blame] | 142 | { |
Rob Landley | f198082 | 2007-11-24 22:02:07 -0600 | [diff] [blame^] | 143 | uint64_t count = this->count << 3; |
| 144 | unsigned int i; |
| 145 | unsigned char buf; |
Rob Landley | 509dd14 | 2007-11-24 21:25:18 -0600 | [diff] [blame] | 146 | |
Rob Landley | f198082 | 2007-11-24 22:02:07 -0600 | [diff] [blame^] | 147 | // 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 Landley | b911de3 | 2007-11-24 21:26:56 -0600 | [diff] [blame] | 161 | for (i = 0; i < 20; i++) { |
| 162 | digest[i] = (unsigned char) |
Rob Landley | 0a22c5c | 2007-11-24 21:33:23 -0600 | [diff] [blame] | 163 | ((this->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); |
Rob Landley | b911de3 | 2007-11-24 21:26:56 -0600 | [diff] [blame] | 164 | } |
| 165 | /* Wipe variables */ |
Rob Landley | f198082 | 2007-11-24 22:02:07 -0600 | [diff] [blame^] | 166 | i = 0; |
Rob Landley | 509dd14 | 2007-11-24 21:25:18 -0600 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | |
| 170 | /*************************************************************/ |
| 171 | |
| 172 | |
| 173 | int main(int argc, char** argv) |
| 174 | { |
Rob Landley | 0a22c5c | 2007-11-24 21:33:23 -0600 | [diff] [blame] | 175 | int i, j; |
| 176 | struct sha1 this; |
| 177 | unsigned char digest[20], buffer[16384]; |
| 178 | FILE* file; |
Rob Landley | 509dd14 | 2007-11-24 21:25:18 -0600 | [diff] [blame] | 179 | |
Rob Landley | b911de3 | 2007-11-24 21:26:56 -0600 | [diff] [blame] | 180 | 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 Landley | 0a22c5c | 2007-11-24 21:33:23 -0600 | [diff] [blame] | 188 | } |
| 189 | sha1_init(&this); |
Rob Landley | b911de3 | 2007-11-24 21:26:56 -0600 | [diff] [blame] | 190 | while (!feof(file)) { /* note: what if ferror(file) */ |
| 191 | i = fread(buffer, 1, 16384, file); |
Rob Landley | 0a22c5c | 2007-11-24 21:33:23 -0600 | [diff] [blame] | 192 | sha1_update(&this, buffer, i); |
Rob Landley | b911de3 | 2007-11-24 21:26:56 -0600 | [diff] [blame] | 193 | } |
Rob Landley | 0a22c5c | 2007-11-24 21:33:23 -0600 | [diff] [blame] | 194 | sha1_final(&this, digest); |
Rob Landley | b911de3 | 2007-11-24 21:26:56 -0600 | [diff] [blame] | 195 | fclose(file); |
Rob Landley | 0a22c5c | 2007-11-24 21:33:23 -0600 | [diff] [blame] | 196 | printy(digest); |
Rob Landley | b911de3 | 2007-11-24 21:26:56 -0600 | [diff] [blame] | 197 | exit(0); |
Rob Landley | 509dd14 | 2007-11-24 21:25:18 -0600 | [diff] [blame] | 198 | } |