]> git.cryptolib.org Git - avr-crypto-lib.git/blobdiff - twister-big.c
new hash function abstraction layer + shavs + dump util + ...
[avr-crypto-lib.git] / twister-big.c
index 9c8cef75e21883d282df11f78d729bbacb9cd526..795190951d3263d4a78718ff887c0fc98bd25d7c 100644 (file)
@@ -1,4 +1,21 @@
 /* twister-big.c */
+/*
+    This file is part of the AVR-Crypto-Lib.
+    Copyright (C) 2008  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
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
 
 #include <stdint.h>
 #include <string.h>
@@ -6,82 +23,7 @@
 #include "twister.h"
 #include "twister-big.h"
 
-#undef DEBUG
-#define DEBUG
-
-#ifdef DEBUG
-#include <avr/pgmspace.h>
-#include "uart.h"
-void print_checksum(twister_big_ctx_t* ctx, PGM_P s){
-       uint8_t i;
-       uart_putstr_P(PSTR("\r\n"));
-       uart_putstr_P(s);
-       uart_putstr_P(PSTR("\r\n checksum:\r\n"));
-       for(i=0; i<8; ++i){
-               uart_putstr_P(PSTR("  [ "));
-               uart_hexdump(&(ctx->checksum[i][0]), 8);
-               uart_putstr_P(PSTR("]\r\n"));
-       }
-}
-
-void print_matrix(void* m, PGM_P s){
-       uint8_t i;
-       uart_putstr_P(PSTR("\r\n"));
-       uart_putstr_P(s);
-       uart_putstr_P(PSTR("\r\n matrix:\r\n"));
-       for(i=0; i<8; ++i){
-               uart_putstr_P(PSTR("  [ "));
-               uart_hexdump(((uint8_t*)m)+i*8, 8);
-               uart_putstr_P(PSTR("]\r\n"));
-       }
-}
-
-#define DEBUG_CHKSUM(a,s) print_checksum((a),PSTR(s)) 
-#else
-#define DEBUG_CHKSUM(a,s) 
-#endif
-
-
-#ifdef DEBUG
-# define DEBUG_PRINT(ctx, msg) debug_print((ctx), PSTR(msg)) 
-#else
-# define DEBUG_PRINT(ctx, msg) 
-#endif 
-
-#ifdef DEBUG
-
-void print_twister_state(twister_state_t* ctx){
-       uint8_t i;
-       uart_putstr_P(PSTR("\r\nState:\r\n matrix:\r\n"));
-       for(i=0; i<8; ++i){
-               uart_putstr_P(PSTR("\t[ "));
-               uart_hexdump(&(ctx->s[i][0]), 8);
-               uart_putstr_P(PSTR("]\r\n"));
-       }
-       uart_putstr_P(PSTR("counter: "));
-       uart_hexdump(&(ctx->counter), 8);
-
-       uart_putstr_P(PSTR("\r\nlength_counter_b: "));
-       uart_hexdump(&(ctx->length_counter_b), 8);
-       uart_putstr_P(PSTR("\r\n"));
-} 
-
-void debug_print(twister_state_t* ctx, PGM_P msg){
-       uart_putstr_P(PSTR("\r\n"));
-       uart_putstr_P(msg);
-       print_twister_state(ctx);
-}
-
-#endif
-
-void transp_matrix(void* dest, void* src){
-       uint8_t i,j;
-       for(i=0; i<8; i++){
-               for(j=0; j<8; ++j){
-                       ((uint8_t*)dest)[i*8+j] = ((uint8_t*)src)[j*8+i];
-               }
-       }
-}
+/*********************************************************************/
 
 static
 void checksum_update(twister_big_ctx_t* ctx, uint8_t col){
@@ -100,9 +42,10 @@ void checksum_update(twister_big_ctx_t* ctx, uint8_t col){
                carry = sum>>8;
 
        }
-//     DEBUG_CHKSUM(ctx, "post run");
 }
 
+/*********************************************************************/
+
 void twister_big_init(twister_big_ctx_t* ctx, uint16_t hashsize_b){
        memset(ctx->state.s,  0, 64);
        memset(ctx->checksum, 0, 64);
@@ -112,7 +55,9 @@ void twister_big_init(twister_big_ctx_t* ctx, uint16_t hashsize_b){
        ctx->state.length_counter_b = 0;
 }
 
-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 */
@@ -161,12 +106,16 @@ void twister_big_nextBlock(twister_big_ctx_t* ctx, void* msg){
        ctx->state.length_counter_b += 512;
 }
 
+/*********************************************************************/
+
 void twister_inject_chksum(twister_big_ctx_t* ctx, uint8_t col){
        *((uint64_t*)(&ctx->state.s[7][0])) ^= *((uint64_t*)(&ctx->checksum[col][0]));
        twister_blank_round(&ctx->state);
 }
 
-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);
@@ -180,8 +129,6 @@ void twister_big_lastBlock(twister_big_ctx_t* ctx, void* msg, uint16_t length_b)
        ctx->state.length_counter_b -= 512 - length_b;
        twister_mini_round(&(ctx->state), &(ctx->state.length_counter_b));
 
-//     DEBUG_PRINT(&(ctx->state), "pre check-round");
-
        memcpy(tmp, ctx->state.s, 64);
        twister_inject_chksum(ctx, 0);
        twister_inject_chksum(ctx, 1);
@@ -200,34 +147,42 @@ void twister_big_lastBlock(twister_big_ctx_t* ctx, void* msg, uint16_t length_b)
        twister_blank_round(&(ctx->state));
        memxor(ctx->state.s, tmp, 64);
 
-
-//     DEBUG_PRINT(&(ctx->state), "post check-round");
 }
 
+/*********************************************************************/
+
 void twister_big_ctx2hash(void* dest, twister_big_ctx_t* ctx, uint16_t hashsize_b){
        twister_ctx2hash(dest, &(ctx->state), hashsize_b);
 }
 
-/******************************************************************************/
-/******************************************************************************/
+/*********************************************************************/
+/*********************************************************************/
 
 void twister384_init(twister384_ctx_t* ctx){
        twister_big_init(ctx, 384);
 }
 
-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_ctx2hash(void* dest, twister384_ctx_t* ctx){
        twister_big_ctx2hash(dest, ctx, 384);
 }
 
-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){
@@ -239,25 +194,35 @@ void twister384(void* dest, void* msg, uint32_t msg_length_b){
        twister_big_ctx2hash(dest, &ctx, 384);
 }
 
-/******************************************************************************/
+/*********************************************************************/
+/*********************************************************************/
+
 
 void twister512_init(twister512_ctx_t* ctx){
        twister_big_init(ctx, 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_ctx2hash(void* dest, twister512_ctx_t* ctx){
        twister_big_ctx2hash(dest, ctx, 512);
 }
 
-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){