Skip to content

Commit fa0b197

Browse files
Key unwrap with metadata: separate function to unwrap to volatile
Using attributes with psa_key_unwrap_with_policy() is inconvenient. When unwrapping a key to its original location (a straightforward restore from backup), there's no reason to specify any attributes. When unwrapping a key in a different location and possibly restricting its policy, the new attributes are generally a refinement of the old ones. Remove the attributes argument from psa_key_unwrap_with_policy() and provide another function to unwrap to a volatile location. The application can then use existing functions to manipulate the key, including psa_get_key_attributes() and psa_copy_key().
1 parent d29bc2d commit fa0b197

File tree

2 files changed

+95
-29
lines changed

2 files changed

+95
-29
lines changed

include/psa/crypto.h

+88-28
Original file line numberDiff line numberDiff line change
@@ -921,27 +921,12 @@ psa_status_t psa_wrap_key_with_policy(psa_key_handle_t wrapping_key,
921921
* a public key and the unwrapping operation uses the corresponding private
922922
* key, you must unwrap with the corresponding unwrapping key.
923923
*
924+
* The resulting key object has the same attributes as the original key:
925+
* same lifetime, same identifier if persistent, same usage policy.
926+
* To restore a key to a different location or with a different policy,
927+
* use psa_unwrap_key_to_alternate_lifetime().
928+
*
924929
* \param wrapping_key Handle to the key to unwrap with.
925-
* \param[in] attributes The attributes for the new key.
926-
* They are used as follows:
927-
* - The key type and size may be 0. If either is
928-
* nonzero, it must match the corresponding
929-
* attribute of the wrapped key data.
930-
* - The key location (the lifetime and, for
931-
* persistent keys, the key identifier) is
932-
* used directly.
933-
* If the wrapped key does not have the usage
934-
* flag #PSA_KEY_USAGE_COPY, then the location
935-
* must match the location embedded in \p data.
936-
* If the wrapped key has the usage
937-
* flag #PSA_KEY_USAGE_COPY, then the location
938-
* embedded in \p data is ignored.
939-
* - The policy constraints (usage flags and
940-
* algorithm policy) are combined from
941-
* the wrapped key data and \p attributes so that
942-
* both sets of restrictions apply. The
943-
* policy restrictions are calculated in the
944-
* same way as in psa_copy_key().
945930
* \param[in] data Buffer containing the wrapped key material.
946931
* The expected format of this buffer depends
947932
* on the wrapping key.
@@ -957,19 +942,16 @@ psa_status_t psa_wrap_key_with_policy(psa_key_handle_t wrapping_key,
957942
* This is an attempt to create a persistent key, and there is
958943
* already a persistent key with the given identifier.
959944
* \retval #PSA_ERROR_INVALID_ARGUMENT
960-
* The key attributes, as a whole, are invalid.
961-
* \retval #PSA_ERROR_INVALID_ARGUMENT
962945
* The key data is not correctly formatted.
963946
* \retval #PSA_ERROR_INVALID_ARGUMENT
964-
* The size in \p attributes is nonzero and does not match the size
965-
* of the key data.
966-
* \retval #PSA_ERROR_INVALID_ARGUMENT
967947
* \p wrapping_key does not support unwrapping keys with metadata.
968948
* \retval #PSA_ERROR_INVALID_SIGNATURE
969949
* \p data is not a valid wrapped key for \p wrapping_key.
970950
* \retval #PSA_ERROR_NOT_SUPPORTED
971-
* Some of the metadata in either \p attributes or \p data is
972-
* not supported.
951+
* Some of the metadata encoded in \p data is not supported.
952+
* This can only happen when attempting to unwrap a key that
953+
* was wrapped under a different implementation or a
954+
* differently-configured implementation.
973955
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
974956
* \retval #PSA_ERROR_INSUFFICIENT_STORAGE
975957
* \retval #PSA_ERROR_COMMUNICATION_FAILURE
@@ -982,11 +964,89 @@ psa_status_t psa_wrap_key_with_policy(psa_key_handle_t wrapping_key,
982964
* results in this error code.
983965
*/
984966
psa_status_t psa_unwrap_key_with_policy(psa_key_handle_t wrapping_key,
985-
const psa_key_attributes_t *attributes,
986967
const uint8_t *data,
987968
size_t data_length,
988969
psa_key_handle_t *handle);
989970

971+
/**
972+
* \brief Import a wrapped key with its metadata to volatile memory.
973+
*
974+
* This function supports any output from psa_wrap_key_with_policy()
975+
* where the wrapped key has the usage flag #PSA_KEY_USAGE_COPY set.
976+
* For symmetric wrapping, you must unwrap with the same key that was
977+
* used to wrap. For asymmetric wrapping where the wrapping operation uses
978+
* a public key and the unwrapping operation uses the corresponding private
979+
* key, you must unwrap with the corresponding unwrapping key.
980+
*
981+
* The resulting key object has the same attributes as the original key
982+
* apart from its lifetime, in particular it has the same usage policy.
983+
* To unwrap a key and give it a more restrictive policy or place it
984+
* in a different location:
985+
* -# Call psa_unwrap_key_to_alternate_lifetime() to unwrap the key to a
986+
* volatile location.
987+
* -# If desired, call psa_get_key_attributes() to retrieve the
988+
* attributes of the key, then modify them as desired for the
989+
* subsequent copy.
990+
* -# Call psa_copy_key() to copy the key to its desired location
991+
* (volatile or persistent) and with the desired policy.
992+
* -# Call psa_destroy_key() to destroy the intermediate volatile key.
993+
*
994+
* The resulting key does not have a persistent identifier, therefore
995+
* the target lifetime must be volatile.
996+
*
997+
* \param wrapping_key Handle to the key to unwrap with.
998+
* \param lifetime The lifetime to unwrap to.
999+
* This must be a volatile lifetime.
1000+
* The wrapping key may constrain which lifetimes
1001+
* are permitted; such constraints are
1002+
* implementation-defined. As a guideline,
1003+
* implementations should allow unwrapping a key
1004+
* to a lifetime that is in the same location
1005+
* as the original key.
1006+
* \param[in] data Buffer containing the wrapped key material.
1007+
* The expected format of this buffer depends
1008+
* on the wrapping key.
1009+
* \param data_length Size of the \p data buffer in bytes.
1010+
* \param[out] handle On success, a handle to the newly created key.
1011+
* \c 0 on failure.
1012+
*
1013+
* \retval #PSA_SUCCESS
1014+
* Success.
1015+
* If the unwrapped key is persistent, the key material and the
1016+
* key's metadata have been saved to persistent storage.
1017+
* \retval #PSA_ERROR_INVALID_ARGUMENT
1018+
* \p lifetime is not a volatile lifetime.
1019+
* \retval #PSA_ERROR_INVALID_ARGUMENT
1020+
* The key data is not correctly formatted.
1021+
* \retval #PSA_ERROR_INVALID_ARGUMENT
1022+
* \p wrapping_key does not support unwrapping keys with metadata.
1023+
* \retval #PSA_ERROR_INVALID_SIGNATURE
1024+
* \p data is not a valid wrapped key for \p wrapping_key.
1025+
* \retval #PSA_ERROR_NOT_SUPPORTED
1026+
* Some of the metadata encoded in \p data is not supported.
1027+
* This can only happen when attempting to unwrap a key that
1028+
* was wrapped under a different implementation or a
1029+
* differently-configured implementation.
1030+
* \retval #PSA_ERROR_NOT_PERMITTED
1031+
* The wrapped key does not have the #PSA_KEY_USAGE_COPY flag.
1032+
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1033+
* \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1034+
* \retval #PSA_ERROR_COMMUNICATION_FAILURE
1035+
* \retval #PSA_ERROR_STORAGE_FAILURE
1036+
* \retval #PSA_ERROR_HARDWARE_FAILURE
1037+
* \retval #PSA_ERROR_CORRUPTION_DETECTED
1038+
* \retval #PSA_ERROR_BAD_STATE
1039+
* The library has not been previously initialized by psa_crypto_init().
1040+
* It is implementation-dependent whether a failure to initialize
1041+
* results in this error code.
1042+
*/
1043+
psa_status_t psa_unwrap_key_to_alternate_lifetime(
1044+
psa_key_handle_t wrapping_key,
1045+
psa_key_lifetime_t lifetime,
1046+
const uint8_t *data,
1047+
size_t data_length,
1048+
psa_key_handle_t *handle);
1049+
9901050
/**
9911051
* \brief Export key material in wrapped form.
9921052
*

include/psa/crypto_values.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -1649,6 +1649,9 @@
16491649
* For keys with the lifetime #PSA_KEY_LIFETIME_VOLATILE or
16501650
* #PSA_KEY_LIFETIME_PERSISTENT, the usage flag #PSA_KEY_USAGE_COPY
16511651
* is sufficient to permit the copy.
1652+
*
1653+
* When combined with #PSA_KEY_USAGE_BACKUP, this flag allows the use
1654+
* of psa_unwrap_key_to_alternate_lifetime() on a wrapped key data.
16521655
*/
16531656
#define PSA_KEY_USAGE_COPY ((psa_key_usage_t)0x00000002)
16541657

@@ -1679,7 +1682,10 @@
16791682
* This is also known as key binding.
16801683
*
16811684
* This flag allows the use of psa_wrap_key_with_policy() on the given key,
1682-
* with any suitable wrapping key.
1685+
* with any suitable wrapping key, and the subsequent use of
1686+
* psa_unwrap_key_with_policy() on the resulting data.
1687+
* When combined with #PSA_KEY_USAGE_COPY, this flag also allows the use
1688+
* of psa_unwrap_key_to_alternate_lifetime() to unwrap the resulting data.
16831689
*
16841690
* A wrapped form of the key object preserves the confidentiality and
16851691
* authenticity of the key material and the authenticity of the key

0 commit comments

Comments
 (0)