]> git.cryptolib.org Git - avr-crypto-lib.git/blob - jh/jh_simple_aux.c
fixing E-Mail-Address & Copyright
[avr-crypto-lib.git] / jh / jh_simple_aux.c
1 /* jh_simple_speed.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 #include <stdint.h>
21 #include <avr/pgmspace.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include "memxor.h"
25 #include "jh_simple.h"
26
27 #define DEBUG 0
28
29 #if DEBUG
30 #include "cli.h"
31 #endif
32
33 void jh_encrypt(uint8_t *a);
34
35 void jh_init(uint16_t hashlen_b, jh_ctx_t *ctx){
36         memset(ctx->a, 0, 128);
37         ctx->a[0] = hashlen_b>>8;
38         ctx->a[1] = hashlen_b&0xff;
39         jh_encrypt(ctx->a);
40         ctx->block_hashed=0;
41 }
42
43 void jh_nextBlock(jh_ctx_t *ctx, void *block){
44         memxor(ctx->a, block, 64);
45         jh_encrypt(ctx->a);
46         memxor(ctx->a+64, block, 64);
47         ctx->block_hashed++;
48 }
49
50 void jh_lastBlock(jh_ctx_t *ctx, void *block, uint16_t length_b){
51         while(length_b>=64*8){
52                 jh_nextBlock(ctx, block);
53                 block = (uint8_t*)block + 64;
54                 length_b -= 64*8;
55         }
56         uint8_t buffer[64];
57         uint64_t total_length;
58         memset(buffer, 0, 64);
59         memcpy(buffer, block, (length_b+7)/8);
60         buffer[length_b/8] |= 0x80>>(length_b%8);
61         total_length=ctx->block_hashed*512+length_b;
62         if(length_b==0){
63
64         }else{
65                 jh_nextBlock(ctx, buffer);
66                 buffer[0]=0;
67         }
68         memset(buffer+1, 0, 64-8-1);
69         buffer[63] = total_length&0xff;
70         buffer[62] = (total_length>> 8)&0xff;
71         buffer[61] = (total_length>>16)&0xff;
72         buffer[60] = (total_length>>24)&0xff;
73         buffer[59] = (total_length>>32)&0xff;
74         buffer[58] = (total_length>>40)&0xff;
75         buffer[57] = (total_length>>48)&0xff;
76         buffer[56] = (total_length>>56)&0xff;
77         jh_nextBlock(ctx, buffer);
78 }
79
80 void jh_ctx2hash(void *dest, uint16_t length_b, jh_ctx_t *ctx){
81         memcpy(dest, ctx->a+128-(length_b+7)/8, (length_b+7)/8);
82 }
83
84
85 void jh224_init(jh_ctx_t *ctx){
86         jh_init(224, ctx);
87 }
88
89 void jh224_ctx2hash(void *dest, jh_ctx_t *ctx){
90         jh_ctx2hash(dest, 224, ctx);
91 }
92
93 void jh256_init(jh_ctx_t *ctx){
94         jh_init(256, ctx);
95 }
96
97 void jh256_ctx2hash(void *dest, jh_ctx_t *ctx){
98         jh_ctx2hash(dest, 256, ctx);
99 }
100
101 void jh384_init(jh_ctx_t *ctx){
102         jh_init(384, ctx);
103 }
104
105 void jh384_ctx2hash(void *dest, jh_ctx_t *ctx){
106         jh_ctx2hash(dest, 384, ctx);
107 }
108
109 void jh512_init(jh_ctx_t *ctx){
110         jh_init(512, ctx);
111 }
112
113 void jh512_ctx2hash(void *dest, jh_ctx_t *ctx){
114         jh_ctx2hash(dest, 512, ctx);
115 }