Skip to content

Commit 46ca443

Browse files
authored
Merge pull request #282 from MarcusJGStreets/check_key_usage
Check key usage
2 parents 7339236 + 09044b4 commit 46ca443

File tree

3 files changed

+125
-16
lines changed

3 files changed

+125
-16
lines changed

doc/crypto/api.db/psa/crypto.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ typedef struct psa_custom_key_parameters_t {
331331
#define PSA_KEY_USAGE_COPY ((psa_key_usage_t)0x00000002)
332332
#define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200)
333333
#define PSA_KEY_USAGE_DERIVE ((psa_key_usage_t)0x00004000)
334+
#define PSA_KEY_USAGE_DERIVE_PUBLIC ((psa_key_usage_t)0x00000080)
334335
#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
335336
#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
336337
#define PSA_KEY_USAGE_SIGN_HASH ((psa_key_usage_t)0x00001000)
@@ -464,6 +465,9 @@ psa_status_t psa_attach_key(const psa_key_attributes_t * attributes,
464465
const uint8_t * label,
465466
size_t label_length,
466467
psa_key_id_t * key);
468+
psa_status_t psa_check_key_usage(psa_key_id_t key,
469+
psa_algorithm_t alg,
470+
psa_key_usage_t usage);
467471
psa_status_t psa_cipher_abort(psa_cipher_operation_t * operation);
468472
psa_status_t psa_cipher_decrypt(psa_key_id_t key,
469473
psa_algorithm_t alg,

doc/crypto/api/keys/policy.rst

Lines changed: 120 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,17 @@ When a key is used in a cryptographic operation, the application must supply the
8181
Key usage flags
8282
---------------
8383

84-
The usage flags are encoded in a bitmask, which has the type `psa_key_usage_t`. Four kinds of usage flag can be specified:
84+
The usage flags are encoded in a bitmask, which has the type `psa_key_usage_t`. There are two kinds of usage flag:
8585

86-
* The extractable flag `PSA_KEY_USAGE_EXPORT` determines whether the key material can be extracted from the cryptoprocessor, or copied outside of its current security boundary.
87-
* The copyable flag `PSA_KEY_USAGE_COPY` determines whether the key material can be copied into a new key, which can have a different lifetime or a more restrictive policy.
88-
* The cacheable flag `PSA_KEY_USAGE_CACHE` determines whether the implementation is permitted to retain non-essential copies of the key material in RAM. This policy only applies to persistent keys. See also :secref:`key-material`.
89-
* The following usage flags determine whether the corresponding operations are permitted with the key:
86+
1. Key-management usage flags.
87+
88+
- The extractable flag `PSA_KEY_USAGE_EXPORT` determines whether the key material can be extracted from the cryptoprocessor, or copied outside of its current security boundary.
89+
- The copyable flag `PSA_KEY_USAGE_COPY` determines whether the key material can be copied into a new key, which can have a different lifetime or a more restrictive policy.
90+
- The cacheable flag `PSA_KEY_USAGE_CACHE` determines whether the implementation is permitted to retain non-essential copies of the key material in RAM. This policy only applies to persistent keys. See also :secref:`key-material`.
91+
92+
2. Cryptographic-operation usage flags.
93+
94+
The following usage flags determine whether the corresponding cryptographic operations are permitted with the key:
9095

9196
- `PSA_KEY_USAGE_ENCRYPT`
9297
- `PSA_KEY_USAGE_DECRYPT`
@@ -97,6 +102,8 @@ The usage flags are encoded in a bitmask, which has the type `psa_key_usage_t`.
97102
- `PSA_KEY_USAGE_DERIVE`
98103
- `PSA_KEY_USAGE_VERIFY_DERIVATION`
99104

105+
The flag `PSA_KEY_USAGE_DERIVE_PUBLIC` is used in the function `psa_check_key_usage()` to query if a key can be used for the public role in the specified algorithm.
106+
100107
.. typedef:: uint32_t psa_key_usage_t
101108

102109
.. summary::
@@ -108,7 +115,7 @@ The usage flags are encoded in a bitmask, which has the type `psa_key_usage_t`.
108115
.. summary::
109116
Permission to export the key.
110117

111-
This flag permits a key to be moved outside of the security boundary of its current storage location. In particular:
118+
This key-management usage flag permits a key to be moved outside of the security boundary of its current storage location. In particular:
112119

113120
* This flag is required to export a key from the cryptoprocessor using `psa_export_key()`. A public key or the public part of a key pair can always be exported regardless of the value of this permission flag.
114121

@@ -122,7 +129,7 @@ The usage flags are encoded in a bitmask, which has the type `psa_key_usage_t`.
122129
.. summary::
123130
Permission to copy the key.
124131

125-
This flag is required to make a copy of a key using `psa_copy_key()`.
132+
This key-management usage flag is required to make a copy of a key using `psa_copy_key()`.
126133

127134
For a key lifetime that corresponds to a secure element location that enforces the non-exportability of keys, copying a key outside the secure element also requires the usage flag `PSA_KEY_USAGE_EXPORT`. Copying the key within the secure element is permitted with just `PSA_KEY_USAGE_COPY`, if the secure element supports it. For keys with the lifetime `PSA_KEY_LIFETIME_VOLATILE` or `PSA_KEY_LIFETIME_PERSISTENT`, the usage flag `PSA_KEY_USAGE_COPY` is sufficient to permit the copy.
128135

@@ -132,7 +139,7 @@ The usage flags are encoded in a bitmask, which has the type `psa_key_usage_t`.
132139
.. summary::
133140
Permission for the implementation to cache the key.
134141

135-
This flag permits the implementation to make additional copies of the key material that are not in storage and not for the purpose of an ongoing operation. Applications can use it as a hint for the cryptoprocessor, to keep a copy of the key around for repeated access.
142+
This key-management usage flag permits the implementation to make additional copies of the key material that are not in storage and not for the purpose of an ongoing operation. Applications can use it as a hint for the cryptoprocessor, to keep a copy of the key around for repeated access.
136143

137144
An application can request that cached key material is removed from memory by calling `psa_purge_key()`.
138145

@@ -153,7 +160,7 @@ The usage flags are encoded in a bitmask, which has the type `psa_key_usage_t`.
153160
.. summary::
154161
Permission to encrypt a message, or perform key encapsulation, with the key.
155162

156-
This flag is required to use the key in a symmetric encryption operation, in an AEAD encryption-and-authentication operation, in an asymmetric encryption operation, or in a key-encapsulation operation. The flag must be present on keys used with the following APIs:
163+
This cryptographic-operation usage flag is required to use the key in a symmetric encryption operation, in an AEAD encryption-and-authentication operation, in an asymmetric encryption operation, or in a key-encapsulation operation. The flag must be present on keys used with the following APIs:
157164

158165
* `psa_cipher_encrypt()`
159166
* `psa_cipher_encrypt_setup()`
@@ -170,7 +177,7 @@ The usage flags are encoded in a bitmask, which has the type `psa_key_usage_t`.
170177
.. summary::
171178
Permission to decrypt a message, or perform key decapsulation, with the key.
172179

173-
This flag is required to use the key in a symmetric decryption operation, in an AEAD decryption-and-verification operation, in an asymmetric decryption operation, or in a key-decapsulation operation. The flag must be present on keys used with the following APIs:
180+
This cryptographic-operation usage flag is required to use the key in a symmetric decryption operation, in an AEAD decryption-and-verification operation, in an asymmetric decryption operation, or in a key-decapsulation operation. The flag must be present on keys used with the following APIs:
174181

175182
* `psa_cipher_decrypt()`
176183
* `psa_cipher_decrypt_setup()`
@@ -187,7 +194,7 @@ The usage flags are encoded in a bitmask, which has the type `psa_key_usage_t`.
187194
.. summary::
188195
Permission to sign a message with the key.
189196

190-
This flag is required to use the key in a MAC calculation operation, or in an asymmetric message signature operation. The flag must be present on keys used with the following APIs:
197+
This cryptographic-operation usage flag is required to use the key in a MAC calculation operation, or in an asymmetric message signature operation. The flag must be present on keys used with the following APIs:
191198

192199
* `psa_mac_compute()`
193200
* `psa_mac_sign_setup()`
@@ -201,7 +208,7 @@ The usage flags are encoded in a bitmask, which has the type `psa_key_usage_t`.
201208
.. summary::
202209
Permission to verify a message signature with the key.
203210

204-
This flag is required to use the key in a MAC verification operation, or in an asymmetric message signature verification operation. The flag must be present on keys used with the following APIs:
211+
This cryptographic-operation usage flag is required to use the key in a MAC verification operation, or in an asymmetric message signature verification operation. The flag must be present on keys used with the following APIs:
205212

206213
* `psa_mac_verify()`
207214
* `psa_mac_verify_setup()`
@@ -215,7 +222,7 @@ The usage flags are encoded in a bitmask, which has the type `psa_key_usage_t`.
215222
.. summary::
216223
Permission to sign a message hash with the key.
217224

218-
This flag is required to use the key to sign a pre-computed message hash in an asymmetric signature operation. The flag must be present on keys used with the following APIs:
225+
This cryptographic-operation usage flag is required to use the key to sign a pre-computed message hash in an asymmetric signature operation. The flag must be present on keys used with the following APIs:
219226

220227
* `psa_sign_hash()`
221228

@@ -229,7 +236,7 @@ The usage flags are encoded in a bitmask, which has the type `psa_key_usage_t`.
229236
.. summary::
230237
Permission to verify a message hash with the key.
231238

232-
This flag is required to use the key to verify a pre-computed message hash in an asymmetric signature verification operation. The flag must be present on keys used with the following APIs:
239+
This cryptographic-operation usage flag is required to use the key to verify a pre-computed message hash in an asymmetric signature verification operation. The flag must be present on keys used with the following APIs:
233240

234241
* `psa_verify_hash()`
235242

@@ -243,7 +250,7 @@ The usage flags are encoded in a bitmask, which has the type `psa_key_usage_t`.
243250
.. summary::
244251
Permission to derive other keys or produce a password hash from this key.
245252

246-
This flag is required to use the key for derivation in a key-derivation operation, or in a key-agreement operation.
253+
This cryptographic-operation usage flag is required to use the key for derivation in a key-derivation operation, or in a key-agreement operation.
247254

248255
This flag must be present on keys used with the following APIs:
249256

@@ -261,12 +268,32 @@ The usage flags are encoded in a bitmask, which has the type `psa_key_usage_t`.
261268

262269
.. versionadded:: 1.1
263270

264-
This flag is required to use the key for verification in a key-derivation operation.
271+
This cryptographic-operation usage flag is required to use the key for verification in a key-derivation operation.
265272

266273
This flag must be present on keys used with `psa_key_derivation_verify_key()`.
267274

268275
If this flag is present on all keys used in calls to `psa_key_derivation_input_key()` for a key-derivation operation, then it permits calling `psa_key_derivation_verify_bytes()` or `psa_key_derivation_verify_key()` at the end of the operation.
269276

277+
.. macro:: PSA_KEY_USAGE_DERIVE_PUBLIC
278+
:definition: ((psa_key_usage_t)0x00000080)
279+
280+
.. summary::
281+
Used in the `psa_check_key_usage()` function to determine if the key can be used in the public key role in a key-agreement or a PAKE operation.
282+
283+
.. versionadded:: 1.4
284+
285+
This cryptographic-operation usage flag is only used with the `psa_check_key_usage()` function.
286+
This flag is not currently checked when performing any cryptographic operation.
287+
288+
For example, calling `psa_check_key_usage()` with `PSA_KEY_USAGE_DERIVE_PUBLIC` and with:
289+
290+
* `PSA_ALG_ECDH` checks that the key can be used as the public share in the ECDH key agreement.
291+
There are no checks on permissions as the key share is provided in a buffer.
292+
* `PSA_ALG_SPAKE2P_HMAC` will check that the key can be used in the Verifier role in the SPAKE2+ algorithm.
293+
The key must have the `PSA_KEY_USAGE_DERIVE` permission.
294+
* `PSA_ALG_HKDF` is invalid, as there is no such role in single-key derivation algorithms.
295+
296+
270297
.. function:: psa_set_key_usage_flags
271298

272299
.. summary::
@@ -302,3 +329,80 @@ The usage flags are encoded in a bitmask, which has the type `psa_key_usage_t`.
302329
.. admonition:: Implementation note
303330

304331
This is a simple accessor function that is not required to validate its inputs. It can be efficiently implemented as a ``static inline`` function or a function-like-macro.
332+
333+
.. function:: psa_check_key_usage
334+
335+
.. summary::
336+
Query the capability of a key.
337+
338+
.. versionadded:: 1.4
339+
340+
.. param:: psa_key_id_t key
341+
Identifier of the key to check.
342+
.. param:: psa_algorithm_t alg
343+
An algorithm identifier: a value of type `psa_algorithm_t`.
344+
.. param:: psa_key_usage_t usage
345+
A single ``PSA_KEY_USAGE_xxx`` flag.
346+
347+
.. return:: psa_status_t
348+
349+
.. retval:: PSA_SUCCESS
350+
``key`` can be used for the requested operation on this implementation.
351+
.. retval:: PSA_ERROR_INVALID_ARGUMENT
352+
The following conditions can result in this error:
353+
354+
* ``usage`` is a key-management usage flag and ``alg`` is not `PSA_ALG_NONE`.
355+
* ``usage`` is a cryptographic-operation usage flag and ``alg`` is not a valid, specific algorithm.
356+
A 'specific algorithm' is one that is neither `PSA_ALG_NONE` nor a wildcard algorithm.
357+
* ``usage`` is not a valid role for algorithm ``alg``.
358+
* ``key`` is not compatible with ``alg`` and ``usage``.
359+
.. retval: PSA_ERROR_INVALID_HANDLE:
360+
``key`` is not a valid key identifier.
361+
.. retval: PSA_ERROR_NOT_PERMITTED
362+
``key`` does not permit the requested usage or algorithm.
363+
.. retval:: PSA_ERROR_NOT_SUPPORTED
364+
The following conditions can result in this error:
365+
366+
* The implementation does not support algorithm ``alg``.
367+
* The implementation does not support using ``key`` with the operation associated with ``alg`` and ``usage``.
368+
.. retval:: PSA_ERROR_INSUFFICIENT_MEMORY
369+
.. retval:: PSA_ERROR_COMMUNICATION_FAILURE
370+
.. retval:: PSA_ERROR_CORRUPTION_DETECTED
371+
.. retval:: PSA_ERROR_STORAGE_FAILURE
372+
.. retval:: PSA_ERROR_DATA_CORRUPT
373+
.. retval:: PSA_ERROR_DATA_INVALID
374+
.. retval:: PSA_ERROR_BAD_STATE
375+
The library requires initializing by a call to `psa_crypto_init()`.
376+
377+
This function reports whether the implementation supports the use of a key with the operation associated with a provided algorithm and usage.
378+
This function does not attempt to perform the operation.
379+
380+
If ``usage`` is a key-management usage flag, then:
381+
382+
* ``alg`` must be `PSA_ALG_NONE`.
383+
* ``key`` must exist, and permit the requested usage flag.
384+
385+
If ``usage`` is a cryptographic-operation usage flag, then:
386+
387+
* ``alg`` must be a valid, fully specified algorithm, and not a wildcard.
388+
For example:
389+
390+
- :code:`PSA_ALG_ECDSA(PSA_ALG_ANY_HASH)` is invalid as it is a wildcard algorithm.
391+
- :code:`PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 9)` is invalid as it has an invalid tag-length for GCM.
392+
- :code:`PSA_ALG_SPAKE2P_HMAC(PSA_ALG_SHA_1)` is invalid as SPAKE2+ does have SHA-1 in any cipher-suite.
393+
* ``usage`` must identify a valid role within the algorithm.
394+
For example, if :code:`alg == PSA_ALG_GCM`, the ``usage`` must be either `PSA_KEY_USAGE_ENCRYPT` or `PSA_KEY_USAGE_DECRYPT`, as these are the key-usage policy flags for AEAD functions.
395+
* ``key`` must exist, have a type and size that are compatible with the operation associated with ``alg`` and ``usage``, and have the required permission for the algorithm and usage.
396+
For example:
397+
398+
- An Edwards25519 key pair is not compatible with :code:`PSA_ALG_ECDSA(PSA_ALG_SHA_256)`.
399+
- A ``512``-bit RSA key pair is not compatible with :code:`PSA_ALG_RSA_OAEP(PSA_ALG_SHA_512)` as the algorithm requires a larger key size.
400+
- A ``512``-bit AES key (double-length key for use in AES-256-XTS) is not compatible with `PSA_ALG_CTR`.
401+
402+
.. note::
403+
For the key pair or public key of a valid type in a key agreement function, this function returns :code:`PSA_SUCCESS` for the usage `PSA_KEY_USAGE_DERIVE_PUBLIC`, regardless of the key's policy.
404+
This is because the corresponding API functions take a key buffer as input, not a key object, and the key data can extracted by calling `psa_export_public_key()`, which does not require any usage flag.
405+
406+
.. admonition:: Implementation note
407+
408+
The intended behavior of this function is to include any check that can be made using the accessible key attributes, but without requiring logic or arithmetic using the key material.

doc/crypto/appendix/history.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Changes to the API
1818
~~~~~~~~~~~~~~~~~~
1919

2020
* Added `psa_attach_key()` to register existing key material as a volatile key within the implementation.
21+
* Added `psa_check_key_usage()` to query a key's capabilities.
2122

2223
Clarifications and fixes
2324
~~~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)