diff --git a/drygascon/Implementations/crypto_aead/drygascon128/add_arm-cm0/api.h b/drygascon/Implementations/crypto_aead/drygascon128/add_arm-cm0/api.h new file mode 100644 index 0000000..a4aa567 --- /dev/null +++ b/drygascon/Implementations/crypto_aead/drygascon128/add_arm-cm0/api.h @@ -0,0 +1,5 @@ +#define CRYPTO_KEYBYTES 16 +#define CRYPTO_NSECBYTES 0 +#define CRYPTO_NPUBBYTES 16 +#define CRYPTO_ABYTES 16 +#define CRYPTO_NOOVERLAP 1 diff --git a/drygascon/Implementations/crypto_aead/drygascon128/add_arm-cm0/bytes_utils.h b/drygascon/Implementations/crypto_aead/drygascon128/add_arm-cm0/bytes_utils.h new file mode 100644 index 0000000..3e8bfee --- /dev/null +++ b/drygascon/Implementations/crypto_aead/drygascon128/add_arm-cm0/bytes_utils.h @@ -0,0 +1,129 @@ + +#ifndef __BYTES_UTILS_H__ +#define __BYTES_UTILS_H__ + +#include +#include +#include + +//#ifndef bytes_utiles_printf +//#define bytes_utiles_printf printf +//#endif +#ifndef bytes_utiles_printf +#define bytes_utiles_printf printf +#endif + +//replace 0 by . +static void print_diff_byte(uint8_t d, const char *sep){ + unsigned int n=d>>4; + if(0==n) bytes_utiles_printf("."); else bytes_utiles_printf("%X",n); + n = d & 0xF; + if(0==n) bytes_utiles_printf("."); else bytes_utiles_printf("%X",n); + bytes_utiles_printf("%s",sep); +} +static void print_diff_bytes_sep(const char *msg,const void *vbuf, unsigned int size, const char *m2, const char *sep){ + const uint8_t*const buf = (const uint8_t*const)vbuf; + bytes_utiles_printf("%s",msg); + if(size){ + unsigned int i; + for(i=0;i(len/2)) + dst_size = (len/2); + memset(dst,0,dst_size); + for(unsigned int i=0;i +typedef uint64_t DRYSPONGE_EXT_t; + +#define DRYSPONGE_EXT + +#include "drysponge_common.h" + +//input width for one round of MixPhaseRound +#define DRYSPONGE_MPR_INPUT_MASK ((((uint64_t)1)<16 + #error "DRYSPONGE_XSIZE32>16" +#endif + +#if DRYSPONGE_XSIZE32 == 4 + #define DRYSPONGE_X_IDX_WIDTH 2 +#endif + +#if DRYSPONGE_MPR_INPUT_WIDTH == 10 + #define DRYSPONGE_RANK_BYTES 2 + typedef uint32_t permut_rank_t; +#endif +#if DRYSPONGE_MPR_INPUT_WIDTH == 18 + #define DRYSPONGE_RANK_BYTES 3 + typedef uint32_t permut_rank_t; +#endif + +#define DRYSPONGE_X_IDX_MASK ((1<>shift) & DRYSPONGE_MPR_INPUT_MASK; + r^=ext; + for(unsigned int j=0;j> DRYSPONGE_X_IDX_WIDTH; + c64[j]^=x32[i]; + } +} +#endif + +struct DRYSPONGE_struct_t; +typedef struct DRYSPONGE_struct_t DRYSPONGE_t ; +DRYSPONGE_FUNC void DRYSPONGE_MixPhase( + DRYSPONGE_t *const ctx, + const uint8_t *const in +); +DRYSPONGE_FUNC void DRYSPONGE_CoreRound( + DRYSPONGE_t *const ctx, + unsigned int r +); + +#include "drysponge_le32.h" + +#ifndef DRYSPONGE_OPT_F +DRYSPONGE_FUNC void DRYSPONGE_MixPhase( + DRYSPONGE_t *const ctx, + const uint8_t *const in +){ + unsigned int bitidx=0; + #if DRYSPONGE_MPR_ROUNDS > 1 + for(unsigned int i=0;i= 4 + printf("Mix phase MixPhaseRound entry %lu:\n",i); + DRYSPONGE_print_state(ctx); + #endif + DRYSPONGE_EXT_t ext=0; + #if ((DRYSPONGE_MPR_ROUNDS-1)*(DRYSPONGE_MPR_INPUT_WIDTH))>(DRYSPONGE_BLOCKSIZE*8) + if((ctx->ext) && (i==(DRYSPONGE_MPR_ROUNDS-2))){ + //DS info is split accross this block and the last one + ext = ctx->ext; + ctx->ext = ctx->ext >> ((DRYSPONGE_BLOCKSIZE*8)%DRYSPONGE_MPR_INPUT_WIDTH); + ctx->ext = ctx->ext >> ((((DRYSPONGE_MPR_ROUNDS-1)*DRYSPONGE_MPR_INPUT_WIDTH))-(DRYSPONGE_BLOCKSIZE*8)); + } + #endif + DRYSPONGE_MixPhaseRound(ext,ctx->c,ctx->x,in,bitidx,DRYSPONGE_BLOCKSIZE); + bitidx+=DRYSPONGE_MPR_INPUT_WIDTH; + #if DRYSPONGE_DBG_EN >= 4 + printf("Mix phase CoreRound entry %lu:\n",i); + DRYSPONGE_print_state(ctx); + #endif + DRYSPONGE_CoreRound(ctx,0); + } + #endif + #if DRYSPONGE_DBG_EN >= 4 + printf("Mix phase MixPhaseRound entry %lu:\n",DRYSPONGE_MPR_ROUNDS-1); + DRYSPONGE_print_state(ctx); + #endif + DRYSPONGE_MixPhaseRound(ctx->ext,ctx->c,ctx->x,in,bitidx,DRYSPONGE_BLOCKSIZE); + ctx->ext=0; +} +#endif + +//#ifndef DRYSPONGE_OPT_G //keep for now, needed for key init +DRYSPONGE_FUNC void gascon_sboxes(uint64_t * const x, unsigned int nw){ + uint64_t t[DRYSPONGE_CAPACITYSIZE64]; + const unsigned int mid = nw/2; + for(unsigned int i=0;i 5 + x[5] ^= gascon_rotr64_interleaved(x[5], 31) ^ gascon_rotr64_interleaved(x[5], 26); + x[6] ^= gascon_rotr64_interleaved(x[6], 53) ^ gascon_rotr64_interleaved(x[6], 58); + x[7] ^= gascon_rotr64_interleaved(x[7], 9) ^ gascon_rotr64_interleaved(x[7], 46); + x[8] ^= gascon_rotr64_interleaved(x[8], 43) ^ gascon_rotr64_interleaved(x[8], 50); + #endif +} +DRYSPONGE_FUNC void gascon_permutation_round(uint64_t* S, unsigned int round) { + (void)DRYSPONGE_rotr64; + // addition of round constant + gascon_add_cst(S, round); + // substitution layer + gascon_sboxes(S,DRYSPONGE_CAPACITYSIZE64); + // linear diffusion layer + gascon_lin_layer(S); +} + +DRYSPONGE_FUNC void DRYSPONGE_CoreRound( + DRYSPONGE_t *const ctx, + unsigned int r +){ + gascon_permutation_round(ctx->c, r); +} + +#endif diff --git a/drygascon/Implementations/crypto_aead/drygascon128/add_arm-cm0/drysponge.h b/drygascon/Implementations/crypto_aead/drygascon128/add_arm-cm0/drysponge.h new file mode 100644 index 0000000..a351347 --- /dev/null +++ b/drygascon/Implementations/crypto_aead/drygascon128/add_arm-cm0/drysponge.h @@ -0,0 +1 @@ +#include "drygascon128_le32.h" diff --git a/drygascon/Implementations/crypto_aead/drygascon128/add_arm-cm0/drysponge_common.h b/drygascon/Implementations/crypto_aead/drygascon128/add_arm-cm0/drysponge_common.h new file mode 100644 index 0000000..36792aa --- /dev/null +++ b/drygascon/Implementations/crypto_aead/drygascon128/add_arm-cm0/drysponge_common.h @@ -0,0 +1,141 @@ +#ifndef __DRYSPONGE_COMMON_H__ +#define __DRYSPONGE_COMMON_H__ + +#ifndef DRYSPONGE_FUNC +#define DRYSPONGE_FUNC inline static +#endif + +//convention: +// width means length in bits +// size means length in bytes + +#include +#include +#include + +#if DRYSPONGE_DBG_EN +#include "bytes_utils.h" +#endif + +#define DRYSPONGE_PASS 0 + +#define DRYSPONGE_DS 2 +#define DRYSPONGE_DD 1 +#define DRYSPONGE_DA 2 +#define DRYSPONGE_DM 3 + +#define DRYSPONGE_STATESIZE (DRYSPONGE_CAPACITYSIZE+DRYSPONGE_BLOCKSIZE) +#define DRYSPONGE_DIGESTSIZE (DRYSPONGE_KEYSIZE*2) +#define DRYSPONGE_TAGSIZE DRYSPONGE_KEYSIZE +#define DRYSPONGE_KEYMAXSIZE (DRYSPONGE_CAPACITYSIZE+DRYSPONGE_XSIZE) + +#define DRYSPONGE_DIVUP(a,b) (((a)+(b)-1)/(b)) +#define DRYSPONGE_ROTR32(x,n) (0xFFFFFFFF & (((x)>>(n))|((x)<<(0x1F & (32-(n)))))) +#define DRYSPONGE_ROTR64(x,n) (0xFFFFFFFFFFFFFFFF & (((x)>>(n))|((x)<<(0x3F & (64-(n)))))) + +#define DRYSPONGE_STATESIZE32 DRYSPONGE_DIVUP(DRYSPONGE_STATESIZE,4) +#define DRYSPONGE_CE_SIZE32 DRYSPONGE_DIVUP(DRYSPONGE_CE_SIZE,4) +#define DRYSPONGE_BLOCKSIZE32 DRYSPONGE_DIVUP(DRYSPONGE_BLOCKSIZE,4) +#define DRYSPONGE_CAPACITYSIZE32 DRYSPONGE_DIVUP(DRYSPONGE_CAPACITYSIZE,4) +#define DRYSPONGE_XSIZE32 DRYSPONGE_DIVUP(DRYSPONGE_XSIZE,4) +#define DRYSPONGE_KEYSIZE32 DRYSPONGE_DIVUP(DRYSPONGE_KEYSIZE,4) + +#define DRYSPONGE_STATESIZE64 DRYSPONGE_DIVUP(DRYSPONGE_STATESIZE,8) +#define DRYSPONGE_CE_SIZE64 DRYSPONGE_DIVUP(DRYSPONGE_CE_SIZE,8) +#define DRYSPONGE_BLOCKSIZE64 DRYSPONGE_DIVUP(DRYSPONGE_BLOCKSIZE,8) +#define DRYSPONGE_CAPACITYSIZE64 DRYSPONGE_DIVUP(DRYSPONGE_CAPACITYSIZE,8) +#define DRYSPONGE_XSIZE64 DRYSPONGE_DIVUP(DRYSPONGE_XSIZE,8) +#define DRYSPONGE_KEYSIZE64 DRYSPONGE_DIVUP(DRYSPONGE_KEYSIZE,8) +#define DRYSPONGE_TAGSIZE64 DRYSPONGE_DIVUP(DRYSPONGE_TAGSIZE,8) +#define DRYSPONGE_KEYMAXSIZE64 DRYSPONGE_DIVUP(DRYSPONGE_KEYMAXSIZE,8) +#define DRYSPONGE_NONCESIZE64 DRYSPONGE_DIVUP(DRYSPONGE_NONCESIZE,8) + +#if DRYSPONGE_NONCESIZE < 12 + #error "DRYSPONGE_NONCESIZE < 12" +#endif + +#if DRYSPONGE_KEYSIZE < 16 + #error "DRYSPONGE_KEYSIZE < 16" +#endif + +#if DRYSPONGE_DIGESTSIZE < 2*DRYSPONGE_KEYSIZE + #error "DRYSPONGE_DIGESTSIZE < 2*DRYSPONGE_KEYSIZE" +#endif + +#if DRYSPONGE_ACCUMULATE_FACTOR > ((DRYSPONGE_CAPACITYSIZE/4)/DRYSPONGE_BLOCKSIZE32) + #error "DRYSPONGE_ACCUMULATE_FACTOR > ((DRYSPONGE_CAPACITYSIZE/4)/DRYSPONGE_BLOCKSIZE32)" +#endif + +#ifdef DRYSPONGE_EXT +#define DRYSPONGE_EXT_ARG (&(ctx->ext)) +#else +#define DRYSPONGE_EXT_ARG 0 +#endif + +DRYSPONGE_FUNC unsigned int DRYSPONGE_DSINFO(unsigned int padded, unsigned int domain, unsigned int finalize){ + #if DRYSPONGE_DBG_EN + bytes_utiles_printf(" Adding DS: padded=%d, domain=%u, finalize=%d\n",padded,domain,finalize); + #endif + return padded+(finalize<<1)+(domain<<2); +} + +DRYSPONGE_FUNC uint32_t DRYSPONGE_rotr32(uint32_t x, unsigned int n){ + assert(n<32); + return DRYSPONGE_ROTR32(x,n); +} + +DRYSPONGE_FUNC uint64_t DRYSPONGE_rotr64(uint64_t x, unsigned int n){ + assert(n<64); + return DRYSPONGE_ROTR64(x,n); +} + +DRYSPONGE_FUNC void DRYSPONGE_xor( + const uint8_t *const a,//exactly one block of input + const uint8_t *const b, + uint8_t *const y +){ + for(unsigned int i=0;i> (8*i); + } +} + +DRYSPONGE_FUNC void DRYSPONGE_load64(uint64_t* x, uint8_t* in) { + *x = 0; + for(unsigned int i = 0;i<8;i++){ + uint64_t b = in[i]; + *x = *x | (b<<(8*i)); + } +} + +DRYSPONGE_FUNC void DRYSPONGE_store64(uint8_t* out, uint64_t x) { + (void)DRYSPONGE_rotr32; + (void)DRYSPONGE_load16; + (void)DRYSPONGE_store32; + for(unsigned int i = 0;i<8;i++){ + out[i] = x >> (8*i); + } +} + +#endif diff --git a/drygascon/Implementations/crypto_aead/drygascon128/add_arm-cm0/drysponge_dbg_support.h b/drygascon/Implementations/crypto_aead/drygascon128/add_arm-cm0/drysponge_dbg_support.h new file mode 100644 index 0000000..5b6eafc --- /dev/null +++ b/drygascon/Implementations/crypto_aead/drygascon128/add_arm-cm0/drysponge_dbg_support.h @@ -0,0 +1,49 @@ +#ifndef __DRYSPONGE_DBG_SUPPORT_H__ +#define __DRYSPONGE_DBG_SUPPORT_H__ + +#define DRYSPONGE_DBG_NONE 0 +#define DRYSPONGE_DBG_ALG_IO 1 +#define DRYSPONGE_DBG_F_IO 2 +#define DRYSPONGE_DBG_ROUND_IO 3 +#define DRYSPONGE_DBG_FULL 4 + +#if DRYSPONGE_DBG_EN + #define DRYSPONGE_DBG(a) a; +#else + #define DRYSPONGE_DBG(a) +#endif + + +#if DRYSPONGE_DBG_EN +#include +#include +#include "bytes_utils.h" +static void DRYSPONGE_print_state( + DRYSPONGE_t *const ctx +){ + (void)xor_bytes; + (void)println_128; + (void)bytes_utils_remove_unused_warnings; + unsigned int linesize = 32; + if(linesizec; + for(unsigned int i=0;ix; + for(unsigned int i=0;ic),ctx->rounds); +} +#else +static void DRYSPONGE_g( + DRYSPONGE_t *const ctx +){ + #if DRYSPONGE_DBG_EN + printf(" G entry %lu:\n",ctx->fcnt); + DRYSPONGE_print_state(ctx); + #endif + ctx->fcnt++; + DRYSPONGE_xor64(ctx->r,ctx->r,ctx->r);//r=0 + for(unsigned int j = 0;jrounds;j++){ + #if DRYSPONGE_DBG_EN >= DRYSPONGE_DBG_ROUND_IO + printf(" CoreRound entry %d:\n",j); + DRYSPONGE_print_state(ctx); + #endif + DRYSPONGE_CoreRound(ctx,j); + uint32_t r32[DRYSPONGE_BLOCKSIZE32]; + uint32_t cpart[DRYSPONGE_BLOCKSIZE32]; + memcpy(r32,ctx->r,sizeof(r32)); + for(unsigned int k=0;kc+k*DRYSPONGE_BLOCKSIZE64,sizeof(cpart)); + for(unsigned int i=0;ir,r32,sizeof(r32)); + } +} +#endif + +#ifdef DRYSPONGE_OPT_F +static void DRYSPONGE_DomainSeparator( + DRYSPONGE_EXT_t *const ext, + unsigned int dsinfo +){ + *ext = dsinfo; +} +void drygascon128_f(uint64_t* x, uint32_t*in,uint32_t ds,uint32_t rounds); +static void DRYSPONGE_f( + DRYSPONGE_t *const ctx, + const uint8_t *const i +){ + DRYSPONGE_OPT_F((uint64_t*)&(ctx->c),(uint32_t*)i,(uint32_t)ctx->ext,ctx->rounds); + ctx->ext=0; +} +#else +static void DRYSPONGE_f( + DRYSPONGE_t *const ctx, + const uint8_t *const i +){ + #if DRYSPONGE_DBG_EN + printf(" F entry %lu:\n",ctx->fcnt); + DRYSPONGE_print_state(ctx); + print_bytes_sep(" I = ",i,DRYSPONGE_BLOCKSIZE,"\n",""); + #endif + DRYSPONGE_MixPhase(ctx,i); + #if DRYSPONGE_DBG_EN >= DRYSPONGE_DBG_ROUND_IO + printf(" After mix phase:\n"); + DRYSPONGE_print_state(ctx); + #endif + DRYSPONGE_g(ctx); +} +#endif + +static void DRYSPONGE_set_key( + DRYSPONGE_t *const ctx, + const uint8_t *const key, + const unsigned int keylen +){ + assert(DRYSPONGE_KEYSIZE<=keylen); + const unsigned int midkeysize = DRYSPONGE_KEYSIZE+DRYSPONGE_XSIZE; + const unsigned int fullkeysize = DRYSPONGE_CAPACITYSIZE+DRYSPONGE_XSIZE; + if(DRYSPONGE_KEYSIZE!=keylen){//all words for x assumed to be different + if(fullkeysize == keylen){ + memcpy(ctx->c,key,DRYSPONGE_CAPACITYSIZE); + memcpy(ctx->x,key+DRYSPONGE_CAPACITYSIZE,DRYSPONGE_XSIZE); + } else { + uint8_t c[DRYSPONGE_CAPACITYSIZE]; + uint8_t x[DRYSPONGE_XSIZE]; + assert(midkeysize==keylen); + for(unsigned int i=0;ic,c,DRYSPONGE_CAPACITYSIZE); + memcpy(ctx->x,x,DRYSPONGE_XSIZE); + } + }else{ + uint8_t c[DRYSPONGE_CAPACITYSIZE]; + for(unsigned int i=0;ic,c,DRYSPONGE_CAPACITYSIZE); + DRYSPONGE_CoreRound(ctx,0); + //need to fixup x such that all words are different + unsigned int modified=1; + while(modified){ + uint32_t c32[DRYSPONGE_CAPACITYSIZE32]; + memcpy(c32,ctx->c,DRYSPONGE_CAPACITYSIZE); + modified=0; + for(unsigned int i=0;ix,ctx->c,DRYSPONGE_XSIZE); + memcpy(ctx->c,key,DRYSPONGE_XSIZE); + } + uint32_t x32[DRYSPONGE_XSIZE32];// = (uint32_t *const)ctx->x; + memcpy(x32,ctx->x,DRYSPONGE_XSIZE); + //sanity check: all words in x shall be different + for(unsigned int i=0;i DRYSPONGE_BLOCKSIZE ? DRYSPONGE_BLOCKSIZE : remaining; + memcpy(out,ctx->r,len); + out+=len; + remaining-=len; + if(remaining){ + DRYSPONGE_g(ctx); + } + } +} + +static void DRYSPONGE_init_ctx( + DRYSPONGE_t *const ctx +){ + #ifdef DRYSPONGE_EXT + memset(DRYSPONGE_EXT_ARG,0,sizeof(DRYSPONGE_EXT_t)); + #endif + ctx->fcnt=0; + memset(ctx->r,0x00,DRYSPONGE_BLOCKSIZE); +} + +static void DRYSPONGE_hash( + const uint8_t *const message, + const size_t mlen, + uint8_t *const digest +){ + DRYSPONGE_t ctx_storage; + DRYSPONGE_t *const ctx = &ctx_storage; + DRYSPONGE_init_ctx(ctx); + ctx->rounds=DRYSPONGE_ROUNDS; + #if DRYSPONGE_DBG_EN + printf("Hashing %lu bytes message: ",mlen); + print_bytes_sep("",message,mlen,"\n",""); + #endif + const uint64_t CST_H[] = { + 0xd308a385886a3f24, + 0x447370032e8a1913, + 0xd0319f29223809a4, + 0x896c4eec98fa2e08, + 0x7713d038e6212845, + 0x6c0ce934cf6654be, + 0xdd507cc9b729acc0, + 0x170947b5b5d5843f, + 0x1bfb7989d9d51692, + 0xacb5df98a60b31d1, + 0xb7df1ad0db72fd2f, + 0x967e266aedafe1b8, + 0x997f2cf145907cba, + 0xf76c91b34799a124, + 0x16fc8e85e2f20108, + 0x694e5771d8206963, + }; + DRYSPONGE_set_key(ctx,(const uint8_t*)CST_H,DRYSPONGE_KEYSIZE+DRYSPONGE_XSIZE); + DRYSPONGE_absorb_only(ctx,message,mlen,DRYSPONGE_DS,1); + DRYSPONGE_squeez_only(ctx,digest,DRYSPONGE_DIGESTSIZE); + #if DRYSPONGE_DBG_EN + printf(" Final state:\n"); + DRYSPONGE_print_state(ctx); + print_bytes_sep(" Digest: ",digest,DRYSPONGE_DIGESTSIZE,"\n",""); + #endif +} + +static void DRYSPONGE_init( + DRYSPONGE_t *const ctx, + const uint8_t *const key, + const unsigned int klen, + const uint8_t *const nonce, + uint8_t *out_buffer,//output buffer + unsigned int finalize +){ + DRYSPONGE_init_ctx(ctx); + ctx->rounds=DRYSPONGE_ROUNDS; + DRYSPONGE_set_key(ctx,key,klen); + ctx->obuf = out_buffer; + DRYSPONGE_DomainSeparator(DRYSPONGE_EXT_ARG,DRYSPONGE_DSINFO(0,DRYSPONGE_DD,finalize)); + ctx->rounds=DRYSPONGE_INIT_ROUNDS; + #if DRYSPONGE_NONCESIZE>DRYSPONGE_BLOCKSIZE + assert(0==(DRYSPONGE_NONCESIZE%DRYSPONGE_BLOCKSIZE)); + unsigned int nloops = DRYSPONGE_DIVUP(DRYSPONGE_NONCESIZE,DRYSPONGE_BLOCKSIZE); + for(unsigned int i=0;irounds=DRYSPONGE_ROUNDS; +} + +static void DRYSPONGE_enc_core( + DRYSPONGE_t *const ctx, + const uint64_t *const ib//exactly one block of input +){ + + DRYSPONGE_xor((uint8_t *)ctx->r,(uint8_t *)ib,ctx->obuf); + DRYSPONGE_f(ctx,(uint8_t *)ib); + ctx->obuf+=DRYSPONGE_BLOCKSIZE; +} + +static void DRYSPONGE_enc_core_aligned( + DRYSPONGE_t *const ctx, + const uint64_t *const ib//exactly one block of input +){ + assert((((uintptr_t)ctx->obuf)%8) == 0); + DRYSPONGE_xor64(ctx->r,ib,(uint64_t*const)ctx->obuf); + DRYSPONGE_f(ctx,(uint8_t *)ib); + ctx->obuf+=DRYSPONGE_BLOCKSIZE; +} + +static const uint8_t* DRYSPONGE_enc_blocks( + DRYSPONGE_t *const ctx, + const uint8_t *im,//whole message + size_t m +){ + (void)DRYSPONGE_load32; + (void)DRYSPONGE_store32; + (void)DRYSPONGE_load64; + (void)DRYSPONGE_store64; + uint64_t buf64[DRYSPONGE_BLOCKSIZE64]; + const uint64_t *ib64; + #if DRYSPONGE_BLOCKSIZE % ALIGN64 + unsigned int input_aligned = 0; + unsigned int output_aligned = 0; + #else + unsigned int input_aligned = 0==(((uintptr_t)im)%ALIGN64); + unsigned int output_aligned = 0==(((uintptr_t)ctx->obuf)%ALIGN64); + #endif + if(input_aligned && output_aligned){ + for(size_t i = 0; ir,ib,ctx->obuf); + DRYSPONGE_f(ctx,ctx->obuf); + ctx->obuf+=DRYSPONGE_BLOCKSIZE; +} + +static void DRYSPONGE_dec_core_aligned( + DRYSPONGE_t *const ctx, + const uint64_t *const ib//exactly one block of input +){ + DRYSPONGE_xor64(ctx->r,ib,(uint64_t*const)ctx->obuf); + DRYSPONGE_f(ctx,ctx->obuf); + ctx->obuf+=DRYSPONGE_BLOCKSIZE; +} + +static const uint8_t* DRYSPONGE_dec_blocks( + DRYSPONGE_t *const ctx, + const uint8_t *im,//whole message + size_t m +){ + const uint64_t *ib64; + #if DRYSPONGE_BLOCKSIZE % ALIGN64 + unsigned int input_aligned = 0; + unsigned int output_aligned = 0; + #else + unsigned int input_aligned = 0==(((uintptr_t)im)%ALIGN64); + unsigned int output_aligned = 0==(((uintptr_t)ctx->obuf)%ALIGN64); + #endif + if(input_aligned && output_aligned){ + for(size_t i = 0; iobuf = ciphertext + mlen;//fix the size + } + DRYSPONGE_squeez_only(ctx,ctx->obuf,DRYSPONGE_TAGSIZE); + *clen = mlen+DRYSPONGE_TAGSIZE; + #if DRYSPONGE_DBG_EN + printf(" Final state:\n"); + DRYSPONGE_print_state(ctx); + print_bytes_sep(" CipherText: ",ciphertext,*clen,"\n",""); + #endif +} + +//WARNING the function writes plaintext into "message" before checking the tag. +//It is the responsability of the caller to ensure that the "message" buffer is +//not accessible by anything until this function has return. +static int DRYSPONGE_dec( + const uint8_t *const key, + const unsigned int klen, + const uint8_t *const nonce, + const uint8_t *const ciphertext, + const size_t clen, + const uint8_t * const ad, + const size_t alen, + uint8_t *message +){ + if(clenr,last_block64,last_block64); + uint8_t mpad = DRYSPONGE_padding(last_block,remaining,last_block); + im+=remaining; + DRYSPONGE_DomainSeparator(DRYSPONGE_EXT_ARG,DRYSPONGE_DSINFO(mpad,DRYSPONGE_DM,1)); + memcpy(ctx->obuf,last_block,remaining); + DRYSPONGE_f(ctx,last_block); + } + uint64_t tag64[DRYSPONGE_TAGSIZE64]; + uint8_t*tag = (uint8_t*)tag64; + DRYSPONGE_squeez_only(ctx,tag,DRYSPONGE_TAGSIZE); + DRYSPONGE_DBG(print_bytes_sep("expected tag=",im,DRYSPONGE_TAGSIZE,"\n","")); + DRYSPONGE_DBG(print_bytes_sep("computed tag=",tag,DRYSPONGE_TAGSIZE,"\n","")); + if(memcmp(tag,im,DRYSPONGE_TAGSIZE)){ + memset(message,0,mlen);//erase all output + return ~DRYSPONGE_PASS; + } + return DRYSPONGE_PASS; +} +#endif diff --git a/drygascon/Implementations/crypto_aead/drygascon128/add_arm-cm0/encrypt.c b/drygascon/Implementations/crypto_aead/drygascon128/add_arm-cm0/encrypt.c new file mode 100644 index 0000000..c42ca12 --- /dev/null +++ b/drygascon/Implementations/crypto_aead/drygascon128/add_arm-cm0/encrypt.c @@ -0,0 +1,52 @@ +#include "crypto_aead.h" +#define DRYSPONGE_OPT_G drygascon128_g +#define DRYSPONGE_OPT_F drygascon128_f +#include "drysponge.h" + +/** +generating a ciphertext c[0],c[1],...,c[*clen-1] +from a plaintext m[0],m[1],...,m[mlen-1] +and associated data ad[0],ad[1],...,ad[adlen-1] +and nonce npub[0],npub[1],... +and secret key k[0],k[1],... +the implementation shall not use nsec +*/ +int crypto_aead_encrypt( + unsigned char *c,unsigned long long *clen, + const unsigned char *m,unsigned long long mlen, + const unsigned char *ad,unsigned long long adlen, + const unsigned char *nsec, + const unsigned char *npub, + const unsigned char *k +){ + (void) nsec; //avoid warning + (void) DRYSPONGE_hash; //avoid warning + size_t impl_clen; + DRYSPONGE_enc(k,DRYSPONGE_KEYSIZE,npub,m,mlen,ad,adlen,c,&impl_clen); + *clen = impl_clen; + return 0; +} + +/** +the code for the AEAD implementation goes here, +generating a plaintext m[0],m[1],...,m[*mlen-1] +and secret message number nsec[0],nsec[1],... +from a ciphertext c[0],c[1],...,c[clen-1] +and associated data ad[0],ad[1],...,ad[adlen-1] +and nonce number npub[0],npub[1],... +and secret key k[0],k[1],... +*/ +int crypto_aead_decrypt( + unsigned char *m,unsigned long long *mlen, + unsigned char *nsec, + const unsigned char *c,unsigned long long clen, + const unsigned char *ad,unsigned long long adlen, + const unsigned char *npub, + const unsigned char *k +){ + (void) nsec; //avoid warning + if(DRYSPONGE_PASS!=DRYSPONGE_dec(k,DRYSPONGE_KEYSIZE,npub,c,clen,ad,adlen,m)) + return -1; + *mlen = clen - DRYSPONGE_TAGSIZE; + return 0; +} diff --git a/drygascon/Implementations/crypto_aead/drygascon128/add_arm-cm0/nistlwc b/drygascon/Implementations/crypto_aead/drygascon128/add_arm-cm0/nistlwc new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/drygascon/Implementations/crypto_aead/drygascon128/add_arm-cm0/nistlwc