X-Git-Url: https://git.cryptolib.org/?a=blobdiff_plain;f=ubi256.c;h=09eb1e9b2a92fbaa90275e4730680d678c27048c;hb=92725df162cf1ce83c4bd002fdaff69707e5f310;hp=8adfda89f02d8969e29becb881d829ea65dd7d90;hpb=b5a057d2dfa6b43f3850b3858c11a079eb9086f0;p=avr-crypto-lib.git diff --git a/ubi256.c b/ubi256.c index 8adfda8..09eb1e9 100644 --- a/ubi256.c +++ b/ubi256.c @@ -30,13 +30,13 @@ #include "memxor.h" #include "ubi.h" -void ubi256_init(ubi256_ctx_t* ctx, void* g, uint8_t type){ +void ubi256_init(ubi256_ctx_t* ctx, const void* g, uint8_t type){ memset(ctx->tweak, 0, 15); ctx->tweak[15] = 0x40+type; memcpy(ctx->g, g, 32); } -void ubi256_nextBlock(ubi256_ctx_t* ctx, void* block){ +void ubi256_nextBlock(ubi256_ctx_t* ctx, const void* block){ threefish256_ctx_t tfctx; ((uint64_t*)(ctx->tweak))[0] += UBI256_BLOCKSIZE_B; threefish256_init(ctx->g, ctx->tweak, &tfctx); @@ -47,7 +47,7 @@ void ubi256_nextBlock(ubi256_ctx_t* ctx, void* block){ } -void ubi256_lastBlock(ubi256_ctx_t* ctx, void* block, uint16_t length_b){ +void ubi256_lastBlock(ubi256_ctx_t* ctx, const void* block, uint16_t length_b){ threefish256_ctx_t tfctx; while(length_b>UBI256_BLOCKSIZE){ ubi256_nextBlock(ctx, block); @@ -56,18 +56,25 @@ void ubi256_lastBlock(ubi256_ctx_t* ctx, void* block, uint16_t length_b){ } ctx->tweak[15] |= 0x80; ((uint64_t*)(ctx->tweak))[0] += (length_b+7)/8; - if(length_b & 0x07) + if(length_b & 0x07){ ctx->tweak[14] |= 0x80; + } threefish256_init(ctx->g, ctx->tweak, &tfctx); memset(ctx->g, 0, UBI256_BLOCKSIZE_B); memcpy(ctx->g, block, (length_b+7)/8); - if(length_b & 0x07) - ctx->g[(length_b+7)/8-1] |= 0x80>>(length_b&7); + if(length_b & 0x07){ + ctx->g[((length_b+7)/8)-1] |= 0x80>>(length_b&7); + ctx->g[((length_b+7)/8)-1] &= ~((0x80>>(length_b&7))-1); + } threefish256_enc(ctx->g, &tfctx); memxor(ctx->g, block, (length_b+7)/8); + if(length_b & 0x07){ + ctx->g[((length_b+7)/8)-1] ^= 0x80>>(length_b&7); + } + } -void ubi256_ctx2hash(void* dest, ubi256_ctx_t* ctx){ +void ubi256_ctx2hash(void* dest, const ubi256_ctx_t* ctx){ memcpy(dest, ctx->g, UBI256_BLOCKSIZE_B); }