From 645e9084b34afb9f6ba591fc82eb9a8393259015 Mon Sep 17 00:00:00 2001
From: Mitya Selivanov <automainint@guattari.tech>
Date: Sat, 4 Feb 2023 13:07:00 +0100
Subject: sha256

---
 source/kit/CMakeLists.txt            |   8 +-
 source/kit/sha256.c                  | 146 +++++++++++++++++++++++++++++++++++
 source/kit/sha256.h                  |  29 +++++++
 source/test/unittests/CMakeLists.txt |   2 +-
 source/test/unittests/sha256.test.c  |  42 ++++++++++
 5 files changed, 223 insertions(+), 4 deletions(-)
 create mode 100644 source/kit/sha256.c
 create mode 100644 source/kit/sha256.h
 create mode 100644 source/test/unittests/sha256.test.c

diff --git a/source/kit/CMakeLists.txt b/source/kit/CMakeLists.txt
index 4113668..e776a29 100644
--- a/source/kit/CMakeLists.txt
+++ b/source/kit/CMakeLists.txt
@@ -2,9 +2,10 @@ target_sources(
   kit
     PRIVATE
       input_buffer.c bigint.c status.c secure_random.c
-      thread.posix.c atomic.win32.c condition_variable.c thread.win32.c
-      move_back.c input_stream.c file.c string_ref.c async_function.c
-      allocator.c array_ref.c dynamic_array.c mutex.c mersenne_twister_64.c
+      sha256.c thread.posix.c atomic.win32.c condition_variable.c
+      thread.win32.c move_back.c input_stream.c file.c string_ref.c
+      async_function.c allocator.c array_ref.c dynamic_array.c mutex.c
+      mersenne_twister_64.c
     PUBLIC
       $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/mutex.h>
       $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/status.h>
@@ -22,6 +23,7 @@ target_sources(
       $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/thread_defs.h>
       $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/input_buffer.h>
       $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/lower_bound.h>
+      $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/sha256.h>
       $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/file.h>
       $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/array_ref.h>
       $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/mersenne_twister_64.h>)
diff --git a/source/kit/sha256.c b/source/kit/sha256.c
new file mode 100644
index 0000000..5d6a37a
--- /dev/null
+++ b/source/kit/sha256.c
@@ -0,0 +1,146 @@
+#include "sha256.h"
+
+#include <assert.h>
+#include <string.h>
+
+#define ROTLEFT(a, b) (((a) << (b)) | ((a) >> (32 - (b))))
+#define ROTRIGHT(a, b) (((a) >> (b)) | ((a) << (32 - (b))))
+
+#define CH(x, y, z) (((x) & (y)) ^ (~(x) & (z)))
+#define MAJ(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
+#define EP0(x) (ROTRIGHT(x, 2) ^ ROTRIGHT(x, 13) ^ ROTRIGHT(x, 22))
+#define EP1(x) (ROTRIGHT(x, 6) ^ ROTRIGHT(x, 11) ^ ROTRIGHT(x, 25))
+#define SIG0(x) (ROTRIGHT(x, 7) ^ ROTRIGHT(x, 18) ^ ((x) >> 3))
+#define SIG1(x) (ROTRIGHT(x, 17) ^ ROTRIGHT(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_transform(uint32_t *const      state,
+                             const uint8_t *const data) {
+  assert(state != NULL);
+  assert(data != NULL);
+
+  uint32_t a, b, c, d, e, f, g, h, i, j, t1, t2, m[64];
+
+  for (i = 0, j = 0; i < 16; ++i, j += 4)
+    m[i] = ((uint32_t) data[j] << 24) |
+           ((uint32_t) data[j + 1] << 16) |
+           ((uint32_t) data[j + 2] << 8) | ((uint32_t) data[j + 3]);
+  for (; i < 64; ++i)
+    m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16];
+
+  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) {
+    t1 = h + EP1(e) + CH(e, f, g) + k[i] + m[i];
+    t2 = EP0(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;
+}
+
+kit_sha256_hash_t kit_sha256(ptrdiff_t const      in_size,
+                             uint8_t const *const in_data) {
+  assert(in_size >= 0);
+  assert(in_data != NULL);
+
+  uint32_t state[8] = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372,
+                        0xa54ff53a, 0x510e527f, 0x9b05688c,
+                        0x1f83d9ab, 0x5be0cd19 };
+
+  uint8_t data[64];
+
+  ptrdiff_t i;
+  ptrdiff_t datalen = 0;
+  ptrdiff_t bitlen  = 0;
+
+  if (in_data != NULL)
+    for (i = 0; i < in_size; ++i) {
+      data[datalen] = in_data[i];
+      datalen++;
+
+      if (datalen != 64)
+        continue;
+
+      sha256_transform(state, data);
+      bitlen += 512;
+      datalen = 0;
+    }
+
+  i = datalen;
+
+  if (datalen < 56) {
+    data[i++] = 0x80;
+    while (i < 56) data[i++] = 0x00;
+  } else {
+    data[i++] = 0x80;
+    while (i < 64) data[i++] = 0x00;
+
+    sha256_transform(state, data);
+    memset(data, 0, 56);
+  }
+
+  bitlen += datalen * 8;
+  data[63] = bitlen;
+  data[62] = bitlen >> 8;
+  data[61] = bitlen >> 16;
+  data[60] = bitlen >> 24;
+  data[59] = bitlen >> 32;
+  data[58] = bitlen >> 40;
+  data[57] = bitlen >> 48;
+  data[56] = bitlen >> 56;
+
+  sha256_transform(state, data);
+
+  kit_sha256_hash_t hash;
+  memset(&hash, 0, sizeof hash);
+
+  for (i = 0; i < 4; ++i) {
+    hash.v[i]      = (state[0] >> (24 - i * 8)) & 0xff;
+    hash.v[i + 4]  = (state[1] >> (24 - i * 8)) & 0xff;
+    hash.v[i + 8]  = (state[2] >> (24 - i * 8)) & 0xff;
+    hash.v[i + 12] = (state[3] >> (24 - i * 8)) & 0xff;
+    hash.v[i + 16] = (state[4] >> (24 - i * 8)) & 0xff;
+    hash.v[i + 20] = (state[5] >> (24 - i * 8)) & 0xff;
+    hash.v[i + 24] = (state[6] >> (24 - i * 8)) & 0xff;
+    hash.v[i + 28] = (state[7] >> (24 - i * 8)) & 0xff;
+  }
+
+  return hash;
+}
diff --git a/source/kit/sha256.h b/source/kit/sha256.h
new file mode 100644
index 0000000..806bff3
--- /dev/null
+++ b/source/kit/sha256.h
@@ -0,0 +1,29 @@
+#ifndef KIT_SHA256_H
+#define KIT_SHA256_H
+
+#include <stddef.h>
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+enum { KIT_SHA256_BLOCK_SIZE = 32 };
+
+typedef struct {
+  uint8_t v[KIT_SHA256_BLOCK_SIZE];
+} kit_sha256_hash_t;
+
+kit_sha256_hash_t kit_sha256(ptrdiff_t size, uint8_t const *data);
+
+#ifdef __cplusplus
+}
+#endif
+
+#ifndef KIT_DISABLE_SHORT_NAMES
+#  define SHA256_BLOCK_SIZE KIT_SHA256_BLOCK_SIZE
+#  define sha256_hash_t kit_sha256_hash_t
+#  define sha256 kit_sha256
+#endif
+
+#endif
diff --git a/source/test/unittests/CMakeLists.txt b/source/test/unittests/CMakeLists.txt
index 2ebc195..a73ca6e 100644
--- a/source/test/unittests/CMakeLists.txt
+++ b/source/test/unittests/CMakeLists.txt
@@ -3,6 +3,6 @@ target_sources(
     PRIVATE
       async_function.test.c bigint.test.c mutex.test.c
       test_duration.test.c main.test.c string_ref.test.c atomic.test.c thread.test.c
-      array_ref.test.c input_stream.test.c lower_bound.test.c
+      array_ref.test.c input_stream.test.c sha256.test.c lower_bound.test.c
       secure_random.test.c condition_variable.test.c mersenne_twister_64.test.c
       input_buffer.test.c move_back.test.c dynamic_array.test.c file.test.c)
