]> git.cryptolib.org Git - avr-crypto-lib.git/blob - sha2/sha224.c
fixing E-Mail-Address & Copyright
[avr-crypto-lib.git] / sha2 / sha224.c
1 /* sha224.c */
2 /*
3     This file is part of the ARM-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  * \file                sha224.c
21  * \author              Daniel Otte
22  * \date                16.05.2006
23  *
24  * \par License:
25  *      GPL
26  *
27  * \brief SHA-224 implementation.
28  *
29  *
30  */
31
32 #include <stdint.h>
33 #include <string.h> /* for memcpy, memmove, memset */
34 #include <avr/pgmspace.h>
35 #include "sha2_small_common.h"
36 #include "sha224.h"
37
38 #define LITTLE_ENDIAN
39
40 #if defined LITTLE_ENDIAN
41 #elif defined BIG_ENDIAN
42 #else
43         #error specify endianess!!!
44 #endif
45
46
47 /*************************************************************************/
48
49 const
50 uint32_t sha224_init_vector[] PROGMEM = {
51         0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939,
52         0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4
53 };
54
55
56 /*************************************************************************/
57
58 /**
59  * \brief \c sh224_init initialises a sha224 context for hashing.
60  * \c sh224_init c initialises the given sha224 context for hashing
61  * @param state pointer to a sha224 context
62  * @return none
63  */
64 void sha224_init(sha224_ctx_t *state){
65         state->length=0;
66         memcpy_P(state->h, sha224_init_vector, 8*4);
67 }
68
69 /*************************************************************************/
70 void sha224_nextBlock (sha224_ctx_t *state, const void *block){
71         sha2_small_common_nextBlock(state, block);
72 }
73
74 /*************************************************************************/
75 void sha224_lastBlock (sha224_ctx_t *state, const void *block, uint16_t length_b){
76         sha2_small_common_lastBlock(state, block, length_b);
77 }
78 /*************************************************************************/
79
80 /**
81  * \brief function to process the last block being hashed
82  * @param state Pointer to the context in which this block should be processed.
83  * @param block Pointer to the message wich should be hashed.
84  * @param length is the length of only THIS block in BITS not in bytes!
85  *  bits are big endian, meaning high bits come first.
86  *      if you have a message with bits at the end, the byte must be padded with zeros
87  */
88
89 /*************************************************************************/
90
91 /*
92  * length in bits!
93  */
94 void sha224(void *dest, const void *msg, uint32_t length_b){ /* length could be choosen longer but this is for µC */
95         sha224_ctx_t s;
96         sha224_init(&s);
97         while(length_b >= SHA224_BLOCK_BITS){
98                 sha224_nextBlock(&s, msg);
99                 msg = (uint8_t*)msg + SHA224_BLOCK_BITS/8;
100                 length_b -= SHA224_BLOCK_BITS;
101         }
102         sha224_lastBlock(&s, msg, length_b);
103         sha224_ctx2hash(dest,&s);
104 }
105
106
107
108 /*************************************************************************/
109
110 void sha224_ctx2hash(void *dest, const sha224_ctx_t *state){
111 #if defined LITTLE_ENDIAN
112         uint8_t i, j, *s=(uint8_t*)(state->h);
113         i=7;
114         do{
115                 j=3;
116                 do{
117                         *((uint8_t*)dest) = s[j];
118                         dest = (uint8_t*)dest + 1;
119                 }while(j--);
120                 s += 4;
121         }while(--i);
122 #elif BIG_ENDIAN
123         memcpy(dest, state->h, 28);
124 #else
125 # error unsupported endian type!
126 #endif
127 }
128
129