From: bg Date: Wed, 7 Jan 2009 19:13:06 +0000 (+0000) Subject: sha3 api for twister X-Git-Url: https://git.cryptolib.org/?a=commitdiff_plain;h=3caccd2034e682222162e24c256d20bf65d7c649;p=avr-crypto-lib.git sha3 api for twister --- diff --git a/aes_dec.c b/aes_dec.c index 3b54641..ebfdda9 100644 --- a/aes_dec.c +++ b/aes_dec.c @@ -1,7 +1,7 @@ /* aes.c */ /* This file is part of the Crypto-avr-lib/microcrypt-lib. - Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de) + Copyright (C) 2008, 2009 Daniel Otte (daniel.otte@rub.de) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/aes_keyschedule.c b/aes_keyschedule.c index 429cef6..72fd679 100644 --- a/aes_keyschedule.c +++ b/aes_keyschedule.c @@ -43,8 +43,6 @@ void aes_rotword(void* a){ ((uint8_t*)a)[3] = t; } -#include "uart.h" - void aes_init(const void* key, uint16_t keysize_b, aes_genctx_t* ctx){ uint8_t hi,i,nk, next_nk; uint8_t rc=1; diff --git a/sha3-api.h b/sha3-api.h new file mode 100644 index 0000000..179ca78 --- /dev/null +++ b/sha3-api.h @@ -0,0 +1,24 @@ +/* sha3-api.h */ + +/** + * \file sha3-api.h + * \author Daniel Otte + * \email daniel.otte@rub.de + * \date 2009-01-06 + * \license GPLv3 or later + * + */ +/* + This file implements algortihm independent data types of the NIST API for + SHA3 candidates as described in: + http://csrc.nist.gov/groups/ST/hash/documents/SHA3-C-API.pdf + . +*/ + +#ifndef SHA3_API_H_ +#define SHA3_API_H_ +typedef unsigned char BitSequence; +typedef unsigned long long DataLength; +typedef enum { SUCCESS = 0, FAIL = 1, BAD_HASHBITLEN = 2 } HashReturn; + +#endif /* SHA3_API_H_ */ diff --git a/test_src/main-skipjack-test.c b/test_src/main-skipjack-test.c index a7f1165..68baa3a 100644 --- a/test_src/main-skipjack-test.c +++ b/test_src/main-skipjack-test.c @@ -112,11 +112,23 @@ void testdecrypt(uint8_t* block, uint8_t* key){ } void testrun_skipjack(void){ - uint8_t key[]={ 0x00, 0x99, 0x88, 0x77, 0x66, - 0x55, 0x44, 0x33, 0x22, 0x11}; - uint8_t data[]={ 0x33, 0x22, 0x11, 0x00, 0xdd, 0xcc, 0xbb, 0xaa}; - testencrypt(data,key); - testdecrypt(data,key); + uint8_t key[2][10]={ + { 0x00, 0x99, 0x88, 0x77, 0x66, + 0x55, 0x44, 0x33, 0x22, 0x11 }, + { 0x11, 0x22, 0x33, 0x44, 0x55, + 0x66, 0x77, 0x88, 0x99, 0x00 } + }; + + uint8_t data[2][8]={ + { 0x33, 0x22, 0x11, 0x00, + 0xdd, 0xcc, 0xbb, 0xaa }, + { 0xaa, 0xbb, 0xcc, 0xdd, + 0x00, 0x11, 0x22, 0x33 } + }; + testencrypt(data[0],key[0]); + testdecrypt(data[0],key[0]); + testencrypt(data[1],key[1]); + testdecrypt(data[1],key[1]); } diff --git a/twister-big.c b/twister-big.c index b0a6fd3..cc0c121 100644 --- a/twister-big.c +++ b/twister-big.c @@ -57,7 +57,7 @@ void twister_big_init(twister_big_ctx_t* ctx, uint16_t hashsize_b){ /*********************************************************************/ -void twister_big_nextBlock(twister_big_ctx_t* ctx, void* msg){ +void twister_big_nextBlock(twister_big_ctx_t* ctx, const void* msg){ uint8_t tmp[8][8]; /* 1st maxi round */ @@ -115,7 +115,7 @@ void twister_inject_chksum(twister_big_ctx_t* ctx, uint8_t col){ /*********************************************************************/ -void twister_big_lastBlock(twister_big_ctx_t* ctx, void* msg, uint16_t length_b){ +void twister_big_lastBlock(twister_big_ctx_t* ctx, const void* msg, uint16_t length_b){ uint8_t tmp[64]; while(length_b>512){ twister_big_nextBlock(ctx, msg); @@ -164,13 +164,13 @@ void twister384_init(twister384_ctx_t* ctx){ /*********************************************************************/ -void twister384_nextBlock(twister384_ctx_t* ctx, void* msg){ +void twister384_nextBlock(twister384_ctx_t* ctx, const void* msg){ twister_big_nextBlock(ctx, msg); } /*********************************************************************/ -void twister384_lastBlock(twister384_ctx_t* ctx, void* msg, uint16_t length_b){ +void twister384_lastBlock(twister384_ctx_t* ctx, const void* msg, uint16_t length_b){ twister_big_lastBlock(ctx, msg, length_b); } @@ -182,7 +182,7 @@ void twister384_ctx2hash(void* dest, twister384_ctx_t* ctx){ /*********************************************************************/ -void twister384(void* dest, void* msg, uint32_t msg_length_b){ +void twister384(void* dest, const void* msg, uint32_t msg_length_b){ twister_big_ctx_t ctx; twister_big_init(&ctx, 384); while(msg_length_b >=512){ @@ -204,13 +204,13 @@ void twister512_init(twister512_ctx_t* ctx){ /*********************************************************************/ -void twister512_nextBlock(twister512_ctx_t* ctx, void* msg){ +void twister512_nextBlock(twister512_ctx_t* ctx, const void* msg){ twister_big_nextBlock(ctx, msg); } /*********************************************************************/ -void twister512_lastBlock(twister512_ctx_t* ctx, void* msg, uint16_t length_b){ +void twister512_lastBlock(twister512_ctx_t* ctx, const void* msg, uint16_t length_b){ twister_big_lastBlock(ctx, msg, length_b); } @@ -222,7 +222,7 @@ void twister512_ctx2hash(void* dest, twister512_ctx_t* ctx){ /*********************************************************************/ -void twister512(void* dest, void* msg, uint32_t msg_length_b){ +void twister512(void* dest, const void* msg, uint32_t msg_length_b){ twister_big_ctx_t ctx; twister_big_init(&ctx, 512); while(msg_length_b >=512){ diff --git a/twister-big.h b/twister-big.h index dd9c1fc..b10c0f0 100644 --- a/twister-big.h +++ b/twister-big.h @@ -19,26 +19,26 @@ typedef twister_big_ctx_t twister512_ctx_t; /*********************************************************************/ -void twister_big_nextBlock(twister_big_ctx_t* ctx, void* msg); +void twister_big_nextBlock(twister_big_ctx_t* ctx, const void* msg); void twister_big_init(twister_big_ctx_t* ctx, uint16_t hashsize_b); -void twister_big_lastBlock(twister_big_ctx_t* ctx, void* msg, uint16_t length_b); +void twister_big_lastBlock(twister_big_ctx_t* ctx, const void* msg, uint16_t length_b); void twister_big_ctx2hash(void* dest, twister_big_ctx_t* ctx, uint16_t hashsize_b); /*********************************************************************/ void twister384_init(twister384_ctx_t* ctx); -void twister384_nextBlock(twister384_ctx_t* ctx, void* msg); -void twister384_lastBlock(twister384_ctx_t* ctx, void* msg, uint16_t length_b); +void twister384_nextBlock(twister384_ctx_t* ctx, const void* msg); +void twister384_lastBlock(twister384_ctx_t* ctx, const void* msg, uint16_t length_b); void twister384_ctx2hash(void* dest, twister384_ctx_t* ctx); -void twister384(void* dest, void* msg, uint32_t msg_length_b); +void twister384(void* dest, const void* msg, uint32_t msg_length_b); /*********************************************************************/ void twister512_init(twister512_ctx_t* ctx); -void twister512_nextBlock(twister512_ctx_t* ctx, void* msg); -void twister512_lastBlock(twister512_ctx_t* ctx, void* msg, uint16_t length_b); +void twister512_nextBlock(twister512_ctx_t* ctx, const void* msg); +void twister512_lastBlock(twister512_ctx_t* ctx, const void* msg, uint16_t length_b); void twister512_ctx2hash(void* dest, twister512_ctx_t* ctx); -void twister512(void* dest, void* msg, uint32_t msg_length_b); +void twister512(void* dest, const void* msg, uint32_t msg_length_b); #endif /* TWISTER_BIG_H_ */ diff --git a/twister-sha3api.c b/twister-sha3api.c new file mode 100644 index 0000000..4ad0233 --- /dev/null +++ b/twister-sha3api.c @@ -0,0 +1,113 @@ +/* twister-sha3api.c */ + +#include "sha3-api.h" +#include "twister-sha3api.h" +#include "twister.h" +#include "twister-small.h" +#include "twister-big.h" +#include +#include + +#define BLOCKSIZE 512 + +HashReturn Init(hashState *state, int hashbitlen){ + if(!state) + return FAIL; + if(hashbitlen<32 || hashbitlen>512 || hashbitlen%32) + return BAD_HASHBITLEN; + + if (hashbitlen>=32 && hashbitlen<=256){ + state->hashbitlen = hashbitlen; + state->big = 0; + state->ctx = malloc(sizeof(twister_state_t)); + twister_small_init(state->ctx, hashbitlen); + state->buffer_fill_state=0; + memset(state->buffer, 0, 64); + return SUCCESS; + } + if (hashbitlen>256 && hashbitlen<=512){ + state->hashbitlen = hashbitlen; + state->big = 1; + state->ctx = malloc(sizeof(twister_big_ctx_t)); + twister_big_init(state->ctx, hashbitlen); + state->buffer_fill_state=0; + memset(state->buffer, 0, 64); + return SUCCESS; + } + return BAD_HASHBITLEN; +} + +void insertBitToBuffer(hashState *state, uint8_t bit){ + state->buffer[state->buffer_fill_state/8] |= + bit<<(7-(state->buffer_fill_state%8)); + state->buffer_fill_state++; +} + + +HashReturn Update(hashState *state, const BitSequence *data, + DataLength databitlen){ + if(state->buffer_fill_state+databitlen>(7-i%8))); + } + return SUCCESS; + } + if(state->buffer_fill_state==0){ + while(databitlen>=512){ + if(state->big){ + twister_big_nextBlock(state->ctx, data); + }else{ + twister_small_nextBlock(state->ctx, data); + } + data += 64; + databitlen -= 512; + } + if(databitlen!=0) + Update(state, data, databitlen); + return SUCCESS; + }else{ + DataLength i; + for(i=0; ibuffer_fill_state==512){ + if(state->big){ + twister_big_nextBlock(state->ctx, state->buffer); + }else{ + twister_small_nextBlock(state->ctx, state->buffer); + } + memset(state->buffer, 0, 64); + state->buffer_fill_state = 0; + } + insertBitToBuffer(state, 1&(data[i/8]>>(7-i%8))); + } + return SUCCESS; + } + return FAIL; +} + +HashReturn Final(hashState *state, BitSequence *hashval){ + if(state->big){ + twister_big_lastBlock(state->ctx, state->buffer, state->buffer_fill_state); + twister_big_ctx2hash(hashval, state->ctx, state->hashbitlen); + return SUCCESS; + }else{ + twister_small_lastBlock(state->ctx, state->buffer, state->buffer_fill_state); + twister_small_ctx2hash(hashval, state->ctx, state->hashbitlen); + return SUCCESS; + } +} + +HashReturn Hash(int hashbitlen, const BitSequence *data, + DataLength databitlen, BitSequence *hashval){ + HashReturn ret; + hashState state; + ret = Init(&state, hashbitlen); + if(ret!=SUCCESS) + return ret; + ret = Update(&state, data, databitlen); + if(ret!=SUCCESS) + return ret; + return Final(&state, hashval); +} + diff --git a/twister-sha3api.h b/twister-sha3api.h new file mode 100644 index 0000000..173dd52 --- /dev/null +++ b/twister-sha3api.h @@ -0,0 +1,20 @@ +/* twister-sha3api.h */ + +#include "sha3-api.h" +#include + +typedef struct{ + uint16_t hashbitlen; + uint8_t big; + void* ctx; /* points either to twister_state_t or twister_big_ctx_t */ + uint8_t buffer[64]; + uint16_t buffer_fill_state; +}hashState; + +HashReturn Init(hashState *state, int hashbitlen); +HashReturn Update(hashState *state, const BitSequence *data, + DataLength databitlen); +HashReturn Final(hashState *state, BitSequence *hashval); +HashReturn Hash(int hashbitlen, const BitSequence *data, + DataLength databitlen, BitSequence *hashval); + diff --git a/twister-small.c b/twister-small.c index 4421285..3575bd6 100644 --- a/twister-small.c +++ b/twister-small.c @@ -17,7 +17,7 @@ void twister_small_init(twister_state_t* ctx, uint16_t hashsize_b){ /*********************************************************************/ -void twister_small_nextBlock(twister_state_t* ctx, void* msg){ +void twister_small_nextBlock(twister_state_t* ctx, const void* msg){ uint8_t tmp[8][8]; /* round 1 */ memcpy(tmp, ctx->s, 64); @@ -51,7 +51,7 @@ void twister_small_nextBlock(twister_state_t* ctx, void* msg){ /*********************************************************************/ -void twister_small_lastBlock(twister_state_t* ctx, void* msg, uint16_t length_b){ +void twister_small_lastBlock(twister_state_t* ctx, const void* msg, uint16_t length_b){ uint8_t tmp[64]; while(length_b>512){ twister_small_nextBlock(ctx, msg); @@ -85,13 +85,13 @@ void twister256_init(twister256_ctx_t* ctx){ /*********************************************************************/ -void twister256_nextBlock(twister256_ctx_t* ctx, void* msg){ +void twister256_nextBlock(twister256_ctx_t* ctx, const void* msg){ twister_small_nextBlock(ctx, msg); } /*********************************************************************/ -void twister256_lastBlock(twister256_ctx_t* ctx, void* msg, uint16_t length_b){ +void twister256_lastBlock(twister256_ctx_t* ctx, const void* msg, uint16_t length_b){ twister_small_lastBlock(ctx, msg, length_b); } @@ -103,7 +103,7 @@ void twister256_ctx2hash(void* dest, twister256_ctx_t* ctx){ /*********************************************************************/ -void twister256(void* dest, void* msg, uint32_t msg_length_b){ +void twister256(void* dest, const void* msg, uint32_t msg_length_b){ twister_state_t ctx; twister_small_init(&ctx, 256); while(msg_length_b >=512){ @@ -130,13 +130,13 @@ void twister224_init(twister224_ctx_t* ctx){ /*********************************************************************/ -void twister224_nextBlock(twister224_ctx_t* ctx, void* msg){ +void twister224_nextBlock(twister224_ctx_t* ctx, const void* msg){ twister_small_nextBlock(ctx, msg); } /*********************************************************************/ -void twister224_lastBlock(twister224_ctx_t* ctx, void* msg, uint16_t length_b){ +void twister224_lastBlock(twister224_ctx_t* ctx, const void* msg, uint16_t length_b){ twister_small_lastBlock(ctx, msg, length_b); } @@ -148,7 +148,7 @@ void twister224_ctx2hash(void* dest, twister224_ctx_t* ctx){ /*********************************************************************/ -void twister224(void* dest, void* msg, uint32_t msg_length_b){ +void twister224(void* dest, const void* msg, uint32_t msg_length_b){ twister_state_t ctx; twister_small_init(&ctx, 224); while(msg_length_b >=512){ diff --git a/twister-small.h b/twister-small.h index ebc27b5..ca55119 100644 --- a/twister-small.h +++ b/twister-small.h @@ -13,21 +13,21 @@ typedef twister_state_t twister256_ctx_t; typedef twister_state_t twister224_ctx_t; void twister_small_init(twister_state_t* ctx, uint16_t hashsize_b); -void twister_small_lastBlock(twister_state_t* ctx, void* msg, uint16_t length_b); +void twister_small_lastBlock(twister_state_t* ctx, const void* msg, uint16_t length_b); void twister_small_ctx2hash(void* dest, twister_state_t* ctx, uint16_t hashsize_b); -void twister_small_nextBlock(twister_state_t* ctx, void* msg); +void twister_small_nextBlock(twister_state_t* ctx, const void* msg); void twister256_init(twister256_ctx_t* ctx); -void twister256_nextBlock(twister256_ctx_t* ctx, void* msg); -void twister256_lastBlock(twister256_ctx_t* ctx, void* msg, uint16_t length_b); +void twister256_nextBlock(twister256_ctx_t* ctx, const void* msg); +void twister256_lastBlock(twister256_ctx_t* ctx, const void* msg, uint16_t length_b); void twister256_ctx2hash(void* dest, twister_state_t* ctx); -void twister256(void* dest, void* msg, uint32_t msg_length_b); +void twister256(void* dest, const void* msg, uint32_t msg_length_b); void twister224_init(twister224_ctx_t* ctx); -void twister224_nextBlock(twister224_ctx_t* ctx, void* msg); -void twister224_lastBlock(twister224_ctx_t* ctx, void* msg, uint16_t length_b); +void twister224_nextBlock(twister224_ctx_t* ctx, const void* msg); +void twister224_lastBlock(twister224_ctx_t* ctx, const void* msg, uint16_t length_b); void twister224_ctx2hash(void* dest, twister224_ctx_t* ctx); -void twister224(void* dest, void* msg, uint32_t msg_length_b); +void twister224(void* dest, const void* msg, uint32_t msg_length_b); #endif /* TWISTER_SMALL_H_ */ diff --git a/twister.c b/twister.c index b795a2d..494a7a1 100644 --- a/twister.c +++ b/twister.c @@ -38,7 +38,7 @@ #endif void twister_blank_round(twister_state_t* ctx){ - uint8_t i,j,k=0; + uint8_t i,j,k; uint8_t tmp[8][8]; /* add twist counter */ for(i=0; i<8; ++i){ @@ -70,7 +70,7 @@ void twister_blank_round(twister_state_t* ctx){ } } -void twister_mini_round(twister_state_t* ctx, void* msg){ +void twister_mini_round(twister_state_t* ctx, const void* msg){ /* inject message */ uint8_t i; for(i=0; i<8; ++i){ diff --git a/twister.h b/twister.h index 88850b6..e31ba19 100644 --- a/twister.h +++ b/twister.h @@ -38,7 +38,7 @@ typedef struct { typedef uint8_t twister_checksum_t[8][8]; void twister_blank_round(twister_state_t* ctx); -void twister_mini_round(twister_state_t* ctx, void* msg); +void twister_mini_round(twister_state_t* ctx, const void* msg); void twister_ctx2hash(void* dest, twister_state_t* ctx, uint16_t hashsize_b); #endif /* TWISTER_H_ */