]> git.cryptolib.org Git - avr-crypto-lib.git/blobdiff - groestl/groestl_large.c
fixing E-Mail-Address & Copyright
[avr-crypto-lib.git] / groestl / groestl_large.c
index 322e1c6e2678afc59e4f145571e2b665e72b7566..28071999d976e9788e603458093ad93306050e72 100644 (file)
@@ -1,7 +1,7 @@
 /* groestl_large.c */
 /*
     This file is part of the AVR-Crypto-Lib.
-    Copyright (C) 2009  Daniel Otte (daniel.otte@rub.de)
+    Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org)
 
     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
 /*
  * \file    groestl_large.c
  * \author  Daniel Otte
- * \email   daniel.otte@rub.de
+ * \email   bg@nerilex.org
  * \date    2009-06-11
  * \license GPLv3 or later
  * 
  */
 
 #include "groestl_large.h"
-#include "aes/aes_sbox.h"
-#include "gf256mul/gf256mul.h"
-#include "memxor/memxor.h"
+#include "aes_sbox.h"
+#include "gf256mul.h"
+#include "memxor.h"
 #include <stdint.h>
 #include <avr/pgmspace.h>
 #include <string.h>
@@ -40,7 +40,7 @@
 
 #if DEBUG
  #include "cli.h"
