Merge remote-tracking branch 'origin/development' into msft-aarch64
This commit is contained in:
commit
b351d60e99
40 changed files with 976 additions and 532 deletions
|
@ -43,6 +43,17 @@
|
|||
#include <immintrin.h>
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ARCH_IS_X86)
|
||||
#if defined(MBEDTLS_COMPILER_IS_GCC)
|
||||
#pragma GCC push_options
|
||||
#pragma GCC target ("pclmul,sse2,aes")
|
||||
#define MBEDTLS_POP_TARGET_PRAGMA
|
||||
#elif defined(__clang__)
|
||||
#pragma clang attribute push (__attribute__((target("pclmul,sse2,aes"))), apply_to=function)
|
||||
#define MBEDTLS_POP_TARGET_PRAGMA
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if !defined(MBEDTLS_AES_USE_HARDWARE_ONLY)
|
||||
/*
|
||||
* AES-NI support detection routine
|
||||
|
@ -189,7 +200,7 @@ void mbedtls_aesni_gcm_mult(unsigned char c[16],
|
|||
const unsigned char a[16],
|
||||
const unsigned char b[16])
|
||||
{
|
||||
__m128i aa, bb, cc, dd;
|
||||
__m128i aa = { 0 }, bb = { 0 }, cc, dd;
|
||||
|
||||
/* The inputs are in big-endian order, so byte-reverse them */
|
||||
for (size_t i = 0; i < 16; i++) {
|
||||
|
@ -398,6 +409,15 @@ static void aesni_setkey_enc_256(unsigned char *rk_bytes,
|
|||
}
|
||||
#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
|
||||
|
||||
#if defined(MBEDTLS_POP_TARGET_PRAGMA)
|
||||
#if defined(__clang__)
|
||||
#pragma clang attribute pop
|
||||
#elif defined(__GNUC__)
|
||||
#pragma GCC pop_options
|
||||
#endif
|
||||
#undef MBEDTLS_POP_TARGET_PRAGMA
|
||||
#endif
|
||||
|
||||
#else /* MBEDTLS_AESNI_HAVE_CODE == 1 */
|
||||
|
||||
#if defined(__has_feature)
|
||||
|
|
|
@ -39,7 +39,7 @@
|
|||
* (Only implemented with certain compilers, only for certain targets.)
|
||||
*/
|
||||
#undef MBEDTLS_AESNI_HAVE_INTRINSICS
|
||||
#if defined(_MSC_VER)
|
||||
#if defined(_MSC_VER) && !defined(__clang__)
|
||||
/* Visual Studio supports AESNI intrinsics since VS 2008 SP1. We only support
|
||||
* VS 2013 and up for other reasons anyway, so no need to check the version. */
|
||||
#define MBEDTLS_AESNI_HAVE_INTRINSICS
|
||||
|
@ -47,7 +47,11 @@
|
|||
/* GCC-like compilers: currently, we only support intrinsics if the requisite
|
||||
* target flag is enabled when building the library (e.g. `gcc -mpclmul -msse2`
|
||||
* or `clang -maes -mpclmul`). */
|
||||
#if defined(__GNUC__) && defined(__AES__) && defined(__PCLMUL__)
|
||||
#if (defined(__GNUC__) || defined(__clang__)) && defined(__AES__) && defined(__PCLMUL__)
|
||||
#define MBEDTLS_AESNI_HAVE_INTRINSICS
|
||||
#endif
|
||||
/* For 32-bit, we only support intrinsics */
|
||||
#if defined(MBEDTLS_ARCH_IS_X86) && (defined(__GNUC__) || defined(__clang__))
|
||||
#define MBEDTLS_AESNI_HAVE_INTRINSICS
|
||||
#endif
|
||||
|
||||
|
@ -60,13 +64,11 @@
|
|||
#if defined(MBEDTLS_AESNI_HAVE_INTRINSICS)
|
||||
#define MBEDTLS_AESNI_HAVE_CODE 2 // via intrinsics
|
||||
#elif defined(MBEDTLS_HAVE_ASM) && \
|
||||
defined(__GNUC__) && defined(MBEDTLS_ARCH_IS_X64)
|
||||
(defined(__GNUC__) || defined(__clang__)) && defined(MBEDTLS_ARCH_IS_X64)
|
||||
/* Can we do AESNI with inline assembly?
|
||||
* (Only implemented with gas syntax, only for 64-bit.)
|
||||
*/
|
||||
#define MBEDTLS_AESNI_HAVE_CODE 1 // via assembly
|
||||
#elif defined(__GNUC__)
|
||||
# error "Must use `-mpclmul -msse2 -maes` for MBEDTLS_AESNI_C"
|
||||
#else
|
||||
#error "MBEDTLS_AESNI_C defined, but neither intrinsics nor assembly available"
|
||||
#endif
|
||||
|
|
|
@ -263,8 +263,11 @@ int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx,
|
|||
|
||||
memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
|
||||
|
||||
if (NULL == (ctx->cipher_ctx = mbedtls_cipher_get_base(cipher_info)->ctx_alloc_func())) {
|
||||
return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
|
||||
if (mbedtls_cipher_get_base(cipher_info)->ctx_alloc_func != NULL) {
|
||||
ctx->cipher_ctx = mbedtls_cipher_get_base(cipher_info)->ctx_alloc_func();
|
||||
if (ctx->cipher_ctx == NULL) {
|
||||
return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
ctx->cipher_info = cipher_info;
|
||||
|
|
|
@ -568,17 +568,18 @@ static const mbedtls_cipher_info_t aes_256_xts_info = {
|
|||
};
|
||||
#endif
|
||||
#endif /* MBEDTLS_CIPHER_MODE_XTS */
|
||||
#endif /* MBEDTLS_AES_C */
|
||||
|
||||
#if defined(MBEDTLS_GCM_C)
|
||||
#if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C)
|
||||
static int gcm_aes_setkey_wrap(void *ctx, const unsigned char *key,
|
||||
unsigned int key_bitlen)
|
||||
{
|
||||
return mbedtls_gcm_setkey((mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
|
||||
key, key_bitlen);
|
||||
}
|
||||
#endif /* MBEDTLS_GCM_C */
|
||||
#endif /* MBEDTLS_GCM_C && MBEDTLS_AES_C */
|
||||
|
||||
#if defined(MBEDTLS_CIPHER_HAVE_GCM_VIA_LEGACY_OR_USE_PSA)
|
||||
#if defined(MBEDTLS_CIPHER_HAVE_GCM_AES_VIA_LEGACY_OR_USE_PSA)
|
||||
static const mbedtls_cipher_base_t gcm_aes_info = {
|
||||
MBEDTLS_CIPHER_ID_AES,
|
||||
NULL,
|
||||
|
@ -612,9 +613,9 @@ static const mbedtls_cipher_base_t gcm_aes_info = {
|
|||
NULL,
|
||||
#endif /* MBEDTLS_GCM_C */
|
||||
};
|
||||
#endif /* MBEDTLS_CIPHER_HAVE_GCM_VIA_LEGACY_OR_USE_PSA */
|
||||
#endif /* MBEDTLS_CIPHER_HAVE_GCM_AES_VIA_LEGACY_OR_USE_PSA */
|
||||
|
||||
#if defined(MBEDTLS_CIPHER_HAVE_GCM_VIA_LEGACY_OR_USE_PSA)
|
||||
#if defined(MBEDTLS_CIPHER_HAVE_GCM_AES_VIA_LEGACY_OR_USE_PSA)
|
||||
static const mbedtls_cipher_info_t aes_128_gcm_info = {
|
||||
"AES-128-GCM",
|
||||
16,
|
||||
|
@ -649,18 +650,18 @@ static const mbedtls_cipher_info_t aes_256_gcm_info = {
|
|||
MBEDTLS_CIPHER_BASE_INDEX_GCM_AES
|
||||
};
|
||||
#endif
|
||||
#endif /* MBEDTLS_CIPHER_HAVE_GCM_VIA_LEGACY_OR_USE_PSA */
|
||||
#endif /* MBEDTLS_CIPHER_HAVE_GCM_AES_VIA_LEGACY_OR_USE_PSA */
|
||||
|
||||
#if defined(MBEDTLS_CCM_C)
|
||||
#if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)
|
||||
static int ccm_aes_setkey_wrap(void *ctx, const unsigned char *key,
|
||||
unsigned int key_bitlen)
|
||||
{
|
||||
return mbedtls_ccm_setkey((mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
|
||||
key, key_bitlen);
|
||||
}
|
||||
#endif /* MBEDTLS_CCM_C */
|
||||
#endif /* MBEDTLS_CCM_C && MBEDTLS_AES_C */
|
||||
|
||||
#if defined(MBEDTLS_CIPHER_HAVE_CCM_VIA_LEGACY_OR_USE_PSA)
|
||||
#if defined(MBEDTLS_CIPHER_HAVE_CCM_AES_VIA_LEGACY_OR_USE_PSA)
|
||||
static const mbedtls_cipher_base_t ccm_aes_info = {
|
||||
MBEDTLS_CIPHER_ID_AES,
|
||||
NULL,
|
||||
|
@ -694,9 +695,9 @@ static const mbedtls_cipher_base_t ccm_aes_info = {
|
|||
NULL,
|
||||
#endif
|
||||
};
|
||||
#endif /* MBEDTLS_CIPHER_HAVE_CCM_VIA_LEGACY_OR_USE_PSA */
|
||||
#endif /* MBEDTLS_CIPHER_HAVE_CCM_AES_VIA_LEGACY_OR_USE_PSA */
|
||||
|
||||
#if defined(MBEDTLS_CIPHER_HAVE_CCM_VIA_LEGACY_OR_USE_PSA)
|
||||
#if defined(MBEDTLS_CIPHER_HAVE_CCM_AES_VIA_LEGACY_OR_USE_PSA)
|
||||
static const mbedtls_cipher_info_t aes_128_ccm_info = {
|
||||
"AES-128-CCM",
|
||||
16,
|
||||
|
@ -731,9 +732,9 @@ static const mbedtls_cipher_info_t aes_256_ccm_info = {
|
|||
MBEDTLS_CIPHER_BASE_INDEX_CCM_AES
|
||||
};
|
||||
#endif
|
||||
#endif /* MBEDTLS_CIPHER_HAVE_CCM_VIA_LEGACY_OR_USE_PSA */
|
||||
#endif /* MBEDTLS_CIPHER_HAVE_CCM_AES_VIA_LEGACY_OR_USE_PSA */
|
||||
|
||||
#if defined(MBEDTLS_CIPHER_HAVE_CCM_STAR_NO_TAG_VIA_LEGACY_OR_USE_PSA)
|
||||
#if defined(MBEDTLS_CIPHER_HAVE_CCM_STAR_NO_TAG_AES_VIA_LEGACY_OR_USE_PSA)
|
||||
static const mbedtls_cipher_info_t aes_128_ccm_star_no_tag_info = {
|
||||
"AES-128-CCM*-NO-TAG",
|
||||
16,
|
||||
|
@ -768,9 +769,8 @@ static const mbedtls_cipher_info_t aes_256_ccm_star_no_tag_info = {
|
|||
MBEDTLS_CIPHER_BASE_INDEX_CCM_AES
|
||||
};
|
||||
#endif
|
||||
#endif /* MBEDTLS_CIPHER_HAVE_CCM_STAR_NO_TAG_VIA_LEGACY_OR_USE_PSA */
|
||||
#endif /* MBEDTLS_CIPHER_HAVE_CCM_STAR_NO_TAG_AES_VIA_LEGACY_OR_USE_PSA */
|
||||
|
||||
#endif /* MBEDTLS_AES_C */
|
||||
|
||||
#if defined(MBEDTLS_CAMELLIA_C)
|
||||
|
||||
|
@ -2269,28 +2269,28 @@ const mbedtls_cipher_definition_t mbedtls_cipher_definitions[] =
|
|||
{ MBEDTLS_CIPHER_AES_256_XTS, &aes_256_xts_info },
|
||||
#endif
|
||||
#endif
|
||||
#if defined(MBEDTLS_CIPHER_HAVE_GCM_VIA_LEGACY_OR_USE_PSA)
|
||||
#endif /* MBEDTLS_AES_C */
|
||||
#if defined(MBEDTLS_CIPHER_HAVE_GCM_AES_VIA_LEGACY_OR_USE_PSA)
|
||||
{ MBEDTLS_CIPHER_AES_128_GCM, &aes_128_gcm_info },
|
||||
#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
|
||||
{ MBEDTLS_CIPHER_AES_192_GCM, &aes_192_gcm_info },
|
||||
{ MBEDTLS_CIPHER_AES_256_GCM, &aes_256_gcm_info },
|
||||
#endif
|
||||
#endif
|
||||
#if defined(MBEDTLS_CIPHER_HAVE_CCM_VIA_LEGACY_OR_USE_PSA)
|
||||
#if defined(MBEDTLS_CIPHER_HAVE_CCM_AES_VIA_LEGACY_OR_USE_PSA)
|
||||
{ MBEDTLS_CIPHER_AES_128_CCM, &aes_128_ccm_info },
|
||||
#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
|
||||
{ MBEDTLS_CIPHER_AES_192_CCM, &aes_192_ccm_info },
|
||||
{ MBEDTLS_CIPHER_AES_256_CCM, &aes_256_ccm_info },
|
||||
#endif
|
||||
#endif
|
||||
#if defined(MBEDTLS_CIPHER_HAVE_CCM_STAR_NO_TAG_VIA_LEGACY_OR_USE_PSA)
|
||||
#if defined(MBEDTLS_CIPHER_HAVE_CCM_STAR_NO_TAG_AES_VIA_LEGACY_OR_USE_PSA)
|
||||
{ MBEDTLS_CIPHER_AES_128_CCM_STAR_NO_TAG, &aes_128_ccm_star_no_tag_info },
|
||||
#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
|
||||
{ MBEDTLS_CIPHER_AES_192_CCM_STAR_NO_TAG, &aes_192_ccm_star_no_tag_info },
|
||||
{ MBEDTLS_CIPHER_AES_256_CCM_STAR_NO_TAG, &aes_256_ccm_star_no_tag_info },
|
||||
#endif
|
||||
#endif
|
||||
#endif /* MBEDTLS_AES_C */
|
||||
|
||||
#if defined(MBEDTLS_CAMELLIA_C)
|
||||
{ MBEDTLS_CIPHER_CAMELLIA_128_ECB, &camellia_128_ecb_info },
|
||||
|
|
|
@ -62,6 +62,12 @@ extern "C" {
|
|||
#define MBEDTLS_CIPHER_HAVE_CCM_STAR_NO_TAG_VIA_LEGACY_OR_USE_PSA
|
||||
#endif
|
||||
|
||||
#if (defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)) || \
|
||||
(defined(MBEDTLS_USE_PSA_CRYPTO) && defined(PSA_WANT_ALG_CCM_STAR_NO_TAG) && \
|
||||
defined(PSA_WANT_KEY_TYPE_AES))
|
||||
#define MBEDTLS_CIPHER_HAVE_CCM_STAR_NO_TAG_AES_VIA_LEGACY_OR_USE_PSA
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_CHACHAPOLY_C) || \
|
||||
(defined(MBEDTLS_USE_PSA_CRYPTO) && defined(PSA_WANT_ALG_CHACHA20_POLY1305))
|
||||
#define MBEDTLS_CIPHER_HAVE_CHACHAPOLY_VIA_LEGACY_OR_USE_PSA
|
||||
|
|
|
@ -348,8 +348,14 @@ static inline void mbedtls_xor_no_simd(unsigned char *r,
|
|||
# define MBEDTLS_MAYBE_UNUSED __attribute__((unused))
|
||||
#endif
|
||||
#if !defined(MBEDTLS_MAYBE_UNUSED) && defined(__IAR_SYSTEMS_ICC__) && defined(__VER__)
|
||||
# if (__VER__ >= 8010000) // IAR 8.1 or later
|
||||
# define MBEDTLS_MAYBE_UNUSED __attribute__((unused))
|
||||
/* IAR does support __attribute__((unused)), but only if the -e flag (extended language support)
|
||||
* is given; the pragma always works.
|
||||
* Unfortunately the pragma affects the rest of the file where it is used, but this is harmless.
|
||||
* Check for version 5.2 or later - this pragma may be supported by earlier versions, but I wasn't
|
||||
* able to find documentation).
|
||||
*/
|
||||
# if (__VER__ >= 5020000)
|
||||
# define MBEDTLS_MAYBE_UNUSED _Pragma("diag_suppress=Pe177")
|
||||
# endif
|
||||
#endif
|
||||
#if !defined(MBEDTLS_MAYBE_UNUSED) && defined(_MSC_VER)
|
||||
|
|
|
@ -1424,7 +1424,7 @@ MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der(
|
|||
unsigned char *buf;
|
||||
unsigned char *p, *end;
|
||||
mbedtls_asn1_buf pbe_alg_oid, pbe_params;
|
||||
#if defined(MBEDTLS_PKCS12_C)
|
||||
#if defined(MBEDTLS_PKCS12_C) && defined(MBEDTLS_CIPHER_PADDING_PKCS7)
|
||||
mbedtls_cipher_type_t cipher_alg;
|
||||
mbedtls_md_type_t md_alg;
|
||||
#endif
|
||||
|
@ -1472,7 +1472,7 @@ MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der(
|
|||
/*
|
||||
* Decrypt EncryptedData with appropriate PBE
|
||||
*/
|
||||
#if defined(MBEDTLS_PKCS12_C)
|
||||
#if defined(MBEDTLS_PKCS12_C) && defined(MBEDTLS_CIPHER_PADDING_PKCS7)
|
||||
if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
|
||||
if ((ret = mbedtls_pkcs12_pbe_ext(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
|
||||
cipher_alg, md_alg,
|
||||
|
@ -1487,7 +1487,7 @@ MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der(
|
|||
decrypted = 1;
|
||||
} else
|
||||
#endif /* MBEDTLS_PKCS12_C */
|
||||
#if defined(MBEDTLS_PKCS5_C)
|
||||
#if defined(MBEDTLS_PKCS5_C) && defined(MBEDTLS_CIPHER_PADDING_PKCS7)
|
||||
if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
|
||||
if ((ret = mbedtls_pkcs5_pbes2_ext(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
|
||||
p, len, buf, len, &outlen)) != 0) {
|
||||
|
|
|
@ -265,7 +265,7 @@ mbedtls_ms_time_t mbedtls_ms_time(void)
|
|||
struct timespec tv;
|
||||
mbedtls_ms_time_t current_ms;
|
||||
|
||||
#if defined(__linux__)
|
||||
#if defined(__linux__) && defined(CLOCK_BOOTTIME)
|
||||
ret = clock_gettime(CLOCK_BOOTTIME, &tv);
|
||||
#else
|
||||
ret = clock_gettime(CLOCK_MONOTONIC, &tv);
|
||||
|
|
|
@ -43,19 +43,15 @@ static psa_status_t psa_aead_setup(
|
|||
psa_algorithm_t alg)
|
||||
{
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
size_t key_bits;
|
||||
const mbedtls_cipher_info_t *cipher_info;
|
||||
mbedtls_cipher_id_t cipher_id;
|
||||
|
||||
mbedtls_cipher_mode_t mode;
|
||||
size_t key_bits = attributes->core.bits;
|
||||
(void) key_buffer_size;
|
||||
|
||||
key_bits = attributes->core.bits;
|
||||
|
||||
cipher_info = mbedtls_cipher_info_from_psa(alg,
|
||||
attributes->core.type, key_bits,
|
||||
&cipher_id);
|
||||
if (cipher_info == NULL) {
|
||||
return PSA_ERROR_NOT_SUPPORTED;
|
||||
status = mbedtls_cipher_values_from_psa(alg, attributes->core.type,
|
||||
&key_bits, &mode, &cipher_id);
|
||||
if (status != PSA_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
|
||||
|
|
|
@ -31,14 +31,70 @@
|
|||
|
||||
#include <string.h>
|
||||
|
||||
const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
|
||||
/* mbedtls_cipher_values_from_psa() below only checks if the proper build symbols
|
||||
* are enabled, but it does not provide any compatibility check between them
|
||||
* (i.e. if the specified key works with the specified algorithm). This helper
|
||||
* function is meant to provide this support.
|
||||
* mbedtls_cipher_info_from_psa() might be used for the same purpose, but it
|
||||
* requires CIPHER_C to be enabled.
|
||||
*/
|
||||
static psa_status_t mbedtls_cipher_validate_values(
|
||||
psa_algorithm_t alg,
|
||||
psa_key_type_t key_type)
|
||||
{
|
||||
switch (alg) {
|
||||
case PSA_ALG_STREAM_CIPHER:
|
||||
case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
|
||||
if (key_type != PSA_KEY_TYPE_CHACHA20) {
|
||||
return PSA_ERROR_NOT_SUPPORTED;
|
||||
}
|
||||
break;
|
||||
|
||||
case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
|
||||
case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
|
||||
case PSA_ALG_CCM_STAR_NO_TAG:
|
||||
if ((key_type != PSA_KEY_TYPE_AES) &&
|
||||
(key_type != PSA_KEY_TYPE_ARIA) &&
|
||||
(key_type != PSA_KEY_TYPE_CAMELLIA)) {
|
||||
return PSA_ERROR_NOT_SUPPORTED;
|
||||
}
|
||||
break;
|
||||
|
||||
case PSA_ALG_CTR:
|
||||
case PSA_ALG_CFB:
|
||||
case PSA_ALG_OFB:
|
||||
case PSA_ALG_XTS:
|
||||
case PSA_ALG_ECB_NO_PADDING:
|
||||
case PSA_ALG_CBC_NO_PADDING:
|
||||
case PSA_ALG_CBC_PKCS7:
|
||||
case PSA_ALG_CMAC:
|
||||
if ((key_type != PSA_KEY_TYPE_AES) &&
|
||||
(key_type != PSA_KEY_TYPE_ARIA) &&
|
||||
(key_type != PSA_KEY_TYPE_DES) &&
|
||||
(key_type != PSA_KEY_TYPE_CAMELLIA)) {
|
||||
return PSA_ERROR_NOT_SUPPORTED;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
return PSA_ERROR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
return PSA_SUCCESS;
|
||||
}
|
||||
|
||||
psa_status_t mbedtls_cipher_values_from_psa(
|
||||
psa_algorithm_t alg,
|
||||
psa_key_type_t key_type,
|
||||
size_t key_bits,
|
||||
size_t *key_bits,
|
||||
mbedtls_cipher_mode_t *mode,
|
||||
mbedtls_cipher_id_t *cipher_id)
|
||||
{
|
||||
mbedtls_cipher_mode_t mode;
|
||||
mbedtls_cipher_id_t cipher_id_tmp;
|
||||
/* Only DES modifies key_bits */
|
||||
#if !defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
|
||||
(void) key_bits;
|
||||
#endif
|
||||
|
||||
if (PSA_ALG_IS_AEAD(alg)) {
|
||||
alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0);
|
||||
|
@ -48,66 +104,66 @@ const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
|
|||
switch (alg) {
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_STREAM_CIPHER)
|
||||
case PSA_ALG_STREAM_CIPHER:
|
||||
mode = MBEDTLS_MODE_STREAM;
|
||||
*mode = MBEDTLS_MODE_STREAM;
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_CTR)
|
||||
case PSA_ALG_CTR:
|
||||
mode = MBEDTLS_MODE_CTR;
|
||||
*mode = MBEDTLS_MODE_CTR;
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_CFB)
|
||||
case PSA_ALG_CFB:
|
||||
mode = MBEDTLS_MODE_CFB;
|
||||
*mode = MBEDTLS_MODE_CFB;
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_OFB)
|
||||
case PSA_ALG_OFB:
|
||||
mode = MBEDTLS_MODE_OFB;
|
||||
*mode = MBEDTLS_MODE_OFB;
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECB_NO_PADDING)
|
||||
case PSA_ALG_ECB_NO_PADDING:
|
||||
mode = MBEDTLS_MODE_ECB;
|
||||
*mode = MBEDTLS_MODE_ECB;
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING)
|
||||
case PSA_ALG_CBC_NO_PADDING:
|
||||
mode = MBEDTLS_MODE_CBC;
|
||||
*mode = MBEDTLS_MODE_CBC;
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7)
|
||||
case PSA_ALG_CBC_PKCS7:
|
||||
mode = MBEDTLS_MODE_CBC;
|
||||
*mode = MBEDTLS_MODE_CBC;
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM_STAR_NO_TAG)
|
||||
case PSA_ALG_CCM_STAR_NO_TAG:
|
||||
mode = MBEDTLS_MODE_CCM_STAR_NO_TAG;
|
||||
*mode = MBEDTLS_MODE_CCM_STAR_NO_TAG;
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
|
||||
case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
|
||||
mode = MBEDTLS_MODE_CCM;
|
||||
*mode = MBEDTLS_MODE_CCM;
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
|
||||
case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
|
||||
mode = MBEDTLS_MODE_GCM;
|
||||
*mode = MBEDTLS_MODE_GCM;
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
|
||||
case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
|
||||
mode = MBEDTLS_MODE_CHACHAPOLY;
|
||||
*mode = MBEDTLS_MODE_CHACHAPOLY;
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
return NULL;
|
||||
return PSA_ERROR_NOT_SUPPORTED;
|
||||
}
|
||||
} else if (alg == PSA_ALG_CMAC) {
|
||||
mode = MBEDTLS_MODE_ECB;
|
||||
*mode = MBEDTLS_MODE_ECB;
|
||||
} else {
|
||||
return NULL;
|
||||
return PSA_ERROR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
switch (key_type) {
|
||||
|
@ -125,7 +181,7 @@ const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
|
|||
case PSA_KEY_TYPE_DES:
|
||||
/* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
|
||||
* and 192 for three-key Triple-DES. */
|
||||
if (key_bits == 64) {
|
||||
if (*key_bits == 64) {
|
||||
cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
|
||||
} else {
|
||||
cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
|
||||
|
@ -133,8 +189,8 @@ const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
|
|||
/* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
|
||||
* but two-key Triple-DES is functionally three-key Triple-DES
|
||||
* with K1=K3, so that's how we present it to mbedtls. */
|
||||
if (key_bits == 128) {
|
||||
key_bits = 192;
|
||||
if (*key_bits == 128) {
|
||||
*key_bits = 192;
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
|
@ -149,16 +205,38 @@ const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
|
|||
break;
|
||||
#endif
|
||||
default:
|
||||
return NULL;
|
||||
return PSA_ERROR_NOT_SUPPORTED;
|
||||
}
|
||||
if (cipher_id != NULL) {
|
||||
*cipher_id = cipher_id_tmp;
|
||||
}
|
||||
|
||||
return mbedtls_cipher_info_from_values(cipher_id_tmp,
|
||||
(int) key_bits, mode);
|
||||
return mbedtls_cipher_validate_values(alg, key_type);
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_CIPHER_C)
|
||||
const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
|
||||
psa_algorithm_t alg,
|
||||
psa_key_type_t key_type,
|
||||
size_t key_bits,
|
||||
mbedtls_cipher_id_t *cipher_id)
|
||||
{
|
||||
mbedtls_cipher_mode_t mode;
|
||||
psa_status_t status;
|
||||
mbedtls_cipher_id_t cipher_id_tmp;
|
||||
|
||||
status = mbedtls_cipher_values_from_psa(alg, key_type, &key_bits, &mode, &cipher_id_tmp);
|
||||
if (status != PSA_SUCCESS) {
|
||||
return NULL;
|
||||
}
|
||||
if (cipher_id != NULL) {
|
||||
*cipher_id = cipher_id_tmp;
|
||||
}
|
||||
|
||||
return mbedtls_cipher_info_from_values(cipher_id_tmp, (int) key_bits, mode);
|
||||
}
|
||||
#endif /* MBEDTLS_CIPHER_C */
|
||||
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
|
||||
|
||||
static psa_status_t psa_cipher_setup(
|
||||
|
|
|
@ -24,6 +24,28 @@
|
|||
#include <mbedtls/cipher.h>
|
||||
#include <psa/crypto.h>
|
||||
|
||||
/** Get Mbed TLS cipher information given the cipher algorithm PSA identifier
|
||||
* as well as the PSA type and size of the key to be used with the cipher
|
||||
* algorithm.
|
||||
*
|
||||
* \param[in] alg PSA cipher algorithm identifier
|
||||
* \param[in] key_type PSA key type
|
||||
* \param[in,out] key_bits Size of the key in bits. The value provided in input
|
||||
* might be updated if necessary.
|
||||
* \param[out] mode Mbed TLS cipher mode
|
||||
* \param[out] cipher_id Mbed TLS cipher algorithm identifier
|
||||
*
|
||||
* \return On success \c PSA_SUCCESS is returned and key_bits, mode and cipher_id
|
||||
* are properly updated.
|
||||
* \c PSA_ERROR_NOT_SUPPORTED is returned if the cipher algorithm is not
|
||||
* supported.
|
||||
*/
|
||||
|
||||
psa_status_t mbedtls_cipher_values_from_psa(psa_algorithm_t alg, psa_key_type_t key_type,
|
||||
size_t *key_bits, mbedtls_cipher_mode_t *mode,
|
||||
mbedtls_cipher_id_t *cipher_id);
|
||||
|
||||
#if defined(MBEDTLS_CIPHER_C)
|
||||
/** Get Mbed TLS cipher information given the cipher algorithm PSA identifier
|
||||
* as well as the PSA type and size of the key to be used with the cipher
|
||||
* algorithm.
|
||||
|
@ -39,6 +61,7 @@
|
|||
const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
|
||||
psa_algorithm_t alg, psa_key_type_t key_type, size_t key_bits,
|
||||
mbedtls_cipher_id_t *cipher_id);
|
||||
#endif /* MBEDTLS_CIPHER_C */
|
||||
|
||||
/**
|
||||
* \brief Set the key for a multipart symmetric encryption operation.
|
||||
|
|
|
@ -2129,6 +2129,12 @@ int mbedtls_ssl_tls13_write_early_data_ext(mbedtls_ssl_context *ssl,
|
|||
unsigned char *buf,
|
||||
const unsigned char *end,
|
||||
size_t *out_len);
|
||||
|
||||
#if defined(MBEDTLS_SSL_SRV_C)
|
||||
#define MBEDTLS_SSL_EARLY_DATA_STATUS_NOT_RECEIVED \
|
||||
MBEDTLS_SSL_EARLY_DATA_STATUS_NOT_SENT
|
||||
#endif /* MBEDTLS_SSL_SRV_C */
|
||||
|
||||
#endif /* MBEDTLS_SSL_EARLY_DATA */
|
||||
|
||||
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
|
||||
|
|
|
@ -1749,6 +1749,25 @@ static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
|
|||
return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_SSL_EARLY_DATA)
|
||||
static void ssl_tls13_update_early_data_status(mbedtls_ssl_context *ssl)
|
||||
{
|
||||
mbedtls_ssl_handshake_params *handshake = ssl->handshake;
|
||||
|
||||
if ((handshake->received_extensions &
|
||||
MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) == 0) {
|
||||
MBEDTLS_SSL_DEBUG_MSG(
|
||||
1, ("EarlyData: no early data extension received."));
|
||||
ssl->early_data_status = MBEDTLS_SSL_EARLY_DATA_STATUS_NOT_RECEIVED;
|
||||
return;
|
||||
}
|
||||
|
||||
/* We do not accept early data for the time being */
|
||||
ssl->early_data_status = MBEDTLS_SSL_EARLY_DATA_STATUS_REJECTED;
|
||||
|
||||
}
|
||||
#endif /* MBEDTLS_SSL_EARLY_DATA */
|
||||
|
||||
/* Update the handshake state machine */
|
||||
|
||||
MBEDTLS_CHECK_RETURN_CRITICAL
|
||||
|
@ -1775,6 +1794,11 @@ static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl)
|
|||
return ret;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_SSL_EARLY_DATA)
|
||||
/* There is enough information, update early data state. */
|
||||
ssl_tls13_update_early_data_status(ssl);
|
||||
#endif /* MBEDTLS_SSL_EARLY_DATA */
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue