Key derivation and pseudorandom generation#

If single-shot key derivation is used (sl_psa_key_derivation_single_shot), following limitations have to be considered:

  1. PBKDF2-CMAC is not suported on xG21

  2. PBKDF2-CMAC is only KDF supported for xG27

Functions#

psa_key_derivation_operation_t

Return an initial value for a key derivation operation object.

psa_key_derivation_setup(psa_key_derivation_operation_t *operation, psa_algorithm_t alg)

Set up a key derivation operation.

psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation, size_t *capacity)

Retrieve the current capacity of a key derivation operation.

psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation, size_t capacity)

Set the maximum capacity of a key derivation operation.

psa_key_derivation_input_bytes(psa_key_derivation_operation_t *operation, psa_key_derivation_step_t step, const uint8_t *data, size_t data_length)

Provide an input for key derivation or key agreement.

psa_key_derivation_input_integer(psa_key_derivation_operation_t *operation, psa_key_derivation_step_t step, uint64_t value)

Provide a numeric input for key derivation or key agreement.

psa_key_derivation_input_key(psa_key_derivation_operation_t *operation, psa_key_derivation_step_t step, mbedtls_svc_key_id_t key)

Provide an input for key derivation in the form of a key.

sl_psa_key_derivation_single_shot(psa_algorithm_t alg, mbedtls_svc_key_id_t key_in, const uint8_t *info, size_t info_length, const uint8_t *salt, size_t salt_length, size_t iterations, const psa_key_attributes_t *key_out_attributes, mbedtls_svc_key_id_t *key_out)

Perform a single-shot key derivation operation and output the resulting key.

psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation, psa_key_derivation_step_t step, mbedtls_svc_key_id_t private_key, const uint8_t *peer_key, size_t peer_key_length)

Perform a key agreement and use the shared secret as input to a key derivation.

psa_key_derivation_output_bytes(psa_key_derivation_operation_t *operation, uint8_t *output, size_t output_length)

Read some data from a key derivation operation.

psa_key_derivation_output_key(const psa_key_attributes_t *attributes, psa_key_derivation_operation_t *operation, mbedtls_svc_key_id_t *key)

Derive a key from an ongoing key derivation operation.

psa_key_derivation_verify_bytes(psa_key_derivation_operation_t *operation, const uint8_t *expected_output, size_t output_length)

Compare output data from a key derivation operation to an expected value.

psa_key_derivation_verify_key(psa_key_derivation_operation_t *operation, mbedtls_svc_key_id_t expected)

Compare output data from a key derivation operation to an expected value stored in a key object.

psa_key_derivation_abort(psa_key_derivation_operation_t *operation)

Abort a key derivation operation.

psa_raw_key_agreement(psa_algorithm_t alg, mbedtls_svc_key_id_t private_key, const uint8_t *peer_key, size_t peer_key_length, uint8_t *output, size_t output_size, size_t *output_length)

Perform a key agreement and return the raw shared secret.

Macros#

#define

Use the maximum possible capacity for a key derivation operation.

#define

Use the maximum possible capacity for a key derivation operation.

Function Documentation#

psa_key_derivation_operation_init#

static psa_key_derivation_operation_t psa_key_derivation_operation_init (void)

Return an initial value for a key derivation operation object.

Parameters
N/A

Definition at line 3218 of file util/third_party/trusted-firmware-m/interface/include/psa/crypto.h

psa_key_derivation_setup#

psa_status_t psa_key_derivation_setup (psa_key_derivation_operation_t *operation, psa_algorithm_t alg)

Set up a key derivation operation.

Parameters
[inout]operation

The key derivation operation object to set up. It must have been initialized but not set up yet.

N/Aalg

The key derivation algorithm to compute (PSA_ALG_XXX value such that PSA_ALG_IS_KEY_DERIVATION(alg) is true).

A key derivation algorithm takes some inputs and uses them to generate a byte stream in a deterministic way. This byte stream can be used to produce keys and other cryptographic material.

