]> git.cryptolib.org Git - avr-crypto-lib.git/commitdiff
[keccak-asm] improving last block handling (reducing stack space)
authorbg <daniel.otte@rub.de>
Thu, 27 Dec 2012 00:21:32 +0000 (01:21 +0100)
committerbg <daniel.otte@rub.de>
Thu, 27 Dec 2012 00:21:32 +0000 (01:21 +0100)
keccak/keccak-asm.S
keccak/keccak-stub.c

index 9d613f95042bdec0006cb4f1fb2d6ae8183fe195..4a13e09721fc4936bcbfdc5ca4121fb2d1bef4a6 100644 (file)
@@ -409,7 +409,7 @@ keccak_f1600:
 
 ;      ret
 /*
-  rho & pi
+   -- rho & pi --
        for(i = 0; i < 5; ++i){
                for(j = 0; j < 5; ++j){
                        b[(2 * i + 3 * j) % 5][j] =
index ff198e7c3fe692b0af74880e4b8779f63881abc2..886e2d199716cf391cae2aa2aab209270493c481 100644 (file)
@@ -88,53 +88,50 @@ void keccak_nextBlock(keccak_ctx_t* ctx, const void* block){
 }
 
 void keccak_lastBlock(keccak_ctx_t* ctx, const void* block, uint16_t length_b){
-       while(length_b>=ctx->r){
+       while(length_b >= ctx->r){
                keccak_nextBlock(ctx, block);
                block = (uint8_t*)block + ctx->bs;
                length_b -=  ctx->r;
        }
-       uint8_t tmp[ctx->bs];
+//     uint8_t tmp[ctx->bs];
        uint8_t pad[3];
-       memset(tmp, 0x00, ctx->bs);
-       memcpy(tmp, block, (length_b+7)/8);
+//     memset(tmp, 0x00, ctx->bs);
+       memxor(ctx->a, block, (length_b)/8);
        /* appand 1 */
        if(length_b & 7){
                /* we have some single bits */
                uint8_t t;
-               t = tmp[length_b / 8] >> (8 - (length_b & 7));
+               t = ((uint8_t*)block)[length_b / 8] >> (8 - (length_b & 7));
                t |= 0x01 << (length_b & 7);
-               tmp[length_b / 8] = t;
+               ((uint8_t*)ctx->a)[length_b / 8] ^= t;
        }else{
-               tmp[length_b / 8] = 0x01;
+           ((uint8_t*)ctx->a)[length_b / 8] ^= 0x01;
        }
        pad[0] = ctx->d;
        pad[1] = ctx->bs;
        pad[2] = 0x01;
        if(length_b / 8 + 1 + 3 <= ctx->bs){
-               memcpy(tmp + length_b / 8 + 1, pad, 3);
+               memxor((uint8_t*)ctx->a + length_b / 8 + 1, pad, 3);
        }else{
                if(length_b / 8 + 1 + 2 <= ctx->bs){
-                       memcpy(tmp+length_b/8+1, pad, 2);
-                       keccak_nextBlock(ctx, tmp);
-                       memset(tmp, 0x00, ctx->bs);
-                       tmp[0] = 0x01;
+                       memxor((uint8_t*)ctx->a + length_b / 8 + 1, pad, 2);
+                       keccak_f1600(ctx->a);
+                       ((uint8_t*)ctx->a)[0] ^= 0x01;
                }else{
                        if(length_b/8+1+1 <= ctx->bs){
-                               memcpy(tmp + length_b / 8 + 1, pad, 1);
-                               keccak_nextBlock(ctx, tmp);
-                               memset(tmp, 0x00, ctx->bs);
-                               tmp[0] = ctx->bs;
-                               tmp[1] = 0x01;
+                               memxor((uint8_t*)ctx->a + length_b / 8 + 1, pad, 1);
+                               keccak_f1600(ctx->a);
+                               ((uint8_t*)ctx->a)[0] ^= ctx->bs;
+                               ((uint8_t*)ctx->a)[1] ^= 0x01;
                        }else{
-                               keccak_nextBlock(ctx, tmp);
-                               memset(tmp, 0x00, ctx->bs);
-                               tmp[0] = ctx->d;
-                               tmp[1] = ctx->bs;
-                               tmp[2] = 0x01;
+                               keccak_f1600(ctx->a);
+                               ((uint8_t*)ctx->a)[0] ^= ctx->d;
+                               ((uint8_t*)ctx->a)[1] ^= ctx->bs;
+                               ((uint8_t*)ctx->a)[2] ^= 0x01;
                        }
                }
        }
-       keccak_nextBlock(ctx, tmp);
+       keccak_f1600(ctx->a);
 }
 
 void keccak_ctx2hash(void* dest, uint16_t length_b, keccak_ctx_t* ctx){