Skip to content

Commit dae83ef

Browse files
committed
Add XChaCha20 keys and algorithms
1 parent 7588b29 commit dae83ef

7 files changed

Lines changed: 76 additions & 5 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ typedef /* implementation-defined type */ psa_mac_operation_t;
155155
#define PSA_ALG_TLS12_PSK_TO_MS(hash_alg) /* specification-defined value */
156156
#define PSA_ALG_TRUNCATED_MAC(mac_alg, mac_length) \
157157
/* specification-defined value */
158+
#define PSA_ALG_XCHACHA20_POLY1305 ((psa_algorithm_t)0x05100600)
158159
#define PSA_ALG_XTS ((psa_algorithm_t)0x0440ff00)
159160
#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE \
160161
/* implementation-defined value */
@@ -287,6 +288,7 @@ typedef /* implementation-defined type */ psa_mac_operation_t;
287288
#define PSA_KEY_TYPE_RSA_KEY_PAIR ((psa_key_type_t)0x7001)
288289
#define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x4001)
289290
#define PSA_KEY_TYPE_SM4 ((psa_key_type_t)0x2405)
291+
#define PSA_KEY_TYPE_XCHACHA20 ((psa_key_type_t)0x2007)
290292
#define PSA_KEY_USAGE_CACHE ((psa_key_usage_t)0x00000004)
291293
#define PSA_KEY_USAGE_COPY ((psa_key_usage_t)0x00000002)
292294
#define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200)

doc/crypto/api/keys/types.rst

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.. SPDX-FileCopyrightText: Copyright 2018-2022 Arm Limited and/or its affiliates <open-source-office@arm.com>
1+
.. SPDX-FileCopyrightText: Copyright 2018-2023 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
@@ -401,6 +401,23 @@ Symmetric keys
401401
| `PSA_ALG_STREAM_CIPHER`
402402
| `PSA_ALG_CHACHA20_POLY1305`
403403
404+
.. macro:: PSA_KEY_TYPE_XCHACHA20
405+
:definition: ((psa_key_type_t)0x2007)
406+
407+
.. summary::
408+
Key for the XChaCha20 stream cipher or the XChaCha20-Poly1305 AEAD algorithm.
409+
410+
The XChaCha20 key size is 256 bits (32 bytes).
411+
412+
* Use algorithm `PSA_ALG_STREAM_CIPHER` to use this key with the XChaCha20 cipher for unauthenticated encryption. See `PSA_ALG_STREAM_CIPHER` for details of this algorithm.
413+
414+
* Use algorithm `PSA_ALG_XCHACHA20_POLY1305` to use this key with the XChaCha20 cipher and Poly1305 authenticator for AEAD. See `PSA_ALG_XCHACHA20_POLY1305` for details of this algorithm.
415+
416+
.. subsection:: Compatible algorithms
417+
418+
| `PSA_ALG_STREAM_CIPHER`
419+
| `PSA_ALG_XCHACHA20_POLY1305`
420+
404421

405422
.. _asymmetric-keys:
406423

doc/crypto/api/ops/aead.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,24 @@ AEAD algorithms
121121

122122
| `PSA_KEY_TYPE_CHACHA20`
123123
124+
.. macro:: PSA_ALG_XCHACHA20_POLY1305
125+
:definition: ((psa_algorithm_t)0x05100600)
126+
127+
.. summary::
128+
The XChaCha20-Poly1305 AEAD algorithm.
129+
130+
XChaCha20-Poly1305 is a variation of the ChaCha20-Poly1305 AEAD algorithm, but uses a 192-bit nonce. The larger nonce provides much lower probability of nonce misuse.
131+
132+
XChaCha20-Poly1305 requires a 24-byte nonce.
133+
134+
Implementations must support 16-byte tags. It is recommended that truncated tag sizes are rejected.
135+
136+
XChaCha20-Poly1305 is defined in :cite-title:`XCHACHA`.
137+
138+
.. subsection:: Compatible key types
139+
140+
| `PSA_KEY_TYPE_XCHACHA20`
141+
124142
.. macro:: PSA_ALG_AEAD_WITH_SHORTENED_TAG
125143
:definition: /* specification-defined value */
126144

doc/crypto/api/ops/ciphers.rst

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Cipher algorithms
4444
.. summary::
4545
The stream cipher mode of a stream cipher algorithm.
4646

47-
The underlying stream cipher is determined by the key type. The ARC4 and ChaCha20 ciphers use this algorithm identifier.
47+
The underlying stream cipher is determined by the key type. The ARC4, ChaCha20, and XChaCha20 ciphers use this algorithm identifier.
4848

4949
.. subsection:: ARC4
5050

@@ -72,14 +72,38 @@ Cipher algorithms
7272
* A call to `psa_cipher_set_iv()` on a multi-part cipher operation can support the following IV sizes:
7373

7474
- 12 bytes: the provided IV is used as the nonce, and the counter value is set to zero.
75-
- 16 bytes: the first four bytes of the IV are used as the counter value (encoded as little-endian), and the remaining 12 bytes is used as the nonce.
75+
- 16 bytes: the first four bytes of the IV are used as the counter value (encoded as little-endian), and the remaining 12 bytes are used as the nonce.
7676
- 8 bytes: the cipher operation uses the original :cite:`CHACHA20` definition of ChaCha20: the provided IV is used as the 64-bit nonce, and the 64-bit counter value is set to zero.
7777
- It is recommended that implementations do not support other sizes of IV.
7878

