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 ubi1024_init(ubi1024_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, UBI1024_BLOCKSIZE_B);
58 /******************************************************************************/
60 void ubi1024_ctx2hash(void* dest, const ubi1024_ctx_t* ctx){
61 memcpy(dest, ctx->g, UBI1024_BLOCKSIZE_B);
68 .global ubi1024_ctx2hash
80 /******************************************************************************/
82 void ubi1024_nextBlock(ubi1024_ctx_t* ctx, const void* block){
83 threefish1024_ctx_t tfctx;
84 ((uint64_t*)(ctx->tweak))[0] += UBI1024_BLOCKSIZE_B;
85 threefish1024_init(ctx->g, ctx->tweak, &tfctx);
86 memcpy(ctx->g, block, UBI1024_BLOCKSIZE_B);
87 threefish1024_enc(ctx->g, &tfctx);
88 memxor(ctx->g, block, UBI1024_BLOCKSIZE_B);
89 ctx->tweak[15] &= (uint8_t)~0x40;
94 * param block: r22:r23
102 .global ubi1024_nextBlock
104 stack_alloc_large 20*8
106 adiw r30, 1 /* Z points to tfctx */
111 /* add BLOCKSIZE_B (128) to tweak */
122 /* call threefish1024_init */
126 movw CTX0, r24 /* CTX points to ctx->g */
128 rcall threefish1024_init
129 /* copy block to ctx->g */
137 /* call threefish1024_enc */
140 rcall threefish1024_enc
141 /* xor block into ctx->g */
151 /* clear 'first' bit in tweak */
160 stack_free_large2 20*8
163 /******************************************************************************/
165 void ubi1024_lastBlock(ubi1024_ctx_t* ctx, const void* block, uint16_t length_b){
166 threefish1024_ctx_t tfctx;
167 while(length_b>UBI1024_BLOCKSIZE){
168 ubi1024_nextBlock(ctx, block);
169 block = (uint8_t*)block + UBI1024_BLOCKSIZE_B;
170 length_b -= UBI1024_BLOCKSIZE;
172 ctx->tweak[15] |= 0x80;
173 ((uint64_t*)(ctx->tweak))[0] += (length_b+7)/8;
175 ctx->tweak[14] |= 0x80;
176 threefish1024_init(ctx->g, ctx->tweak, &tfctx);
177 memset(ctx->g, 0, UBI1024_BLOCKSIZE_B);
178 memcpy(ctx->g, block, (length_b+7)/8);
180 ctx->g[(length_b+7)/8-1] |= 0x80>>(length_b&7);
181 threefish1024_enc(ctx->g, &tfctx);
182 memxor(ctx->g, block, (length_b+7)/8);
184 ctx->g[((length_b+7)/8)-1] ^= 0x80>>(length_b&7);
190 * param block: r22:r23
191 * param ength_b: r20:r21
203 .global ubi1024_lastBlock
205 /* run nextBlock for preceding blocks*/
214 rcall ubi1024_nextBlock
226 rcall ubi1024_nextBlock
231 3: /* now the real fun */
232 stack_alloc_large 20*8
235 /* calculate LEN_B */
245 /* add length to tweak */
261 /* store in MASK_B if we do bit processing and set 'BitPad' bit*/
275 4: /* call threefish1024_init*/
279 movw CTX0, r24 /* CTX points at ctx->g */
281 rcall threefish1024_init
282 /* copy block to ctx->g */
304 4: /* call threefish1024_enc */
307 rcall threefish1024_enc
308 /* xor block into ctx->g */
324 7: stack_free_large2 20*8