Skip to content

Commit b5dcc91

Browse files
committed
CC3XX: Define a Chacha20-Poly1305 state context
The Chacha20-Poly1305 algorithm implementation for multipart needs to have an associated context to keep track of the Poly1305 state across multipart API calls to both update() and update_ad(). Thus far single part and Chacha20 multipart were using the existing ChachaContext_t but that needs to be extended for Chacha20-Poly1305. Signed-off-by: Antonio de Angelis <[email protected]> Change-Id: I37d71da465a81754a57d4637f87cc849dd81c33f
1 parent 1728f1c commit b5dcc91

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

lib/ext/cryptocell-312-runtime/codesafe/src/psa_driver_api/include/cc3xx_crypto_primitives_private.h

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, Arm Limited. All rights reserved.
2+
* Copyright (c) 2021-2022, Arm Limited. All rights reserved.
33
*
44
* SPDX-License-Identifier: BSD-3-Clause
55
*
@@ -28,6 +28,13 @@
2828
#include "aesccm_driver.h"
2929
#include "chacha_driver.h"
3030

31+
/* Include the internal layer defines for Chacha20-Poly1305 because it is there
32+
* that the Chacha20-Poly1305 context is defined. This is due to the fact that
33+
* the low-level driver contexts don't support Chacha20-Poly1305 as a combined
34+
* operation with a requirement for state support (i.e. to support multipart)
35+
*/
36+
#include "cc3xx_internal_chacha20_poly1305.h"
37+
3138
#ifdef __cplusplus
3239
extern "C" {
3340
#endif
@@ -96,9 +103,9 @@ struct cc3xx_aead_operation_s {
96103
size_t tag_length; /*!< Size of the authentication tag */
97104

98105
union {
99-
AesGcmContext_t gcm; /*!< Low-level GCM context */
100-
AesCcmContext_t ccm; /*!< Low-level CCM context */
101-
ChachaContext_t chacha; /*!< Low-level Chacha context */
106+
AesGcmContext_t gcm; /*!< Low-level GCM context */
107+
AesCcmContext_t ccm; /*!< Low-level CCM context */
108+
ChachaPolyContext_t chachapoly; /*!< Low-level Chacha20-Poly1305 ctx */
102109
} ctx;
103110
};
104111

lib/ext/cryptocell-312-runtime/codesafe/src/psa_driver_api/include/cc3xx_internal_chacha20_poly1305.h

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, Arm Limited. All rights reserved.
2+
* Copyright (c) 2021-2022, Arm Limited. All rights reserved.
33
*
44
* SPDX-License-Identifier: BSD-3-Clause
55
*
@@ -16,11 +16,31 @@
1616
#define CC3XX_INTERNAL_CHACHA20_POLY1305_H
1717

1818
#include "psa/crypto.h"
19+
#include "chacha_driver.h"
20+
#include "poly.h"
1921

2022
#ifdef __cplusplus
2123
extern "C" {
2224
#endif
2325

26+
/**
27+
* \brief A context type to implement multipart APIs on the Chacha20-Poly1305
28+
* algorithm. The low level driver does not implement it so we let the
29+
* driver interface implement it by separately including Chacha20 context
30+
* and Poly1305 state defined in the low level driver and combining them.
31+
* As this is an internal detail of the driver implementation, follow
32+
* the same guidelines of the other contexts in terms of naming.
33+
*/
34+
typedef struct ChachaPolyContext_t {
35+
ChachaContext_t chacha; /*!< Context of the underlying Chacha20 */
36+
PolyState_t poly; /*!< Context of the underlying Poly1305 */
37+
size_t ad_len; /*!< Length of the data to be authenticated */
38+
size_t plaintext_len; /*!< Length of the data to be encrypted */
39+
size_t curr_ad_len; /*!< Size of the data authenticated so far */
40+
size_t curr_plaintext_len; /*!< Size of the data encrypted so far */
41+
bool bAuthenticateInput; /*!< True when input is used for AEAD authent */
42+
} ChachaPolyContext_t;
43+
2444
/**
2545
* \brief Encrypt and create auth tag with Chacha20-Poly1305
2646
*/

lib/ext/cryptocell-312-runtime/codesafe/src/psa_driver_api/src/cc3xx_internal_chacha20_poly1305.c

+12-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@
2424
#include "cc3xx_internal_chacha20.h"
2525
#include "cc3xx_internal_chacha20_poly1305.h"
2626

27+
/* This function implements the block which generates the one time key
28+
* to be used for Poly1305 as part of RFC7539 to be generated through
29+
* Chacha20, i.e.
30+
* poly1305_key_gen(key, nonce):
31+
* counter = 0;
32+
* block = chacha_block(key, counter, nonce)
33+
* return block[0..31]
34+
*/
2735
static psa_status_t chacha20_poly1305_gen_otk(ChachaContext_t *context,
2836
uint8_t *otk,
2937
size_t otk_size)
@@ -43,9 +51,10 @@ static psa_status_t chacha20_poly1305_gen_otk(ChachaContext_t *context,
4351
return status;
4452
}
4553

46-
/* Calling chacha20_update after setting the counter to 0 and using an all-
47-
* zero input is equivalent in getting as output of the Chacha20 encryption
48-
* stage the output of the chacha20_block stage only, i.e. otk as per RFC
54+
/* Calling chacha20_update using an all-zero input is equivalent in getting
55+
* the output of the chacha_block() function only, i.e. the keystream. The
56+
* size will be a 64 byte block but we need to take only the first 32 as
57+
* output and they will represent the OTK (256 bit key (r,s))
4958
*/
5059
status = cc3xx_chacha20_update(context,
5160
chachaInState, sizeof(chachaInState),

0 commit comments

Comments
 (0)