Skip to content

Commit 3cff077

Browse files
committed
crypto: Update to Mbed Crypto 3.0.0d0
1 parent 789a193 commit 3cff077

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1033
-627
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
mbedcrypto-2.1.0d0
1+
mbedcrypto-3.0.0d0

features/mbedtls/mbed-crypto/importer/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929

3030
# Set the Mbed Crypto release to import (this can/should be edited before
3131
# import)
32-
CRYPTO_RELEASE ?= mbedcrypto-2.1.0d0
33-
CRYPTO_REPO_URL ?= [email protected]:ARMmbed/mbedtls-psa.git
32+
CRYPTO_RELEASE ?= mbedcrypto-3.0.0d0
33+
CRYPTO_REPO_URL ?= [email protected]:ARMmbed/mbed-crypto.git
3434

3535
# Translate between Mbed Crypto namespace and Mbed OS namespace
3636
TARGET_PREFIX:=..

features/mbedtls/mbed-crypto/inc/mbedtls/asn1.h

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
#define MBEDTLS_ASN1_OCTET_STRING 0x04
7676
#define MBEDTLS_ASN1_NULL 0x05
7777
#define MBEDTLS_ASN1_OID 0x06
78+
#define MBEDTLS_ASN1_ENUMERATED 0x0A
7879
#define MBEDTLS_ASN1_UTF8_STRING 0x0C
7980
#define MBEDTLS_ASN1_SEQUENCE 0x10
8081
#define MBEDTLS_ASN1_SET 0x11
@@ -254,13 +255,32 @@ int mbedtls_asn1_get_bool( unsigned char **p,
254255
* a valid ASN.1 INTEGER.
255256
* \return #MBEDTLS_ERR_ASN1_INVALID_LENGTH if the parsed value does
256257
* not fit in an \c int.
257-
* \return An ASN.1 error code if the input does not start with
258-
* a valid ASN.1 INTEGER.
259258
*/
260259
int mbedtls_asn1_get_int( unsigned char **p,
261260
const unsigned char *end,
262261
int *val );
263262

263+
/**
264+
* \brief Retrieve an enumerated ASN.1 tag and its value.
265+
* Updates the pointer to immediately behind the full tag.
266+
*
267+
* \param p On entry, \c *p points to the start of the ASN.1 element.
268+
* On successful completion, \c *p points to the first byte
269+
* beyond the ASN.1 element.
270+
* On error, the value of \c *p is undefined.
271+
* \param end End of data.
272+
* \param val On success, the parsed value.
273+
*
274+
* \return 0 if successful.
275+
* \return An ASN.1 error code if the input does not start with
276+
* a valid ASN.1 ENUMERATED.
277+
* \return #MBEDTLS_ERR_ASN1_INVALID_LENGTH if the parsed value does
278+
* not fit in an \c int.
279+
*/
280+
int mbedtls_asn1_get_enum( unsigned char **p,
281+
const unsigned char *end,
282+
int *val );
283+
264284
/**
265285
* \brief Retrieve a bitstring ASN.1 tag and its value.
266286
* Updates the pointer to immediately behind the full tag.
@@ -367,8 +387,6 @@ int mbedtls_asn1_get_sequence_of( unsigned char **p,
367387
* \return #MBEDTLS_ERR_ASN1_INVALID_LENGTH if the parsed value does
368388
* not fit in an \c int.
369389
* \return An MPI error code if the parsed value is too large.
370-
* \return An ASN.1 error code if the input does not start with
371-
* a valid ASN.1 INTEGER.
372390
*/
373391
int mbedtls_asn1_get_mpi( unsigned char **p,
374392
const unsigned char *end,

features/mbedtls/mbed-crypto/inc/mbedtls/asn1write.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,21 @@ int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start,
192192
*/
193193
int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val );
194194

195+
/**
196+
* \brief Write an enum tag (#MBEDTLS_ASN1_ENUMERATED) and value
197+
* in ASN.1 format.
198+
*
199+
* \note This function works backwards in data buffer.
200+
*
201+
* \param p The reference to the current position pointer.
202+
* \param start The start of the buffer, for bounds-checking.
203+
* \param val The integer value to write.
204+
*
205+
* \return The number of bytes written to \p p on success.
206+
* \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
207+
*/
208+
int mbedtls_asn1_write_enum( unsigned char **p, unsigned char *start, int val );
209+
195210
/**
196211
* \brief Write a string in ASN.1 format using a specific
197212
* string encoding tag.

features/mbedtls/mbed-crypto/inc/mbedtls/ctr_drbg.h

Lines changed: 118 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,14 @@
1212
* The Mbed TLS implementation of CTR_DRBG uses AES-256 (default) or AES-128
1313
* (if \c MBEDTLS_CTR_DRBG_USE_128_BIT_KEY is enabled at compile time)
1414
* as the underlying block cipher, with a derivation function.
15-
* The initial seeding grabs #MBEDTLS_CTR_DRBG_ENTROPY_LEN bytes of entropy.
16-
* See the documentation of mbedtls_ctr_drbg_seed() for more details.
17-
*
18-
* Based on NIST SP 800-90A §10.2.1 table 3 and NIST SP 800-57 part 1 table 2,
19-
* here are the security strengths achieved in typical configuration:
20-
* - 256 bits under the default configuration of the library, with AES-256
21-
* and with #MBEDTLS_CTR_DRBG_ENTROPY_LEN set to 48 or more.
22-
* - 256 bits if AES-256 is used, #MBEDTLS_CTR_DRBG_ENTROPY_LEN is set
23-
* to 32 or more, and the DRBG is initialized with an explicit
24-
* nonce in the \c custom parameter to mbedtls_ctr_drbg_seed().
25-
* - 128 bits if AES-256 is used but #MBEDTLS_CTR_DRBG_ENTROPY_LEN is
26-
* between 24 and 47 and the DRBG is not initialized with an explicit
27-
* nonce (see mbedtls_ctr_drbg_seed()).
28-
* - 128 bits if AES-128 is used (\c MBEDTLS_CTR_DRBG_USE_128_BIT_KEY enabled)
29-
* and #MBEDTLS_CTR_DRBG_ENTROPY_LEN is set to 24 or more (which is
30-
* always the case unless it is explicitly set to a different value
31-
* in config.h).
32-
*
33-
* Note that the value of #MBEDTLS_CTR_DRBG_ENTROPY_LEN defaults to:
34-
* - \c 48 if the module \c MBEDTLS_SHA512_C is enabled and the symbol
35-
* \c MBEDTLS_ENTROPY_FORCE_SHA256 is disabled at compile time.
36-
* This is the default configuration of the library.
37-
* - \c 32 if the module \c MBEDTLS_SHA512_C is disabled at compile time.
38-
* - \c 32 if \c MBEDTLS_ENTROPY_FORCE_SHA256 is enabled at compile time.
15+
*
16+
* The security strength as defined in NIST SP 800-90A is
17+
* 128 bits when AES-128 is used (\c MBEDTLS_CTR_DRBG_USE_128_BIT_KEY enabled)
18+
* and 256 bits otherwise, provided that #MBEDTLS_CTR_DRBG_ENTROPY_LEN is
19+
* kept at its default value (and not overridden in config.h) and that the
20+
* DRBG instance is set up with default parameters.
21+
* See the documentation of mbedtls_ctr_drbg_seed() for more
22+
* information.
3923
*/
4024
/*
4125
* Copyright (C) 2006-2019, Arm Limited (or its affiliates), All Rights Reserved
@@ -163,20 +147,47 @@
163147
extern "C" {
164148
#endif
165149

150+
#if MBEDTLS_CTR_DRBG_ENTROPY_LEN >= MBEDTLS_CTR_DRBG_KEYSIZE * 3 / 2
151+
/** The default length of the nonce read from the entropy source.
152+
*
153+
* This is \c 0 because a single read from the entropy source is sufficient
154+
* to include a nonce.
155+
* See the documentation of mbedtls_ctr_drbg_seed() for more information.
156+
*/
157+
#define MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN 0
158+
#else
159+
/** The default length of the nonce read from the entropy source.
160+
*
161+
* This is half of the default entropy length because a single read from
162+
* the entropy source does not provide enough material to form a nonce.
163+
* See the documentation of mbedtls_ctr_drbg_seed() for more information.
164+
*/
165+
#define MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN ( MBEDTLS_CTR_DRBG_ENTROPY_LEN + 1 ) / 2
166+
#endif
167+
166168
/**
167169
* \brief The CTR_DRBG context structure.
168170
*/
169171
typedef struct mbedtls_ctr_drbg_context
170172
{
171173
unsigned char counter[16]; /*!< The counter (V). */
172-
int reseed_counter; /*!< The reseed counter. */
174+
int reseed_counter; /*!< The reseed counter.
175+
* This is the number of requests that have
176+
* been made since the last (re)seeding,
177+
* minus one.
178+
* Before the initial seeding, this field
179+
* contains the amount of entropy in bytes
180+
* to use as a nonce for the initial seeding.
181+
*/
173182
int prediction_resistance; /*!< This determines whether prediction
174183
resistance is enabled, that is
175184
whether to systematically reseed before
176185
each random generation. */
177186
size_t entropy_len; /*!< The amount of entropy grabbed on each
178-
seed or reseed operation. */
179-
int reseed_interval; /*!< The reseed interval. */
187+
seed or reseed operation, in bytes. */
188+
int reseed_interval; /*!< The reseed interval.
189+
* This is the maximum number of requests
190+
* that can be made between reseedings. */
180191

181192
mbedtls_aes_context aes_ctx; /*!< The AES context. */
182193

@@ -214,47 +225,71 @@ void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx );
214225
* with mbedtls_entropy_init() (which registers the platform's default
215226
* entropy sources).
216227
*
217-
* \p f_entropy is always called with a buffer size equal to the entropy
218-
* length. The entropy length is initially #MBEDTLS_CTR_DRBG_ENTROPY_LEN
219-
* and this value is always used for the initial seeding. You can change
220-
* the entropy length for subsequent seeding by calling
221-
* mbedtls_ctr_drbg_set_entropy_len() after this function.
228+
* The entropy length is #MBEDTLS_CTR_DRBG_ENTROPY_LEN by default.
229+
* You can override it by calling mbedtls_ctr_drbg_set_entropy_len().
230+
*
231+
* The entropy nonce length is:
232+
* - \c 0 if the entropy length is at least 3/2 times the entropy length,
233+
* which guarantees that the security strength is the maximum permitted
234+
* by the key size and entropy length according to NIST SP 800-90A §10.2.1;
235+
* - Half the entropy length otherwise.
236+
* You can override it by calling mbedtls_ctr_drbg_set_nonce_len().
237+
* With the default entropy length, the entropy nonce length is
238+
* #MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN.
222239
*
223-
* You can provide a personalization string in addition to the
240+
* You can provide a nonce and personalization string in addition to the
224241
* entropy source, to make this instantiation as unique as possible.
242+
* See SP 800-90A §8.6.7 for more details about nonces.
225243
*
226-
* \note The _seed_material_ value passed to the derivation
227-
* function in the CTR_DRBG Instantiate Process
228-
* described in NIST SP 800-90A §10.2.1.3.2
229-
* is the concatenation of the string obtained from
230-
* calling \p f_entropy and the \p custom string.
231-
* The origin of the nonce depends on the value of
232-
* the entropy length relative to the security strength.
233-
* - If the entropy length is at least 1.5 times the
234-
* security strength then the nonce is taken from the
235-
* string obtained with \p f_entropy.
236-
* - If the entropy length is less than the security
237-
* strength, then the nonce is taken from \p custom.
238-
* In this case, for compliance with SP 800-90A,
239-
* you must pass a unique value of \p custom at
240-
* each invocation. See SP 800-90A §8.6.7 for more
241-
* details.
244+
* The _seed_material_ value passed to the derivation function in
245+
* the CTR_DRBG Instantiate Process described in NIST SP 800-90A §10.2.1.3.2
246+
* is the concatenation of the following strings:
247+
* - A string obtained by calling \p f_entropy function for the entropy
248+
* length.
242249
*/
243-
#if MBEDTLS_CTR_DRBG_ENTROPY_LEN < MBEDTLS_CTR_DRBG_KEYSIZE * 3 / 2
244-
/** \warning When #MBEDTLS_CTR_DRBG_ENTROPY_LEN is less than
245-
* #MBEDTLS_CTR_DRBG_KEYSIZE * 3 / 2, to achieve the
246-
* maximum security strength permitted by CTR_DRBG,
247-
* you must pass a value of \p custom that is a nonce:
248-
* this value must never be repeated in subsequent
249-
* runs of the same application or on a different
250-
* device.
250+
#if MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN == 0
251+
/**
252+
* - If mbedtls_ctr_drbg_set_nonce_len() has been called, a string
253+
* obtained by calling \p f_entropy function for the specified length.
254+
*/
255+
#else
256+
/**
257+
* - A string obtained by calling \p f_entropy function for the entropy nonce
258+
* length. If the entropy nonce length is \c 0, this function does not
259+
* make a second call to \p f_entropy.
251260
*/
252261
#endif
253262
/**
263+
* - The \p custom string.
264+
*
265+
* \note To achieve the nominal security strength permitted
266+
* by CTR_DRBG, the entropy length must be:
267+
* - at least 16 bytes for a 128-bit strength
268+
* (maximum achievable strength when using AES-128);
269+
* - at least 32 bytes for a 256-bit strength
270+
* (maximum achievable strength when using AES-256).
271+
*
272+
* In addition, if you do not pass a nonce in \p custom,
273+
* the sum of the entropy length
274+
* and the entropy nonce length must be:
275+
* - at least 24 bytes for a 128-bit strength
276+
* (maximum achievable strength when using AES-128);
277+
* - at least 48 bytes for a 256-bit strength
278+
* (maximum achievable strength when using AES-256).
279+
*
254280
* \param ctx The CTR_DRBG context to seed.
281+
* It must have been initialized with
282+
* mbedtls_ctr_drbg_init().
283+
* After a successful call to mbedtls_ctr_drbg_seed(),
284+
* you may not call mbedtls_ctr_drbg_seed() again on
285+
* the same context unless you call
286+
* mbedtls_ctr_drbg_free() and mbedtls_ctr_drbg_init()
287+
* again first.
255288
* \param f_entropy The entropy callback, taking as arguments the
256289
* \p p_entropy context, the buffer to fill, and the
257290
* length of the buffer.
291+
* \p f_entropy is always called with a buffer size
292+
* less than or equal to the entropy length.
258293
* \param p_entropy The entropy context to pass to \p f_entropy.
259294
* \param custom The personalization string.
260295
* This can be \c NULL, in which case the personalization
@@ -298,15 +333,10 @@ void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx,
298333

299334
/**
300335
* \brief This function sets the amount of entropy grabbed on each
301-
* subsequent reseed.
336+
* seed or reseed.
302337
*
303338
* The default value is #MBEDTLS_CTR_DRBG_ENTROPY_LEN.
304339
*
305-
* \note mbedtls_ctr_drbg_seed() always sets the entropy length
306-
* to #MBEDTLS_CTR_DRBG_ENTROPY_LEN, so this function
307-
* only has an effect when it is called after
308-
* mbedtls_ctr_drbg_seed().
309-
*
310340
* \note The security strength of CTR_DRBG is bounded by the
311341
* entropy length. Thus:
312342
* - When using AES-256
@@ -321,11 +351,35 @@ void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx,
321351
*
322352
* \param ctx The CTR_DRBG context.
323353
* \param len The amount of entropy to grab, in bytes.
324-
* This must be at most #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT.
354+
* This must be at most #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT
355+
* and at most the maximum length accepted by the
356+
* entropy function that is set in the context.
325357
*/
326358
void mbedtls_ctr_drbg_set_entropy_len( mbedtls_ctr_drbg_context *ctx,
327359
size_t len );
328360

361+
/**
362+
* \brief This function sets the amount of entropy grabbed
363+
* as a nonce for the initial seeding.
364+
*
365+
* Call this function before calling mbedtls_ctr_drbg_seed() to read
366+
* a nonce from the entropy source during the initial seeding.
367+
*
368+
* \param ctx The CTR_DRBG context.
369+
* \param len The amount of entropy to grab for the nonce, in bytes.
370+
* This must be at most #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT
371+
* and at most the maximum length accepted by the
372+
* entropy function that is set in the context.
373+
*
374+
* \return \c 0 on success.
375+
* \return #MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG if \p len is
376+
* more than #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT.
377+
* \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
378+
* if the initial seeding has already taken place.
379+
*/
380+
int mbedtls_ctr_drbg_set_nonce_len( mbedtls_ctr_drbg_context *ctx,
381+
size_t len );
382+
329383
/**
330384
* \brief This function sets the reseed interval.
331385
*
@@ -499,11 +553,6 @@ int mbedtls_ctr_drbg_self_test( int verbose );
499553

500554
#endif /* MBEDTLS_SELF_TEST */
501555

502-
/* Internal functions (do not call directly) */
503-
int mbedtls_ctr_drbg_seed_entropy_len( mbedtls_ctr_drbg_context *,
504-
int (*)(void *, unsigned char *, size_t), void *,
505-
const unsigned char *, size_t, size_t );
506-
507556
#ifdef __cplusplus
508557
}
509558
#endif

