]> git.cryptolib.org Git - avr-crypto-lib.git/blob - skein/ubi256.c
fixing E-Mail-Address & Copyright
[avr-crypto-lib.git] / skein / ubi256.c
1 /* ubi256.c */
2 /*
3     This file is part of the ARM-Crypto-Lib.
4     Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org)
5
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.
10
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.
15
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/>.
18 */
19 /*
20  * \author  Daniel Otte
21  * \email   bg@nerilex.org
22  * \date    2009-03-12
23  * \license GPLv3 or later
24  * 
25  */
26
27 #include <stdint.h>
28 #include <string.h>
29 #include "threefish.h"
30 #include "memxor.h"
31 #include "ubi.h"
32
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);
37 }
38
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;
47
48
49
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;
56         }
57         ctx->tweak.v8[15] |= 0x80;
58         ctx->tweak.v64[0] += (length_b+7)/8;
59         if(length_b & 0x07){
60                 ctx->tweak.v8[14] |= 0x80;
61         }
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);
65         if(length_b & 0x07){
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);
68         }
69         threefish256_enc(ctx->g, &tfctx);
70         memxor(ctx->g, block, (length_b+7)/8);
71         if(length_b & 0x07){
72                 ctx->g[((length_b+7)/8)-1] ^= 0x80>>(length_b&7);
73         }
74
75
76
77 void ubi256_ctx2hash(void *dest, const ubi256_ctx_t *ctx){
78         memcpy(dest, ctx->g, UBI256_BLOCKSIZE_B);
79 }
80