]> git.cryptolib.org Git - arm-crypto-lib.git/blobdiff - blake/blake_large.c
more precise type for arguments of bcal_nessie_multiple()
[arm-crypto-lib.git] / blake / blake_large.c
index 15eb680d38ad2c24d2f7e790b636573ac98b5c1c..e3b5d5339542b5b1ae16ff7a0de0d5ab4ad98058 100644 (file)
@@ -82,21 +82,23 @@ void blake_large_changeendian(void* dest, const void* src){
 static
 void blake_large_compress(uint64_t* v,const void* m){
        uint8_t r,i;
-       uint8_t s0, s1;
+       uint16_t s, *p=(uint16_t*)blake_sigma;
        union {
                uint32_t v32;
                uint8_t v8[4];
        }idx;
-       for(r=0; r<14; ++r){
+       for(r=0; r<16; ++r){
                for(i=0; i<8; ++i){
                        idx.v32 = ((uint32_t*)blake_index_lut)[i];
-                       s0 = blake_sigma[16*r+2*i+0];
-                       s1 = blake_sigma[16*r+2*i+1];
-                       A += B + (((uint64_t*)m)[s0] ^ blake_c[s1]);
+                       s = *p++;
+                       if(p==(uint16_t*)(blake_sigma + 160)){
+                               p=(uint16_t*)blake_sigma;
+                       }
+                       A += B + (((uint64_t*)m)[s&0xff] ^ blake_c[s>>8]);
                        D  = ROTR64(D^A, 32);
                        C += D;
                        B  = ROTR64(B^C, 25);
-                       A += B + (((uint64_t*)m)[s1] ^ blake_c[s0]);
+                       A += B + (((uint64_t*)m)[s>>8] ^ blake_c[s&0xff]);
                        D  = ROTR64(D^A, 16);
                        C += D;
                        B  = ROTR64(B^C, 11);
@@ -135,21 +137,24 @@ void blake_large_lastBlock(blake_large_ctx_t* ctx, const void* msg, uint16_t len
                msg = (uint8_t*)msg + BLAKE_LARGE_BLOCKSIZE_B;
                length_b -= BLAKE_LARGE_BLOCKSIZE;
        }
-       uint8_t buffer[128];
+       union {
+               uint8_t   v8[128];
+               uint64_t v64[ 16];
+       } buffer;
        uint64_t v[16];
        uint64_t ctr;
        ctr = ctx->counter*1024+length_b;
-       memset(buffer, 0, 128);
-       memcpy(buffer, msg, (length_b+7)/8);
-       buffer[length_b/8] |= 0x80 >> (length_b&0x7);
-       blake_large_changeendian(buffer, buffer);
+       memset(buffer.v8, 0, 128);
+       memcpy(buffer.v8, msg, (length_b+7)/8);
+       buffer.v8[length_b/8] |= 0x80 >> (length_b&0x7);
+       blake_large_changeendian(buffer.v8, buffer.v8);
        blake_large_expand(v, ctx);
        if(length_b>1024-128-2){
                v[12] ^= ctr;
                v[13] ^= ctr;
-               blake_large_compress(v, buffer);
+               blake_large_compress(v, buffer.v8);
                blake_large_collapse(ctx, v);
-               memset(buffer, 0, 128-8);
+               memset(buffer.v8, 0, 128-8);
                blake_large_expand(v, ctx);
        } else {
                if(length_b){
@@ -158,25 +163,25 @@ void blake_large_lastBlock(blake_large_ctx_t* ctx, const void* msg, uint16_t len
                }
        }
        if(ctx->appendone)
-               buffer[128-16-8] |= 0x01;
-       *((uint64_t*)(&(buffer[128-8]))) = ctr;
-       blake_large_compress(v, buffer);
+               buffer.v8[128-16-8] |= 0x01;
+       buffer.v64[15] = ctr;
+       blake_large_compress(v, buffer.v8);
        blake_large_collapse(ctx, v);
 
 }
 
 static const
-uint64_t blake64_iv[] = {
+uint64_t blake512_iv[] = {
     0x6A09E667F3BCC908LL, 0xBB67AE8584CAA73BLL,
     0x3C6EF372FE94F82BLL, 0xA54FF53A5F1D36F1LL,
     0x510E527FADE682D1LL, 0x9B05688C2B3E6C1FLL,
     0x1F83D9ABFB41BD6BLL, 0x5BE0CD19137E2179LL
 };
 
-void blake64_init(blake64_ctx_t* ctx){
+void blake512_init(blake512_ctx_t* ctx){
        uint8_t i;
        for(i=0; i<8; ++i){
-               ctx->h[i] = blake64_iv[i];
+               ctx->h[i] = blake512_iv[i];
        }
        memset(ctx->s, 0, 4*8);
        ctx->counter = 0;
@@ -184,24 +189,24 @@ void blake64_init(blake64_ctx_t* ctx){
 }
 
 static const
-uint64_t blake48_iv[] = {
+uint64_t blake384_iv[] = {
     0xCBBB9D5DC1059ED8LL, 0x629A292A367CD507LL,
     0x9159015A3070DD17LL, 0x152FECD8F70E5939LL,
     0x67332667FFC00B31LL, 0x8EB44A8768581511LL,
     0xDB0C2E0D64F98FA7LL, 0x47B5481DBEFA4FA4LL
 };
 
-void blake48_init(blake48_ctx_t* ctx){
+void blake384_init(blake384_ctx_t* ctx){
        uint8_t i;
        for(i=0; i<8; ++i){
-               ctx->h[i] = blake48_iv[i];
+               ctx->h[i] = blake384_iv[i];
        }
        memset(ctx->s, 0, 4*8);
        ctx->counter = 0;
        ctx->appendone = 0;
 }
 
-void blake64_ctx2hash(void* dest, const blake64_ctx_t* ctx){
+void blake512_ctx2hash(void* dest, const blake512_ctx_t* ctx){
        uint8_t i;
        for(i=0; i<8; ++i){
                ((uint32_t*)dest)[2*i+0] = CHANGE_ENDIAN32((ctx->h[i])>>32);
@@ -209,7 +214,7 @@ void blake64_ctx2hash(void* dest, const blake64_ctx_t* ctx){
        }
 }
 
-void blake48_ctx2hash(void* dest, const blake48_ctx_t* ctx){
+void blake384_ctx2hash(void* dest, const blake384_ctx_t* ctx){
        uint8_t i;
        for(i=0; i<6; ++i){
                ((uint32_t*)dest)[2*i+0] = CHANGE_ENDIAN32((ctx->h[i])>>32);
@@ -217,42 +222,42 @@ void blake48_ctx2hash(void* dest, const blake48_ctx_t* ctx){
        }
 }
 
-void blake64_nextBlock(blake64_ctx_t* ctx, const void* block){
+void blake512_nextBlock(blake512_ctx_t* ctx, const void* block){
        blake_large_nextBlock(ctx, block);
 }
 
-void blake48_nextBlock(blake48_ctx_t* ctx, const void* block){
+void blake384_nextBlock(blake384_ctx_t* ctx, const void* block){
        blake_large_nextBlock(ctx, block);
 }
 
-void blake64_lastBlock(blake64_ctx_t* ctx, const void* block, uint16_t length_b){
+void blake512_lastBlock(blake512_ctx_t* ctx, const void* block, uint16_t length_b){
        blake_large_lastBlock(ctx, block, length_b);
 }
 
-void blake48_lastBlock(blake48_ctx_t* ctx, const void* block, uint16_t length_b){
+void blake384_lastBlock(blake384_ctx_t* ctx, const void* block, uint16_t length_b){
        blake_large_lastBlock(ctx, block, length_b);
 }
 
-void blake64(void* dest, const void* msg, uint32_t length_b){
+void blake512(void* dest, const void* msg, uint32_t length_b){
        blake_large_ctx_t ctx;
-       blake64_init(&ctx);
+       blake512_init(&ctx);
        while(length_b>=BLAKE_LARGE_BLOCKSIZE){
                blake_large_nextBlock(&ctx, msg);
                msg = (uint8_t*)msg + BLAKE_LARGE_BLOCKSIZE_B;
                length_b -= BLAKE_LARGE_BLOCKSIZE;
        }
        blake_large_lastBlock(&ctx, msg, length_b);
-       blake64_ctx2hash(dest, &ctx);
+       blake512_ctx2hash(dest, &ctx);
 }
 
-void blake48(void* dest, const void* msg, uint32_t length_b){
+void blake384(void* dest, const void* msg, uint32_t length_b){
        blake_large_ctx_t ctx;
-       blake48_init(&ctx);
+       blake384_init(&ctx);
        while(length_b>=BLAKE_LARGE_BLOCKSIZE){
                blake_large_nextBlock(&ctx, msg);
                msg = (uint8_t*)msg + BLAKE_LARGE_BLOCKSIZE_B;
                length_b -= BLAKE_LARGE_BLOCKSIZE;
        }
        blake_large_lastBlock(&ctx, msg, length_b);
-       blake48_ctx2hash(dest, &ctx);
+       blake384_ctx2hash(dest, &ctx);
 }