xref: /optee_os/core/drivers/crypto/caam/cipher/local.h (revision 9bdff33e4e1801cd9a723bdcd932f239612d9de7)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright 2018-2020 NXP
4  *
5  * CAAM Cipher Local header.
6  */
7 #ifndef __LOCAL_H__
8 #define __LOCAL_H__
9 
10 #include <drvcrypt.h>
11 #include <drvcrypt_cipher.h>
12 
13 /*
14  * Definition of the maximum number of CAAM Job descriptor entries
15  */
16 #ifdef CFG_CAAM_64BIT
17 #define MAX_DESC_ENTRIES 22
18 #else
19 #define MAX_DESC_ENTRIES 16
20 #endif
21 
22 /*
23  * Definition of flags tagging which key(s) is required
24  */
25 #define NEED_KEY1  BIT(0)
26 #define NEED_KEY2  BIT(1)
27 #define NEED_IV    BIT(2)
28 #define NEED_TWEAK BIT(3)
29 
30 /*
31  * Cipher Algorithm definition
32  */
33 struct cipheralg {
34 	uint32_t type;             /* Algo type for operation */
35 	uint8_t size_block;        /* Computing block size */
36 	uint8_t size_ctx;          /* CAAM Context Register size */
37 	uint8_t ctx_offset;        /* CAAM Context Register offset */
38 	uint8_t require_key;       /* Tag defining key(s) required */
39 	struct caamdefkey def_key; /* Key size accepted */
40 
41 	TEE_Result (*update)(struct drvcrypt_cipher_update *dupdate);
42 };
43 
44 /*
45  * Full Cipher data SW context
46  */
47 struct cipherdata {
48 	uint32_t *descriptor;        /* Job descriptor */
49 	bool encrypt;                /* Encrypt direction */
50 	struct caambuf key1;         /* First Key */
51 	struct caambuf key2;         /* Second Key */
52 	struct caambuf tweak;        /* XTS Tweak */
53 	struct caambuf ctx;          /* CAAM Context Register */
54 	struct caamblock blockbuf;   /* Temporary Block buffer */
55 	const struct cipheralg *alg; /* Reference to the algo constants */
56 };
57 
58 /*
59  * Cipher additionnal data block
60  */
61 enum caam_cipher_block {
62 	CIPHER_BLOCK_NONE = 0,
63 	CIPHER_BLOCK_IN,
64 	CIPHER_BLOCK_OUT,
65 	CIPHER_BLOCK_BOTH,
66 };
67 
68 /*
69  * Update of the cipher operation of complete block except
70  * if last block. Last block can be partial block.
71  *
72  * @ctx      Cipher context
73  * @savectx  Save or not the context
74  * @keyid    Id of the key to be used during operation
75  * @encrypt  Encrypt or decrypt direction
76  * @src      Source data to encrypt/decrypt
77  * @dst      [out] Destination data encrypted/decrypted
78  * @blocks   Additionnal data block to handle (input/output)
79  */
80 enum caam_status caam_cipher_block(struct cipherdata *ctx, bool savectx,
81 				   uint8_t keyid, bool encrypt,
82 				   struct caambuf *src, struct caambuf *dst,
83 				   enum caam_cipher_block blocks);
84 
85 /*
86  * Update of the cipher operation in xts mode.
87  *
88  * @dupdate  Data update object
89  */
90 TEE_Result caam_cipher_update_xts(struct drvcrypt_cipher_update *dupdate);
91 
92 /*
93  * Initialization of the cipher operation
94  *
95  * @dinit  Data initialization object
96  */
97 TEE_Result caam_cipher_initialize(struct drvcrypt_cipher_init *dinit);
98 
99 /*
100  * Free software context
101  *
102  * @ctx    Caller context variable
103  */
104 void caam_cipher_free(void *ctx);
105 
106 /*
107  * Copy software Context
108  *
109  * @dst_ctx  [out] Reference the context destination
110  * @src_ctx  Reference the context source
111  */
112 void caam_cipher_copy_state(void *dst_ctx, void *src_ctx);
113 
114 #endif /* __LOCAL_H__ */
115