To derive a key:

  1. Start with an initialized object of type #psa_key_derivation_operation_t.

  2. Call psa_key_derivation_setup() to select the algorithm.

  3. Provide the inputs for the key derivation by calling psa_key_derivation_input_bytes() or psa_key_derivation_input_key() as appropriate. Which inputs are needed, in what order, and whether they may be keys and if so of what type depends on the algorithm.

  4. Optionally set the operation's maximum capacity with psa_key_derivation_set_capacity(). You may do this before, in the middle of or after providing inputs. For some algorithms, this step is mandatory because the output depends on the maximum capacity.

  5. To derive a key, call psa_key_derivation_output_key(). To derive a byte string for a different purpose, call psa_key_derivation_output_bytes(). Successive calls to these functions use successive output bytes calculated by the key derivation algorithm.

  6. Clean up the key derivation operation object with psa_key_derivation_abort().

If this function returns an error, the key derivation operation object is not changed.

If an error occurs at any step after a call to psa_key_derivation_setup(), the operation will need to be reset by a call to psa_key_derivation_abort().

Implementations must reject an attempt to derive a key of size 0.


Definition at line 3278 of file util/third_party/trusted-firmware-m/interface/include/psa/crypto.h

psa_key_derivation_get_capacity#

psa_status_t psa_key_derivation_get_capacity (const psa_key_derivation_operation_t *operation, size_t *capacity)

Retrieve the current capacity of a key derivation operation.

Parameters
[in]operation

The operation to query.

[out]capacity

On success, the capacity of the operation.

The capacity of a key derivation is the maximum number of bytes that it can return. When you get N bytes of output from a key derivation operation, this reduces its capacity by N.


Definition at line 3301 of file util/third_party/trusted-firmware-m/interface/include/psa/crypto.h

psa_key_derivation_set_capacity#

psa_status_t psa_key_derivation_set_capacity (psa_key_derivation_operation_t *operation, size_t capacity)

Set the maximum capacity of a key derivation operation.

Parameters
[inout]operation

The key derivation operation object to modify.

N/Acapacity

The new capacity of the operation. It must be less or equal to the operation's current capacity.

The capacity of a key derivation operation is the maximum number of bytes that the key derivation operation can return from this point onwards.


Definition at line 3329 of file util/third_party/trusted-firmware-m/interface/include/psa/crypto.h

psa_key_derivation_input_bytes#

psa_status_t psa_key_derivation_input_bytes (psa_key_derivation_operation_t *operation, psa_key_derivation_step_t step, const uint8_t *data, size_t data_length)

Provide an input for key derivation or key agreement.

Parameters
[inout]operation

The key derivation operation object to use. It must have been set up with psa_key_derivation_setup() and must not have produced any output yet.

N/Astep

Which step the input data is for.

[in]data

Input data to use.

N/Adata_length

Size of the data buffer in bytes.

Which inputs are required and in what order depends on the algorithm. Refer to the documentation of each key derivation or key agreement algorithm for information.

This function passes direct inputs, which is usually correct for non-secret inputs. To pass a secret input, which should be in a key object, call psa_key_derivation_input_key() instead of this function. Refer to the documentation of individual step types (PSA_KEY_DERIVATION_INPUT_xxx values of type psa_key_derivation_step_t) for more information.

If this function returns an error status, the operation enters an error state and must be aborted by calling psa_key_derivation_abort().


Definition at line 3382 of file util/third_party/trusted-firmware-m/interface/include/psa/crypto.h

psa_key_derivation_input_integer#

psa_status_t psa_key_derivation_input_integer (psa_key_derivation_operation_t *operation, psa_key_derivation_step_t step, uint64_t value)

Provide a numeric input for key derivation or key agreement.

Parameters
[inout]operation

The key derivation operation object to use. It must have been set up with psa_key_derivation_setup() and must not have produced any output yet.

N/Astep

Which step the input data is for.

[in]value

The value of the numeric input.

