Skip to content

Commit 4dcb293

Browse files
committed
[zep noup] interface: mbedtls: align Mbed TLS headers to v3.6.5
TF-M holds a copy of Mbed TLS header files and prior to this commit they were aligned to Mbed TLS v3.6.3. This commit updates them to what's in release v3.6.5. This commit will be removed in the next TF-M release that includes Mbed TLS v3.6.5. Signed-off-by: Valerio Setti <vsetti@baylibre.com>
1 parent d3341a6 commit 4dcb293

File tree

23 files changed

+602
-322
lines changed

23 files changed

+602
-322
lines changed

interface/include/mbedtls/bignum.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "mbedtls/private_access.h"
1313

1414
#include "mbedtls/build_info.h"
15+
#include "mbedtls/platform_util.h"
1516

1617
#include <stddef.h>
1718
#include <stdint.h>
@@ -928,7 +929,7 @@ int mbedtls_mpi_exp_mod(mbedtls_mpi *X, const mbedtls_mpi *A,
928929
* be relevant in applications like deterministic ECDSA.
929930
*/
930931
int mbedtls_mpi_fill_random(mbedtls_mpi *X, size_t size,
931-
int (*f_rng)(void *, unsigned char *, size_t),
932+
mbedtls_f_rng_t *f_rng,
932933
void *p_rng);
933934

934935
/** Generate a random number uniformly in a range.
@@ -966,13 +967,14 @@ int mbedtls_mpi_fill_random(mbedtls_mpi *X, size_t size,
966967
int mbedtls_mpi_random(mbedtls_mpi *X,
967968
mbedtls_mpi_sint min,
968969
const mbedtls_mpi *N,
969-
int (*f_rng)(void *, unsigned char *, size_t),
970+
mbedtls_f_rng_t *f_rng,
970971
void *p_rng);
971972

972973
/**
973974
* \brief Compute the greatest common divisor: G = gcd(A, B)
974975
*
975976
* \param G The destination MPI. This must point to an initialized MPI.
977+
* This will always be positive or 0.
976978
* \param A The first operand. This must point to an initialized MPI.
977979
* \param B The second operand. This must point to an initialized MPI.
978980
*
@@ -987,10 +989,12 @@ int mbedtls_mpi_gcd(mbedtls_mpi *G, const mbedtls_mpi *A,
987989
* \brief Compute the modular inverse: X = A^-1 mod N
988990
*
989991
* \param X The destination MPI. This must point to an initialized MPI.
992+
* The value returned on success will be between [1, N-1].
990993
* \param A The MPI to calculate the modular inverse of. This must point
991-
* to an initialized MPI.
994+
* to an initialized MPI. This value can be negative, in which
995+
* case a positive answer will still be returned in \p X.
992996
* \param N The base of the modular inversion. This must point to an
993-
* initialized MPI.
997+
* initialized MPI and be greater than one.
994998
*
995999
* \return \c 0 if successful.
9961000
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
@@ -1030,7 +1034,7 @@ int mbedtls_mpi_inv_mod(mbedtls_mpi *X, const mbedtls_mpi *A,
10301034
* \return Another negative error code on other kinds of failure.
10311035
*/
10321036
int mbedtls_mpi_is_prime_ext(const mbedtls_mpi *X, int rounds,
1033-
int (*f_rng)(void *, unsigned char *, size_t),
1037+
mbedtls_f_rng_t *f_rng,
10341038
void *p_rng);
10351039
/**
10361040
* \brief Flags for mbedtls_mpi_gen_prime()
@@ -1063,7 +1067,7 @@ typedef enum {
10631067
* \c 3 and #MBEDTLS_MPI_MAX_BITS.
10641068
*/
10651069
int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int flags,
1066-
int (*f_rng)(void *, unsigned char *, size_t),
1070+
mbedtls_f_rng_t *f_rng,
10671071
void *p_rng);
10681072

10691073
#if defined(MBEDTLS_SELF_TEST)

interface/include/mbedtls/build_info.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@
2626
*/
2727
#define MBEDTLS_VERSION_MAJOR 3
2828
#define MBEDTLS_VERSION_MINOR 6
29-
#define MBEDTLS_VERSION_PATCH 3
29+
#define MBEDTLS_VERSION_PATCH 5
3030

