]> git.cryptolib.org Git - avr-crypto-lib.git/blobdiff - groestl/groestl_small.c
switch gcm-test from aes to aes-enconly
[avr-crypto-lib.git] / groestl / groestl_small.c
index e5a3d9422393761add7bd59f0f6ac2b5b658d449..0b6650553e4e65d1621d84059f6961b3b497690f 100644 (file)
@@ -22,7 +22,7 @@
  * \email   daniel.otte@rub.de
  * \date    2009-05-19
  * \license GPLv3 or later
- * 
+ *
  */
 
 #include "groestl_small.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<8; ++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,37 +65,79 @@ static uint8_t matrix[] PROGMEM = {
  2, 3, 4, 5, 3, 5, 7, 2
 };
 
+
+static const uint8_t p_shifts[] PROGMEM = {
+               0, 1, 2, 3, 4, 5, 6, 7
+};
+
+static const uint8_t q_shifts[] PROGMEM = {
+               1, 3, 5, 7, 0, 2, 4, 6
+};
+
+
+static
+void shift_columns(uint8_t *a, PGM_VOID_P shifts){
+       uint8_t s;
+       uint8_t tmp[8];
+       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<8;++j){
+                       tmp[j] = a[i+j*8];
+               }
+               for(j=0; j<8; ++j){
+                       a[i+((j-s+8)%8)*8] = tmp[j];
+               }
+       }
+}
+
+
+
 void groestl_small_rounds(uint8_t *m, uint8_t q){
        uint8_t r,i,j;
        uint8_t tmp[8];
+#if DEBUG
+       cli_putstr_P(PSTR("\r\n:: BEGIN "));
+       cli_putc(q?'Q':'P');
+#endif
+
        for(r=0; r<ROUNDS; ++r){
                if(q){
-                       m[7] ^= 0xff ^ r;
+                       for(i=0; i<8*8; ++i){
+                               m[i] ^= 0xff;
+                       }
+                       for(i=0; i<8; ++i){
+                               m[7+i*8] ^=  r ^ (i<<4);
+                       }
                }else{
-                       m[0] ^= r;
+                       for(i=0; i<8; ++i){
+                               m[i*8] ^= r ^ (i<<4);
+                       }
                }
-#if DEBUG              
-               if(r<2){
+#if DEBUG
+       //      if(r<2){
                        cli_putstr_P(PSTR("\r\npost add-const"));
                        dump_m(m);
-               }
+       //      }
 #endif
                for(i=0;i<8*8; ++i){
                        m[i] = pgm_read_byte(aes_sbox+m[i]);
                }
-               for(i=1; i<8; ++i){
-                       for(j=0; j<8; ++j)
-                               tmp[j] = m[i+8*j];
-                       for(j=0; j<8; ++j){
-                               m[i+((j-i+8)%8)*8] = tmp[j];
-                       }
+               if(!q){
+                       shift_columns(m, p_shifts);
+               }else{
+                       shift_columns(m, q_shifts);
                }
-#if DEBUG              
+#if DEBUG
                if(r<2){
                        cli_putstr_P(PSTR("\r\npost shift-bytes"));
                        dump_m(m);
                }
-#endif                 
+#endif
                for(i=0; i<8; ++i){
                        memcpy(tmp, m+8*i, 8);
                        for(j=0; j<8; ++j){
@@ -114,30 +156,30 @@ void groestl_small_rounds(uint8_t *m, uint8_t q){
                        cli_putstr_P(PSTR("\r\npost mix-bytes"));
                        dump_m(m);
                }
-#endif         
+#endif
        }
 }
 
-void groestl224_init(groestl224_ctx_tctx){
+void groestl224_init(groestl224_ctx_t *ctx){
        memset(ctx->h, 0, 8*8);
        ctx->h[8*8-1] = 224;
-       ctx->counter = 0;
+       ctx->counter = 1;
 }
 
-void groestl256_init(groestl256_ctx_tctx){
+void groestl256_init(groestl256_ctx_t *ctx){
        memset(ctx->h, 0, 8*8);
        ctx->h[8*8-2] = 1;
-       ctx->counter = 0;
+       ctx->counter = 1;
 }
 
-void groestl_small_nextBlock(groestl_small_ctx_t* ctx, const void* block){
+void groestl_small_nextBlock(groestl_small_ctx_t *ctx, const void *block){
        uint8_t tmp1[64], tmp2[64];
 /*     for(i=0; i<8; ++i){
                for(j=0; j<8; ++j){
                        tmp1[j*8+i] = ((uint8_t*)block)[i*8+j];
                }
        }
-*/ 
+*/
        memcpy(tmp1, block, 64);
        memcpy(tmp2, tmp1, 64);
        memxor(tmp1, ctx->h, 64);
@@ -148,7 +190,7 @@ void groestl_small_nextBlock(groestl_small_ctx_t* ctx, const void* block){
        ctx->counter++;
 }
 
-void groestl_small_lastBlock(groestl_small_ctx_t* ctx, const void* block, uint16_t length_b){
+void groestl_small_lastBlock(groestl_small_ctx_t *ctx, const void *block, uint16_t length_b){
        uint8_t buffer[64];
        while(length_b>=GROESTL_SMALL_BLOCKSIZE){
                groestl_small_nextBlock(ctx, block);
@@ -157,12 +199,12 @@ void groestl_small_lastBlock(groestl_small_ctx_t* ctx, const void* block, uint16
        }
        memset(buffer, 0, 64);
        memcpy(buffer, block, (length_b+7)/8);
-       buffer[length_b/8] |= 0x80>>(length_b%8);
+       buffer[length_b/8] |= 0x80>>(length_b&0x7);
        if(length_b>512-65){
                groestl_small_nextBlock(ctx, buffer);
                memset(buffer, 0, 64-4);
        }
-       ctx->counter++;
+//     ctx->counter++;
        buffer[64-1]  = (uint8_t)(ctx->counter);
        buffer[64-2]  = (uint8_t)((ctx->counter)>>8);
        buffer[64-3]  = (uint8_t)((ctx->counter)>>16);
@@ -170,7 +212,7 @@ void groestl_small_lastBlock(groestl_small_ctx_t* ctx, const void* block, uint16
        groestl_small_nextBlock(ctx, buffer);
 }
 
-void groestl_small_ctx2hash(void* dest, const groestl_small_ctx_t* ctx, uint16_t outlength_b){
+void groestl_small_ctx2hash(void *dest, const groestl_small_ctx_t *ctx, uint16_t outlength_b){
        uint8_t tmp[64];
        memcpy(tmp, ctx->h, 64);
        groestl_small_rounds(tmp, 0);
@@ -178,35 +220,35 @@ void groestl_small_ctx2hash(void* dest, const groestl_small_ctx_t* ctx, uint16_t
 #if DEBUG
        cli_putstr_P(PSTR("\r\npost finalisation"));
        dump_m(tmp);
-#endif         
+#endif
        memcpy(dest, tmp+64-outlength_b/8, outlength_b/8);
 }
 
-void groestl224_ctx2hash(void* dest, const groestl224_ctx_t* ctx){
+void groestl224_ctx2hash(void *dest, const groestl224_ctx_t *ctx){
        groestl_small_ctx2hash(dest, ctx, 224);
 }
 
-void groestl256_ctx2hash(void* dest, const groestl256_ctx_t* ctx){
+void groestl256_ctx2hash(void *dest, const groestl256_ctx_t *ctx){
        groestl_small_ctx2hash(dest, ctx, 256);
 }
 
-void groestl224_nextBlock(groestl224_ctx_t* ctx, const void* block){
+void groestl224_nextBlock(groestl224_ctx_t *ctx, const void *block){
        groestl_small_nextBlock(ctx, block);
 }
 
-void groestl256_nextBlock(groestl256_ctx_t* ctx, const void* block){
+void groestl256_nextBlock(groestl256_ctx_t *ctx, const void *block){
        groestl_small_nextBlock(ctx, block);
 }
 
-void groestl224_lastBlock(groestl224_ctx_t* ctx, const void* block, uint16_t length_b){
+void groestl224_lastBlock(groestl224_ctx_t *ctx, const void *block, uint16_t length_b){
        groestl_small_lastBlock(ctx, block, length_b);
 }
 
-void groestl256_lastBlock(groestl256_ctx_t* ctx, const void* block, uint16_t length_b){
+void groestl256_lastBlock(groestl256_ctx_t *ctx, const void *block, uint16_t length_b){
        groestl_small_lastBlock(ctx, block, length_b);
 }
 
-void groestl224(void* dest, const void* msg, uint32_t length_b){
+void groestl224(void *dest, const void *msg, uint32_t length_b){
        groestl_small_ctx_t ctx;
        groestl224_init(&ctx);
        while(length_b>=GROESTL_SMALL_BLOCKSIZE){
@@ -218,7 +260,7 @@ void groestl224(void* dest, const void* msg, uint32_t length_b){
        groestl_small_ctx2hash(dest, &ctx, 224);
 }
 
-void groestl256(void* dest, const void* msg, uint32_t length_b){
+void groestl256(void *dest, const void *msg, uint32_t length_b){
        groestl_small_ctx_t ctx;
        groestl256_init(&ctx);
        while(length_b>=GROESTL_SMALL_BLOCKSIZE){