Skip to content

Commit 1801740

Browse files
SE driver: report the bit size on key import
Add a parameter to the key import method of a secure element driver to make it report the key size in bits. This is necessary (otherwise the core has no idea what the bit-size is), and making import report it is easier than adding a separate method (for other key creation methods, this information is an input, not an output).
1 parent dc5bfe9 commit 1801740

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

include/psa/crypto_se_driver.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -833,14 +833,18 @@ typedef psa_status_t (*psa_drv_se_allocate_key_t)(
833833
*
834834
* \param[in,out] drv_context The driver context structure.
835835
* \param[in] key_slot Slot where the key will be stored
836-
* This must be a valid slot for a key of the chosen
837-
* type. It must be unoccupied.
836+
* This must be a valid slot for a key of the
837+
* chosen type. It must be unoccupied.
838838
* \param[in] lifetime The required lifetime of the key storage
839839
* \param[in] type Key type (a \c PSA_KEY_TYPE_XXX value)
840840
* \param[in] algorithm Key algorithm (a \c PSA_ALG_XXX value)
841841
* \param[in] usage The allowed uses of the key
842842
* \param[in] p_data Buffer containing the key data
843843
* \param[in] data_length Size of the `data` buffer in bytes
844+
* \param[out] bits On success, the key size in bits. The driver
845+
* must determine this value after parsing the
846+
* key according to the key type.
847+
* This value is not used if the function fails.
844848
*
845849
* \retval #PSA_SUCCESS
846850
* Success.
@@ -852,7 +856,8 @@ typedef psa_status_t (*psa_drv_se_import_key_t)(psa_drv_se_context_t *drv_contex
852856
psa_algorithm_t algorithm,
853857
psa_key_usage_t usage,
854858
const uint8_t *p_data,
855-
size_t data_length);
859+
size_t data_length,
860+
size_t *bits);
856861

857862
/**
858863
* \brief A function that destroys a secure element key and restore the slot to

library/psa_crypto.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,19 +1711,19 @@ psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
17111711
psa_get_se_driver_context( driver ),
17121712
slot->data.se.slot_number,
17131713
slot->lifetime, slot->type, slot->policy.alg, slot->policy.usage,
1714-
data, data_length );
1715-
/* TOnogrepDO: psa_check_key_slot_attributes? */
1714+
data, data_length,
1715+
&slot->data.se.bits );
17161716
}
17171717
else
17181718
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
17191719
{
17201720
status = psa_import_key_into_slot( slot, data, data_length );
17211721
if( status != PSA_SUCCESS )
17221722
goto exit;
1723-
status = psa_check_key_slot_attributes( slot, attributes );
1724-
if( status != PSA_SUCCESS )
1725-
goto exit;
17261723
}
1724+
status = psa_check_key_slot_attributes( slot, attributes );
1725+
if( status != PSA_SUCCESS )
1726+
goto exit;
17271727

17281728
status = psa_finish_key_creation( slot, driver );
17291729
exit:

tests/suites/test_suite_psa_crypto_se_driver_hal.function

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ static psa_status_t null_import( psa_drv_se_context_t *context,
6262
psa_algorithm_t algorithm,
6363
psa_key_usage_t usage,
6464
const uint8_t *p_data,
65-
size_t data_length )
65+
size_t data_length,
66+
size_t *bits )
6667
{
6768
(void) context;
6869
(void) slot_number;
@@ -71,7 +72,9 @@ static psa_status_t null_import( psa_drv_se_context_t *context,
7172
(void) algorithm;
7273
(void) usage;
7374
(void) p_data;
74-
(void) data_length;
75+
/* We're supposed to return a key size. Return one that's correct for
76+
* plain data keys. */
77+
*bits = PSA_BYTES_TO_BITS( data_length );
7578
return( PSA_SUCCESS );
7679
}
7780

@@ -110,7 +113,8 @@ static psa_status_t ram_import( psa_drv_se_context_t *context,
110113
psa_algorithm_t algorithm,
111114
psa_key_usage_t usage,
112115
const uint8_t *p_data,
113-
size_t data_length )
116+
size_t data_length,
117+
size_t *bits )
114118
{
115119
(void) context;
116120
DRIVER_ASSERT( slot_number < ARRAY_LENGTH( ram_slots ) );
@@ -119,6 +123,7 @@ static psa_status_t ram_import( psa_drv_se_context_t *context,
119123
ram_slots[slot_number].lifetime = lifetime;
120124
ram_slots[slot_number].type = type;
121125
ram_slots[slot_number].bits = PSA_BYTES_TO_BITS( data_length );
126+
*bits = PSA_BYTES_TO_BITS( data_length );
122127
(void) algorithm;
123128
(void) usage;
124129
memcpy( ram_slots[slot_number].content, p_data, data_length );

0 commit comments

Comments
 (0)