aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.c31
-rw-r--r--src/sha1.c80
-rw-r--r--src/sha256.c79
3 files changed, 186 insertions, 4 deletions
diff --git a/src/main.c b/src/main.c
index 608dc90..305d627 100644
--- a/src/main.c
+++ b/src/main.c
@@ -6,19 +6,42 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/03 20:29:33 by cacharle #+# #+# */
-/* Updated: 2020/08/02 14:17:53 by charles ### ########.fr */
+/* Updated: 2020/08/02 16:47:01 by charles ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_ssl.h"
-static const uint32_t g_md5_state[4] = {MD5_A_INIT, MD5_B_INIT, MD5_C_INIT, MD5_D_INIT};
+static const uint32_t g_md5_state[4] = {
+ 0x67452301,
+ 0xefcdab89,
+ 0x98badcfe,
+ 0x10325476,
+};
+
+static const uint32_t g_sha1_state[5] = {
+ 0x67452301,
+ 0xefcdab89,
+ 0x98badcfe,
+ 0x10325476,
+ 0xc3d2e1f0,
+};
-/* static uint32_t g_sha1_state[5] = {0}; */
+static const uint32_t g_sha256_state[8] = {
+ 0x6a09e667,
+ 0xbb67ae85,
+ 0x3c6ef372,
+ 0xa54ff53a,
+ 0x510e527f,
+ 0x9b05688c,
+ 0x1f83d9ab,
+ 0x5be0cd19,
+};
t_compression_entry g_compression_table[] = {
{"md5", {md5_compression_func, g_md5_state, sizeof(g_md5_state), 64}},
- /* {"sha1", {sha1_compression_func, g_sha1_state, sizeof(g_sha1_state), 64}} */
+ {"sha1", {sha1_compression_func, g_sha1_state, sizeof(g_sha1_state), 64}},
+ {"sha256", {sha256_compression_func, g_sha256_state, sizeof(g_sha256_state), 64}},
};
t_message_digest_param *dispatch_command(char *command)
diff --git a/src/sha1.c b/src/sha1.c
new file mode 100644
index 0000000..12369e8
--- /dev/null
+++ b/src/sha1.c
@@ -0,0 +1,80 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* sha1.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: charles <me@cacharle.xyz> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/08/02 14:20:39 by charles #+# #+# */
+/* Updated: 2020/08/02 15:42:51 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_ssl.h"
+
+void *sha1_compression_func(void *v_state, uint8_t *chunk)
+{
+ uint32_t buf[80] = {0};
+ size_t i;
+ uint32_t a, b, c, d, e, f, k, tmp;
+ uint32_t *state;
+
+ for (i = 0; i < 16; i++)
+ {
+ buf[i] = chunk[i * 4] << 24;
+ buf[i] |= chunk[i * 4 + 1] << 16;
+ buf[i] |= chunk[i * 4 + 2] << 8;
+ buf[i] |= chunk[i * 4 + 3];
+ }
+ /* ft_memcpy(buf, chunk, 64); */
+ i = 16;
+ while (i < 80)
+ {
+ buf[i] = rotate_left(buf[i - 3] ^ buf[i - 8] ^ buf[i - 14] ^ buf[i - 16], 1);
+ i++;
+ }
+ state = v_state;
+ a = state[0];
+ b = state[1];
+ c = state[2];
+ d = state[3];
+ e = state[4];
+
+ i = 0;
+ while (i < 80)
+ {
+ if (i < 20)
+ {
+ f = (b & c) | ((~b) & d);
+ k = 0x5a827999;
+ }
+ else if (i < 40)
+ {
+ f = b ^ c ^ d;
+ k = 0x6ed9eba1;
+ }
+ else if (i < 60)
+ {
+ f = (b & c) | (b & d) | (c & d);
+ k = 0x8f1bbcdc;
+ }
+ else
+ {
+ f = b ^ c ^ d;
+ k = 0xca62c1d6;
+ }
+ tmp = rotate_left(a, 5) + f + e + k + buf[i];
+ e = d;
+ d = c;
+ c = rotate_left(b, 30);
+ b = a;
+ a = tmp;
+ i++;
+ }
+ state[0] += a;
+ state[1] += b;
+ state[2] += c;
+ state[3] += d;
+ state[4] += e;
+ return (state);
+}
diff --git a/src/sha256.c b/src/sha256.c
new file mode 100644
index 0000000..29e2cde
--- /dev/null
+++ b/src/sha256.c
@@ -0,0 +1,79 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* sha256.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: charles <me@cacharle.xyz> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/08/02 16:36:43 by charles #+# #+# */
+/* Updated: 2020/08/02 16:46:32 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_ssl.h"
+
+static const uint32_t g_cube_roots[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
+};
+
+void *sha256_compression_func(void *v_state, uint8_t *chunk)
+{
+ uint32_t buf[64] = {0};
+ uint32_t *state = v_state;
+ uint32_t a, b, c, d, e, f, g, h, s0, s1, ch, temp1, temp2, maj;
+ size_t i;
+
+ ft_memcpy(buf, chunk, 64);
+
+ for (i = 16; i < 64; i++)
+ {
+ s0 = rotate_right(buf[i - 15], 7) ^ rotate_right(buf[i - 15], 18) ^ rotate_right(buf[i - 15], 3);
+ s1 = rotate_right(buf[i - 2], 17) ^ rotate_right(buf[i - 2], 19) ^ rotate_right(buf[i - 2], 10);
+ buf[i] = buf[i - 16] + s0 + buf[i - 7] + s1;
+ }
+
+ 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++)
+ {
+ s1 = rotate_right(e, 6) ^ rotate_right(e, 11) ^ rotate_right(e, 25);
+ ch = (e & f) ^ (~e & g);
+ temp1 = h + s1 + ch + g_cube_roots[i] + buf[i];
+ s0 = rotate_right(a, 2) ^ rotate_right(a, 13) ^ rotate_right(a, 22);
+ maj = (a & b) ^ (a & c) ^ (b & c);
+ temp2 = s0 + maj;
+
+ h = g;
+ g = f;
+ f = e;
+ e = d + temp1;
+ d = c;
+ c = b;
+ b = a;
+ a = temp1 + temp2;
+ }
+
+ state[0] += a;
+ state[1] += b;
+ state[2] += c;
+ state[3] += d;
+ state[4] += e;
+ state[5] += f;
+ state[6] += g;
+ state[7] += h;
+ return (state);
+}