- void dump_m(const uint8_tm){
+ void dump_m(const uint8_t *m){
         uint8_t i,j;
         for(i=0; i<16; ++i){
                cli_putstr_P(PSTR("\r\n"));
@@ -54,7 +54,7 @@
  #define dump_m(m)
 #endif
 
-static uint8_t matrix[] PROGMEM = {
+static const uint8_t matrix[] PROGMEM = {
  2, 2, 3, 4, 5, 3, 5, 7,
  7, 2, 2, 3, 4, 5, 3, 5,
  5, 7, 2, 2, 3, 4, 5, 3,
@@ -65,14 +65,49 @@ static uint8_t matrix[] PROGMEM = {
  2, 3, 4, 5, 3, 5, 7, 2
 };
 
+static
+void shift_columns(uint8_t *a, PGM_VOID_P shifts){
+       uint8_t s;
+       uint8_t tmp[16];
+       uint8_t i,j;
+       for(i=0; i<8; ++i){
+               s = pgm_read_byte(shifts);
+               shifts = (uint8_t*)shifts + 1;
+               if(s==0){
+                       continue;
+               }
+               for(j=0;j<16;++j){
+                       tmp[j] = a[i+j*8];
+               }
+               for(j=0; j<16; ++j){
+                       a[i+((j-s+16)%16)*8] = tmp[j];
+               }
+       }
+}
+
+static const uint8_t p_shifts[] PROGMEM = {
+               0, 1, 2, 3, 4, 5, 6, 11
+};
+
+static const uint8_t q_shifts[] PROGMEM = {
+               1, 3, 5, 11, 0, 2, 4, 6
+};
+
 void groestl_large_rounds(uint8_t *m, uint8_t q){
        uint8_t r,i,j;
        uint8_t tmp[16];
        for(r=0; r<ROUNDS; ++r){
                if(q){
-                       m[7] ^= 0xff ^ r;
+                       for(i=0; i<(8*16); ++i){
+                               m[i] ^= 0xff;
+                       }
+                       for(i=0; i<16; ++i){
+                               m[7+i*8] ^= r ^ (i<<4);
+                       }
                }else{
-                       m[0] ^= r;
+                       for(i=0; i<16; ++i){
+                               m[i*8] ^= r ^ (i<<4);
+                       }
                }       
 #if DEBUG              
                if(r<2){
@@ -83,17 +118,10 @@ void groestl_large_rounds(uint8_t *m, uint8_t q){
                for(i=0;i<16*8; ++i){
                        m[i] = pgm_read_byte(aes_sbox+m[i]);
                }
-               for(i=1; i<7; ++i){
-                       for(j=0; j<16; ++j)
-                               tmp[j] = m[i+8*j];
-                       for(j=0; j<16; ++j){
-                               m[i+((j-i+16)%16)*8] = tmp[j];
-                       }
-               }
-               for(j=0; j<16; ++j)
-                       tmp[j] = m[7+8*j];
-               for(j=0; j<16; ++j){
-                       m[7+((j-11+16)%16)*8] = tmp[j];
+               if(!q){
+                       shift_columns(m, p_shifts);
+               }else{
+                       shift_columns(m, q_shifts);
                }
                
 #if DEBUG              
@@ -124,20 +152,20 @@ void groestl_large_rounds(uint8_t *m, uint8_t q){
        }
 }
 
-void groestl384_init(groestl384_ctx_tctx){
+void groestl384_init(groestl384_ctx_t *ctx){
        memset(ctx->h, 0, 16*8);
        ctx->h[8*16-1] = (uint8_t)384;
        ctx->h[8*16-2] = (uint8_t)(384>>8);
        ctx->counter = 0;
 }
 
-void groestl512_init(groestl512_ctx_tctx){
+void groestl512_init(groestl512_ctx_t *ctx){
        memset(ctx->h, 0, 16*8);
        ctx->h[8*16-2] = 2;
        ctx->counter = 0;
 }
 
-void groestl_large_nextBlock(groestl_large_ctx_t* ctx, const void* block){
+void groestl_large_nextBlock(groestl_large_ctx_t *ctx, const void *block){
        uint8_t tmp1[128], tmp2[128];
 /*
        for(i=0; i<8; ++i){
@@ -156,7 +184,7 @@ void groestl_large_nextBlock(groestl_large_ctx_t* ctx, const void* block){
        ctx->counter++;
 }
 
-void groestl_large_lastBlock(groestl_large_ctx_t* ctx, const void* block, uint16_t length_b){
+void groestl_large_lastBlock(groestl_large_ctx_t *ctx, const void *block, uint16_t length_b){
        uint8_t buffer[128];
        while(length_b>=GROESTL_LARGE_BLOCKSIZE){
                groestl_large_nextBlock(ctx, block);
@@ -178,7 +206,7 @@ void groestl_large_lastBlock(groestl_large_ctx_t* ctx, const void* block, uint16
        groestl_large_nextBlock(ctx, buffer);
 }
 
-void groestl_large_ctx2hash(void* dest, const groestl_large_ctx_t* ctx, uint16_t outlength_b){
+void groestl_large_ctx2hash(void *dest, const groestl_large_ctx_t *ctx, uint16_t outlength_b){
        uint8_t tmp[128];
        memcpy(tmp, ctx->h, 128);
        groestl_large_rounds(tmp, 0);
@@ -190,31 +218,31 @@ void groestl_large_ctx2hash(void* dest, const groestl_large_ctx_t* ctx, uint16_t
        memcpy(dest, tmp+128-outlength_b/8, outlength_b/8);
 }
 
-void groestl384_ctx2hash(void* dest, const groestl384_ctx_t* ctx){
+void groestl384_ctx2hash(void *dest, const groestl384_ctx_t *ctx){
        groestl_large_ctx2hash(dest, ctx, 384);
 }
 
-void groestl512_ctx2hash(void* dest, const groestl512_ctx_t* ctx){
+void groestl512_ctx2hash(void *dest, const groestl512_ctx_t *ctx){
        groestl_large_ctx2hash(dest, ctx, 512);
 }
 
-void groestl384_nextBlock(groestl384_ctx_t* ctx, const void* block){
+void groestl384_nextBlock(groestl384_ctx_t *ctx, const void *block){
        groestl_large_nextBlock(ctx, block);
 }
 
-void groestl512_nextBlock(groestl512_ctx_t* ctx, const void* block){
+void groestl512_nextBlock(groestl512_ctx_t *ctx, const void *block){
        groestl_large_nextBlock(ctx, block);
 }
 
-void groestl384_lastBlock(groestl384_ctx_t* ctx, const void* block, uint16_t length_b){
+void groestl384_lastBlock(groestl384_ctx_t *ctx, const void *block, uint16_t length_b){
        groestl_large_lastBlock(ctx, block, length_b);
 }
 
-void groestl512_lastBlock(groestl512_ctx_t* ctx, const void* block, uint16_t length_b){
+void groestl512_lastBlock(groestl512_ctx_t *ctx, const void *block, uint16_t length_b){
        groestl_large_lastBlock(ctx, block, length_b);
 }
 
-void groestl384(void* dest, const void* msg, uint32_t length_b){
+void groestl384(void *dest, const void *msg, uint32_t length_b){
        groestl_large_ctx_t ctx;
        groestl384_init(&ctx);
        while(length_b>=GROESTL_LARGE_BLOCKSIZE){
@@ -226,7 +254,7 @@ void groestl384(void* dest, const void* msg, uint32_t length_b){
        groestl_large_ctx2hash(dest, &ctx, 384);
 }
 
-void groestl512(void* dest, const void* msg, uint32_t length_b){
+void groestl512(void *dest, const void *msg, uint32_t length_b){
        groestl_large_ctx_t ctx;
        groestl512_init(&ctx);
        while(length_b>=GROESTL_LARGE_BLOCKSIZE){