X-Git-Url: https://git.cryptolib.org/?a=blobdiff_plain;f=blake%2Fblake_small.c;h=e096d5256fad35e69f9986f44c3219e0f3b70c25;hb=257ce629ccb9d28193912b855322c91408fd19a7;hp=0b3fca281ad4e101912f6d2a614cf093c6c64dbb;hpb=d70d1d77bab1a5f5278227d674bc59da0378fe15;p=arm-crypto-lib.git diff --git a/blake/blake_small.c b/blake/blake_small.c index 0b3fca2..e096d52 100644 --- a/blake/blake_small.c +++ b/blake/blake_small.c @@ -27,7 +27,7 @@ #include #include -#include "memxor/memxor.h" +#include "memxor.h" #include "blake_small.h" #include "blake_common.h" @@ -76,21 +76,23 @@ void blake_small_changeendian(void* dest, const void* src){ static void blake_small_compress(uint32_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<10; ++r){ + for(r=0; r<14; ++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 + (((uint32_t*)m)[s0] ^ blake_c[s1]); + s = *p++; + if(p==(uint16_t*)(blake_sigma+160)){ + p=(uint16_t*)blake_sigma; + } + A += B + (((uint32_t*)m)[s&0xff] ^ blake_c[s>>8]); D = ROTR32(A^D, 16); C += D; B = ROTR32(B^C, 12); - A += B + (((uint32_t*)m)[s1] ^ blake_c[s0]); + A += B + (((uint32_t*)m)[s>>8] ^ blake_c[s&0xff]); D = ROTR32(A^D, 8); C += D; B = ROTR32(B^C, 7); @@ -131,26 +133,30 @@ void blake_small_lastBlock(blake_small_ctx_t* ctx, const void* msg, uint16_t len msg = (uint8_t*)msg + BLAKE_SMALL_BLOCKSIZE_B; length_b -= BLAKE_SMALL_BLOCKSIZE; } - uint8_t buffer[64]; + union { + uint8_t v8[64]; + uint32_t v32[16]; + uint64_t v64[ 8]; + } buffer; uint32_t v[16]; union { uint64_t v64; uint32_t v32[2]; }ctr; ctr.v64 = ctx->counter*512+length_b; - memset(buffer, 0, 64); - memcpy(buffer, msg, (length_b+7)/8); - buffer[length_b/8] |= 0x80 >> (length_b&0x7); - blake_small_changeendian(buffer, buffer); + memset(buffer.v8, 0, 64); + memcpy(buffer.v8, msg, (length_b+7)/8); + buffer.v8[length_b/8] |= 0x80 >> (length_b&0x7); + blake_small_changeendian(buffer.v8, buffer.v8); blake_small_expand(v, ctx); if(length_b>512-64-2){ v[12] ^= ctr.v32[0]; v[13] ^= ctr.v32[0]; v[14] ^= ctr.v32[1]; v[15] ^= ctr.v32[1]; - blake_small_compress(v, buffer); + blake_small_compress(v, buffer.v8); blake_small_collapse(ctx, v); - memset(buffer, 0, 64-8); + memset(buffer.v8, 0, 64-8); blake_small_expand(v, ctx); }else{ if(length_b){ @@ -161,26 +167,26 @@ void blake_small_lastBlock(blake_small_ctx_t* ctx, const void* msg, uint16_t len } } if(ctx->appendone) - buffer[64-8-4] |= 0x01; - *((uint32_t*)(&(buffer[64-8]))) = ctr.v32[1]; - *((uint32_t*)(&(buffer[64-4]))) = ctr.v32[0]; - blake_small_compress(v, buffer); + buffer.v8[64-8-4] |= 0x01; + buffer.v32[14] = ctr.v32[1]; + buffer.v32[15] = ctr.v32[0]; + blake_small_compress(v, buffer.v8); blake_small_collapse(ctx, v); } static const -uint32_t blake32_iv[] = { +uint32_t blake256_iv[] = { 0x6A09E667L, 0xBB67AE85, 0x3C6EF372L, 0xA54FF53A, 0x510E527FL, 0x9B05688C, 0x1F83D9ABL, 0x5BE0CD19 }; -void blake32_init(blake32_ctx_t* ctx){ +void blake256_init(blake256_ctx_t* ctx){ uint8_t i; for(i=0; i<8; ++i){ - ctx->h[i] = blake32_iv[i]; + ctx->h[i] = blake256_iv[i]; } memset(ctx->s, 0, 4*4); ctx->counter = 0; @@ -188,73 +194,73 @@ void blake32_init(blake32_ctx_t* ctx){ } static const -uint32_t blake28_iv[] = { +uint32_t blake224_iv[] = { 0xC1059ED8, 0x367CD507, 0x3070DD17, 0xF70E5939, 0xFFC00B31, 0x68581511, 0x64F98FA7, 0xBEFA4FA4 }; -void blake28_init(blake28_ctx_t* ctx){ +void blake224_init(blake224_ctx_t* ctx){ uint8_t i; for(i=0; i<8; ++i){ - ctx->h[i] = blake28_iv[i]; + ctx->h[i] = blake224_iv[i]; } memset(ctx->s, 0, 4*4); ctx->counter = 0; ctx->appendone = 0; } -void blake32_ctx2hash(void* dest, const blake32_ctx_t* ctx){ +void blake256_ctx2hash(void* dest, const blake256_ctx_t* ctx){ uint8_t i; for(i=0; i<8; ++i){ ((uint32_t*)dest)[i] = CHANGE_ENDIAN32(ctx->h[i]); } } -void blake28_ctx2hash(void* dest, const blake28_ctx_t* ctx){ +void blake224_ctx2hash(void* dest, const blake224_ctx_t* ctx){ uint8_t i; for(i=0; i<7; ++i){ ((uint32_t*)dest)[i] = CHANGE_ENDIAN32(ctx->h[i]); } } -void blake32_nextBlock(blake32_ctx_t* ctx, const void* block){ +void blake256_nextBlock(blake256_ctx_t* ctx, const void* block){ blake_small_nextBlock(ctx, block); } -void blake28_nextBlock(blake28_ctx_t* ctx, const void* block){ +void blake224_nextBlock(blake224_ctx_t* ctx, const void* block){ blake_small_nextBlock(ctx, block); } -void blake32_lastBlock(blake32_ctx_t* ctx, const void* block, uint16_t length_b){ +void blake256_lastBlock(blake256_ctx_t* ctx, const void* block, uint16_t length_b){ blake_small_lastBlock(ctx, block, length_b); } -void blake28_lastBlock(blake28_ctx_t* ctx, const void* block, uint16_t length_b){ +void blake224_lastBlock(blake224_ctx_t* ctx, const void* block, uint16_t length_b){ blake_small_lastBlock(ctx, block, length_b); } -void blake32(void* dest, const void* msg, uint32_t length_b){ +void blake256(void* dest, const void* msg, uint32_t length_b){ blake_small_ctx_t ctx; - blake32_init(&ctx); + blake256_init(&ctx); while(length_b>=BLAKE_SMALL_BLOCKSIZE){ blake_small_nextBlock(&ctx, msg); msg = (uint8_t*)msg + BLAKE_SMALL_BLOCKSIZE_B; length_b -= BLAKE_SMALL_BLOCKSIZE; } blake_small_lastBlock(&ctx, msg, length_b); - blake32_ctx2hash(dest, &ctx); + blake256_ctx2hash(dest, &ctx); } -void blake28(void* dest, const void* msg, uint32_t length_b){ +void blake224(void* dest, const void* msg, uint32_t length_b){ blake_small_ctx_t ctx; - blake28_init(&ctx); + blake224_init(&ctx); while(length_b>=BLAKE_SMALL_BLOCKSIZE){ blake_small_nextBlock(&ctx, msg); msg = (uint8_t*)msg + BLAKE_SMALL_BLOCKSIZE_B; length_b -= BLAKE_SMALL_BLOCKSIZE; } blake_small_lastBlock(&ctx, msg, length_b); - blake28_ctx2hash(dest, &ctx); + blake224_ctx2hash(dest, &ctx); }