blob: 3130396987f431e7291c0d2cdd6947901b356ae4 [file] [log] [blame]
Antonio de Angeliscf85ba22018-10-09 13:29:40 +01001/*
2 * Copyright (c) 2019, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#ifndef __CRYPTO_ENGINE_H__
9#define __CRYPTO_ENGINE_H__
10
11#ifdef __cplusplus
12extern "C" {
13#endif
14
15#include <limits.h>
16#include "psa_crypto.h"
17
18#if defined(TFM_CRYPTO_ENGINE_MBEDTLS)
19/* Pre include Mbed TLS headers */
20#define LIB_PREFIX_NAME __tfm_crypto__
21#include "mbedtls_global_symbols.h"
22
23/* Include the Mbed TLS configuration file, the way Mbed TLS does it
24 * in each of its header files. */
25#if !defined(MBEDTLS_CONFIG_FILE)
26#include "platform/ext/common/tfm_mbedtls_config.h"
27#else
Antonio de Angelis4743e672019-04-11 11:38:48 +010028// cppcheck-suppress preprocessorErrorDirective
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010029#include MBEDTLS_CONFIG_FILE
30#endif
31
32/**
33 * \brief List of engine specific includes for Mbed TLS
34 *
35 */
36#include "mbedtls/memory_buffer_alloc.h"
37#include "mbedtls/md.h"
38#include "mbedtls/cipher.h"
39#include "mbedtls/cmac.h"
40
41#endif /* TFM_CRYPTO_ENGINE_MBEDTLS */
42
43/**
44 * \brief Generic engine cipher context type
45 *
46 */
47union engine_cipher_context {
48 uint32_t dummy; /* Needed to keep the union always not empty */
49#if defined(TFM_CRYPTO_ENGINE_MBEDTLS)
50 mbedtls_cipher_context_t ctx;
51#endif
52};
53
54/**
55 * \brief Generic engine hash context type
56 *
57 */
58union engine_hash_context {
59 uint32_t dummy; /* Needed to keep the union always not empty */
60#if defined (TFM_CRYPTO_ENGINE_MBEDTLS)
61 mbedtls_md_context_t ctx;
62#endif
63};
64
65/**
66 * \brief Generic engine cmac context type
67 *
68 */
69union engine_cmac_context {
70 uint32_t dummy; /* Needed to keep the union always not empty */
71#if defined (TFM_CRYPTO_ENGINE_MBEDTLS)
72 mbedtls_cmac_context_t ctx;
73#endif
74};
75
76
77
78/**
79 * \brief For a cipher operation, define the possible modes of configuration.
80 */
81enum engine_cipher_mode_t {
82 ENGINE_CIPHER_MODE_NONE = 0, /*!< Cipher mode not selected */
83 ENGINE_CIPHER_MODE_DECRYPT, /*!< Cipher mode set to decryption */
84 ENGINE_CIPHER_MODE_ENCRYPT, /*!< Cipher mode set to encryption */
85 /* Used to force enum size */
86 ENGINE_CIPHER_MODE_FORCE_INT_SIZE = INT_MAX
87};
88
89/**
90 * \brief Structure used to keep engine information during the engine setup
91 * step for a hash operation.
92 */
93struct hash_engine_info {
94 uint32_t type; /*!< Engine specific identifier which describes
95 * the operation which has to be configured on
96 * the crypto engine
97 */
98};
99
100/**
101 * \brief Structure used to keep engine information during the engine setup
102 * step for a cipher operation
103 */
104struct cipher_engine_info {
105 uint32_t type; /*!< Engine specific identifier which describes
106 * the operation which has to be configured on
107 * the crypto engine
108 */
109 enum engine_cipher_mode_t cipher_mode; /*!< Describes if the cipher on
110 * the engine is configured for
111 * encryption or decryption
112 */
113 uint32_t padding_mode; /*!< Engine specific identifier which describes
114 * the padding mode which has been configured
115 * on the cipher, if any
116 */
117};
118
119/**
120 * \brief This function performs all the generic operations required to
121 * initialise the crypto engine
122 *
123 * \return Return values as specified by \ref psa_status_t
124 */
125psa_status_t tfm_crypto_engine_init(void);
126
127/**
128 * \brief This function performs the setup of a multipart hash operation on the
129 * crypto engine
130 *
131 * \param[in] alg Algorithm to be setup
132 * \param[out] engine_info Pointer to the engine_info structure containing
133 * engine parameters determined during the setup
134 *
135 * \return Return values as specified by \ref psa_status_t
136 */
137psa_status_t tfm_crypto_engine_hash_setup(const psa_algorithm_t alg,
138 struct hash_engine_info *engine_info);
139/**
140 * \brief This function starts a multipart hash operation on the crypto engine
141 *
Gyorgy Szing40a7af02019-02-06 14:19:47 +0100142 * \param[in,out] hp Pointer to the hash engine context to be used
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100143 * \param[in] engine_info Pointer to the engine_info structure as determined
144 * during the setup step
145 *
146 * \return Return values as specified by \ref psa_status_t
147 */
148psa_status_t tfm_crypto_engine_hash_start(union engine_hash_context *hp,
149 const struct hash_engine_info *engine_info);
150/**
151 * \brief This function updates a multipart hash operation with one chunk of
152 * input data
153 *
Gyorgy Szing40a7af02019-02-06 14:19:47 +0100154 * \param[in,out] hp Pointer to the hash engine context to be used
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100155 * \param[in] input Pointer to the buffer containing the input data
156 * \param[in] input_length Size in bytes of the input data
157 *
158 * \return Return values as specified by \ref psa_status_t
159 */
160psa_status_t tfm_crypto_engine_hash_update(union engine_hash_context *hp,
161 const uint8_t *input,
162 const uint32_t input_length);
163/**
164 * \brief This function finalises a multipart hash operation producing one hash
165 * value in output as described by the operation context
166 *
Gyorgy Szing40a7af02019-02-06 14:19:47 +0100167 * \param[in,out] hp Pointer to the hash engine context to be used
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100168 * \param[out] hash Pointer to the buffer containing the output hash value
169 *
170 * \return Return values as specified by \ref psa_status_t
171 */
172psa_status_t tfm_crypto_engine_hash_finish(union engine_hash_context *hp,
173 uint8_t *hash);
174/**
175 * \brief This function releases the crypto engine resources associated to a
176 * multipart hash operation context
177 *
Gyorgy Szing40a7af02019-02-06 14:19:47 +0100178 * \param[in,out] hp Pointer to the hash engine context to be used
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100179 *
180 * \return Return values as specified by \ref psa_status_t
181 */
182psa_status_t tfm_crypto_engine_hash_release(union engine_hash_context *hp);
183
184/**
185 * \brief This function performs the setup of a multipart cipher operation on
186 * the crypto engine
187 *
188 * \param[in] alg Algorithm to be configured
189 * \param[in] key_type Key type of the key that will be used
190 * \param[in] key_size Size in bits of the key that will be used
191 * \param[in] engine_cipher_mode Parameter specifying if the cipher must be set
192 * in encrypt or decrypt mode
193 * \param[out] engine_info Pointer to the engine configuration structure
194 * containing engine parameters determined during
195 * setup
196 *
197 * \return Return values as specified by \ref psa_status_t
198 */
199psa_status_t tfm_crypto_engine_cipher_setup(const psa_algorithm_t alg,
200 const psa_key_type_t key_type,
201 const uint32_t key_size,
202 const enum engine_cipher_mode_t engine_cipher_mode,
203 struct cipher_engine_info *engine_info);
204/**
205 * \brief This function sets the padding mode on the crypto engine based on the
206 * information gathered during the setup phase
207 *
Gyorgy Szing40a7af02019-02-06 14:19:47 +0100208 * \param[in,out] cp Pointer to the cipher engine context to be used
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100209 * \param[in] engine_info Pointer to the engine configuration structure
210 * containing engine parameters determined during
211 * setup
212 *
213 * \return Return values as specified by \ref psa_status_t
214 */
215psa_status_t tfm_crypto_engine_cipher_set_padding_mode(
216 union engine_cipher_context *cp,
217 const struct cipher_engine_info *engine_info);
218/**
219 * \brief This function starts a multipart cipher operation
220 *
Gyorgy Szing40a7af02019-02-06 14:19:47 +0100221 * \param[in,out] cp Pointer to the cipher engine context to be used
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100222 * \param[in] engine_info Pointer to the engine configuration structure
223 * containing engine parameters determined during
224 * setup
225 *
226 * \return Return values as specified by \ref psa_status_t
227 */
228psa_status_t tfm_crypto_engine_cipher_start(
229 union engine_cipher_context *cp,
230 const struct cipher_engine_info *engine_info);
231/**
232 * \brief This function sets the key data on the crypto engine for a multipart
233 * cipher operation. It reads also from the engine_info configuration
234 * item to determine if the key needs to be set in encryption or
235 * decryption mode on the engine.
236 *
Gyorgy Szing40a7af02019-02-06 14:19:47 +0100237 * \param[in,out] cp Pointer to the cipher engine context to be used
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100238 * \param[in] key_data Pointer to the buffer containing key material
239 * \param[in] key_size Size in bytes of the key pointed by key_data
240 * \param[in] engine_info Pointer to the engine configuration structure
241 * containing engine parameters determined during
242 * setup
243 *
244 * \return Return values as specified by \ref psa_status_t
245 */
246psa_status_t tfm_crypto_engine_cipher_set_key(
247 union engine_cipher_context *cp,
248 const uint8_t *key_data,
249 const uint32_t key_size,
250 const struct cipher_engine_info *engine_info);
251/**
252 * \brief This function sets the initialisation vector on the crypto engine for
253 * a multipart cipher operation
254 *
Gyorgy Szing40a7af02019-02-06 14:19:47 +0100255 * \param[in,out] cp Pointer to the cipher engine context to be used
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100256 * \param[in] iv Pointer to the buffer containing the IV
257 * \param[in] iv_length Size in bytes of the initialisation vector
258 *
259 * \return Return values as specified by \ref psa_status_t
260 */
261psa_status_t tfm_crypto_engine_cipher_set_iv(union engine_cipher_context *cp,
262 const uint8_t *iv,
263 const uint32_t iv_length);
264/**
265 * \brief This function updates a multipart cipher operation on the crypto
266 * engine with a new chunk of input data. It may produce output data.
267 *
Gyorgy Szing40a7af02019-02-06 14:19:47 +0100268 * \param[in,out] cp Pointer to the cipher engine context to be used
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100269 * \param[in] input Pointer to the buffer containing the input data
270 * chunk
271 * \param[in] input_length Size in bytes of the input data chunk
272 * \param[out] output Pointer to the buffer containing the output data
273 * \param[out] output_length Pointer to the size in bytes of the data produced
274 * as output
275 *
276 * \return Return values as specified by \ref psa_status_t
277 */
278psa_status_t tfm_crypto_engine_cipher_update(union engine_cipher_context *cp,
279 const uint8_t *input,
280 const uint32_t input_length,
281 uint8_t *output,
282 uint32_t *output_length);
283/**
284 * \brief This function finalises a multipart cipher operation on the crypto
285 * engine. It may produce output data.
286 *
Gyorgy Szing40a7af02019-02-06 14:19:47 +0100287 * \param[in,out] cp Pointer to the cipher engine context to be used
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100288 * \param[out] output Pointer to the buffer containing the output data
289 * \param[out] output_length Pointer to the size in bytes of the data produced
290 * as output
291 *
292 * \return Return values as specified by \ref psa_status_t
293 */
294psa_status_t tfm_crypto_engine_cipher_finish(union engine_cipher_context *cp,
295 uint8_t *output,
296 uint32_t *output_length);
297/**
298 * \brief This function releases the crypto engine resources associated to a
299 * multipart cipher operation context
300 *
Gyorgy Szing40a7af02019-02-06 14:19:47 +0100301 * \param[in,out] cp Pointer to the cipher engine context to be used
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100302 *
303 * \return Return values as specified by \ref psa_status_t
304 */
305psa_status_t tfm_crypto_engine_cipher_release(union engine_cipher_context *cp);
Antonio de Angelis3a480992018-11-07 11:53:28 +0000306
307/**
308 * \brief This function performs an AEAD encryption on the provided data
309 *
310 * \param[in] key_type Key type of the key that will be used
311 * \param[in] alg Algorithm to be used
312 * \param[in] key_data Pointer to the buffer containing key
313 * material
314 * \param[in] key_size Size in bytes of the key pointed to by
315 * key_data
316 * \param[in] nonce Pointer to a buffer holding a nonce or IV
317 * to use
318 * \param[in] nonce_length Size in bytes of the nonce or IV data
319 * \param[in] additional_data Additional information to be authenticated
320 * \param[in] additional_data_length Size in bytes of the additional data
321 * \param[in] plaintext Buffer pointing to data to be encrypted
322 * \param[in] plaintext_length Size in bytes of the plain text buffer
323 * \param[out] ciphertext Output encrypted data, with the
324 * authentication tag appended
325 * \param[in] ciphertext_size Size in bytes of the buffer to hold the
326 * cipher text plus authentication tag
327 * \param[out] ciphertext_length Size of the ciphertext plus tag produced
328 * as output
329 *
330 * \return Return values as specified by \ref psa_status_t
331 */
332psa_status_t tfm_crypto_engine_aead_encrypt(psa_key_type_t key_type,
333 psa_algorithm_t alg,
334 const uint8_t *key_data,
335 uint32_t key_size,
336 const uint8_t *nonce,
337 uint32_t nonce_length,
338 const uint8_t *additional_data,
339 uint32_t additional_data_length,
340 const uint8_t *plaintext,
341 uint32_t plaintext_length,
342 uint8_t *ciphertext,
343 uint32_t ciphertext_size,
344 uint32_t *ciphertext_length);
345/**
346 * \brief This function performs an AEAD decryption on the provided data
347 *
348 * \param[in] key_type Key type of the key that will be used
349 * \param[in] alg Algorithm to be used
350 * \param[in] key_data Pointer to the buffer containing key
351 * material
352 * \param[in] key_size Size in bytes of the key pointed to by
353 * key_data
354 * \param[in] nonce Pointer to a buffer holding a nonce or IV
355 * to use
356 * \param[in] nonce_length Size in bytes of the nonce or IV data
357 * \param[in] additional_data Additional information which was
358 * authenticated but not encrypted
359 * \param[in] additional_data_length Size in bytes of the additional data
360 * \param[in] ciphertext Buffer pointing to data be decrypted
361 * \param[in] ciphertext_length Size in bytes of the cipher text buffer
362 * \param[out] plaintext Buffer for decrypted output data
363 * \param[in] plaintext_size Size in bytes of the buffer to hold the
364 * plain text
365 * \param[out] plaintext_length Size of the plain text actually produced
366 *
367 * \return Return values as specified by \ref psa_status_t
368 */
369psa_status_t tfm_crypto_engine_aead_decrypt(psa_key_type_t key_type,
370 psa_algorithm_t alg,
371 const uint8_t *key_data,
372 uint32_t key_size,
373 const uint8_t *nonce,
374 uint32_t nonce_length,
375 const uint8_t *additional_data,
376 uint32_t additional_data_length,
377 const uint8_t *ciphertext,
378 uint32_t ciphertext_length,
379 uint8_t *plaintext,
380 uint32_t plaintext_size,
381 uint32_t *plaintext_length);
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100382#ifdef __cplusplus
383}
384#endif
385
386#endif /* __CRYPTO_ENGINE_H__ */