3131
/**
3232
* The single version number has the following structure:
3333
* MMNNPP00
3434
* Major version | Minor version | Patch version
3535
*/
36-
#define MBEDTLS_VERSION_NUMBER 0x03060300
37-
#define MBEDTLS_VERSION_STRING "3.6.3"
38-
#define MBEDTLS_VERSION_STRING_FULL "Mbed TLS 3.6.3"
36+
#define MBEDTLS_VERSION_NUMBER 0x03060500
37+
#define MBEDTLS_VERSION_STRING "3.6.5"
38+
#define MBEDTLS_VERSION_STRING_FULL "Mbed TLS 3.6.5"
3939

4040
/* Macros for build-time platform detection */
4141

interface/include/mbedtls/cipher.h

Lines changed: 93 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,15 @@ typedef struct mbedtls_cipher_context_t {
329329
/** Padding functions to use, if relevant for
330330
* the specific cipher mode.
331331
*/
332-
void(*MBEDTLS_PRIVATE(add_padding))(unsigned char *output, size_t olen, size_t data_len);
333-
int(*MBEDTLS_PRIVATE(get_padding))(unsigned char *input, size_t ilen, size_t *data_len);
332+
void(*MBEDTLS_PRIVATE(add_padding))(unsigned char *output, size_t olen,
333+
size_t data_len);
334+
/* Report invalid-padding condition through the output parameter
335+
* invalid_padding. To minimize changes in Mbed TLS 3.6, where this
336+
* declaration is in a public header, use the public type size_t
337+
* rather than the internal type mbedtls_ct_condition_t. */
338+
int(*MBEDTLS_PRIVATE(get_padding))(unsigned char *input, size_t ilen,
339+
size_t *data_len,
340+
size_t *invalid_padding);
334341
#endif
335342

336343
/** Buffer for input that has not been processed yet. */
@@ -878,23 +885,24 @@ int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx,
878885
*
879886
* \note With non-AEAD ciphers, the order of calls for each message
880887
* is as follows:
881-
* 1. mbedtls_cipher_set_iv() if the mode uses an IV/nonce.
882-
* 2. mbedtls_cipher_reset()
883-
* 3. mbedtls_cipher_update() one or more times
884-
* 4. mbedtls_cipher_finish()
888+
* 1. mbedtls_cipher_set_iv() if the mode uses an IV/nonce;
889+
* 2. mbedtls_cipher_reset();
890+
* 3. mbedtls_cipher_update() zero, one or more times;
891+
* 4. mbedtls_cipher_finish_padded() (recommended for decryption
892+
* if the mode uses padding) or mbedtls_cipher_finish().
885893
* .
886894
* This sequence can be repeated to encrypt or decrypt multiple
887895
* messages with the same key.
888896
*
889897
* \note With AEAD ciphers, the order of calls for each message
890898
* is as follows:
891-
* 1. mbedtls_cipher_set_iv() if the mode uses an IV/nonce.
892-
* 2. mbedtls_cipher_reset()
893-
* 3. mbedtls_cipher_update_ad()
894-
* 4. mbedtls_cipher_update() one or more times
895-
* 5. mbedtls_cipher_finish()
899+
* 1. mbedtls_cipher_set_iv() if the mode uses an IV/nonce;
900+
* 2. mbedtls_cipher_reset();
901+
* 3. mbedtls_cipher_update_ad();
902+
* 4. mbedtls_cipher_update() zero, one or more times;
903+
* 5. mbedtls_cipher_finish() (or mbedtls_cipher_finish_padded());
896904
* 6. mbedtls_cipher_check_tag() (for decryption) or
897-
* mbedtls_cipher_write_tag() (for encryption).
905+
* mbedtls_cipher_write_tag() (for encryption).
898906
* .
899907
* This sequence can be repeated to encrypt or decrypt multiple
900908
* messages with the same key.
@@ -930,7 +938,8 @@ int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx,
930938
* many block-sized blocks of data as possible to output.
931939
* Any data that cannot be written immediately is either
932940
* added to the next block, or flushed when
933-
* mbedtls_cipher_finish() is called.
941+
* mbedtls_cipher_finish() or mbedtls_cipher_finish_padded()
942+
* is called.
934943
* Exception: For MBEDTLS_MODE_ECB, expects a single block
935944
* in size. For example, 16 Bytes for AES.
936945
*
@@ -964,30 +973,97 @@ int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx,
964973
* contained in it is padded to the size of
965974
* the last block, and written to the \p output buffer.
966975
*
976+
* \warning This function reports invalid padding through an error
977+
* code. Adversaries may be able to decrypt encrypted
978+
* data if they can submit chosen ciphertexts and
979+
* detect whether it has valid padding or not,
980+
* either through direct observation or through a side
981+
* channel such as timing. This is known as a
982+
* padding oracle attack.
983+
* Therefore applications that call this function for
984+
* decryption with a cipher that involves padding
985+
* should take care around error handling. Preferably,
986+
* such applications should use
987+
* mbedtls_cipher_finish_padded() instead of this function.
988+
*
967989
* \param ctx The generic cipher context. This must be initialized and
968990
* bound to a key.
969991
* \param output The buffer to write data to. This needs to be a writable
970992
* buffer of at least block_size Bytes.
971993
* \param olen The length of the data written to the \p output buffer.
972994
* This may not be \c NULL.
995+
* Note that when decrypting in a mode with padding,
996+
* the actual output length is sensitive and may be
997+
* used to mount a padding oracle attack (see warning
998+
* above), although less efficiently than through
999+
* the invalid-padding condition.
9731000
*
9741001
* \return \c 0 on success.
9751002
* \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
9761003
* parameter-verification failure.
9771004
* \return #MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED on decryption
9781005
* expecting a full block but not receiving one.
9791006
* \return #MBEDTLS_ERR_CIPHER_INVALID_PADDING on invalid padding
980-
* while decrypting.
1007+
* while decrypting. Note that invalid-padding errors
1008+
* should be handled carefully; see the warning above.
9811009
* \return A cipher-specific error code on failure.
9821010
*/
9831011
int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
9841012
unsigned char *output, size_t *olen);
9851013

