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