]> git.cryptolib.org Git - avr-crypto-lib.git/blob - hfal-basic.c
new hash function abstraction layer + shavs + dump util + ...
[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 size){
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, size);
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         uint16_t bs,bsb;
68         uint8_t ctx[pgm_read_word(&(hash_descriptor->ctxsize_B))];
69         f=(void_fpt)pgm_read_word(&(hash_descriptor->init));
70         ((hf_init_fpt)f)(ctx);
71         bs=pgm_read_word(&(hash_descriptor->blocksize_b));
72         bsb=bs/8;
73         f=(void_fpt)pgm_read_word(&(hash_descriptor->nextBlock));
74         while(length_b>=bs){
75                 ((hf_nextBlock_fpt)f)(ctx, msg);
76                 length_b -= bs;
77                 msg = (uint8_t*)msg + bsb;
78         }
79         f=(void_fpt)pgm_read_word(&(hash_descriptor->lastBlock));
80         ((hf_lastBlock_fpt)f)(ctx, msg, length_b);
81         f=(void_fpt)pgm_read_word(&(hash_descriptor->ctx2hash));
82         ((hf_ctx2hash_fpt)f)(dest, ctx);
83