xref: /optee_os/core/drivers/crypto/stm32/stm32_cryp.h (revision 8b826c3ba66f1825cd3e29b451cb072d0caa7034)
15e64ae67SNicolas Toromanoff /* SPDX-License-Identifier: BSD-2-Clause */
25e64ae67SNicolas Toromanoff /*
35e64ae67SNicolas Toromanoff  * Copyright (c) 2021, STMicroelectronics - All Rights Reserved
45e64ae67SNicolas Toromanoff  */
55e64ae67SNicolas Toromanoff 
65e64ae67SNicolas Toromanoff #ifndef STM32_CRYP_H
75e64ae67SNicolas Toromanoff #define STM32_CRYP_H
85e64ae67SNicolas Toromanoff 
9*8b826c3bSEtienne Carriere #include <drivers/clk.h>
10047c4fe1SEtienne Carriere #include <drivers/rstctrl.h>
115e64ae67SNicolas Toromanoff #include <kernel/mutex.h>
125e64ae67SNicolas Toromanoff #include <mm/core_memprot.h>
135e64ae67SNicolas Toromanoff #include <stdbool.h>
145e64ae67SNicolas Toromanoff #include <stddef.h>
155e64ae67SNicolas Toromanoff #include <stdint.h>
165e64ae67SNicolas Toromanoff 
17*8b826c3bSEtienne Carriere /*
18*8b826c3bSEtienne Carriere  * Platform data related to CRYP instance
19*8b826c3bSEtienne Carriere  * @base - IO memory base address
20*8b826c3bSEtienne Carriere  * @clk - CRYP clock reference
21*8b826c3bSEtienne Carriere  * @rstctrl - CRYP reset controller reference
22*8b826c3bSEtienne Carriere  */
235e64ae67SNicolas Toromanoff struct stm32_cryp_platdata {
245e64ae67SNicolas Toromanoff 	struct io_pa_va base;
25*8b826c3bSEtienne Carriere 	struct clk *clock;
26047c4fe1SEtienne Carriere 	struct rstctrl *reset;
275e64ae67SNicolas Toromanoff };
285e64ae67SNicolas Toromanoff 
295e64ae67SNicolas Toromanoff enum stm32_cryp_algo_mode {
305e64ae67SNicolas Toromanoff 	STM32_CRYP_MODE_TDES_ECB,
315e64ae67SNicolas Toromanoff 	STM32_CRYP_MODE_TDES_CBC,
325e64ae67SNicolas Toromanoff 	STM32_CRYP_MODE_DES_ECB,
335e64ae67SNicolas Toromanoff 	STM32_CRYP_MODE_DES_CBC,
345e64ae67SNicolas Toromanoff 	STM32_CRYP_MODE_AES_ECB,
355e64ae67SNicolas Toromanoff 	STM32_CRYP_MODE_AES_CBC,
365e64ae67SNicolas Toromanoff 	STM32_CRYP_MODE_AES_CTR,
375e64ae67SNicolas Toromanoff 	STM32_CRYP_MODE_AES_GCM,
385e64ae67SNicolas Toromanoff 	STM32_CRYP_MODE_AES_CCM,
395e64ae67SNicolas Toromanoff };
405e64ae67SNicolas Toromanoff 
415e64ae67SNicolas Toromanoff /*
425e64ae67SNicolas Toromanoff  * Full CRYP context.
435e64ae67SNicolas Toromanoff  * Store CRYP internal state to be able to compute any supported algorithm.
445e64ae67SNicolas Toromanoff  */
455e64ae67SNicolas Toromanoff struct stm32_cryp_context {
465e64ae67SNicolas Toromanoff 	vaddr_t base;
475e64ae67SNicolas Toromanoff 	uint32_t cr;
485e64ae67SNicolas Toromanoff 	struct mutex *lock; /* Protect CRYP HW instance access */
495e64ae67SNicolas Toromanoff 	uint32_t assoc_len;
505e64ae67SNicolas Toromanoff 	uint32_t load_len;
515e64ae67SNicolas Toromanoff 	uint32_t key[8]; /* In HW byte order */
525e64ae67SNicolas Toromanoff 	size_t key_size;
535e64ae67SNicolas Toromanoff 	size_t block_u32;
545e64ae67SNicolas Toromanoff 	uint32_t iv[4];  /* In HW byte order */
555e64ae67SNicolas Toromanoff 	uint32_t pm_gcmccm[8];
565e64ae67SNicolas Toromanoff 	union {
575e64ae67SNicolas Toromanoff 		uint32_t pm_gcm[8];
585e64ae67SNicolas Toromanoff 		uint32_t ctr0_ccm[4];
595e64ae67SNicolas Toromanoff 	};
605e64ae67SNicolas Toromanoff 	uint32_t extra[4];
615e64ae67SNicolas Toromanoff 	size_t extra_size;
625e64ae67SNicolas Toromanoff };
635e64ae67SNicolas Toromanoff 
645e64ae67SNicolas Toromanoff TEE_Result stm32_cryp_init(struct stm32_cryp_context *ctx, bool is_decrypt,
655e64ae67SNicolas Toromanoff 			   enum stm32_cryp_algo_mode mode,
665e64ae67SNicolas Toromanoff 			   const void *key, size_t key_size, const void *iv,
675e64ae67SNicolas Toromanoff 			   size_t iv_size);
685e64ae67SNicolas Toromanoff TEE_Result stm32_cryp_update(struct stm32_cryp_context *ctx, bool last_block,
695e64ae67SNicolas Toromanoff 			     uint8_t *data_in, uint8_t *data_out,
705e64ae67SNicolas Toromanoff 			     size_t data_size);
715e64ae67SNicolas Toromanoff TEE_Result stm32_cryp_update_assodata(struct stm32_cryp_context *ctx,
725e64ae67SNicolas Toromanoff 				      uint8_t *data, size_t data_size);
735e64ae67SNicolas Toromanoff TEE_Result stm32_cryp_update_load(struct stm32_cryp_context *ctx,
745e64ae67SNicolas Toromanoff 				  uint8_t *data_in, uint8_t *data_out,
755e64ae67SNicolas Toromanoff 				  size_t data_size);
765e64ae67SNicolas Toromanoff TEE_Result stm32_cryp_final(struct stm32_cryp_context *ctx, uint8_t *tag,
775e64ae67SNicolas Toromanoff 			    size_t tag_size);
785e64ae67SNicolas Toromanoff #endif
79