Skip to content

Commit 3cb715b

Browse files
Declare key binding functions
Declare functions to wrap and unwrap a key together with its metadata, in an unspecified vendor-specific format but with an explicitly specified key. Declare a corresponding usage flag.
1 parent da79e92 commit 3cb715b

File tree

3 files changed

+167
-0
lines changed

3 files changed

+167
-0
lines changed

include/psa/crypto.h

+129
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,135 @@ psa_status_t psa_export_public_key(psa_key_handle_t handle,
854854
size_t data_size,
855855
size_t *data_length);
856856

857+
/**
858+
* \brief Export a key and its metadata in wrapped form.
859+
*
860+
* A wrapped form of the key object preserves the confidentiality and
861+
* authenticity of the key material and the authenticity of the key
862+
* policy. In practical terms, the key material is encrypted, and
863+
* the key data and metadata are authenticated together.
864+
*
865+
* The format of the wrapped data is implementation-dependent. It may depend
866+
* both on the choice of wrapping key and on the type of key to wrap.
867+
*
868+
* The policy on the key must have the usage flag
869+
* #PSA_KEY_USAGE_EXPORT_WRAPPED set.
870+
*
871+
* \param wrapping_key Handle to the key to wrap with.
872+
* \param handle Handle to the key to export in wrapped form.
873+
* \param[out] data Buffer where the wrapped key data is to be written.
874+
* \param data_size Size of the \p data buffer in bytes.
875+
* \param[out] data_length On success, the number of bytes
876+
* that make up the wrapped key data.
877+
*
878+
* \retval #PSA_SUCCESS
879+
* \retval #PSA_ERROR_INVALID_HANDLE
880+
* One or both of \p handle and \p wrapping_key is not a valid
881+
* handle to a key.
882+
* \retval #PSA_ERROR_NOT_PERMITTED
883+
* The key \p handle does not have the #PSA_KEY_USAGE_BACKUP flag.
884+
* \retval #PSA_ERROR_INVALID_ARGUMENT
885+
* \p wrapping_key does not support wrapping keys with metadata.
886+
* \retval #PSA_ERROR_NOT_SUPPORTED
887+
* \p wrapping_key does not support wrapping the key designated
888+
* by \p handle.
889+
* \retval #PSA_ERROR_BUFFER_TOO_SMALL
890+
* The size of the \p data buffer is too small. You can determine a
891+
* sufficient buffer size by calling
892+
* #PSA_WRAP_KEY_WITH_POLICY_OUTPUT_SIZE(\c type, \c bits)
893+
* where \c type is the key type of \p handle
894+
* and \c bits is the key size of \p handle in bits.
895+
* \retval #PSA_ERROR_COMMUNICATION_FAILURE
896+
* \retval #PSA_ERROR_HARDWARE_FAILURE
897+
* \retval #PSA_ERROR_CORRUPTION_DETECTED
898+
* \retval #PSA_ERROR_STORAGE_FAILURE
899+
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
900+
* \retval #PSA_ERROR_BAD_STATE
901+
* The library has not been previously initialized by psa_crypto_init().
902+
* It is implementation-dependent whether a failure to initialize
903+
* results in this error code.
904+
*/
905+
psa_status_t psa_wrap_key_with_policy(psa_key_handle_t wrapping_key,
906+
psa_key_handle_t handle,
907+
uint8_t *data,
908+
size_t data_size,
909+
size_t *data_length);
910+
911+
/**
912+
* \brief Import a wrapped key with its metadata.
913+
*
914+
* This function supports any output from psa_wrap_key_with_policy().
915+
* For symmetric wrapping, you must unwrap with the same key that was
916+
* used to wrap. For asymmetric wrapping where the wrapping operation uses
917+
* a public key and the unwrapping operation uses the corresponding private
918+
* key, you must unwrap with the corresponding unwrapping key.
919+
*
920+
* \param wrapping_key Handle to the key to unwrap with.
921+
* \param[in] attributes The attributes for the new key.
922+
* They are used as follows:
923+
* - The key type and size may be 0. If either is
924+
* nonzero, it must match the corresponding
925+
* attribute of the wrapped key data.
926+
* - The key location (the lifetime and, for
927+
* persistent keys, the key identifier) is
928+
* used directly.
929+
* If the wrapped key does not have the usage
930+
* flag #PSA_KEY_USAGE_COPY, then the location
931+
* must match the location embedded in \p data.
932+
* If the wrapped key has the usage
933+
* flag #PSA_KEY_USAGE_COPY, then the location
934+
* embedded in \p data is ignored.
935+
* - The policy constraints (usage flags and
936+
* algorithm policy) are combined from
937+
* the wrapped key data and \p attributes so that
938+
* both sets of restrictions apply. The
939+
* policy restrictions are calculated in the
940+
* same way as in psa_copy_key().
941+
* \param[in] data Buffer containing the wrapped key material.
942+
* The expected format of this buffer depends
943+
* on the wrapping key.
944+
* \param data_length Size of the \p data buffer in bytes.
945+
* \param[out] handle On success, a handle to the newly created key.
946+
* \c 0 on failure.
947+
*
948+
* \retval #PSA_SUCCESS
949+
* Success.
950+
* If the unwrapped key is persistent, the key material and the
951+
* key's metadata have been saved to persistent storage.
952+
* \retval #PSA_ERROR_ALREADY_EXISTS
953+
* This is an attempt to create a persistent key, and there is
954+
* already a persistent key with the given identifier.
955+
* \retval #PSA_ERROR_INVALID_ARGUMENT
956+
* The key attributes, as a whole, are invalid.
957+
* \retval #PSA_ERROR_INVALID_ARGUMENT
958+
* The key data is not correctly formatted.
959+
* \retval #PSA_ERROR_INVALID_ARGUMENT
960+
* The size in \p attributes is nonzero and does not match the size
961+
* of the key data.
962+
* \retval #PSA_ERROR_INVALID_ARGUMENT
963+
* \p wrapping_key does not support unwrapping keys with metadata.
964+
* \retval #PSA_ERROR_INVALID_SIGNATURE
965+
* \p data is not a valid wrapped key for \p wrapping_key.
966+
* \retval #PSA_ERROR_NOT_SUPPORTED
967+
* Some of the metadata in either \p attributes or \p data is
968+
* not supported.
969+
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
970+
* \retval #PSA_ERROR_INSUFFICIENT_STORAGE
971+
* \retval #PSA_ERROR_COMMUNICATION_FAILURE
972+
* \retval #PSA_ERROR_STORAGE_FAILURE
973+
* \retval #PSA_ERROR_HARDWARE_FAILURE
974+
* \retval #PSA_ERROR_CORRUPTION_DETECTED
975+
* \retval #PSA_ERROR_BAD_STATE
976+
* The library has not been previously initialized by psa_crypto_init().
977+
* It is implementation-dependent whether a failure to initialize
978+
* results in this error code.
979+
*/
980+
psa_status_t psa_unwrap_key_with_policy(psa_key_handle_t wrapping_key,
981+
const psa_key_attributes_t *attributes,
982+
const uint8_t *data,
983+
size_t data_length,
984+
psa_key_handle_t *handle);
985+
857986

