1 /*
2  * Copyright (c) 2021-2023, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 /** \file cc3xx_crypto_primitives_private.h
9  *
10  * This file contains the definition of public types associated
11  * to the CC3XX driver. It's meant to be included only by those
12  * modules which require access to the private implementation
13  * of the CC3XX types
14  *
15  */
16 
17 #ifndef CC3XX_CRYPTO_PRIMITIVES_PRIVATE_H
18 #define CC3XX_CRYPTO_PRIMITIVES_PRIVATE_H
19 
20 /* Include the public header first as it contains the typedefs */
21 #include "cc3xx_crypto_primitives.h"
22 #include "cc3xx_psa_api_config.h"
23 #include "psa/crypto.h"
24 
25 #include "hash_driver.h"
26 #include "aes_driver.h"
27 #include "aesgcm_driver.h"
28 #include "aesccm_driver.h"
29 #include "chacha_driver.h"
30 
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 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 /*!
43  * \struct cc3xx_hash_operation_s
44  *
45  * \brief A structure holding state information for an Hash operation
46  */
47 struct cc3xx_hash_operation_s {
48     HashContext_t ctx; /*!< Low-level hash context */
49 };
50 
51 /*!
52  * \struct cc3xx_cipher_operation_s
53  *
54  * \brief A structure holding state information for an Cipher operation
55  */
56 struct cc3xx_cipher_operation_s {
57     psa_algorithm_t alg;          /*!< Cipher algorithm */
58     psa_key_type_t key_type;      /*!< Key type */
59     psa_encrypt_or_decrypt_t dir; /*!< Encrypt/decrypt direction */
60     size_t block_size;            /*!< Block size of the cipher */
61 
62     /*! Function that adds padding for padding-enabled cipher modes */
63     psa_status_t(*add_padding)(uint8_t *, size_t, size_t);
64     /*! Function that processes the padding when padding modes are used */
65     psa_status_t(*get_padding)(const uint8_t *, size_t, size_t *);
66 
67     uint8_t unprocessed_data[AES_BLOCK_SIZE]; /*!< Cached data */
68     size_t  unprocessed_size;                 /*!< Size of the cached data */
69     uint8_t iv[AES_IV_SIZE];                  /*!< Initialisation Vector */
70     size_t  iv_size;                          /*!< Size of the IV */
71 
72     union {
73         AesContext_t    aes;     /*!< Low-level AES context */
74         ChachaContext_t chacha;  /*!< Low-level Chacha context */
75     } ctx;
76 };
77 
78 /*!
79  * \struct cc3xx_mac_operation_s
80  *
81  * \brief A structure holding state information for a MAC operation
82  */
83 struct cc3xx_mac_operation_s {
84     psa_algorithm_t alg; /*!< MAC algorithm used in this context */
85     union {
86         cc3xx_cipher_operation_t cmac; /*!< Underlying cipher op for CMAC */
87         cc3xx_hash_operation_t hmac;   /*!< Underlying hash op for HMAC */
88     };
89     /* Only for HMAC */
90     uint8_t opad[PSA_HMAC_MAX_HASH_BLOCK_SIZE]; /*!< Opad as RFC-2104 */
91 };
92 
93 /*!
94  * \struct cc3xx_aead_operation_s
95  *
96  * \brief A structure holding state information for an AEAD operation
97  */
98 struct cc3xx_aead_operation_s {
99     psa_algorithm_t alg;          /*!< AEAD algorithm */
100     psa_key_type_t key_type;      /*!< Key type */
101     psa_encrypt_or_decrypt_t dir; /*!< Encrypt/decrypt direction */
102     size_t tag_length;            /*!< Size of the authentication tag */
103 
104     union {
105         AesGcmContext_t gcm;            /*!< Low-level GCM context */
106         AesCcmContext_t ccm;            /*!< Low-level CCM context */
107         ChachaPolyContext_t chachapoly; /*!< Low-level Chacha20-Poly1305 ctx */
108     } ctx;
109 
110 #if defined(CC3XX_CONFIG_ENABLE_AEAD_AES_CACHED_MODE)
111     uint8_t cache_buf[AES_BLOCK_SIZE]; /*!< Required to support cached mode */
112     size_t curr_cache_size;            /*!< Size of data currently cached */
113 #endif /* CC3XX_CONFIG_ENABLE_AEAD_AES_CACHED_MODE */
114 };
115 
116 #ifdef __cplusplus
117 }
118 #endif
119 
120 #endif /* CC3XX_CRYPTO_PRIMITIVES_PRIVATE_H */
121