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 ubi256_init(ubi256_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, 32);
58 /******************************************************************************/
60 void ubi256_ctx2hash(void *dest, const ubi256_ctx_t *ctx){
61 memcpy(dest, ctx->g, UBI256_BLOCKSIZE_B);
68 .global ubi256_ctx2hash
80 /******************************************************************************/
82 void ubi256_nextBlock(ubi256_ctx_t *ctx, const void *block){
83 threefish256_ctx_t tfctx;
84 ((uint64_t*)(ctx->tweak))[0] += UBI256_BLOCKSIZE_B;
85 threefish256_init(ctx->g, ctx->tweak, &tfctx);
86 memcpy(ctx->g, block, UBI256_BLOCKSIZE_B);
87 threefish256_enc(ctx->g, &tfctx);
88 memxor(ctx->g, block, UBI256_BLOCKSIZE_B);
89 ctx->tweak[15] &= (uint8_t)~0x40;
94 * param block: r22:r23
102 .global ubi256_nextBlock
106 adiw r30, 1 /* Z points to tfctx */
111 /* add BLOCKSIZE_B (32) to tweak */
122 /* call threefish256_init */
126 movw CTX0, r24 /* CTX points to ctx->g */
128 rcall threefish256_init
129 /* copy block to ctx->g */
137 /* call threefish256_enc */
140 rcall threefish256_enc
141 /* xor block into ctx->g */
151 /* clear 'first' bit in tweak */
161 /******************************************************************************/
163 void ubi256_lastBlock(ubi256_ctx_t *ctx, const void *block, uint16_t length_b){
164 threefish256_ctx_t tfctx;
165 while(length_b>UBI256_BLOCKSIZE){
166 ubi256_nextBlock(ctx, block);
167 block = (uint8_t*)block + UBI256_BLOCKSIZE_B;
168 length_b -= UBI256_BLOCKSIZE;
170 ctx->tweak[15] |= 0x80;
171 ((uint64_t*)(ctx->tweak))[0] += (length_b+7)/8;
173 ctx->tweak[14] |= 0x80;
175 threefish256_init(ctx->g, ctx->tweak, &tfctx);
176 memset(ctx->g, 0, UBI256_BLOCKSIZE_B);
177 memcpy(ctx->g, block, (length_b+7)/8);
179 ctx->g[((length_b+7)/8)-1] |= 0x80>>(length_b&7);
180 ctx->g[((length_b+7)/8)-1] &= ~((0x80>>(length_b&7))-1);
182 threefish256_enc(ctx->g, &tfctx);
183 memxor(ctx->g, block, (length_b+7)/8);
185 ctx->g[((length_b+7)/8)-1] ^= 0x80>>(length_b&7);
191 * param block: r22:r23
192 * param ength_b: r20:r21
204 .global ubi256_lastBlock
206 /* run nextBlock for preceding blocks*/
215 rcall ubi256_nextBlock
227 rcall ubi256_nextBlock
232 3: /* now the real fun */
236 /* calculate LEN_B */
244 /* add length to tweak */
260 /* store in T if we do bit processing and set 'BitPad' bit*/
274 4: /* call threefish256_init*/
278 movw CTX0, r24 /* CTX points at ctx->g */
280 rcall threefish256_init
281 /* copy block to ctx->g */
303 4: /* call threefish256_enc */
306 rcall threefish256_enc
307 /* xor block into ctx->g */
323 7: stack_free_large 64