3 @section Hash functions
4 A hash function is an algorithm to map an arbitrary long message (in the form
5 of a bit string) to a fixed length message digest or hash value.
6 The hash function aims to be collision free, which means that it is not
7 practicable to find two messages with the same hash value (although this
8 collision must exist). Also it should not be practicable to construct a
9 message which maps to a given hash value.
11 @subsection List of available hash functions
12 The following hash functions are currently implemented:
15 @item BlueMidnightWish
27 @subsection High frequent parameters:
32 128 bits, 160 bits, 224 bits, 256 bits, 384 bits, 512 bits
36 @subsection Parts of a hash function
38 @item initialization function
39 @item compression algorithm
40 @item finalization function
43 @subsection hash function API
44 The API is not always consistent due to the fact that we tried to optimize the
45 code for size (flash, heap and stack) and speed (runtime of the different
47 Generally the API of the implemented block ciphers consists of:
50 function, which implements the initialisation of the context
52 function, which implements the compression algorithm
54 function, which implements the the padding algorithm
56 function, which turns a context into an actual hash value
58 context type, which can contains the state of a hashing process
61 @subsubsection @code{*_init} function
62 The @code{*_init} function generally takes a pointer to the context as parameter.
63 This function initializes the context with algorithm specific values.
65 @subsubsection @code{*_nextBlock} function
66 The @code{*_nextBlock} function is the core of each hash function. It updates the hash
67 state with a given message block. So this function uses a context pointer and
68 a message pointer as parameters. The size of a message block is fixed for each
69 hash function (mostly 512 bit). For the last block of a messages which may be
70 smaller than the blocksize you have to use the @code{*_lastBlock} function described
73 @subsubsection @code{*_lastBlock} function
74 The @code{*_lastBlock} function finalizes the context with the last bits of a
75 message. Since the last block is not required to have the blocksize you have
76 to specify the length of the last block (normally in bits). This function
77 performs the padding and final processing.
79 @subsubsection @code{*_ctx2hash} function
80 The @code{*_ctx2hash} function turns a given hash context into an actual hash value.
81 If multiple sized hash value may be created from a context it is necessary to
82 give the the size of the hash value as parameter.
85 @subsection Hash function abstraction layer (HFAL)
86 The HashFunctionAbstractionLayer (BCAL) is an abstraction layer which allows
87 usage of all implemented hash functions in a simple way. It abstracts specific
88 function details and is suitable for implementations which want to be flexible
89 in the choosing of specific hash functions. Another important aspect is that this
90 abstraction layer enables the implementation of hash function operating modes
91 independently from concrete hash function. It is very simple to use and reassembles
92 the API used to implement individual hash functions.
94 The main component is a hash function descriptor which contains the details of
95 the individual hash functions.
97 @subsection Parts of HFAL
98 The HFAL is split up in different parts:
100 @item HFAL declaration for HFAL decriptors
101 @item algorithm specific definitions of HFAL decriptors
102 @item HFAL basic context type
103 @item HFAL basic functions
106 @subsection HFAL declaration for HFAL decriptors
107 The HFAL descriptor is a structure which is usually placed in FLASH or ROM since
108 modification is unnecessary. It contains all information required to use the
109 according hash function.
113 uint8_t type; /* 2 == hashfunction */
117 uint16_t blocksize_b;
120 hf_nextBlock_fpt nextBlock;
121 hf_lastBlock_fpt lastBlock;
122 hf_ctx2hash_fpt ctx2hash;
125 } hfdesc_t; /* hashfunction descriptor type */
130 should be set to @samp{2} to indicate that this descriptor is for a
134 currently unused, should be set to zero.
137 is a pointer to a zero terminated ASCII string giving the name of the
138 implemented primitive. On targets with Harvard-architecture the string resides
139 in code memory (FLASH, ROM, ...).
142 is the number of bytes which should be allocated for the context variable.
145 is the number of bits on which are hashed by one iteration of the nextBlock
149 is the number of bits on which are outputed as final hash value.
152 is a pointer to the init function.
155 is a pointer to the algorithm specific nextBlock function.
158 is a pointer to the algorithm specific lastBlock function.
161 is a pointer to the algorithm specific ctx2hash function.
164 is a pointer to the free function or NULL if there is no free function.
167 is a pointer to the algorithm specific mem function. This function hashes
168 a complete message which has to reside entirely in RAM. This value may be
169 NULL if there is no such function.
172 @subsection HFAL-Basic context
173 Besides the context types for individual hash functions there is a generic context
174 type for HFAL. This is the context to use when using HFAL based functions.
175 The HFAL context has the following structure:
184 a pointer to the HFAL descriptor
186 pointer to the hash function specific context
189 @subsection HFAL-Basic
190 HFAL-Basic provides the basic features of an hash function on top of the
191 HFAL. To use it you simply have to include the algorithms you want to use,
192 the HFAL descriptor file and of course the HFAL-Basic implementation.
194 The following functions are provided:
196 @subsubsection @code{hfal_hash_init}
197 @code{uint8_t hfal_hash_init(const hfdesc_t* hash_descriptor, hfgen_ctx_t* ctx)}
198 this function initializes a HFAL context based on the given HFAL descriptor
199 pointer (first parameter). The context to initialize is designated by the
200 pointer passed as second parameter.
202 If everything works fine @samp{0} is returned. In the case something fails
203 the following codes are returned:
206 It was not possible to allocate enough memory to hold the context variable
207 for the selected hash function.
210 @subsubsection @code{hfal_hash_nextBlock}
211 @code{ void hfal_hash_nextBlock(hfgen_ctx_t* ctx, const void* block)}
212 this function hashes a block of memory (of algorithm specific length) and
213 updates the context accordingly.
215 @subsubsection @code{hfal_hash_lastBlock}
216 @code{ void hfal_hash_lastBlock(hfgen_ctx_t* ctx, const void* block, uint16_t length_b)}
217 this function is used to hash the last block of a message. Since messages are
218 not required to consist of full blocks (or even full bytes) the length of the
219 block must be given in bits. The context is updated accordingly. This function
220 already performs padding and related stuff.
222 @subsubsection @code{hfal_hash_ctx2hash}
223 @code{ void hfal_hash_ctx2hash(void* dest, hfgen_ctx_t* ctx)}
224 this function converts a context into an actual hash value which is stored in
225 @code{dest}. The application is responsible for allocating enough room.
227 @subsubsection @code{hfal_hash_free}
228 @code{ void hfal_hash_free(hfgen_ctx_t* ctx)}
229 this function differs from the individual hash functions @code{free} function
230 in that it is allways provided and must be called to avoid memory holes.
231 This function also automatically calls the implementation specific @code{free}
232 function if one is provided.
234 @subsubsection @code{hfal_hash_mem}
235 @code{ void hfal_hash_mem(const hfdesc_t* hash_descriptor, void* dest, const void* msg, uint32_t length_b)}
236 this function is allways provided (even if the actual algorithm does not
237 specify a @code{mem} function. It hashes an entire message which resides in
238 RAM and stores the hash value in @code{dest}. @code{msg} is the pointer to the
239 message and @code{length_b} is the message length in bits.
241 @subsubsection @code{hfal_hash_getBlocksize}
242 @code{ uint16_t hfal_hash_getBlocksize(const hfdesc_t* hash_descriptor)}
243 returns the blocksize of the described (@code{hash_descriptor}) hash function.
245 @subsubsection @code{hfal_hash_getHashsize}
246 @code{ uint16_t hfal_hash_getHashsize(const hfdesc_t* hash_descriptor)}
247 returns the hash value size of the described (@code{hash_descriptor}) hash function.
249 @subsubsection @code{hfal_hash_getCtxsize}
250 @code{ uint16_t hfal_hash_getCtxsize_B(const hfdesc_t* hash_descriptor)}
251 returns the size of a context variable of the described (@code{hash_descriptor}) hash function.