3 This file is part of the ARM-Crypto-Lib.
4 Copyright (C) 2006-2010 Daniel Otte (daniel.otte@rub.de)
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * \email daniel.otte@rub.de
23 * \license GPLv3 or later
29 #include "threefish.h"
33 void ubi256_init(ubi256_ctx_t* ctx, const void* g, uint8_t type){
34 memset(ctx->tweak.v8, 0, 15);
35 ctx->tweak.v8[15] = 0x40+type;
36 memcpy(ctx->g, g, 32);
39 void ubi256_nextBlock(ubi256_ctx_t* ctx, const void* block){
40 threefish256_ctx_t tfctx;
41 ctx->tweak.v64[0] += UBI256_BLOCKSIZE_B;
42 threefish256_init(ctx->g, ctx->tweak.v8, &tfctx);
43 memcpy(ctx->g, block, UBI256_BLOCKSIZE_B);
44 threefish256_enc(ctx->g, &tfctx);
45 memxor(ctx->g, block, UBI256_BLOCKSIZE_B);
46 ctx->tweak.v8[15] &= (uint8_t)~0x40;
50 void ubi256_lastBlock(ubi256_ctx_t* ctx, const void* block, uint16_t length_b){
51 threefish256_ctx_t tfctx;
52 while(length_b>UBI256_BLOCKSIZE){
53 ubi256_nextBlock(ctx, block);
54 block = (uint8_t*)block + UBI256_BLOCKSIZE_B;
55 length_b -= UBI256_BLOCKSIZE;
57 ctx->tweak.v8[15] |= 0x80;
58 ctx->tweak.v64[0] += (length_b+7)/8;
60 ctx->tweak.v8[14] |= 0x80;
62 threefish256_init(ctx->g, ctx->tweak.v8, &tfctx);
63 memset(ctx->g, 0, UBI256_BLOCKSIZE_B);
64 memcpy(ctx->g, block, (length_b+7)/8);
66 ctx->g[((length_b+7)/8)-1] |= 0x80>>(length_b&7);
67 ctx->g[((length_b+7)/8)-1] &= ~((0x80>>(length_b&7))-1);
69 threefish256_enc(ctx->g, &tfctx);
70 memxor(ctx->g, block, (length_b+7)/8);
72 ctx->g[((length_b+7)/8)-1] ^= 0x80>>(length_b&7);
77 void ubi256_ctx2hash(void* dest, const ubi256_ctx_t* ctx){
78 memcpy(dest, ctx->g, UBI256_BLOCKSIZE_B);