Which inputs are required and in what order depends on the algorithm. However, when an algorithm requires a particular order, numeric inputs usually come first as they tend to be configuration parameters. Refer to the documentation of each key derivation or key agreement algorithm for information.

This function is used for inputs which are fixed-size non-negative integers.

If this function returns an error status, the operation enters an error state and must be aborted by calling psa_key_derivation_abort().


Definition at line 3425 of file util/third_party/trusted-firmware-m/interface/include/psa/crypto.h

psa_key_derivation_input_key#

psa_status_t psa_key_derivation_input_key (psa_key_derivation_operation_t *operation, psa_key_derivation_step_t step, mbedtls_svc_key_id_t key)

Provide an input for key derivation in the form of a key.

Parameters
[inout]operation

The key derivation operation object to use. It must have been set up with psa_key_derivation_setup() and must not have produced any output yet.

N/Astep

Which step the input data is for.

N/Akey

Identifier of the key. It must have an appropriate type for step and must allow the usage PSA_KEY_USAGE_DERIVE or PSA_KEY_USAGE_VERIFY_DERIVATION (see note) and the algorithm used by the operation.

Which inputs are required and in what order depends on the algorithm. Refer to the documentation of each key derivation or key agreement algorithm for information.

This function obtains input from a key object, which is usually correct for secret inputs or for non-secret personalization strings kept in the key store. To pass a non-secret parameter which is not in the key store, call psa_key_derivation_input_bytes() instead of this function. Refer to the documentation of individual step types (PSA_KEY_DERIVATION_INPUT_xxx values of type psa_key_derivation_step_t) for more information.

If this function returns an error status, the operation enters an error state and must be aborted by calling psa_key_derivation_abort().

Note


Definition at line 3492 of file util/third_party/trusted-firmware-m/interface/include/psa/crypto.h

sl_psa_key_derivation_single_shot#

psa_status_t sl_psa_key_derivation_single_shot (psa_algorithm_t alg, mbedtls_svc_key_id_t key_in, const uint8_t *info, size_t info_length, const uint8_t *salt, size_t salt_length, size_t iterations, const psa_key_attributes_t *key_out_attributes, mbedtls_svc_key_id_t *key_out)

Perform a single-shot key derivation operation and output the resulting key.

Parameters
N/Aalg

The key derivation algorithm to compute (PSA_ALG_XXX value such that PSA_ALG_IS_KEY_DERIVATION(alg) is true).

N/Akey_in

Identifier of the secret key to input to the operation. It must allow the usage PSA_KEY_USAGE_DERIVE and be of a symmetric type.

[in]info

A context- and application specific information string. Only used for HKDF, but can be omitted.

N/Ainfo_length

The length of the provided info in bytes.

[in]salt

An optional salt value (a non-secret random value). Used for both HKDF and PBKDF2. Recommended for PBKDF2.

N/Asalt_length

The length of the provided salt in bytes.

N/Aiterations

The number of iterations to use. Maximum supported value is 16384. Only used for PBKDF2.

[in]key_out_attributes

The attributes for the new key output by the derivation operation. The key must be of a symmetric type.

[out]key_out

The identifier of the new key output by the derivation operation.

NOTE: this is a Silicon Labs custom API, and is not a part of the official PSA Cryptography specification.

This function supports HKDF and PBKDF2.

This function obtains its secret input from a key object, and any additional inputs such as buffers and integers. The output of this function is a key object containing the output of the selected key derivation function.


Definition at line 3550 of file util/third_party/trusted-firmware-m/interface/include/psa/crypto.h

psa_key_derivation_key_agreement#

psa_status_t psa_key_derivation_key_agreement (psa_key_derivation_operation_t *operation, psa_key_derivation_step_t step, mbedtls_svc_key_id_t private_key, const uint8_t *peer_key, size_t peer_key_length)

Perform a key agreement and use the shared secret as input to a key derivation.

Parameters
[inout]operation