1014+
/**
1015+
* \brief The generic cipher finalization function. If data still
1016+
* needs to be flushed from an incomplete block, the data
1017+
* contained in it is padded to the size of
1018+
* the last block, and written to the \p output buffer.
1019+
*
1020+
* \note This function is similar to mbedtls_cipher_finish().
1021+
* The only difference is that it reports invalid padding
1022+
* decryption differently, through the \p invalid_padding
1023+
* parameter rather than an error code.
1024+
* For encryption, and in modes without padding (including
1025+
* all authenticated modes), this function is identical
1026+
* to mbedtls_cipher_finish().
1027+
*
1028+
* \param[in,out] ctx The generic cipher context. This must be initialized and
1029+
* bound to a key.
1030+
* \param[out] output The buffer to write data to. This needs to be a writable
1031+
* buffer of at least block_size Bytes.
1032+
* \param[out] olen The length of the data written to the \p output buffer.
1033+
* This may not be \c NULL.
1034+
* Note that when decrypting in a mode with padding,
1035+
* the actual output length is sensitive and may be
1036+
* used to mount a padding oracle attack (see warning
1037+
* on mbedtls_cipher_finish()).
1038+
* \param[out] invalid_padding
1039+
* If this function returns \c 0 on decryption,
1040+
* \p *invalid_padding is \c 0 if the ciphertext was
1041+
* valid, and all-bits-one if the ciphertext had invalid
1042+
* padding.
1043+
* On encryption, or in a mode without padding (including
1044+
* all authenticated modes), \p *invalid_padding is \c 0
1045+
* on success.
1046+
* The value in \p *invalid_padding is unspecified if
1047+
* this function returns a nonzero status.
1048+
*
1049+
* \return \c 0 on success.
1050+
* Also \c 0 for decryption with invalid padding.
1051+
* \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
1052+
* parameter-verification failure.
1053+
* \return #MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED on decryption
1054+
* expecting a full block but not receiving one.
1055+
* \return A cipher-specific error code on failure.
1056+
*/
1057+
int mbedtls_cipher_finish_padded(mbedtls_cipher_context_t *ctx,
1058+
unsigned char *output, size_t *olen,
1059+
size_t *invalid_padding);
1060+
9861061
#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
9871062
/**
9881063
* \brief This function writes a tag for AEAD ciphers.
9891064
* Currently supported with GCM and ChaCha20+Poly1305.
990-
* This must be called after mbedtls_cipher_finish().
1065+
* This must be called after mbedtls_cipher_finish()
1066+
* or mbedtls_cipher_finish_padded().
9911067
*
9921068
* \param ctx The generic cipher context. This must be initialized,
9931069
* bound to a key, and have just completed a cipher
@@ -1006,7 +1082,8 @@ int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
10061082
/**
10071083
* \brief This function checks the tag for AEAD ciphers.
10081084
* Currently supported with GCM and ChaCha20+Poly1305.
1009-
* This must be called after mbedtls_cipher_finish().
1085+
* This must be called after mbedtls_cipher_finish()
1086+
* or mbedtls_cipher_finish_padded().
10101087
*
10111088
* \param ctx The generic cipher context. This must be initialized.
10121089
* \param tag The buffer holding the tag. This must be a readable

interface/include/mbedtls/config_psa.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
* before we deduce what built-ins are required. */
3535
#include "psa/crypto_adjust_config_key_pair_types.h"
3636