features/mbedtls/mbed-crypto/inc/mbedtls/hmac_drbg.h

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,11 @@ void mbedtls_hmac_drbg_init( mbedtls_hmac_drbg_context *ctx );
139139
* Note that SHA-256 is just as efficient as SHA-224.
140140
* The security strength can be reduced if a smaller
141141
* entropy length is set with
142-
* mbedtls_hmac_drbg_set_entropy_len() afterwards.
142+
* mbedtls_hmac_drbg_set_entropy_len().
143143
*
144-
* \note The entropy length for the initial seeding is
145-
* the security strength (converted from bits to bytes).
146-
* You can set a different entropy length for subsequent
147-
* seeding by calling mbedtls_hmac_drbg_set_entropy_len()
148-
* after this function.
144+
* \note The default entropy length is the security strength
145+
* (converted from bits to bytes). You can override
146+
* it by calling mbedtls_hmac_drbg_set_entropy_len().
149147
*
150148
* \note During the initial seeding, this function calls
151149
* the entropy source to obtain a nonce
@@ -224,14 +222,9 @@ void mbedtls_hmac_drbg_set_prediction_resistance( mbedtls_hmac_drbg_context *ctx
224222

225223
/**
226224
* \brief This function sets the amount of entropy grabbed on each
227-
* reseed.
225+
* seed or reseed.
228226
*
229-
* The default value is set by mbedtls_hmac_drbg_seed().
230-
*
231-
* \note mbedtls_hmac_drbg_seed() always sets the entropy length
232-
* to the default value based on the chosen MD algorithm,
233-
* so this function only has an effect if it is called
234-
* after mbedtls_hmac_drbg_seed().
227+
* See the documentation of mbedtls_hmac_drbg_seed() for the default value.
235228
*
236229
* \param ctx The HMAC_DRBG context.
237230
* \param len The amount of entropy to grab, in bytes.

0 commit comments

Comments
 (0)