]> git.cryptolib.org Git - avr-crypto-lib.git/blob - skein/skein512.c
fixing E-Mail-Address & Copyright
[avr-crypto-lib.git] / skein / skein512.c
1 /* skein512.c */
2 /*
3     This file is part of the AVR-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 "ubi.h"
30 #include "skein.h"
31
32
33 void skein512_init(skein512_ctx_t *ctx, uint16_t outsize_b){
34         skein_config_t conf;
35         uint8_t null[UBI512_BLOCKSIZE_B];
36         memset(null, 0, UBI512_BLOCKSIZE_B);
37         memset(&conf, 0, sizeof(skein_config_t));
38         conf.schema[0] = 'S';
39         conf.schema[1] = 'H';
40         conf.schema[2] = 'A';
41         conf.schema[3] = '3';
42         conf.version = 1;
43         conf.out_length = outsize_b;
44         ctx->outsize_b = outsize_b;
45         ubi512_init(&(ctx->ubictx), null, UBI_TYPE_CFG);
46         ubi512_lastBlock(&(ctx->ubictx), &conf, 256);
47         ubi512_init(&(ctx->ubictx), ctx->ubictx.g, UBI_TYPE_MSG);
48 }
49
50 void skein512_nextBlock(skein512_ctx_t *ctx, const void *block){
51         ubi512_nextBlock(&(ctx->ubictx), block);
52 }
53
54 void skein512_lastBlock(skein512_ctx_t *ctx, const void *block, uint16_t length_b){
55         ubi512_lastBlock(&(ctx->ubictx), block, length_b);
56 }
57
58 void skein512_ctx2hash(void *dest, skein512_ctx_t *ctx){
59         ubi512_ctx_t uctx;
60         uint16_t outsize_b;
61         
62         uint64_t counter=0;
63         uint8_t outbuffer[UBI512_BLOCKSIZE_B];
64         ubi512_init(&(ctx->ubictx), ctx->ubictx.g, UBI_TYPE_OUT);
65         
66         outsize_b = ctx->outsize_b;
67         while(1){
68                 memcpy(&uctx, &(ctx->ubictx), sizeof(ubi512_ctx_t));
69                 ubi512_lastBlock(&uctx, &counter, 64);
70                 ubi512_ctx2hash(outbuffer, &uctx);
71                 if(outsize_b<=UBI512_BLOCKSIZE){
72                         memcpy(dest, outbuffer, (ctx->outsize_b+7)/8);
73                         break;
74                 }else{
75                         memcpy(dest, outbuffer, UBI512_BLOCKSIZE_B);
76                         dest = (uint8_t*)dest + UBI512_BLOCKSIZE_B;
77                         outsize_b -= UBI512_BLOCKSIZE;
78                         counter++;
79                 }
80         }
81 }
82
83 void skein512(void *dest, uint16_t outlength_b,const void *msg, uint32_t length_b){
84         skein512_ctx_t ctx;
85         skein512_init(&ctx, outlength_b);
86         while(length_b>SKEIN512_BLOCKSIZE){
87                 skein512_nextBlock(&ctx, msg);
88                 msg = (uint8_t*)msg + SKEIN512_BLOCKSIZE_B;
89                 length_b -= SKEIN512_BLOCKSIZE;
90         }
91         skein512_lastBlock(&ctx, msg, length_b);
92         skein512_ctx2hash(dest, &ctx);
93 }
94