37-
#if defined(MBEDTLS_PSA_CRYPTO_C)
37+
#if defined(MBEDTLS_PSA_CRYPTO_C) || defined(MCUBOOT_USE_PSA_CRYPTO)
3838
/* If we are implementing PSA crypto ourselves, then we want to enable the
3939
* required built-ins. Otherwise, PSA features will be provided by the server. */
4040
#include "mbedtls/config_adjust_legacy_from_psa.h"

interface/include/mbedtls/dhm.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ int mbedtls_dhm_read_params(mbedtls_dhm_context *ctx,
183183
*/
184184
int mbedtls_dhm_make_params(mbedtls_dhm_context *ctx, int x_size,
185185
unsigned char *output, size_t *olen,
186-
int (*f_rng)(void *, unsigned char *, size_t),
186+
mbedtls_f_rng_t *f_rng,
187187
void *p_rng);
188188

189189
/**
@@ -250,7 +250,7 @@ int mbedtls_dhm_read_public(mbedtls_dhm_context *ctx,
250250
*/
251251
int mbedtls_dhm_make_public(mbedtls_dhm_context *ctx, int x_size,
252252
unsigned char *output, size_t olen,
253-
int (*f_rng)(void *, unsigned char *, size_t),
253+
mbedtls_f_rng_t *f_rng,
254254
void *p_rng);
255255

256256
/**
@@ -281,7 +281,7 @@ int mbedtls_dhm_make_public(mbedtls_dhm_context *ctx, int x_size,
281281
*/
282282
int mbedtls_dhm_calc_secret(mbedtls_dhm_context *ctx,
283283
unsigned char *output, size_t output_size, size_t *olen,
284-
int (*f_rng)(void *, unsigned char *, size_t),
284+
mbedtls_f_rng_t *f_rng,
285285
void *p_rng);
286286

287287
/**

interface/include/mbedtls/ecdh.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ int mbedtls_ecdh_can_do(mbedtls_ecp_group_id gid);
189189
* \c MBEDTLS_MPI_XXX error code on failure.
190190
*/
191191
int mbedtls_ecdh_gen_public(mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
192-
int (*f_rng)(void *, unsigned char *, size_t),
192+
mbedtls_f_rng_t *f_rng,
193193
void *p_rng);
194194

195195
/**
@@ -225,7 +225,7 @@ int mbedtls_ecdh_gen_public(mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_
225225
*/
226226
int mbedtls_ecdh_compute_shared(mbedtls_ecp_group *grp, mbedtls_mpi *z,
227227
const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
228-
int (*f_rng)(void *, unsigned char *, size_t),
228+
mbedtls_f_rng_t *f_rng,
229229
void *p_rng);
230230

231231
/**
@@ -290,7 +290,7 @@ void mbedtls_ecdh_free(mbedtls_ecdh_context *ctx);
290290
*/
291291
int mbedtls_ecdh_make_params(mbedtls_ecdh_context *ctx, size_t *olen,
292292
unsigned char *buf, size_t blen,
293-
int (*f_rng)(void *, unsigned char *, size_t),
293+
mbedtls_f_rng_t *f_rng,
294294
void *p_rng);
295295

296296
/**
@@ -372,7 +372,7 @@ int mbedtls_ecdh_get_params(mbedtls_ecdh_context *ctx,
372372
*/
373373
int mbedtls_ecdh_make_public(mbedtls_ecdh_context *ctx, size_t *olen,
374374
unsigned char *buf, size_t blen,
375-
int (*f_rng)(void *, unsigned char *, size_t),
375+
mbedtls_f_rng_t *f_rng,
376376
void *p_rng);
377377

378378
/**
@@ -428,7 +428,7 @@ int mbedtls_ecdh_read_public(mbedtls_ecdh_context *ctx,
428428
*/
429429
int mbedtls_ecdh_calc_secret(mbedtls_ecdh_context *ctx, size_t *olen,
430430
unsigned char *buf, size_t blen,
431-
int (*f_rng)(void *, unsigned char *, size_t),
431+
mbedtls_f_rng_t *f_rng,
432432
void *p_rng);
433433

434434
#if defined(MBEDTLS_ECP_RESTARTABLE)

0 commit comments

Comments
 (0)