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