aboutsummaryrefslogblamecommitdiffstats
path: root/libhashsum_init_blake512_hasher.c
blob: b80d4e8aabfeaff1072816cff7df3d45a3da2343 (plain) (tree)




























                                                                                                   


                                                             



                                                                                                             
                                                                      
                                                                           




















                                                                                                    


                                                             





                                                                                               
                                                                                                         
                                                                           



                 






















                                                               
   
                                                                                 


                                              
                                                                                           



                                                        

                                                                                 
                                   


                                               
                             
                             
                                                               





                 
                                                                                 

                    
                    



                       
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifdef SUPPORT_BLAKE512


LIBHASHSUM_1_NONNULL_
static size_t
process(struct libhashsum_hasher *this, const void *data, size_t bytes)
{
	return libblake_blake512_update(&this->state.blake512.s, data, bytes);
}


LIBHASHSUM_1_NONNULL_
static int
finalise_const(struct libhashsum_hasher *this, const void *data, size_t bytes, unsigned extra_bits)
{
	const uint8_t *m = data;
	size_t r;

	if (extra_bits > 7U) {
		errno = EINVAL;
		return -1;
	}

	r = libblake_blake512_update(&this->state.blake512.s, data, bytes);
	m = &m[r];
	bytes -= r;

	if (!this->hash_output)
		this->hash_output = this->state.blake512.buf;

	memcpy(this->state.blake512.buf, m, bytes + (size_t)(extra_bits > 0U));
	if (extra_bits)
		this->state.blake512.buf[bytes] = libhashsum_reverse_byte__(this->state.blake512.buf[bytes]);
	libblake_blake512_digest(&this->state.blake512.s, this->state.blake512.buf, bytes,
	                         extra_bits, NULL, this->hash_output);
	memset(&this->state.blake512.s, 0, sizeof(this->state.blake512.s));
	return 0;
}


LIBHASHSUM_1_NONNULL_
static int
finalise(struct libhashsum_hasher *this, void *data, size_t bytes, unsigned extra_bits, size_t size)
{
	uint8_t *m = data;
	size_t r;

	if (extra_bits > 7U) {
		errno = EINVAL;
		return -1;
	}

	r = libblake_blake512_update(&this->state.blake512.s, data, bytes);
	m = &m[r];
	bytes -= r;
	size -= r;

	if (!this->hash_output)
		this->hash_output = this->state.blake512.buf;

	if (size < libblake_blake512_digest_get_required_input_size(bytes, extra_bits, NULL)) {
		memcpy(this->state.blake512.buf, m, bytes + (size_t)(extra_bits > 0U));
		m = this->state.blake512.buf;
	}
	if (extra_bits)
		m[bytes] = libhashsum_reverse_byte__(m[bytes]);
	libblake_blake512_digest(&this->state.blake512.s, m, bytes, extra_bits, NULL, this->hash_output);
	memset(&this->state.blake512.s, 0, sizeof(this->state.blake512.s));
	return 0;
}


static const char *
mkalgostr(char *buf, const char *name, const uint8_t *salt)
{
	char *p;
	size_t i;
	if (!salt)
		return name;
	for (i = 0; i < 32U; i++)
		if (salt[i])
			goto nonzero;
	return name;
nonzero:
	p = stpcpy(stpcpy(buf, name), "[salt=");
	for (i = 0; i < 32U; i++) {
		*p++ = "0123456789abcdef"[(salt[i] >> 4) & 15];
		*p++ = "0123456789abcdef"[(salt[i] >> 0) & 15];
	}
	*p++ = ']';
	*p++ = '\0';
	return buf;
}


int
libhashsum_init_blake512_hasher(struct libhashsum_hasher *this, const void *salt)
{
	libblake_init();
	this->algorithm = LIBHASHSUM_BLAKE512;
	this->algorithm_string = mkalgostr(this->state.blake512.algostr, "BLAKE512", salt);
	this->input_block_size = 128U;
	this->hash_size = LIBHASHSUM_BLAKE512_HASH_SIZE;
	this->hash_output = NULL;
	this->supports_non_whole_bytes = 1;
	this->standard_partial_byte_input_encoding = LIBHASHSUM_MOST_SIGNIFICANT;
	this->standard_partial_byte_output_encoding = LIBHASHSUM_UNSUPPORTED;
	this->hash_excess_bits = 0;
	this->process = &process;
	this->finalise_const = &finalise_const;
	this->finalise = &finalise;
	this->stretch = NULL;
	this->destroy = NULL;
	libblake_blake512_init2(&this->state.blake512.s, salt);
	return 0;
}


#else
int
libhashsum_init_blake512_hasher(struct libhashsum_hasher *this, const void *salt)
{
	(void) this;
	(void) salt;
	errno = ENOSYS;
	return -1;
}
#endif