permutation.c 1.96 KB
Newer Older
Enrico Pozzobon committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
#include <stdio.h>
#include "isap.h"

#ifdef KECCAKP400
#include "lib/KeccakP-400-reference/KeccakP-400-SnP.h"
#elif defined(ASCON)
#include "lib/Ascon-reference/Ascon-reference.h"
#endif

void print_state(void* state){
	printf("     ");
	for (size_t i = 0; i < ISAP_STATE_SZ; i++) {
		printf("%02X",((unsigned char*)state)[i]);
	}
	printf("\n");
}

void Permutation_Initialize(
	void *state
){
	if(DEBUG) printf("   # Initializing state #\n");
	#ifdef KECCAKP400
	KeccakP400_Initialize(state);
	#elif defined(ASCON)
	Ascon_Initialize(state);
	#endif
	if(DEBUG) print_state(state);
}

void Permutation_AddBytes(
	void *state,
	const unsigned char *data,
	unsigned int offset,
	unsigned int length
){
	if(DEBUG) printf("   # Adding %u bytes starting at state[%u] #\n",length,offset);
	#ifdef KECCAKP400
	KeccakP400_AddBytes(state,data,offset,length);
	#elif defined(ASCON)
	Ascon_AddBytes(state,data,offset,length);
	#endif
	if(DEBUG) print_state(state);
}

void Permutation_OverwriteBytes(
	void *state,
	const unsigned char *data,
	unsigned int offset,
	unsigned int length
){
	if(DEBUG) printf("   # Overwriting %u bytes starting at state[%u] #\n",length,offset);
	#ifdef KECCAKP400
	KeccakP400_OverwriteBytes(state,data,offset,length);
	#elif defined(ASCON)
	Ascon_OverwriteBytes(state,data,offset,length);
	#endif
	if(DEBUG) print_state(state);
}

void Permutation_Permute_Nrounds(
	void *state,
	unsigned int nrounds
){
	if(DEBUG) printf("   # Permuting %u times #\n",nrounds);
	#ifdef KECCAKP400
	KeccakP400_Permute_Nrounds(state,nrounds);
	#elif defined(ASCON)
	Ascon_Permute_Nrounds(state,nrounds);
	#endif
	if(DEBUG) print_state(state);
}

void Permutation_ExtractBytes(
	const void *state,
	unsigned char *data,
	unsigned int offset,
	unsigned int length
){
	if(DEBUG) printf("   # Extracting %u bytes starting from state[%u] #\n",length,offset);
	#ifdef KECCAKP400
	KeccakP400_ExtractBytes(state,data,offset,length);
	#elif defined(ASCON)
	Ascon_ExtractBytes(state,data,offset,length);
	#endif
}