]> git.cryptolib.org Git - avr-crypto-lib.git/blob - sha256/sha256.h
small optimizations to sha2 / sha256
[avr-crypto-lib.git] / sha256 / sha256.h
1 /* sha256.h */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2008  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.h
21  * \author  Daniel Otte 
22  * \date    2006-05-16
23  * \license     GPLv3 or later
24  * 
25  */
26
27 #ifndef SHA256_H_
28 #define SHA256_H_
29
30 #define __LITTLE_ENDIAN__
31
32
33 #include <stdint.h>
34
35 /** \def SHA256_HASH_BITS
36  * defines the size of a SHA-256 hash value in bits
37  */
38
39 /** \def SHA256_HASH_BYTES
40  * defines the size of a SHA-256 hash value in bytes
41  */
42
43 /** \def SHA256_BLOCK_BITS
44  * defines the size of a SHA-256 input block in bits
45  */
46
47 /** \def SHA256_BLOCK_BYTES
48  * defines the size of a SHA-256 input block in bytes
49  */
50
51 #define SHA256_HASH_BITS  256
52 #define SHA256_HASH_BYTES (SHA256_HASH_BITS / 8)
53 #define SHA256_BLOCK_BITS 512
54 #define SHA256_BLOCK_BYTES (SHA256_BLOCK_BITS / 8)
55
56 /** \typedef sha256_ctx_t
57  * \brief SHA-256 context type
58  * 
59  * A variable of this type may hold the state of a SHA-256 hashing process
60  */
61 typedef struct {
62         uint32_t h[8];
63         uint32_t length;
64 } sha256_ctx_t;
65
66 /** \typedef sha256_hash_t
67  * \brief SHA-256 hash value type
68  * 
69  * A variable of this type may hold the hash value produced by the
70  * sha256_ctx2hash(sha256_hash_t *dest, const sha256_ctx_t *state) function.
71  */
72 typedef uint8_t sha256_hash_t[SHA256_HASH_BYTES];
73
74 /** \fn void sha256_init(sha256_ctx_t *state)
75  * \brief initialise a SHA-256 context
76  * 
77  * This function sets a ::sha256_ctx_t to the initial values for hashing.
78  * \param state pointer to the SHA-256 hashing context
79  */
80 void sha256_init(sha256_ctx_t *state);
81
82 /** \fn void sha256_nextBlock (sha256_ctx_t *state, const void *block)
83  * \brief update the context with a given block
84  * 
85  * This function updates the SHA-256 hash context by processing the given block
86  * of fixed length.
87  * \param state pointer to the SHA-256 hash context
88  * \param block pointer to the block of fixed length (512 bit = 64 byte)
89  */
90 void sha256_nextBlock (sha256_ctx_t *state, const void *block);
91
92 /** \fn void sha256_lastBlock(sha256_ctx_t *state, const void *block, uint16_t length_b)
93  * \brief finalize the context with the given block 
94  * 
95  * This function finalizes the SHA-256 hash context by processing the given block
96  * of variable length.
97  * \param state pointer to the SHA-256 hash context
98  * \param block pointer to the block of fixed length (512 bit = 64 byte)
99  * \param length_b the length of the block in bits
100  */
101 void sha256_lastBlock(sha256_ctx_t *state, const void *block, uint16_t length_b);
102
103 /** \fn void sha256_ctx2hash(sha256_hash_t *dest, const sha256_ctx_t *state)
104  * \brief convert the hash state into the hash value
105  * This function reads the context and writes the hash value to the destination
106  * \param dest pointer to the location where the hash value should be written
107  * \param state pointer to the SHA-256 hash context
108  */
109 void sha256_ctx2hash(sha256_hash_t *dest, const sha256_ctx_t *state);
110
111 /** \fn void sha256(sha256_hash_t *dest, const void *msg, uint32_t length_b)
112  * \brief simple SHA-256 hashing function for direct hashing
113  * 
114  * This function automaticaly hashes a given message of arbitary length with
115  * the SHA-256 hashing algorithm.
116  * \param dest pointer to the location where the hash value is going to be written to
117  * \param msg pointer to the message thats going to be hashed
118  * \param length_b length of the message in bits
119  */
120 void sha256(sha256_hash_t *dest, const void *msg, uint32_t length_b);
121
122 #endif /*SHA256_H_*/