diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2020-08-02 16:48:18 +0200 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2020-08-02 16:48:18 +0200 |
| commit | 49005573d3f6ee9123e827f71e694434b29251af (patch) | |
| tree | 305314a8760a00a900e0b4dfad1113d2ad560115 | |
| parent | c7ae1f028446165a1f9f4ab6a4bb63f8239c43b0 (diff) | |
| download | ft_ssl-49005573d3f6ee9123e827f71e694434b29251af.tar.gz ft_ssl-49005573d3f6ee9123e827f71e694434b29251af.tar.bz2 ft_ssl-49005573d3f6ee9123e827f71e694434b29251af.zip | |
Added sha1 and sha256 (not working)
| -rw-r--r-- | Makefile | 4 | ||||
| -rw-r--r-- | inc/ft_ssl.h | 4 | ||||
| -rw-r--r-- | src/main.c | 31 | ||||
| -rw-r--r-- | src/sha1.c | 80 | ||||
| -rw-r--r-- | src/sha256.c | 79 |
5 files changed, 191 insertions, 7 deletions
@@ -6,7 +6,7 @@ # By: cacharle <marvin@42.fr> +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/02/03 20:14:50 by cacharle #+# #+# # -# Updated: 2020/07/27 15:01:57 by charles ### ########.fr # +# Updated: 2020/08/02 15:51:02 by charles ### ########.fr # # # # **************************************************************************** # @@ -21,7 +21,7 @@ OBJ_DIR = obj OBJ_SUBDIR = $(shell find $(SRC_DIR) -type d | sed 's/src/obj/') CC = gcc -CCFLAGS = -I$(LIBFT_DIR)/include -I$(INCLUDE_DIR) -Wall -Wextra #-Werror +CCFLAGS = -I$(LIBFT_DIR)/include -I$(INCLUDE_DIR) -Wall -Wextra -Wpedantic #-Werror LDFLAGS = -L$(LIBFT_DIR) -lft INCLUDE = $(shell find $(INCLUDE_DIR) -type f -name "*.h") diff --git a/inc/ft_ssl.h b/inc/ft_ssl.h index c874287..2b16d53 100644 --- a/inc/ft_ssl.h +++ b/inc/ft_ssl.h @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/03 20:27:19 by cacharle #+# #+# */ -/* Updated: 2020/08/02 14:04:47 by charles ### ########.fr */ +/* Updated: 2020/08/02 16:37:45 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include <stdio.h> @@ -30,6 +30,8 @@ typedef struct } t_message_digest_param; void *md5_compression_func(void *v_state, uint8_t *chunk); +void *sha1_compression_func(void *v_state, uint8_t *chunk); +void *sha256_compression_func(void *v_state, uint8_t *chunk); char *message_digest(t_message_digest_param *param, uint8_t *message_origin, uint64_t size); @@ -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); +} |
