diff --git a/c/sha256.c b/c/sha256.c new file mode 100644 index 0000000..b25be8e --- /dev/null +++ b/c/sha256.c @@ -0,0 +1,153 @@ +#include +#include +#include +#include +#include + +static uint64_t pad_length(uint64_t len) { + uint64_t space_needed = 1; + + while(((len + space_needed) % 64) != 56) { + space_needed += 1; + } + + return space_needed; +} + +static uint32_t rotate(uint8_t bits, uint32_t word) { + return (word >> bits) | (word << (32 - bits)); +} + +unsigned char *sha2(const char *msg, uint64_t len) { + uint64_t L = len * CHAR_BIT; + uint64_t needed = pad_length(len); + unsigned char *buf = malloc(len + needed + 8); + + if(buf == NULL) { + return NULL; + } + + strncpy((char *) buf, msg, len); + buf[len++] = 0x80; + + for(; needed > 1; --needed) { + buf[len++] = 0x00; + } + + uint8_t count = 8; + + while(count--) { + buf[len++] = (L & ((uint64_t) 0xFF << 56)) >> 56; + L = L << 8; + } + + uint32_t H[] = { + 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, + 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 + }; + + uint32_t k[] = { + 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 + }; + + uint64_t chunk = 0; + + for(; chunk < len; chunk += 64) { + uint32_t w[64] = { 0 }; + uint8_t i = 0; + + for(; i < 16; i++) { + w[i] = buf[chunk + (i * 4)] << 24; + w[i] |= buf[chunk + (i * 4 + 1)] << 16; + w[i] |= buf[chunk + (i * 4 + 2)] << 8; + w[i] |= buf[chunk + (i * 4 + 3)]; + } + + for(i = 16; i < 64; i++) { + uint32_t s0 = rotate(7, w[i - 15]) ^ rotate(18, w[i - 15]) ^ (w[i - 15] >> 3); + uint32_t s1 = rotate(17, w[i - 2]) ^ rotate(19, w[i - 2]) ^ (w[i - 2] >> 10); + w[i] = w[i - 16] + s0 + w[i - 7] + s1; + } + + uint32_t a = H[0]; + uint32_t b = H[1]; + uint32_t c = H[2]; + uint32_t d = H[3]; + uint32_t e = H[4]; + uint32_t f = H[5]; + uint32_t g = H[6]; + uint32_t h = H[7]; + + for(i = 0; i < 64; i++) { + uint32_t S1 = rotate(6, e) ^ rotate(11, e) ^ rotate(25, e); + uint32_t ch = (e & f) ^ ((~e) & g); + uint32_t temp1 = h + S1 + ch + k[i] + w[i]; + uint32_t S0 = rotate(2, a) ^ rotate(13, a) ^ rotate(22, a); + uint32_t maj = (a & b) ^ (a & c) ^ (b & c); + uint32_t temp2 = S0 + maj; + + h = g; + g = f; + f = e; + e = d + temp1; + d = c; + c = b; + b = a; + a = temp1 + temp2; + } + + H[0] += a; + H[1] += b; + H[2] += c; + H[3] += d; + H[4] += e; + H[5] += f; + H[6] += g; + H[7] += h; + } + + unsigned char *digest = malloc(32); + + if(digest == NULL) { + return NULL; + } + + for(count = 0; count < 8; count++) { + digest[count * 4] = H[count] >> 24; + digest[(count * 4) + 1] = (H[count] >> 16) & 0xFF; + digest[(count * 4) + 2] = (H[count] >> 8) & 0xFF; + digest[(count * 4) + 3] = H[count] & 0xFF; + } + + return digest; +} + +int main(int argc, char **argv) { + for(++argv; argc > 1; ++argv, --argc) { + unsigned char *hash = sha2(*argv, strlen(*argv)); + uint8_t count = 0; + + for(; count < 32; ++count) { + printf("%02X", (unsigned) hash[count]); + } + + printf("\n"); + } + + return 0; +}