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