master
  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	/* akcipher sign/verify opcodes are deprecated */
333#define VIRTIO_CRYPTO_AKCIPHER_SIGN \
334	VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_AKCIPHER, 0x02)
335#define VIRTIO_CRYPTO_AKCIPHER_VERIFY \
336	VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_AKCIPHER, 0x03)
337	__le32 opcode;
338	/* algo should be service-specific algorithms */
339	__le32 algo;
340	/* session_id should be service-specific algorithms */
341	__le64 session_id;
342	/* control flag to control the request */
343	__le32 flag;
344	__le32 padding;
345};
346
347struct virtio_crypto_cipher_para {
348	/*
349	 * Byte Length of valid IV/Counter
350	 *
351	 * For block ciphers in CBC or F8 mode, or for Kasumi in F8 mode, or for
352	 *   SNOW3G in UEA2 mode, this is the length of the IV (which
353	 *   must be the same as the block length of the cipher).
354	 * For block ciphers in CTR mode, this is the length of the counter
355	 *   (which must be the same as the block length of the cipher).
356	 * For AES-XTS, this is the 128bit tweak, i, from IEEE Std 1619-2007.
357	 *
358	 * The IV/Counter will be updated after every partial cryptographic
359	 * operation.
360	 */
361	__le32 iv_len;
362	/* length of source data */
363	__le32 src_data_len;
364	/* length of dst data */
365	__le32 dst_data_len;
366	__le32 padding;
367};
368
369struct virtio_crypto_hash_para {
370	/* length of source data */
371	__le32 src_data_len;
372	/* hash result length */
373	__le32 hash_result_len;
374};
375
376struct virtio_crypto_mac_para {
377	struct virtio_crypto_hash_para hash;
378};
379
380struct virtio_crypto_aead_para {
381	/*
382	 * Byte Length of valid IV data pointed to by the below iv_addr
383	 * parameter.
384	 *
385	 * For GCM mode, this is either 12 (for 96-bit IVs) or 16, in which
386	 *   case iv_addr points to J0.
387	 * For CCM mode, this is the length of the nonce, which can be in the
388	 *   range 7 to 13 inclusive.
389	 */
390	__le32 iv_len;
391	/* length of additional auth data */
392	__le32 aad_len;
393	/* length of source data */
394	__le32 src_data_len;
395	/* length of dst data */
396	__le32 dst_data_len;
397};
398
399struct virtio_crypto_cipher_data_req {
400	/* Device-readable part */
401	struct virtio_crypto_cipher_para para;
402	__u8 padding[24];
403};
404
405struct virtio_crypto_hash_data_req {
406	/* Device-readable part */
407	struct virtio_crypto_hash_para para;
408	__u8 padding[40];
409};
410
411struct virtio_crypto_mac_data_req {
412	/* Device-readable part */
413	struct virtio_crypto_mac_para para;
414	__u8 padding[40];
415};
416
417struct virtio_crypto_alg_chain_data_para {
418	__le32 iv_len;
419	/* Length of source data */
420	__le32 src_data_len;
421	/* Length of destination data */
422	__le32 dst_data_len;
423	/* Starting point for cipher processing in source data */
424	__le32 cipher_start_src_offset;
425	/* Length of the source data that the cipher will be computed on */
426	__le32 len_to_cipher;
427	/* Starting point for hash processing in source data */
428	__le32 hash_start_src_offset;
429	/* Length of the source data that the hash will be computed on */
430	__le32 len_to_hash;
431	/* Length of the additional auth data */
432	__le32 aad_len;
433	/* Length of the hash result */
434	__le32 hash_result_len;
435	__le32 reserved;
436};
437
438struct virtio_crypto_alg_chain_data_req {
439	/* Device-readable part */
440	struct virtio_crypto_alg_chain_data_para para;
441};
442
443struct virtio_crypto_sym_data_req {
444	union {
445		struct virtio_crypto_cipher_data_req cipher;
446		struct virtio_crypto_alg_chain_data_req chain;
447		__u8 padding[40];
448	} u;
449
450	/* See above VIRTIO_CRYPTO_SYM_OP_* */
451	__le32 op_type;
452	__le32 padding;
453};
454
455struct virtio_crypto_aead_data_req {
456	/* Device-readable part */
457	struct virtio_crypto_aead_para para;
458	__u8 padding[32];
459};
460
461struct virtio_crypto_akcipher_para {
462	__le32 src_data_len;
463	__le32 dst_data_len;
464};
465
466struct virtio_crypto_akcipher_data_req {
467	struct virtio_crypto_akcipher_para para;
468	__u8 padding[40];
469};
470
471/* The request of the data virtqueue's packet */
472struct virtio_crypto_op_data_req {
473	struct virtio_crypto_op_header header;
474
475	union {
476		struct virtio_crypto_sym_data_req  sym_req;
477		struct virtio_crypto_hash_data_req hash_req;
478		struct virtio_crypto_mac_data_req mac_req;
479		struct virtio_crypto_aead_data_req aead_req;
480		struct virtio_crypto_akcipher_data_req akcipher_req;
481		__u8 padding[48];
482	} u;
483};
484
485#define VIRTIO_CRYPTO_OK        0
486#define VIRTIO_CRYPTO_ERR       1
487#define VIRTIO_CRYPTO_BADMSG    2
488#define VIRTIO_CRYPTO_NOTSUPP   3
489#define VIRTIO_CRYPTO_INVSESS   4 /* Invalid session id */
490#define VIRTIO_CRYPTO_NOSPC     5 /* no free session ID */
491#define VIRTIO_CRYPTO_KEY_REJECTED 6 /* Signature verification failed */
492
493/* The accelerator hardware is ready */
494#define VIRTIO_CRYPTO_S_HW_READY  (1 << 0)
495
496struct virtio_crypto_config {
497	/* See VIRTIO_CRYPTO_OP_* above */
498	__le32  status;
499
500	/*
501	 * Maximum number of data queue
502	 */
503	__le32  max_dataqueues;
504
505	/*
506	 * Specifies the services mask which the device support,
507	 * see VIRTIO_CRYPTO_SERVICE_* above
508	 */
509	__le32 crypto_services;
510
511	/* Detailed algorithms mask */
512	__le32 cipher_algo_l;
513	__le32 cipher_algo_h;
514	__le32 hash_algo;
515	__le32 mac_algo_l;
516	__le32 mac_algo_h;
517	__le32 aead_algo;
518	/* Maximum length of cipher key */
519	__le32 max_cipher_key_len;
520	/* Maximum length of authenticated key */
521	__le32 max_auth_key_len;
522	__le32 akcipher_algo;
523	/* Maximum size of each crypto request's content */
524	__le64 max_size;
525};
526
527struct virtio_crypto_inhdr {
528	/* See VIRTIO_CRYPTO_* above */
529	__u8 status;
530};
531#endif