diff --git a/source/test/unittests/sha256.test.c b/source/test/unittests/sha256.test.c
new file mode 100644
index 0000000..e74b286
--- /dev/null
+++ b/source/test/unittests/sha256.test.c
@@ -0,0 +1,42 @@
+#include "../../kit/sha256.h"
+#include "../../kit/array_ref.h"
+
+#define KIT_TEST_FILE sha256_64
+#include "../../kit_test/test.h"
+
+#include <string.h>
+
+TEST("sha256") {
+  uint8_t text1[] = "abc";
+  uint8_t text2[] =
+      "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
+
+  uint8_t text3[1000000];
+  memset(text3, 'a', sizeof text3);
+
+  uint8_t hash1[] = {
+    0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40,
+    0xde, 0x5d, 0xae, 0x22, 0x23, 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17,
+    0x7a, 0x9c, 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
+  };
+  uint8_t hash2[] = {
+    0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8, 0xe5, 0xc0, 0x26,
+    0x93, 0x0c, 0x3e, 0x60, 0x39, 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff,
+    0x21, 0x67, 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1
+  };
+  uint8_t hash3[] = {
+    0xcd, 0xc7, 0x6e, 0x5c, 0x99, 0x14, 0xfb, 0x92, 0x81, 0xa1, 0xc7,
+    0xe2, 0x84, 0xd7, 0x3e, 0x67, 0xf1, 0x80, 0x9a, 0x48, 0xa4, 0x97,
+    0x20, 0x0e, 0x04, 0x6d, 0x39, 0xcc, 0xc7, 0x11, 0x2c, 0xd0
+  };
+
+  REQUIRE(ar_equal_bytes(1, SHA256_BLOCK_SIZE,
+                         sha256((sizeof text1) - 1, text1).v, 1,
+                         SHA256_BLOCK_SIZE, hash1));
+  REQUIRE(ar_equal_bytes(1, SHA256_BLOCK_SIZE,
+                         sha256((sizeof text2) - 1, text2).v, 1,
+                         SHA256_BLOCK_SIZE, hash2));
+  REQUIRE(ar_equal_bytes(1, SHA256_BLOCK_SIZE,
+                         sha256(sizeof text3, text3).v, 1,
+                         SHA256_BLOCK_SIZE, hash3));
+}
-- 
cgit v1.2.3