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 ubi512_init(ubi512_ctx_t* ctx, const void* g, uint8_t type){
34 memset(ctx->tweak, 0, 15);
35 ctx->tweak[15] = 0x40+type;
36 memcpy(ctx->g, g, UBI512_BLOCKSIZE_B);
39 void ubi512_nextBlock(ubi512_ctx_t* ctx, const void* block){
40 threefish512_ctx_t tfctx;
41 ((uint64_t*)(ctx->tweak))[0] += UBI512_BLOCKSIZE_B;
42 threefish512_init(ctx->g, ctx->tweak, &tfctx);
43 memcpy(ctx->g, block, UBI512_BLOCKSIZE_B);
44 threefish512_enc(ctx->g, &tfctx);
45 memxor(ctx->g, block, UBI512_BLOCKSIZE_B);
46 ctx->tweak[15] &= (uint8_t)~0x40;
50 void ubi512_lastBlock(ubi512_ctx_t* ctx, const void* block, uint16_t length_b){
51 threefish512_ctx_t tfctx;
52 while(length_b>UBI512_BLOCKSIZE){
53 ubi512_nextBlock(ctx, block);
54 block = (uint8_t*)block + UBI512_BLOCKSIZE_B;
55 length_b -= UBI512_BLOCKSIZE;
57 ctx->tweak[15] |= 0x80;
58 ((uint64_t*)(ctx->tweak))[0] += (length_b+7)/8;
60 ctx->tweak[14] |= 0x80;
61 threefish512_init(ctx->g, ctx->tweak, &tfctx);
62 memset(ctx->g, 0, UBI512_BLOCKSIZE_B);
63 memcpy(ctx->g, block, (length_b+7)/8);
65 ctx->g[(length_b+7)/8-1] |= 0x80>>(length_b&7);
66 threefish512_enc(ctx->g, &tfctx);
67 memxor(ctx->g, block, (length_b+7)/8);
69 ctx->g[((length_b+7)/8)-1] ^= 0x80>>(length_b&7);
73 void ubi512_ctx2hash(void* dest, const ubi512_ctx_t* ctx){
74 memcpy(dest, ctx->g, UBI512_BLOCKSIZE_B);