1 /*
2  * The device /dev/cryptocop is accessible using this driver using
3  * CRYPTOCOP_MAJOR (254) and minor number 0.
4  */
5 
6 #ifndef CRYPTOCOP_H
7 #define CRYPTOCOP_H
8 
9 #include <linux/uio.h>
10 
11 
12 #define CRYPTOCOP_SESSION_ID_NONE (0)
13 
14 typedef unsigned long long int cryptocop_session_id;
15 
16 /* cryptocop ioctls */
17 #define ETRAXCRYPTOCOP_IOCTYPE         (250)
18 
19 #define CRYPTOCOP_IO_CREATE_SESSION    _IOWR(ETRAXCRYPTOCOP_IOCTYPE, 1, struct strcop_session_op)
20 #define CRYPTOCOP_IO_CLOSE_SESSION     _IOW(ETRAXCRYPTOCOP_IOCTYPE, 2, struct strcop_session_op)
21 #define CRYPTOCOP_IO_PROCESS_OP        _IOWR(ETRAXCRYPTOCOP_IOCTYPE, 3, struct strcop_crypto_op)
22 #define CRYPTOCOP_IO_MAXNR             (3)
23 
24 typedef enum {
25 	cryptocop_cipher_des = 0,
26 	cryptocop_cipher_3des = 1,
27 	cryptocop_cipher_aes = 2,
28 	cryptocop_cipher_m2m = 3, /* mem2mem is essentially a NULL cipher with blocklength=1 */
29 	cryptocop_cipher_none
30 } cryptocop_cipher_type;
31 
32 typedef enum {
33 	cryptocop_digest_sha1 = 0,
34 	cryptocop_digest_md5 = 1,
35 	cryptocop_digest_none
36 } cryptocop_digest_type;
37 
38 typedef enum {
39 	cryptocop_csum_le = 0,
40 	cryptocop_csum_be = 1,
41 	cryptocop_csum_none
42 } cryptocop_csum_type;
43 
44 typedef enum {
45 	cryptocop_cipher_mode_ecb = 0,
46 	cryptocop_cipher_mode_cbc,
47 	cryptocop_cipher_mode_none
48 } cryptocop_cipher_mode;
49 
50 typedef enum {
51 	cryptocop_3des_eee = 0,
52 	cryptocop_3des_eed = 1,
53 	cryptocop_3des_ede = 2,
54 	cryptocop_3des_edd = 3,
55 	cryptocop_3des_dee = 4,
56 	cryptocop_3des_ded = 5,
57 	cryptocop_3des_dde = 6,
58 	cryptocop_3des_ddd = 7
59 } cryptocop_3des_mode;
60 
61 /* Usermode accessible (ioctl) operations. */
62 struct strcop_session_op{
63 	cryptocop_session_id    ses_id;
64 
65 	cryptocop_cipher_type   cipher; /* AES, DES, 3DES, m2m, none */
66 
67 	cryptocop_cipher_mode   cmode; /* ECB, CBC, none */
68 	cryptocop_3des_mode     des3_mode;
69 
70 	cryptocop_digest_type   digest; /* MD5, SHA1, none */
71 
72 	cryptocop_csum_type     csum;   /* BE, LE, none */
73 
74 	unsigned char           *key;
75 	size_t                  keylen;
76 };
77 
78 #define CRYPTOCOP_CSUM_LENGTH         (2)
79 #define CRYPTOCOP_MAX_DIGEST_LENGTH   (20)  /* SHA-1 20, MD5 16 */
80 #define CRYPTOCOP_MAX_IV_LENGTH       (16)  /* (3)DES==8, AES == 16 */
81 #define CRYPTOCOP_MAX_KEY_LENGTH      (32)
82 
83 struct strcop_crypto_op{
84 	cryptocop_session_id ses_id;
85 
86 	/* Indata. */
87 	unsigned char            *indata;
88 	size_t                   inlen; /* Total indata length. */
89 
90 	/* Cipher configuration. */
91 	unsigned char            do_cipher:1;
92 	unsigned char            decrypt:1; /* 1 == decrypt, 0 == encrypt */
93 	unsigned char            cipher_explicit:1;
94 	size_t                   cipher_start;
95 	size_t                   cipher_len;
96 	/* cipher_iv is used if do_cipher and cipher_explicit and the cipher
97 	   mode is CBC.  The length is controlled by the type of cipher,
98 	   e.g. DES/3DES 8 octets and AES 16 octets. */
99 	unsigned char            cipher_iv[CRYPTOCOP_MAX_IV_LENGTH];
100 	/* Outdata. */
101 	unsigned char            *cipher_outdata;
102 	size_t                   cipher_outlen;
103 
104 	/* digest configuration. */
105 	unsigned char            do_digest:1;
106 	size_t                   digest_start;
107 	size_t                   digest_len;
108 	/* Outdata.  The actual length is determined by the type of the digest. */
109 	unsigned char            digest[CRYPTOCOP_MAX_DIGEST_LENGTH];
110 
111 	/* Checksum configuration. */
112 	unsigned char            do_csum:1;
113 	size_t                   csum_start;
114 	size_t                   csum_len;
115 	/* Outdata. */
116 	unsigned char            csum[CRYPTOCOP_CSUM_LENGTH];
117 };
118 
119 
120 
121 #ifdef __KERNEL__
122 
123 /********** The API to use from inside the kernel. ************/
124 
125 #include <arch/hwregs/dma.h>
126 
127 typedef enum {
128 	cryptocop_alg_csum = 0,
129 	cryptocop_alg_mem2mem,
130 	cryptocop_alg_md5,
131 	cryptocop_alg_sha1,
132 	cryptocop_alg_des,
133 	cryptocop_alg_3des,
134 	cryptocop_alg_aes,
135 	cryptocop_no_alg,
136 } cryptocop_algorithm;
137 
138 typedef u8 cryptocop_tfrm_id;
139 
140 
141 struct cryptocop_operation;
142 
143 typedef void (cryptocop_callback)(struct cryptocop_operation*, void*);
144 
145 struct cryptocop_transform_init {
146 	cryptocop_algorithm    alg;
147 	/* Keydata for ciphers. */
148 	unsigned char          key[CRYPTOCOP_MAX_KEY_LENGTH];
149 	unsigned int           keylen;
150 	cryptocop_cipher_mode  cipher_mode;
151 	cryptocop_3des_mode    tdes_mode;
152 	cryptocop_csum_type    csum_mode; /* cryptocop_csum_none is not allowed when alg==cryptocop_alg_csum */
153 
154 	cryptocop_tfrm_id tid; /* Locally unique in session; assigned by user, checked by driver. */
155 	struct cryptocop_transform_init *next;
156 };
157 
158 
159 typedef enum {
160 	cryptocop_source_dma = 0,
161 	cryptocop_source_des,
162 	cryptocop_source_3des,
163 	cryptocop_source_aes,
164 	cryptocop_source_md5,
165 	cryptocop_source_sha1,
166 	cryptocop_source_csum,
167 	cryptocop_source_none,
168 } cryptocop_source;
169 
170 
171 struct cryptocop_desc_cfg {
172 	cryptocop_tfrm_id tid;
173 	cryptocop_source src;
174 	unsigned int last:1; /* Last use of this transform in the operation.  Will push outdata when encountered. */
175 	struct cryptocop_desc_cfg *next;
176 };
177 
178 struct cryptocop_desc {
179 	size_t length;
180 	struct cryptocop_desc_cfg *cfg;
181 	struct cryptocop_desc *next;
182 };
183 
184 
185 /* Flags for cryptocop_tfrm_cfg */
186 #define CRYPTOCOP_NO_FLAG     (0x00)
187 #define CRYPTOCOP_ENCRYPT     (0x01)
188 #define CRYPTOCOP_DECRYPT     (0x02)
189 #define CRYPTOCOP_EXPLICIT_IV (0x04)
190 
191 struct cryptocop_tfrm_cfg {
192 	cryptocop_tfrm_id tid;
193 
194 	unsigned int flags; /* DECRYPT, ENCRYPT, EXPLICIT_IV */
195 
196 	/* CBC initialisation vector for cihers. */
197 	u8 iv[CRYPTOCOP_MAX_IV_LENGTH];
198 
199 	/* The position in output where to write the transform output.  The order
200 	   in which the driver writes the output is unspecified, hence if several
201 	   transforms write on the same positions in the output the result is
202 	   unspecified. */
203 	size_t inject_ix;
204 
205 	struct cryptocop_tfrm_cfg *next;
206 };
207 
208 
209 
210 struct cryptocop_dma_list_operation{
211 	/* The consumer can provide DMA lists to send to the co-processor.  'use_dmalists' in
212 	   struct cryptocop_operation must be set for the driver to use them.  outlist,
213 	   out_data_buf, inlist and in_data_buf must all be physical addresses since they will
214 	   be loaded to DMA . */
215 	dma_descr_data *outlist; /* Out from memory to the co-processor. */
216 	char           *out_data_buf;
217 	dma_descr_data *inlist; /* In from the co-processor to memory. */
218 	char           *in_data_buf;
219 
220 	cryptocop_3des_mode tdes_mode;
221 	cryptocop_csum_type csum_mode;
222 };
223 
224 
225 struct cryptocop_tfrm_operation{
226 	/* Operation configuration, if not 'use_dmalists' is set. */
227 	struct cryptocop_tfrm_cfg *tfrm_cfg;
228 	struct cryptocop_desc *desc;
229 
230 	struct iovec *indata;
231 	size_t incount;
232 	size_t inlen; /* Total inlength. */
233 
234 	struct iovec *outdata;
235 	size_t outcount;
236 	size_t outlen; /* Total outlength. */
237 };
238 
239 
240 struct cryptocop_operation {
241 	cryptocop_callback *cb;
242 	void *cb_data;
243 
244 	cryptocop_session_id sid;
245 
246 	/* The status of the operation when returned to consumer. */
247 	int operation_status; /* 0, -EAGAIN */
248 
249 	/* Flags */
250 	unsigned int use_dmalists:1;  /* Use outlist and inlist instead of the desc/tfrm_cfg configuration. */
251 	unsigned int in_interrupt:1;  /* Set if inserting job from interrupt context. */
252 	unsigned int fast_callback:1; /* Set if fast callback wanted, i.e. from interrupt context. */
253 
254 	union{
255 		struct cryptocop_dma_list_operation list_op;
256 		struct cryptocop_tfrm_operation tfrm_op;
257 	};
258 };
259 
260 
261 int cryptocop_new_session(cryptocop_session_id *sid, struct cryptocop_transform_init *tinit, int alloc_flag);
262 int cryptocop_free_session(cryptocop_session_id sid);
263 
264 int cryptocop_job_queue_insert_csum(struct cryptocop_operation *operation);
265 
266 int cryptocop_job_queue_insert_crypto(struct cryptocop_operation *operation);
267 
268 int cryptocop_job_queue_insert_user_job(struct cryptocop_operation *operation);
269 
270 #endif /* __KERNEL__ */
271 
272 #endif /* CRYPTOCOP_H */
273