]> git.cryptolib.org Git - avr-crypto-lib.git/blob - hfal/hfal-basic.c
5741df19f95530e0e73409b2c983a4cfb75e2060
[avr-crypto-lib.git] / hfal / 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         uint16_t tmp;
28         ctx->desc_ptr = (hfdesc_t*)hash_descriptor;
29         tmp = pgm_read_word(&(hash_descriptor->ctxsize_B));
30         if(!(ctx->ctx=malloc(tmp)))
31                 return 3;
32         f= (hf_init_fpt)pgm_read_word(&(hash_descriptor->init));
33         f(ctx->ctx);
34         return 0;
35 }
36
37 void hfal_hash_nextBlock(hfgen_ctx_t *ctx, const void *block){
38         hf_nextBlock_fpt f;
39         hfdesc_t *x=(ctx->desc_ptr);
40         f =(hf_nextBlock_fpt)pgm_read_word(&(x->nextBlock));
41         f(ctx->ctx, block);
42 }
43
44 void hfal_hash_lastBlock(hfgen_ctx_t *ctx, const void *block, uint16_t length_b){
45         hf_lastBlock_fpt f;
46         hfdesc_t *x=ctx->desc_ptr;
47         f =(hf_lastBlock_fpt)pgm_read_word(&(x->lastBlock));
48         f(ctx->ctx, block, length_b);
49 }
50
51 void hfal_hash_ctx2hash(void *dest, hfgen_ctx_t *ctx){
52         hf_ctx2hash_fpt f;
53         hfdesc_t *x=ctx->desc_ptr;
54         f =(hf_ctx2hash_fpt)pgm_read_word(&(x->ctx2hash));
55         f(dest, ctx->ctx);
56 }
57
58 void hfal_hash_free(hfgen_ctx_t *ctx){
59         hf_free_fpt f;
60         hfdesc_t *x=ctx->desc_ptr;
61         f =(hf_free_fpt)pgm_read_word(&(x->free));
62         if(f)
63                 f(ctx->ctx);
64         free(ctx->ctx);
65 }
66
67 void hfal_hash_mem(const hfdesc_t *hash_descriptor, void *dest, const void *msg, uint32_t length_b){
68         void_fpt f;
69         f = (void_fpt)pgm_read_word(&(hash_descriptor->mem));
70         if(f){
71                 ((hf_mem_fpt)f)(dest, msg, length_b);
72         }else{
73                 uint16_t bs,bsb;
74                 uint8_t ctx[pgm_read_word(&(hash_descriptor->ctxsize_B))];
75                 f=(void_fpt)(pgm_read_word(&(hash_descriptor->init)));
76                 ((hf_init_fpt)f)(ctx);
77                 bs=pgm_read_word(&(hash_descriptor->blocksize_b));
78                 bsb=bs/8;
79                 f=(void_fpt)(pgm_read_word(&(hash_descriptor->nextBlock)));
80                 while(length_b>bs){
81                         ((hf_nextBlock_fpt)f)(ctx, msg);
82                         length_b -= bs;
83                         msg = (uint8_t*)msg + bsb;
84                 }
85                 f=(void_fpt)(pgm_read_word(&(hash_descriptor->lastBlock)));
86                 ((hf_lastBlock_fpt)f)(ctx, msg, length_b);
87                 f=(void_fpt)(pgm_read_word(&(hash_descriptor->ctx2hash)));
88                 ((hf_ctx2hash_fpt)f)(dest, ctx);
89         }
90 }
91
92 uint16_t hfal_hash_getBlocksize(const hfdesc_t *hash_descriptor){
93         uint16_t ret;
94         ret = pgm_read_word(&(hash_descriptor->blocksize_b));
95         return ret;
96 }
97
98 uint16_t hfal_hash_getHashsize(const hfdesc_t *hash_descriptor){
99         uint16_t ret;
100         ret = pgm_read_word(&(hash_descriptor->hashsize_b));
101         return ret;
102 }
103
104 uint16_t hfal_hash_getCtxsize_B(const hfdesc_t *hash_descriptor){
105         uint16_t ret;
106         ret = pgm_read_word(&(hash_descriptor->ctxsize_B));
107         return ret;
108 }