3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2009 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
26 #include "avr-asm-macros.S"
28 /******************************************************************************/
30 void ubi512_init(ubi512_ctx_t *ctx, const void *g, uint8_t type){
31 memset(ctx->tweak, 0, 15);
32 ctx->tweak[15] = 0x40+type;
33 memcpy(ctx->g, g, UBI512_BLOCKSIZE_B);
58 /******************************************************************************/
60 void ubi512_ctx2hash(void *dest, const ubi512_ctx_t *ctx){
61 memcpy(dest, ctx->g, UBI512_BLOCKSIZE_B);
68 .global ubi512_ctx2hash
80 /******************************************************************************/
82 void ubi512_nextBlock(ubi512_ctx_t *ctx, const void *block){
83 threefish512_ctx_t tfctx;
84 ((uint64_t*)(ctx->tweak))[0] += UBI512_BLOCKSIZE_B;
85 threefish512_init(ctx->g, ctx->tweak, &tfctx);
86 memcpy(ctx->g, block, UBI512_BLOCKSIZE_B);
87 threefish512_enc(ctx->g, &tfctx);
88 memxor(ctx->g, block, UBI512_BLOCKSIZE_B);
89 ctx->tweak[15] &= (uint8_t)~0x40;
94 * param block: r22:r23
102 .global ubi512_nextBlock
104 stack_alloc_large 12*8
106 adiw r30, 1 /* Z points to tfctx */
111 /* add BLOCKSIZE_B (64) to tweak */
122 /* call threefish512_init */
126 movw CTX0, r24 /* CTX points to ctx->g */
128 rcall threefish512_init
129 /* copy block to ctx->g */
137 /* call threefish512_enc */
140 rcall threefish512_enc
141 /* xor block into ctx->g */
151 /* clear 'first' bit in tweak */
159 stack_free_large 12*8
162 /******************************************************************************/
164 void ubi512_lastBlock(ubi512_ctx_t *ctx, const void *block, uint16_t length_b){
165 threefish512_ctx_t tfctx;
166 while(length_b>UBI512_BLOCKSIZE){
167 ubi512_nextBlock(ctx, block);
168 block = (uint8_t*)block + UBI512_BLOCKSIZE_B;
169 length_b -= UBI512_BLOCKSIZE;
171 ctx->tweak[15] |= 0x80;
172 ((uint64_t*)(ctx->tweak))[0] += (length_b+7)/8;
174 ctx->tweak[14] |= 0x80;
175 threefish512_init(ctx->g, ctx->tweak, &tfctx);
176 memset(ctx->g, 0, UBI512_BLOCKSIZE_B);
177 memcpy(ctx->g, block, (length_b+7)/8);
179 ctx->g[(length_b+7)/8-1] |= 0x80>>(length_b&7);
180 threefish512_enc(ctx->g, &tfctx);
181 memxor(ctx->g, block, (length_b+7)/8);
183 ctx->g[((length_b+7)/8)-1] ^= 0x80>>(length_b&7);
189 * param block: r22:r23
190 * param ength_b: r20:r21
202 .global ubi512_lastBlock
204 /* run nextBlock for preceding blocks*/
213 rcall ubi512_nextBlock
225 rcall ubi512_nextBlock
230 3: /* now the real fun */
231 stack_alloc_large 8*12
234 /* calculate LEN_B */
243 /* add length to tweak */
259 /* store in MASk_B if we do bit processing and set 'BitPad' bit*/
273 4: /* call threefish512_init*/
277 movw CTX0, r24 /* CTX points at ctx->g */
279 rcall threefish512_init
280 /* copy block to ctx->g */
302 4: /* call threefish512_enc */
305 rcall threefish512_enc
306 /* xor block into ctx->g */
322 7: stack_free_large 8*12