79+
.. subsection:: XChaCha20
80+
81+
To use XChaCha20, use a key type of `PSA_KEY_TYPE_XCHACHA20` and algorithm id `PSA_ALG_STREAM_CIPHER`.
82+
83+
XChaCha20 is a variation of ChaCha20 that uses a 192-bit nonce and a 64-bit counter. The larger nonce provides much lower probability of nonce misuse.
84+
85+
When using an XChaCha20 key with the `PSA_ALG_STREAM_CIPHER` algorithm, the nonce and an initial counter values are provided using the initialization vector (IV) functions in the following ways:
86+
87+
* A call to `psa_cipher_encrypt()` will generate a random 24-byte nonce, and set the counter value to zero. The random nonce is output as a 24-byte IV value in the output.
88+
89+
* A call to `psa_cipher_decrypt()` will use first 24 bytes of the input buffer as the nonce and set the counter value to zero.
90+
91+
* A call to `psa_cipher_generate_iv()` on a multi-part cipher operation will generate and return a random 24-byte nonce and set the counter value to zero.
92+
93+
* A call to `psa_cipher_set_iv()` on a multi-part cipher operation can support the following IV sizes:
94+
95+
- 24 bytes: the provided IV is used as the nonce, and the counter value is set to zero.
96+
- 32 bytes: the first 8 bytes of the IV are used as the counter value (encoded as little-endian), and the remaining 24 bytes are used as the nonce.
97+
98+
Other sizes of IV are invalid.
99+
100+
XChaCha20 is defined in :cite-title:`XCHACHA`.
101+
79102
.. subsection:: Compatible key types
80103

81104
| `PSA_KEY_TYPE_ARC4`
82105
| `PSA_KEY_TYPE_CHACHA20`
106+
| `PSA_KEY_TYPE_XCHACHA20`
83107
84108
.. macro:: PSA_ALG_CTR
85109
:definition: ((psa_algorithm_t)0x04c01000)

doc/crypto/appendix/encodings.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.. SPDX-FileCopyrightText: Copyright 2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
1+
.. SPDX-FileCopyrightText: Copyright 2022-2023 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
.. _appendix-encodings:
@@ -236,7 +236,8 @@ W is a flag to indicate a wildcard permitted-algorithm policy:
236236
AEAD algorithm, B, AEAD-TYPE, Algorithm identifier, Algorithm value
237237
CCM :sup:`a`, 1, ``0x01``, `PSA_ALG_CCM`, ``0x05500100`` :sup:`b`
238238
GCM :sup:`a`, 1, ``0x02``, `PSA_ALG_GCM`, ``0x05500200`` :sup:`b`
239-
ChaCha20-poly1305, 0, ``0x05``, `PSA_ALG_CHACHA20_POLY1305`, ``0x05100500`` :sup:`b`
239+
ChaCha20-Poly1305, 0, ``0x05``, `PSA_ALG_CHACHA20_POLY1305`, ``0x05100500`` :sup:`b`
240+
XChaCha20-Poly1305, 0, ``0x06``, `PSA_ALG_XCHACHA20_POLY1305`, ``0x05100600`` :sup:`b`
240241

241242
a. This is an AEAD mode of an underlying block cipher. The block cipher is determined by the key type that is provided to the AEAD operation.
242243

@@ -499,6 +500,7 @@ The defined values for BLK, SYM-TYPE and P are shown in :numref:`table-symmetric
499500
Symmetric key type, BLK, SYM-TYPE, P, Key type, Key type value
500501
ARC4, 0, 1, 0, `PSA_KEY_TYPE_ARC4`, ``0x2002``
501502
ChaCha20, 0, 2, 0, `PSA_KEY_TYPE_CHACHA20`, ``0x2004``
503+
XChaCha20, 0, 3, 1, `PSA_KEY_TYPE_XCHACHA20`, ``0x2007``
502504
DES, 3, 0, 1, `PSA_KEY_TYPE_DES`, ``0x2301``
503505
AES, 4, 0, 0, `PSA_KEY_TYPE_AES`, ``0x2400``
504506
CAMELLIA, 4, 1, 1, `PSA_KEY_TYPE_CAMELLIA`, ``0x2403``

doc/crypto/appendix/history.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Changes to the API
2424

2525
* Added `psa_key_agreement()` for standalone key agreement that outputs to a new key object. Also added `PSA_ALG_IS_STANDALONE_KEY_AGREEMENT()` as a synonym for `PSA_ALG_IS_RAW_KEY_AGREEMENT()`.
2626

27+
* Added support for the XChaCha20 cipher and XChaCha20-Poly1305 AEAD algorithms. See `PSA_KEY_TYPE_XCHACHA20` and `PSA_ALG_XCHACHA20_POLY1305`.
28+
2729
Clarifications and fixes
2830
~~~~~~~~~~~~~~~~~~~~~~~~
2931

doc/crypto/references

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,12 @@
336336
:url: http://cr.yp.to/chacha/chacha-20080128.pdf
337337
:publication: January 2008
338338

339+
.. reference:: XCHACHA
340+
:title: XChaCha: eXtended-nonce ChaCha and AEAD_XChaCha20_Poly1305
341+
:author: Arciszewski
342+
:publication: January 2020
343+
:url: datatracker.ietf.org/doc/html/draft-irtf-cfrg-xchacha-03
344+
339345
.. reference:: CLULOW
340346
:author: Clulow, Jolyon
341347
:title: On the Security of PKCS #11

0 commit comments

Comments
 (0)