]> git.cryptolib.org Git - avr-crypto-lib.git/commitdiff
sha3 api for twister
authorbg <bg@b1d182e4-1ff8-0310-901f-bddb46175740>
Wed, 7 Jan 2009 19:13:06 +0000 (19:13 +0000)
committerbg <bg@b1d182e4-1ff8-0310-901f-bddb46175740>
Wed, 7 Jan 2009 19:13:06 +0000 (19:13 +0000)
12 files changed:
aes_dec.c
aes_keyschedule.c
sha3-api.h [new file with mode: 0644]
test_src/main-skipjack-test.c
twister-big.c
twister-big.h
twister-sha3api.c [new file with mode: 0644]
twister-sha3api.h [new file with mode: 0644]
twister-small.c
twister-small.h
twister.c
twister.h

index 3b546411aba18aa7ae082a8aad7565ee9939213c..ebfdda9fb25d1ef46cf2ea2c0c23de99b47a8df8 100644 (file)
--- 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
index 429cef639fed0c295313e4aabbeae65da09cfbd4..72fd6798501f2f79803cdfdef649a3f7a49d70c6 100644 (file)
@@ -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 (file)
index 0000000..179ca78
--- /dev/null
@@ -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_ */
index a7f1165c697fa97bd0ccac0e9cea178b657ed47a..68baa3a697e9a9a734de2cbe919144ff0344b078 100644 (file)
@@ -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]);    
 }
 
 
index b0a6fd30e70144a7cecddfe770d0fb205e0575e2..cc0c1215ef72542442b02ec5443655ab851c096b 100644 (file)
@@ -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){
index dd9c1fcb038685148ad4ab990bbaf9bf8967864b..b10c0f023db598c077d4369f51d5dee60387479c 100644 (file)
@@ -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 (file)
index 0000000..4ad0233
--- /dev/null
@@ -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 <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);
+}
+
diff --git a/twister-sha3api.h b/twister-sha3api.h
new file mode 100644 (file)
index 0000000..173dd52
--- /dev/null
@@ -0,0 +1,20 @@
+/* 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);
+
index 44212859a0bf4d7a82081b3e8654f406c89c63be..3575bd60584a5c6a08c1e673a59af27a2ec66a2d 100644 (file)
@@ -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){
index ebc27b579cd64b86aa43eacd7d3c01beee611df4..ca5511950042b11feff207643479b96a374205e2 100644 (file)
@@ -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_ */
index b795a2d8eebf3c35a35ce77ccce906831d3e58dd..494a7a11f72cfd44bc7efad4d777ecf0bcf9cc50 100644 (file)
--- 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){
index 88850b62663f02516fd9e21e904e3487db919bcc..e31ba1909c40a6f44ec26f2d0091cadb1fa0e248 100644 (file)
--- 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_ */