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 (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
10  *  Copyright (C) 2018-2022, Intel Corporation.
11  *  SPDX-License-Identifier: Apache-2.0
12  *
13  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
14  *  not use this file except in compliance with the License.
15  *  You may obtain a copy of the License at
16  *
17  *  http://www.apache.org/licenses/LICENSE-2.0
18  *
19  *  Unless required by applicable law or agreed to in writing, software
20  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22  *  See the License for the specific language governing permissions and
23  *  limitations under the License.
24  *
25  *  This file is part of Mbed TLS (https://tls.mbed.org)
26  */
27 
28 #ifndef MBEDTLS_MD_H
29 #define MBEDTLS_MD_H
30 
31 #include <rtl.h>
32 #include "sha256.h"
33 
34 #define MBEDTLS_ERR_MD_BAD_INPUT_DATA                     -0x5100  /**< Bad input parameters to function. */
35 
36 #define mbedtls_platform_zeroize(buf, len) memset((buf), 0U, (len))
37 
38 /**
39  * \brief     Supported message digests.
40  *
41  */
42 typedef enum {
43     MBEDTLS_MD_NONE = 0,    /**< None. */
44     MBEDTLS_MD_SHA256,    /**< The SHA-256 message digest. */
45 } mbedtls_md_type_t;
46 
47 #define MBEDTLS_MD_MAX_SIZE         32  /* longest known is SHA256 or less */
48 
49 /**
50  * Opaque struct defined in md_internal.h.
51  */
52 typedef struct mbedtls_md_info mbedtls_md_info_t;
53 
54 /**
55  * The generic message-digest context.
56  */
57 typedef struct {
58     /** Information about the associated message digest. */
59     const mbedtls_md_info_t *md_info;
60 
61     /** The digest-specific context. */
62     uint8_t md_ctx[sizeof(mbedtls_sha256_context)];
63 
64     /** The HMAC part of the context. Use array here to avoid dynamic memory
65      *  allocation. The hardcode value 128 is 2 times of block_size which
66      *  is 64 in md_wrap.c
67      */
68     uint8_t hmac_ctx[128];
69 } mbedtls_md_context_t;
70 
71 /**
72  * \brief           This function returns the message-digest information
73  *                  associated with the given digest type.
74  *
75  * \param md_type   The type of digest to search for.
76  *
77  * \return          The message-digest information associated with \p md_type.
78  * \return          NULL if the associated message-digest information is not found.
79  */
80 const mbedtls_md_info_t *mbedtls_md_info_from_type(mbedtls_md_type_t md_type);
81 
82 /**
83  * \brief           This function initializes a message-digest context without
84  *                  binding it to a particular message-digest algorithm.
85  *
86  *                  This function should always be called first. It prepares the
87  *                  context for mbedtls_md_setup() for binding it to a
88  *                  message-digest algorithm.
89  */
90 void mbedtls_md_init(mbedtls_md_context_t *ctx);
91 
92 /**
93  * \brief           This function clears the internal structure of \p ctx and
94  *                  frees any embedded internal structure, but does not free
95  *                  \p ctx itself.
96  *
97  *                  If you have called mbedtls_md_setup() on \p ctx, you must
98  *                  call mbedtls_md_free() when you are no longer using the
99  *                  context.
100  *                  Calling this function if you have previously
101  *                  called mbedtls_md_init() and nothing else is optional.
102  *                  You must not call this function if you have not called
103  *                  mbedtls_md_init().
104  */
105 void mbedtls_md_free(mbedtls_md_context_t *ctx);
106 
107 /**
108  * \brief           This function selects the message digest algorithm to use,
109  *                  and allocates internal structures.
110  *
111  *                  It should be called after mbedtls_md_init() or
112  *                  mbedtls_md_free(). Makes it necessary to call
113  *                  mbedtls_md_free() later.
114  *
115  * \param ctx       The context to set up.
116  * \param md_info   The information structure of the message-digest algorithm
117  *                  to use.
118  *
119  * \return          \c 0 on success.
120  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
121  *                  failure.
122  * \return          #MBEDTLS_ERR_MD_ALLOC_FAILED on memory-allocation failure.
123  */
124 int32_t mbedtls_md_setup(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info);
125 
126 /**
127  * \brief           This function extracts the message-digest size from the
128  *                  message-digest information structure.
129  *
130  * \param md_info   The information structure of the message-digest algorithm
131  *                  to use.
132  *
133  * \return          The size of the message-digest output in Bytes.
134  */
135 uint8_t mbedtls_md_get_size(const mbedtls_md_info_t *md_info);
136 
137 /**
138  * \brief           This function sets the HMAC key and prepares to
139  *                  authenticate a new message.
140  *
141  *                  Call this function after mbedtls_md_setup(), to use
142  *                  the MD context for an HMAC calculation, then call
143  *                  mbedtls_md_hmac_update() to provide the input data, and
144  *                  mbedtls_md_hmac_finish() to get the HMAC value.
145  *
146  * \param ctx       The message digest context containing an embedded HMAC
147  *                  context.
148  * \param key       The HMAC secret key.
149  * \param keylen    The length of the HMAC key in Bytes.
150  *
151  * \return          \c 0 on success.
152  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
153  *                  failure.
154  */
155 int32_t mbedtls_md_hmac_starts(mbedtls_md_context_t *ctx, const uint8_t *key, size_t keylen);
156 
157 /**
158  * \brief           This function feeds an input buffer into an ongoing HMAC
159  *                  computation.
160  *
161  *                  Call mbedtls_md_hmac_starts() or mbedtls_md_hmac_reset()
162  *                  before calling this function.
163  *                  You may call this function multiple times to pass the
164  *                  input piecewise.
165  *                  Afterwards, call mbedtls_md_hmac_finish().
166  *
167  * \param ctx       The message digest context containing an embedded HMAC
168  *                  context.
169  * \param input     The buffer holding the input data.
170  * \param ilen      The length of the input data.
171  *
172  * \return          \c 0 on success.
173  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
174  *                  failure.
175  */
176 int32_t mbedtls_md_hmac_update(mbedtls_md_context_t *ctx, const uint8_t *input, size_t ilen);
177 
178 /**
179  * \brief           This function finishes the HMAC operation, and writes
180  *                  the result to the output buffer.
181  *
182  *                  Call this function after mbedtls_md_hmac_starts() and
183  *                  mbedtls_md_hmac_update() to get the HMAC value. Afterwards
184  *                  you may either call mbedtls_md_free() to clear the context,
185  *                  or call mbedtls_md_hmac_reset() to reuse the context with
186  *                  the same HMAC key.
187  *
188  * \param ctx       The message digest context containing an embedded HMAC
189  *                  context.
190  * \param output    The generic HMAC checksum result.
191  *
192  * \return          \c 0 on success.
193  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
194  *                  failure.
195  */
196 int32_t mbedtls_md_hmac_finish(mbedtls_md_context_t *ctx, uint8_t *output);
197 
198 /**
199  * \brief          This function calculates the full generic HMAC
200  *                 on the input buffer with the provided key.
201  *
202  *                 The function allocates the context, performs the
203  *                 calculation, and frees the context.
204  *
205  *                 The HMAC result is calculated as
206  *                 output = generic HMAC(hmac key, input buffer).
207  *
208  * \param md_info  The information structure of the message-digest algorithm
209  *                 to use.
210  * \param key      The HMAC secret key.
211  * \param keylen   The length of the HMAC secret key in Bytes.
212  * \param input    The buffer holding the input data.
213  * \param ilen     The length of the input data.
214  * \param output   The generic HMAC result.
215  *
216  * \return         \c 0 on success.
217  * \return         #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
218  *                 failure.
219  */
220 int32_t mbedtls_md_hmac(const mbedtls_md_info_t *md_info, const uint8_t *key, size_t keylen,
221                 const uint8_t *input, size_t ilen, uint8_t *output);
222 
223 #endif /* MBEDTLS_MD_H */
224