Key import and export
Description
Functions |
|
| psa_status_t | psa_import_key (const psa_key_attributes_t *attributes, const uint8_t *data, size_t data_length, mbedtls_svc_key_id_t *key) |
|
Import a key in binary format.
|
|
| psa_status_t | psa_export_key (mbedtls_svc_key_id_t key, uint8_t *data, size_t data_size, size_t *data_length) |
|
Export a key in binary format.
|
|
| psa_status_t | psa_export_public_key (mbedtls_svc_key_id_t key, uint8_t *data, size_t data_size, size_t *data_length) |
|
Export a public key or the public part of a key pair in binary format.
|
|
Function Documentation
◆ psa_import_key()
| psa_status_t psa_import_key | ( | const psa_key_attributes_t * |
attributes,
|
| const uint8_t * |
data,
|
||
| size_t |
data_length,
|
||
| mbedtls_svc_key_id_t * |
key
|
||
| ) |
Import a key in binary format.
This function supports any output from psa_export_key() . Refer to the documentation of psa_export_public_key() for the format of public keys and to the documentation of psa_export_key() for the format for other key types.
The key data determines the key size. The attributes may optionally specify a key size; in this case it must match the size determined from the key data. A key size of 0 in
attributes
indicates that the key size is solely determined by the key data.
Implementations must reject an attempt to import a key of size 0.
This specification supports a single format for each key type. Implementations may support other formats as long as the standard format is supported. Implementations that support other formats should ensure that the formats are clearly unambiguous so as to minimize the risk that an invalid input is accidentally interpreted according to a different format.
- Parameters
-
[in] attributesThe attributes for the new key. The key size is always determined from the databuffer. If the key size inattributesis nonzero, it must be equal to the size fromdata.[out] keyOn success, an identifier to the newly created key. For persistent keys, this is the key identifier defined in attributes.0on failure.[in] dataBuffer containing the key data. The content of this buffer is interpreted according to the type declared in attributes. All implementations must support at least the format described in the documentation of psa_export_key() or psa_export_public_key() for the chosen type. Implementations may allow other formats, but should be conservative: implementations should err on the side of rejecting content if it may be erroneous (e.g. wrong type or truncated data).data_lengthSize of the databuffer in bytes.
- Return values
-
PSA_SUCCESSSuccess. If the key is persistent, the key material and the key's metadata have been saved to persistent storage. PSA_ERROR_ALREADY_EXISTSThis is an attempt to create a persistent key, and there is already a persistent key with the given identifier. PSA_ERROR_NOT_SUPPORTEDThe key type or key size is not supported, either by the implementation in general or in this particular persistent location. PSA_ERROR_INVALID_ARGUMENTThe key attributes, as a whole, are invalid. PSA_ERROR_INVALID_ARGUMENTThe key data is not correctly formatted. PSA_ERROR_INVALID_ARGUMENTThe size in attributesis nonzero and does not match the size of the key data.PSA_ERROR_INSUFFICIENT_MEMORYPSA_ERROR_INSUFFICIENT_STORAGEPSA_ERROR_COMMUNICATION_FAILUREPSA_ERROR_DATA_CORRUPTPSA_ERROR_DATA_INVALIDPSA_ERROR_STORAGE_FAILUREPSA_ERROR_HARDWARE_FAILUREPSA_ERROR_CORRUPTION_DETECTEDPSA_ERROR_BAD_STATEThe library has not been previously initialized by psa_crypto_init() . It is implementation-dependent whether a failure to initialize results in this error code.
◆ psa_export_key()
| psa_status_t psa_export_key | ( | mbedtls_svc_key_id_t |
key,
|
| uint8_t * |
data,
|
||
| size_t |
data_size,
|
||
| size_t * |
data_length
|
||
| ) |
Export a key in binary format.
The output of this function can be passed to psa_import_key() to create an equivalent object.
If the implementation of psa_import_key() supports other formats beyond the format specified here, the output from psa_export_key() must use the representation specified here, not the original representation.
For standard key types, the output format is as follows:
- For symmetric keys (including MAC keys), the format is the raw bytes of the key.
- For DES, the key data consists of 8 bytes. The parity bits must be correct.
- For Triple-DES, the format is the concatenation of the two or three DES keys.
-
For RSA key pairs (
PSA_KEY_TYPE_RSA_KEY_PAIR
), the format is the non-encrypted DER encoding of the representation defined by PKCS#1 (RFC 8017) as
RSAPrivateKey, version 0.RSAPrivateKey ::= SEQUENCE {version INTEGER, -- must be 0modulus INTEGER, -- npublicExponent INTEGER, -- eprivateExponent INTEGER, -- dprime1 INTEGER, -- pprime2 INTEGER, -- qexponent1 INTEGER, -- d mod (p-1)exponent2 INTEGER, -- d mod (q-1)coefficient INTEGER, -- (inverse of q) mod p} -
For elliptic curve key pairs (key types for which
PSA_KEY_TYPE_IS_ECC_KEY_PAIR
is true), the format is a representation of the private value as a
ceiling(m/8)-byte string wheremis the bit size associated with the curve, i.e. the bit size of the order of the curve's coordinate field. This byte string is in little-endian order for Montgomery curves (curve typesPSA_ECC_FAMILY_CURVEXXX), and in big-endian order for Weierstrass curves (curve typesPSA_ECC_FAMILY_SECTXXX,PSA_ECC_FAMILY_SECPXXXandPSA_ECC_FAMILY_BRAINPOOL_PXXX). For Weierstrass curves, this is the content of theprivateKeyfield of theECPrivateKeyformat defined by RFC 5915. For Montgomery curves, the format is defined by RFC 7748, and output is masked according to §5. For twisted Edwards curves, the private key is as defined by RFC 8032 (a 32-byte string for Edwards25519, a 57-byte string for Edwards448). -
For Diffie-Hellman key exchange key pairs (key types for which
PSA_KEY_TYPE_IS_DH_KEY_PAIR
is true), the format is the representation of the private key
xas a big-endian byte string. The length of the byte string is the private key size in bytes (leading zeroes are not stripped). - For public keys (key types for which PSA_KEY_TYPE_IS_PUBLIC_KEY is true), the format is the same as for psa_export_public_key() .
The policy on the key must have the usage flag PSA_KEY_USAGE_EXPORT set.
- Parameters
-
keyIdentifier of the key to export. It must allow the usage PSA_KEY_USAGE_EXPORT , unless it is a public key. [out] dataBuffer where the key data is to be written. data_sizeSize of the databuffer in bytes.[out] data_lengthOn success, the number of bytes that make up the key data.
- Return values
-
PSA_SUCCESSPSA_ERROR_INVALID_HANDLEPSA_ERROR_NOT_PERMITTEDThe key does not have the PSA_KEY_USAGE_EXPORT flag. PSA_ERROR_NOT_SUPPORTEDPSA_ERROR_BUFFER_TOO_SMALLThe size of the databuffer is too small. You can determine a sufficient buffer size by calling #PSA_EXPORT_KEY_OUTPUT_SIZE(type,bits) wheretypeis the key type andbitsis the key size in bits.PSA_ERROR_COMMUNICATION_FAILUREPSA_ERROR_HARDWARE_FAILUREPSA_ERROR_CORRUPTION_DETECTEDPSA_ERROR_STORAGE_FAILUREPSA_ERROR_INSUFFICIENT_MEMORYPSA_ERROR_BAD_STATEThe library has not been previously initialized by psa_crypto_init() . It is implementation-dependent whether a failure to initialize results in this error code.
◆ psa_export_public_key()
| psa_status_t psa_export_public_key | ( | mbedtls_svc_key_id_t |
key,
|
| uint8_t * |
data,
|
||
| size_t |
data_size,
|
||
| size_t * |
data_length
|
||
| ) |
Export a public key or the public part of a key pair in binary format.
The output of this function can be passed to psa_import_key() to create an object that is equivalent to the public key.
This specification supports a single format for each key type. Implementations may support other formats as long as the standard format is supported. Implementations that support other formats should ensure that the formats are clearly unambiguous so as to minimize the risk that an invalid input is accidentally interpreted according to a different format.
For standard key types, the output format is as follows:
-
For RSA public keys (
PSA_KEY_TYPE_RSA_PUBLIC_KEY
), the DER encoding of the representation defined by RFC 3279 §2.3.1 as
RSAPublicKey.RSAPublicKey ::= SEQUENCE {modulus INTEGER, -- npublicExponent INTEGER } -- e - For elliptic curve keys on a twisted Edwards curve (key types for which PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true and PSA_KEY_TYPE_ECC_GET_FAMILY returns PSA_ECC_FAMILY_TWISTED_EDWARDS ), the public key is as defined by RFC 8032 (a 32-byte string for Edwards25519, a 57-byte string for Edwards448).
-
For other elliptic curve public keys (key types for which
PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY
is true), the format is the uncompressed representation defined by SEC1 §2.3.3 as the content of an ECPoint. Let
mbe the bit size associated with the curve, i.e. the bit size ofqfor a curve overF_q. The representation consists of:- The byte 0x04;
-
x_Pas aceiling(m/8)-byte string, big-endian; -
y_Pas aceiling(m/8)-byte string, big-endian.
-
For Diffie-Hellman key exchange public keys (key types for which
PSA_KEY_TYPE_IS_DH_PUBLIC_KEY
is true), the format is the representation of the public key
y = g^x mod pas a big-endian byte string. The length of the byte string is the length of the base primepin bytes.
Exporting a public key object or the public part of a key pair is always permitted, regardless of the key's usage flags.
- Parameters
-
keyIdentifier of the key to export. [out] dataBuffer where the key data is to be written. data_sizeSize of the databuffer in bytes.[out] data_lengthOn success, the number of bytes that make up the key data.
- Return values
-
PSA_SUCCESSPSA_ERROR_INVALID_HANDLEPSA_ERROR_INVALID_ARGUMENTThe key is neither a public key nor a key pair. PSA_ERROR_NOT_SUPPORTEDPSA_ERROR_BUFFER_TOO_SMALLThe size of the databuffer is too small. You can determine a sufficient buffer size by calling #PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR (type),bits) wheretypeis the key type andbitsis the key size in bits.PSA_ERROR_COMMUNICATION_FAILUREPSA_ERROR_HARDWARE_FAILUREPSA_ERROR_CORRUPTION_DETECTEDPSA_ERROR_STORAGE_FAILUREPSA_ERROR_INSUFFICIENT_MEMORYPSA_ERROR_BAD_STATEThe library has not been previously initialized by psa_crypto_init() . It is implementation-dependent whether a failure to initialize results in this error code.