Skip to content

Commit ad57e51

Browse files
authored
Merge pull request #222 from athoelke/crypto-encapsulation-ecies
Encapsulation and ECIES (v2)
2 parents 3b63e4b + e99d9e0 commit ad57e51

22 files changed

Lines changed: 520 additions & 15 deletions

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ typedef struct psa_custom_key_parameters_t {
7676
#define PSA_ALG_ECDH ((psa_algorithm_t)0x09020000)
7777
#define PSA_ALG_ECDSA(hash_alg) /* specification-defined value */
7878
#define PSA_ALG_ECDSA_ANY ((psa_algorithm_t) 0x06000600)
79+
#define PSA_ALG_ECIES_SEC1 ((psa_algorithm_t)0x0c000100)
7980
#define PSA_ALG_ED25519PH ((psa_algorithm_t) 0x0600090B)
8081
#define PSA_ALG_ED448PH ((psa_algorithm_t) 0x06000915)
8182
#define PSA_ALG_FFDH ((psa_algorithm_t)0x09010000)
@@ -107,6 +108,7 @@ typedef struct psa_custom_key_parameters_t {
107108
#define PSA_ALG_IS_KEY_DERIVATION(alg) /* specification-defined value */
108109
#define PSA_ALG_IS_KEY_DERIVATION_STRETCHING(alg) \
109110
/* specification-defined value */
111+
#define PSA_ALG_IS_KEY_ENCAPSULATION(alg) /* specification-defined value */
110112
#define PSA_ALG_IS_MAC(alg) /* specification-defined value */
111113
#define PSA_ALG_IS_PAKE(alg) /* specification-defined value */
112114
#define PSA_ALG_IS_PBKDF2_HMAC(alg) /* specification-defined value */
@@ -221,6 +223,9 @@ typedef struct psa_custom_key_parameters_t {
221223
#define PSA_ECC_FAMILY_SECT_R1 ((psa_ecc_family_t) 0x22)
222224
#define PSA_ECC_FAMILY_SECT_R2 ((psa_ecc_family_t) 0x2b)
223225
#define PSA_ECC_FAMILY_TWISTED_EDWARDS ((psa_ecc_family_t) 0x42)
226+
#define PSA_ENCAPSULATE_CIPHERTEXT_MAX_SIZE /* implementation-defined value */
227+
#define PSA_ENCAPSULATE_CIPHERTEXT_SIZE(key_type, key_bits, alg) \
228+
/* implementation-defined value */
224229
#define PSA_ERROR_INSUFFICIENT_ENTROPY ((psa_status_t)-148)
225230
#define PSA_ERROR_INVALID_PADDING ((psa_status_t)-150)
226231
#define PSA_EXPORT_ASYMMETRIC_KEY_MAX_SIZE /* implementation-defined value */
@@ -498,7 +503,20 @@ psa_status_t psa_copy_key(psa_key_id_t source_key,
498503
const psa_key_attributes_t * attributes,
499504
psa_key_id_t * target_key);
500505
psa_status_t psa_crypto_init(void);
506+
psa_status_t psa_decapsulate(psa_key_id_t key,
507+
psa_algorithm_t alg,
508+
const uint8_t * ciphertext,
509+
size_t ciphertext_length,
510+
const psa_key_attributes_t * attributes,
511+
psa_key_id_t * output_key);
501512
psa_status_t psa_destroy_key(psa_key_id_t key);
513+
psa_status_t psa_encapsulate(psa_key_id_t key,
514+
psa_algorithm_t alg,
515+
const psa_key_attributes_t * attributes,
516+
psa_key_id_t * output_key,
517+
uint8_t * ciphertext,
518+
size_t ciphertext_size,
519+
size_t * ciphertext_length);
502520
psa_status_t psa_export_key(psa_key_id_t key,
503521
uint8_t * data,
504522
size_t data_size,

doc/crypto/api/keys/attributes.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ Managing key attributes
112112
#. Set the key policy with `psa_set_key_usage_flags()` and `psa_set_key_algorithm()`.
113113
#. Set the key type with `psa_set_key_type()`. Skip this step if copying an existing key with `psa_copy_key()`.
114114
#. When generating a random key with `psa_generate_key()` or `psa_generate_key_custom()`, or deriving a key with `psa_key_derivation_output_key()` or `psa_key_derivation_output_key_custom()`, set the desired key size with `psa_set_key_bits()`.
115-
#. Call a key creation function: `psa_import_key()`, `psa_generate_key()`, `psa_generate_key_custom()`, `psa_key_derivation_output_key()`, `psa_key_derivation_output_key_custom()`, `psa_key_agreement()`, `psa_pake_get_shared_key()`, or `psa_copy_key()`. This function reads the attribute object, creates a key with these attributes, and outputs an identifier for the newly created key.
115+
#. Call a key creation function: `psa_import_key()`, `psa_generate_key()`, `psa_generate_key_custom()`, `psa_key_derivation_output_key()`, `psa_key_derivation_output_key_custom()`, `psa_key_agreement()`, `psa_encapsulate()`, `psa_decapsulate()`, `psa_pake_get_shared_key()`, or `psa_copy_key()`. This function reads the attribute object, creates a key with these attributes, and outputs an identifier for the newly created key.
116116
#. Optionally call `psa_reset_key_attributes()`, now that the attribute object is no longer needed. Currently this call is not required as the attributes defined in this specification do not require additional resources beyond the object itself.
117117

118118
A typical sequence to query a key's attributes is as follows:

doc/crypto/api/keys/ids.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ Attribute accessors
9292

9393
If the attribute object currently declares the key as volatile, which is the default lifetime of an attribute object, this function sets the lifetime attribute to `PSA_KEY_LIFETIME_PERSISTENT`.
9494

95-
This function does not access storage, it merely stores the given value in the attribute object. The persistent key will be written to storage when the attribute object is passed to a key creation function such as `psa_import_key()`, `psa_generate_key()`, `psa_generate_key_custom()`, `psa_key_derivation_output_key()`, `psa_key_derivation_output_key_custom()`, `psa_key_agreement()`, `psa_pake_get_shared_key()`, or `psa_copy_key()`.
95+
This function does not access storage, it merely stores the given value in the attribute object. The persistent key will be written to storage when the attribute object is passed to a key creation function such as `psa_import_key()`, `psa_generate_key()`, `psa_generate_key_custom()`, `psa_key_derivation_output_key()`, `psa_key_derivation_output_key_custom()`, `psa_key_agreement()`, `psa_encapsulate()`, `psa_decapsulate()`, `psa_pake_get_shared_key()`, or `psa_copy_key()`.
9696

9797
.. admonition:: Implementation note
9898

doc/crypto/api/keys/lifetimes.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ Attribute accessors
273273

274274
To make a key persistent, give it a persistent key identifier by using `psa_set_key_id()`. By default, a key that has a persistent identifier is stored in the default storage area identifier by `PSA_KEY_LIFETIME_PERSISTENT`. Call this function to choose a storage area, or to explicitly declare the key as volatile.
275275

276-
This function does not access storage, it merely stores the given value in the attribute object. The persistent key will be written to storage when the attribute object is passed to a key creation function such as `psa_import_key()`, `psa_generate_key()`, `psa_generate_key_custom()`, `psa_key_derivation_output_key()`, `psa_key_derivation_output_key_custom()`, `psa_key_agreement()`, `psa_pake_get_shared_key()`, or `psa_copy_key()`.
276+
This function does not access storage, it merely stores the given value in the attribute object. The persistent key will be written to storage when the attribute object is passed to a key creation function such as `psa_import_key()`, `psa_generate_key()`, `psa_generate_key_custom()`, `psa_key_derivation_output_key()`, `psa_key_derivation_output_key_custom()`, `psa_key_agreement()`, `psa_encapsulate()`, `psa_decapsulate()`, `psa_pake_get_shared_key()`, or `psa_copy_key()`.
277277

278278
.. admonition:: Implementation note
279279

doc/crypto/api/keys/management.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ New keys can be created in the following ways:
1818
* `psa_generate_key()` and `psa_generate_key_custom()` create a key from randomly generated data.
1919
* `psa_key_derivation_output_key()` and `psa_key_derivation_output_key_custom()` create a key from data generated by a pseudorandom derivation process. See :secref:`kdf`.
2020
* `psa_key_agreement()` creates a key from the shared secret result of a key agreement process. See :secref:`key-agreement`.
21+
* `psa_encapsulate()` and `psa_decapsulate()` create a shared secret key using a key-encapsulation mechanism.
2122
* `psa_pake_get_shared_key()` creates a key from the shared secret result of a password-authenticated key exchange. See :secref:`pake`.
2223
* `psa_copy_key()` duplicates an existing key with a different lifetime or with a more restrictive usage policy.
2324

doc/crypto/api/keys/policy.rst

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.. SPDX-FileCopyrightText: Copyright 2018-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
1+
.. SPDX-FileCopyrightText: Copyright 2018-2024 Arm Limited and/or its affiliates <open-source-office@arm.com>
22
.. SPDX-License-Identifier: CC-BY-SA-4.0 AND LicenseRef-Patent-license
33
44
.. header:: psa/crypto
@@ -86,7 +86,16 @@ The usage flags are encoded in a bitmask, which has the type `psa_key_usage_t`.
8686
* 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.
8787
* 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.
8888
* 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 other usage flags, for example, `PSA_KEY_USAGE_ENCRYPT` and `PSA_KEY_USAGE_SIGN_MESSAGE`, determine whether the corresponding operation is permitted on the key.
89+
* The following usage flags determine whether the corresponding operations are permitted with the key:
90+
91+
- `PSA_KEY_USAGE_ENCRYPT`
92+
- `PSA_KEY_USAGE_DECRYPT`
93+
- `PSA_KEY_USAGE_SIGN_MESSAGE`
94+
- `PSA_KEY_USAGE_VERIFY_MESSAGE`
95+
- `PSA_KEY_USAGE_SIGN_HASH`
96+
- `PSA_KEY_USAGE_VERIFY_HASH`
97+
- `PSA_KEY_USAGE_DERIVE`
98+
- `PSA_KEY_USAGE_VERIFY_DERIVATION`
9099

91100
.. typedef:: uint32_t psa_key_usage_t
92101

@@ -142,31 +151,33 @@ The usage flags are encoded in a bitmask, which has the type `psa_key_usage_t`.
142151
:definition: ((psa_key_usage_t)0x00000100)
143152

144153
.. summary::
145-
Permission to encrypt a message with the key.
154+
Permission to encrypt a message, or perform key encapsulation, with the key.
146155

147-
This flag is required to use the key in a symmetric encryption operation, in an AEAD encryption-and-authentication operation, or in an asymmetric encryption operation. The flag must be present on keys used with the following APIs:
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:
148157

149158
* `psa_cipher_encrypt()`
150159
* `psa_cipher_encrypt_setup()`
151160
* `psa_aead_encrypt()`
152161
* `psa_aead_encrypt_setup()`
153162
* `psa_asymmetric_encrypt()`
163+
* `psa_encapsulate()`
154164

155165
For a key pair, this concerns the public key.
156166

157167
.. macro:: PSA_KEY_USAGE_DECRYPT
158168
:definition: ((psa_key_usage_t)0x00000200)
159169

160170
.. summary::
161-
Permission to decrypt a message with the key.
171+
Permission to decrypt a message, or perform key decapsulation, with the key.
162172

163-
This flag is required to use the key in a symmetric decryption operation, in an AEAD decryption-and-verification operation, or in an asymmetric decryption operation. The flag must be present on keys used with the following APIs:
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:
164174

165175
* `psa_cipher_decrypt()`
166176
* `psa_cipher_decrypt_setup()`
167177
* `psa_aead_decrypt()`
168178
* `psa_aead_decrypt_setup()`
169179
* `psa_asymmetric_decrypt()`
180+
* `psa_decapsulate()`
170181

171182
For a key pair, this concerns the private key.
172183

doc/crypto/api/keys/types.rst

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ The curve type affects the key format, the key derivation procedure, and the alg
797797
* - Curve type
798798
- Compatible algorithms
799799
* - Weierstrass
800-
- Weierstrass curve key-pairs can be used in asymmetric signature and key agreement algorithms.
800+
- Weierstrass curve key-pairs can be used in asymmetric signature, key agreement, and key-encapsulation algorithms.
801801

802802
`PSA_ALG_DETERMINISTIC_ECDSA`
803803

@@ -806,10 +806,16 @@ The curve type affects the key format, the key derivation procedure, and the alg
806806
`PSA_ALG_ECDSA_ANY`
807807

808808
`PSA_ALG_ECDH`
809+
810+
`PSA_ALG_ECIES_SEC1`
811+
809812
* - Montgomery
810-
- Montgomery curve key-pairs can only be used in key agreement algorithms.
813+
- Montgomery curve key-pairs can be used in key agreement and key-encapsulation algorithms.
811814

812815
`PSA_ALG_ECDH`
816+
817+
`PSA_ALG_ECIES_SEC1`
818+
813819
* - Twisted Edwards
814820
- Twisted Edwards curve key-pairs can only be used in asymmetric signature algorithms.
815821

@@ -920,16 +926,23 @@ The curve type affects the key format, the key derivation procedure, and the alg
920926
* - Curve type
921927
- Compatible algorithms
922928
* - Weierstrass
923-
- Weierstrass curve public keys can be used in asymmetric signature algorithms.
929+
- Weierstrass curve public keys can be used in asymmetric signature and key-encapsulation algorithms.
924930

925931
`PSA_ALG_DETERMINISTIC_ECDSA`
926932

927933
`PSA_ALG_ECDSA`
928934

929935
`PSA_ALG_ECDSA_ANY`
930936

937+
`PSA_ALG_ECIES_SEC1`
938+
939+
* - Montgomery
940+
- Montgomery curve public keys can only be used in key-encapsulation algorithms.
941+
942+
`PSA_ALG_ECIES_SEC1`
943+
931944
* - Twisted Edwards
932-
- Twisted Edwards curve public key can only be used in asymmetric signature algorithms.
945+
- Twisted Edwards curve public keys can only be used in asymmetric signature algorithms.
933946

934947
`PSA_ALG_PURE_EDDSA`
935948

doc/crypto/api/ops/algorithms.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ The specific algorithm identifiers are described alongside the cryptographic ope
2323
* :secref:`sign`
2424
* :secref:`asymmetric-encryption-algorithms`
2525
* :secref:`key-agreement-algorithms`
26+
* :secref:`key-encapsulation-algorithms`
2627
* :secref:`pake`
2728

2829

@@ -193,6 +194,20 @@ Algorithm categories
193194
``1`` if ``alg`` is a password-authenticated key exchange (PAKE) algorithm, ``0`` otherwise.
194195
This macro can return either ``0`` or ``1`` if ``alg`` is not a supported algorithm identifier.
195196

197+
.. macro:: PSA_ALG_IS_KEY_ENCAPSULATION
198+
:definition: /* specification-defined value */
199+
200+
.. summary::
201+
Whether the specified algorithm is a key-encapsulation algorithm.
202+
203+
.. param:: alg
204+
An algorithm identifier: a value of type `psa_algorithm_t`.
205+
206+
.. return::
207+
``1`` if ``alg`` is a key-encapsulation algorithm, ``0`` otherwise. This macro can return either ``0`` or ``1`` if ``alg`` is not a supported algorithm identifier.
208+
209+
See :secref:`key-encapsulation-algorithms` for a list of defined key-encapsulation algorithms.
210+
196211
Support macros
197212
--------------
198213

doc/crypto/api/ops/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ Cryptographic operation reference
1818
signature
1919
pk-encryption
2020
key-agreement
21+
key-encapsulation
2122
pake
2223
rng

0 commit comments

Comments
 (0)