diff --git a/doc/crypto/api.db/psa/crypto.h b/doc/crypto/api.db/psa/crypto.h index 685d2af8..7faca35c 100644 --- a/doc/crypto/api.db/psa/crypto.h +++ b/doc/crypto/api.db/psa/crypto.h @@ -6,7 +6,10 @@ typedef uint32_t psa_algorithm_t; typedef /* implementation-defined type */ psa_cipher_operation_t; typedef uint8_t psa_dh_family_t; typedef uint8_t psa_ecc_family_t; +typedef /* implementation-defined type */ psa_export_public_key_iop_t; +typedef /* implementation-defined type */ psa_generate_key_iop_t; typedef /* implementation-defined type */ psa_hash_operation_t; +typedef /* implementation-defined type */ psa_key_agreement_iop_t; typedef /* implementation-defined type */ psa_key_attributes_t; typedef /* implementation-defined type */ psa_key_derivation_operation_t; typedef uint16_t psa_key_derivation_step_t; @@ -24,8 +27,10 @@ typedef uint32_t psa_pake_primitive_t; typedef uint8_t psa_pake_primitive_type_t; typedef uint8_t psa_pake_role_t; typedef uint8_t psa_pake_step_t; +typedef /* implementation-defined type */ psa_sign_iop_t; typedef /* implementation-defined type */ psa_sign_operation_t; typedef uint8_t psa_slh_dsa_family_t; +typedef /* implementation-defined type */ psa_verify_iop_t; typedef /* implementation-defined type */ psa_verify_operation_t; typedef /* implementation-defined type */ psa_xof_operation_t; typedef struct psa_custom_key_parameters_t { @@ -219,6 +224,8 @@ typedef struct psa_custom_key_parameters_t { #define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0200000c) #define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0200000d) #define PSA_ALG_SIGN_SUPPORTS_CONTEXT(alg) /* implementation-defined value */ +#define PSA_ALG_SIGN_SUPPORTS_DEFERRED_SIGNATURE(alg) \ + /* implementation-defined value */ #define PSA_ALG_SLH_DSA ((psa_algorithm_t) 0x06004000) #define PSA_ALG_SM3 ((psa_algorithm_t)0x02000014) #define PSA_ALG_SP800_108_COUNTER_CMAC ((psa_algorithm_t)0x08000800) @@ -294,9 +301,11 @@ typedef struct psa_custom_key_parameters_t { #define PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits) \ /* implementation-defined value */ #define PSA_EXPORT_KEY_PAIR_MAX_SIZE /* implementation-defined value */ +#define PSA_EXPORT_PUBLIC_KEY_IOP_INIT /* implementation-defined value */ #define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE /* implementation-defined value */ #define PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits) \ /* implementation-defined value */ +#define PSA_GENERATE_KEY_IOP_INIT /* implementation-defined value */ #define PSA_HASH_BLOCK_LENGTH(alg) /* implementation-defined value */ #define PSA_HASH_LENGTH(alg) /* implementation-defined value */ #define PSA_HASH_MAX_SIZE /* implementation-defined value */ @@ -308,6 +317,8 @@ typedef struct psa_custom_key_parameters_t { /* specification-defined value */ #define PSA_HASH_SUSPEND_OUTPUT_MAX_SIZE /* implementation-defined value */ #define PSA_HASH_SUSPEND_OUTPUT_SIZE(alg) /* specification-defined value */ +#define PSA_IOP_MAX_OPS_UNLIMITED UINT32_MAX +#define PSA_KEY_AGREEMENT_IOP_INIT /* implementation-defined value */ #define PSA_KEY_ATTRIBUTES_INIT /* implementation-defined value */ #define PSA_KEY_DERIVATION_INPUT_CONTEXT /* implementation-defined value */ #define PSA_KEY_DERIVATION_INPUT_COST /* implementation-defined value */ @@ -473,6 +484,7 @@ typedef struct psa_custom_key_parameters_t { #define PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(key_type, key_bits) \ /* implementation-defined value */ #define PSA_SIGNATURE_MAX_SIZE /* implementation-defined value */ +#define PSA_SIGN_IOP_INIT /* implementation-defined value */ #define PSA_SIGN_OPERATION_INIT /* implementation-defined value */ #define PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \ /* implementation-defined value */ @@ -482,6 +494,7 @@ typedef struct psa_custom_key_parameters_t { #define PSA_SLH_DSA_FAMILY_SHAKE_S ((psa_slh_dsa_family_t) 0x0b) #define PSA_TLS12_ECJPAKE_TO_PMS_OUTPUT_SIZE 32 #define PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE /* implementation-defined value */ +#define PSA_VERIFY_IOP_INIT /* implementation-defined value */ #define PSA_VERIFY_OPERATION_INIT /* implementation-defined value */ #define PSA_WRAP_KEY_OUTPUT_SIZE(wrap_key_type, alg, key_type, key_bits) \ /* implementation-defined value */ @@ -639,6 +652,15 @@ psa_status_t psa_export_public_key(psa_key_id_t key, uint8_t * data, size_t data_size, size_t * data_length); +psa_status_t psa_export_public_key_iop_abort(psa_export_public_key_iop_t * operation); +psa_status_t psa_export_public_key_iop_complete(psa_export_public_key_iop_t * operation, + uint8_t * data, + size_t data_size, + size_t * data_length); +uint32_t psa_export_public_key_iop_get_num_ops(psa_export_public_key_iop_t * operation); +psa_export_public_key_iop_t psa_export_public_key_iop_init(void); +psa_status_t psa_export_public_key_iop_start(psa_export_public_key_iop_t * operation, + psa_key_id_t key); psa_status_t psa_generate_key(const psa_key_attributes_t * attributes, psa_key_id_t * key); psa_status_t psa_generate_key_custom(const psa_key_attributes_t * attributes, @@ -646,6 +668,17 @@ psa_status_t psa_generate_key_custom(const psa_key_attributes_t * attributes, const uint8_t * custom_data, size_t custom_data_length, psa_key_id_t * key); +psa_status_t psa_generate_key_iop_abort(psa_generate_key_iop_t * operation); +psa_status_t psa_generate_key_iop_complete(psa_generate_key_iop_t * operation, + psa_key_id_t * key); +psa_status_t psa_generate_key_iop_custom(psa_generate_key_iop_t * operation, + const psa_custom_key_parameters_t * custom, + const uint8_t * custom_data, + size_t custom_data_length); +uint32_t psa_generate_key_iop_get_num_ops(psa_generate_key_iop_t * operation); +psa_generate_key_iop_t psa_generate_key_iop_init(void); +psa_status_t psa_generate_key_iop_start(psa_generate_key_iop_t * operation, + const psa_key_attributes_t * attributes); psa_status_t psa_generate_random(uint8_t * output, size_t output_size); psa_algorithm_t psa_get_key_algorithm(const psa_key_attributes_t * attributes); @@ -694,12 +727,25 @@ psa_status_t psa_import_key(const psa_key_attributes_t * attributes, const uint8_t * data, size_t data_length, psa_key_id_t * key); +uint32_t psa_iop_get_max_ops(void); +void psa_iop_set_max_ops(uint32_t max_ops); psa_status_t psa_key_agreement(psa_key_id_t private_key, const uint8_t * peer_key, size_t peer_key_length, psa_algorithm_t alg, const psa_key_attributes_t * attributes, psa_key_id_t * key); +psa_status_t psa_key_agreement_iop_abort(psa_key_agreement_iop_t * operation); +psa_status_t psa_key_agreement_iop_complete(psa_key_agreement_iop_t * operation, + psa_key_id_t * key); +uint32_t psa_key_agreement_iop_get_num_ops(psa_key_agreement_iop_t * operation); +psa_key_agreement_iop_t psa_key_agreement_iop_init(void); +psa_status_t psa_key_agreement_iop_start(psa_key_agreement_iop_t * operation, + psa_key_id_t private_key, + const uint8_t * peer_key, + size_t peer_key_length, + psa_algorithm_t alg, + const psa_key_attributes_t * attributes); psa_key_attributes_t psa_key_attributes_init(void); psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t * operation); psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t * operation, @@ -852,6 +898,26 @@ psa_status_t psa_sign_hash_with_context(psa_key_id_t key, uint8_t * signature, size_t signature_size, size_t * signature_length); +psa_status_t psa_sign_iop_abort(psa_sign_iop_t * operation); +psa_status_t psa_sign_iop_complete(psa_sign_iop_t * operation, + uint8_t * signature, + size_t signature_size, + size_t * signature_length); +uint32_t psa_sign_iop_get_num_ops(psa_sign_iop_t * operation); +psa_status_t psa_sign_iop_hash(psa_sign_iop_t * operation, + const uint8_t * hash, + size_t hash_length); +psa_sign_iop_t psa_sign_iop_init(void); +psa_status_t psa_sign_iop_set_context(psa_sign_iop_t * operation, + const uint8_t * context, + size_t context_length); +psa_status_t psa_sign_iop_setup_complete(psa_sign_iop_t * operation); +psa_status_t psa_sign_iop_setup_start(psa_sign_iop_t * operation, + psa_key_id_t key, + psa_algorithm_t alg); +psa_status_t psa_sign_iop_update(psa_sign_iop_t * operation, + const uint8_t * input, + size_t input_length); psa_status_t psa_sign_message(psa_key_id_t key, psa_algorithm_t alg, const uint8_t * input, @@ -900,6 +966,31 @@ psa_status_t psa_verify_hash_with_context(psa_key_id_t key, size_t context_length, const uint8_t * signature, size_t signature_length); +psa_status_t psa_verify_iop_abort(psa_verify_iop_t * operation); +psa_status_t psa_verify_iop_complete(psa_verify_iop_t * operation); +uint32_t psa_verify_iop_get_num_ops(psa_verify_iop_t * operation); +psa_status_t psa_verify_iop_hash(psa_verify_iop_t * operation, + const uint8_t * hash, + size_t hash_length); +psa_verify_iop_t psa_verify_iop_init(void); +psa_status_t psa_verify_iop_set_context(psa_verify_iop_t * operation, + const uint8_t * context, + size_t context_length); +psa_status_t psa_verify_iop_set_signature(psa_verify_iop_t * operation, + const uint8_t * signature, + size_t signature_length); +psa_status_t psa_verify_iop_setup_complete(psa_verify_iop_t * operation); +psa_status_t psa_verify_iop_setup_deferred_signature_start(psa_verify_iop_t * operation, + psa_key_id_t key, + psa_algorithm_t alg); +psa_status_t psa_verify_iop_setup_start(psa_verify_iop_t * operation, + psa_key_id_t key, + psa_algorithm_t alg, + const uint8_t * signature, + size_t signature_length); +psa_status_t psa_verify_iop_update(psa_verify_iop_t * operation, + const uint8_t * input, + size_t input_length); psa_status_t psa_verify_message(psa_key_id_t key, psa_algorithm_t alg, const uint8_t * input, diff --git a/doc/crypto/api/keys/management.rst b/doc/crypto/api/keys/management.rst index b482a2bb..9c5913b2 100644 --- a/doc/crypto/api/keys/management.rst +++ b/doc/crypto/api/keys/management.rst @@ -237,6 +237,9 @@ When creating a key, the attributes for the new key are specified in a `psa_key_ This function is equivalent to calling `psa_generate_key_custom()` with the production parameters `PSA_CUSTOM_KEY_PARAMETERS_INIT` and ``custom_data_length == 0`` (``custom_data`` is ignored). + If an application requires bounded execution time when generating a key, the implementation might provide support for interruptible key generation. + See :secref:`interruptible-generate-key`. + .. function:: psa_generate_key_custom .. summary:: @@ -723,6 +726,10 @@ Key export Exporting a public-key object or the public part of a key pair is always permitted, regardless of the key's usage flags. + If an application requires bounded execution time when exporting a public key, it can use an interruptible public-key export operation. + See :secref:`interruptible-export-key`. + + .. macro:: PSA_EXPORT_KEY_OUTPUT_SIZE :definition: /* implementation-defined value */ @@ -830,3 +837,549 @@ Key export This value must be a sufficient buffer size when calling `psa_export_key()` or `psa_export_public_key()` to export any asymmetric key pair or public key that is supported by the implementation, regardless of the exact key type and key size. See also `PSA_EXPORT_KEY_PAIR_MAX_SIZE`, `PSA_EXPORT_PUBLIC_KEY_MAX_SIZE`, and `PSA_EXPORT_KEY_OUTPUT_SIZE()`. + +.. _interruptible-generate-key: + +Interruptible key generation +---------------------------- + +Generation of some key types can be computationally expensive. +For example, RSA keys, and elliptic curve public keys. + +For such keys, an interruptible key-generation operation can be used instead of calling `psa_generate_key()`, in applications that have bounded execution time requirements for use cases that require key generation. + +By default, this operation uses the default production parameters of `psa_generate_key()`. +To use custom production parameters, call `psa_generate_key_iop_custom()` after `psa_generate_key_iop_start()` and before `psa_generate_key_iop_complete()`. + +.. note:: + An implementation of the |API| does not need to provide incremental generation for all key types supported by the implementation. + Use `psa_generate_key()` to create keys for types that do not need to be incrementally generated. + +An interruptible key-generation operation is used as follows: + +1. Allocate an interruptible key-generation operation object, of type `psa_generate_key_iop_t`, which will be passed to all the functions listed here. +#. Initialize the operation object with one of the methods described in the documentation for `psa_generate_key_iop_t`, for example, `PSA_GENERATE_KEY_IOP_INIT`. +#. Call `psa_generate_key_iop_start()` to specify the key attributes. +#. Optionally, call `psa_generate_key_iop_custom()` to specify custom production parameters. +#. Call `psa_generate_key_iop_complete()` to finish generating the key, until this function returns a status code other than :code:`PSA_OPERATION_INCOMPLETE`. +#. If an error occurs at any stage, or to terminate the operation early, call `psa_generate_key_iop_abort()`. + +.. typedef:: /* implementation-defined type */ psa_generate_key_iop_t + + .. summary:: + The type of the state data structure for an interruptible key-generation operation. + + .. versionadded:: 1.6 + + Before calling any function on an interruptible key-generation operation object, the application must initialize it by any of the following means: + + * Set the object to all-bits-zero, for example: + + .. code-block:: xref + + psa_generate_key_iop_t operation; + memset(&operation, 0, sizeof(operation)); + + * Initialize the object to logical zero values by declaring the object as static or global without an explicit initializer, for example: + + .. code-block:: xref + + static psa_generate_key_iop_t operation; + + * Initialize the object to the initializer `PSA_GENERATE_KEY_IOP_INIT`, for example: + + .. code-block:: xref + + psa_generate_key_iop_t operation = PSA_GENERATE_KEY_IOP_INIT; + + * Assign the result of the function `psa_generate_key_iop_init()` to the object, for example: + + .. code-block:: xref + + psa_generate_key_iop_t operation; + operation = psa_generate_key_iop_init(); + + This is an implementation-defined type. + Applications that make assumptions about the content of this object will result in implementation-specific behavior, and are non-portable. + +.. macro:: PSA_GENERATE_KEY_IOP_INIT + :definition: /* implementation-defined value */ + + .. summary:: + This macro evaluates to an initializer for an interruptible key-generation operation object of type `psa_generate_key_iop_t`. + + .. versionadded:: 1.6 + +.. function:: psa_generate_key_iop_init + + .. summary:: + Return an initial value for an interruptible key-generation operation object. + + .. versionadded:: 1.6 + + .. return:: psa_generate_key_iop_t + +.. function:: psa_generate_key_iop_get_num_ops + + .. summary:: + Get the number of *ops* that an interruptible key-generation operation has taken so far. + + .. versionadded:: 1.6 + + .. param:: psa_generate_key_iop_t * operation + The interruptible key-generation operation to inspect. + + .. return:: uint32_t + Number of *ops* that the operation has taken so far. + + After the interruptible operation has completed, the returned value is the number of *ops* spent on the entire operation. + The value is reset to zero by a successful call to either `psa_generate_key_iop_start()` or `psa_generate_key_iop_abort()`. + A failed call to `psa_generate_key_iop_start()` can also reset the value to zero. + + This function can be used to tune the value passed to `psa_iop_set_max_ops()`. + + The value is undefined if the operation object has not been initialized. + +.. function:: psa_generate_key_iop_start + + .. summary:: + Start an interruptible operation to generate a key or key pair. + + .. versionadded:: 1.6 + + .. param:: psa_generate_key_iop_t * operation + The interruptible key-generation operation to start. + It must have been initialized as per the documentation for `psa_generate_key_iop_t`, and be inactive. + .. param:: const psa_key_attributes_t * attributes + The attributes for the new key. + + The following attributes are required for all keys: + + * The key type. It must not be an asymmetric public key. + * The key size. It must be a valid size for the key type. + + The following attributes must be set for keys used in cryptographic operations: + + * The key permitted-algorithm policy, see :secref:`permitted-algorithms`. + * The key usage flags, see :secref:`key-usage-flags`. + + The following attributes must be set for keys that do not use the default volatile lifetime: + + * The key lifetime, see :secref:`key-lifetimes`. + * The key identifier is required for a key with a persistent lifetime, see :secref:`key-identifiers`. + + .. note:: + This is an input parameter: it is not updated with the final key attributes. + The final attributes of the new key can be queried by calling `psa_get_key_attributes()` with the key's identifier. + + .. return:: psa_status_t + .. retval:: PSA_SUCCESS + Success. + The interruptible operation can be configured with custom production parameters by calling `psa_generate_key_iop_custom()`, or completed by calling `psa_generate_key_iop_complete()`. + .. retval:: PSA_ERROR_ALREADY_EXISTS + This is an attempt to create a persistent key, and there is already a persistent key with the given identifier. + .. retval:: PSA_ERROR_NOT_SUPPORTED + The following conditions can result in this error: + + * The implementation does not support incremental generation of the requested key type. + * The key attributes, as a whole, are not supported, either by the implementation in general or in the specified storage location. + .. retval:: PSA_ERROR_INVALID_ARGUMENT + The following conditions can result in this error: + + * The key type is invalid, or is an asymmetric public key type. + * The key size is not valid for the key type. + * The key lifetime is invalid. + * The key identifier is not valid for the key lifetime. + * The key usage flags include invalid values. + * The key's permitted-usage algorithm is invalid. + * The key attributes, as a whole, are invalid. + .. retval:: PSA_ERROR_NOT_PERMITTED + The implementation does not permit creating a key with the specified attributes due to some implementation-specific policy. + .. retval:: PSA_ERROR_BAD_STATE + The following conditions can result in this error: + + * The operation state is not valid: it must be inactive. + * The library requires initializing by a call to `psa_crypto_init()`. + .. retval:: PSA_ERROR_INSUFFICIENT_MEMORY + .. retval:: PSA_ERROR_INSUFFICIENT_ENTROPY + .. retval:: PSA_ERROR_COMMUNICATION_FAILURE + .. retval:: PSA_ERROR_CORRUPTION_DETECTED + .. retval:: PSA_ERROR_INSUFFICIENT_STORAGE + .. retval:: PSA_ERROR_STORAGE_FAILURE + .. retval:: PSA_ERROR_DATA_CORRUPT + .. retval:: PSA_ERROR_DATA_INVALID + + This function starts the random generation of a new key. + The location, policy, type, and size of the key are taken from ``attributes``. + + If a persistent key identifier already exists, then it is unspecified whether `psa_generate_key_iop_start()` returns :code:`PSA_ERROR_ALREADY_EXISTS`, or whether `psa_generate_key_iop_complete()` returns this error. + Applications must be prepared for either function to report this error. + + Implementations must reject an attempt to generate a key of size ``0``. + + The following type-specific considerations apply: + + * For RSA keys (`PSA_KEY_TYPE_RSA_KEY_PAIR`), the public exponent is 65537. + The modulus is a product of two probabilistic primes between :math:`2^{n-1}` and :math:`2^n` where :math:`n` is the bit size specified in the attributes. + + After a successful call to `psa_generate_key_iop_start()`, the operation is active. + Before calling `psa_generate_key_iop_complete()`, the application can configure the operation with custom production parameters by calling `psa_generate_key_iop_custom()`. + The application must then call `psa_generate_key_iop_complete()` repeatedly, until it returns a status code that is not :code:`PSA_OPERATION_INCOMPLETE`. + Once active, the application must eventually terminate the operation. The following events terminate an operation: + + * A successful call to `psa_generate_key_iop_complete()`. + * A call to `psa_generate_key_iop_abort()`. + + If `psa_generate_key_iop_start()` returns an error, the operation object remains inactive, but its number of *ops* can be reset to zero. + +.. function:: psa_generate_key_iop_custom + + .. summary:: + Set custom production parameters for an interruptible key-generation operation. + + .. versionadded:: 1.6 + + .. param:: psa_generate_key_iop_t * operation + The interruptible key-generation operation to configure. + The operation must be active, and neither `psa_generate_key_iop_custom()` nor `psa_generate_key_iop_complete()` must have been called. + .. param:: const psa_custom_key_parameters_t * custom + Customized production parameters for the key generation. + .. param:: const uint8_t * custom_data + A buffer containing additional variable-sized production parameters. + .. param:: size_t custom_data_length + Length of ``custom_data`` in bytes. + + .. return:: psa_status_t + .. retval:: PSA_SUCCESS + Success. + .. retval:: PSA_ERROR_NOT_SUPPORTED + The production parameters are not supported by the implementation. + .. retval:: PSA_ERROR_INVALID_ARGUMENT + The production parameters are invalid. + .. retval:: PSA_ERROR_BAD_STATE + The following conditions can result in this error: + + * The operation state is not valid: it must be active, and neither `psa_generate_key_iop_custom()` nor `psa_generate_key_iop_complete()` must have been called. + * The library requires initializing by a call to `psa_crypto_init()`. + .. retval:: PSA_ERROR_INSUFFICIENT_MEMORY + .. retval:: PSA_ERROR_COMMUNICATION_FAILURE + .. retval:: PSA_ERROR_CORRUPTION_DETECTED + + This function sets custom production parameters for a key-generation operation. + The application must call `psa_generate_key_iop_start()` before calling this function. + It may call this function at most once for an operation. + + If this function is not called, the operation uses the default production parameters `PSA_CUSTOM_KEY_PARAMETERS_INIT` with ``custom_data_length == 0``. + + See the documentation of `psa_custom_key_parameters_t` for a list of non-default production parameters. See the key type definitions in :secref:`key-types` for details of the custom production parameters used for key generation. + + After a successful call to this function, the application must call `psa_generate_key_iop_complete()` repeatedly, until it returns a status code that is not :code:`PSA_OPERATION_INCOMPLETE`. + + If this function returns an error status, the operation enters an error state and must be aborted by calling `psa_generate_key_iop_abort()`. + +.. function:: psa_generate_key_iop_complete + + .. summary:: + Progress an interruptible operation to generate a key. + + .. versionadded:: 1.6 + + .. param:: psa_generate_key_iop_t * operation + The interruptible key-generation operation to use. + The operation must be active. + .. param:: psa_key_id_t * key + On success, an identifier for the newly created key. + `PSA_KEY_ID_NULL` on failure. + + .. return:: psa_status_t + .. retval:: PSA_SUCCESS + Success. + If the key is persistent, the key material and the key's metadata have been saved to persistent storage. + .. retval:: PSA_OPERATION_INCOMPLETE + The function was interrupted after exhausting the maximum *ops*. + The computation is incomplete, and this function must be called again with the same operation object to continue. + .. retval:: PSA_ERROR_ALREADY_EXISTS + This is an attempt to create a persistent key, and there is already a persistent key with the given identifier. + .. retval:: PSA_ERROR_BAD_STATE + The following conditions can result in this error: + + * The operation state is not valid: it must be active. + * The library requires initializing by a call to `psa_crypto_init()`. + .. retval:: PSA_ERROR_INSUFFICIENT_MEMORY + .. retval:: PSA_ERROR_COMMUNICATION_FAILURE + .. retval:: PSA_ERROR_CORRUPTION_DETECTED + .. retval:: PSA_ERROR_INSUFFICIENT_STORAGE + .. retval:: PSA_ERROR_STORAGE_FAILURE + .. retval:: PSA_ERROR_DATA_CORRUPT + .. retval:: PSA_ERROR_DATA_INVALID + .. retval:: PSA_ERROR_INSUFFICIENT_ENTROPY + + .. note:: + This is an interruptible function, and must be called repeatedly, until it returns a status code that is not :code:`PSA_OPERATION_INCOMPLETE`. + + If a persistent key identifier already exists, then it is unspecified whether `psa_generate_key_iop_start()` returns :code:`PSA_ERROR_ALREADY_EXISTS`, or whether this function returns this error. + Applications must be prepared for either function to report this error. + + When this function returns successfully, the new key is returned in ``key``, and the operation becomes inactive. + If this function returns :code:`PSA_OPERATION_INCOMPLETE`, no key is returned, and this function must be called again to continue the operation. + If this function returns an error status, the operation enters an error state and must be aborted by calling `psa_generate_key_iop_abort()`. + + The amount of calculation performed in a single call to this function is determined by the maximum *ops* setting. See `psa_iop_set_max_ops()`. + +.. function:: psa_generate_key_iop_abort + + .. summary:: + Abort an interruptible key-generation operation. + + .. versionadded:: 1.6 + + .. param:: psa_generate_key_iop_t * operation + The interruptible key-generation operation to abort. + + .. return:: psa_status_t + .. retval:: PSA_SUCCESS + Success. + The operation object can now be discarded or reused. + .. retval:: PSA_ERROR_COMMUNICATION_FAILURE + .. retval:: PSA_ERROR_CORRUPTION_DETECTED + .. retval:: PSA_ERROR_BAD_STATE + The library requires initializing by a call to `psa_crypto_init()`. + + 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_generate_key_iop_start()` again. + + This function can be called at any time after the operation object has been initialized as described in `psa_generate_key_iop_t`. + + In particular, it is valid to call `psa_generate_key_iop_abort()` twice, or to call `psa_generate_key_iop_abort()` on an operation that has not been started. + +.. _interruptible-export-key: + +Interruptible public-key export +------------------------------- + +Extracting a public key from an asymmetric key pair can be computationally expensive. +For example, computing an elliptic curve public key from the private key. + +An interruptible public-key export operation can be used instead of calling `psa_export_public_key()`, in applications that have bounded execution time requirements for use cases that require public-key export. + +An interruptible public-key export operation is used as follows: + +1. Allocate an interruptible public-key export operation object, of type `psa_export_public_key_iop_t`, which will be passed to all the functions listed here. +#. Initialize the operation object with one of the methods described in the documentation for `psa_export_public_key_iop_t`, for example, `PSA_EXPORT_PUBLIC_KEY_IOP_INIT`. +#. Call `psa_export_public_key_iop_start()` to specify the key to export. +#. Call `psa_export_public_key_iop_complete()` to finish exporting the key data, until this function returns a status code other than :code:`PSA_OPERATION_INCOMPLETE`. +#. If an error occurs at any stage, or to terminate the operation early, call `psa_export_public_key_iop_abort()`. + +.. typedef:: /* implementation-defined type */ psa_export_public_key_iop_t + + .. summary:: + The type of the state data structure for an interruptible public-key export operation. + + .. versionadded:: 1.6 + + Before calling any function on an interruptible public-key export operation object, the application must initialize it by any of the following means: + + * Set the object to all-bits-zero, for example: + + .. code-block:: xref + + psa_export_public_key_iop_t operation; + memset(&operation, 0, sizeof(operation)); + + * Initialize the object to logical zero values by declaring the object as static or global without an explicit initializer, for example: + + .. code-block:: xref + + static psa_export_public_key_iop_t operation; + + * Initialize the object to the initializer `PSA_EXPORT_PUBLIC_KEY_IOP_INIT`, for example: + + .. code-block:: xref + + psa_export_public_key_iop_t operation = PSA_EXPORT_PUBLIC_KEY_IOP_INIT; + + * Assign the result of the function `psa_export_public_key_iop_init()` to the object, for example: + + .. code-block:: xref + + psa_export_public_key_iop_t operation; + operation = psa_export_public_key_iop_init(); + + This is an implementation-defined type. + Applications that make assumptions about the content of this object will result in implementation-specific behavior, and are non-portable. + +.. macro:: PSA_EXPORT_PUBLIC_KEY_IOP_INIT + :definition: /* implementation-defined value */ + + .. summary:: + This macro evaluates to an initializer for an interruptible public-key export operation object of type `psa_export_public_key_iop_t`. + + .. versionadded:: 1.6 + +.. function:: psa_export_public_key_iop_init + + .. summary:: + Return an initial value for an interruptible public-key export operation object. + + .. versionadded:: 1.6 + + .. return:: psa_export_public_key_iop_t + +.. function:: psa_export_public_key_iop_get_num_ops + + .. summary:: + Get the number of *ops* that an interruptible public-key export operation has taken so far. + + .. versionadded:: 1.6 + + .. param:: psa_export_public_key_iop_t * operation + The interruptible public-key export operation to inspect. + + .. return:: uint32_t + Number of *ops* that the operation has taken so far. + + After the interruptible operation has completed, the returned value is the number of *ops* spent on the entire operation. + The value is reset to zero by a successful call to either `psa_export_public_key_iop_start()` or `psa_export_public_key_iop_abort()`. + A failed call to `psa_export_public_key_iop_start()` can also reset the value to zero. + + This function can be used to tune the value passed to `psa_iop_set_max_ops()`. + + The value is undefined if the operation object has not been initialized. + +.. function:: psa_export_public_key_iop_start + + .. summary:: + Start an interruptible operation to export a public key or the public part of a key pair in binary format. + + .. versionadded:: 1.6 + + .. param:: psa_export_public_key_iop_t * operation + The interruptible public-key export operation to start. + It must have been initialized as per the documentation for `psa_export_public_key_iop_t`, and be inactive. + .. param:: psa_key_id_t key + Identifier of the key to export. + + .. return:: psa_status_t + .. retval:: PSA_SUCCESS + Success. + The interruptible operation must now be completed by calling `psa_export_public_key_iop_complete()`. + .. retval:: PSA_ERROR_INVALID_HANDLE + ``key`` is not a valid key identifier. + .. retval:: PSA_ERROR_INVALID_ARGUMENT + The key is neither a public key nor a key pair. + .. retval:: PSA_ERROR_NOT_SUPPORTED + The following conditions can result in this error: + + * The key's storage location does not support export of the key. + * The implementation does not support export of keys with this key type. + .. retval:: PSA_ERROR_BAD_STATE + The following conditions can result in this error: + + * The operation state is not valid: it must be inactive. + * The library requires initializing by a call to `psa_crypto_init()`. + .. retval:: PSA_ERROR_COMMUNICATION_FAILURE + .. retval:: PSA_ERROR_CORRUPTION_DETECTED + .. retval:: PSA_ERROR_STORAGE_FAILURE + .. retval:: PSA_ERROR_DATA_CORRUPT + .. retval:: PSA_ERROR_DATA_INVALID + .. retval:: PSA_ERROR_INSUFFICIENT_MEMORY + + This function starts the export of a public key in binary format. + For standard key types, the output format is defined in the relevant *Key format* section in :secref:`key-types`. + + Exporting a public key object or the public part of a key pair is always permitted, regardless of the key's usage flags. + + After a successful call to `psa_export_public_key_iop_start()`, the operation is active. + The operation can be completed by calling `psa_export_public_key_iop_complete()` repeatedly, until it returns a status code that is not :code:`PSA_OPERATION_INCOMPLETE`. + Once active, the application must eventually terminate the operation. + The following events terminate an operation: + + * A successful call to `psa_export_public_key_iop_complete()`. + * A call to `psa_export_public_key_iop_abort()`. + + If `psa_export_public_key_iop_start()` returns an error, the operation object remains inactive, but its number of *ops* can be reset to zero. + +.. function:: psa_export_public_key_iop_complete + + .. summary:: + Progress an interruptible operation to export a public key. + + .. versionadded:: 1.6 + + .. param:: psa_export_public_key_iop_t * operation + The interruptible public-key export operation to use. + The operation must be active. + .. param:: uint8_t * data + Buffer where the key data is to be written. + .. param:: size_t data_size + Size of the ``data`` buffer in bytes. + This must be appropriate for the key: + + * The required output size is :code:`PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(type, bits)` where ``type`` is the key type and ``bits`` is the key size in bits. + * `PSA_EXPORT_PUBLIC_KEY_MAX_SIZE` evaluates to the maximum output size of any supported public key or public part of a key pair. + * `PSA_EXPORT_ASYMMETRIC_KEY_MAX_SIZE` evaluates to the maximum output size of any supported public key or key pair. + .. param:: size_t * data_length + On success, the number of bytes that make up the key data. + + .. return:: psa_status_t + .. retval:: PSA_SUCCESS + Success. + The first ``(*data_length)`` bytes of ``data`` contain the exported public key. + .. retval:: PSA_OPERATION_INCOMPLETE + The function was interrupted after exhausting the maximum *ops*. + The computation is incomplete, and this function must be called again with the same operation object to continue. + .. retval:: PSA_ERROR_BUFFER_TOO_SMALL + The size of the ``data`` buffer is too small. + `PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE()`, `PSA_EXPORT_PUBLIC_KEY_MAX_SIZE`, or `PSA_EXPORT_ASYMMETRIC_KEY_MAX_SIZE` can be used to determine a sufficient buffer size. + .. retval:: PSA_ERROR_BAD_STATE + The following conditions can result in this error: + + * The operation state is not valid: it must be active. + * The library requires initializing by a call to `psa_crypto_init()`. + .. retval:: PSA_ERROR_COMMUNICATION_FAILURE + .. retval:: PSA_ERROR_CORRUPTION_DETECTED + .. retval:: PSA_ERROR_STORAGE_FAILURE + .. retval:: PSA_ERROR_DATA_CORRUPT + .. retval:: PSA_ERROR_DATA_INVALID + .. retval:: PSA_ERROR_INSUFFICIENT_MEMORY + + .. note:: + This is an interruptible function, and must be called repeatedly, until it returns a status code that is not :code:`PSA_OPERATION_INCOMPLETE`. + + When this function returns successfully, the public key data is returned in ``data``, and the operation becomes inactive. + The output of this function can be passed to `psa_import_key()` to create a new key that is equivalent to the public key. + + If this function returns :code:`PSA_OPERATION_INCOMPLETE`, no key is returned, and this function must be called again to continue the operation. + If this function returns an error status, the operation enters an error state and must be aborted by calling `psa_export_public_key_iop_abort()`. + + The amount of calculation performed in a single call to this function is determined by the maximum *ops* setting. See `psa_iop_set_max_ops()`. + + .. note:: + + If the implementation of `psa_import_key()` supports other formats beyond the format specified here, the output from `psa_export_public_key_iop_complete()` must use the representation specified in :secref:`key-types`, not the originally imported representation. + +.. function:: psa_export_public_key_iop_abort + + .. summary:: + Abort an interruptible public-key export operation. + + .. versionadded:: 1.6 + + .. param:: psa_export_public_key_iop_t * operation + The interruptible public-key export operation to abort. + + .. return:: psa_status_t + .. retval:: PSA_SUCCESS + Success. + The operation object can now be discarded or reused. + .. retval:: PSA_ERROR_COMMUNICATION_FAILURE + .. retval:: PSA_ERROR_CORRUPTION_DETECTED + .. retval:: PSA_ERROR_BAD_STATE + The library requires initializing by a call to `psa_crypto_init()`. + + 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_export_public_key_iop_start()` again. + + This function can be called at any time after the operation object has been initialized as described in `psa_export_public_key_iop_t`. + + In particular, it is valid to call `psa_export_public_key_iop_abort()` twice, or to call `psa_export_public_key_iop_abort()` on an operation that has not been started. diff --git a/doc/crypto/api/keys/policy.rst b/doc/crypto/api/keys/policy.rst index afad2bfb..afae5a5b 100644 --- a/doc/crypto/api/keys/policy.rst +++ b/doc/crypto/api/keys/policy.rst @@ -210,6 +210,7 @@ The usage flags are encoded in a bitmask, which has the type `psa_key_usage_t`. * `psa_sign_message()` * `psa_sign_message_with_context()` * `psa_sign_setup()` + * `psa_sign_iop_setup_start()`, when signing a message. For a key pair, this concerns the private key. @@ -226,6 +227,8 @@ The usage flags are encoded in a bitmask, which has the type `psa_key_usage_t`. * `psa_verify_message()` * `psa_verify_message_with_context()` * `psa_verify_setup()` + * `psa_verify_iop_setup_start()`, when verifying the signature of a message. + * `psa_verify_iop_setup_deferred_signature_start()` For a key pair, this concerns the public key. @@ -239,6 +242,7 @@ The usage flags are encoded in a bitmask, which has the type `psa_key_usage_t`. * `psa_sign_hash()` * `psa_sign_hash_with_context()` + * `psa_sign_iop_setup_start()` when signing a pre-computed hash. This flag automatically sets `PSA_KEY_USAGE_SIGN_MESSAGE`: if an application sets the flag `PSA_KEY_USAGE_SIGN_HASH` when creating a key, then the key always has the permissions conveyed by `PSA_KEY_USAGE_SIGN_MESSAGE`, and the flag `PSA_KEY_USAGE_SIGN_MESSAGE` will also be present when the application queries the usage flags of the key. @@ -254,6 +258,7 @@ The usage flags are encoded in a bitmask, which has the type `psa_key_usage_t`. * `psa_verify_hash()` * `psa_verify_hash_with_context()` + * `psa_verify_iop_setup_start()`, when verifying the signature of a pre-computed hash. This flag automatically sets `PSA_KEY_USAGE_VERIFY_MESSAGE`: if an application sets the flag `PSA_KEY_USAGE_VERIFY_HASH` when creating a key, then the key always has the permissions conveyed by `PSA_KEY_USAGE_VERIFY_MESSAGE`, and the flag `PSA_KEY_USAGE_VERIFY_MESSAGE` will also be present when the application queries the usage flags of the key. @@ -270,6 +275,7 @@ The usage flags are encoded in a bitmask, which has the type `psa_key_usage_t`. This flag must be present on keys used with the following APIs: * `psa_key_agreement()` + * `psa_key_agreement_iop_start()` * `psa_key_derivation_key_agreement()` * `psa_raw_key_agreement()` diff --git a/doc/crypto/api/library/library.rst b/doc/crypto/api/library/library.rst index c101e650..59e17514 100644 --- a/doc/crypto/api/library/library.rst +++ b/doc/crypto/api/library/library.rst @@ -74,3 +74,60 @@ Library initialization .. warning:: The set of functions that depend on successful initialization of the library is :scterm:`IMPLEMENTATION DEFINED`. Applications that rely on calling functions before initializing the library might not be portable to other implementations. + + +Interruptible operation limit +----------------------------- + +An interruptible operation lets an application limit the computation performed by an individual function call. The limit is controlled by the *maximum ops* value. + +See :secref:`interruptible-operations`. + +.. function:: psa_iop_set_max_ops + + .. summary:: + Set the maximum number of *ops* allowed to be executed by an interruptible function in a single call. + + .. versionadded:: 1.6 + + .. param:: uint32_t max_ops + The maximum number of *ops* to execute in a single call. This can be a value from ``0`` to `PSA_IOP_MAX_OPS_UNLIMITED`. + + .. return:: void + + Interruptible functions use this value to limit the computation in a single call. If the limit is reached before the operation is complete, the function returns :code:`PSA_OPERATION_INCOMPLETE`. The application must call the function again until it returns a different status, or abort the operation. + + After implementation initialization, the maximum *ops* defaults to `PSA_IOP_MAX_OPS_UNLIMITED`. This permits an interruptible function to finish its calculation before returning. Call `psa_iop_set_max_ops()` to set a limit. + + .. note:: + + The computation and time represented by an *op* are implementation- and function-specific. They can depend on the hardware, algorithm, key type, curve, and current stage of the operation. Successive *ops* in one operation can take different amounts of time. The ``psa_xxx_iop_get_num_ops()`` functions can help tune this value. + + .. admonition:: Implementation note + + The interpretation of the maximum is implementation-defined. An implementation intended for a hard real-time system can interpret it as a hard execution limit. An implementation for a non-real-time system can use a less strict interpretation. In either case, the implementation documentation should describe how it applies the limit. + + .. warning:: + With implementations that interpret this value as a hard limit, setting it too low can prevent any useful computation. Repeated calls can then return :code:`PSA_OPERATION_INCOMPLETE` without completing the operation. + +.. function:: psa_iop_get_max_ops + + .. summary:: + Get the maximum number of *ops* allowed to be executed by an interruptible function in a single call. + + .. versionadded:: 1.6 + + .. return:: uint32_t + Maximum number of *ops* allowed to be executed by an interruptible function in a single call. + + This returns the value last set by `psa_iop_set_max_ops()`, or `PSA_IOP_MAX_OPS_UNLIMITED` if the application has not set a value. + +.. macro:: PSA_IOP_MAX_OPS_UNLIMITED + :definition: UINT32_MAX + + .. summary:: + Maximum value for use with `psa_iop_set_max_ops()`. + + .. versionadded:: 1.6 + + Using this value in a call to `psa_iop_set_max_ops()` permits interruptible functions to complete their calculation before returning. diff --git a/doc/crypto/api/library/status.rst b/doc/crypto/api/library/status.rst index 7dbebd26..adc2c008 100644 --- a/doc/crypto/api/library/status.rst +++ b/doc/crypto/api/library/status.rst @@ -37,6 +37,8 @@ The following elements are defined in :file:`psa/error.h` from :cite-title:`PSA #define PSA_ERROR_DATA_CORRUPT ((psa_status_t)-152) #define PSA_ERROR_DATA_INVALID ((psa_status_t)-153) + #define PSA_OPERATION_INCOMPLETE ((psa_status_t)-248) + These definitions must be available to an application that includes the :file:`psa/crypto.h` header file. .. admonition:: Implementation note diff --git a/doc/crypto/api/ops/key-agreement.rst b/doc/crypto/api/ops/key-agreement.rst index 8ac4148c..9c4c9760 100644 --- a/doc/crypto/api/ops/key-agreement.rst +++ b/doc/crypto/api/ops/key-agreement.rst @@ -9,7 +9,7 @@ Key agreement ============= -Three functions are provided for a Diffie-Hellman-style key agreement where each party combines its own private key with the peer’s public key, to produce a shared secret value: +Three functions are provided for a Diffie-Hellman-style key agreement where each party combines its own private key with the peer's public key, to produce a shared secret value: * A call to `psa_key_agreement()` will compute the shared secret and store the result in a new derivation key. @@ -17,7 +17,10 @@ Three functions are provided for a Diffie-Hellman-style key agreement where each * Where an application needs direct access to the shared secret, it can call `psa_raw_key_agreement()` instead. -Using `psa_key_agreement()` or `psa_key_derivation_key_agreement()` is recommended, as these do not expose the shared secret to the application. +If an application requires bounded execution time during a key agreement, it can use an interruptible key-agreement operation. +See :secref:`interruptible-key-agreement`. + +Using `psa_key_agreement()`, `psa_key_derivation_key_agreement()`, or an interruptible key-agreement operation is recommended, as these do not expose the shared secret to the application. .. note:: @@ -173,7 +176,7 @@ Standalone key agreement .. return:: psa_status_t .. retval:: PSA_SUCCESS Success. - The new key contains the share secret. + The new key contains the shared secret. If the key is persistent, the key material and the key's metadata have been saved to persistent storage. .. retval:: PSA_ERROR_INVALID_HANDLE ``private_key`` is not a valid key identifier. @@ -228,6 +231,9 @@ Standalone key agreement .. warning:: The shared secret resulting from a key-agreement algorithm such as finite field Diffie-Hellman or elliptic curve Diffie-Hellman has biases. This makes it unsuitable for use as key material, for example, as an AES key. Instead, it is recommended that a key-derivation algorithm is applied to the result, to derive unbiased cryptographic keys. + If an application requires bounded execution time during key agreement, it can use an interruptible key-agreement operation. + See :secref:`interruptible-key-agreement`. + .. function:: psa_raw_key_agreement .. summary:: @@ -352,6 +358,297 @@ Combining key agreement and key derivation Instead, the application can call `psa_key_agreement()` to obtain the shared secret as a derivation key. This key can be used as input to as many key-derivation operations as required. +.. _interruptible-key-agreement: + +Interruptible key agreement +--------------------------- + +Most key-agreement algorithms are computationally expensive. + +An interruptible key-agreement operation can be used instead of calling `psa_key_agreement()`, in applications that have bounded execution time requirements for use cases involving key agreement. + +An interruptible key-agreement operation is used as follows: + +1. Allocate an interruptible key-agreement operation object, of type `psa_key_agreement_iop_t`, which will be passed to all the functions listed here. +#. Initialize the operation object with one of the methods described in the documentation for `psa_key_agreement_iop_t`, for example, `PSA_KEY_AGREEMENT_IOP_INIT`. +#. Call `psa_key_agreement_iop_start()` to specify the algorithm, and provide the private key and the peer public key. +#. Call `psa_key_agreement_iop_complete()` to finish the key agreement and output the shared secret, until this function returns a status code other than :code:`PSA_OPERATION_INCOMPLETE`. +#. If an error occurs at any stage, or to terminate the operation early, call `psa_key_agreement_iop_abort()`. + + +.. typedef:: /* implementation-defined type */ psa_key_agreement_iop_t + + .. summary:: + The type of the state data structure for an interruptible key-agreement operation. + + .. versionadded:: 1.6 + + Before calling any function on an interruptible key-agreement operation object, the application must initialize it by any of the following means: + + * Set the object to all-bits-zero, for example: + + .. code-block:: xref + + psa_key_agreement_iop_t operation; + memset(&operation, 0, sizeof(operation)); + + * Initialize the object to logical zero values by declaring the object as static or global without an explicit initializer, for example: + + .. code-block:: xref + + static psa_key_agreement_iop_t operation; + + * Initialize the object to the initializer `PSA_KEY_AGREEMENT_IOP_INIT`, for example: + + .. code-block:: xref + + psa_key_agreement_iop_t operation = PSA_KEY_AGREEMENT_IOP_INIT; + + * Assign the result of the function `psa_key_agreement_iop_init()` to the object, for example: + + .. code-block:: xref + + psa_key_agreement_iop_t operation; + operation = psa_key_agreement_iop_init(); + + This is an implementation-defined type. + Applications that make assumptions about the content of this object will result in implementation-specific behavior, and are non-portable. + +.. macro:: PSA_KEY_AGREEMENT_IOP_INIT + :definition: /* implementation-defined value */ + + .. summary:: + This macro evaluates to an initializer for an interruptible key-agreement operation object of type `psa_key_agreement_iop_t`. + + .. versionadded:: 1.6 + +.. function:: psa_key_agreement_iop_init + + .. summary:: + Return an initial value for an interruptible key-agreement operation object. + + .. versionadded:: 1.6 + + .. return:: psa_key_agreement_iop_t + +.. function:: psa_key_agreement_iop_get_num_ops + + .. summary:: + Get the number of *ops* that an interruptible key-agreement operation has taken so far. + + .. versionadded:: 1.6 + + .. param:: psa_key_agreement_iop_t * operation + The interruptible key-agreement operation to inspect. + + .. return:: uint32_t + Number of *ops* that the operation has taken so far. + + After the interruptible operation has completed, the returned value is the number of *ops* spent on the entire operation. + The value is reset to zero by a successful call to either `psa_key_agreement_iop_start()` or `psa_key_agreement_iop_abort()`. + A failed call to `psa_key_agreement_iop_start()` can also reset the value to zero. + + This function can be used to tune the value passed to `psa_iop_set_max_ops()`. + + The value is undefined if the operation object has not been initialized. + +.. function:: psa_key_agreement_iop_start + + .. summary:: + Start an interruptible operation to perform a key agreement. + + .. versionadded:: 1.6 + + .. param:: psa_key_agreement_iop_t * operation + The interruptible key-agreement operation to start. + It must have been initialized as per the documentation for `psa_key_agreement_iop_t`, and be inactive. + .. param:: psa_key_id_t private_key + Identifier of the private key to use. + It must permit the usage `PSA_KEY_USAGE_DERIVE`. + .. param:: const uint8_t * peer_key + Public key of the peer. + The peer key data is parsed with the type :code:`PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type)` where ``type`` is the type of ``private_key``, and with the same bit-size as ``private_key``. + The peer key must be in the format that `psa_import_key()` accepts for this public key type. + These formats are described with the public key type in :secref:`key-types`. + .. param:: size_t peer_key_length + Size of ``peer_key`` in bytes. + .. param:: psa_algorithm_t alg + The standalone key-agreement algorithm to compute: a value of type `psa_algorithm_t` such that :code:`PSA_ALG_IS_STANDALONE_KEY_AGREEMENT(alg)` is true. + .. param:: const psa_key_attributes_t * attributes + The attributes for the key to be output on completion. + + The following attributes are required for all keys: + + * The key type, which must be one of `PSA_KEY_TYPE_DERIVE`, `PSA_KEY_TYPE_RAW_DATA`, `PSA_KEY_TYPE_HMAC`, or `PSA_KEY_TYPE_PASSWORD`. + + Implementations must support the `PSA_KEY_TYPE_DERIVE` and `PSA_KEY_TYPE_RAW_DATA` key types. + + The following attributes must be set for keys used in cryptographic operations: + + * The key permitted-algorithm policy, see :secref:`permitted-algorithms`. + * The key usage flags, see :secref:`key-usage-flags`. + + The following attributes must be set for keys that do not use the default volatile lifetime: + + * The key lifetime, see :secref:`key-lifetimes`. + * The key identifier is required for a key with a persistent lifetime, see :secref:`key-identifiers`. + + The following attributes are optional: + + * If the key size is nonzero, it must be equal to the output size of the key agreement, in bits. + + The output size, in bits, of the key agreement is :code:`8 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(type, bits)`, where ``type`` and ``bits`` are the type and bit-size of ``private_key``. + + .. note:: + This is an input parameter: it is not updated with the final key attributes. + The final attributes of the new key can be queried by calling `psa_get_key_attributes()` with the key's identifier. + + .. return:: psa_status_t + .. retval:: PSA_SUCCESS + Success. + The interruptible operation must now be completed by calling `psa_key_agreement_iop_complete()`. + .. retval:: PSA_ERROR_INVALID_HANDLE + ``private_key`` is not a valid key identifier. + .. retval:: PSA_ERROR_NOT_PERMITTED + The following conditions can result in this error: + + * ``private_key`` does not have the `PSA_KEY_USAGE_DERIVE` flag, or it does not permit the requested algorithm. + * The implementation does not permit creating a key with the specified attributes due to some implementation-specific policy. + .. retval:: PSA_ERROR_ALREADY_EXISTS + This is an attempt to create a persistent key, and there is already a persistent key with the given identifier. + .. retval:: PSA_ERROR_INVALID_ARGUMENT + The following conditions can result in this error: + + * ``alg`` is not a key-agreement algorithm. + * ``private_key`` is not compatible with ``alg``. + * ``peer_key`` is not a valid public key corresponding to ``private_key``. + * The output key attributes in ``attributes`` are not valid : + + - The key type is not valid for key-agreement output. + - The key size is nonzero, and is not the size of the shared secret. + - The key lifetime is invalid. + - The key identifier is not valid for the key lifetime. + - The key usage flags include invalid values. + - The key's permitted-usage algorithm is invalid. + - The key attributes, as a whole, are invalid. + + .. retval:: PSA_ERROR_NOT_SUPPORTED + The following conditions can result in this error: + + * ``alg`` is not supported or is not a key-agreement algorithm. + * ``private_key`` is not supported for use with ``alg``. + * The output key attributes, as a whole, are not supported, either by the implementation in general or in the specified storage location. + .. retval:: PSA_ERROR_BAD_STATE + The following conditions can result in this error: + + * The operation state is not valid: it must be inactive. + * The library requires initializing by a call to `psa_crypto_init()`. + .. retval:: PSA_ERROR_INSUFFICIENT_MEMORY + .. retval:: PSA_ERROR_COMMUNICATION_FAILURE + .. retval:: PSA_ERROR_CORRUPTION_DETECTED + .. retval:: PSA_ERROR_STORAGE_FAILURE + .. retval:: PSA_ERROR_DATA_CORRUPT + .. retval:: PSA_ERROR_DATA_INVALID + .. retval:: PSA_ERROR_INSUFFICIENT_STORAGE + + This function starts an interruptible operation to perform a key-agreement. + A key-agreement algorithm takes two inputs: a private key ``private_key``, and a public key ``peer_key``. + + When the interruptible operation completes, the shared secret is output in a key. The key's location, policy, and type are taken from ``attributes``. The size of the key is always the bit-size of the shared secret, rounded up to a whole number of bytes. + + If a persistent key identifier already exists, then it is unspecified whether `psa_key_agreement_iop_start()` returns :code:`PSA_ERROR_ALREADY_EXISTS`, or whether `psa_key_agreement_iop_complete()` returns this error. + Applications must be prepared for either function to report this error. + + After a successful call to `psa_key_agreement_iop_start()`, the operation is active. + The operation can be completed by calling `psa_key_agreement_iop_complete()` repeatedly, until it returns a status code that is not :code:`PSA_OPERATION_INCOMPLETE`. + Once active, the application must eventually terminate the operation. + The following events terminate an operation: + + * A successful call to `psa_key_agreement_iop_complete()`. + * A call to `psa_key_agreement_iop_abort()`. + + If `psa_key_agreement_iop_start()` returns an error, the operation object remains inactive, but its number of *ops* can be reset to zero. + +.. function:: psa_key_agreement_iop_complete + + .. summary:: + Progress an interruptible key-agreement operation and return the shared secret. + + .. versionadded:: 1.6 + + .. param:: psa_key_agreement_iop_t * operation + The interruptible key-agreement operation to use. + The operation must be active. + .. param:: psa_key_id_t * key + On success, an identifier for the newly created key. + `PSA_KEY_ID_NULL` if the function returns :code:`PSA_OPERATION_INCOMPLETE` or an error status. + + .. return:: psa_status_t + .. retval:: PSA_SUCCESS + Success. + The new key contains the shared secret. + If the key is persistent, the key material and the key's metadata have been saved to persistent storage. + .. retval:: PSA_OPERATION_INCOMPLETE + The function was interrupted after exhausting the maximum *ops*. + The computation is incomplete, and this function must be called again with the same operation object to continue. + .. retval:: PSA_ERROR_ALREADY_EXISTS + This is an attempt to create a persistent key, and there is already a persistent key with the given identifier. + .. retval:: PSA_ERROR_BAD_STATE + The following conditions can result in this error: + + * The operation state is not valid: it must be active. + * The library requires initializing by a call to `psa_crypto_init()`. + .. retval:: PSA_ERROR_INSUFFICIENT_MEMORY + .. retval:: PSA_ERROR_COMMUNICATION_FAILURE + .. retval:: PSA_ERROR_CORRUPTION_DETECTED + .. retval:: PSA_ERROR_INSUFFICIENT_STORAGE + .. retval:: PSA_ERROR_STORAGE_FAILURE + .. retval:: PSA_ERROR_DATA_CORRUPT + .. retval:: PSA_ERROR_DATA_INVALID + + .. note:: + This is an interruptible function, and must be called repeatedly, until it returns a status code that is not :code:`PSA_OPERATION_INCOMPLETE`. + + If a persistent key identifier already exists, then it is unspecified whether `psa_key_agreement_iop_start()` returns :code:`PSA_ERROR_ALREADY_EXISTS`, or whether this function returns this error. + Applications must be prepared for either function to report this error. + + When this function returns successfully, the shared secret is returned as a derivation key in ``key``, and the operation becomes inactive. + The attributes of the new key are specified in the call to `psa_key_agreement_iop_start()` used to start this operation. + This key can be input to a key derivation operation using `psa_key_derivation_input_key()`. + + .. warning:: + The shared secret resulting from a key-agreement algorithm such as finite-field Diffie-Hellman or elliptic curve Diffie-Hellman has biases. This makes it unsuitable for use as key material, for example, as an AES key. Instead, it is recommended that a key derivation algorithm is applied to the result, to derive unbiased cryptographic keys. + + If this function returns :code:`PSA_OPERATION_INCOMPLETE`, ``*key`` is `PSA_KEY_ID_NULL`, and this function must be called again to continue the operation. + If this function returns an error status, the operation enters an error state and must be aborted by calling `psa_key_agreement_iop_abort()`. + + The amount of calculation performed in a single call to this function is determined by the maximum *ops* setting. See `psa_iop_set_max_ops()`. + +.. function:: psa_key_agreement_iop_abort + + .. summary:: + Abort an interruptible key-agreement operation. + + .. versionadded:: 1.6 + + .. param:: psa_key_agreement_iop_t * operation + The interruptible key-agreement operation to abort. + + .. return:: psa_status_t + .. retval:: PSA_SUCCESS + Success. + The operation object can now be discarded or reused. + .. retval:: PSA_ERROR_COMMUNICATION_FAILURE + .. retval:: PSA_ERROR_CORRUPTION_DETECTED + .. retval:: PSA_ERROR_BAD_STATE + The library requires initializing by a call to `psa_crypto_init()`. + + 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_agreement_iop_start()` again. + + This function can be called at any time after the operation object has been initialized as described in `psa_key_agreement_iop_t`. + + In particular, it is valid to call `psa_key_agreement_iop_abort()` twice, or to call `psa_key_agreement_iop_abort()` on an operation that has not been started. + Support macros -------------- diff --git a/doc/crypto/api/ops/signature.rst b/doc/crypto/api/ops/signature.rst index 04f5254c..b0a24fb2 100644 --- a/doc/crypto/api/ops/signature.rst +++ b/doc/crypto/api/ops/signature.rst @@ -129,6 +129,18 @@ For large or streamed messages, it might be necessary to compute or verify a sig See :secref:`multi-part-signature`. +.. rubric:: Signature interruptible operations + +Some applications need to bound the expensive computation performed in an individual signature API call. + +* Version 1.6 of the |API| introduces optional interruptible signature and verification operations, `psa_sign_iop_t` and `psa_verify_iop_t`. They limit the computation performed in a call and can return :code:`PSA_OPERATION_INCOMPLETE` when further calls are required. + + These operations are distinct from the multi-part operations and are intended for applications that require bounded execution time. If the algorithm has a context parameter, an interruptible operation uses a zero-length context unless the application calls `psa_sign_iop_set_context()` or `psa_verify_iop_set_context()`. + + Interruptible verification normally receives the signature when the operation is set up. A streaming protocol that provides the signature after the message data can use the deferred-signature verification flow, if the selected algorithm supports it. + +See :secref:`interruptible-sign` and :secref:`interruptible-verify`. + .. _rsa-sign-algorithms: RSA signature algorithms @@ -151,7 +163,7 @@ RSA signature algorithms This hash-and-sign signature algorithm can be used with both the message and hash signature functions. RSA PKCS#1 v1.5 does not have a context parameter. However, the sign or verify with context functions can be used with a zero-length context. - This algorithm can be used with the multi-part sign and verify operations. + This algorithm can be used with the multi-part sign and verify operations. It can also be used with the interruptible signature and verification operations. This signature scheme is defined by :RFC-title:`8017#8.2` under the name RSASSA-PKCS1-v1_5. @@ -206,9 +218,8 @@ RSA signature algorithms .. summary:: The raw RSA PKCS#1 v1.5 signature algorithm, without hashing. - This specialized signature algorithm can only be used with the `psa_sign_hash()` and `psa_verify_hash()` functions. + This specialized signature algorithm can only be used with the `psa_sign_hash()` and `psa_verify_hash()` functions, their context variants with a zero-length context, or the interruptible signature and verification operations. RSA PKCS#1 v1.5 does not have a context parameter. - However, `psa_sign_hash_with_context()` or `psa_verify_hash_with_context()` can be used with a zero-length context. This signature scheme is defined by :RFC-title:`8017#8.2` under the name RSASSA-PKCS1-v1_5. @@ -245,7 +256,7 @@ RSA signature algorithms This hash-and-sign signature algorithm can be used with both the message and hash signature functions. RSA PSS does not have a context parameter. However, the sign or verify with context functions can be used with a zero-length context. - This algorithm can be used with the multi-part sign and verify operations. + This algorithm can be used with the multi-part sign and verify operations. It can also be used with the interruptible signature and verification operations. This algorithm is randomized: each invocation returns a different, equally valid signature. @@ -287,7 +298,7 @@ RSA signature algorithms This hash-and-sign signature algorithm can be used with both the message and hash signature functions. RSA PSS does not have a context parameter. However, the sign or verify with context functions can be used with a zero-length context. - This algorithm can be used with the multi-part sign and verify operations. + This algorithm can be used with the multi-part sign and verify operations. It can also be used with the interruptible signature and verification operations. This algorithm is randomized: each invocation returns a different, equally valid signature. @@ -401,7 +412,7 @@ ECDSA signature algorithms This hash-and-sign signature algorithm can be used with both the message and hash signature functions. ECDSA does not have a context parameter. However, the sign or verify with context functions can be used with a zero-length context. - This algorithm can be used with the multi-part sign and verify operations. + This algorithm can be used with the multi-part sign and verify operations. It can also be used with the interruptible signature and verification operations. When used with `psa_sign_hash()` or `psa_verify_hash()`, the provided ``hash`` parameter is the message digest, computed using the ``hash_alg`` hash algorithm. @@ -441,9 +452,8 @@ ECDSA signature algorithms .. summary:: The randomized ECDSA signature scheme, without hashing. - This specialized signature algorithm can only be used with the `psa_sign_hash()` and `psa_verify_hash()` functions. + This specialized signature algorithm can only be used with the `psa_sign_hash()` and `psa_verify_hash()` functions, their context variants with a zero-length context, or the interruptible signature and verification operations. ECDSA does not have a context parameter. - However, `psa_sign_hash_with_context()` or `psa_verify_hash_with_context()` can be used with a zero-length context. This algorithm is randomized: each invocation returns a different, equally valid signature. @@ -484,7 +494,7 @@ ECDSA signature algorithms This hash-and-sign signature algorithm can be used with both the message and hash signature functions. ECDSA does not have a context parameter. However, the sign or verify with context functions can be used with a zero-length context. - This algorithm can be used with the multi-part sign and verify operations. + This algorithm can be used with the multi-part sign and verify operations. It can also be used with the interruptible signature and verification operations. When used with `psa_sign_hash()` or `psa_verify_hash()`, the provided ``hash`` parameter is the message digest, computed using the ``hash_alg`` hash algorithm. @@ -580,6 +590,7 @@ They are used with the Edwards25519 and Edwards448 elliptic curve keys, see `PSA Both PureEdDSA and HashEdDSA can be used with contexts, which enables domain-separation when signatures are made of different message structures with the same key. For EdDSA, the context is an arbitrary byte string between zero and 255 bytes in length. +Interruptible signature operations use a zero-length context unless the application sets a context with `psa_sign_iop_set_context()` or `psa_verify_iop_set_context()`. The development of EdDSA resulted in a total of five distinct algorithms: @@ -617,7 +628,7 @@ The development of EdDSA resulted in a total of five distinct algorithms: This message-signature algorithm can be used with the `psa_sign_message()` and `psa_verify_message()` functions. With a zero-length context, `PSA_ALG_PURE_EDDSA` can also be used with the `psa_sign_message_with_context()` and `psa_verify_message_with_context()` functions. It cannot be used to sign hashes. - This algorithm can be used with the multi-part verify operation, but not with the multi-part sign operation. + This algorithm can be used with the multi-part verify operation, but not with the multi-part sign operation. It can also be used with the interruptible signature and verification operations. This is the PureEdDSA digital signature algorithm defined by :RFC-title:`8032`, with zero-length context. @@ -630,6 +641,11 @@ The development of EdDSA resulted in a total of five distinct algorithms: * Edwards448: the Ed448 algorithm is computed, with a zero-length context. The output signature is a 114-byte string: the concatenation of :math:`R` and :math:`S` as defined by :RFC:`8032#5.2.6`. + .. note:: + When using an interruptible signature operation with this algorithm, it is not possible to fragment the message data when calculating the signature. The message must be passed in a single call to `psa_sign_iop_update()`. + + However, it is possible to fragment the message data when verifying a signature using an interruptible verification operation. + .. note:: To sign or verify the pre-computed hash of a message using EdDSA, the HashEdDSA algorithms (`PSA_ALG_ED25519PH` and `PSA_ALG_ED448PH`) can be used. The signature produced by HashEdDSA is distinct from that produced by PureEdDSA. @@ -654,7 +670,7 @@ The development of EdDSA resulted in a total of five distinct algorithms: This message-signature algorithm can be used with both the message and message with context signature functions. It cannot be used to sign hashes. - This algorithm can be used with the multi-part verify operation, but not with the multi-part sign operation. + This algorithm can be used with the multi-part verify operation, but not with the multi-part sign operation. It can also be used with the interruptible signature and verification operations. This is the PureEdDSA digital signature algorithm defined by :RFC-title:`8032`, with a context parameter. The context parameter can be between zero and 255 bytes in length. @@ -668,9 +684,12 @@ The development of EdDSA resulted in a total of five distinct algorithms: * Edwards448: the Ed448 algorithm is computed, with a zero-length context. The output signature is a 114-byte string: the concatenation of :math:`R` and :math:`S` as defined by :RFC:`8032#5.2.6`. - To use a non-zero-length context, use the message-signature functions that accept a context parameter, :code:`psa_sign_message_with_context()` and :code:`psa_verify_message_with_context()` + To use a non-zero-length context, use the message-signature functions that accept a context parameter, :code:`psa_sign_message_with_context()` and :code:`psa_verify_message_with_context()`, or call `psa_sign_iop_set_context()` or `psa_verify_iop_set_context()` when using an interruptible signature operation. The `psa_sign_message()` and `psa_verify_message()` functions use a zero-length context when computing or verifying signatures. + .. note:: + An interruptible signature operation for this algorithm must receive the complete message in one call to `psa_sign_iop_update()`. An interruptible verification operation can receive the message in fragments. + .. note:: To sign or verify the pre-computed hash of a message using EdDSA, the HashEdDSA algorithms (`PSA_ALG_ED25519PH` and `PSA_ALG_ED448PH`) can be used. The signature produced by HashEdDSA is distinct from that produced by PureEdDSA. @@ -701,7 +720,7 @@ The development of EdDSA resulted in a total of five distinct algorithms: .. versionadded:: 1.1 This hash-and-sign signature algorithm can be used with both the message and hash signature functions. - This algorithm can be used with the multi-part sign and verify operations. + This algorithm can be used with the multi-part sign and verify operations. It can also be used with the interruptible signature and verification operations. This calculates the Ed25519ph algorithm as specified in :RFC-title:`8032#5.1`, and requires an Edwards25519 curve key. @@ -709,7 +728,7 @@ The development of EdDSA resulted in a total of five distinct algorithms: When used to sign or verify a hash, the ``hash`` parameter is the SHA-512 message digest. The signature functions without a context parameter use a zero-length context when computing or verifying signatures. - To use a non-zero-length context, use the signature functions that accept a context parameter, such as :code:`psa_sign_hash_with_context()` or :code:`psa_verify_message_with_context()` + To use a non-zero-length context, use the signature functions that accept a context parameter, such as :code:`psa_sign_hash_with_context()` or :code:`psa_verify_message_with_context()`, or call `psa_sign_iop_set_context()` or `psa_verify_iop_set_context()` when using an interruptible signature operation. The context parameter can be between zero and 255 bytes in length. .. subsection:: Usage @@ -751,7 +770,7 @@ The development of EdDSA resulted in a total of five distinct algorithms: .. versionadded:: 1.1 This hash-and-sign signature algorithm can be used with both the message and hash signature functions. - This algorithm can be used with the multi-part sign and verify operations. + This algorithm can be used with the multi-part sign and verify operations. It can also be used with the interruptible signature and verification operations. This calculates the Ed448ph algorithm as specified in :RFC-title:`8032#5.2`, and requires an Edwards448 curve key. @@ -759,7 +778,7 @@ The development of EdDSA resulted in a total of five distinct algorithms: When used to sign or verify a hash, the ``hash`` parameter is the truncated SHAKE256 message digest. The signature functions without a context parameter use a zero-length context when computing or verifying signatures. - To use a non-zero-length context, use the signature functions that accept a context parameter, for example, `psa_sign_hash_with_context()` or `psa_verify_message_with_context()` + To use a non-zero-length context, use the signature functions that accept a context parameter, for example, `psa_sign_hash_with_context()` or `psa_verify_message_with_context()`, or call `psa_sign_iop_set_context()` or `psa_verify_iop_set_context()` when using an interruptible signature operation. The context parameter can be between zero and 255 bytes in length. .. subsection:: Usage @@ -898,7 +917,8 @@ All SLH-DSA algorithms can be used with contexts, which enables domain-separatio Context values are arbitrary strings between zero and 255 bytes in length. * The signature functions without a context parameter provide a zero-length context when computing or verifying SLH-DSA signatures. -* To provide a context, use the ``psa_xxxx_with_context()`` signature functions with a context parameter, such as :code:`psa_sign_message_with_context()`. +* To provide a context, use the ``psa_xxxx_with_context()`` signature functions with a context parameter, such as :code:`psa_sign_message_with_context()`, or call `psa_sign_iop_set_context()` or `psa_verify_iop_set_context()` when using an interruptible signature operation. +* Interruptible signature operations use a zero-length context unless the application sets a context with `psa_sign_iop_set_context()` or `psa_verify_iop_set_context()`. .. macro:: PSA_ALG_SLH_DSA :definition: ((psa_algorithm_t) 0x06004000) @@ -910,7 +930,7 @@ Context values are arbitrary strings between zero and 255 bytes in length. This message-signature algorithm can be used with both the message and message with context signature functions. It cannot be used to sign hashes. - This algorithm can be used with the multi-part verify operation, but not with the multi-part sign operation. + This algorithm can be used with the multi-part verify operation, but not with the multi-part sign operation. It can also be used with the interruptible signature and verification operations. This is the pure SLH-DSA digital signature algorithm, defined by :cite-title:`FIPS205`, using hedging. SLH-DSA requires an SLH-DSA key, which determines the SLH-DSA parameter set for the operation. @@ -921,10 +941,13 @@ Context values are arbitrary strings between zero and 255 bytes in length. This algorithm has a context parameter. See the `notes on SLH-DSA contexts `_. + .. note:: + An interruptible signature operation for this algorithm must receive the complete message in one call to `psa_sign_iop_update()`. An interruptible verification operation can receive the message in fragments. + When `PSA_ALG_SLH_DSA` is used as a permitted algorithm in a key policy, this permits: - * `PSA_ALG_SLH_DSA` as the algorithm in a call to any single-part message signing function. - * `PSA_ALG_SLH_DSA` or `PSA_ALG_DETERMINISTIC_SLH_DSA` as the algorithm in a call to any message signature verification function or when setting up a multi-part verify operation. + * `PSA_ALG_SLH_DSA` as the algorithm in a call to any single-part message signing function or when setting up an interruptible signature operation. + * `PSA_ALG_SLH_DSA` or `PSA_ALG_DETERMINISTIC_SLH_DSA` as the algorithm in a call to any message signature verification function or when setting up a multi-part verify operation or an interruptible verification operation. .. note:: To sign or verify the pre-computed hash of a message using SLH-DSA, the HashSLH-DSA algorithms (`PSA_ALG_HASH_SLH_DSA()` and `PSA_ALG_DETERMINISTIC_HASH_SLH_DSA()`) can also be used with :code:`psa_sign_hash()` and :code:`psa_verify_hash()`. @@ -946,7 +969,7 @@ Context values are arbitrary strings between zero and 255 bytes in length. This message-signature algorithm can be used with both the message and message with context signature functions. It cannot be used to sign hashes. - This algorithm can be used with the multi-part verify operation, but not with the multi-part sign operation. + This algorithm can be used with the multi-part verify operation, but not with the multi-part sign operation. It can also be used with the interruptible signature and verification operations. This is the pure SLH-DSA digital signature algorithm, defined by `[FIPS205]`, without hedging. SLH-DSA requires an SLH-DSA key, which determines the SLH-DSA parameter set for the operation. @@ -960,10 +983,13 @@ Context values are arbitrary strings between zero and 255 bytes in length. This algorithm has a context parameter. See the `notes on SLH-DSA contexts `_. + .. note:: + An interruptible signature operation for this algorithm must receive the complete message in one call to `psa_sign_iop_update()`. An interruptible verification operation can receive the message in fragments. + When `PSA_ALG_DETERMINISTIC_SLH_DSA` is used as a permitted algorithm in a key policy, this permits: - * `PSA_ALG_DETERMINISTIC_SLH_DSA` as the algorithm in a call to any single-part message signing function. - * `PSA_ALG_SLH_DSA` or `PSA_ALG_DETERMINISTIC_SLH_DSA` as the algorithm in a call to any message signature verification function or when setting up a multi-part verify operation. + * `PSA_ALG_DETERMINISTIC_SLH_DSA` as the algorithm in a call to any single-part message signing function or when setting up an interruptible signature operation. + * `PSA_ALG_SLH_DSA` or `PSA_ALG_DETERMINISTIC_SLH_DSA` as the algorithm in a call to any message signature verification function or when setting up a multi-part verify operation or an interruptible verification operation. .. note:: To sign or verify the pre-computed hash of a message using SLH-DSA, the HashSLH-DSA algorithms (`PSA_ALG_HASH_SLH_DSA()` and `PSA_ALG_DETERMINISTIC_HASH_SLH_DSA()`) can also be used with :code:`psa_sign_hash()` and :code:`psa_verify_hash()`. @@ -993,7 +1019,7 @@ Context values are arbitrary strings between zero and 255 bytes in length. Unspecified if ``hash_alg`` is not a supported hash algorithm. This hash-and-sign signature algorithm can be used with both the message and hash signature functions. - This algorithm can be used with the multi-part sign and verify operations. + This algorithm can be used with the multi-part sign and verify operations. It can also be used with the interruptible signature and verification operations. This is the pre-hashed SLH-DSA digital signature algorithm, defined by `[FIPS205]`, using hedging. SLH-DSA requires an SLH-DSA key, which determines the SLH-DSA parameter set for the operation. @@ -1011,8 +1037,8 @@ Context values are arbitrary strings between zero and 255 bytes in length. When `PSA_ALG_HASH_SLH_DSA()` is used as a permitted algorithm in a key policy, this permits: - * `PSA_ALG_HASH_SLH_DSA()` as the algorithm in a call to any signing function or when setting up a multi-part sign operation. - * `PSA_ALG_HASH_SLH_DSA()` or `PSA_ALG_DETERMINISTIC_HASH_SLH_DSA()` as the algorithm in a call to any signature verification function or when setting up a multi-part verify operation. + * `PSA_ALG_HASH_SLH_DSA()` as the algorithm in a call to any signing function or when setting up a multi-part sign operation or an interruptible signature operation. + * `PSA_ALG_HASH_SLH_DSA()` or `PSA_ALG_DETERMINISTIC_HASH_SLH_DSA()` as the algorithm in a call to any signature verification function or when setting up a multi-part verify operation or an interruptible verification operation. .. note:: The signature produced by HashSLH-DSA is distinct from that produced by SLH-DSA. @@ -1054,7 +1080,7 @@ Context values are arbitrary strings between zero and 255 bytes in length. Unspecified if ``hash_alg`` is not a supported hash algorithm. This hash-and-sign signature algorithm can be used with both the message and hash signature functions. - This algorithm can be used with the multi-part sign and verify operations. + This algorithm can be used with the multi-part sign and verify operations. It can also be used with the interruptible signature and verification operations. This is the pre-hashed SLH-DSA digital signature algorithm, defined by `[FIPS205]`, without hedging. SLH-DSA requires an SLH-DSA key, which determines the SLH-DSA parameter set for the operation. @@ -1075,8 +1101,8 @@ Context values are arbitrary strings between zero and 255 bytes in length. When `PSA_ALG_DETERMINISTIC_HASH_SLH_DSA()` is used as a permitted algorithm in a key policy, this permits: - * `PSA_ALG_DETERMINISTIC_HASH_SLH_DSA()` as the algorithm in a call to any signing function or when setting up a multi-part sign operation. - * `PSA_ALG_HASH_SLH_DSA()` or `PSA_ALG_DETERMINISTIC_HASH_SLH_DSA()` as the algorithm in a call to any signature verification function or when setting up a multi-part verify operation. + * `PSA_ALG_DETERMINISTIC_HASH_SLH_DSA()` as the algorithm in a call to any signing function or when setting up a multi-part sign operation or an interruptible signature operation. + * `PSA_ALG_HASH_SLH_DSA()` or `PSA_ALG_DETERMINISTIC_HASH_SLH_DSA()` as the algorithm in a call to any signature verification function or when setting up a multi-part verify operation or an interruptible verification operation. .. note:: The signature produced by HashSLH-DSA is distinct from that produced by SLH-DSA. @@ -1243,7 +1269,8 @@ All ML-DSA algorithms can be used with contexts, which enables domain-separation Context values are arbitrary strings between zero and 255 bytes in length. * The signature functions without a context parameter provide a zero-length context when computing or verifying ML-DSA signatures. -* To provide a context, use the ``psa_xxxx_with_context()`` signature functions with a context parameter, such as :code:`psa_sign_message_with_context()`. +* To provide a context, use the ``psa_xxxx_with_context()`` signature functions with a context parameter, such as :code:`psa_sign_message_with_context()`, or call `psa_sign_iop_set_context()` or `psa_verify_iop_set_context()` when using an interruptible signature operation. +* Interruptible signature operations use a zero-length context unless the application sets a context with `psa_sign_iop_set_context()` or `psa_verify_iop_set_context()`. .. macro:: PSA_ALG_ML_DSA :definition: ((psa_algorithm_t) 0x06004400) @@ -1255,7 +1282,7 @@ Context values are arbitrary strings between zero and 255 bytes in length. This message-signature algorithm can be used with both the message and message with context signature functions. It cannot be used to sign hashes. - This algorithm can be used with the multi-part sign and verify operations. + This algorithm can be used with the multi-part sign and verify operations. It can also be used with the interruptible signature and verification operations. This is the pure ML-DSA digital signature algorithm, defined by :cite-title:`FIPS204`, using hedging. ML-DSA requires an ML-DSA key, which determines the ML-DSA parameter set for the operation. @@ -1268,8 +1295,8 @@ Context values are arbitrary strings between zero and 255 bytes in length. When `PSA_ALG_ML_DSA` is used as a permitted algorithm in a key policy, this permits: - * `PSA_ALG_ML_DSA` as the algorithm in a call to any message signing function or when setting up a multi-part sign operation. - * `PSA_ALG_ML_DSA` or `PSA_ALG_DETERMINISTIC_ML_DSA` as the algorithm in a call to any message signature verification function or when setting up a multi-part verify operation. + * `PSA_ALG_ML_DSA` as the algorithm in a call to any message signing function or when setting up a multi-part sign operation or an interruptible signature operation. + * `PSA_ALG_ML_DSA` or `PSA_ALG_DETERMINISTIC_ML_DSA` as the algorithm in a call to any message signature verification function or when setting up a multi-part verify operation or an interruptible verification operation. .. note:: To sign or verify the pre-computed hash of a message using ML-DSA, the HashML-DSA algorithms (`PSA_ALG_HASH_ML_DSA()` and `PSA_ALG_DETERMINISTIC_HASH_ML_DSA()`) can also be used with :code:`psa_sign_hash()` and :code:`psa_verify_hash()`. @@ -1291,7 +1318,7 @@ Context values are arbitrary strings between zero and 255 bytes in length. This message-signature algorithm can be used with both the message and message with context signature functions. It cannot be used to sign hashes. - This algorithm can be used with the multi-part sign and verify operations. + This algorithm can be used with the multi-part sign and verify operations. It can also be used with the interruptible signature and verification operations. This is the pure ML-DSA digital signature algorithm, defined by :cite-title:`FIPS204`, without hedging. ML-DSA requires an ML-DSA key, which determines the ML-DSA parameter set for the operation. @@ -1307,8 +1334,8 @@ Context values are arbitrary strings between zero and 255 bytes in length. When `PSA_ALG_DETERMINISTIC_ML_DSA` is used as a permitted algorithm in a key policy, this permits: - * `PSA_ALG_DETERMINISTIC_ML_DSA` as the algorithm in a call to any message signing function or when setting up a multi-part sign operation. - * `PSA_ALG_ML_DSA` or `PSA_ALG_DETERMINISTIC_ML_DSA` as the algorithm in a call to any message signature verification function or when setting up a multi-part verify operation. + * `PSA_ALG_DETERMINISTIC_ML_DSA` as the algorithm in a call to any message signing function or when setting up a multi-part sign operation or an interruptible signature operation. + * `PSA_ALG_ML_DSA` or `PSA_ALG_DETERMINISTIC_ML_DSA` as the algorithm in a call to any message signature verification function or when setting up a multi-part verify operation or an interruptible verification operation. .. note:: To sign or verify the pre-computed hash of a message using ML-DSA, the HashML-DSA algorithms (`PSA_ALG_HASH_ML_DSA()` and `PSA_ALG_DETERMINISTIC_HASH_ML_DSA()`) can also be used with :code:`psa_sign_hash()` and :code:`psa_verify_hash()`. @@ -1338,7 +1365,7 @@ Context values are arbitrary strings between zero and 255 bytes in length. Unspecified if ``hash_alg`` is not a supported hash algorithm. This hash-and-sign signature algorithm can be used with both the message and hash signature functions. - This algorithm can be used with the multi-part sign and verify operations. + This algorithm can be used with the multi-part sign and verify operations. It can also be used with the interruptible signature and verification operations. This is the pre-hashed ML-DSA digital signature algorithm, defined by :cite-title:`FIPS204`, using hedging. ML-DSA requires an ML-DSA key, which determines the ML-DSA parameter set for the operation. @@ -1356,8 +1383,8 @@ Context values are arbitrary strings between zero and 255 bytes in length. When `PSA_ALG_HASH_ML_DSA()` is used as a permitted algorithm in a key policy, this permits: - * `PSA_ALG_HASH_ML_DSA()` as the algorithm in a call to any signing function or when setting up a multi-part sign operation. - * `PSA_ALG_HASH_ML_DSA()` or `PSA_ALG_DETERMINISTIC_HASH_ML_DSA()` as the algorithm in a call to any signature verification function or when setting up a multi-part verify operation. + * `PSA_ALG_HASH_ML_DSA()` as the algorithm in a call to any signing function or when setting up a multi-part sign operation or an interruptible signature operation. + * `PSA_ALG_HASH_ML_DSA()` or `PSA_ALG_DETERMINISTIC_HASH_ML_DSA()` as the algorithm in a call to any signature verification function or when setting up a multi-part verify operation or an interruptible verification operation. .. note:: The signature produced by HashML-DSA is distinct from that produced by ML-DSA. @@ -1399,7 +1426,7 @@ Context values are arbitrary strings between zero and 255 bytes in length. Unspecified if ``hash_alg`` is not a supported hash algorithm. This hash-and-sign signature algorithm can be used with both the message and hash signature functions. - This algorithm can be used with the multi-part sign and verify operations. + This algorithm can be used with the multi-part sign and verify operations. It can also be used with the interruptible signature and verification operations. This is the pre-hashed ML-DSA digital signature algorithm, defined by :cite-title:`FIPS204`, without hedging. ML-DSA requires an ML-DSA key, which determines the ML-DSA parameter set for the operation. @@ -1420,8 +1447,8 @@ Context values are arbitrary strings between zero and 255 bytes in length. When `PSA_ALG_DETERMINISTIC_HASH_ML_DSA()` is used as a permitted algorithm in a key policy, this permits: - * `PSA_ALG_DETERMINISTIC_HASH_ML_DSA()` as the algorithm in a call to any signing function or when setting up a multi-part sign operation. - * `PSA_ALG_HASH_ML_DSA()` or `PSA_ALG_DETERMINISTIC_HASH_ML_DSA()` as the algorithm in a call to any signature verification function or when setting up a multi-part verify operation. + * `PSA_ALG_DETERMINISTIC_HASH_ML_DSA()` as the algorithm in a call to any signing function or when setting up a multi-part sign operation or an interruptible signature operation. + * `PSA_ALG_HASH_ML_DSA()` or `PSA_ALG_DETERMINISTIC_HASH_ML_DSA()` as the algorithm in a call to any signature verification function or when setting up a multi-part verify operation or an interruptible verification operation. .. note:: The signature produced by HashML-DSA is distinct from that produced by ML-DSA. @@ -1714,6 +1741,8 @@ Single-part asymmetric signature functions * For a hash-and-sign signature algorithm, use a `psa_hash_operation_t` multi-part hash operation and then pass the resulting hash to `psa_sign_hash()`. :code:`PSA_ALG_GET_HASH(alg)` can be used to determine the hash algorithm to use. + An implementation that supports interruptible signature operations can instead use `psa_sign_iop_t` when bounded execution time is required. This API has distinct availability and completion requirements, and is not a replacement for the multi-part signature API. + .. function:: psa_sign_message_with_context .. summary:: @@ -1790,6 +1819,8 @@ Single-part asymmetric signature functions * For a hash-and-sign signature algorithm, use a `psa_hash_operation_t` multi-part hash operation and then pass the resulting hash to `psa_sign_hash_with_context()`. :code:`PSA_ALG_GET_HASH(alg)` can be used to determine the hash algorithm to use. + Interruptible signature operations use a zero-length context unless the application calls `psa_sign_iop_set_context()` or `psa_verify_iop_set_context()`. + .. function:: psa_verify_message .. summary:: @@ -1850,6 +1881,8 @@ Single-part asymmetric signature functions * For a hash-and-sign signature algorithm, use a `psa_hash_operation_t` multi-part hash operation and then pass the resulting hash to `psa_verify_hash()`. :code:`PSA_ALG_GET_HASH(alg)` can be used to determine the hash algorithm to use. + An implementation that supports interruptible signature operations can instead use `psa_verify_iop_t` when bounded execution time is required. This API has distinct availability and completion requirements, and is not a replacement for the multi-part signature API. + .. function:: psa_verify_message_with_context .. summary:: @@ -1918,6 +1951,8 @@ Single-part asymmetric signature functions * For a hash-and-sign signature algorithm, use a `psa_hash_operation_t` multi-part hash operation and then pass the resulting hash to `psa_verify_hash_with_context()`. :code:`PSA_ALG_GET_HASH(alg)` can be used to determine the hash algorithm to use. + Interruptible signature operations use a zero-length context unless the application calls `psa_sign_iop_set_context()` or `psa_verify_iop_set_context()`. + .. function:: psa_sign_hash .. summary:: @@ -2750,80 +2785,1062 @@ Multi-part asymmetric signature operations In particular, calling `psa_verify_abort()` after the operation has been terminated by a call to `psa_verify_abort()` or `psa_verify_finish()` is safe and has no effect. -Support macros --------------- +.. _interruptible-sign: -.. macro:: PSA_ALG_IS_SIGN_MESSAGE - :definition: /* specification-defined value */ +Interruptible asymmetric signature +---------------------------------- + +The interruptible signature operation calculates the signature of a message, or pre-computed hash, in an interruptible manner. For example, this can enable an application to remain responsive in an execution environment that does not provide multi-tasking. + +An interruptible signature operation is used as follows: + +1. Allocate an interruptible signature operation object, of type `psa_sign_iop_t`, which will be passed to all the functions listed here. +#. Initialize the operation object with one of the methods described in the documentation for `psa_sign_iop_t`, for example, `PSA_SIGN_IOP_INIT`. +#. Call `psa_sign_iop_setup_start()` to specify the algorithm and key. +#. Call `psa_sign_iop_setup_complete()` to complete the setup, until this function returns a status code other than :code:`PSA_OPERATION_INCOMPLETE`. +#. Optionally, call `psa_sign_iop_set_context()` to provide a context. +#. Either: + + 1. Call `psa_sign_iop_hash()` with a pre-computed hash of the message to sign; or + 2. Call `psa_sign_iop_update()` zero, one or more times, passing a fragment of the message each time. The signature that is calculated is that of the concatenation of these fragments, in order. +#. Call `psa_sign_iop_complete()` to finish calculating the signature value, until this function returns a status code other than :code:`PSA_OPERATION_INCOMPLETE`. +#. If an error occurs at any stage, or to terminate the operation early, call `psa_sign_iop_abort()`. + + +.. typedef:: /* implementation-defined type */ psa_sign_iop_t .. summary:: - Whether the specified algorithm is a signature algorithm that can be used with message signature and verification functions, and with sign and verify multi-part operations. + The type of the state data structure for an interruptible signature operation. - .. param:: alg - An algorithm identifier: a value of type `psa_algorithm_t`. + .. versionadded:: 1.6 - .. return:: - ``1`` if ``alg`` is a signature algorithm that can be used to sign a message. ``0`` if ``alg`` is a signature algorithm that can only be used to sign a pre-computed hash. ``0`` if ``alg`` is not a signature algorithm. This macro can return either ``0`` or ``1`` if ``alg`` is not a supported algorithm identifier. + Before calling any function on an interruptible signature operation object, the application must initialize it by any of the following means: - This macro evaluates to ``1`` for hash-and-sign and message-signature algorithms. + * Set the object to all-bits-zero, for example: -.. macro:: PSA_ALG_IS_SIGN_HASH - :definition: /* specification-defined value */ + .. code-block:: xref + + psa_sign_iop_t operation; + memset(&operation, 0, sizeof(operation)); + + * Initialize the object to logical zero values by declaring the object as static or global without an explicit initializer, for example: + + .. code-block:: xref + + static psa_sign_iop_t operation; + + * Initialize the object to the initializer `PSA_SIGN_IOP_INIT`, for example: + + .. code-block:: xref + + psa_sign_iop_t operation = PSA_SIGN_IOP_INIT; + + * Assign the result of the function `psa_sign_iop_init()` to the object, for example: + + .. code-block:: xref + + psa_sign_iop_t operation; + operation = psa_sign_iop_init(); + + This is an implementation-defined type. Applications that make assumptions about the content of this object will result in implementation-specific behavior, and are non-portable. + +.. macro:: PSA_SIGN_IOP_INIT + :definition: /* implementation-defined value */ .. summary:: - Whether the specified algorithm is a signature algorithm that can be used with hash signature and verification functions. + This macro evaluates to an initializer for an interruptible signature operation object of type `psa_sign_iop_t`. - .. param:: alg - An algorithm identifier: a value of type `psa_algorithm_t`. + .. versionadded:: 1.6 - .. return:: - ``1`` if ``alg`` is a signature algorithm that can be used to sign a hash. ``0`` if ``alg`` is a signature algorithm that can only be used to sign a message. ``0`` if ``alg`` is not a signature algorithm. This macro can return either ``0`` or ``1`` if ``alg`` is not a supported algorithm identifier. +.. function:: psa_sign_iop_init - This macro evaluates to ``1`` for hash-and-sign and specialized signature algorithms. + .. summary:: + Return an initial value for an interruptible signature operation object. -.. macro:: PSA_ALG_IS_HASH_AND_SIGN - :definition: /* specification-defined value */ + .. versionadded:: 1.6 + + .. return:: psa_sign_iop_t + +.. function:: psa_sign_iop_get_num_ops .. summary:: - Whether the specified algorithm is a hash-and-sign algorithm that signs exactly the hash value. + Get the number of *ops* that an interruptible signature operation has taken so far. - .. param:: alg - An algorithm identifier: a value of type `psa_algorithm_t`. + .. versionadded:: 1.6 - .. return:: - ``1`` if ``alg`` is a hash-and-sign algorithm that signs exactly the hash value, ``0`` otherwise. This macro can return either ``0`` or ``1`` if ``alg`` is not a supported algorithm identifier. + .. param:: psa_sign_iop_t * operation + The interruptible signature operation to inspect. - A wildcard signature algorithm policy, using `PSA_ALG_ANY_HASH`, returns the same value as the signature algorithm parameterized with a valid hash algorithm. + .. return:: uint32_t + Number of *ops* that the operation has taken so far. - This macro identifies algorithms that can be used with `psa_sign_hash()` that use the exact message hash value as an input the signature operation. For example, if :code:`PSA_ALG_IS_HASH_AND_SIGN(alg)` is true, the following call sequence is equivalent to :code:`psa_sign_message(key, alg, msg, msg_len, ...)`: + After the interruptible operation has completed, the returned value is the number of *ops* spent on the entire operation. The value is reset to zero by a successful call to either `psa_sign_iop_setup_start()` or `psa_sign_iop_abort()`. + A failed call to `psa_sign_iop_setup_start()` can also reset the value to zero. - .. code-block:: xref + This function can be used to tune the value passed to `psa_iop_set_max_ops()`. - uint8_t hash[PSA_HASH_MAX_SIZE]; - size_t hash_len; - psa_hash_compute(PSA_ALG_GET_HASH(alg), msg, msg_len, - hash, sizeof(hash), &hash_len); - psa_sign_hash(key, alg, hash, hash_len, ...); + The value is undefined if the operation object has not been initialized. -.. macro:: PSA_ALG_SIGN_SUPPORTS_CONTEXT - :definition: /* implementation-defined value */ +.. function:: psa_sign_iop_setup_start .. summary:: - Whether the specified signature algorithm can be used with a non-zero-length context. + Begin the setup of an interruptible signature operation. - .. versionadded:: 1.4 + .. versionadded:: 1.6 - .. param:: alg - A signature algorithm identifier: a value of type `psa_algorithm_t` such that :code:`PSA_ALG_IS_SIGN(alg)` is true. + .. param:: psa_sign_iop_t * operation + The interruptible signature operation to set up. It must have been initialized as per the documentation for `psa_sign_iop_t` and not yet in use. + .. param:: psa_key_id_t key + Identifier of the key to use for the operation. It must be an asymmetric key pair. The key must either permit the usage `PSA_KEY_USAGE_SIGN_HASH` or `PSA_KEY_USAGE_SIGN_MESSAGE`. + .. param:: psa_algorithm_t alg + An asymmetric signature algorithm: a value of type `psa_algorithm_t` such that :code:`PSA_ALG_IS_SIGN(alg)` is true. - .. return:: - ``1`` if ``alg`` is a signature algorithm that can be used with a non-zero-length context. - ``0`` if ``alg`` is a signature algorithm that cannot be used with a non-zero-length context. - This macro can return either ``0`` or ``1`` if ``alg`` is not a supported signature algorithm identifier. + .. return:: psa_status_t + .. retval:: PSA_SUCCESS + Success. + The operation setup must now be completed by calling `psa_sign_iop_setup_complete()`. + .. retval:: PSA_ERROR_INVALID_HANDLE + ``key`` is not a valid key identifier. + .. retval:: PSA_ERROR_NOT_PERMITTED + The following conditions can result in this error: - A wildcard signature algorithm policy, using `PSA_ALG_ANY_HASH`, returns the same value as the signature algorithm parameterized with a valid hash algorithm. + * The key has neither the `PSA_KEY_USAGE_SIGN_HASH` nor the `PSA_KEY_USAGE_SIGN_MESSAGE` usage flag. + * The key does not permit the requested algorithm. + .. retval:: PSA_ERROR_NOT_SUPPORTED + The following conditions can result in this error: - This macro identifies signature algorithms that have a context parameter, and can be used with the appropriate functions that support non-zero-length contexts. + * ``alg`` is not supported or is not an asymmetric signature algorithm. + * ``key`` is not supported for use with ``alg``. + .. retval:: PSA_ERROR_INVALID_ARGUMENT + The following conditions can result in this error: + + * ``alg`` is not an asymmetric signature algorithm. + * ``key`` is not an asymmetric key pair, that is compatible with ``alg``. + .. retval:: PSA_ERROR_BAD_STATE + The following conditions can result in this error: + + * The operation state is not valid: it must be inactive. + * The library requires initializing by a call to `psa_crypto_init()`. + .. retval:: PSA_ERROR_INSUFFICIENT_MEMORY + .. retval:: PSA_ERROR_COMMUNICATION_FAILURE + .. retval:: PSA_ERROR_CORRUPTION_DETECTED + .. retval:: PSA_ERROR_STORAGE_FAILURE + .. retval:: PSA_ERROR_DATA_CORRUPT + .. retval:: PSA_ERROR_DATA_INVALID + .. retval:: PSA_ERROR_INSUFFICIENT_ENTROPY + + This function sets up the calculation of an asymmetric signature of a message or pre-computed hash. To verify an asymmetric signature against an expected value, use an interruptible verification operation, see :secref:`interruptible-verify`. + + After a successful call to `psa_sign_iop_setup_start()`, the operation is in setup state. Setup can be completed by calling `psa_sign_iop_setup_complete()` repeatedly, until it returns a status code that is not :code:`PSA_OPERATION_INCOMPLETE`. Once setup has begun, the application must eventually terminate the operation. The following events terminate an operation: + + * A successful call to `psa_sign_iop_complete()`. + * A call to `psa_sign_iop_abort()`. + + If `psa_sign_iop_setup_start()` returns an error, the operation object remains inactive, but its number of *ops* can be reset to zero. + +.. function:: psa_sign_iop_setup_complete + + .. summary:: + Finish setting up an interruptible signature operation. + + .. versionadded:: 1.6 + + .. param:: psa_sign_iop_t * operation + The interruptible signature operation to use. The operation must be in the process of being set up. + + .. return:: psa_status_t + .. retval:: PSA_SUCCESS + Success. + The operation is now ready for input of data to sign. + .. retval:: PSA_OPERATION_INCOMPLETE + The function was interrupted after exhausting the maximum *ops*. The computation is incomplete, and this function must be called again with the same operation object to continue. + .. retval:: PSA_ERROR_BAD_STATE + The following conditions can result in this error: + + * The operation state is not valid: the operation setup must have started, but not yet finished. + * The library requires initializing by a call to `psa_crypto_init()`. + .. retval:: PSA_ERROR_INSUFFICIENT_MEMORY + .. retval:: PSA_ERROR_COMMUNICATION_FAILURE + .. retval:: PSA_ERROR_CORRUPTION_DETECTED + .. retval:: PSA_ERROR_STORAGE_FAILURE + .. retval:: PSA_ERROR_DATA_CORRUPT + .. retval:: PSA_ERROR_DATA_INVALID + .. retval:: PSA_ERROR_INSUFFICIENT_ENTROPY + + .. note:: + This is an interruptible function, and must be called repeatedly, until it returns a status code that is not :code:`PSA_OPERATION_INCOMPLETE`. + + When this function returns successfully, the operation is ready for context or data input using a call to `psa_sign_iop_set_context()`, `psa_sign_iop_hash()`, or `psa_sign_iop_update()`. + + If this function returns :code:`PSA_OPERATION_INCOMPLETE`, setup is not complete, and this function must be called again to continue the operation. + + If this function returns an error status, the operation enters an error state and must be aborted by calling `psa_sign_iop_abort()`. + + The amount of calculation performed in a single call to this function is determined by the maximum *ops* setting. See `psa_iop_set_max_ops()`. + +.. function:: psa_sign_iop_set_context + + .. summary:: + Provide a context for an interruptible signature operation. + + .. versionadded:: 1.6 + + .. param:: psa_sign_iop_t * operation + The interruptible signature operation to configure. + The operation setup must be complete, with no context, hash, or message input, and completion must not have started. + .. param:: const uint8_t * context + Buffer containing the context value. + .. param:: size_t context_length + Size of the ``context`` buffer in bytes. + + .. return:: psa_status_t + .. retval:: PSA_SUCCESS + Success. + .. retval:: PSA_ERROR_BAD_STATE + The following conditions can result in this error: + + * The operation state is not valid: setup must be complete, and no call to `psa_sign_iop_set_context()`, `psa_sign_iop_hash()`, `psa_sign_iop_update()`, or `psa_sign_iop_complete()` has been made. + * The library requires initializing by a call to `psa_crypto_init()`. + .. retval:: PSA_ERROR_INVALID_ARGUMENT + The following conditions can result in this error: + + * ``context_length`` is not valid for the algorithm and key type. + * ``context`` is not a valid input value for the algorithm and key type. + .. retval:: PSA_ERROR_NOT_SUPPORTED + The context value is not supported by this implementation. + .. retval:: PSA_ERROR_INSUFFICIENT_MEMORY + .. retval:: PSA_ERROR_COMMUNICATION_FAILURE + .. retval:: PSA_ERROR_CORRUPTION_DETECTED + + This function sets the context value in an interruptible signature operation. + The application must complete setup by calling `psa_sign_iop_setup_complete()` before calling this function. + + For a signature algorithm that has a context parameter: + + * If this function is not called, the operation uses the algorithm with a zero-length or empty context. + * To set a non-zero-length context, call this function after `psa_sign_iop_setup_complete()` has returned success and before calling any input or completion function on the operation. + + If a context parameter is not supported by the algorithm, this function call can be omitted, or can be called with a zero-length context. + + The macro `PSA_ALG_SIGN_SUPPORTS_CONTEXT()` can be used to determine if a signature algorithm supports non-zero-length context values. + + If this function returns an error status, the operation enters an error state and must be aborted by calling `psa_sign_iop_abort()`. + +.. function:: psa_sign_iop_hash + + .. summary:: + Input a pre-computed hash to an interruptible signature operation. + + .. versionadded:: 1.6 + + .. param:: psa_sign_iop_t * operation + The interruptible signature operation to use. The operation must have been set up, with no data input, and completion must not have started. + .. param:: const uint8_t * hash + The input to sign. This is usually the hash of a message. + + See the description of this function, or the description of individual signature algorithms, for details of the acceptable inputs. + .. param:: size_t hash_length + Size of the ``hash`` buffer in bytes. + + .. return:: psa_status_t + .. retval:: PSA_SUCCESS + Success. + The operation is now ready for completion. + .. retval:: PSA_ERROR_BAD_STATE + The following conditions can result in this error: + + * The operation state is not valid: setup must be complete, and no call to `psa_sign_iop_hash()`, `psa_sign_iop_update()`, or `psa_sign_iop_complete()` has been made. + * The library requires initializing by a call to `psa_crypto_init()`. + .. retval:: PSA_ERROR_NOT_PERMITTED + The key does not have the `PSA_KEY_USAGE_SIGN_HASH` flag. + .. retval:: PSA_ERROR_INVALID_ARGUMENT + The following conditions can result in this error: + + * The algorithm does not allow signing of a pre-computed hash. + * ``hash_length`` is not valid for the algorithm and key type. + * ``hash`` is not a valid input value for the algorithm and key type. + .. retval:: PSA_ERROR_NOT_SUPPORTED + The implementation does not support signing of a pre-computed hash. + .. retval:: PSA_ERROR_INSUFFICIENT_MEMORY + .. retval:: PSA_ERROR_COMMUNICATION_FAILURE + .. retval:: PSA_ERROR_CORRUPTION_DETECTED + .. retval:: PSA_ERROR_STORAGE_FAILURE + .. retval:: PSA_ERROR_DATA_CORRUPT + .. retval:: PSA_ERROR_DATA_INVALID + .. retval:: PSA_ERROR_INSUFFICIENT_ENTROPY + + The application must complete the setup of the operation before calling this function. + + For hash-and-sign signature algorithms, the ``hash`` input to this function is the hash of the message to sign. The algorithm used to calculate this hash is encoded in the signature algorithm. For such algorithms, ``hash_length`` must equal the length of the hash output: :code:`hash_length == PSA_HASH_LENGTH(PSA_ALG_GET_HASH(alg))`. + + Specialized signature algorithms can apply a padding or encoding to the hash. In such cases, the encoded hash must be passed to this function. For example, see `PSA_ALG_RSA_PKCS1V15_SIGN_RAW`. + + After input of the hash, the signature operation can be completed by calling `psa_sign_iop_complete()` until it returns a status code that is not :code:`PSA_OPERATION_INCOMPLETE`. + + If this function returns an error status, the operation enters an error state and must be aborted by calling `psa_sign_iop_abort()`. + +.. function:: psa_sign_iop_update + + .. summary:: + Add a message fragment to an interruptible signature operation. + + .. versionadded:: 1.6 + + .. param:: psa_sign_iop_t * operation + The interruptible signature operation to use. The operation must have been set up, with no pre-computed hash value input, and completion must not have started. + .. param:: const uint8_t * input + Buffer containing the message fragment to add to the signature calculation. + .. param:: size_t input_length + Size of the ``input`` buffer in bytes. + + .. return:: psa_status_t + .. retval:: PSA_SUCCESS + Success. + .. retval:: PSA_ERROR_BAD_STATE + The following conditions can result in this error: + + * The operation state is not valid: setup must be complete, and no call to `psa_sign_iop_hash()` or `psa_sign_iop_complete()` has been made. + * The library requires initializing by a call to `psa_crypto_init()`. + .. retval:: PSA_ERROR_NOT_PERMITTED + The key does not have the `PSA_KEY_USAGE_SIGN_MESSAGE` flag. + .. retval:: PSA_ERROR_INVALID_ARGUMENT + The following conditions can result in this error: + + * The algorithm does not allow signing of a message. + * The selected algorithm permits only one message fragment, and `psa_sign_iop_update()` has already been called. + * The total input for the operation is too large for the signature algorithm. + .. retval:: PSA_ERROR_NOT_SUPPORTED + The following conditions can result in this error: + + * The implementation does not support signing of a message. + * The selected algorithm or implementation permits only one message fragment, and `psa_sign_iop_update()` has already been called. + * The total input for the operation is too large for the implementation. + .. retval:: PSA_ERROR_INSUFFICIENT_MEMORY + .. retval:: PSA_ERROR_COMMUNICATION_FAILURE + .. retval:: PSA_ERROR_CORRUPTION_DETECTED + .. retval:: PSA_ERROR_STORAGE_FAILURE + .. retval:: PSA_ERROR_DATA_CORRUPT + .. retval:: PSA_ERROR_DATA_INVALID + .. retval:: PSA_ERROR_INSUFFICIENT_ENTROPY + + The application must complete the setup of the operation before calling this function. + + For message-signature algorithms that process the message data multiple times when computing a signature, `psa_sign_iop_update()` must be called exactly once with the entire message content. For signature algorithms that only process the message data once, the message content can be passed in a series of calls to `psa_sign_iop_update()`. + + After input of the message, the signature operation can be completed by calling `psa_sign_iop_complete()` until it returns a status code that is not :code:`PSA_OPERATION_INCOMPLETE`. + + If this function returns an error status, the operation enters an error state and must be aborted by calling `psa_sign_iop_abort()`. + +.. function:: psa_sign_iop_complete + + .. summary:: + Progress an interruptible signature calculation. + + .. versionadded:: 1.6 + + .. param:: psa_sign_iop_t * operation + The interruptible signature operation to use. The operation must be active, and setup must be complete. + .. param:: uint8_t * signature + Buffer where the signature is to be written. + .. param:: size_t signature_size + Size of the ``signature`` buffer in bytes. This must be appropriate for the selected algorithm and key: + + * The required signature size is :code:`PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg)` where ``key_type`` and ``key_bits`` are attributes of the key, and ``alg`` is the algorithm used to calculate the signature. + * `PSA_SIGNATURE_MAX_SIZE` evaluates to the maximum signature size of any supported signature algorithm. + .. param:: size_t * signature_length + On success, the number of bytes that make up the returned signature value. + + .. return:: psa_status_t + .. retval:: PSA_SUCCESS + Success. + The first ``(*signature_length)`` bytes of ``signature`` contain the signature value. + .. retval:: PSA_OPERATION_INCOMPLETE + The function was interrupted after exhausting the maximum *ops*. The computation is incomplete, and this function must be called again with the same operation object to continue. + .. retval:: PSA_ERROR_NOT_PERMITTED + If no data has been input to the operation, the key does not have the `PSA_KEY_USAGE_SIGN_MESSAGE` flag. + .. retval:: PSA_ERROR_BAD_STATE + The following conditions can result in this error: + + * The operation state is not valid: the operation must be active, and setup must be complete. + * The library requires initializing by a call to `psa_crypto_init()`. + .. retval:: PSA_ERROR_INVALID_ARGUMENT + If no data has been input to the operation, the algorithm does not allow signing of a message. + .. retval:: PSA_ERROR_BUFFER_TOO_SMALL + The size of the ``signature`` buffer is too small. + `PSA_SIGN_OUTPUT_SIZE()` or `PSA_SIGNATURE_MAX_SIZE` can be used to determine a sufficient buffer size. + .. retval:: PSA_ERROR_NOT_SUPPORTED + If no data has been input to the operation, the implementation does not support signing of a message. + .. retval:: PSA_ERROR_INSUFFICIENT_MEMORY + .. retval:: PSA_ERROR_COMMUNICATION_FAILURE + .. retval:: PSA_ERROR_CORRUPTION_DETECTED + .. retval:: PSA_ERROR_STORAGE_FAILURE + .. retval:: PSA_ERROR_DATA_CORRUPT + .. retval:: PSA_ERROR_DATA_INVALID + .. retval:: PSA_ERROR_INSUFFICIENT_ENTROPY + + .. note:: + This is an interruptible function, and must be called repeatedly, until it returns a status code that is not :code:`PSA_OPERATION_INCOMPLETE`. + + When this function returns successfully, the signature is returned in ``signature``, and the operation becomes inactive. + If neither a pre-computed hash nor a message fragment has been input, the signature is of the zero-length message. + + If this function returns :code:`PSA_OPERATION_INCOMPLETE`, no signature is returned, and this function must be called again to continue the operation. + + If this function returns an error status, the operation enters an error state and must be aborted by calling `psa_sign_iop_abort()`. + + The amount of calculation performed in a single call to this function is determined by the maximum *ops* setting. See `psa_iop_set_max_ops()`. + +.. function:: psa_sign_iop_abort + + .. summary:: + Abort an interruptible signature operation. + + .. versionadded:: 1.6 + + .. param:: psa_sign_iop_t * operation + The interruptible signature operation to abort. + + .. return:: psa_status_t + .. retval:: PSA_SUCCESS + Success. + The operation object can now be discarded or reused. + .. retval:: PSA_ERROR_COMMUNICATION_FAILURE + .. retval:: PSA_ERROR_CORRUPTION_DETECTED + .. retval:: PSA_ERROR_BAD_STATE + The library requires initializing by a call to `psa_crypto_init()`. + + 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_sign_iop_setup_start()` again. + + This function can be called at any time after the operation object has been initialized as described in `psa_sign_iop_t`. + + In particular, it is valid to call `psa_sign_iop_abort()` twice, or to call `psa_sign_iop_abort()` on an operation that has not been set up. + + +.. _interruptible-verify: + +Interruptible asymmetric verification +------------------------------------- + +The interruptible verification operation verifies the signature of a message, or pre-computed hash, in an interruptible manner. For example, this can enable an application to remain responsive in an execution environment that does not provide multi-tasking. + +An interruptible verification operation is used as follows: + +1. Allocate an interruptible verification operation object, of type `psa_verify_iop_t`, which will be passed to all the functions listed here. +#. Initialize the operation object with one of the methods described in the documentation for `psa_verify_iop_t`, for example, `PSA_VERIFY_IOP_INIT`. +#. Call `psa_verify_iop_setup_start()` to specify the algorithm, key, and the signature to verify. +#. Call `psa_verify_iop_setup_complete()` to complete the setup, until this function returns a status code other than :code:`PSA_OPERATION_INCOMPLETE`. +#. Optionally, call `psa_verify_iop_set_context()` to provide a context. +#. Either: + + 1. Call `psa_verify_iop_hash()` with a pre-computed hash of the message to verify; or + 2. Call `psa_verify_iop_update()` zero, one or more times, passing a fragment of the message each time. The signature is verified against the concatenation of these fragments, in order. +#. Call `psa_verify_iop_complete()` to finish verifying the signature value, until this function returns a status code other than :code:`PSA_OPERATION_INCOMPLETE`. +#. If an error occurs at any stage, or to terminate the operation early, call `psa_verify_iop_abort()`. + +To verify a message received from a streaming protocol that provides the signature after the message data, use the deferred-signature flow instead. The first two steps are the same as above, and then: + +3. Call `psa_verify_iop_setup_deferred_signature_start()` to specify the algorithm and key. +#. Call `psa_verify_iop_setup_complete()` to complete the setup, until this function returns a status code other than :code:`PSA_OPERATION_INCOMPLETE`. +#. Optionally, call `psa_verify_iop_set_context()` to provide a context. +#. Call `psa_verify_iop_update()` zero, one or more times, passing a fragment of the message each time. +#. Call `psa_verify_iop_set_signature()` to provide the signature, after all message fragments have been passed to the operation. +#. Call `psa_verify_iop_complete()` to finish verifying the signature value, until this function returns a status code other than :code:`PSA_OPERATION_INCOMPLETE`. +#. If an error occurs at any stage, or to terminate the operation early, call `psa_verify_iop_abort()`. + + +.. typedef:: /* implementation-defined type */ psa_verify_iop_t + + .. summary:: + The type of the state data structure for an interruptible verification operation. + + .. versionadded:: 1.6 + + Before calling any function on an interruptible verification operation object, the application must initialize it by any of the following means: + + * Set the object to all-bits-zero, for example: + + .. code-block:: xref + + psa_verify_iop_t operation; + memset(&operation, 0, sizeof(operation)); + + * Initialize the object to logical zero values by declaring the object as static or global without an explicit initializer, for example: + + .. code-block:: xref + + static psa_verify_iop_t operation; + + * Initialize the object to the initializer `PSA_VERIFY_IOP_INIT`, for example: + + .. code-block:: xref + + psa_verify_iop_t operation = PSA_VERIFY_IOP_INIT; + + * Assign the result of the function `psa_verify_iop_init()` to the object, for example: + + .. code-block:: xref + + psa_verify_iop_t operation; + operation = psa_verify_iop_init(); + + This is an implementation-defined type. Applications that make assumptions about the content of this object will result in implementation-specific behavior, and are non-portable. + +.. macro:: PSA_VERIFY_IOP_INIT + :definition: /* implementation-defined value */ + + .. summary:: + This macro evaluates to an initializer for an interruptible verification operation object of type `psa_verify_iop_t`. + + .. versionadded:: 1.6 + +.. function:: psa_verify_iop_init + + .. summary:: + Return an initial value for an interruptible verification operation object. + + .. versionadded:: 1.6 + + .. return:: psa_verify_iop_t + +.. function:: psa_verify_iop_get_num_ops + + .. summary:: + Get the number of *ops* that an interruptible verification operation has taken so far. + + .. versionadded:: 1.6 + + .. param:: psa_verify_iop_t * operation + The interruptible verification operation to inspect. + + .. return:: uint32_t + Number of *ops* that the operation has taken so far. + + After the interruptible operation has completed, the returned value is the number of *ops* spent on the entire operation. The value is reset to zero by a successful call to `psa_verify_iop_setup_start()`, `psa_verify_iop_setup_deferred_signature_start()`, or `psa_verify_iop_abort()`. + A failed call to either setup function can also reset the value to zero. + + This function can be used to tune the value passed to `psa_iop_set_max_ops()`. + + The value is undefined if the operation object has not been initialized. + +.. function:: psa_verify_iop_setup_start + + .. summary:: + Begin the setup of an interruptible verification operation. + + .. versionadded:: 1.6 + + .. param:: psa_verify_iop_t * operation + The interruptible verification operation to set up. It must have been initialized as per the documentation for `psa_verify_iop_t` and not yet in use. + .. param:: psa_key_id_t key + Identifier of the key to use for the operation. It must be an asymmetric key pair or asymmetric public key. The key must either permit the usage `PSA_KEY_USAGE_VERIFY_HASH` or `PSA_KEY_USAGE_VERIFY_MESSAGE`. + .. param:: psa_algorithm_t alg + An asymmetric signature algorithm: a value of type `psa_algorithm_t` such that :code:`PSA_ALG_IS_SIGN(alg)` is true. + .. param:: const uint8_t * signature + Buffer containing the signature to verify. + .. param:: size_t signature_length + Size of the ``signature`` buffer in bytes. + + .. return:: psa_status_t + .. retval:: PSA_SUCCESS + Success. + The operation setup must now be completed by calling `psa_verify_iop_setup_complete()`. + .. retval:: PSA_ERROR_INVALID_HANDLE + ``key`` is not a valid key identifier. + .. retval:: PSA_ERROR_NOT_PERMITTED + The following conditions can result in this error: + + * The key has neither the `PSA_KEY_USAGE_VERIFY_HASH` nor the `PSA_KEY_USAGE_VERIFY_MESSAGE` usage flag. + * The key does not permit the requested algorithm. + .. retval:: PSA_ERROR_NOT_SUPPORTED + The following conditions can result in this error: + + * ``alg`` is not supported or is not an asymmetric signature algorithm. + * ``key`` is not supported for use with ``alg``. + .. retval:: PSA_ERROR_INVALID_ARGUMENT + The following conditions can result in this error: + + * ``alg`` is not an asymmetric signature algorithm. + * ``key`` is not an asymmetric key pair, or asymmetric public key, that is compatible with ``alg``. + * ``signature`` is not a valid signature for the algorithm and key. + .. retval:: PSA_ERROR_BAD_STATE + The following conditions can result in this error: + + * The operation state is not valid: it must be inactive. + * The library requires initializing by a call to `psa_crypto_init()`. + .. retval:: PSA_ERROR_INVALID_SIGNATURE + ``signature`` is not a valid signature for the algorithm and key. + .. retval:: PSA_ERROR_INSUFFICIENT_MEMORY + .. retval:: PSA_ERROR_COMMUNICATION_FAILURE + .. retval:: PSA_ERROR_CORRUPTION_DETECTED + .. retval:: PSA_ERROR_STORAGE_FAILURE + .. retval:: PSA_ERROR_DATA_CORRUPT + .. retval:: PSA_ERROR_DATA_INVALID + + This function sets up the verification of an asymmetric signature of a message or pre-computed hash. To calculate an asymmetric signature, use an interruptible signature operation, see :secref:`interruptible-sign`. + + Use this function when the signature is available before the message or hash input. It supports all signature algorithms that are available through the interruptible verification operation. + To verify a message whose signature is available only after the message data, use `psa_verify_iop_setup_deferred_signature_start()` instead. + + After a successful call to `psa_verify_iop_setup_start()`, the operation is in setup state. Setup can be completed by calling `psa_verify_iop_setup_complete()` repeatedly, until it returns a status code that is not :code:`PSA_OPERATION_INCOMPLETE`. Once setup has begun, the application must eventually terminate the operation. The following events terminate an operation: + + * A successful call to `psa_verify_iop_complete()`. + * A call to `psa_verify_iop_abort()`. + + If `psa_verify_iop_setup_start()` returns an error, the operation object remains inactive, but its number of *ops* can be reset to zero. + +.. function:: psa_verify_iop_setup_deferred_signature_start + + .. summary:: + Begin the setup of an interruptible verification operation with a deferred signature. + + .. versionadded:: 1.6 + + .. param:: psa_verify_iop_t * operation + The interruptible verification operation to set up. It must have been initialized as per the documentation for `psa_verify_iop_t` and not yet in use. + .. param:: psa_key_id_t key + Identifier of the key to use for the operation. It must be an asymmetric key pair or asymmetric public key. The key must permit the usage `PSA_KEY_USAGE_VERIFY_MESSAGE`. + .. param:: psa_algorithm_t alg + An asymmetric message signature algorithm that supports deferred signatures: a value of type `psa_algorithm_t` such that :code:`PSA_ALG_IS_SIGN_MESSAGE(alg)` and :code:`PSA_ALG_SIGN_SUPPORTS_DEFERRED_SIGNATURE(alg)` are true. + + .. return:: psa_status_t + .. retval:: PSA_SUCCESS + Success. + The operation setup must now be completed by calling `psa_verify_iop_setup_complete()`. + .. retval:: PSA_ERROR_INVALID_HANDLE + ``key`` is not a valid key identifier. + .. retval:: PSA_ERROR_NOT_PERMITTED + The key does not have the `PSA_KEY_USAGE_VERIFY_MESSAGE` flag, or it does not permit the requested algorithm. + .. retval:: PSA_ERROR_NOT_SUPPORTED + The following conditions can result in this error: + + * ``alg`` is not supported, is not an asymmetric message signature algorithm, or does not support deferred signatures. + * ``key`` is not supported for use with ``alg``. + .. retval:: PSA_ERROR_INVALID_ARGUMENT + The following conditions can result in this error: + + * ``alg`` is not an asymmetric message signature algorithm, or does not support deferred signatures. + * ``key`` is not an asymmetric key pair, or asymmetric public key, that is compatible with ``alg``. + .. retval:: PSA_ERROR_BAD_STATE + The following conditions can result in this error: + + * The operation state is not valid: it must be inactive. + * The library requires initializing by a call to `psa_crypto_init()`. + .. retval:: PSA_ERROR_INSUFFICIENT_MEMORY + .. retval:: PSA_ERROR_COMMUNICATION_FAILURE + .. retval:: PSA_ERROR_CORRUPTION_DETECTED + .. retval:: PSA_ERROR_STORAGE_FAILURE + .. retval:: PSA_ERROR_DATA_CORRUPT + .. retval:: PSA_ERROR_DATA_INVALID + + This function sets up verification of an asymmetric signature of a message when the signature is received after the message data. + It does not accept verification of a pre-computed hash. + The application must provide the signature by calling `psa_verify_iop_set_signature()` after all message input, and before calling `psa_verify_iop_complete()`. + + `PSA_ALG_SIGN_SUPPORTS_DEFERRED_SIGNATURE()` can be used to determine whether a signature algorithm supports this flow. + + After a successful call to `psa_verify_iop_setup_deferred_signature_start()`, the operation is in setup state. Setup can be completed by calling `psa_verify_iop_setup_complete()` repeatedly, until it returns a status code that is not :code:`PSA_OPERATION_INCOMPLETE`. Once setup has begun, the application must eventually terminate the operation. The following events terminate an operation: + + * A successful call to `psa_verify_iop_complete()`. + * A call to `psa_verify_iop_abort()`. + + If this function returns an error, the operation object remains inactive, but its number of *ops* can be reset to zero. + +.. function:: psa_verify_iop_setup_complete + + .. summary:: + Finish setting up an interruptible verification operation. + + .. versionadded:: 1.6 + + .. param:: psa_verify_iop_t * operation + The interruptible verification operation to use. The operation must be in the process of being set up. + + .. return:: psa_status_t + .. retval:: PSA_SUCCESS + Success. + The operation is now ready for input of data to verify. + .. retval:: PSA_OPERATION_INCOMPLETE + The function was interrupted after exhausting the maximum *ops*. The computation is incomplete, and this function must be called again with the same operation object to continue. + .. retval:: PSA_ERROR_BAD_STATE + The following conditions can result in this error: + + * The operation state is not valid: the operation setup must have started, but not yet finished. + * The library requires initializing by a call to `psa_crypto_init()`. + .. retval:: PSA_ERROR_INVALID_SIGNATURE + The signature is not a valid signature for the algorithm and key. + .. retval:: PSA_ERROR_INSUFFICIENT_MEMORY + .. retval:: PSA_ERROR_COMMUNICATION_FAILURE + .. retval:: PSA_ERROR_CORRUPTION_DETECTED + .. retval:: PSA_ERROR_STORAGE_FAILURE + .. retval:: PSA_ERROR_DATA_CORRUPT + .. retval:: PSA_ERROR_DATA_INVALID + + .. note:: + This is an interruptible function, and must be called repeatedly, until it returns a status code that is not :code:`PSA_OPERATION_INCOMPLETE`. + + When this function returns successfully, the operation is ready for the next function in the selected verification flow. Depending on the setup function and the choice of hash or message input, the next call can be `psa_verify_iop_set_context()`, `psa_verify_iop_hash()`, `psa_verify_iop_update()`, `psa_verify_iop_set_signature()`, or `psa_verify_iop_complete()`. See the operation flows at the start of :secref:`interruptible-verify` for the permitted call sequences. + + If this function returns :code:`PSA_OPERATION_INCOMPLETE`, setup is not complete, and this function must be called again to continue the operation. + + If this function returns an error status, the operation enters an error state and must be aborted by calling `psa_verify_iop_abort()`. + + The amount of calculation performed in a single call to this function is determined by the maximum *ops* setting. See `psa_iop_set_max_ops()`. + +.. function:: psa_verify_iop_set_context + + .. summary:: + Provide a context for an interruptible verification operation. + + .. versionadded:: 1.6 + + .. param:: psa_verify_iop_t * operation + The interruptible verification operation to configure. + The operation setup must be complete, with no context, hash, message, or deferred signature input, and completion must not have started. + .. param:: const uint8_t * context + Buffer containing the context value. + .. param:: size_t context_length + Size of the ``context`` buffer in bytes. + + .. return:: psa_status_t + .. retval:: PSA_SUCCESS + Success. + .. retval:: PSA_ERROR_BAD_STATE + The following conditions can result in this error: + + * The operation state is not valid: setup must be complete, and no call to `psa_verify_iop_set_context()`, `psa_verify_iop_hash()`, `psa_verify_iop_update()`, `psa_verify_iop_set_signature()`, or `psa_verify_iop_complete()` has been made. + * The library requires initializing by a call to `psa_crypto_init()`. + .. retval:: PSA_ERROR_INVALID_ARGUMENT + The following conditions can result in this error: + + * ``context_length`` is not valid for the algorithm and key type. + * ``context`` is not a valid input value for the algorithm and key type. + .. retval:: PSA_ERROR_NOT_SUPPORTED + The context value is not supported by this implementation. + .. retval:: PSA_ERROR_INSUFFICIENT_MEMORY + .. retval:: PSA_ERROR_COMMUNICATION_FAILURE + .. retval:: PSA_ERROR_CORRUPTION_DETECTED + + This function sets the context value in an interruptible verification operation. + The application must complete setup by calling `psa_verify_iop_setup_complete()` before calling this function. + + For a signature algorithm that has a context parameter: + + * If this function is not called, the operation uses the algorithm with a zero-length or empty context. + * To set a non-zero-length context, call this function after `psa_verify_iop_setup_complete()` has returned success and before calling any input or completion function on the operation. + + If a context parameter is not supported by the algorithm, this function call can be omitted, or can be called with a zero-length context. + + The macro `PSA_ALG_SIGN_SUPPORTS_CONTEXT()` can be used to determine if a signature algorithm supports non-zero-length context values. + + If this function returns an error status, the operation enters an error state and must be aborted by calling `psa_verify_iop_abort()`. + +.. function:: psa_verify_iop_hash + + .. summary:: + Input a pre-computed hash to an interruptible verification operation. + + .. versionadded:: 1.6 + + .. param:: psa_verify_iop_t * operation + The interruptible verification operation to use. The operation must have been set up with `psa_verify_iop_setup_start()`, with no data input, and completion must not have started. + .. param:: const uint8_t * hash + The input whose signature is to be verified. This is usually the hash of a message. + + See the description of this function, or the description of individual signature algorithms, for details of the acceptable inputs. + .. param:: size_t hash_length + Size of the ``hash`` buffer in bytes. + + .. return:: psa_status_t + .. retval:: PSA_SUCCESS + Success. + The operation is now ready for completion. + .. retval:: PSA_ERROR_BAD_STATE + The following conditions can result in this error: + + * The operation state is not valid: the operation must have been set up with `psa_verify_iop_setup_start()`, and no call to `psa_verify_iop_hash()`, `psa_verify_iop_update()`, or `psa_verify_iop_complete()` has been made. + * The library requires initializing by a call to `psa_crypto_init()`. + .. retval:: PSA_ERROR_NOT_PERMITTED + The key does not have the `PSA_KEY_USAGE_VERIFY_HASH` flag. + .. retval:: PSA_ERROR_INVALID_ARGUMENT + The following conditions can result in this error: + + * The algorithm does not allow verification of a pre-computed hash. + * ``hash_length`` is not valid for the algorithm and key type. + * ``hash`` is not a valid input value for the algorithm and key type. + .. retval:: PSA_ERROR_NOT_SUPPORTED + The implementation does not support verification of a pre-computed hash. + .. retval:: PSA_ERROR_INSUFFICIENT_MEMORY + .. retval:: PSA_ERROR_COMMUNICATION_FAILURE + .. retval:: PSA_ERROR_CORRUPTION_DETECTED + .. retval:: PSA_ERROR_STORAGE_FAILURE + .. retval:: PSA_ERROR_DATA_CORRUPT + .. retval:: PSA_ERROR_DATA_INVALID + + The application must complete the setup of the operation before calling this function. A pre-computed hash can only be used with an interruptible verification operation that provides the signature as part of the set up, using `psa_verify_iop_setup_start()`. + + For hash-and-sign signature algorithms, the ``hash`` input to this function is the hash of the message to verify. The algorithm used to calculate this hash is encoded in the signature algorithm. For such algorithms, ``hash_length`` must equal the length of the hash output: :code:`hash_length == PSA_HASH_LENGTH(PSA_ALG_GET_HASH(alg))`. + + Specialized signature algorithms can apply a padding or encoding to the hash. In such cases, the encoded hash must be passed to this function. For example, see `PSA_ALG_RSA_PKCS1V15_SIGN_RAW`. + + After input of the hash, the verification operation can be completed by calling `psa_verify_iop_complete()` until it returns a status code that is not :code:`PSA_OPERATION_INCOMPLETE`. + + If this function returns an error status, the operation enters an error state and must be aborted by calling `psa_verify_iop_abort()`. + + +.. function:: psa_verify_iop_update + + .. summary:: + Add a message fragment to an interruptible verification operation. + + .. versionadded:: 1.6 + + .. param:: psa_verify_iop_t * operation + The interruptible verification operation to use. The operation must have been set up, with no pre-computed hash value or signature input, and completion must not have started. + .. param:: const uint8_t * input + Buffer containing the message fragment to add to the verification. + .. param:: size_t input_length + Size of the ``input`` buffer in bytes. + + .. return:: psa_status_t + .. retval:: PSA_SUCCESS + Success. + .. retval:: PSA_ERROR_BAD_STATE + The following conditions can result in this error: + + * The operation state is not valid: setup must be complete, and no call to `psa_verify_iop_hash()`, `psa_verify_iop_set_signature()`, or `psa_verify_iop_complete()` has been made. + * The library requires initializing by a call to `psa_crypto_init()`. + .. retval:: PSA_ERROR_NOT_PERMITTED + The key does not have the `PSA_KEY_USAGE_VERIFY_MESSAGE` flag. + .. retval:: PSA_ERROR_INVALID_ARGUMENT + The following conditions can result in this error: + + * The algorithm does not allow verification of a message. + * The selected algorithm permits only one message fragment, and `psa_verify_iop_update()` has already been called. + * The total input for the operation is too large for the signature algorithm. + .. retval:: PSA_ERROR_NOT_SUPPORTED + The following conditions can result in this error: + + * The implementation does not support verification of a message. + * The selected algorithm or implementation permits only one message fragment, and `psa_verify_iop_update()` has already been called. + * The total input for the operation is too large for the implementation. + .. retval:: PSA_ERROR_INSUFFICIENT_MEMORY + .. retval:: PSA_ERROR_COMMUNICATION_FAILURE + .. retval:: PSA_ERROR_CORRUPTION_DETECTED + .. retval:: PSA_ERROR_STORAGE_FAILURE + .. retval:: PSA_ERROR_DATA_CORRUPT + .. retval:: PSA_ERROR_DATA_INVALID + + The application must complete the setup of the operation before calling this function. + + For message-signature algorithms that process the message data multiple times when verifying a signature, `psa_verify_iop_update()` must be called exactly once with the entire message content. For signature algorithms that only process the message data once, the message content can be passed in a series of calls to `psa_verify_iop_update()`. + + After input of the message, the verification operation can be completed by calling `psa_verify_iop_complete()` until it returns a status code that is not :code:`PSA_OPERATION_INCOMPLETE`. + + If this function returns an error status, the operation enters an error state and must be aborted by calling `psa_verify_iop_abort()`. + +.. function:: psa_verify_iop_set_signature + + .. summary:: + Provide the signature for a deferred-signature interruptible verification operation. + + .. versionadded:: 1.6 + + .. param:: psa_verify_iop_t * operation + The interruptible verification operation to use. It must have been set up with `psa_verify_iop_setup_deferred_signature_start()`, setup must be complete, and completion must not have started. + .. param:: const uint8_t * signature + Buffer containing the signature to verify. + .. param:: size_t signature_length + Size of the ``signature`` buffer in bytes. + + .. return:: psa_status_t + .. retval:: PSA_SUCCESS + Success. + The operation is ready for completion. + .. retval:: PSA_ERROR_BAD_STATE + The following conditions can result in this error: + + * The operation state is not valid: the operation must have been set up with `psa_verify_iop_setup_deferred_signature_start()`, setup must be complete, and no call to `psa_verify_iop_set_signature()` or `psa_verify_iop_complete()` has been made. + * The library requires initializing by a call to `psa_crypto_init()`. + .. retval:: PSA_ERROR_INVALID_ARGUMENT + ``signature`` is not a valid signature for the algorithm and key. + .. retval:: PSA_ERROR_INVALID_SIGNATURE + ``signature`` is not a valid signature for the algorithm and key. + .. retval:: PSA_ERROR_INSUFFICIENT_MEMORY + .. retval:: PSA_ERROR_COMMUNICATION_FAILURE + .. retval:: PSA_ERROR_CORRUPTION_DETECTED + + The application must complete the setup of the operation before calling this function. This function is for use with the deferred-signature flow, when the operation is set up by calling `psa_verify_iop_setup_deferred_signature_start()`. + + The application must call this function after all calls to `psa_verify_iop_update()`, and before the first call to `psa_verify_iop_complete()`. + + If this function returns an error status, the operation enters an error state and must be aborted by calling `psa_verify_iop_abort()`. + +.. function:: psa_verify_iop_complete + + .. summary:: + Progress an interruptible signature verification. + + .. versionadded:: 1.6 + + .. param:: psa_verify_iop_t * operation + The interruptible verification operation to use. The operation must be active, and setup must be complete. If the operation was set up with `psa_verify_iop_setup_deferred_signature_start()`, the signature must have been provided with `psa_verify_iop_set_signature()`. + + .. return:: psa_status_t + .. retval:: PSA_SUCCESS + Success. + The signature is valid. + .. retval:: PSA_OPERATION_INCOMPLETE + The function was interrupted after exhausting the maximum *ops*. The computation is incomplete, and this function must be called again with the same operation object to continue. + .. retval:: PSA_ERROR_NOT_PERMITTED + If no data has been input to the operation, the key does not have the `PSA_KEY_USAGE_VERIFY_MESSAGE` flag. + .. retval:: PSA_ERROR_BAD_STATE + The following conditions can result in this error: + + * The operation state is not valid: the operation must be active, setup must be complete, and a deferred-signature operation must have a signature input. + * The library requires initializing by a call to `psa_crypto_init()`. + .. retval:: PSA_ERROR_INVALID_ARGUMENT + If no data has been input to the operation, the algorithm does not allow verification of a message. + .. retval:: PSA_ERROR_NOT_SUPPORTED + If no data has been input to the operation, the implementation does not support verification of a message. + .. retval:: PSA_ERROR_INVALID_SIGNATURE + The signature is not the result of signing the input message, or hash value, with the requested algorithm, using the private key corresponding to the key provided to the operation. + .. retval:: PSA_ERROR_INSUFFICIENT_MEMORY + .. retval:: PSA_ERROR_COMMUNICATION_FAILURE + .. retval:: PSA_ERROR_CORRUPTION_DETECTED + .. retval:: PSA_ERROR_STORAGE_FAILURE + .. retval:: PSA_ERROR_DATA_CORRUPT + .. retval:: PSA_ERROR_DATA_INVALID + + .. note:: + This is an interruptible function, and must be called repeatedly, until it returns a status code that is not :code:`PSA_OPERATION_INCOMPLETE`. + + When this function returns successfully, the operation becomes inactive. + If neither a pre-computed hash nor a message fragment has been input, the signature is verified against the zero-length message. + + If this function returns :code:`PSA_OPERATION_INCOMPLETE`, this function must be called again to continue the operation. + + If this function returns an error status, the operation enters an error state and must be aborted by calling `psa_verify_iop_abort()`. + + The amount of calculation performed in a single call to this function is determined by the maximum *ops* setting. See `psa_iop_set_max_ops()`. + +.. function:: psa_verify_iop_abort + + .. summary:: + Abort an interruptible verification operation. + + .. versionadded:: 1.6 + + .. param:: psa_verify_iop_t * operation + The interruptible verification operation to abort. + + .. return:: psa_status_t + .. retval:: PSA_SUCCESS + Success. + The operation object can now be discarded or reused. + .. retval:: PSA_ERROR_COMMUNICATION_FAILURE + .. retval:: PSA_ERROR_CORRUPTION_DETECTED + .. retval:: PSA_ERROR_BAD_STATE + The library requires initializing by a call to `psa_crypto_init()`. + + 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_verify_iop_setup_start()` or `psa_verify_iop_setup_deferred_signature_start()`. + + This function can be called at any time after the operation object has been initialized as described in `psa_verify_iop_t`. + + In particular, it is valid to call `psa_verify_iop_abort()` twice, or to call `psa_verify_iop_abort()` on an operation that has not been set up. + +Support macros +-------------- + +.. macro:: PSA_ALG_IS_SIGN_MESSAGE + :definition: /* specification-defined value */ + + .. summary:: + Whether the specified algorithm is a signature algorithm that can be used with message signature and verification functions, multi-part signature operations, and interruptible signature operations. + + .. param:: alg + An algorithm identifier: a value of type `psa_algorithm_t`. + + .. return:: + ``1`` if ``alg`` is a signature algorithm that can be used to sign a message. ``0`` if ``alg`` is a signature algorithm that can only be used to sign a pre-computed hash. ``0`` if ``alg`` is not a signature algorithm. This macro can return either ``0`` or ``1`` if ``alg`` is not a supported algorithm identifier. + + This macro evaluates to ``1`` for hash-and-sign and message-signature algorithms. + +.. macro:: PSA_ALG_IS_SIGN_HASH + :definition: /* specification-defined value */ + + .. summary:: + Whether the specified algorithm is a signature algorithm that can be used with hash signature and verification functions, and with interruptible signature operations. + + .. param:: alg + An algorithm identifier: a value of type `psa_algorithm_t`. + + .. return:: + ``1`` if ``alg`` is a signature algorithm that can be used to sign a hash. ``0`` if ``alg`` is a signature algorithm that can only be used to sign a message. ``0`` if ``alg`` is not a signature algorithm. This macro can return either ``0`` or ``1`` if ``alg`` is not a supported algorithm identifier. + + This macro evaluates to ``1`` for hash-and-sign and specialized signature algorithms. + +.. macro:: PSA_ALG_IS_HASH_AND_SIGN + :definition: /* specification-defined value */ + + .. summary:: + Whether the specified algorithm is a hash-and-sign algorithm that signs exactly the hash value. + + .. param:: alg + An algorithm identifier: a value of type `psa_algorithm_t`. + + .. return:: + ``1`` if ``alg`` is a hash-and-sign algorithm that signs exactly the hash value, ``0`` otherwise. This macro can return either ``0`` or ``1`` if ``alg`` is not a supported algorithm identifier. + + A wildcard signature algorithm policy, using `PSA_ALG_ANY_HASH`, returns the same value as the signature algorithm parameterized with a valid hash algorithm. + + This macro identifies algorithms that can be used with `psa_sign_hash()` that use the exact message hash value as an input the signature operation. For example, if :code:`PSA_ALG_IS_HASH_AND_SIGN(alg)` is true, the following call sequence is equivalent to :code:`psa_sign_message(key, alg, msg, msg_len, ...)`: + + .. code-block:: xref + + uint8_t hash[PSA_HASH_MAX_SIZE]; + size_t hash_len; + psa_hash_compute(PSA_ALG_GET_HASH(alg), msg, msg_len, + hash, sizeof(hash), &hash_len); + psa_sign_hash(key, alg, hash, hash_len, ...); + +.. macro:: PSA_ALG_SIGN_SUPPORTS_CONTEXT + :definition: /* implementation-defined value */ + + .. summary:: + Whether the specified signature algorithm can be used with a non-zero-length context. + + .. versionadded:: 1.4 + + .. param:: alg + A signature algorithm identifier: a value of type `psa_algorithm_t` such that :code:`PSA_ALG_IS_SIGN(alg)` is true. + + .. return:: + ``1`` if ``alg`` is a signature algorithm that can be used with a non-zero-length context. + ``0`` if ``alg`` is a signature algorithm that cannot be used with a non-zero-length context. + This macro can return either ``0`` or ``1`` if ``alg`` is not a supported signature algorithm identifier. + + A wildcard signature algorithm policy, using `PSA_ALG_ANY_HASH`, returns the same value as the signature algorithm parameterized with a valid hash algorithm. + + This macro identifies signature algorithms that have a context parameter, and can be used with the appropriate functions that support non-zero-length contexts. + +.. macro:: PSA_ALG_SIGN_SUPPORTS_DEFERRED_SIGNATURE + :definition: /* implementation-defined value */ + + .. summary:: + Whether the specified signature algorithm supports verification with a deferred signature. + + .. versionadded:: 1.6 + + .. param:: alg + A signature algorithm identifier: a value of type `psa_algorithm_t` such that :code:`PSA_ALG_IS_SIGN(alg)` is true. + + .. return:: + ``1`` if ``alg`` is a signature algorithm that can verify a message when the signature is provided after the message input. + ``0`` if ``alg`` is a signature algorithm that requires the signature before message input. + This macro can return either ``0`` or ``1`` if ``alg`` is not a supported signature algorithm identifier. + + This macro identifies algorithms that can be used with the deferred-signature interruptible verification flow, beginning with `psa_verify_iop_setup_deferred_signature_start()`. + It indicates algorithm compatibility only. An implementation can still return :code:`PSA_ERROR_NOT_SUPPORTED` if it does not support the deferred-signature flow for the algorithm. .. macro:: PSA_ALG_ANY_HASH :definition: ((psa_algorithm_t)0x020000ff) diff --git a/doc/crypto/appendix/history.rst b/doc/crypto/appendix/history.rst index 050497ff..641f148f 100644 --- a/doc/crypto/appendix/history.rst +++ b/doc/crypto/appendix/history.rst @@ -12,21 +12,33 @@ Document change history This section provides the detailed changes made between published version of the document. -Changes in the draft GlobalPlatform publication revision -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +Changes between *1.5* and *1.6* +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Changes to the API +~~~~~~~~~~~~~~~~~~ + +* Added interruptible operations for asymmetric signatures, key generation, public-key export, and key agreement. See: + + - :secref:`interruptible-sign` and :secref:`interruptible-verify` + - :secref:`interruptible-generate-key` + - :secref:`interruptible-export-key` + - :secref:`interruptible-key-agreement` + + These APIs enable applications to bound the expensive computation performed in a single call. See :secref:`interruptible-operations`. Clarifications and fixes ~~~~~~~~~~~~~~~~~~~~~~~~ -* Corrected the WPA3-SAE operation example code: the send-confirm counter input step is `PSA_PAKE_STEP_CONFIRM_COUNT`, and the shared key is extracted from the ``wpa3_sae`` operation. -* Corrected the SPAKE2+ operation example code: the Prover input step for the Verifier confirmation value is `PSA_PAKE_STEP_CONFIRM`. +* Corrected the WPA3-SAE operation example code: the send-confirm counter input step is `PSA_PAKE_STEP_CONFIRM_COUNT`, and the shared key is extracted from the ``wpa3_sae`` operation. +* Corrected the SPAKE2+ operation example code: the Prover input step for the Verifier confirmation value is `PSA_PAKE_STEP_CONFIRM`. Other changes ~~~~~~~~~~~~~ -* Migrated the document to the 2026 PSA API template. +* Migrated the document to the 2026 PSA API template. - This changes the document front matter structure and publication styling, without changing the API. + This changes the document front matter structure and publication styling, without changing the API. Changes between *1.4.1* and *1.5* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/doc/crypto/figure/interruptible_operation.pdf b/doc/crypto/figure/interruptible_operation.pdf new file mode 100644 index 00000000..5f6cd4ed Binary files /dev/null and b/doc/crypto/figure/interruptible_operation.pdf differ diff --git a/doc/crypto/figure/interruptible_operation.puml b/doc/crypto/figure/interruptible_operation.puml new file mode 100644 index 00000000..df7508c2 --- /dev/null +++ b/doc/crypto/figure/interruptible_operation.puml @@ -0,0 +1,32 @@ +' SPDX-FileCopyrightText: Copyright 2023-2024 Arm Limited and/or its affiliates +' SPDX-License-Identifier: CC-BY-SA-4.0 AND LicenseRef-Patent-license + +@startuml +!include psa-spec.pumh + +skinparam LegendFontSize 12 + +legend bottom + --""———""-- Solid lines show successful operation + ""---"" Dashed lines show error flows + ""………"" Dotted lines show operation cancellation +end legend + +state inactive as "//inactive//" +state active as "//active//" +state error as "//error//" ##darkred + +[*] --> inactive: **Initialize** +note as N1 + Operation object starts as + uninitialised memory +end note +inactive --> active: **Start** +active --> active: **Complete**\n//incomplete// +active --> inactive: **Complete**\n//success// +error -[#darkred,dashed]r-> inactive: **Abort** +inactive -[#darkred,dashed]-> inactive: **Start**\n//fails// +active -[#darkred,dashed]-> error: **Complete**\n//fails// +active -[#blue,dotted]l-> inactive: **Abort** + +@enduml diff --git a/doc/crypto/figure/interruptible_operation.svg b/doc/crypto/figure/interruptible_operation.svg new file mode 100644 index 00000000..2273ea55 --- /dev/null +++ b/doc/crypto/figure/interruptible_operation.svg @@ -0,0 +1 @@ +inactiveactiveerrorOperation object starts asuninitialised memoryInitializeStartCompletesuccessAbortCompleteincompleteAbortStartfailsCompletefails———Solid lines show successful operation---Dashed lines show error flows………Dotted lines show operation cancellation \ No newline at end of file diff --git a/doc/crypto/figure/interruptible_operation_complex.pdf b/doc/crypto/figure/interruptible_operation_complex.pdf new file mode 100644 index 00000000..3a51237b Binary files /dev/null and b/doc/crypto/figure/interruptible_operation_complex.pdf differ diff --git a/doc/crypto/figure/interruptible_operation_complex.puml b/doc/crypto/figure/interruptible_operation_complex.puml new file mode 100644 index 00000000..e4ffa518 --- /dev/null +++ b/doc/crypto/figure/interruptible_operation_complex.puml @@ -0,0 +1,44 @@ +' SPDX-FileCopyrightText: Copyright 2023-2024 Arm Limited and/or its affiliates +' SPDX-License-Identifier: CC-BY-SA-4.0 AND LicenseRef-Patent-license + +@startuml +!include psa-spec.pumh + +skinparam LegendFontSize 12 + +legend bottom + --""———""-- Solid lines show successful operation + ""---"" Dashed lines show error flows + ""………"" Dotted lines show operation cancellation +end legend + +state inactive as "//inactive//" +state setup as "//setup//" +state input as "//input//" +state completing as "//completing//" +state error as "//error//" ##darkred + +[*] --> inactive: **Initialize** +note as N1 + Operation object starts as + uninitialised memory +end note +inactive --> setup: **Setup-start**\n//success// +setup --> setup: **Setup-complete**\n//incomplete// +setup --> input: **Setup-complete**\n//success// +input --> input: **Update**\n//success// +input --> completing: **Complete**\n//incomplete// +input -u-> inactive: **Complete**\n//success// +completing --> completing: **Complete**\n//incomplete// +completing -u-> inactive: **Complete**\n//success// +error -[#darkred,dashed]r-> inactive: **Abort** +inactive -[#darkred,dashed]-> inactive: **Setup-start**\n//fails// +setup -[#darkred,dashed]-> error: **Setup-complete**\n//fails// +input -[#darkred,dashed]-> error: **Update**\n//fails// +input -[#darkred,dashed]-> error: **Complete**\n//fails// +completing -[#darkred,dashed]-> error: **Complete**\n//fails// +setup -[#blue,dotted]u-> inactive: **Abort** +input -[#blue,dotted]u-> inactive: **Abort** +completing -[#blue,dotted]u-> inactive: **Abort** + +@enduml diff --git a/doc/crypto/figure/interruptible_operation_complex.svg b/doc/crypto/figure/interruptible_operation_complex.svg new file mode 100644 index 00000000..38674281 --- /dev/null +++ b/doc/crypto/figure/interruptible_operation_complex.svg @@ -0,0 +1 @@ +inactivesetupinputcompletingerrorOperation object starts asuninitialised memoryInitializeSetup-startsuccessAbortSetup-completeincompleteSetup-completesuccessUpdatesuccessCompleteincompleteCompletesuccessAbortCompleteincompleteCompletesuccessAbortAbortSetup-startfailsSetup-completefailsUpdatefailsCompletefailsCompletefails———Solid lines show successful operation---Dashed lines show error flows………Dotted lines show operation cancellation \ No newline at end of file diff --git a/doc/crypto/overview/functionality.rst b/doc/crypto/overview/functionality.rst index 17e7d5f2..6b840755 100644 --- a/doc/crypto/overview/functionality.rst +++ b/doc/crypto/overview/functionality.rst @@ -121,7 +121,7 @@ The API supports cryptographic operations through two kinds of interfaces: * A *multi-part operation* is a set of functions that work with a stored operation state. This provides more control over operation configuration, piecewise processing of large input data, or handling for multi-step processes. See :secref:`multi-part-operations`. -Depending on the mechanism, one or both kind of interfaces may be provided. +* An *interruptible operation* is a distinct set of functions that works with a stored operation state. It enables the application to limit the computation performed in a single function call for computationally expensive algorithms, for example digital signatures. See :secref:`interruptible-operations`. .. _single-part-functions: @@ -134,6 +134,7 @@ Single-part functions do not meet the needs of all use cases: * Some use cases involve messages that are too large to be assembled in memory, or require non-default configuration of the algorithm. These use cases require the use of a `multi-part operation `. +* Some use cases require that the time spent in a single function call is bounded. Processing input data in fragments can meet this requirement for some algorithms. When an algorithm has computationally expensive steps that are independent of the input size, the use case requires an `interruptible operation `. .. _multi-part-operations: @@ -222,6 +223,179 @@ It is safe to move a multi-part operation object to a different memory location, Each type of multi-part operation can have multiple *active* states. Documentation for the specific operation describes the configuration and update functions, and any requirements about their usage and ordering. +See :secref:`hash-mp` for an example of a multi-part operation. + +.. _interruptible-operations: + +Interruptible operations +~~~~~~~~~~~~~~~~~~~~~~~~ + +Interruptible operations split a computationally expensive operation into a sequence of function calls, each of which limits the computational progress made. They are useful when responsiveness is critical and the environment does not provide suitable multitasking. + +Processing a bounded amount of input in each call can meet this need for some operations. For example, a hash can be computed using a multi-part operation to break the computation into smaller blocks. However, a multi-part operation does not bound computation that is independent of the input size, such as calculating a shared secret in key agreement. + +.. note:: + + Interruptible operations and multi-part operations are distinct optional API capabilities. An implementation can provide one without the other. Applications that need only fragmented input should use the ordinary multi-part API when it is available. Applications that require bounded computation must use the interruptible API, and must handle :code:`PSA_OPERATION_INCOMPLETE`. + + Some interruptible operations accept input in a sequence of update calls. This does not make them substitutes for the ordinary multi-part operations: they have additional state, use an execution budget, and can report :code:`PSA_OPERATION_INCOMPLETE`. + +Use cases for which the |API| defines interruptible operations include: + +* Asymmetric signature generation and verification. +* Key exchange protocols, including the use of ephemeral key-pairs. + +Interruptible operations have three principal components: + +* A specific object type to maintain the state of the operation, in a similar way to multi-part operations. These types are implementation-defined. +* A non-error status code, :code:`PSA_OPERATION_INCOMPLETE`, that is returned by some interruptible operation functions to indicate that the computation is incomplete. The same function must be called repeatedly until it returns either a success or an error status. +* The concept of a unit of work --- called *ops* --- that can be carried out by an interruptible operation function. + +*ops* limits and accounting +^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +The *maximum ops* value limits the *ops* performed by each call to an interruptible function. An application sets this value by calling `psa_iop_set_max_ops()`, and can query the current value with `psa_iop_get_max_ops()`. If a function exhausts the maximum *ops* before its calculation is complete, it returns :code:`PSA_OPERATION_INCOMPLETE`. + +After a successful call to `psa_crypto_init()`, the *maximum ops* value defaults to `PSA_IOP_MAX_OPS_UNLIMITED`. With this default, interruptible functions complete their calculation before returning. An application can set a lower value to bound the computation performed in one function call. + +The computation and execution time represented by an *op* have no fixed meaning across implementations or functions. They can depend on the hardware, algorithm, key type, and current stage of the operation. Applications can use the cumulative *ops* count to tune the maximum value for a particular implementation. + +The cumulative *ops* count for an interruptible operation can be retrieved by calling the operation's associated ``psa_xxx_iop_get_num_ops()`` function. This count can be queried while the operation is active, or after a successful completion to obtain the count for the entire operation. It is reset when the operation is aborted or when an operation object is successfully started for a new operation. A failed start can also reset the count. + +Interruptible operation flow +^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Interruptible operations follow a common pattern of use, which is shown in :numref:`fig-interruptible`. + +.. figure:: /figure/interruptible_operation.* + :name: fig-interruptible + + General state model for an interruptible operation + +The typical sequence of actions with an interruptible operation is as follows: + +1. **Allocate:** Allocate memory for an operation object of the appropriate type. + The application can use any allocation strategy: stack, heap, static, etc. + +#. **Initialize:** Initialize or assign the interruptible operation object by one of the following methods: + + - Set it to logical zero. + This is automatic for static and global variables. + Explicit initialization must use the associated ``PSA_xxx_IOP_INIT`` macro as the type is implementation-defined. + - Set it to all-bits zero. + This is automatic if the object was allocated with ``calloc()``. + - Assign the value of the associated macro ``PSA_xxx_IOP_INIT``. + - Assign the result of calling the associated function ``psa_xxx_iop_init()``. + + The resulting object is now *inactive*. + + It is an error to initialize an interruptible operation object that is in *active* or *error* states. This can leak memory or other resources. + +#. **Start:** Start a new interruptible operation on an *inactive* operation object. + Each interruptible operation object will define one or more functions that start a specific operation. + + The accumulated *ops* value for the operation is reset to zero. + + On success, a start function will put an interruptible operation object into an *active* state. + On failure, the operation object will remain *inactive*. + +#. **Complete:** To end an interruptible operation, call the applicable completion function. + This will perform the final computation, produce any final outputs, and then release any resources associated with the operation. + + If the computation cannot be completed within the *maximum ops*, the interruptible operation is left in an *active* state. + If the operation completes successfully, the operation enters an *inactive* state. + On failure, the operation object will enter an *error* state. + + An application needs to repeat this step until the completion function returns with a success or an error status. + +#. **Abort:** An interruptible operation can be aborted at any stage during its use by calling the associated ``psa_xxx_iop_abort()`` function. + This will release any resources associated with the operation, return the operation object to the *inactive* state, and reset the accumulated *ops* value to zero. + + Any error that occurs to an operation while it is in an *active* state will result in the operation entering an *error* state. + The application must call the associated ``psa_xxx_iop_abort()`` function to release the operation resources and return the object to the *inactive* state. + + ``psa_xxx_iop_abort()`` can be called on an *inactive* interruptible operation, and this has no effect. + +Once an interruptible operation object is returned to the *inactive* state, it can be reused by calling one of the start functions again. + +If an interruptible operation object is not initialized before use, the behavior is undefined. + +If an interruptible operation function determines that the operation object is not in any valid state, it can return :code:`PSA_ERROR_CORRUPTION_DETECTED`. + +If an interruptible operation function is called with an operation object in the wrong state, the function will return :code:`PSA_ERROR_BAD_STATE` and the operation object will enter the *error* state. + +It is safe to move an interruptible operation object to a different memory location, for example, using a bitwise copy, and then to use the object in the new location. +For example, an application can allocate an operation object on the stack and return it, or the operation object can be allocated within memory managed by a garbage collector. +However, this does not permit the following behaviors: + +* Moving the object while a function is being called on the object. See also :secref:`concurrency`. +* Working with both the original and the copied operation objects. + +Each type of interruptible operation can have multiple *active* states. +Documentation for the specific operation describes the start and completion functions, and any requirements about their usage and ordering. + +See :secref:`interruptible-generate-key` for an example of an interruptible operation. + +.. _interruptible-signature-operations: + +Interruptible signature operations +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The interruptible signature and verification APIs are separate from the ordinary multi-part signature APIs. They provide bounded computation for the setup and completion of an asymmetric signature operation. They also accept message input where the selected algorithm permits it, but this is incidental to their execution-budget purpose. + +These operations can have more than one step at which the application calls the same function repeatedly until it returns a status other than :code:`PSA_OPERATION_INCOMPLETE`. + +:numref:`fig-interruptible-signature` shows the state model used for interruptible signature and verification operations. + +.. figure:: /figure/interruptible_operation_complex.* + :name: fig-interruptible-signature + + State model for an interruptible signature operation + +The sequence has the common interruptible-operation steps, with a setup phase that can itself require bounded computation and an optional input phase. The following flow adds detail for an interruptible signature operation. Steps without accompanying text are the same as in the preceding interruptible-operation flow: + +1. **Allocate** + +#. **Initialize** + +#. **Start setup:** Start the setup phase of a new interruptible signature or verification operation on an *inactive* object. + + The accumulated *ops* value for the operation is reset to zero. + + On success, an operation object enters a *setup* state. + On failure, the operation object will remain *inactive*. + +#. **Complete setup:** Complete setup on an object in the *setup* state. + + If the setup computation is interrupted, the operation remains in *setup* state. + If setup completes successfully, the operation enters an *input* state. + On failure, the operation object will enter an *error* state. + + An application needs to repeat this step until the setup completes with success or an error status. + +#. **Input:** Provide data to an object in the *input* state. + The interruptible signature and verification APIs provide functions to set the context, input a pre-computed message hash or a message fragment, and provide a deferred signature. See the individual interruptible operations for the specific ordering requirements on input data. + + On success, the operation object remains in *input* state. + On failure, the operation object will enter an *error* state. + +#. **Complete:** To end an interruptible operation, call the applicable completion function. + This will perform the final computation, produce any final outputs, and then release any resources associated with the operation. + + If the finishing computation is interrupted, the operation is left in the *completing* state. + If the operation completes successfully, the operation enters an *inactive* state. + On failure, the operation object will enter an *error* state. + + An application needs to repeat this step until the completion function completes with success or an error status. + +#. **Abort** + +The rules for use of an interruptible operation apply to an interruptible signature operation. See :secref:`interruptible-operations`. + +Each type of interruptible signature operation can have multiple *setup*, *input*, and *completing* states. The operation documentation describes the setup, input, and completion functions and their ordering requirements. + +See :secref:`interruptible-sign` for the interruptible signature API. + Symmetric cryptography ~~~~~~~~~~~~~~~~~~~~~~ @@ -254,7 +428,7 @@ Here is an example of a use case where a master key is used to generate both a m #. Populate a `psa_key_attributes_t` object with the derived message encryption key’s attributes. #. Call `psa_key_derivation_output_key()` to create the derived message key. #. Call `psa_key_derivation_output_bytes()` to generate the derived IV. - #. Call `psa_key_derivation_abort()` to release the key-derivation operation memory. + #. Call `psa_key_derivation_abort()` to release the key derivation operation memory. #. Encrypt the message with the derived material. @@ -277,11 +451,11 @@ This specification defines interfaces for the following types of asymmetric cryp * Key encapsulation. See :secref:`key-encapsulation`. * Password-authenticated key exchange (PAKE). See :secref:`pake`. -For asymmetric signature, the API provides *single-part* functions and *multi-part* operations. +For asymmetric signature, the API provides single-part functions, multi-part operations, and interruptible operations. For asymmetric encryption, the API provides single-part functions. -For key agreement, the API provides single-part functions and an additional input method for a key-derivation operation. +For key agreement, the API provides single-part functions, an interruptible operation, and an additional input method for a key derivation operation. For key encapsulation, the API provides single-part functions.