Mbed-TLS / mbedtls

An open source, portable, easy to use, readable and flexible TLS library, and reference implementation of the PSA Cryptography API. Releases are on a varying cadence, typically around 3 - 6 months between releases.
https://www.trustedfirmware.org/projects/mbed-tls/
Other
5.26k stars 2.57k forks source link

PSA: HMAC is supported when not requested #5830

Closed gilles-peskine-arm closed 1 year ago

gilles-peskine-arm commented 2 years ago

If PSA is compiled without support for HMAC keys, it still accepts creating them, which causes test_suite_psa_crypto_not_supported.generated to fail. See also https://github.com/Mbed-TLS/mbedtls/pull/5783#discussion_r864813228.

There is no product bug, and not even useless code (HMAC keys are just a bunch of bytes with no special handling). But it's annoying that the unit tests fail.

To reproduce:

$ cat configs/config-psa-asym.h
#define MBEDTLS_PSA_CRYPTO_C
#define MBEDTLS_PSA_CRYPTO_CONFIG
#define MBEDTLS_AES_C
#define MBEDTLS_CTR_DRBG_C
#define MBEDTLS_ENTROPY_C
$ cat configs/psa/config-psa-asym.h 
#define PSA_WANT_ALG_SHA_224
#define PSA_WANT_ALG_SHA_256
#define PSA_WANT_KEY_TYPE_ECC_KEYPAIR 1
#define PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY 1
#define PSA_WANT_ECC_SECP_R1_256 1
#define PSA_WANT_ALG_ECDSA
$ make CFLAGS='-DMBEDTLS_CRYPTO_CONFIG_FILE=\"../configs/config-psa-asym.h\" -DMBEDTLS_PSA_CRYPTO_CONFIG_FILE=\"../configs/psa/config-psa-asym.h\" -Wno-error=unused-parameter' test

Failing tests from test_suite_psa_crypto_not_supported.generated as of a745c7d4398b22ceae1ce21bfbcb2fa2f7fab4d3:

PSA import HMAC 128-bit not supported ............................. FAILED
  psa_import_key( &attributes, key_material->x, key_material->len, &key_id ) == PSA_ERROR_NOT_SUPPORTED
  at line 23, ../source/tests/suites/test_suite_psa_crypto_not_supported.function
  lhs = 0x0000000000000000 = 0
  rhs = 0xffffffffffffff7a = -134
PSA generate HMAC 128-bit not supported ........................... FAILED
  psa_generate_key( &attributes, &key_id ) == PSA_ERROR_NOT_SUPPORTED
  at line 44, ../source/tests/suites/test_suite_psa_crypto_not_supported.function
  lhs = 0x0000000000000000 = 0
  rhs = 0xffffffffffffff7a = -134
PSA import HMAC 160-bit not supported ............................. FAILED
  psa_import_key( &attributes, key_material->x, key_material->len, &key_id ) == PSA_ERROR_NOT_SUPPORTED
  at line 23, ../source/tests/suites/test_suite_psa_crypto_not_supported.function
  lhs = 0x0000000000000000 = 0
  rhs = 0xffffffffffffff7a = -134
PSA generate HMAC 160-bit not supported ........................... FAILED
  psa_generate_key( &attributes, &key_id ) == PSA_ERROR_NOT_SUPPORTED
  at line 44, ../source/tests/suites/test_suite_psa_crypto_not_supported.function
  lhs = 0x0000000000000000 = 0
  rhs = 0xffffffffffffff7a = -134
PSA import HMAC 224-bit not supported ............................. FAILED
  psa_import_key( &attributes, key_material->x, key_material->len, &key_id ) == PSA_ERROR_NOT_SUPPORTED
  at line 23, ../source/tests/suites/test_suite_psa_crypto_not_supported.function
  lhs = 0x0000000000000000 = 0
  rhs = 0xffffffffffffff7a = -134
PSA generate HMAC 224-bit not supported ........................... FAILED
  psa_generate_key( &attributes, &key_id ) == PSA_ERROR_NOT_SUPPORTED
  at line 44, ../source/tests/suites/test_suite_psa_crypto_not_supported.function
  lhs = 0x0000000000000000 = 0
  rhs = 0xffffffffffffff7a = -134
