]> git.cryptolib.org Git - avr-crypto-lib.git/blob - skein/ubi512.c
fixing E-Mail-Address & Copyright
[avr-crypto-lib.git] / skein / ubi512.c
1 /* ubi512.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 ubi512_init(ubi512_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, UBI512_BLOCKSIZE_B);
37 }
38
39 void ubi512_nextBlock(ubi512_ctx_t *ctx, const void *block){
40         threefish512_ctx_t tfctx;
41         ctx->tweak.v64[0] += UBI512_BLOCKSIZE_B;
42         threefish512_init(ctx->g, ctx->tweak.v8, &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.v8[15] &= (uint8_t)~0x40;
47
48
49
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;
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         threefish512_init(ctx->g, ctx->tweak.v8, &tfctx);
62         memset(ctx->g, 0, UBI512_BLOCKSIZE_B);
63         memcpy(ctx->g, block, (length_b+7)/8);
64         if(length_b & 0x07)
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);
68         if(length_b & 0x07){
69                 ctx->g[((length_b+7)/8)-1] ^= 0x80>>(length_b&7);
70         }
71
72
73 void ubi512_ctx2hash(void *dest, const ubi512_ctx_t *ctx){
74         memcpy(dest, ctx->g, UBI512_BLOCKSIZE_B);
75 }
76