Skip to content

Commit 341ab2c

Browse files
Declare key material wrapping functions
Declare functions to wrap and unwrap key material in standard formats. Declare an algorithm category for key wrapping. Declare a corresponding usage flag.
1 parent ec1b1ba commit 341ab2c

File tree

3 files changed

+170
-0
lines changed

3 files changed

+170
-0
lines changed

include/psa/crypto.h

+109
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,116 @@ psa_status_t psa_unwrap_key_with_policy(psa_key_handle_t wrapping_key,
987987
size_t data_length,
988988
psa_key_handle_t *handle);
989989

990+
/**
991+
* \brief Export key material in wrapped form.
992+
*
993+
* A wrapped form of the key material preserves the confidentiality
994+
* and authenticity of the key material. In practical terms, the key
995+
* material is encrypted and authenticated.
996+
*
997+
* The policy on the key must have the usage flag
998+
* #PSA_KEY_USAGE_EXPORT_WRAPPED set.
999+
*
1000+
* \param wrapping_key Handle to the key to wrap with.
1001+
* \param alg The key wrapping algorithm to compute
1002+
* (\c PSA_ALG_XXX value such that
1003+
* #PSA_ALG_IS_KEY_WRAP(\p alg) is true).
1004+
* \param handle Handle to the key whose material is to be
1005+
* exported in wrapped form.
1006+
* \param[out] data Buffer where the wrapped key data is to be written.
1007+
* \param data_size Size of the \p data buffer in bytes.
1008+
* \param[out] data_length On success, the number of bytes
1009+
* that make up the wrapped key data.
1010+
*
1011+
* \retval #PSA_SUCCESS
1012+
* \retval #PSA_ERROR_INVALID_HANDLE
1013+
* One or both of \p handle and \p wrapping_key is not a valid
1014+
* handle to a key.
1015+
* \retval #PSA_ERROR_NOT_PERMITTED
1016+
* The key \p handle does not have the #PSA_KEY_USAGE_BACKUP flag.
1017+
* \retval #PSA_ERROR_INVALID_ARGUMENT
1018+
* \p wrapping_key does not support wrapping key material.
1019+
* \retval #PSA_ERROR_NOT_SUPPORTED
1020+
* \p wrapping_key does not support wrapping the key designated
1021+
* by \p handle.
1022+
* \retval #PSA_ERROR_BUFFER_TOO_SMALL
1023+
* The size of the \p data buffer is too small. You can determine a
1024+
* sufficient buffer size by calling
1025+
* #PSA_WRAP_KEY_MATERIAL_OUTPUT_SIZE(\p alg, \c type, \c bits)
1026+
* where \c type is the key type of \p handle
1027+
* and \c bits is the key size of \p handle in bits.
1028+
* \retval #PSA_ERROR_COMMUNICATION_FAILURE
1029+
* \retval #PSA_ERROR_HARDWARE_FAILURE
1030+
* \retval #PSA_ERROR_CORRUPTION_DETECTED
1031+
* \retval #PSA_ERROR_STORAGE_FAILURE
1032+
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1033+
* \retval #PSA_ERROR_BAD_STATE
1034+
* The library has not been previously initialized by psa_crypto_init().
1035+
* It is implementation-dependent whether a failure to initialize
1036+
* results in this error code.
1037+
*/
1038+
psa_status_t psa_wrap_key_material(psa_key_handle_t wrapping_key,
1039+
psa_algorithm_t alg,
1040+
psa_key_handle_t handle,
1041+
uint8_t *data,
1042+
size_t data_size,
1043+
size_t *data_length);
9901044

1045+
/**
1046+
* \brief Import wrapped key material.
1047+
*
1048+
* \param wrapping_key Handle to the key to unwrap with.
1049+
* \param alg The key unwrapping algorithm to compute
1050+
* (\c PSA_ALG_XXX value such that
1051+
* #PSA_ALG_IS_KEY_WRAP(\p alg) is true).
1052+
* \param[in] attributes The attributes for the new key.
1053+
* They are used in the same way as by
1054+
* psa_import_key().
1055+
* \param[in] data Buffer containing the key data. The expected
1056+
* format of this buffer depends on the wrapping
1057+
* key and on the type declared in \p attributes.
1058+
* \param data_length Size of the \p data buffer in bytes.
1059+
* \param[out] handle On success, a handle to the newly created key.
1060+
* \c 0 on failure.
1061+
*
1062+
* \retval #PSA_SUCCESS
1063+
* Success.
1064+
* If the unwrapped key is persistent, the key material and the
1065+
* key's metadata have been saved to persistent storage.
1066+
* \retval #PSA_ERROR_ALREADY_EXISTS
1067+
* This is an attempt to create a persistent key, and there is
1068+
* already a persistent key with the given identifier.
1069+
* \retval #PSA_ERROR_INVALID_ARGUMENT
1070+
* The key attributes, as a whole, are invalid.
1071+
* \retval #PSA_ERROR_INVALID_ARGUMENT
1072+
* The key data is not correctly formatted.
1073+
* \retval #PSA_ERROR_INVALID_ARGUMENT
1074+
* The size in \p attributes is nonzero and does not match the size
1075+
* of the key data.
1076+
* \retval #PSA_ERROR_INVALID_ARGUMENT
1077+
* \p wrapping_key does not support unwrapping keys with metadata.
1078+
* \retval #PSA_ERROR_INVALID_SIGNATURE
1079+
* \p data is not a valid wrapped key for \p wrapping_key.
1080+
* \retval #PSA_ERROR_NOT_SUPPORTED
1081+
* The key type or key size is not supported, either by the
1082+
* implementation in general or in this particular persistent location.
1083+
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1084+
* \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1085+
* \retval #PSA_ERROR_COMMUNICATION_FAILURE
1086+
* \retval #PSA_ERROR_STORAGE_FAILURE
1087+
* \retval #PSA_ERROR_HARDWARE_FAILURE
1088+
* \retval #PSA_ERROR_CORRUPTION_DETECTED
1089+
* \retval #PSA_ERROR_BAD_STATE
1090+
* The library has not been previously initialized by psa_crypto_init().
1091+
* It is implementation-dependent whether a failure to initialize
1092+
* results in this error code.
1093+
*/
1094+
psa_status_t psa_unwrap_key_material(psa_key_handle_t wrapping_key,
1095+
psa_algorithm_t alg,
1096+
const psa_key_attributes_t *attributes,
1097+
const uint8_t *data,
1098+
size_t data_length,
1099+
psa_key_handle_t *handle);
9911100