858987

859988
/**@}*/

include/psa/crypto_sizes.h

+25
Original file line numberDiff line numberDiff line change
@@ -659,4 +659,29 @@
659659
PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
660660
0)
661661

662+
/** Sufficient output buffer size for psa_wrap_key_with_policy().
663+
*
664+
* This macro returns a compile-time constant if its arguments are
665+
* compile-time constants.
666+
*
667+
* \warning This function may call its arguments multiple times or
668+
* zero times, so you should not pass arguments that contain
669+
* side effects.
670+
*
671+
* \param key_type A supported key type.
672+
* \param key_bits The size of the key in bits.
673+
*
674+
* \return If the parameters are valid and supported, return
675+
* a buffer size in bytes that guarantees that
676+
* psa_wrap_key_with_policy() will not fail with
677+
* #PSA_ERROR_BUFFER_TOO_SMALL.
678+
* If the parameters are a valid combination that is not supported
679+
* by the implementation, this macro shall return either a
680+
* sensible size or 0.
681+
* If the parameters are not valid, the
682+
* return value is unspecified.
683+
*/
684+
#define PSA_WRAP_KEY_WITH_POLICY_OUTPUT_SIZE(key_type, key_bits) \
685+
0u /*not implemented yet*/
686+
662687
#endif /* PSA_CRYPTO_SIZES_H */

include/psa/crypto_values.h

+13
Original file line numberDiff line numberDiff line change
@@ -1606,6 +1606,19 @@
16061606
*/
16071607
#define PSA_KEY_USAGE_COPY ((psa_key_usage_t)0x00000002)
16081608

1609+
/** Whether the key object may be saved outside the device in wrapped form.
1610+
* This is also known as key binding.
1611+
*
1612+
* This flag allows the use of psa_wrap_key_with_policy() on the given key,
1613+
* with any suitable wrapping key.
1614+
*
1615+
* A wrapped form of the key object preserves the confidentiality and
1616+
* authenticity of the key material and the authenticity of the key
1617+
* policy. In practical terms, the key material is encrypted, and
1618+
* the key data and metadata are authenticated together.
1619+
*/
1620+
#define PSA_KEY_USAGE_BACKUP ((psa_key_usage_t)0x00000020)
1621+
16091622
/** Whether the key may be used to encrypt a message.
16101623
*
16111624
* This flag allows the key to be used for a symmetric encryption operation,

0 commit comments

Comments
 (0)