aboutsummaryrefslogblamecommitdiffstats
path: root/libhashsum_init_keccak_hasher.c
blob: 45e8627282b3e8c41c4e17de63c076200d65bba6 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11




                                                         
                                                                                                                                


                                                

                              



























                                                                 
                                                                              














                                                                




                                                                                                                      

                                                                    
                                                                             



     
                                                                                                                                
                    
                        



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


int
libhashsum_init_keccak_hasher(struct libhashsum_hasher *this, size_t ratebits, size_t capbits, size_t hashbits, size_t squeezes)
{
	struct libkeccak_generalised_spec gspec;
	struct libkeccak_spec spec;

	if (!squeezes)
		squeezes = 1U;

	if ((ratebits | capbits | hashbits) > (size_t)LONG_MAX) {
		errno = EINVAL;
		return -1;
	} else if (!ratebits || !capbits || !hashbits) {
		libkeccak_generalised_spec_initialise(&gspec);
		if (ratebits)
			gspec.bitrate = (long int)ratebits;
		if (capbits)
			gspec.capacity = (long int)capbits;
		if (hashbits)
			gspec.output = (long int)hashbits;
		if (libkeccak_degeneralise_spec(&gspec, &spec)) {
			errno = EINVAL;
			return -1;
		}
		ratebits = (size_t)spec.bitrate;
		capbits = (size_t)spec.capacity;
		hashbits = (size_t)spec.output;
	} else {
		spec.bitrate = (long int)ratebits;
		spec.capacity = (long int)capbits;
		spec.output = (long int)hashbits;
	}
	if (libkeccak_spec_check(&spec)) {
		errno = EINVAL;
		return -1;
	}

	this->algorithm = LIBHASHSUM_KECCAK;
	if ((capbits >> 1) == hashbits && !(capbits & 1U) && squeezes == 1U &&
	    1600U >= capbits && ratebits == 1600U - capbits) {
		if (hashbits == 224U) {
			this->algorithm = LIBHASHSUM_KECCAK_224;
			this->algorithm_string = "Keccak-224";
		} else if (hashbits == 256U) {
			this->algorithm = LIBHASHSUM_KECCAK_256;
			this->algorithm_string = "Keccak-256";
		} else if (hashbits == 384U) {
			this->algorithm = LIBHASHSUM_KECCAK_384;
			this->algorithm_string = "Keccak-384";
		} else if (hashbits == 512U) {
			this->algorithm = LIBHASHSUM_KECCAK_512;
			this->algorithm_string = "Keccak-512";
		}
	}
	if (this->algorithm == LIBHASHSUM_KECCAK) {
		if (squeezes > 1U) {
			sprintf(this->state.keccak.algostr, "Keccak[r=%zu,c=%zu,n=%zu,z=%zu]",
			        ratebits, capbits, hashbits, squeezes);
		} else {
			sprintf(this->state.keccak.algostr, "Keccak[r=%zu,c=%zu,n=%zu]", ratebits, capbits, hashbits);
		}
		this->algorithm_string = this->state.keccak.algostr;
	}

	return libhashsum_init_keccak__(this, hashbits, &spec, squeezes, "");
}


#else
int
libhashsum_init_keccak_hasher(struct libhashsum_hasher *this, size_t ratebits, size_t capbits, size_t hashbits, size_t squeezes)
{
	(void) this;
	(void) ratebits;
	(void) capbits;
	(void) hashbits;
	errno = ENOSYS;
	return -1;
}
#endif