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