X-Git-Url: https://git.cryptolib.org/?a=blobdiff_plain;f=blake%2Fblake_small.c;h=9467b04e622641ee49e0b83fbe185a9ef94810e4;hb=a707b2a401ed87cbeee4888bba923f605e951c89;hp=cc3f6d2dd21a0579ba400c1b0946d88aa471e58e;hpb=701cee0d98aab48dd3192c8cc7c77eb42581bc56;p=avr-crypto-lib.git diff --git a/blake/blake_small.c b/blake/blake_small.c index cc3f6d2..9467b04 100644 --- a/blake/blake_small.c +++ b/blake/blake_small.c @@ -51,7 +51,7 @@ const uint32_t blake_c[] PROGMEM = { ((0x00ff0000&(a))>>8)| \ (a)>>24 ) static -void blake_small_expand(uint32_t* v, const blake_small_ctx_t* ctx){ +void blake_small_expand(uint32_t *v, const blake_small_ctx_t *ctx){ uint8_t i; memcpy(v, ctx->h, 8*4); for(i=0; i<8; ++i){ @@ -62,7 +62,7 @@ void blake_small_expand(uint32_t* v, const blake_small_ctx_t* ctx){ } static -void blake_small_changeendian(void* dest, const void* src){ +void blake_small_changeendian(void *dest, const void *src){ uint8_t i; for(i=0; i<16; ++i){ ((uint32_t*)dest)[i] = CHANGE_ENDIAN32(((uint32_t*)src)[i]); @@ -70,7 +70,7 @@ void blake_small_changeendian(void* dest, const void* src){ } static -void blake_small_compress(uint32_t* v,const void* m){ +void blake_small_compress(uint32_t *v,const void *m){ uint8_t r,i; uint8_t a,b,c,d, s0, s1, sigma_idx=0; uint32_t lv[4]; @@ -109,14 +109,14 @@ void blake_small_compress(uint32_t* v,const void* m){ } static -void blake_small_collapse(blake_small_ctx_t* ctx, uint32_t* v){ +void blake_small_collapse(blake_small_ctx_t *ctx, uint32_t *v){ uint8_t i; for(i=0; i<8; ++i){ ctx->h[i] ^= ctx->s[i%4] ^ v[i] ^ v[8+i]; } } -void blake_small_nextBlock(blake_small_ctx_t* ctx, const void* msg){ +void blake_small_nextBlock(blake_small_ctx_t *ctx, const void *msg){ uint32_t v[16]; uint32_t m[16]; union { @@ -135,32 +135,35 @@ void blake_small_nextBlock(blake_small_ctx_t* ctx, const void* msg){ blake_small_collapse(ctx, v); } -void blake_small_lastBlock(blake_small_ctx_t* ctx, const void* msg, uint16_t length_b){ +void blake_small_lastBlock(blake_small_ctx_t *ctx, const void *msg, uint16_t length_b){ while(length_b>=BLAKE_SMALL_BLOCKSIZE){ blake_small_nextBlock(ctx, msg); 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]; + } 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){ @@ -171,10 +174,10 @@ 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); } @@ -186,7 +189,7 @@ const uint32_t blake256_iv[] PROGMEM = { 0x1F83D9ABL, 0x5BE0CD19 }; -void blake256_init(blake256_ctx_t* ctx){ +void blake256_init(blake256_ctx_t *ctx){ uint8_t i; for(i=0; i<8; ++i){ ctx->h[i] = pgm_read_dword(&(blake256_iv[i])); @@ -203,7 +206,7 @@ const uint32_t blake224_iv[] PROGMEM = { 0x64F98FA7, 0xBEFA4FA4 }; -void blake224_init(blake224_ctx_t* ctx){ +void blake224_init(blake224_ctx_t *ctx){ uint8_t i; for(i=0; i<8; ++i){ ctx->h[i] = pgm_read_dword(&(blake224_iv[i])); @@ -213,37 +216,37 @@ void blake224_init(blake224_ctx_t* ctx){ ctx->appendone = 0; } -void blake256_ctx2hash(void* dest, const blake256_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 blake224_ctx2hash(void* dest, const blake224_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 blake256_nextBlock(blake256_ctx_t* ctx, const void* block){ +void blake256_nextBlock(blake256_ctx_t *ctx, const void *block){ blake_small_nextBlock(ctx, block); } -void blake224_nextBlock(blake224_ctx_t* ctx, const void* block){ +void blake224_nextBlock(blake224_ctx_t *ctx, const void *block){ blake_small_nextBlock(ctx, block); } -void blake256_lastBlock(blake256_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 blake224_lastBlock(blake224_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 blake256(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; blake256_init(&ctx); while(length_b>=BLAKE_SMALL_BLOCKSIZE){ @@ -255,7 +258,7 @@ void blake256(void* dest, const void* msg, uint32_t length_b){ blake256_ctx2hash(dest, &ctx); } -void blake224(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; blake224_init(&ctx); while(length_b>=BLAKE_SMALL_BLOCKSIZE){