1  /**
2  * \file md.h
3  *
4  * \brief This file contains the generic message-digest wrapper.
5  *
6  * \author Adriaan de Jong <dejong@fox-it.com>
7  */
8 /*
9  *  Copyright The Mbed TLS Contributors
10  *  SPDX-License-Identifier: Apache-2.0
11  *
12  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
13  *  not use this file except in compliance with the License.
14  *  You may obtain a copy of the License at
15  *
16  *  http://www.apache.org/licenses/LICENSE-2.0
17  *
18  *  Unless required by applicable law or agreed to in writing, software
19  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
20  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  *  See the License for the specific language governing permissions and
22  *  limitations under the License.
23  */
24 
25 #ifndef MBEDTLS_MD_H
26 #define MBEDTLS_MD_H
27 #include "mbedtls/private_access.h"
28 
29 #include <stddef.h>
30 
31 #include "mbedtls/build_info.h"
32 
33 #define MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE                -0x5080  /**< The selected feature is not available. */
34 #define MBEDTLS_ERR_MD_BAD_INPUT_DATA                     -0x5100  /**< Bad input parameters to function. */
35 #define MBEDTLS_ERR_MD_ALLOC_FAILED                       -0x5180  /**< Failed to allocate memory. */
36 #define MBEDTLS_ERR_MD_FILE_IO_ERROR                      -0x5200  /**< Opening or reading of file failed. */
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 /**
43  * \brief     Supported message digests.
44  *
45  * \warning   MD5 and SHA-1 are considered weak message digests and
46  *            their use constitutes a security risk. We recommend considering
47  *            stronger message digests instead.
48  *
49  */
50 typedef enum {
51     MBEDTLS_MD_NONE=0,    /**< None. */
52     MBEDTLS_MD_MD5,       /**< The MD5 message digest. */
53     MBEDTLS_MD_SHA1,      /**< The SHA-1 message digest. */
54     MBEDTLS_MD_SHA224,    /**< The SHA-224 message digest. */
55     MBEDTLS_MD_SHA256,    /**< The SHA-256 message digest. */
56     MBEDTLS_MD_SHA384,    /**< The SHA-384 message digest. */
57     MBEDTLS_MD_SHA512,    /**< The SHA-512 message digest. */
58     MBEDTLS_MD_RIPEMD160, /**< The RIPEMD-160 message digest. */
59 } mbedtls_md_type_t;
60 
61 #if defined(MBEDTLS_SHA512_C)
62 #define MBEDTLS_MD_MAX_SIZE         64  /* longest known is SHA512 */
63 #else
64 #define MBEDTLS_MD_MAX_SIZE         32  /* longest known is SHA256 or less */
65 #endif
66 
67 #if defined(MBEDTLS_SHA512_C)
68 #define MBEDTLS_MD_MAX_BLOCK_SIZE         128
69 #else
70 #define MBEDTLS_MD_MAX_BLOCK_SIZE         64
71 #endif
72 
73 /**
74  * Opaque struct.
75  *
76  * Constructed using either #mbedtls_md_info_from_string or
77  * #mbedtls_md_info_from_type.
78  *
79  * Fields can be accessed with #mbedtls_md_get_size,
80  * #mbedtls_md_get_type and #mbedtls_md_get_name.
81  */
82 /* Defined internally in library/md_wrap.h. */
83 typedef struct mbedtls_md_info_t mbedtls_md_info_t;
84 
85 /**
86  * The generic message-digest context.
87  */
88 typedef struct mbedtls_md_context_t
89 {
90     /** Information about the associated message digest. */
91     const mbedtls_md_info_t *MBEDTLS_PRIVATE(md_info);
92 
93     /** The digest-specific context. */
94     void *MBEDTLS_PRIVATE(md_ctx);
95 
96     /** The HMAC part of the context. */
97     void *MBEDTLS_PRIVATE(hmac_ctx);
98 } mbedtls_md_context_t;
99 
100 /**
101  * \brief           This function returns the list of digests supported by the
102  *                  generic digest module.
103  *
104  * \note            The list starts with the strongest available hashes.
105  *
106  * \return          A statically allocated array of digests. Each element
107  *                  in the returned list is an integer belonging to the
108  *                  message-digest enumeration #mbedtls_md_type_t.
109  *                  The last entry is 0.
110  */
111 const int *mbedtls_md_list( void );
112 
113 /**
114  * \brief           This function returns the message-digest information
115  *                  associated with the given digest name.
116  *
117  * \param md_name   The name of the digest to search for.
118  *
119  * \return          The message-digest information associated with \p md_name.
120  * \return          NULL if the associated message-digest information is not found.
121  */
122 const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name );
123 
124 /**
125  * \brief           This function returns the message-digest information
126  *                  associated with the given digest type.
127  *
128  * \param md_type   The type of digest to search for.
129  *
130  * \return          The message-digest information associated with \p md_type.
131  * \return          NULL if the associated message-digest information is not found.
132  */
133 const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type );
134 
135 /**
136  * \brief           This function initializes a message-digest context without
137  *                  binding it to a particular message-digest algorithm.
138  *
139  *                  This function should always be called first. It prepares the
140  *                  context for mbedtls_md_setup() for binding it to a
141  *                  message-digest algorithm.
142  */
143 void mbedtls_md_init( mbedtls_md_context_t *ctx );
144 
145 /**
146  * \brief           This function clears the internal structure of \p ctx and
147  *                  frees any embedded internal structure, but does not free
148  *                  \p ctx itself.
149  *
150  *                  If you have called mbedtls_md_setup() on \p ctx, you must
151  *                  call mbedtls_md_free() when you are no longer using the
152  *                  context.
153  *                  Calling this function if you have previously
154  *                  called mbedtls_md_init() and nothing else is optional.
155  *                  You must not call this function if you have not called
156  *                  mbedtls_md_init().
157  */
158 void mbedtls_md_free( mbedtls_md_context_t *ctx );
159 
160 
161 /**
162  * \brief           This function selects the message digest algorithm to use,
163  *                  and allocates internal structures.
164  *
165  *                  It should be called after mbedtls_md_init() or
166  *                  mbedtls_md_free(). Makes it necessary to call
167  *                  mbedtls_md_free() later.
168  *
169  * \param ctx       The context to set up.
170  * \param md_info   The information structure of the message-digest algorithm
171  *                  to use.
172  * \param hmac      Defines if HMAC is used. 0: HMAC is not used (saves some memory),
173  *                  or non-zero: HMAC is used with this context.
174  *
175  * \return          \c 0 on success.
176  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
177  *                  failure.
178  * \return          #MBEDTLS_ERR_MD_ALLOC_FAILED on memory-allocation failure.
179  */
180 int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac );
181 
182 /**
183  * \brief           This function clones the state of an message-digest
184  *                  context.
185  *
186  * \note            You must call mbedtls_md_setup() on \c dst before calling
187  *                  this function.
188  *
189  * \note            The two contexts must have the same type,
190  *                  for example, both are SHA-256.
191  *
192  * \warning         This function clones the message-digest state, not the
193  *                  HMAC state.
194  *
195  * \param dst       The destination context.
196  * \param src       The context to be cloned.
197  *
198  * \return          \c 0 on success.
199  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification failure.
200  */
201 int mbedtls_md_clone( mbedtls_md_context_t *dst,
202                       const mbedtls_md_context_t *src );
203 
204 /**
205  * \brief           This function extracts the message-digest size from the
206  *                  message-digest information structure.
207  *
208  * \param md_info   The information structure of the message-digest algorithm
209  *                  to use.
210  *
211  * \return          The size of the message-digest output in Bytes.
212  */
213 unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info );
214 
215 /**
216  * \brief           This function extracts the message-digest type from the
217  *                  message-digest information structure.
218  *
219  * \param md_info   The information structure of the message-digest algorithm
220  *                  to use.
221  *
222  * \return          The type of the message digest.
223  */
224 mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info );
225 
226 /**
227  * \brief           This function extracts the message-digest name from the
228  *                  message-digest information structure.
229  *
230  * \param md_info   The information structure of the message-digest algorithm
231  *                  to use.
232  *
233  * \return          The name of the message digest.
234  */
235 const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info );
236 
237 /**
238  * \brief           This function starts a message-digest computation.
239  *
240  *                  You must call this function after setting up the context
241  *                  with mbedtls_md_setup(), and before passing data with
242  *                  mbedtls_md_update().
243  *
244  * \param ctx       The generic message-digest context.
245  *
246  * \return          \c 0 on success.
247  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
248  *                  failure.
249  */
250 int mbedtls_md_starts( mbedtls_md_context_t *ctx );
251 
252 /**
253  * \brief           This function feeds an input buffer into an ongoing
254  *                  message-digest computation.
255  *
256  *                  You must call mbedtls_md_starts() before calling this
257  *                  function. You may call this function multiple times.
258  *                  Afterwards, call mbedtls_md_finish().
259  *
260  * \param ctx       The generic message-digest context.
261  * \param input     The buffer holding the input data.
262  * \param ilen      The length of the input data.
263  *
264  * \return          \c 0 on success.
265  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
266  *                  failure.
267  */
268 int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen );
269 
270 /**
271  * \brief           This function finishes the digest operation,
272  *                  and writes the result to the output buffer.
273  *
274  *                  Call this function after a call to mbedtls_md_starts(),
275  *                  followed by any number of calls to mbedtls_md_update().
276  *                  Afterwards, you may either clear the context with
277  *                  mbedtls_md_free(), or call mbedtls_md_starts() to reuse
278  *                  the context for another digest operation with the same
279  *                  algorithm.
280  *
281  * \param ctx       The generic message-digest context.
282  * \param output    The buffer for the generic message-digest checksum result.
283  *
284  * \return          \c 0 on success.
285  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
286  *                  failure.
287  */
288 int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output );
289 
290 /**
291  * \brief          This function calculates the message-digest of a buffer,
292  *                 with respect to a configurable message-digest algorithm
293  *                 in a single call.
294  *
295  *                 The result is calculated as
296  *                 Output = message_digest(input buffer).
297  *
298  * \param md_info  The information structure of the message-digest algorithm
299  *                 to use.
300  * \param input    The buffer holding the data.
301  * \param ilen     The length of the input data.
302  * \param output   The generic message-digest checksum result.
303  *
304  * \return         \c 0 on success.
305  * \return         #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
306  *                 failure.
307  */
308 int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
309         unsigned char *output );
310 
311 #if defined(MBEDTLS_FS_IO)
312 /**
313  * \brief          This function calculates the message-digest checksum
314  *                 result of the contents of the provided file.
315  *
316  *                 The result is calculated as
317  *                 Output = message_digest(file contents).
318  *
319  * \param md_info  The information structure of the message-digest algorithm
320  *                 to use.
321  * \param path     The input file name.
322  * \param output   The generic message-digest checksum result.
323  *
324  * \return         \c 0 on success.
325  * \return         #MBEDTLS_ERR_MD_FILE_IO_ERROR on an I/O error accessing
326  *                 the file pointed by \p path.
327  * \return         #MBEDTLS_ERR_MD_BAD_INPUT_DATA if \p md_info was NULL.
328  */
329 int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path,
330                      unsigned char *output );
331 #endif /* MBEDTLS_FS_IO */
332 
333 /**
334  * \brief           This function sets the HMAC key and prepares to
335  *                  authenticate a new message.
336  *
337  *                  Call this function after mbedtls_md_setup(), to use
338  *                  the MD context for an HMAC calculation, then call
339  *                  mbedtls_md_hmac_update() to provide the input data, and
340  *                  mbedtls_md_hmac_finish() to get the HMAC value.
341  *
342  * \param ctx       The message digest context containing an embedded HMAC
343  *                  context.
344  * \param key       The HMAC secret key.
345  * \param keylen    The length of the HMAC key in Bytes.
346  *
347  * \return          \c 0 on success.
348  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
349  *                  failure.
350  */
351 int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key,
352                     size_t keylen );
353 
354 /**
355  * \brief           This function feeds an input buffer into an ongoing HMAC
356  *                  computation.
357  *
358  *                  Call mbedtls_md_hmac_starts() or mbedtls_md_hmac_reset()
359  *                  before calling this function.
360  *                  You may call this function multiple times to pass the
361  *                  input piecewise.
362  *                  Afterwards, call mbedtls_md_hmac_finish().
363  *
364  * \param ctx       The message digest context containing an embedded HMAC
365  *                  context.
366  * \param input     The buffer holding the input data.
367  * \param ilen      The length of the input data.
368  *
369  * \return          \c 0 on success.
370  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
371  *                  failure.
372  */
373 int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input,
374                     size_t ilen );
375 
376 /**
377  * \brief           This function finishes the HMAC operation, and writes
378  *                  the result to the output buffer.
379  *
380  *                  Call this function after mbedtls_md_hmac_starts() and
381  *                  mbedtls_md_hmac_update() to get the HMAC value. Afterwards
382  *                  you may either call mbedtls_md_free() to clear the context,
383  *                  or call mbedtls_md_hmac_reset() to reuse the context with
384  *                  the same HMAC key.
385  *
386  * \param ctx       The message digest context containing an embedded HMAC
387  *                  context.
388  * \param output    The generic HMAC checksum result.
389  *
390  * \return          \c 0 on success.
391  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
392  *                  failure.
393  */
394 int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output);
395 
396 /**
397  * \brief           This function prepares to authenticate a new message with
398  *                  the same key as the previous HMAC operation.
399  *
400  *                  You may call this function after mbedtls_md_hmac_finish().
401  *                  Afterwards call mbedtls_md_hmac_update() to pass the new
402  *                  input.
403  *
404  * \param ctx       The message digest context containing an embedded HMAC
405  *                  context.
406  *
407  * \return          \c 0 on success.
408  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
409  *                  failure.
410  */
411 int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx );
412 
413 /**
414  * \brief          This function calculates the full generic HMAC
415  *                 on the input buffer with the provided key.
416  *
417  *                 The function allocates the context, performs the
418  *                 calculation, and frees the context.
419  *
420  *                 The HMAC result is calculated as
421  *                 output = generic HMAC(hmac key, input buffer).
422  *
423  * \param md_info  The information structure of the message-digest algorithm
424  *                 to use.
425  * \param key      The HMAC secret key.
426  * \param keylen   The length of the HMAC secret key in Bytes.
427  * \param input    The buffer holding the input data.
428  * \param ilen     The length of the input data.
429  * \param output   The generic HMAC result.
430  *
431  * \return         \c 0 on success.
432  * \return         #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
433  *                 failure.
434  */
435 int mbedtls_md_hmac( const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen,
436                 const unsigned char *input, size_t ilen,
437                 unsigned char *output );
438 
439 /* Internal use */
440 int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data );
441 
442 #ifdef __cplusplus
443 }
444 #endif
445 
446 #endif /* MBEDTLS_MD_H */
447