PSA import HMAC 256-bit not supported ............................. FAILED
  psa_import_key( &attributes, key_material->x, key_material->len, &key_id ) == PSA_ERROR_NOT_SUPPORTED
  at line 23, ../source/tests/suites/test_suite_psa_crypto_not_supported.function
  lhs = 0x0000000000000000 = 0
  rhs = 0xffffffffffffff7a = -134
PSA generate HMAC 256-bit not supported ........................... FAILED
  psa_generate_key( &attributes, &key_id ) == PSA_ERROR_NOT_SUPPORTED
  at line 44, ../source/tests/suites/test_suite_psa_crypto_not_supported.function
  lhs = 0x0000000000000000 = 0
  rhs = 0xffffffffffffff7a = -134
PSA import HMAC 384-bit not supported ............................. FAILED
  psa_import_key( &attributes, key_material->x, key_material->len, &key_id ) == PSA_ERROR_NOT_SUPPORTED
  at line 23, ../source/tests/suites/test_suite_psa_crypto_not_supported.function
  lhs = 0x0000000000000000 = 0
  rhs = 0xffffffffffffff7a = -134
PSA generate HMAC 384-bit not supported ........................... FAILED
  psa_generate_key( &attributes, &key_id ) == PSA_ERROR_NOT_SUPPORTED
  at line 44, ../source/tests/suites/test_suite_psa_crypto_not_supported.function
  lhs = 0x0000000000000000 = 0
  rhs = 0xffffffffffffff7a = -134
PSA import HMAC 512-bit not supported ............................. FAILED
  psa_import_key( &attributes, key_material->x, key_material->len, &key_id ) == PSA_ERROR_NOT_SUPPORTED
  at line 23, ../source/tests/suites/test_suite_psa_crypto_not_supported.function
  lhs = 0x0000000000000000 = 0
  rhs = 0xffffffffffffff7a = -134
PSA generate HMAC 512-bit not supported ........................... FAILED
  psa_generate_key( &attributes, &key_id ) == PSA_ERROR_NOT_SUPPORTED
  at line 44, ../source/tests/suites/test_suite_psa_crypto_not_supported.function
  lhs = 0x0000000000000000 = 0
  rhs = 0xffffffffffffff7a = -134
--
PSA import ECC_KEY_PAIR(SECP_R1) 256-bit type not supported ....... FAILED
  psa_import_key( &attributes, key_material->x, key_material->len, &key_id ) == PSA_ERROR_NOT_SUPPORTED
  at line 23, ../source/tests/suites/test_suite_psa_crypto_not_supported.function
  lhs = 0x0000000000000000 = 0
  rhs = 0xffffffffffffff7a = -134
--

In this configuration, there's also a unused-variable warning, and in test_suite_psa_crypto there's one failing test case due to an incorrect dependency:

PSA cipher setup: bad algorithm (unknown cipher algorithm) ........ FAILED
  ( psa_import_key( &attributes, key_bytes, key_length, &key ) ) == PSA_SUCCESS
  at line 189, ../source/tests/suites/test_suite_psa_crypto.function
  lhs = 0xffffffffffffff7a = -134
  rhs = 0x0000000000000000 = 0
daverodgman commented 1 year ago

Can reproduce with https://github.com/Mbed-TLS/mbedtls/commit/a745c7d4398b22ceae1ce21bfbcb2fa2f7fab4d3, but cannot reproduce with development - looks like this has been fixed.

Note: I also had to unset PSA_WANT_KEY_TYPE_HMAC to reproduce, i.e.

mtest MBEDTLS_PSA_CRYPTO_C MBEDTLS_PSA_CRYPTO_CONFIG MBEDTLS_AES_C MBEDTLS_CTR_DRBG_C MBEDTLS_ENTROPY_C PSA_WANT_ALG_SHA_224 PSA_WANT_ALG_SHA_256 PSA_WANT_KEY_TYPE_ECC_KEYPAIR PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY PSA_WANT_ECC_SECP_R1_256 PSA_WANT_ALG_ECDSA -PSA_WANT_KEY_TYPE_HMAC test_suite_psa_crypto test_suite_psa_crypto_not_supported.generated