1f43c0076SMichael S. Tsirkin #ifndef _VIRTIO_CRYPTO_H
2f43c0076SMichael S. Tsirkin #define _VIRTIO_CRYPTO_H
35551e3a8SGonglei /* This header is BSD licensed so anyone can use the definitions to implement
45551e3a8SGonglei  * compatible drivers/servers.
55551e3a8SGonglei  *
65551e3a8SGonglei  * Redistribution and use in source and binary forms, with or without
75551e3a8SGonglei  * modification, are permitted provided that the following conditions
85551e3a8SGonglei  * are met:
95551e3a8SGonglei  * 1. Redistributions of source code must retain the above copyright
105551e3a8SGonglei  *    notice, this list of conditions and the following disclaimer.
115551e3a8SGonglei  * 2. Redistributions in binary form must reproduce the above copyright
125551e3a8SGonglei  *    notice, this list of conditions and the following disclaimer in the
135551e3a8SGonglei  *    documentation and/or other materials provided with the distribution.
145551e3a8SGonglei  * 3. Neither the name of IBM nor the names of its contributors
155551e3a8SGonglei  *    may be used to endorse or promote products derived from this software
165551e3a8SGonglei  *    without specific prior written permission.
17f43c0076SMichael S. Tsirkin  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18f43c0076SMichael S. Tsirkin  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19f43c0076SMichael S. Tsirkin  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20f43c0076SMichael S. Tsirkin  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL IBM OR
21f43c0076SMichael S. Tsirkin  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22f43c0076SMichael S. Tsirkin  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23f43c0076SMichael S. Tsirkin  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24f43c0076SMichael S. Tsirkin  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25f43c0076SMichael S. Tsirkin  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26f43c0076SMichael S. Tsirkin  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
27f43c0076SMichael S. Tsirkin  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28f43c0076SMichael S. Tsirkin  * SUCH DAMAGE.
29f43c0076SMichael S. Tsirkin  */
305551e3a8SGonglei #include "standard-headers/linux/types.h"
315551e3a8SGonglei #include "standard-headers/linux/virtio_types.h"
32f43c0076SMichael S. Tsirkin #include "standard-headers/linux/virtio_ids.h"
33f43c0076SMichael S. Tsirkin #include "standard-headers/linux/virtio_config.h"
345551e3a8SGonglei 
355551e3a8SGonglei 
365551e3a8SGonglei #define VIRTIO_CRYPTO_SERVICE_CIPHER 0
375551e3a8SGonglei #define VIRTIO_CRYPTO_SERVICE_HASH   1
385551e3a8SGonglei #define VIRTIO_CRYPTO_SERVICE_MAC    2
395551e3a8SGonglei #define VIRTIO_CRYPTO_SERVICE_AEAD   3
40*e4082063SAlex Williamson #define VIRTIO_CRYPTO_SERVICE_AKCIPHER 4
415551e3a8SGonglei 
425551e3a8SGonglei #define VIRTIO_CRYPTO_OPCODE(service, op)   (((service) << 8) | (op))
435551e3a8SGonglei 
445551e3a8SGonglei struct virtio_crypto_ctrl_header {
455551e3a8SGonglei #define VIRTIO_CRYPTO_CIPHER_CREATE_SESSION \
465551e3a8SGonglei 	   VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_CIPHER, 0x02)
475551e3a8SGonglei #define VIRTIO_CRYPTO_CIPHER_DESTROY_SESSION \
485551e3a8SGonglei 	   VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_CIPHER, 0x03)
495551e3a8SGonglei #define VIRTIO_CRYPTO_HASH_CREATE_SESSION \
505551e3a8SGonglei 	   VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_HASH, 0x02)
515551e3a8SGonglei #define VIRTIO_CRYPTO_HASH_DESTROY_SESSION \
525551e3a8SGonglei 	   VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_HASH, 0x03)
535551e3a8SGonglei #define VIRTIO_CRYPTO_MAC_CREATE_SESSION \
545551e3a8SGonglei 	   VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_MAC, 0x02)
555551e3a8SGonglei #define VIRTIO_CRYPTO_MAC_DESTROY_SESSION \
565551e3a8SGonglei 	   VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_MAC, 0x03)
575551e3a8SGonglei #define VIRTIO_CRYPTO_AEAD_CREATE_SESSION \
585551e3a8SGonglei 	   VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_AEAD, 0x02)
595551e3a8SGonglei #define VIRTIO_CRYPTO_AEAD_DESTROY_SESSION \
605551e3a8SGonglei 	   VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_AEAD, 0x03)
61*e4082063SAlex Williamson #define VIRTIO_CRYPTO_AKCIPHER_CREATE_SESSION \
62*e4082063SAlex Williamson 	   VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_AKCIPHER, 0x04)
63*e4082063SAlex Williamson #define VIRTIO_CRYPTO_AKCIPHER_DESTROY_SESSION \
64*e4082063SAlex Williamson 	   VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_AKCIPHER, 0x05)
65f43c0076SMichael S. Tsirkin 	uint32_t opcode;
66f43c0076SMichael S. Tsirkin 	uint32_t algo;
67f43c0076SMichael S. Tsirkin 	uint32_t flag;
685551e3a8SGonglei 	/* data virtqueue id */
69f43c0076SMichael S. Tsirkin 	uint32_t queue_id;
705551e3a8SGonglei };
715551e3a8SGonglei 
725551e3a8SGonglei struct virtio_crypto_cipher_session_para {
735551e3a8SGonglei #define VIRTIO_CRYPTO_NO_CIPHER                 0
745551e3a8SGonglei #define VIRTIO_CRYPTO_CIPHER_ARC4               1
755551e3a8SGonglei #define VIRTIO_CRYPTO_CIPHER_AES_ECB            2
765551e3a8SGonglei #define VIRTIO_CRYPTO_CIPHER_AES_CBC            3
775551e3a8SGonglei #define VIRTIO_CRYPTO_CIPHER_AES_CTR            4
785551e3a8SGonglei #define VIRTIO_CRYPTO_CIPHER_DES_ECB            5
795551e3a8SGonglei #define VIRTIO_CRYPTO_CIPHER_DES_CBC            6
805551e3a8SGonglei #define VIRTIO_CRYPTO_CIPHER_3DES_ECB           7
815551e3a8SGonglei #define VIRTIO_CRYPTO_CIPHER_3DES_CBC           8
825551e3a8SGonglei #define VIRTIO_CRYPTO_CIPHER_3DES_CTR           9
835551e3a8SGonglei #define VIRTIO_CRYPTO_CIPHER_KASUMI_F8          10
845551e3a8SGonglei #define VIRTIO_CRYPTO_CIPHER_SNOW3G_UEA2        11
855551e3a8SGonglei #define VIRTIO_CRYPTO_CIPHER_AES_F8             12
865551e3a8SGonglei #define VIRTIO_CRYPTO_CIPHER_AES_XTS            13
875551e3a8SGonglei #define VIRTIO_CRYPTO_CIPHER_ZUC_EEA3           14
88f43c0076SMichael S. Tsirkin 	uint32_t algo;
895551e3a8SGonglei 	/* length of key */
90f43c0076SMichael S. Tsirkin 	uint32_t keylen;
915551e3a8SGonglei 
925551e3a8SGonglei #define VIRTIO_CRYPTO_OP_ENCRYPT  1
935551e3a8SGonglei #define VIRTIO_CRYPTO_OP_DECRYPT  2
945551e3a8SGonglei 	/* encrypt or decrypt */
95f43c0076SMichael S. Tsirkin 	uint32_t op;
96f43c0076SMichael S. Tsirkin 	uint32_t padding;
975551e3a8SGonglei };
985551e3a8SGonglei 
995551e3a8SGonglei struct virtio_crypto_session_input {
1005551e3a8SGonglei 	/* Device-writable part */
101f43c0076SMichael S. Tsirkin 	uint64_t session_id;
102f43c0076SMichael S. Tsirkin 	uint32_t status;
103f43c0076SMichael S. Tsirkin 	uint32_t padding;
1045551e3a8SGonglei };
1055551e3a8SGonglei 
1065551e3a8SGonglei struct virtio_crypto_cipher_session_req {
1075551e3a8SGonglei 	struct virtio_crypto_cipher_session_para para;
108f43c0076SMichael S. Tsirkin 	uint8_t padding[32];
1095551e3a8SGonglei };
1105551e3a8SGonglei 
1115551e3a8SGonglei struct virtio_crypto_hash_session_para {
1125551e3a8SGonglei #define VIRTIO_CRYPTO_NO_HASH            0
1135551e3a8SGonglei #define VIRTIO_CRYPTO_HASH_MD5           1
1145551e3a8SGonglei #define VIRTIO_CRYPTO_HASH_SHA1          2
1155551e3a8SGonglei #define VIRTIO_CRYPTO_HASH_SHA_224       3
1165551e3a8SGonglei #define VIRTIO_CRYPTO_HASH_SHA_256       4
1175551e3a8SGonglei #define VIRTIO_CRYPTO_HASH_SHA_384       5
1185551e3a8SGonglei #define VIRTIO_CRYPTO_HASH_SHA_512       6
1195551e3a8SGonglei #define VIRTIO_CRYPTO_HASH_SHA3_224      7
1205551e3a8SGonglei #define VIRTIO_CRYPTO_HASH_SHA3_256      8
1215551e3a8SGonglei #define VIRTIO_CRYPTO_HASH_SHA3_384      9
1225551e3a8SGonglei #define VIRTIO_CRYPTO_HASH_SHA3_512      10
1235551e3a8SGonglei #define VIRTIO_CRYPTO_HASH_SHA3_SHAKE128      11
1245551e3a8SGonglei #define VIRTIO_CRYPTO_HASH_SHA3_SHAKE256      12
125f43c0076SMichael S. Tsirkin 	uint32_t algo;
1265551e3a8SGonglei 	/* hash result length */
127f43c0076SMichael S. Tsirkin 	uint32_t hash_result_len;
128f43c0076SMichael S. Tsirkin 	uint8_t padding[8];
1295551e3a8SGonglei };
1305551e3a8SGonglei 
1315551e3a8SGonglei struct virtio_crypto_hash_create_session_req {
1325551e3a8SGonglei 	struct virtio_crypto_hash_session_para para;
133f43c0076SMichael S. Tsirkin 	uint8_t padding[40];
1345551e3a8SGonglei };
1355551e3a8SGonglei 
1365551e3a8SGonglei struct virtio_crypto_mac_session_para {
1375551e3a8SGonglei #define VIRTIO_CRYPTO_NO_MAC                       0
1385551e3a8SGonglei #define VIRTIO_CRYPTO_MAC_HMAC_MD5                 1
1395551e3a8SGonglei #define VIRTIO_CRYPTO_MAC_HMAC_SHA1                2
1405551e3a8SGonglei #define VIRTIO_CRYPTO_MAC_HMAC_SHA_224             3
1415551e3a8SGonglei #define VIRTIO_CRYPTO_MAC_HMAC_SHA_256             4
1425551e3a8SGonglei #define VIRTIO_CRYPTO_MAC_HMAC_SHA_384             5
1435551e3a8SGonglei #define VIRTIO_CRYPTO_MAC_HMAC_SHA_512             6
1445551e3a8SGonglei #define VIRTIO_CRYPTO_MAC_CMAC_3DES                25
1455551e3a8SGonglei #define VIRTIO_CRYPTO_MAC_CMAC_AES                 26
1465551e3a8SGonglei #define VIRTIO_CRYPTO_MAC_KASUMI_F9                27
1475551e3a8SGonglei #define VIRTIO_CRYPTO_MAC_SNOW3G_UIA2              28
1485551e3a8SGonglei #define VIRTIO_CRYPTO_MAC_GMAC_AES                 41
1495551e3a8SGonglei #define VIRTIO_CRYPTO_MAC_GMAC_TWOFISH             42
1505551e3a8SGonglei #define VIRTIO_CRYPTO_MAC_CBCMAC_AES               49
1515551e3a8SGonglei #define VIRTIO_CRYPTO_MAC_CBCMAC_KASUMI_F9         50
1525551e3a8SGonglei #define VIRTIO_CRYPTO_MAC_XCBC_AES                 53
153f43c0076SMichael S. Tsirkin 	uint32_t algo;
1545551e3a8SGonglei 	/* hash result length */
155f43c0076SMichael S. Tsirkin 	uint32_t hash_result_len;
1565551e3a8SGonglei 	/* length of authenticated key */
157f43c0076SMichael S. Tsirkin 	uint32_t auth_key_len;
158f43c0076SMichael S. Tsirkin 	uint32_t padding;
1595551e3a8SGonglei };
1605551e3a8SGonglei 
1615551e3a8SGonglei struct virtio_crypto_mac_create_session_req {
1625551e3a8SGonglei 	struct virtio_crypto_mac_session_para para;
163f43c0076SMichael S. Tsirkin 	uint8_t padding[40];
1645551e3a8SGonglei };
1655551e3a8SGonglei 
1665551e3a8SGonglei struct virtio_crypto_aead_session_para {
1675551e3a8SGonglei #define VIRTIO_CRYPTO_NO_AEAD     0
1685551e3a8SGonglei #define VIRTIO_CRYPTO_AEAD_GCM    1
1695551e3a8SGonglei #define VIRTIO_CRYPTO_AEAD_CCM    2
1705551e3a8SGonglei #define VIRTIO_CRYPTO_AEAD_CHACHA20_POLY1305  3
171f43c0076SMichael S. Tsirkin 	uint32_t algo;
1725551e3a8SGonglei 	/* length of key */
173f43c0076SMichael S. Tsirkin 	uint32_t key_len;
174f43c0076SMichael S. Tsirkin 	/* hash result length */
175f43c0076SMichael S. Tsirkin 	uint32_t hash_result_len;
1765551e3a8SGonglei 	/* length of the additional authenticated data (AAD) in bytes */
177f43c0076SMichael S. Tsirkin 	uint32_t aad_len;
1785551e3a8SGonglei 	/* encrypt or decrypt, See above VIRTIO_CRYPTO_OP_* */
179f43c0076SMichael S. Tsirkin 	uint32_t op;
180f43c0076SMichael S. Tsirkin 	uint32_t padding;
1815551e3a8SGonglei };
1825551e3a8SGonglei 
1835551e3a8SGonglei struct virtio_crypto_aead_create_session_req {
1845551e3a8SGonglei 	struct virtio_crypto_aead_session_para para;
185f43c0076SMichael S. Tsirkin 	uint8_t padding[32];
1865551e3a8SGonglei };
1875551e3a8SGonglei 
188*e4082063SAlex Williamson struct virtio_crypto_rsa_session_para {
189*e4082063SAlex Williamson #define VIRTIO_CRYPTO_RSA_RAW_PADDING   0
190*e4082063SAlex Williamson #define VIRTIO_CRYPTO_RSA_PKCS1_PADDING 1
191*e4082063SAlex Williamson 	uint32_t padding_algo;
192*e4082063SAlex Williamson 
193*e4082063SAlex Williamson #define VIRTIO_CRYPTO_RSA_NO_HASH   0
194*e4082063SAlex Williamson #define VIRTIO_CRYPTO_RSA_MD2       1
195*e4082063SAlex Williamson #define VIRTIO_CRYPTO_RSA_MD3       2
196*e4082063SAlex Williamson #define VIRTIO_CRYPTO_RSA_MD4       3
197*e4082063SAlex Williamson #define VIRTIO_CRYPTO_RSA_MD5       4
198*e4082063SAlex Williamson #define VIRTIO_CRYPTO_RSA_SHA1      5
199*e4082063SAlex Williamson #define VIRTIO_CRYPTO_RSA_SHA256    6
200*e4082063SAlex Williamson #define VIRTIO_CRYPTO_RSA_SHA384    7
201*e4082063SAlex Williamson #define VIRTIO_CRYPTO_RSA_SHA512    8
202*e4082063SAlex Williamson #define VIRTIO_CRYPTO_RSA_SHA224    9
203*e4082063SAlex Williamson 	uint32_t hash_algo;
204*e4082063SAlex Williamson };
205*e4082063SAlex Williamson 
206*e4082063SAlex Williamson struct virtio_crypto_ecdsa_session_para {
207*e4082063SAlex Williamson #define VIRTIO_CRYPTO_CURVE_UNKNOWN   0
208*e4082063SAlex Williamson #define VIRTIO_CRYPTO_CURVE_NIST_P192 1
209*e4082063SAlex Williamson #define VIRTIO_CRYPTO_CURVE_NIST_P224 2
210*e4082063SAlex Williamson #define VIRTIO_CRYPTO_CURVE_NIST_P256 3
211*e4082063SAlex Williamson #define VIRTIO_CRYPTO_CURVE_NIST_P384 4
212*e4082063SAlex Williamson #define VIRTIO_CRYPTO_CURVE_NIST_P521 5
213*e4082063SAlex Williamson 	uint32_t curve_id;
214*e4082063SAlex Williamson 	uint32_t padding;
215*e4082063SAlex Williamson };
216*e4082063SAlex Williamson 
217*e4082063SAlex Williamson struct virtio_crypto_akcipher_session_para {
218*e4082063SAlex Williamson #define VIRTIO_CRYPTO_NO_AKCIPHER    0
219*e4082063SAlex Williamson #define VIRTIO_CRYPTO_AKCIPHER_RSA   1
220*e4082063SAlex Williamson #define VIRTIO_CRYPTO_AKCIPHER_DSA   2
221*e4082063SAlex Williamson #define VIRTIO_CRYPTO_AKCIPHER_ECDSA 3
222*e4082063SAlex Williamson 	uint32_t algo;
223*e4082063SAlex Williamson 
224*e4082063SAlex Williamson #define VIRTIO_CRYPTO_AKCIPHER_KEY_TYPE_PUBLIC  1
225*e4082063SAlex Williamson #define VIRTIO_CRYPTO_AKCIPHER_KEY_TYPE_PRIVATE 2
226*e4082063SAlex Williamson 	uint32_t keytype;
227*e4082063SAlex Williamson 	uint32_t keylen;
228*e4082063SAlex Williamson 
229*e4082063SAlex Williamson 	union {
230*e4082063SAlex Williamson 		struct virtio_crypto_rsa_session_para rsa;
231*e4082063SAlex Williamson 		struct virtio_crypto_ecdsa_session_para ecdsa;
232*e4082063SAlex Williamson 	} u;
233*e4082063SAlex Williamson };
234*e4082063SAlex Williamson 
235*e4082063SAlex Williamson struct virtio_crypto_akcipher_create_session_req {
236*e4082063SAlex Williamson 	struct virtio_crypto_akcipher_session_para para;
237*e4082063SAlex Williamson 	uint8_t padding[36];
238*e4082063SAlex Williamson };
239*e4082063SAlex Williamson 
2405551e3a8SGonglei struct virtio_crypto_alg_chain_session_para {
2415551e3a8SGonglei #define VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_HASH_THEN_CIPHER  1
2425551e3a8SGonglei #define VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_CIPHER_THEN_HASH  2
243f43c0076SMichael S. Tsirkin 	uint32_t alg_chain_order;
2445551e3a8SGonglei /* Plain hash */
2455551e3a8SGonglei #define VIRTIO_CRYPTO_SYM_HASH_MODE_PLAIN    1
2465551e3a8SGonglei /* Authenticated hash (mac) */
2475551e3a8SGonglei #define VIRTIO_CRYPTO_SYM_HASH_MODE_AUTH     2
2485551e3a8SGonglei /* Nested hash */
2495551e3a8SGonglei #define VIRTIO_CRYPTO_SYM_HASH_MODE_NESTED   3
250f43c0076SMichael S. Tsirkin 	uint32_t hash_mode;
2515551e3a8SGonglei 	struct virtio_crypto_cipher_session_para cipher_param;
2525551e3a8SGonglei 	union {
2535551e3a8SGonglei 		struct virtio_crypto_hash_session_para hash_param;
2545551e3a8SGonglei 		struct virtio_crypto_mac_session_para mac_param;
255f43c0076SMichael S. Tsirkin 		uint8_t padding[16];
2565551e3a8SGonglei 	} u;
2575551e3a8SGonglei 	/* length of the additional authenticated data (AAD) in bytes */
258f43c0076SMichael S. Tsirkin 	uint32_t aad_len;
259f43c0076SMichael S. Tsirkin 	uint32_t padding;
2605551e3a8SGonglei };
2615551e3a8SGonglei 
2625551e3a8SGonglei struct virtio_crypto_alg_chain_session_req {
2635551e3a8SGonglei 	struct virtio_crypto_alg_chain_session_para para;
2645551e3a8SGonglei };
2655551e3a8SGonglei 
2665551e3a8SGonglei struct virtio_crypto_sym_create_session_req {
2675551e3a8SGonglei 	union {
2685551e3a8SGonglei 		struct virtio_crypto_cipher_session_req cipher;
2695551e3a8SGonglei 		struct virtio_crypto_alg_chain_session_req chain;
270f43c0076SMichael S. Tsirkin 		uint8_t padding[48];
2715551e3a8SGonglei 	} u;
2725551e3a8SGonglei 
2735551e3a8SGonglei 	/* Device-readable part */
2745551e3a8SGonglei 
2755551e3a8SGonglei /* No operation */
2765551e3a8SGonglei #define VIRTIO_CRYPTO_SYM_OP_NONE  0
2775551e3a8SGonglei /* Cipher only operation on the data */
2785551e3a8SGonglei #define VIRTIO_CRYPTO_SYM_OP_CIPHER  1
279f43c0076SMichael S. Tsirkin /*
280f43c0076SMichael S. Tsirkin  * Chain any cipher with any hash or mac operation. The order
281f43c0076SMichael S. Tsirkin  * depends on the value of alg_chain_order param
282f43c0076SMichael S. Tsirkin  */
2835551e3a8SGonglei #define VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING  2
284f43c0076SMichael S. Tsirkin 	uint32_t op_type;
285f43c0076SMichael S. Tsirkin 	uint32_t padding;
2865551e3a8SGonglei };
2875551e3a8SGonglei 
2885551e3a8SGonglei struct virtio_crypto_destroy_session_req {
2895551e3a8SGonglei 	/* Device-readable part */
290f43c0076SMichael S. Tsirkin 	uint64_t  session_id;
291f43c0076SMichael S. Tsirkin 	uint8_t padding[48];
2925551e3a8SGonglei };
2935551e3a8SGonglei 
294f43c0076SMichael S. Tsirkin /* The request of the control virtqueue's packet */
2955551e3a8SGonglei struct virtio_crypto_op_ctrl_req {
2965551e3a8SGonglei 	struct virtio_crypto_ctrl_header header;
2975551e3a8SGonglei 
2985551e3a8SGonglei 	union {
299f43c0076SMichael S. Tsirkin 		struct virtio_crypto_sym_create_session_req
300f43c0076SMichael S. Tsirkin 			sym_create_session;
301f43c0076SMichael S. Tsirkin 		struct virtio_crypto_hash_create_session_req
302f43c0076SMichael S. Tsirkin 			hash_create_session;
303f43c0076SMichael S. Tsirkin 		struct virtio_crypto_mac_create_session_req
304f43c0076SMichael S. Tsirkin 			mac_create_session;
305f43c0076SMichael S. Tsirkin 		struct virtio_crypto_aead_create_session_req
306f43c0076SMichael S. Tsirkin 			aead_create_session;
307*e4082063SAlex Williamson 		struct virtio_crypto_akcipher_create_session_req
308*e4082063SAlex Williamson 			akcipher_create_session;
309f43c0076SMichael S. Tsirkin 		struct virtio_crypto_destroy_session_req
310f43c0076SMichael S. Tsirkin 			destroy_session;
311f43c0076SMichael S. Tsirkin 		uint8_t padding[56];
3125551e3a8SGonglei 	} u;
3135551e3a8SGonglei };
3145551e3a8SGonglei 
3155551e3a8SGonglei struct virtio_crypto_op_header {
3165551e3a8SGonglei #define VIRTIO_CRYPTO_CIPHER_ENCRYPT \
3175551e3a8SGonglei 	VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_CIPHER, 0x00)
3185551e3a8SGonglei #define VIRTIO_CRYPTO_CIPHER_DECRYPT \
3195551e3a8SGonglei 	VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_CIPHER, 0x01)
3205551e3a8SGonglei #define VIRTIO_CRYPTO_HASH \
3215551e3a8SGonglei 	VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_HASH, 0x00)
3225551e3a8SGonglei #define VIRTIO_CRYPTO_MAC \
3235551e3a8SGonglei 	VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_MAC, 0x00)
3245551e3a8SGonglei #define VIRTIO_CRYPTO_AEAD_ENCRYPT \
3255551e3a8SGonglei 	VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_AEAD, 0x00)
3265551e3a8SGonglei #define VIRTIO_CRYPTO_AEAD_DECRYPT \
3275551e3a8SGonglei 	VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_AEAD, 0x01)
328*e4082063SAlex Williamson #define VIRTIO_CRYPTO_AKCIPHER_ENCRYPT \
329*e4082063SAlex Williamson 	VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_AKCIPHER, 0x00)
330*e4082063SAlex Williamson #define VIRTIO_CRYPTO_AKCIPHER_DECRYPT \
331*e4082063SAlex Williamson 	VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_AKCIPHER, 0x01)
332*e4082063SAlex Williamson #define VIRTIO_CRYPTO_AKCIPHER_SIGN \
333*e4082063SAlex Williamson 	VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_AKCIPHER, 0x02)
334*e4082063SAlex Williamson #define VIRTIO_CRYPTO_AKCIPHER_VERIFY \
335*e4082063SAlex Williamson 	VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_AKCIPHER, 0x03)
336f43c0076SMichael S. Tsirkin 	uint32_t opcode;
3375551e3a8SGonglei 	/* algo should be service-specific algorithms */
338f43c0076SMichael S. Tsirkin 	uint32_t algo;
3395551e3a8SGonglei 	/* session_id should be service-specific algorithms */
340f43c0076SMichael S. Tsirkin 	uint64_t session_id;
3415551e3a8SGonglei 	/* control flag to control the request */
342f43c0076SMichael S. Tsirkin 	uint32_t flag;
343f43c0076SMichael S. Tsirkin 	uint32_t padding;
3445551e3a8SGonglei };
3455551e3a8SGonglei 
3465551e3a8SGonglei struct virtio_crypto_cipher_para {
3475551e3a8SGonglei 	/*
3485551e3a8SGonglei 	 * Byte Length of valid IV/Counter
3495551e3a8SGonglei 	 *
350f43c0076SMichael S. Tsirkin 	 * For block ciphers in CBC or F8 mode, or for Kasumi in F8 mode, or for
3515551e3a8SGonglei 	 *   SNOW3G in UEA2 mode, this is the length of the IV (which
3525551e3a8SGonglei 	 *   must be the same as the block length of the cipher).
353f43c0076SMichael S. Tsirkin 	 * For block ciphers in CTR mode, this is the length of the counter
3545551e3a8SGonglei 	 *   (which must be the same as the block length of the cipher).
355f43c0076SMichael S. Tsirkin 	 * For AES-XTS, this is the 128bit tweak, i, from IEEE Std 1619-2007.
3565551e3a8SGonglei 	 *
3575551e3a8SGonglei 	 * The IV/Counter will be updated after every partial cryptographic
3585551e3a8SGonglei 	 * operation.
3595551e3a8SGonglei 	 */
360f43c0076SMichael S. Tsirkin 	uint32_t iv_len;
3615551e3a8SGonglei 	/* length of source data */
362f43c0076SMichael S. Tsirkin 	uint32_t src_data_len;
3635551e3a8SGonglei 	/* length of dst data */
364f43c0076SMichael S. Tsirkin 	uint32_t dst_data_len;
365f43c0076SMichael S. Tsirkin 	uint32_t padding;
3665551e3a8SGonglei };
3675551e3a8SGonglei 
3685551e3a8SGonglei struct virtio_crypto_hash_para {
3695551e3a8SGonglei 	/* length of source data */
370f43c0076SMichael S. Tsirkin 	uint32_t src_data_len;
3715551e3a8SGonglei 	/* hash result length */
372f43c0076SMichael S. Tsirkin 	uint32_t hash_result_len;
3735551e3a8SGonglei };
3745551e3a8SGonglei 
3755551e3a8SGonglei struct virtio_crypto_mac_para {
3765551e3a8SGonglei 	struct virtio_crypto_hash_para hash;
3775551e3a8SGonglei };
3785551e3a8SGonglei 
3795551e3a8SGonglei struct virtio_crypto_aead_para {
3805551e3a8SGonglei 	/*
3815551e3a8SGonglei 	 * Byte Length of valid IV data pointed to by the below iv_addr
3825551e3a8SGonglei 	 * parameter.
3835551e3a8SGonglei 	 *
384f43c0076SMichael S. Tsirkin 	 * For GCM mode, this is either 12 (for 96-bit IVs) or 16, in which
3855551e3a8SGonglei 	 *   case iv_addr points to J0.
386f43c0076SMichael S. Tsirkin 	 * For CCM mode, this is the length of the nonce, which can be in the
3875551e3a8SGonglei 	 *   range 7 to 13 inclusive.
3885551e3a8SGonglei 	 */
389f43c0076SMichael S. Tsirkin 	uint32_t iv_len;
3905551e3a8SGonglei 	/* length of additional auth data */
391f43c0076SMichael S. Tsirkin 	uint32_t aad_len;
3925551e3a8SGonglei 	/* length of source data */
393f43c0076SMichael S. Tsirkin 	uint32_t src_data_len;
3945551e3a8SGonglei 	/* length of dst data */
395f43c0076SMichael S. Tsirkin 	uint32_t dst_data_len;
3965551e3a8SGonglei };
3975551e3a8SGonglei 
3985551e3a8SGonglei struct virtio_crypto_cipher_data_req {
3995551e3a8SGonglei 	/* Device-readable part */
4005551e3a8SGonglei 	struct virtio_crypto_cipher_para para;
401f43c0076SMichael S. Tsirkin 	uint8_t padding[24];
4025551e3a8SGonglei };
4035551e3a8SGonglei 
4045551e3a8SGonglei struct virtio_crypto_hash_data_req {
4055551e3a8SGonglei 	/* Device-readable part */
4065551e3a8SGonglei 	struct virtio_crypto_hash_para para;
407f43c0076SMichael S. Tsirkin 	uint8_t padding[40];
4085551e3a8SGonglei };
4095551e3a8SGonglei 
4105551e3a8SGonglei struct virtio_crypto_mac_data_req {
4115551e3a8SGonglei 	/* Device-readable part */
4125551e3a8SGonglei 	struct virtio_crypto_mac_para para;
413f43c0076SMichael S. Tsirkin 	uint8_t padding[40];
4145551e3a8SGonglei };
4155551e3a8SGonglei 
4165551e3a8SGonglei struct virtio_crypto_alg_chain_data_para {
417f43c0076SMichael S. Tsirkin 	uint32_t iv_len;
4185551e3a8SGonglei 	/* Length of source data */
419f43c0076SMichael S. Tsirkin 	uint32_t src_data_len;
4205551e3a8SGonglei 	/* Length of destination data */
421f43c0076SMichael S. Tsirkin 	uint32_t dst_data_len;
4225551e3a8SGonglei 	/* Starting point for cipher processing in source data */
423f43c0076SMichael S. Tsirkin 	uint32_t cipher_start_src_offset;
4245551e3a8SGonglei 	/* Length of the source data that the cipher will be computed on */
425f43c0076SMichael S. Tsirkin 	uint32_t len_to_cipher;
4265551e3a8SGonglei 	/* Starting point for hash processing in source data */
427f43c0076SMichael S. Tsirkin 	uint32_t hash_start_src_offset;
4285551e3a8SGonglei 	/* Length of the source data that the hash will be computed on */
429f43c0076SMichael S. Tsirkin 	uint32_t len_to_hash;
4305551e3a8SGonglei 	/* Length of the additional auth data */
431f43c0076SMichael S. Tsirkin 	uint32_t aad_len;
4325551e3a8SGonglei 	/* Length of the hash result */
433f43c0076SMichael S. Tsirkin 	uint32_t hash_result_len;
434f43c0076SMichael S. Tsirkin 	uint32_t reserved;
4355551e3a8SGonglei };
4365551e3a8SGonglei 
4375551e3a8SGonglei struct virtio_crypto_alg_chain_data_req {
4385551e3a8SGonglei 	/* Device-readable part */
4395551e3a8SGonglei 	struct virtio_crypto_alg_chain_data_para para;
4405551e3a8SGonglei };
4415551e3a8SGonglei 
4425551e3a8SGonglei struct virtio_crypto_sym_data_req {
4435551e3a8SGonglei 	union {
4445551e3a8SGonglei 		struct virtio_crypto_cipher_data_req cipher;
4455551e3a8SGonglei 		struct virtio_crypto_alg_chain_data_req chain;
446f43c0076SMichael S. Tsirkin 		uint8_t padding[40];
4475551e3a8SGonglei 	} u;
4485551e3a8SGonglei 
4495551e3a8SGonglei 	/* See above VIRTIO_CRYPTO_SYM_OP_* */
450f43c0076SMichael S. Tsirkin 	uint32_t op_type;
451f43c0076SMichael S. Tsirkin 	uint32_t padding;
4525551e3a8SGonglei };
4535551e3a8SGonglei 
4545551e3a8SGonglei struct virtio_crypto_aead_data_req {
4555551e3a8SGonglei 	/* Device-readable part */
4565551e3a8SGonglei 	struct virtio_crypto_aead_para para;
457f43c0076SMichael S. Tsirkin 	uint8_t padding[32];
4585551e3a8SGonglei };
4595551e3a8SGonglei 
460*e4082063SAlex Williamson struct virtio_crypto_akcipher_para {
461*e4082063SAlex Williamson 	uint32_t src_data_len;
462*e4082063SAlex Williamson 	uint32_t dst_data_len;
463*e4082063SAlex Williamson };
464*e4082063SAlex Williamson 
465*e4082063SAlex Williamson struct virtio_crypto_akcipher_data_req {
466*e4082063SAlex Williamson 	struct virtio_crypto_akcipher_para para;
467*e4082063SAlex Williamson 	uint8_t padding[40];
468*e4082063SAlex Williamson };
469*e4082063SAlex Williamson 
470f43c0076SMichael S. Tsirkin /* The request of the data virtqueue's packet */
4715551e3a8SGonglei struct virtio_crypto_op_data_req {
4725551e3a8SGonglei 	struct virtio_crypto_op_header header;
4735551e3a8SGonglei 
4745551e3a8SGonglei 	union {
4755551e3a8SGonglei 		struct virtio_crypto_sym_data_req  sym_req;
4765551e3a8SGonglei 		struct virtio_crypto_hash_data_req hash_req;
4775551e3a8SGonglei 		struct virtio_crypto_mac_data_req mac_req;
4785551e3a8SGonglei 		struct virtio_crypto_aead_data_req aead_req;
479*e4082063SAlex Williamson 		struct virtio_crypto_akcipher_data_req akcipher_req;
480f43c0076SMichael S. Tsirkin 		uint8_t padding[48];
4815551e3a8SGonglei 	} u;
4825551e3a8SGonglei };
4835551e3a8SGonglei 
4845551e3a8SGonglei #define VIRTIO_CRYPTO_OK        0
4855551e3a8SGonglei #define VIRTIO_CRYPTO_ERR       1
4865551e3a8SGonglei #define VIRTIO_CRYPTO_BADMSG    2
4875551e3a8SGonglei #define VIRTIO_CRYPTO_NOTSUPP   3
488f43c0076SMichael S. Tsirkin #define VIRTIO_CRYPTO_INVSESS   4 /* Invalid session id */
489*e4082063SAlex Williamson #define VIRTIO_CRYPTO_NOSPC     5 /* no free session ID */
490*e4082063SAlex Williamson #define VIRTIO_CRYPTO_KEY_REJECTED 6 /* Signature verification failed */
4915551e3a8SGonglei 
4925551e3a8SGonglei /* The accelerator hardware is ready */
4935551e3a8SGonglei #define VIRTIO_CRYPTO_S_HW_READY  (1 << 0)
4945551e3a8SGonglei 
4955551e3a8SGonglei struct virtio_crypto_config {
496f43c0076SMichael S. Tsirkin 	/* See VIRTIO_CRYPTO_OP_* above */
497f43c0076SMichael S. Tsirkin 	uint32_t  status;
4985551e3a8SGonglei 
4995551e3a8SGonglei 	/*
500f43c0076SMichael S. Tsirkin 	 * Maximum number of data queue
5015551e3a8SGonglei 	 */
502f43c0076SMichael S. Tsirkin 	uint32_t  max_dataqueues;
5035551e3a8SGonglei 
504f43c0076SMichael S. Tsirkin 	/*
505f43c0076SMichael S. Tsirkin 	 * Specifies the services mask which the device support,
506f43c0076SMichael S. Tsirkin 	 * see VIRTIO_CRYPTO_SERVICE_* above
507f43c0076SMichael S. Tsirkin 	 */
508f43c0076SMichael S. Tsirkin 	uint32_t crypto_services;
5095551e3a8SGonglei 
5105551e3a8SGonglei 	/* Detailed algorithms mask */
511f43c0076SMichael S. Tsirkin 	uint32_t cipher_algo_l;
512f43c0076SMichael S. Tsirkin 	uint32_t cipher_algo_h;
513f43c0076SMichael S. Tsirkin 	uint32_t hash_algo;
514f43c0076SMichael S. Tsirkin 	uint32_t mac_algo_l;
515f43c0076SMichael S. Tsirkin 	uint32_t mac_algo_h;
516f43c0076SMichael S. Tsirkin 	uint32_t aead_algo;
5175551e3a8SGonglei 	/* Maximum length of cipher key */
5185551e3a8SGonglei 	uint32_t max_cipher_key_len;
5195551e3a8SGonglei 	/* Maximum length of authenticated key */
5205551e3a8SGonglei 	uint32_t max_auth_key_len;
521*e4082063SAlex Williamson 	uint32_t akcipher_algo;
522f43c0076SMichael S. Tsirkin 	/* Maximum size of each crypto request's content */
523f43c0076SMichael S. Tsirkin 	uint64_t max_size;
5245551e3a8SGonglei };
5255551e3a8SGonglei 
5265551e3a8SGonglei struct virtio_crypto_inhdr {
5275551e3a8SGonglei 	/* See VIRTIO_CRYPTO_* above */
5285551e3a8SGonglei 	uint8_t status;
5295551e3a8SGonglei };
530f43c0076SMichael S. Tsirkin #endif
531