1#ifndef _VIRTIO_CRYPTO_H
2#define _VIRTIO_CRYPTO_H
3/* This header is BSD licensed so anyone can use the definitions to implement
4 * compatible drivers/servers.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of IBM nor the names of its contributors
15 *    may be used to endorse or promote products derived from this software
16 *    without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL IBM OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
27 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30#include <linux/types.h>
31#include <linux/virtio_types.h>
32#include <linux/virtio_ids.h>
33#include <linux/virtio_config.h>
34
35
36#define VIRTIO_CRYPTO_SERVICE_CIPHER 0
37#define VIRTIO_CRYPTO_SERVICE_HASH   1
38#define VIRTIO_CRYPTO_SERVICE_MAC    2
39#define VIRTIO_CRYPTO_SERVICE_AEAD   3
40#define VIRTIO_CRYPTO_SERVICE_AKCIPHER 4
41
42#define VIRTIO_CRYPTO_OPCODE(service, op)   (((service) << 8) | (op))
43
44struct virtio_crypto_ctrl_header {
45#define VIRTIO_CRYPTO_CIPHER_CREATE_SESSION \
46	   VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_CIPHER, 0x02)
47#define VIRTIO_CRYPTO_CIPHER_DESTROY_SESSION \
48	   VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_CIPHER, 0x03)
49#define VIRTIO_CRYPTO_HASH_CREATE_SESSION \
50	   VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_HASH, 0x02)
51#define VIRTIO_CRYPTO_HASH_DESTROY_SESSION \
52	   VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_HASH, 0x03)
53#define VIRTIO_CRYPTO_MAC_CREATE_SESSION \
54	   VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_MAC, 0x02)
55#define VIRTIO_CRYPTO_MAC_DESTROY_SESSION \
56	   VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_MAC, 0x03)
57#define VIRTIO_CRYPTO_AEAD_CREATE_SESSION \
58	   VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_AEAD, 0x02)
59#define VIRTIO_CRYPTO_AEAD_DESTROY_SESSION \
60	   VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_AEAD, 0x03)
61#define VIRTIO_CRYPTO_AKCIPHER_CREATE_SESSION \
62	   VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_AKCIPHER, 0x04)
63#define VIRTIO_CRYPTO_AKCIPHER_DESTROY_SESSION \
64	   VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_AKCIPHER, 0x05)
65	__le32 opcode;
66	__le32 algo;
67	__le32 flag;
68	/* data virtqueue id */
69	__le32 queue_id;
70};
71
72struct virtio_crypto_cipher_session_para {
73#define VIRTIO_CRYPTO_NO_CIPHER                 0
74#define VIRTIO_CRYPTO_CIPHER_ARC4               1
75#define VIRTIO_CRYPTO_CIPHER_AES_ECB            2
76#define VIRTIO_CRYPTO_CIPHER_AES_CBC            3
77#define VIRTIO_CRYPTO_CIPHER_AES_CTR            4
78#define VIRTIO_CRYPTO_CIPHER_DES_ECB            5
79#define VIRTIO_CRYPTO_CIPHER_DES_CBC            6
80#define VIRTIO_CRYPTO_CIPHER_3DES_ECB           7
81#define VIRTIO_CRYPTO_CIPHER_3DES_CBC           8
82#define VIRTIO_CRYPTO_CIPHER_3DES_CTR           9
83#define VIRTIO_CRYPTO_CIPHER_KASUMI_F8          10
84#define VIRTIO_CRYPTO_CIPHER_SNOW3G_UEA2        11
85#define VIRTIO_CRYPTO_CIPHER_AES_F8             12
86#define VIRTIO_CRYPTO_CIPHER_AES_XTS            13
87#define VIRTIO_CRYPTO_CIPHER_ZUC_EEA3           14
88	__le32 algo;
89	/* length of key */
90	__le32 keylen;
91
92#define VIRTIO_CRYPTO_OP_ENCRYPT  1
93#define VIRTIO_CRYPTO_OP_DECRYPT  2
94	/* encrypt or decrypt */
95	__le32 op;
96	__le32 padding;
97};
98
99struct virtio_crypto_session_input {
100	/* Device-writable part */
101	__le64 session_id;
102	__le32 status;
103	__le32 padding;
104};
105
106struct virtio_crypto_cipher_session_req {
107	struct virtio_crypto_cipher_session_para para;
108	__u8 padding[32];
109};
110
111struct virtio_crypto_hash_session_para {
112#define VIRTIO_CRYPTO_NO_HASH            0
113#define VIRTIO_CRYPTO_HASH_MD5           1
114#define VIRTIO_CRYPTO_HASH_SHA1          2
115#define VIRTIO_CRYPTO_HASH_SHA_224       3
116#define VIRTIO_CRYPTO_HASH_SHA_256       4
117#define VIRTIO_CRYPTO_HASH_SHA_384       5
118#define VIRTIO_CRYPTO_HASH_SHA_512       6
119#define VIRTIO_CRYPTO_HASH_SHA3_224      7
120#define VIRTIO_CRYPTO_HASH_SHA3_256      8
121#define VIRTIO_CRYPTO_HASH_SHA3_384      9
122#define VIRTIO_CRYPTO_HASH_SHA3_512      10
123#define VIRTIO_CRYPTO_HASH_SHA3_SHAKE128      11
124#define VIRTIO_CRYPTO_HASH_SHA3_SHAKE256      12
125	__le32 algo;
126	/* hash result length */
127	__le32 hash_result_len;
128	__u8 padding[8];
129};
130
131struct virtio_crypto_hash_create_session_req {
132	struct virtio_crypto_hash_session_para para;
133	__u8 padding[40];
134};
135
136struct virtio_crypto_mac_session_para {
137#define VIRTIO_CRYPTO_NO_MAC                       0
138#define VIRTIO_CRYPTO_MAC_HMAC_MD5                 1
139#define VIRTIO_CRYPTO_MAC_HMAC_SHA1                2
140#define VIRTIO_CRYPTO_MAC_HMAC_SHA_224             3
141#define VIRTIO_CRYPTO_MAC_HMAC_SHA_256             4
142#define VIRTIO_CRYPTO_MAC_HMAC_SHA_384             5
143#define VIRTIO_CRYPTO_MAC_HMAC_SHA_512             6
144#define VIRTIO_CRYPTO_MAC_CMAC_3DES                25
145#define VIRTIO_CRYPTO_MAC_CMAC_AES                 26
146#define VIRTIO_CRYPTO_MAC_KASUMI_F9                27
147#define VIRTIO_CRYPTO_MAC_SNOW3G_UIA2              28
148#define VIRTIO_CRYPTO_MAC_GMAC_AES                 41
149#define VIRTIO_CRYPTO_MAC_GMAC_TWOFISH             42
150#define VIRTIO_CRYPTO_MAC_CBCMAC_AES               49
151#define VIRTIO_CRYPTO_MAC_CBCMAC_KASUMI_F9         50
152#define VIRTIO_CRYPTO_MAC_XCBC_AES                 53
153	__le32 algo;
154	/* hash result length */
155	__le32 hash_result_len;
156	/* length of authenticated key */
157	__le32 auth_key_len;
158	__le32 padding;
159};
160
161struct virtio_crypto_mac_create_session_req {
162	struct virtio_crypto_mac_session_para para;
163	__u8 padding[40];
164};
165
166struct virtio_crypto_aead_session_para {
167#define VIRTIO_CRYPTO_NO_AEAD     0
168#define VIRTIO_CRYPTO_AEAD_GCM    1
169#define VIRTIO_CRYPTO_AEAD_CCM    2
170#define VIRTIO_CRYPTO_AEAD_CHACHA20_POLY1305  3
171	__le32 algo;
172	/* length of key */
173	__le32 key_len;
174	/* hash result length */
175	__le32 hash_result_len;
176	/* length of the additional authenticated data (AAD) in bytes */
177	__le32 aad_len;
178	/* encrypt or decrypt, See above VIRTIO_CRYPTO_OP_* */
179	__le32 op;
180	__le32 padding;
181};
182
183struct virtio_crypto_aead_create_session_req {
184	struct virtio_crypto_aead_session_para para;
185	__u8 padding[32];
186};
187
188struct virtio_crypto_rsa_session_para {
189#define VIRTIO_CRYPTO_RSA_RAW_PADDING   0
190#define VIRTIO_CRYPTO_RSA_PKCS1_PADDING 1
191	__le32 padding_algo;
192
193#define VIRTIO_CRYPTO_RSA_NO_HASH   0
194#define VIRTIO_CRYPTO_RSA_MD2       1
195#define VIRTIO_CRYPTO_RSA_MD3       2
196#define VIRTIO_CRYPTO_RSA_MD4       3
197#define VIRTIO_CRYPTO_RSA_MD5       4
198#define VIRTIO_CRYPTO_RSA_SHA1      5
199#define VIRTIO_CRYPTO_RSA_SHA256    6
200#define VIRTIO_CRYPTO_RSA_SHA384    7
201#define VIRTIO_CRYPTO_RSA_SHA512    8
202#define VIRTIO_CRYPTO_RSA_SHA224    9
203	__le32 hash_algo;
204};
205
206struct virtio_crypto_ecdsa_session_para {
207#define VIRTIO_CRYPTO_CURVE_UNKNOWN   0
208#define VIRTIO_CRYPTO_CURVE_NIST_P192 1
209#define VIRTIO_CRYPTO_CURVE_NIST_P224 2
210#define VIRTIO_CRYPTO_CURVE_NIST_P256 3
211#define VIRTIO_CRYPTO_CURVE_NIST_P384 4
212#define VIRTIO_CRYPTO_CURVE_NIST_P521 5
213	__le32 curve_id;
214	__le32 padding;
215};
216
217struct virtio_crypto_akcipher_session_para {
218#define VIRTIO_CRYPTO_NO_AKCIPHER    0
219#define VIRTIO_CRYPTO_AKCIPHER_RSA   1
220#define VIRTIO_CRYPTO_AKCIPHER_DSA   2
221#define VIRTIO_CRYPTO_AKCIPHER_ECDSA 3
222	__le32 algo;
223
224#define VIRTIO_CRYPTO_AKCIPHER_KEY_TYPE_PUBLIC  1
225#define VIRTIO_CRYPTO_AKCIPHER_KEY_TYPE_PRIVATE 2
226	__le32 keytype;
227	__le32 keylen;
228
229	union {
230		struct virtio_crypto_rsa_session_para rsa;
231		struct virtio_crypto_ecdsa_session_para ecdsa;
232	} u;
233};
234
235struct virtio_crypto_akcipher_create_session_req {
236	struct virtio_crypto_akcipher_session_para para;
237	__u8 padding[36];
238};
239
240struct virtio_crypto_alg_chain_session_para {
241#define VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_HASH_THEN_CIPHER  1
242#define VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_CIPHER_THEN_HASH  2
243	__le32 alg_chain_order;
244/* Plain hash */
245#define VIRTIO_CRYPTO_SYM_HASH_MODE_PLAIN    1
246/* Authenticated hash (mac) */
247#define VIRTIO_CRYPTO_SYM_HASH_MODE_AUTH     2
248/* Nested hash */
249#define VIRTIO_CRYPTO_SYM_HASH_MODE_NESTED   3
250	__le32 hash_mode;
251	struct virtio_crypto_cipher_session_para cipher_param;
252	union {
253		struct virtio_crypto_hash_session_para hash_param;
254		struct virtio_crypto_mac_session_para mac_param;
255		__u8 padding[16];
256	} u;
257	/* length of the additional authenticated data (AAD) in bytes */
258	__le32 aad_len;
259	__le32 padding;
260};
261
262struct virtio_crypto_alg_chain_session_req {
263	struct virtio_crypto_alg_chain_session_para para;
264};
265
266struct virtio_crypto_sym_create_session_req {
267	union {
268		struct virtio_crypto_cipher_session_req cipher;
269		struct virtio_crypto_alg_chain_session_req chain;
270		__u8 padding[48];
271	} u;
272
273	/* Device-readable part */
274
275/* No operation */
276#define VIRTIO_CRYPTO_SYM_OP_NONE  0
277/* Cipher only operation on the data */
278#define VIRTIO_CRYPTO_SYM_OP_CIPHER  1
279/*
280 * Chain any cipher with any hash or mac operation. The order
281 * depends on the value of alg_chain_order param
282 */
283#define VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING  2
284	__le32 op_type;
285	__le32 padding;
286};
287
288struct virtio_crypto_destroy_session_req {
289	/* Device-readable part */
290	__le64  session_id;
291	__u8 padding[48];
292};
293
294/* The request of the control virtqueue's packet */
295struct virtio_crypto_op_ctrl_req {
296	struct virtio_crypto_ctrl_header header;
297
298	union {
299		struct virtio_crypto_sym_create_session_req
300			sym_create_session;
301		struct virtio_crypto_hash_create_session_req
302			hash_create_session;
303		struct virtio_crypto_mac_create_session_req
304			mac_create_session;
305		struct virtio_crypto_aead_create_session_req
306			aead_create_session;
307		struct virtio_crypto_akcipher_create_session_req
308			akcipher_create_session;
309		struct virtio_crypto_destroy_session_req
310			destroy_session;
311		__u8 padding[56];
312	} u;
313};
314
315struct virtio_crypto_op_header {
316#define VIRTIO_CRYPTO_CIPHER_ENCRYPT \
317	VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_CIPHER, 0x00)
318#define VIRTIO_CRYPTO_CIPHER_DECRYPT \
319	VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_CIPHER, 0x01)
320#define VIRTIO_CRYPTO_HASH \
321	VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_HASH, 0x00)
322#define VIRTIO_CRYPTO_MAC \
323	VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_MAC, 0x00)
324#define VIRTIO_CRYPTO_AEAD_ENCRYPT \
325	VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_AEAD, 0x00)
326#define VIRTIO_CRYPTO_AEAD_DECRYPT \
327	VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_AEAD, 0x01)
328#define VIRTIO_CRYPTO_AKCIPHER_ENCRYPT \
329	VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_AKCIPHER, 0x00)
330#define VIRTIO_CRYPTO_AKCIPHER_DECRYPT \
331	VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_AKCIPHER, 0x01)
332#define VIRTIO_CRYPTO_AKCIPHER_SIGN \
333	VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_AKCIPHER, 0x02)
334#define VIRTIO_CRYPTO_AKCIPHER_VERIFY \
335	VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_AKCIPHER, 0x03)
336	__le32 opcode;
337	/* algo should be service-specific algorithms */
338	__le32 algo;
339	/* session_id should be service-specific algorithms */
340	__le64 session_id;
341	/* control flag to control the request */
342	__le32 flag;
343	__le32 padding;
344};
345
346struct virtio_crypto_cipher_para {
347	/*
348	 * Byte Length of valid IV/Counter
349	 *
350	 * For block ciphers in CBC or F8 mode, or for Kasumi in F8 mode, or for
351	 *   SNOW3G in UEA2 mode, this is the length of the IV (which
352	 *   must be the same as the block length of the cipher).
353	 * For block ciphers in CTR mode, this is the length of the counter
354	 *   (which must be the same as the block length of the cipher).
355	 * For AES-XTS, this is the 128bit tweak, i, from IEEE Std 1619-2007.
356	 *
357	 * The IV/Counter will be updated after every partial cryptographic
358	 * operation.
359	 */
360	__le32 iv_len;
361	/* length of source data */
362	__le32 src_data_len;
363	/* length of dst data */
364	__le32 dst_data_len;
365	__le32 padding;
366};
367
368struct virtio_crypto_hash_para {
369	/* length of source data */
370	__le32 src_data_len;
371	/* hash result length */
372	__le32 hash_result_len;
373};
374
375struct virtio_crypto_mac_para {
376	struct virtio_crypto_hash_para hash;
377};
378
379struct virtio_crypto_aead_para {
380	/*
381	 * Byte Length of valid IV data pointed to by the below iv_addr
382	 * parameter.
383	 *
384	 * For GCM mode, this is either 12 (for 96-bit IVs) or 16, in which
385	 *   case iv_addr points to J0.
386	 * For CCM mode, this is the length of the nonce, which can be in the
387	 *   range 7 to 13 inclusive.
388	 */
389	__le32 iv_len;
390	/* length of additional auth data */
391	__le32 aad_len;
392	/* length of source data */
393	__le32 src_data_len;
394	/* length of dst data */
395	__le32 dst_data_len;
396};
397
398struct virtio_crypto_cipher_data_req {
399	/* Device-readable part */
400	struct virtio_crypto_cipher_para para;
401	__u8 padding[24];
402};
403
404struct virtio_crypto_hash_data_req {
405	/* Device-readable part */
406	struct virtio_crypto_hash_para para;
407	__u8 padding[40];
408};
409
410struct virtio_crypto_mac_data_req {
411	/* Device-readable part */
412	struct virtio_crypto_mac_para para;
413	__u8 padding[40];
414};
415
416struct virtio_crypto_alg_chain_data_para {
417	__le32 iv_len;
418	/* Length of source data */
419	__le32 src_data_len;
420	/* Length of destination data */
421	__le32 dst_data_len;
422	/* Starting point for cipher processing in source data */
423	__le32 cipher_start_src_offset;
424	/* Length of the source data that the cipher will be computed on */
425	__le32 len_to_cipher;
426	/* Starting point for hash processing in source data */
427	__le32 hash_start_src_offset;
428	/* Length of the source data that the hash will be computed on */
429	__le32 len_to_hash;
430	/* Length of the additional auth data */
431	__le32 aad_len;
432	/* Length of the hash result */
433	__le32 hash_result_len;
434	__le32 reserved;
435};
436
437struct virtio_crypto_alg_chain_data_req {
438	/* Device-readable part */
439	struct virtio_crypto_alg_chain_data_para para;
440};
441
442struct virtio_crypto_sym_data_req {
443	union {
444		struct virtio_crypto_cipher_data_req cipher;
445		struct virtio_crypto_alg_chain_data_req chain;
446		__u8 padding[40];
447	} u;
448
449	/* See above VIRTIO_CRYPTO_SYM_OP_* */
450	__le32 op_type;
451	__le32 padding;
452};
453
454struct virtio_crypto_aead_data_req {
455	/* Device-readable part */
456	struct virtio_crypto_aead_para para;
457	__u8 padding[32];
458};
459
460struct virtio_crypto_akcipher_para {
461	__le32 src_data_len;
462	__le32 dst_data_len;
463};
464
465struct virtio_crypto_akcipher_data_req {
466	struct virtio_crypto_akcipher_para para;
467	__u8 padding[40];
468};
469
470/* The request of the data virtqueue's packet */
471struct virtio_crypto_op_data_req {
472	struct virtio_crypto_op_header header;
473
474	union {
475		struct virtio_crypto_sym_data_req  sym_req;
476		struct virtio_crypto_hash_data_req hash_req;
477		struct virtio_crypto_mac_data_req mac_req;
478		struct virtio_crypto_aead_data_req aead_req;
479		struct virtio_crypto_akcipher_data_req akcipher_req;
480		__u8 padding[48];
481	} u;
482};
483
484#define VIRTIO_CRYPTO_OK        0
485#define VIRTIO_CRYPTO_ERR       1
486#define VIRTIO_CRYPTO_BADMSG    2
487#define VIRTIO_CRYPTO_NOTSUPP   3
488#define VIRTIO_CRYPTO_INVSESS   4 /* Invalid session id */
489#define VIRTIO_CRYPTO_NOSPC     5 /* no free session ID */
490#define VIRTIO_CRYPTO_KEY_REJECTED 6 /* Signature verification failed */
491
492/* The accelerator hardware is ready */
493#define VIRTIO_CRYPTO_S_HW_READY  (1 << 0)
494
495struct virtio_crypto_config {
496	/* See VIRTIO_CRYPTO_OP_* above */
497	__le32  status;
498
499	/*
500	 * Maximum number of data queue
501	 */
502	__le32  max_dataqueues;
503
504	/*
505	 * Specifies the services mask which the device support,
506	 * see VIRTIO_CRYPTO_SERVICE_* above
507	 */
508	__le32 crypto_services;
509
510	/* Detailed algorithms mask */
511	__le32 cipher_algo_l;
512	__le32 cipher_algo_h;
513	__le32 hash_algo;
514	__le32 mac_algo_l;
515	__le32 mac_algo_h;
516	__le32 aead_algo;
517	/* Maximum length of cipher key */
518	__le32 max_cipher_key_len;
519	/* Maximum length of authenticated key */
520	__le32 max_auth_key_len;
521	__le32 akcipher_algo;
522	/* Maximum size of each crypto request's content */
523	__le64 max_size;
524};
525
526struct virtio_crypto_inhdr {
527	/* See VIRTIO_CRYPTO_* above */
528	__u8 status;
529};
530#endif
531