diff options
Diffstat (limited to 'src/sha256.c')
| -rw-r--r-- | src/sha256.c | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/src/sha256.c b/src/sha256.c new file mode 100644 index 0000000..11ef912 --- /dev/null +++ b/src/sha256.c @@ -0,0 +1,104 @@ +#include <stdint.h> +#include <string.h> + +typedef uint8_t byte; + +typedef byte hash[ 32 ]; + +static uint32_t ror(uint32_t x, int n) { return (x >> n) | (x << (32 - n)); } + +static uint32_t ch(uint32_t x, uint32_t y, uint32_t z) { return (x & y) ^ (~x & z); } + +static uint32_t maj(uint32_t x, uint32_t y, uint32_t z) { return (x & y) ^ (x & z) ^ (y & z); } + +static uint32_t sigma0(uint32_t x) { return ror(x, 2) ^ ror(x, 13) ^ ror(x, 22); } + +static uint32_t sigma1(uint32_t x) { return ror(x, 6) ^ ror(x, 11) ^ ror(x, 25); } + +static uint32_t gamma0(uint32_t x) { return ror(x, 7) ^ ror(x, 18) ^ (x >> 3); } + +static uint32_t gamma1(uint32_t x) { return ror(x, 17) ^ ror(x, 19) ^ (x >> 10); } + +static const uint32_t K[ 64 ] = { + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, + 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, + 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, + 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, + 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, + 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 +}; + +static void sha256_compress(uint32_t state[ 8 ], const byte block[ 64 ]) { + uint32_t w[ 64 ]; + int i; + for (i = 0; i < 16; i++) { + w[ i ] = (block[ i * 4 ] << 24) | (block[ i * 4 + 1 ] << 16) | (block[ i * 4 + 2 ] << 8) + | block[ i * 4 + 3 ]; + } + for (i = 16; i < 64; i++) { + w[ i ] = gamma1(w[ i - 2 ]) + w[ i - 7 ] + gamma0(w[ i - 15 ]) + w[ i - 16 ]; + } + uint32_t a = state[ 0 ], b = state[ 1 ], c = state[ 2 ], d = state[ 3 ], e = state[ 4 ], + f = state[ 5 ], g = state[ 6 ], h = state[ 7 ]; + for (i = 0; i < 64; i++) { + uint32_t t1 = h + sigma1(e) + ch(e, f, g) + K[ i ] + w[ i ]; + uint32_t t2 = sigma0(a) + maj(a, b, c); + h = g; + g = f; + f = e; + e = d + t1; + d = c; + c = b; + b = a; + a = t1 + t2; + } + state[ 0 ] += a; + state[ 1 ] += b; + state[ 2 ] += c; + state[ 3 ] += d; + state[ 4 ] += e; + state[ 5 ] += f; + state[ 6 ] += g; + state[ 7 ] += h; +} + +struct sha256 { + hash hash; +} sha256_hash(const byte *in, size_t in_len) { + struct sha256 out; + + uint32_t state[ 8 ] = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, + 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 }; + size_t off = 0; + for (; off + 64 <= in_len; off += 64) { sha256_compress(state, in + off); } + byte pad[ 128 ]; + memset(pad, 0, sizeof(pad)); + size_t rem = in_len - off; + memcpy(pad, in + off, rem); + pad[ rem ] = 0x80; + int use_two_blocks = (rem >= 56); + size_t pad_len = 64 + 64 * use_two_blocks; + size_t pos = rem + 1; + while (pos < pad_len - 8) { pad[ pos++ ] = 0; } + uint64_t bit_len = in_len * 8; + pad[ pad_len - 1 ] = bit_len & 0xff; + pad[ pad_len - 2 ] = (bit_len >> 8) & 0xff; + pad[ pad_len - 3 ] = (bit_len >> 16) & 0xff; + pad[ pad_len - 4 ] = (bit_len >> 24) & 0xff; + pad[ pad_len - 5 ] = (bit_len >> 32) & 0xff; + pad[ pad_len - 6 ] = (bit_len >> 40) & 0xff; + pad[ pad_len - 7 ] = (bit_len >> 48) & 0xff; + pad[ pad_len - 8 ] = (bit_len >> 56) & 0xff; + sha256_compress(state, pad); + if (use_two_blocks) { sha256_compress(state, pad + 64); } + for (int i = 0; i < 8; i++) { + out.hash[ 4 * i ] = (state[ i ] >> 24) & 0xff; + out.hash[ 4 * i + 1 ] = (state[ i ] >> 16) & 0xff; + out.hash[ 4 * i + 2 ] = (state[ i ] >> 8) & 0xff; + out.hash[ 4 * i + 3 ] = state[ i ] & 0xff; + } + + return out; +} |