9921101
/**@}*/
9931102

include/psa/crypto_sizes.h

+26
Original file line numberDiff line numberDiff line change
@@ -684,4 +684,30 @@
684684
#define PSA_WRAP_KEY_WITH_POLICY_OUTPUT_SIZE(key_type, key_bits) \
685685
0u /*not implemented yet*/
686686

687+
/** Sufficient output buffer size for psa_wrap_key_material().
688+
*
689+
* This macro returns a compile-time constant if its arguments are
690+
* compile-time constants.
691+
*
692+
* \warning This function may call its arguments multiple times or
693+
* zero times, so you should not pass arguments that contain
694+
* side effects.
695+
*
696+
* \param alg The key wrapping algorithm.
697+
* \param key_type A supported key type.
698+
* \param key_bits The size of the key in bits.
699+
*
700+
* \return If the parameters are valid and supported, return
701+
* a buffer size in bytes that guarantees that
702+
* psa_wrap_key_material() will not fail with
703+
* #PSA_ERROR_BUFFER_TOO_SMALL.
704+
* If the parameters are a valid combination that is not supported
705+
* by the implementation, this macro shall return either a
706+
* sensible size or 0.
707+
* If the parameters are not valid, the
708+
* return value is unspecified.
709+
*/
710+
#define PSA_WRAP_KEY_MATERIAL_OUTPUT_SIZE(alg, key_type, key_bits) \
711+
0u /*not implemented yet*/
712+
687713
#endif /* PSA_CRYPTO_SIZES_H */

include/psa/crypto_values.h

+35
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,7 @@
615615
#define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x02000000)
616616
#define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000)
617617
#define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x06000000)
618+
#define PSA_ALG_CATEGORY_WRAP ((psa_algorithm_t)0x0e000000)
618619
#define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x10000000)
619620
#define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x12000000)
620621
#define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x20000000)
@@ -672,6 +673,17 @@
672673
#define PSA_ALG_IS_AEAD(alg) \
673674
(((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
674675

676+
/** Whether the specified algorithm is a key wrapping algorithm.
677+
*
678+
* \param alg An algorithm identifier (value of type #psa_algorithm_t).
679+
*
680+
* \return 1 if \p alg is a key wrapping algorithm, 0 otherwise.
681+
* This macro may return either 0 or 1 if \p alg is not a supported
682+
* algorithm identifier.
683+
*/
684+
#define PSA_ALG_IS_KEY_WRAP(alg) \
685+
(((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_WRAP)
686+
675687
/** Whether the specified algorithm is a public-key signature algorithm.
676688
*
677689
* \param alg An algorithm identifier (value of type #psa_algorithm_t).
@@ -1622,6 +1634,29 @@
16221634
*/
16231635
#define PSA_KEY_USAGE_COPY ((psa_key_usage_t)0x00000002)
16241636

1637+
/** Whether the key material may be exported in wrapped form.
1638+
*
1639+
* This flag allows the use of psa_wrap_key_material() on the given key,
1640+
* with any suitable wrapping key.
1641+
*
1642+
* A wrapped form of the key material preserves the confidentiality
1643+
* and authenticity of the key material. In practical terms, the key
1644+
* material is encrypted and authenticated.
1645+
*
1646+
* A wrapped form does not preserve the key metadata in general, so it is up
1647+
* to the application to ensure that the key is unwrapped with the correct
1648+
* type and policy.
1649+
*
1650+
* \note Given that a wrapped key can be unwrapped with a different policy,
1651+
* which may include #PSA_KEY_USAGE_EXPORT, there is often no
1652+
* security advantage to allowing the key usage
1653+
* #PSA_KEY_USAGE_EXPORT_WRAPPED but not #PSA_KEY_USAGE_EXPORT.
1654+
* However implementations may impose restrictions on keys that
1655+
* are created through unwrapping with a particular key, in which
1656+
* case unwrapping could preserve the non-extractibility of a key.
1657+
*/
1658+
#define PSA_KEY_USAGE_EXPORT_WRAPPED ((psa_key_usage_t)0x00000010)
1659+
16251660
/** Whether the key object may be saved outside the device in wrapped form.
16261661
* This is also known as key binding.
16271662
*

0 commit comments

Comments
 (0)