Limit keys to 65528 bits
65528 bits is more than any reasonable key until we start supporting
post-quantum cryptography.
This limit is chosen to allow bit-sizes to be stored in 16 bits, with
65535 left to indicate an invalid value. It's a whole number of bytes,
which facilitates some calculations, in particular allowing a key of
exactly PSA_CRYPTO_MAX_STORAGE_SIZE to be created but not one bit
more.
As a resource usage limit, this is arguably too large, but that's out
of scope of the current commit.
Test that key import, generation and derivation reject overly large
sizes.
diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function
index 887ff84..8ed7a7d 100644
--- a/tests/suites/test_suite_psa_crypto.function
+++ b/tests/suites/test_suite_psa_crypto.function
@@ -7,6 +7,13 @@
#include "psa_crypto_helpers.h"
+/* Tests that require more than 128kB of RAM plus change have this symbol
+ * as a dependency. Currently we always define this symbol, so the tests
+ * are always executed. In the future we should make this conditional
+ * so that tests that require a lot of memory are skipped on constrained
+ * platforms. */
+#define HAVE_RAM_AVAILABLE_128k
+
/** An invalid export length that will never be set by psa_export_key(). */
static const size_t INVALID_EXPORT_LENGTH = ~0U;
@@ -556,7 +563,8 @@
TEST_ASSERT( ! "Key derivation algorithm not supported" );
}
- PSA_ASSERT( psa_key_derivation_set_capacity( operation, capacity ) );
+ if( capacity != SIZE_MAX )
+ PSA_ASSERT( psa_key_derivation_set_capacity( operation, capacity ) );
return( 1 );
@@ -1238,6 +1246,54 @@
/* END_CASE */
/* BEGIN_CASE */
+void import_large_key( int type_arg, int byte_size_arg,
+ int expected_status_arg )
+{
+ psa_key_type_t type = type_arg;
+ size_t byte_size = byte_size_arg;
+ psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
+ psa_status_t expected_status = expected_status_arg;
+ psa_key_handle_t handle = 0;
+ psa_status_t status;
+ uint8_t *buffer = NULL;
+ size_t buffer_size = byte_size + 1;
+ size_t n;
+
+ /* It would be better to skip the test than fail it if the allocation
+ * fails, but the test framework doesn't support this yet. */
+ ASSERT_ALLOC( buffer, buffer_size );
+ memset( buffer, 'K', byte_size );
+
+ PSA_ASSERT( psa_crypto_init( ) );
+
+ /* Try importing the key */
+ psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
+ psa_set_key_type( &attributes, type );
+ status = psa_import_key( &attributes, buffer, byte_size, &handle );
+ TEST_EQUAL( status, expected_status );
+
+ if( status == PSA_SUCCESS )
+ {
+ PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
+ TEST_EQUAL( psa_get_key_type( &attributes ), type );
+ TEST_EQUAL( psa_get_key_bits( &attributes ),
+ PSA_BYTES_TO_BITS( byte_size ) );
+ memset( buffer, 0, byte_size + 1 );
+ PSA_ASSERT( psa_export_key( handle, buffer, byte_size, &n ) );
+ for( n = 0; n < byte_size; n++ )
+ TEST_EQUAL( buffer[n], 'K' );
+ for( n = byte_size; n < buffer_size; n++ )
+ TEST_EQUAL( buffer[n], 0 );
+ }
+
+exit:
+ psa_destroy_key( handle );
+ PSA_DONE( );
+ mbedtls_free( buffer );
+}
+/* END_CASE */
+
+/* BEGIN_CASE */
void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
{
psa_key_handle_t handle = 0;
@@ -4564,6 +4620,50 @@
/* END_CASE */
/* BEGIN_CASE */
+void derive_large_key( int alg_arg,
+ data_t *key_data, data_t *input1, data_t *input2,
+ int bits_arg,
+ int expected_status_arg )
+{
+ psa_key_handle_t base_handle = 0;
+ psa_key_handle_t derived_handle = 0;
+ psa_algorithm_t alg = alg_arg;
+ size_t bits = bits_arg;
+ psa_status_t expected_status = expected_status_arg;
+ psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
+ psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
+ psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
+
+ PSA_ASSERT( psa_crypto_init( ) );
+
+ psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
+ psa_set_key_algorithm( &base_attributes, alg );
+ psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
+ PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
+ &base_handle ) );
+
+ if( !setup_key_derivation_wrap( &operation, base_handle, alg,
+ input1->x, input1->len,
+ input2->x, input2->len, SIZE_MAX ) )
+ goto exit;
+
+ psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
+ psa_set_key_algorithm( &derived_attributes, 0 );
+ psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
+ psa_set_key_bits( &derived_attributes, bits );
+ TEST_EQUAL( psa_key_derivation_output_key( &derived_attributes, &operation,
+ &derived_handle ),
+ expected_status );
+
+exit:
+ psa_key_derivation_abort( &operation );
+ psa_destroy_key( base_handle );
+ psa_destroy_key( derived_handle );
+ PSA_DONE( );
+}
+/* END_CASE */
+
+/* BEGIN_CASE */
void key_agreement_setup( int alg_arg,
int our_key_type_arg, data_t *our_key_data,
data_t *peer_key_data,