The key derivation operation object to use. It must have been set up with psa_key_derivation_setup() with a key agreement and derivation algorithm alg (PSA_ALG_XXX value such that PSA_ALG_IS_KEY_AGREEMENT(alg) is true and PSA_ALG_IS_RAW_KEY_AGREEMENT(alg) is false). The operation must be ready for an input of the type given by step.

N/Astep

Which step the input data is for.

N/Aprivate_key

Identifier of the private key to use. It must allow the usage PSA_KEY_USAGE_DERIVE.

[in]peer_key

Public key of the peer. The peer key must be in the same format that psa_import_key() accepts for the public key type corresponding to the type of private_key. That is, this function performs the equivalent of psa_import_key(..., peer_key, peer_key_length) where with key attributes indicating the public key type corresponding to the type of private_key. For example, for EC keys, this means that peer_key is interpreted as a point on the curve that the private key is on. The standard formats for public keys are documented in the documentation of psa_export_public_key().

N/Apeer_key_length

Size of peer_key in bytes.

A key agreement algorithm takes two inputs: a private key private_key a public key peer_key. The result of this function is passed as input to a key derivation. The output of this key derivation can be extracted by reading from the resulting operation to produce keys and other cryptographic material.

If this function returns an error status, the operation enters an error state and must be aborted by calling psa_key_derivation_abort().


Definition at line 3624 of file util/third_party/trusted-firmware-m/interface/include/psa/crypto.h

psa_key_derivation_output_bytes#

psa_status_t psa_key_derivation_output_bytes (psa_key_derivation_operation_t *operation, uint8_t *output, size_t output_length)

Read some data from a key derivation operation.

Parameters
[inout]operation

The key derivation operation object to read from.

[out]output

Buffer where the output will be written.

N/Aoutput_length

Number of bytes to output.

This function calculates output bytes from a key derivation algorithm and return those bytes. If you view the key derivation's output as a stream of bytes, this function destructively reads the requested number of bytes from the stream. The operation's capacity decreases by the number of bytes read.

If this function returns an error status other than PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error state and must be aborted by calling psa_key_derivation_abort().


Definition at line 3671 of file util/third_party/trusted-firmware-m/interface/include/psa/crypto.h

psa_key_derivation_output_key#

psa_status_t psa_key_derivation_output_key (const psa_key_attributes_t *attributes, psa_key_derivation_operation_t *operation, mbedtls_svc_key_id_t *key)

Derive a key from an ongoing key derivation operation.

Parameters
[in]attributes

The attributes for the new key. If the key type to be created is PSA_KEY_TYPE_PASSWORD_HASH then the algorithm in the policy must be the same as in the current operation.

[inout]operation

The key derivation operation object to read from.

[out]key

On success, an identifier for the newly created key. For persistent keys, this is the key identifier defined in attributes. 0 on failure.

This function calculates output bytes from a key derivation algorithm and uses those bytes to generate a key deterministically. The key's location, usage policy, type and size are taken from attributes.

If you view the key derivation's output as a stream of bytes, this function destructively reads as many bytes as required from the stream. The operation's capacity decreases by the number of bytes read.

If this function returns an error status other than PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error state and must be aborted by calling psa_key_derivation_abort().

