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