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.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
30 #include <linux/types.h>
31 #include <linux/virtio_types.h>
32 #include <linux/virtio_ids.h>
33 #include <linux/virtio_config.h>
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
42 #define VIRTIO_CRYPTO_OPCODE(service, op) (((service) << 8) | (op))
44 struct 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)
68 /* data virtqueue id */
72 struct 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
92 #define VIRTIO_CRYPTO_OP_ENCRYPT 1
93 #define VIRTIO_CRYPTO_OP_DECRYPT 2
94 /* encrypt or decrypt */
99 struct virtio_crypto_session_input
{
100 /* Device-writable part */
106 struct virtio_crypto_cipher_session_req
{
107 struct virtio_crypto_cipher_session_para para
;
111 struct 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
126 /* hash result length */
127 __le32 hash_result_len
;
131 struct virtio_crypto_hash_create_session_req
{
132 struct virtio_crypto_hash_session_para para
;
136 struct 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
154 /* hash result length */
155 __le32 hash_result_len
;
156 /* length of authenticated key */
161 struct virtio_crypto_mac_create_session_req
{
162 struct virtio_crypto_mac_session_para para
;
166 struct 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
174 /* hash result length */
175 __le32 hash_result_len
;
176 /* length of the additional authenticated data (AAD) in bytes */
178 /* encrypt or decrypt, See above VIRTIO_CRYPTO_OP_* */
183 struct virtio_crypto_aead_create_session_req
{
184 struct virtio_crypto_aead_session_para para
;
188 struct virtio_crypto_rsa_session_para
{
189 #define VIRTIO_CRYPTO_RSA_RAW_PADDING 0
190 #define VIRTIO_CRYPTO_RSA_PKCS1_PADDING 1
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
206 struct 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
217 struct 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
224 #define VIRTIO_CRYPTO_AKCIPHER_KEY_TYPE_PUBLIC 1
225 #define VIRTIO_CRYPTO_AKCIPHER_KEY_TYPE_PRIVATE 2
230 struct virtio_crypto_rsa_session_para rsa
;
231 struct virtio_crypto_ecdsa_session_para ecdsa
;
235 struct virtio_crypto_akcipher_create_session_req
{
236 struct virtio_crypto_akcipher_session_para para
;
240 struct 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
;
245 #define VIRTIO_CRYPTO_SYM_HASH_MODE_PLAIN 1
246 /* Authenticated hash (mac) */
247 #define VIRTIO_CRYPTO_SYM_HASH_MODE_AUTH 2
249 #define VIRTIO_CRYPTO_SYM_HASH_MODE_NESTED 3
251 struct virtio_crypto_cipher_session_para cipher_param
;
253 struct virtio_crypto_hash_session_para hash_param
;
254 struct virtio_crypto_mac_session_para mac_param
;
257 /* length of the additional authenticated data (AAD) in bytes */
262 struct virtio_crypto_alg_chain_session_req
{
263 struct virtio_crypto_alg_chain_session_para para
;
266 struct virtio_crypto_sym_create_session_req
{
268 struct virtio_crypto_cipher_session_req cipher
;
269 struct virtio_crypto_alg_chain_session_req chain
;
273 /* Device-readable part */
276 #define VIRTIO_CRYPTO_SYM_OP_NONE 0
277 /* Cipher only operation on the data */
278 #define VIRTIO_CRYPTO_SYM_OP_CIPHER 1
280 * Chain any cipher with any hash or mac operation. The order
281 * depends on the value of alg_chain_order param
283 #define VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING 2
288 struct virtio_crypto_destroy_session_req
{
289 /* Device-readable part */
294 /* The request of the control virtqueue's packet */
295 struct virtio_crypto_op_ctrl_req
{
296 struct virtio_crypto_ctrl_header header
;
299 struct virtio_crypto_sym_create_session_req
301 struct virtio_crypto_hash_create_session_req
303 struct virtio_crypto_mac_create_session_req
305 struct virtio_crypto_aead_create_session_req
307 struct virtio_crypto_akcipher_create_session_req
308 akcipher_create_session
;
309 struct virtio_crypto_destroy_session_req
315 struct 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)
338 /* algo should be service-specific algorithms */
340 /* session_id should be service-specific algorithms */
342 /* control flag to control the request */
347 struct virtio_crypto_cipher_para
{
349 * Byte Length of valid IV/Counter
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.
358 * The IV/Counter will be updated after every partial cryptographic
362 /* length of source data */
364 /* length of dst data */
369 struct virtio_crypto_hash_para
{
370 /* length of source data */
372 /* hash result length */
373 __le32 hash_result_len
;
376 struct virtio_crypto_mac_para
{
377 struct virtio_crypto_hash_para hash
;
380 struct virtio_crypto_aead_para
{
382 * Byte Length of valid IV data pointed to by the below iv_addr
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.
391 /* length of additional auth data */
393 /* length of source data */
395 /* length of dst data */
399 struct virtio_crypto_cipher_data_req
{
400 /* Device-readable part */
401 struct virtio_crypto_cipher_para para
;
405 struct virtio_crypto_hash_data_req
{
406 /* Device-readable part */
407 struct virtio_crypto_hash_para para
;
411 struct virtio_crypto_mac_data_req
{
412 /* Device-readable part */
413 struct virtio_crypto_mac_para para
;
417 struct virtio_crypto_alg_chain_data_para
{
419 /* Length of source data */
421 /* Length of destination data */
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 */
431 /* Length of the additional auth data */
433 /* Length of the hash result */
434 __le32 hash_result_len
;
438 struct virtio_crypto_alg_chain_data_req
{
439 /* Device-readable part */
440 struct virtio_crypto_alg_chain_data_para para
;
443 struct virtio_crypto_sym_data_req
{
445 struct virtio_crypto_cipher_data_req cipher
;
446 struct virtio_crypto_alg_chain_data_req chain
;
450 /* See above VIRTIO_CRYPTO_SYM_OP_* */
455 struct virtio_crypto_aead_data_req
{
456 /* Device-readable part */
457 struct virtio_crypto_aead_para para
;
461 struct virtio_crypto_akcipher_para
{
466 struct virtio_crypto_akcipher_data_req
{
467 struct virtio_crypto_akcipher_para para
;
471 /* The request of the data virtqueue's packet */
472 struct virtio_crypto_op_data_req
{
473 struct virtio_crypto_op_header header
;
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
;
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 */
493 /* The accelerator hardware is ready */
494 #define VIRTIO_CRYPTO_S_HW_READY (1 << 0)
496 struct virtio_crypto_config
{
497 /* See VIRTIO_CRYPTO_OP_* above */
501 * Maximum number of data queue
503 __le32 max_dataqueues
;
506 * Specifies the services mask which the device support,
507 * see VIRTIO_CRYPTO_SERVICE_* above
509 __le32 crypto_services
;
511 /* Detailed algorithms mask */
512 __le32 cipher_algo_l
;
513 __le32 cipher_algo_h
;
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 */
527 struct virtio_crypto_inhdr
{
528 /* See VIRTIO_CRYPTO_* above */