/* 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
((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;
--- /dev/null
+/* 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_ */
}
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]);
}
/*********************************************************************/
-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 */
/*********************************************************************/
-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);
/*********************************************************************/
-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);
}
/*********************************************************************/
-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){
/*********************************************************************/
-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);
}
/*********************************************************************/
-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){
/*********************************************************************/
-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_ */
--- /dev/null
+/* twister-sha3api.c */
+
+#include "sha3-api.h"
+#include "twister-sha3api.h"
+#include "twister.h"
+#include "twister-small.h"
+#include "twister-big.h"
+#include <stdlib.h>
+#include <string.h>
+
+#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<BLOCKSIZE){
+ /* move bits from data to buffer */
+ uint16_t i;
+ for(i=0; i<databitlen; ++i){
+ insertBitToBuffer(state, 1&((data[i/8])>>(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; i<databitlen; ++i){
+ if(state->buffer_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);
+}
+
--- /dev/null
+/* twister-sha3api.h */
+
+#include "sha3-api.h"
+#include <stdint.h>
+
+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);
+
/*********************************************************************/
-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);
/*********************************************************************/
-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);
/*********************************************************************/
-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);
}
/*********************************************************************/
-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){
/*********************************************************************/
-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);
}
/*********************************************************************/
-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){
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_ */
#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){
}
}
-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){
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_ */