/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   sl_sha225_256.c                                    :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: bchanot <bchanot@student.42.fr>            +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2018/10/03 19:33:51 by bchanot           #+#    #+#             */
/*   Updated: 2018/10/10 12:45:45 by bchanot          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "ft_ssl.h"
#include "sl_sha2.h"

static void	sl_sha2_get_state(t_sha2 *ctx, t_uint32 m[64])
{
	t_process_sha2	t;
	size_t			i;

	sl_sha2_process_init_add(ctx, &t, true);
	i = -1;
	while (++i < 64)
		sl_sha2_get_registers(m, &t, i);
	sl_sha2_process_init_add(ctx, &t, false);
}

static void	sl_sha2_process(t_sha2 *ctx, const t_uint8 *data)
{
	t_uint32	m[64];
	size_t		i;
	size_t		j;

	i = -1;
	while (++i < 64)
	{
		if (i < 16)
		{
			m[i] = data[i * 4] & 0xff;
			j = 0;
			while (++j < 4)
			{
				m[i] <<= 8;
				m[i] |= data[i * 4 + j] & 0xff;
			}
		}
		else
			m[i] = SIG1_SHA2(m[i - 2]) + m[i - 7] +
				SIG0_SHA2(m[i - 15]) + m[i - 16];
	}
	sl_sha2_get_state(ctx, m);
}

void		sl_sha2_update(t_sha2 *ctx, const t_uint8 *data, size_t len)
{
	size_t	index;
	size_t	i;

	index = (size_t)((ctx->count[0] >> 3) & 0x3f);
	if ((ctx->count[0] += (len << 3)) < (len << 3))
	{
		ctx->count[1]++;
		ctx->count[1] += (len >> 29);
	}
	i = 0;
	if (len >= (sizeof(ctx->buff) - index))
	{
		ft_memcpy(&ctx->buff[index], data, (sizeof(ctx->buff) - index));
		sl_sha2_process(ctx, ctx->buff);
		i = (sizeof(ctx->buff) - index);
		while (i + (sizeof(ctx->buff) - 1) < len)
		{
			sl_sha2_process(ctx, &data[i]);
			i += sizeof(ctx->buff);
		}
		index = 0;
	}
	ft_memcpy(&ctx->buff[index], &data[i], len - i);
}

void		sl_sha2_get_hash(t_sha2 *ctx, t_uint8 hash[32])
{
	size_t	state;
	size_t	i;
	size_t	j;
	int		k;

	i = -1;
	j = 0;
	while (++i < 8)
	{
		state = ctx->state[i];
		k = 4;
		while (--k >= 0)
		{
			hash[j + k] = state;
			state >>= 8;
		}
		j += 4;
	}
}

void		sl_sha2_final(t_sha2 *ctx)
{
	static const t_uint8	padding[64] = {0x80, };
	t_uint8					bits[8];
	size_t					pad_len;
	size_t					t;
	int						i;

	t = ctx->count[0];
	pad_len = 0;
	i = 8;
	while (--i >= 0)
	{
		bits[i] = t;
		t = !(i % 4) ? ctx->count[++pad_len] : (t >> 8);
	}
	t = (ctx->count[0] >> 3) & 0x3f;
	pad_len = (t < 56) ? (56 - t) : ((sizeof(ctx->buff) + 56) - t);
	sl_sha2_update(ctx, padding, pad_len);
	sl_sha2_update(ctx, bits, sizeof(bits));
}
