| 1 |
/********************************************************************* |
| 2 |
* Filename: sha1.c |
| 3 |
* Author: Brad Conte (brad AT bradconte.com) |
| 4 |
* Copyright: |
| 5 |
* Disclaimer: This code is presented "as is" without any guarantees. |
| 6 |
* Details: Implementation of the SHA1 hashing algorithm. |
| 7 |
Algorithm specification can be found here: |
| 8 |
* http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf |
| 9 |
This implementation uses little endian byte order. |
| 10 |
*********************************************************************/ |
| 11 |
|
| 12 |
/*************************** HEADER FILES ***************************/ |
| 13 |
#include <stdlib.h> |
| 14 |
#include <memory.h> |
| 15 |
#include "sha1.h" |
| 16 |
|
| 17 |
/****************************** MACROS ******************************/ |
| 18 |
#define ROTLEFT(a, b) ((a << b) | (a >> (32 - b))) |
| 19 |
|
| 20 |
/*********************** FUNCTION DEFINITIONS ***********************/ |
| 21 |
static void sha1_transform(SHA1_CTX *ctx, const BYTE data[]) { |
| 22 |
WORD a, b, c, d, e, i, j, t, m[80]; |
| 23 |
|
| 24 |
for (i = 0, j = 0; i < 16; ++i, j += 4) |
| 25 |
m[i] = (data[j] << 24) + (data[j + 1] << 16) + (data[j + 2] << 8) + (data[j + 3]); |
| 26 |
for (; i < 80; ++i) { |
| 27 |
m[i] = (m[i - 3] ^ m[i - 8] ^ m[i - 14] ^ m[i - 16]); |
| 28 |
m[i] = (m[i] << 1) | (m[i] >> 31); |
| 29 |
} |
| 30 |
|
| 31 |
a = ctx->state[0]; |
| 32 |
b = ctx->state[1]; |
| 33 |
c = ctx->state[2]; |
| 34 |
d = ctx->state[3]; |
| 35 |
e = ctx->state[4]; |
| 36 |
|
| 37 |
for (i = 0; i < 20; ++i) { |
| 38 |
t = ROTLEFT(a, 5) + ((b & c) ^ (~b & d)) + e + ctx->k[0] + m[i]; |
| 39 |
e = d; |
| 40 |
d = c; |
| 41 |
c = ROTLEFT(b, 30); |
| 42 |
b = a; |
| 43 |
a = t; |
| 44 |
} |
| 45 |
for (; i < 40; ++i) { |
| 46 |
t = ROTLEFT(a, 5) + (b ^ c ^ d) + e + ctx->k[1] + m[i]; |
| 47 |
e = d; |
| 48 |
d = c; |
| 49 |
c = ROTLEFT(b, 30); |
| 50 |
b = a; |
| 51 |
a = t; |
| 52 |
} |
| 53 |
for (; i < 60; ++i) { |
| 54 |
t = ROTLEFT(a, 5) + ((b & c) ^ (b & d) ^ (c & d)) + e + ctx->k[2] + m[i]; |
| 55 |
e = d; |
| 56 |
d = c; |
| 57 |
c = ROTLEFT(b, 30); |
| 58 |
b = a; |
| 59 |
a = t; |
| 60 |
} |
| 61 |
for (; i < 80; ++i) { |
| 62 |
t = ROTLEFT(a, 5) + (b ^ c ^ d) + e + ctx->k[3] + m[i]; |
| 63 |
e = d; |
| 64 |
d = c; |
| 65 |
c = ROTLEFT(b, 30); |
| 66 |
b = a; |
| 67 |
a = t; |
| 68 |
} |
| 69 |
|
| 70 |
ctx->state[0] += a; |
| 71 |
ctx->state[1] += b; |
| 72 |
ctx->state[2] += c; |
| 73 |
ctx->state[3] += d; |
| 74 |
ctx->state[4] += e; |
| 75 |
} |
| 76 |
|
| 77 |
void sha1_init(SHA1_CTX *ctx) { |
| 78 |
ctx->datalen = 0; |
| 79 |
ctx->bitlen = 0; |
| 80 |
ctx->state[0] = 0x67452301; |
| 81 |
ctx->state[1] = 0xEFCDAB89; |
| 82 |
ctx->state[2] = 0x98BADCFE; |
| 83 |
ctx->state[3] = 0x10325476; |
| 84 |
ctx->state[4] = 0xc3d2e1f0; |
| 85 |
ctx->k[0] = 0x5a827999; |
| 86 |
ctx->k[1] = 0x6ed9eba1; |
| 87 |
ctx->k[2] = 0x8f1bbcdc; |
| 88 |
ctx->k[3] = 0xca62c1d6; |
| 89 |
} |
| 90 |
|
| 91 |
void sha1_update(SHA1_CTX *ctx, const BYTE data[], size_t len) { |
| 92 |
size_t i; |
| 93 |
|
| 94 |
for (i = 0; i < len; ++i) { |
| 95 |
ctx->data[ctx->datalen] = data[i]; |
| 96 |
ctx->datalen++; |
| 97 |
if (ctx->datalen == 64) { |
| 98 |
sha1_transform(ctx, ctx->data); |
| 99 |
ctx->bitlen += 512; |
| 100 |
ctx->datalen = 0; |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
void sha1_final(SHA1_CTX *ctx, BYTE hash[]) { |
| 106 |
WORD i; |
| 107 |
|
| 108 |
i = ctx->datalen; |
| 109 |
|
| 110 |
// Pad whatever data is left in the buffer. |
| 111 |
if (ctx->datalen < 56) { |
| 112 |
ctx->data[i++] = 0x80; |
| 113 |
while (i < 56) |
| 114 |
ctx->data[i++] = 0x00; |
| 115 |
} else { |
| 116 |
ctx->data[i++] = 0x80; |
| 117 |
while (i < 64) |
| 118 |
ctx->data[i++] = 0x00; |
| 119 |
sha1_transform(ctx, ctx->data); |
| 120 |
memset(ctx->data, 0, 56); |
| 121 |
} |
| 122 |
|
| 123 |
// Append to the padding the total message's length in bits and transform. |
| 124 |
ctx->bitlen += ctx->datalen * 8; |
| 125 |
ctx->data[63] = ctx->bitlen; |
| 126 |
ctx->data[62] = ctx->bitlen >> 8; |
| 127 |
ctx->data[61] = ctx->bitlen >> 16; |
| 128 |
ctx->data[60] = ctx->bitlen >> 24; |
| 129 |
ctx->data[59] = ctx->bitlen >> 32; |
| 130 |
ctx->data[58] = ctx->bitlen >> 40; |
| 131 |
ctx->data[57] = ctx->bitlen >> 48; |
| 132 |
ctx->data[56] = ctx->bitlen >> 56; |
| 133 |
sha1_transform(ctx, ctx->data); |
| 134 |
|
| 135 |
// Since this implementation uses little endian byte ordering and MD uses big endian, |
| 136 |
// reverse all the bytes when copying the final state to the output hash. |
| 137 |
for (i = 0; i < 4; ++i) { |
| 138 |
hash[i] = (ctx->state[0] >> (24 - i * 8)) & 0x000000ff; |
| 139 |
hash[i + 4] = (ctx->state[1] >> (24 - i * 8)) & 0x000000ff; |
| 140 |
hash[i + 8] = (ctx->state[2] >> (24 - i * 8)) & 0x000000ff; |
| 141 |
hash[i + 12] = (ctx->state[3] >> (24 - i * 8)) & 0x000000ff; |
| 142 |
hash[i + 16] = (ctx->state[4] >> (24 - i * 8)) & 0x000000ff; |
| 143 |
} |
| 144 |
} |