]> git.cryptolib.org Git - avr-crypto-lib.git/blob - hfal-basic.c
bug fixed in md5-asm.S ( wrong values for length_b%512=505..511 )
[avr-crypto-lib.git] / hfal-basic.c
1 /* hfal-basic.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2009  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 <avr/pgmspace.h>
21 #include "hashfunction_descriptor.h"
22 #include "hfal-basic.h"
23 #include <stdlib.h>
24
25 uint8_t hfal_hash_init(const hfdesc_t* hash_descriptor, hfgen_ctx_t* ctx){
26         hf_init_fpt f;
27         ctx->desc_ptr = (hfdesc_t*)hash_descriptor;
28         if(!(ctx->ctx=malloc(pgm_read_word(&(hash_descriptor->ctxsize_B)))))
29                 return 3;
30         f= (hf_init_fpt)pgm_read_word(&(hash_descriptor->init));
31         f(ctx->ctx);
32         return 0;
33 }
34         
35 void hfal_hash_nextBlock(hfgen_ctx_t* ctx, const void* block){
36         hf_nextBlock_fpt f;
37         hfdesc_t* x=(ctx->desc_ptr);
38         f =(hf_nextBlock_fpt)pgm_read_word(&(x->nextBlock));
39         f(ctx->ctx, block);
40 }
41         
42 void hfal_hash_lastBlock(hfgen_ctx_t* ctx, const void* block, uint16_t length_b){
43         hf_lastBlock_fpt f;
44         hfdesc_t* x=ctx->desc_ptr;
45         f =(hf_lastBlock_fpt)pgm_read_word(&(x->lastBlock));
46         f(ctx->ctx, block, length_b);
47 }
48
49 void hfal_hash_ctx2hash(void* dest, hfgen_ctx_t* ctx){
50         hf_ctx2hash_fpt f;
51         hfdesc_t* x=ctx->desc_ptr;
52         f =(hf_ctx2hash_fpt)pgm_read_word(&(x->ctx2hash));
53         f(dest, ctx->ctx);
54 }
55
56 void hfal_hash_free(hfgen_ctx_t* ctx){
57         hf_free_fpt f;
58         hfdesc_t* x=ctx->desc_ptr;
59         f =(hf_free_fpt)pgm_read_word(&(x->free));
60         if(f)
61                 f(ctx->ctx);
62         free(ctx->ctx);
63 }
64
65 void hfal_hash_mem(const hfdesc_t* hash_descriptor, void* dest, const void* msg, uint32_t length_b){
66         void_fpt f;
67         f = (void_fpt)pgm_read_word(&(hash_descriptor->mem));
68         if(f){
69                 ((hf_mem_fpt)f)(dest, msg, length_b);
70         }else{
71                 
72                 uint16_t bs,bsb;
73                 uint8_t ctx[pgm_read_word(&(hash_descriptor->ctxsize_B))];
74                 f=(void_fpt)pgm_read_word(&(hash_descriptor->init));
75                 ((hf_init_fpt)f)(ctx);
76                 bs=pgm_read_word(&(hash_descriptor->blocksize_b));
77                 bsb=bs/8;
78                 f=(void_fpt)pgm_read_word(&(hash_descriptor->nextBlock));
79                 while(length_b>=bs){
80                         ((hf_nextBlock_fpt)f)(ctx, msg);
81                         length_b -= bs;
82                         msg = (uint8_t*)msg + bsb;
83                 }
84                 f=(void_fpt)pgm_read_word(&(hash_descriptor->lastBlock));
85                 ((hf_lastBlock_fpt)f)(ctx, msg, length_b);
86                 f=(void_fpt)pgm_read_word(&(hash_descriptor->ctx2hash));
87                 ((hf_ctx2hash_fpt)f)(dest, ctx);
88         }
89
90
91 uint16_t hfal_hash_getBlocksize(const hfdesc_t* hash_descriptor){
92         uint16_t ret;
93         ret = pgm_read_word(&(hash_descriptor->blocksize_b));
94         return ret;
95 }
96
97 uint16_t hfal_hash_getHashsize(const hfdesc_t* hash_descriptor){
98         uint16_t ret;
99         ret = pgm_read_word(&(hash_descriptor->hashsize_b));
100         return ret;
101 }