]> git.cryptolib.org Git - avr-crypto-lib.git/blob - skein/skein256.c
fixing E-Mail-Address & Copyright
[avr-crypto-lib.git] / skein / skein256.c
1 /* skein256.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 #include "cli.h"
33
34 void skein256_init(skein256_ctx_t *ctx, uint16_t outsize_b){
35         skein_config_t conf;
36         uint8_t null[UBI256_BLOCKSIZE_B];
37         memset(null, 0, UBI256_BLOCKSIZE_B);
38         memset(&conf, 0, sizeof(skein_config_t));
39         conf.schema[0] = 'S';
40         conf.schema[1] = 'H';
41         conf.schema[2] = 'A';
42         conf.schema[3] = '3';
43         conf.version = 1;
44         conf.out_length = outsize_b;
45         ctx->outsize_b = outsize_b;
46         ubi256_init(&(ctx->ubictx), null, UBI_TYPE_CFG);
47         ubi256_lastBlock(&(ctx->ubictx), &conf, 256);
48         ubi256_init(&(ctx->ubictx), ctx->ubictx.g, UBI_TYPE_MSG);
49 }
50
51 void skein256_nextBlock(skein256_ctx_t *ctx, const void *block){
52         ubi256_nextBlock(&(ctx->ubictx), block);
53 }
54
55 void skein256_lastBlock(skein256_ctx_t *ctx, const void *block, uint16_t length_b){
56         ubi256_lastBlock(&(ctx->ubictx), block, length_b);
57 }
58
59 void skein256_ctx2hash(void *dest, skein256_ctx_t *ctx){
60         ubi256_ctx_t uctx;
61         uint16_t outsize_b;
62         
63         uint64_t counter=0;
64         uint8_t outbuffer[UBI256_BLOCKSIZE_B];
65         ubi256_init(&(ctx->ubictx), ctx->ubictx.g, UBI_TYPE_OUT);
66         
67         outsize_b = ctx->outsize_b;
68         while(1){
69                 memcpy(&uctx, &(ctx->ubictx), sizeof(ubi256_ctx_t));
70                 ubi256_lastBlock(&uctx, &counter, 64);
71                 ubi256_ctx2hash(outbuffer, &uctx);
72                 if(outsize_b<=UBI256_BLOCKSIZE){
73                         memcpy(dest, outbuffer, (outsize_b+7)/8);
74                         break;
75                 }else{
76                         memcpy(dest, outbuffer, UBI256_BLOCKSIZE_B);
77                         dest = (uint8_t*)dest + UBI256_BLOCKSIZE_B;
78                         outsize_b -= UBI256_BLOCKSIZE;
79                         counter++;
80                 }
81         }
82 }
83
84 void skein256(void *dest, uint16_t outlength_b,const void *msg, uint32_t length_b){
85         skein256_ctx_t ctx;
86         skein256_init(&ctx, outlength_b);
87         while(length_b>SKEIN256_BLOCKSIZE){
88                 skein256_nextBlock(&ctx, msg);
89                 msg = (uint8_t*)msg + SKEIN256_BLOCKSIZE_B;
90                 length_b -= SKEIN256_BLOCKSIZE;
91         }
92         skein256_lastBlock(&ctx, msg, length_b);
93         skein256_ctx2hash(dest, &ctx);
94 }