3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
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.
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.
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/>.
23 * \license GPLv3 or later
30 #define __LITTLE_ENDIAN__
35 /** \def SHA256_HASH_BITS
36 * defines the size of a SHA-256 hash value in bits
39 /** \def SHA256_HASH_BYTES
40 * defines the size of a SHA-256 hash value in bytes
43 /** \def SHA256_BLOCK_BITS
44 * defines the size of a SHA-256 input block in bits
47 /** \def SHA256_BLOCK_BYTES
48 * defines the size of a SHA-256 input block in bytes
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)
56 /** \typedef sha256_ctx_t
57 * \brief SHA-256 context type
59 * A variable of this type may hold the state of a SHA-256 hashing process
66 /** \typedef sha256_hash_t
67 * \brief SHA-256 hash value type
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.
72 typedef uint8_t sha256_hash_t[SHA256_HASH_BYTES];
74 /** \fn void sha256_init(sha256_ctx_t *state)
75 * \brief initialise a SHA-256 context
77 * This function sets a ::sha256_ctx_t to the initial values for hashing.
78 * \param state pointer to the SHA-256 hashing context
80 void sha256_init(sha256_ctx_t *state);
82 /** \fn void sha256_nextBlock (sha256_ctx_t *state, const void *block)
83 * \brief update the context with a given block
85 * This function updates the SHA-256 hash context by processing the given block
87 * \param state pointer to the SHA-256 hash context
88 * \param block pointer to the block of fixed length (512 bit = 64 byte)
90 void sha256_nextBlock (sha256_ctx_t *state, const void *block);
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
95 * This function finalizes the SHA-256 hash context by processing the given block
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
101 void sha256_lastBlock(sha256_ctx_t *state, const void *block, uint16_t length_b);
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
109 void sha256_ctx2hash(sha256_hash_t *dest, const sha256_ctx_t *state);
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
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
120 void sha256(sha256_hash_t *dest, const void *msg, uint32_t length_b);