How much output is produced and consumed from the operation, and how the key is derived, depends on the key type and on the key size (denoted bits below):

  • For key types for which the key is an arbitrary sequence of bytes of a given size, this function is functionally equivalent to calling psa_key_derivation_output_bytes and passing the resulting output to psa_import_key. However, this function has a security benefit: if the implementation provides an isolation boundary then the key material is not exposed outside the isolation boundary. As a consequence, for these key types, this function always consumes exactly (bits / 8) bytes from the operation. The following key types defined in this specification follow this scheme:

  • For ECC keys on a Montgomery elliptic curve (PSA_KEY_TYPE_ECC_KEY_PAIR(curve) where curve designates a Montgomery curve), this function always draws a byte string whose length is determined by the curve, and sets the mandatory bits accordingly. That is:

  • For key types for which the key is represented by a single sequence of bits bits with constraints as to which bit sequences are acceptable, this function draws a byte string of length (bits / 8) bytes rounded up to the nearest whole number of bytes. If the resulting byte string is acceptable, it becomes the key, otherwise the drawn bytes are discarded. This process is repeated until an acceptable byte string is drawn. The byte string drawn from the operation is interpreted as specified for the output produced by psa_export_key(). The following key types defined in this specification follow this scheme:

    • PSA_KEY_TYPE_DES. Force-set the parity bits, but discard forbidden weak keys. For 2-key and 3-key triple-DES, the three keys are generated successively (for example, for 3-key triple-DES, if the first 8 bytes specify a weak key and the next 8 bytes do not, discard the first 8 bytes, use the next 8 bytes as the first key, and continue reading output from the operation to derive the other two keys).

    • Finite-field Diffie-Hellman keys (PSA_KEY_TYPE_DH_KEY_PAIR(group) where group designates any Diffie-Hellman group) and ECC keys on a Weierstrass elliptic curve (PSA_KEY_TYPE_ECC_KEY_PAIR(curve) where curve designates a Weierstrass curve). For these key types, interpret the byte string as integer in big-endian order. Discard it if it is not in the range [0, N - 2] where N is the boundary of the private key domain (the prime p for Diffie-Hellman, the subprime q for DSA, or the order of the curve's base point for ECC). Add 1 to the resulting integer and use this as the private key x. This method allows compliance to NIST standards, specifically the methods titled "key-pair generation by testing candidates" in NIST SP 800-56A §5.6.1.1.4 for Diffie-Hellman, in FIPS 186-4 §B.1.2 for DSA, and in NIST SP 800-56A §5.6.1.2.2 or FIPS 186-4 §B.4.2 for elliptic curve keys.

  • For other key types, including PSA_KEY_TYPE_RSA_KEY_PAIR, the way in which the operation output is consumed is implementation-defined.

In all cases, the data that is read is discarded from the operation. The operation's capacity is decreased by the number of bytes read.

For algorithms that take an input step PSA_KEY_DERIVATION_INPUT_SECRET, the input to that step must be provided with psa_key_derivation_input_key(). Future versions of this specification may include additional restrictions on the derived key based on the attributes and strength of the secret key.


Definition at line 3821 of file util/third_party/trusted-firmware-m/interface/include/psa/crypto.h

psa_key_derivation_verify_bytes#

psa_status_t psa_key_derivation_verify_bytes (psa_key_derivation_operation_t *operation, const uint8_t *expected_output, size_t output_length)

Compare output data from a key derivation operation to an expected value.

Parameters
[inout]operation

The key derivation operation object to read from.

[in]expected_output

Buffer containing the expected derivation output.

N/Aoutput_length

Length of the expected output; this is also the number of bytes that will be read.

This function calculates output bytes from a key derivation algorithm and compares those bytes to an expected value in constant time. If you view the key derivation's output as a stream of bytes, this function destructively reads the expected number of bytes from the stream before comparing them. The operation's capacity decreases by the number of bytes read.

This is functionally equivalent to the following code:

psa_key_derivation_output_bytes(operation, tmp, output_length);
if (memcmp(output, tmp, output_length) != 0)
    return PSA_ERROR_INVALID_SIGNATURE;

except (1) it works even if the key's policy does not allow outputting the bytes, and (2) the comparison will be done in constant time.

If this function returns an error status other than PSA_ERROR_INSUFFICIENT_DATA or PSA_ERROR_INVALID_SIGNATURE, the operation enters an error state and must be aborted by calling psa_key_derivation_abort().


Definition at line 3879 of file util/third_party/trusted-firmware-m/interface/include/psa/crypto.h

psa_key_derivation_verify_key#

psa_status_t psa_key_derivation_verify_key (psa_key_derivation_operation_t *operation, mbedtls_svc_key_id_t expected)

Compare output data from a key derivation operation to an expected value stored in a key object.

Parameters
[inout]operation

The key derivation operation object to read from.

[in]expected

A key of type PSA_KEY_TYPE_PASSWORD_HASH containing the expected output. Its policy must include the PSA_KEY_USAGE_VERIFY_DERIVATION flag and the permitted algorithm must match the operation. The value of this key was likely computed by a previous call to psa_key_derivation_output_key().

This function calculates output bytes from a key derivation algorithm and compares those bytes to an expected value, provided as key of type PSA_KEY_TYPE_PASSWORD_HASH. If you view the key derivation's output as a stream of bytes, this function destructively reads the number of bytes corresponding to the length of the expected value from the stream before comparing them. The operation's capacity decreases by the number of bytes read.

This is functionally equivalent to exporting the key and calling psa_key_derivation_verify_bytes() on the result, except that it works even if the key cannot be exported.

If this function returns an error status other than PSA_ERROR_INSUFFICIENT_DATA or PSA_ERROR_INVALID_SIGNATURE, the operation enters an error state and must be aborted by calling psa_key_derivation_abort().


Definition at line 3943 of file util/third_party/trusted-firmware-m/interface/include/psa/crypto.h

psa_key_derivation_abort#

psa_status_t psa_key_derivation_abort (psa_key_derivation_operation_t *operation)

Abort a key derivation operation.

Parameters
[inout]operation

The operation to abort.

Aborting an operation frees all associated resources except for the operation structure itself. Once aborted, the operation object can be reused for another operation by calling psa_key_derivation_setup() again.

This function may be called at any time after the operation object has been initialized as described in #psa_key_derivation_operation_t.

In particular, it is valid to call psa_key_derivation_abort() twice, or to call psa_key_derivation_abort() on an operation that has not been set up.


Definition at line 3970 of file util/third_party/trusted-firmware-m/interface/include/psa/crypto.h

psa_raw_key_agreement#

psa_status_t psa_raw_key_agreement (psa_algorithm_t alg, mbedtls_svc_key_id_t private_key, const uint8_t *peer_key, size_t peer_key_length, uint8_t *output, size_t output_size, size_t *output_length)

Perform a key agreement and return the raw shared secret.

Parameters
N/Aalg

The key agreement algorithm to compute (PSA_ALG_XXX value such that PSA_ALG_IS_RAW_KEY_AGREEMENT(alg) is true).

N/Aprivate_key

Identifier of the private key to use. It must allow the usage PSA_KEY_USAGE_DERIVE.

[in]peer_key

Public key of the peer. It must be in the same format that psa_import_key() accepts. The standard formats for public keys are documented in the documentation of psa_export_public_key().

N/Apeer_key_length

Size of peer_key in bytes.

[out]output

Buffer where the decrypted message is to be written.

N/Aoutput_size

Size of the output buffer in bytes.

[out]output_length

On success, the number of bytes that make up the returned output.

Warnings

  • The raw result of a key agreement algorithm such as finite-field Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should not be used directly as key material. It should instead be passed as input to a key derivation algorithm. To chain a key agreement with a key derivation, use psa_key_derivation_key_agreement() and other functions from the key derivation interface.


Definition at line 4023 of file util/third_party/trusted-firmware-m/interface/include/psa/crypto.h

Macro Definition Documentation#

PSA_KEY_DERIVATION_UNLIMITED_CAPACITY#

#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY
Value:
((size_t) (-1))

Use the maximum possible capacity for a key derivation operation.

Use this value as the capacity argument when setting up a key derivation to indicate that the operation should have the maximum possible capacity. The value of the maximum possible capacity depends on the key derivation algorithm.


Definition at line 3340 of file util/third_party/trusted-firmware-m/interface/include/psa/crypto.h

PSA_KEY_DERIVATION_UNLIMITED_CAPACITY#

#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY
Value:
((size_t) (-1))

Use the maximum possible capacity for a key derivation operation.

Use this value as the capacity argument when setting up a key derivation to indicate that the operation should have the maximum possible capacity. The value of the maximum possible capacity depends on the key derivation algorithm.


Definition at line 3352 of file util/third_party/mbedtls/include/psa/crypto.h