xref: /dpdk/lib/cryptodev/rte_crypto_sym.h (revision 4acc862b18a2f1691d1561f7b75542f6a056d41f)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016-2020 Intel Corporation
3  */
4 
5 #ifndef _RTE_CRYPTO_SYM_H_
6 #define _RTE_CRYPTO_SYM_H_
7 
8 /**
9  * @file rte_crypto_sym.h
10  *
11  * RTE Definitions for Symmetric Cryptography
12  *
13  * Defines symmetric cipher and authentication algorithms and modes, as well
14  * as supported symmetric crypto operation combinations.
15  */
16 
17 #include <string.h>
18 
19 #include <rte_compat.h>
20 #include <rte_mbuf.h>
21 #include <rte_memory.h>
22 #include <rte_mempool.h>
23 #include <rte_common.h>
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 /**
30  * Crypto IO Vector (in analogy with struct iovec)
31  * Supposed be used to pass input/output data buffers for crypto data-path
32  * functions.
33  */
34 struct rte_crypto_vec {
35 	/** virtual address of the data buffer */
36 	void *base;
37 	/** IOVA of the data buffer */
38 	rte_iova_t iova;
39 	/** length of the data buffer */
40 	uint32_t len;
41 	/** total buffer length */
42 	uint32_t tot_len;
43 };
44 
45 /**
46  * Crypto scatter-gather list descriptor. Consists of a pointer to an array
47  * of Crypto IO vectors with its size.
48  */
49 struct rte_crypto_sgl {
50 	/** start of an array of vectors */
51 	struct rte_crypto_vec *vec;
52 	/** size of an array of vectors */
53 	uint32_t num;
54 };
55 
56 /**
57  * Crypto virtual and IOVA address descriptor, used to describe cryptographic
58  * data buffer without the length information. The length information is
59  * normally predefined during session creation.
60  */
61 struct rte_crypto_va_iova_ptr {
62 	void *va;
63 	rte_iova_t iova;
64 };
65 
66 /**
67  * Raw data operation descriptor.
68  * Supposed to be used with synchronous CPU crypto API call or asynchronous
69  * RAW data path API call.
70  */
71 struct rte_crypto_sym_vec {
72 	/** number of operations to perform */
73 	uint32_t num;
74 	/** array of SGL vectors */
75 	struct rte_crypto_sgl *src_sgl;
76 	/** array of SGL vectors for OOP, keep it NULL for inplace*/
77 	struct rte_crypto_sgl *dest_sgl;
78 	/** array of pointers to cipher IV */
79 	struct rte_crypto_va_iova_ptr *iv;
80 	/** array of pointers to digest */
81 	struct rte_crypto_va_iova_ptr *digest;
82 
83 	__extension__
84 	union {
85 		/** array of pointers to auth IV, used for chain operation */
86 		struct rte_crypto_va_iova_ptr *auth_iv;
87 		/** array of pointers to AAD, used for AEAD operation */
88 		struct rte_crypto_va_iova_ptr *aad;
89 	};
90 
91 	/**
92 	 * array of statuses for each operation:
93 	 * - 0 on success
94 	 * - errno on error
95 	 */
96 	int32_t *status;
97 };
98 
99 /**
100  * used for cpu_crypto_process_bulk() to specify head/tail offsets
101  * for auth/cipher processing.
102  */
103 union rte_crypto_sym_ofs {
104 	uint64_t raw;
105 	struct {
106 		struct {
107 			uint16_t head;
108 			uint16_t tail;
109 		} auth, cipher;
110 	} ofs;
111 };
112 
113 /** Symmetric Cipher Algorithms
114  *
115  * Note, to avoid ABI breakage across releases
116  * - LIST_END should not be added to this enum
117  * - the order of enums should not be changed
118  * - new algorithms should only be added to the end
119  */
120 enum rte_crypto_cipher_algorithm {
121 	RTE_CRYPTO_CIPHER_NULL = 1,
122 	/**< NULL cipher algorithm. No mode applies to the NULL algorithm. */
123 
124 	RTE_CRYPTO_CIPHER_3DES_CBC,
125 	/**< Triple DES algorithm in CBC mode */
126 	RTE_CRYPTO_CIPHER_3DES_CTR,
127 	/**< Triple DES algorithm in CTR mode */
128 	RTE_CRYPTO_CIPHER_3DES_ECB,
129 	/**< Triple DES algorithm in ECB mode */
130 
131 	RTE_CRYPTO_CIPHER_AES_CBC,
132 	/**< AES algorithm in CBC mode */
133 	RTE_CRYPTO_CIPHER_AES_CTR,
134 	/**< AES algorithm in Counter mode */
135 	RTE_CRYPTO_CIPHER_AES_ECB,
136 	/**< AES algorithm in ECB mode */
137 	RTE_CRYPTO_CIPHER_AES_F8,
138 	/**< AES algorithm in F8 mode */
139 	RTE_CRYPTO_CIPHER_AES_XTS,
140 	/**< AES algorithm in XTS mode */
141 
142 	RTE_CRYPTO_CIPHER_ARC4,
143 	/**< (A)RC4 cipher algorithm */
144 
145 	RTE_CRYPTO_CIPHER_KASUMI_F8,
146 	/**< KASUMI algorithm in F8 mode */
147 
148 	RTE_CRYPTO_CIPHER_SNOW3G_UEA2,
149 	/**< SNOW 3G algorithm in UEA2 mode */
150 
151 	RTE_CRYPTO_CIPHER_ZUC_EEA3,
152 	/**< ZUC algorithm in EEA3 mode */
153 
154 	RTE_CRYPTO_CIPHER_DES_CBC,
155 	/**< DES algorithm in CBC mode */
156 
157 	RTE_CRYPTO_CIPHER_AES_DOCSISBPI,
158 	/**< AES algorithm using modes required by
159 	 * DOCSIS Baseline Privacy Plus Spec.
160 	 * Chained mbufs are not supported in this mode, i.e. rte_mbuf.next
161 	 * for m_src and m_dst in the rte_crypto_sym_op must be NULL.
162 	 */
163 
164 	RTE_CRYPTO_CIPHER_DES_DOCSISBPI,
165 	/**< DES algorithm using modes required by
166 	 * DOCSIS Baseline Privacy Plus Spec.
167 	 * Chained mbufs are not supported in this mode, i.e. rte_mbuf.next
168 	 * for m_src and m_dst in the rte_crypto_sym_op must be NULL.
169 	 */
170 
171 	RTE_CRYPTO_CIPHER_SM4_ECB,
172 	/**< ShangMi 4 (SM4) algorithm in ECB mode */
173 	RTE_CRYPTO_CIPHER_SM4_CBC,
174 	/**< ShangMi 4 (SM4) algorithm in CBC mode */
175 	RTE_CRYPTO_CIPHER_SM4_CTR,
176 	/**< ShangMi 4 (SM4) algorithm in CTR mode */
177 	RTE_CRYPTO_CIPHER_SM4_OFB,
178 	/**< ShangMi 4 (SM4) algorithm in OFB mode */
179 	RTE_CRYPTO_CIPHER_SM4_CFB,
180 	/**< ShangMi 4 (SM4) algorithm in CFB mode */
181 	RTE_CRYPTO_CIPHER_SM4_XTS
182 	/**< ShangMi 4 (SM4) algorithm in XTS mode */
183 };
184 
185 /** Symmetric Cipher Direction */
186 enum rte_crypto_cipher_operation {
187 	RTE_CRYPTO_CIPHER_OP_ENCRYPT,
188 	/**< Encrypt cipher operation */
189 	RTE_CRYPTO_CIPHER_OP_DECRYPT
190 	/**< Decrypt cipher operation */
191 };
192 
193 /** Cipher operation name strings */
194 extern const char *
195 rte_crypto_cipher_operation_strings[];
196 
197 /**
198  * Symmetric Cipher Setup Data.
199  *
200  * This structure contains data relating to Cipher (Encryption and Decryption)
201  *  use to create a session.
202  */
203 struct rte_crypto_cipher_xform {
204 	enum rte_crypto_cipher_operation op;
205 	/**< This parameter determines if the cipher operation is an encrypt or
206 	 * a decrypt operation. For the RC4 algorithm and the F8/CTR modes,
207 	 * only encrypt operations are valid.
208 	 */
209 	enum rte_crypto_cipher_algorithm algo;
210 	/**< Cipher algorithm */
211 
212 	struct {
213 		const uint8_t *data;	/**< pointer to key data */
214 		uint16_t length;	/**< key length in bytes */
215 	} key;
216 	/**< Cipher key
217 	 *
218 	 * In case the PMD supports RTE_CRYPTODEV_FF_CIPHER_WRAPPED_KEY, the
219 	 * original key data provided may be wrapped(encrypted) using key wrap
220 	 * algorithm such as AES key wrap (rfc3394) and hence length of the key
221 	 * may increase beyond the PMD advertised supported key size.
222 	 * PMD shall validate the key length and report EMSGSIZE error while
223 	 * configuring the session and application can skip checking the
224 	 * capability key length in such cases.
225 	 *
226 	 * For the RTE_CRYPTO_CIPHER_AES_F8 mode of operation, key.data will
227 	 * point to a concatenation of the AES encryption key followed by a
228 	 * keymask. As per RFC3711, the keymask should be padded with trailing
229 	 * bytes to match the length of the encryption key used.
230 	 *
231 	 * Cipher key length is in bytes. For AES it can be 128 bits (16 bytes),
232 	 * 192 bits (24 bytes) or 256 bits (32 bytes).
233 	 *
234 	 * For the RTE_CRYPTO_CIPHER_AES_F8 mode of operation, key.length
235 	 * should be set to the combined length of the encryption key and the
236 	 * keymask. Since the keymask and the encryption key are the same size,
237 	 * key.length should be set to 2 x the AES encryption key length.
238 	 *
239 	 * For the AES-XTS mode of operation:
240 	 *  - Two keys must be provided and key.length refers to total length of
241 	 *    the two keys.
242 	 *  - key.data must point to the two keys concatenated together
243 	 *    (key1 || key2).
244 	 *  - Each key can be either 128 bits (16 bytes) or 256 bits (32 bytes).
245 	 *  - Both keys must have the same size.
246 	 */
247 	struct {
248 		uint16_t offset;
249 		/**< Starting point for Initialisation Vector or Counter,
250 		 * specified as number of bytes from start of crypto
251 		 * operation (rte_crypto_op).
252 		 *
253 		 * - For block ciphers in CBC or F8 mode, or for KASUMI
254 		 * in F8 mode, or for SNOW 3G in UEA2 mode, this is the
255 		 * Initialisation Vector (IV) value.
256 		 *
257 		 * - For block ciphers in CTR mode, this is the counter.
258 		 *
259 		 * - For CCM mode, the first byte is reserved, and the
260 		 * nonce should be written starting at &iv[1] (to allow
261 		 * space for the implementation to write in the flags
262 		 * in the first byte). Note that a full 16 bytes should
263 		 * be allocated, even though the length field will
264 		 * have a value less than this. Note that the PMDs may
265 		 * modify the memory reserved (the first byte and the
266 		 * final padding)
267 		 *
268 		 * - For AES-XTS, this is the 128bit tweak, i, from
269 		 * IEEE Std 1619-2007.
270 		 *
271 		 * For optimum performance, the data pointed to SHOULD
272 		 * be 8-byte aligned.
273 		 */
274 		uint16_t length;
275 		/**< Length of valid IV data.
276 		 *
277 		 * - For block ciphers in CBC or F8 mode, or for KASUMI
278 		 * in F8 mode, or for SNOW 3G in UEA2 mode, this is the
279 		 * length of the IV (which must be the same as the
280 		 * block length of the cipher).
281 		 *
282 		 * - For block ciphers in CTR mode, this is the length
283 		 * of the counter (which must be the same as the block
284 		 * length of the cipher) or a 12-byte nonce (AES only)
285 		 *
286 		 * - For CCM mode, this is the length of the nonce,
287 		 * which can be in the range 7 to 13 inclusive.
288 		 */
289 	} iv;	/**< Initialisation vector parameters */
290 
291 	uint32_t dataunit_len;
292 	/**< When RTE_CRYPTODEV_FF_CIPHER_MULTIPLE_DATA_UNITS is enabled,
293 	 * this is the data-unit length of the algorithm,
294 	 * otherwise or when the value is 0, use the operation length.
295 	 * The value should be in the range defined by the dataunit_set field
296 	 * in the cipher capability.
297 	 *
298 	 * - For AES-XTS it is the size of data-unit, from IEEE Std 1619-2007.
299 	 * For-each data-unit in the operation, the tweak (IV) value is
300 	 * assigned consecutively starting from the operation assigned IV.
301 	 */
302 };
303 
304 /** Symmetric Authentication / Hash Algorithms
305  *
306  * Note, to avoid ABI breakage across releases
307  * - LIST_END should not be added to this enum
308  * - the order of enums should not be changed
309  * - new algorithms should only be added to the end
310  */
311 enum rte_crypto_auth_algorithm {
312 	RTE_CRYPTO_AUTH_NULL = 1,
313 	/**< NULL hash algorithm. */
314 
315 	RTE_CRYPTO_AUTH_AES_CBC_MAC,
316 	/**< AES-CBC-MAC algorithm. Only 128-bit keys are supported. */
317 	RTE_CRYPTO_AUTH_AES_CMAC,
318 	/**< AES CMAC algorithm. */
319 	RTE_CRYPTO_AUTH_AES_GMAC,
320 	/**< AES GMAC algorithm. */
321 	RTE_CRYPTO_AUTH_AES_XCBC_MAC,
322 	/**< AES XCBC algorithm. */
323 
324 	RTE_CRYPTO_AUTH_KASUMI_F9,
325 	/**< KASUMI algorithm in F9 mode. */
326 
327 	RTE_CRYPTO_AUTH_MD5,
328 	/**< MD5 algorithm */
329 	RTE_CRYPTO_AUTH_MD5_HMAC,
330 	/**< HMAC using MD5 algorithm */
331 
332 	RTE_CRYPTO_AUTH_SHA1,
333 	/**< 160 bit SHA algorithm. */
334 	RTE_CRYPTO_AUTH_SHA1_HMAC,
335 	/**< HMAC using 160 bit SHA algorithm.
336 	 * HMAC-SHA-1-96 can be generated by setting
337 	 * digest_length to 12 bytes in auth/aead xforms.
338 	 */
339 	RTE_CRYPTO_AUTH_SHA224,
340 	/**< 224 bit SHA algorithm. */
341 	RTE_CRYPTO_AUTH_SHA224_HMAC,
342 	/**< HMAC using 224 bit SHA algorithm. */
343 	RTE_CRYPTO_AUTH_SHA256,
344 	/**< 256 bit SHA algorithm. */
345 	RTE_CRYPTO_AUTH_SHA256_HMAC,
346 	/**< HMAC using 256 bit SHA algorithm. */
347 	RTE_CRYPTO_AUTH_SHA384,
348 	/**< 384 bit SHA algorithm. */
349 	RTE_CRYPTO_AUTH_SHA384_HMAC,
350 	/**< HMAC using 384 bit SHA algorithm. */
351 	RTE_CRYPTO_AUTH_SHA512,
352 	/**< 512 bit SHA algorithm. */
353 	RTE_CRYPTO_AUTH_SHA512_HMAC,
354 	/**< HMAC using 512 bit SHA algorithm. */
355 
356 	RTE_CRYPTO_AUTH_SNOW3G_UIA2,
357 	/**< SNOW 3G algorithm in UIA2 mode. */
358 
359 	RTE_CRYPTO_AUTH_ZUC_EIA3,
360 	/**< ZUC algorithm in EIA3 mode */
361 
362 	RTE_CRYPTO_AUTH_SHA3_224,
363 	/**< 224 bit SHA3 algorithm. */
364 	RTE_CRYPTO_AUTH_SHA3_224_HMAC,
365 	/**< HMAC using 224 bit SHA3 algorithm. */
366 	RTE_CRYPTO_AUTH_SHA3_256,
367 	/**< 256 bit SHA3 algorithm. */
368 	RTE_CRYPTO_AUTH_SHA3_256_HMAC,
369 	/**< HMAC using 256 bit SHA3 algorithm. */
370 	RTE_CRYPTO_AUTH_SHA3_384,
371 	/**< 384 bit SHA3 algorithm. */
372 	RTE_CRYPTO_AUTH_SHA3_384_HMAC,
373 	/**< HMAC using 384 bit SHA3 algorithm. */
374 	RTE_CRYPTO_AUTH_SHA3_512,
375 	/**< 512 bit SHA3 algorithm. */
376 	RTE_CRYPTO_AUTH_SHA3_512_HMAC,
377 	/**< HMAC using 512 bit SHA3 algorithm. */
378 	RTE_CRYPTO_AUTH_SM3,
379 	/**< ShangMi 3 (SM3) algorithm */
380 
381 	RTE_CRYPTO_AUTH_SHAKE_128,
382 	/**< 128 bit SHAKE algorithm. */
383 	RTE_CRYPTO_AUTH_SHAKE_256,
384 	/**< 256 bit SHAKE algorithm. */
385 	RTE_CRYPTO_AUTH_SM3_HMAC,
386 	/** < HMAC using ShangMi 3 (SM3) algorithm */
387 };
388 
389 /** Symmetric Authentication / Hash Operations */
390 enum rte_crypto_auth_operation {
391 	RTE_CRYPTO_AUTH_OP_VERIFY,	/**< Verify authentication digest */
392 	RTE_CRYPTO_AUTH_OP_GENERATE	/**< Generate authentication digest */
393 };
394 
395 /** Authentication operation name strings */
396 extern const char *
397 rte_crypto_auth_operation_strings[];
398 
399 /**
400  * Authentication / Hash transform data.
401  *
402  * This structure contains data relating to an authentication/hash crypto
403  * transforms. The fields op, algo and digest_length are common to all
404  * authentication transforms and MUST be set.
405  */
406 struct rte_crypto_auth_xform {
407 	enum rte_crypto_auth_operation op;
408 	/**< Authentication operation type */
409 	enum rte_crypto_auth_algorithm algo;
410 	/**< Authentication algorithm selection */
411 
412 	struct {
413 		const uint8_t *data;	/**< pointer to key data */
414 		uint16_t length;	/**< key length in bytes */
415 	} key;
416 	/**< Authentication key data.
417 	 * The authentication key length MUST be less than or equal to the
418 	 * block size of the algorithm. It is the callers responsibility to
419 	 * ensure that the key length is compliant with the standard being used
420 	 * (for example RFC 2104, FIPS 198a).
421 	 */
422 
423 	struct {
424 		uint16_t offset;
425 		/**< Starting point for Initialisation Vector or Counter,
426 		 * specified as number of bytes from start of crypto
427 		 * operation (rte_crypto_op).
428 		 *
429 		 * - For SNOW 3G in UIA2 mode, for ZUC in EIA3 mode
430 		 *   this is the authentication Initialisation Vector
431 		 *   (IV) value. For AES-GMAC IV description please refer
432 		 *   to the field `length` in iv struct.
433 		 *
434 		 * - For KASUMI in F9 mode and other authentication
435 		 *   algorithms, this field is not used.
436 		 *
437 		 * For optimum performance, the data pointed to SHOULD
438 		 * be 8-byte aligned.
439 		 */
440 		uint16_t length;
441 		/**< Length of valid IV data.
442 		 *
443 		 * - For SNOW3G in UIA2 mode, for ZUC in EIA3 mode and
444 		 *   for AES-GMAC, this is the length of the IV.
445 		 *
446 		 * - For KASUMI in F9 mode and other authentication
447 		 *   algorithms, this field is not used.
448 		 *
449 		 * - For GMAC mode, this is either:
450 		 * 1) Number greater or equal to one, which means that IV
451 		 *    is used and J0 will be computed internally, a minimum
452 		 *    of 16 bytes must be allocated.
453 		 * 2) Zero, in which case data points to J0. In this case
454 		 *    16 bytes of J0 should be passed where J0 is defined
455 		 *    by NIST SP800-38D.
456 		 *
457 		 */
458 	} iv;	/**< Initialisation vector parameters */
459 
460 	uint16_t digest_length;
461 	/**< Length of the digest to be returned. If the verify option is set,
462 	 * this specifies the length of the digest to be compared for the
463 	 * session.
464 	 *
465 	 * It is the caller's responsibility to ensure that the
466 	 * digest length is compliant with the hash algorithm being used.
467 	 * If the value is less than the maximum length allowed by the hash,
468 	 * the result shall be truncated.
469 	 */
470 };
471 
472 
473 /** Symmetric AEAD Algorithms
474  *
475  * Note, to avoid ABI breakage across releases
476  * - LIST_END should not be added to this enum
477  * - the order of enums should not be changed
478  * - new algorithms should only be added to the end
479  */
480 enum rte_crypto_aead_algorithm {
481 	RTE_CRYPTO_AEAD_AES_CCM = 1,
482 	/**< AES algorithm in CCM mode. */
483 	RTE_CRYPTO_AEAD_AES_GCM,
484 	/**< AES algorithm in GCM mode. */
485 	RTE_CRYPTO_AEAD_CHACHA20_POLY1305
486 	/**< Chacha20 cipher with poly1305 authenticator */
487 };
488 
489 /** Symmetric AEAD Operations */
490 enum rte_crypto_aead_operation {
491 	RTE_CRYPTO_AEAD_OP_ENCRYPT,
492 	/**< Encrypt and generate digest */
493 	RTE_CRYPTO_AEAD_OP_DECRYPT
494 	/**< Verify digest and decrypt */
495 };
496 
497 /** Authentication operation name strings */
498 extern const char *
499 rte_crypto_aead_operation_strings[];
500 
501 struct rte_crypto_aead_xform {
502 	enum rte_crypto_aead_operation op;
503 	/**< AEAD operation type */
504 	enum rte_crypto_aead_algorithm algo;
505 	/**< AEAD algorithm selection */
506 
507 	struct {
508 		const uint8_t *data;	/**< pointer to key data */
509 		uint16_t length;	/**< key length in bytes */
510 	} key;
511 
512 	struct {
513 		uint16_t offset;
514 		/**< Starting point for Initialisation Vector or Counter,
515 		 * specified as number of bytes from start of crypto
516 		 * operation (rte_crypto_op).
517 		 *
518 		 * - For CCM mode, the first byte is reserved, and the
519 		 * nonce should be written starting at &iv[1] (to allow
520 		 * space for the implementation to write in the flags
521 		 * in the first byte). Note that a full 16 bytes should
522 		 * be allocated, even though the length field will
523 		 * have a value less than this.
524 		 *
525 		 * - For Chacha20-Poly1305 it is 96-bit nonce.
526 		 * PMD sets initial counter for Poly1305 key generation
527 		 * part to 0 and for Chacha20 encryption to 1 as per
528 		 * rfc8439 2.8. AEAD construction.
529 		 *
530 		 * For optimum performance, the data pointed to SHOULD
531 		 * be 8-byte aligned.
532 		 */
533 		uint16_t length;
534 		/**< Length of valid IV data.
535 		 *
536 		 * - For GCM mode, this is either:
537 		 * 1) Number greater or equal to one, which means that IV
538 		 *    is used and J0 will be computed internally, a minimum
539 		 *    of 16 bytes must be allocated.
540 		 * 2) Zero, in which case data points to J0. In this case
541 		 *    16 bytes of J0 should be passed where J0 is defined
542 		 *    by NIST SP800-38D.
543 		 *
544 		 * - For CCM mode, this is the length of the nonce,
545 		 * which can be in the range 7 to 13 inclusive.
546 		 *
547 		 * - For Chacha20-Poly1305 this field is always 12.
548 		 */
549 	} iv;	/**< Initialisation vector parameters */
550 
551 	uint16_t digest_length;
552 
553 	uint16_t aad_length;
554 	/**< The length of the additional authenticated data (AAD) in bytes.
555 	 * For CCM mode, this is the length of the actual AAD, even though
556 	 * it is required to reserve 18 bytes before the AAD and padding
557 	 * at the end of it, so a multiple of 16 bytes is allocated.
558 	 */
559 };
560 
561 /** Crypto transformation types */
562 enum rte_crypto_sym_xform_type {
563 	RTE_CRYPTO_SYM_XFORM_NOT_SPECIFIED = 0,	/**< No xform specified */
564 	RTE_CRYPTO_SYM_XFORM_AUTH,		/**< Authentication xform */
565 	RTE_CRYPTO_SYM_XFORM_CIPHER,		/**< Cipher xform  */
566 	RTE_CRYPTO_SYM_XFORM_AEAD		/**< AEAD xform  */
567 };
568 
569 /**
570  * Symmetric crypto transform structure.
571  *
572  * This is used to specify the crypto transforms required, multiple transforms
573  * can be chained together to specify a chain transforms such as authentication
574  * then cipher, or cipher then authentication. Each transform structure can
575  * hold a single transform, the type field is used to specify which transform
576  * is contained within the union
577  */
578 /* Structure rte_crypto_sym_xform 8< */
579 struct rte_crypto_sym_xform {
580 	struct rte_crypto_sym_xform *next;
581 	/**< next xform in chain */
582 	enum rte_crypto_sym_xform_type type
583 	; /**< xform type */
584 	union {
585 		struct rte_crypto_auth_xform auth;
586 		/**< Authentication / hash xform */
587 		struct rte_crypto_cipher_xform cipher;
588 		/**< Cipher xform */
589 		struct rte_crypto_aead_xform aead;
590 		/**< AEAD xform */
591 	};
592 };
593 /* >8 End of structure rte_crypto_sym_xform. */
594 
595 /**
596  * Symmetric Cryptographic Operation.
597  *
598  * This structure contains data relating to performing symmetric cryptographic
599  * processing on a referenced mbuf data buffer.
600  *
601  * When a symmetric crypto operation is enqueued with the device for processing
602  * it must have a valid *rte_mbuf* structure attached, via m_src parameter,
603  * which contains the source data which the crypto operation is to be performed
604  * on.
605  * While the mbuf is in use by a crypto operation no part of the mbuf should be
606  * changed by the application as the device may read or write to any part of the
607  * mbuf. In the case of hardware crypto devices some or all of the mbuf
608  * may be DMAed in and out of the device, so writing over the original data,
609  * though only the part specified by the rte_crypto_sym_op for transformation
610  * will be changed.
611  * Out-of-place (OOP) operation, where the source mbuf is different to the
612  * destination mbuf, is a special case. Data will be copied from m_src to m_dst.
613  * The part copied includes all the parts of the source mbuf that will be
614  * operated on, based on the cipher.data.offset+cipher.data.length and
615  * auth.data.offset+auth.data.length values in the rte_crypto_sym_op. The part
616  * indicated by the cipher parameters will be transformed, any extra data around
617  * this indicated by the auth parameters will be copied unchanged from source to
618  * destination mbuf.
619  * Also in OOP operation the cipher.data.offset and auth.data.offset apply to
620  * both source and destination mbufs. As these offsets are relative to the
621  * data_off parameter in each mbuf this can result in the data written to the
622  * destination buffer being at a different alignment, relative to buffer start,
623  * to the data in the source buffer.
624  */
625 /* Structure rte_crypto_sym_op 8< */
626 struct rte_crypto_sym_op {
627 	struct rte_mbuf *m_src;	/**< source mbuf */
628 	struct rte_mbuf *m_dst;	/**< destination mbuf */
629 
630 	union {
631 		void *session;
632 		/**< Handle for the initialised crypto/security session context */
633 		struct rte_crypto_sym_xform *xform;
634 		/**< Session-less API crypto operation parameters */
635 	};
636 
637 	union {
638 		struct {
639 			struct {
640 				uint32_t offset;
641 				 /**< Starting point for AEAD processing, specified as
642 				  * number of bytes from start of packet in source
643 				  * buffer.
644 				  */
645 				uint32_t length;
646 				 /**< The message length, in bytes, of the source buffer
647 				  * on which the cryptographic operation will be
648 				  * computed.
649 				  */
650 			} data; /**< Data offsets and length for AEAD */
651 			struct {
652 				uint8_t *data;
653 				/**< This points to the location where the digest result
654 				 * should be inserted (in the case of digest generation)
655 				 * or where the purported digest exists (in the case of
656 				 * digest verification).
657 				 *
658 				 * At session creation time, the client specified the
659 				 * digest result length with the digest_length member
660 				 * of the @ref rte_crypto_auth_xform structure. For
661 				 * physical crypto devices the caller must allocate at
662 				 * least digest_length of physically contiguous memory
663 				 * at this location.
664 				 *
665 				 * For digest generation, the digest result will
666 				 * overwrite any data at this location.
667 				 *
668 				 * @note
669 				 * For GCM (@ref RTE_CRYPTO_AEAD_AES_GCM), for
670 				 * "digest result" read "authentication tag T".
671 				 */
672 				rte_iova_t phys_addr;
673 				/**< Physical address of digest */
674 			} digest; /**< Digest parameters */
675 			struct {
676 				uint8_t *data;
677 				/**< Pointer to Additional Authenticated Data (AAD)
678 				 * needed for authenticated cipher mechanisms (CCM and
679 				 * GCM)
680 				 *
681 				 * Specifically for CCM (@ref RTE_CRYPTO_AEAD_AES_CCM),
682 				 * the caller should setup this field as follows:
683 				 *
684 				 * - the additional authentication data itself should
685 				 * be written starting at an offset of 18 bytes into
686 				 * the array, leaving room for the first block (16 bytes)
687 				 * and the length encoding in the first two bytes of the
688 				 * second block.
689 				 *
690 				 * - Note that PMDs may modify the memory reserved
691 				 * (first 18 bytes and the final padding).
692 				 *
693 				 * Finally, for GCM (@ref RTE_CRYPTO_AEAD_AES_GCM), the
694 				 * caller should setup this field as follows:
695 				 *
696 				 */
697 				rte_iova_t phys_addr;	/**< physical address */
698 			} aad;
699 			/**< Additional authentication parameters */
700 		} aead;
701 
702 		struct {
703 			struct {
704 				struct {
705 					uint32_t offset;
706 					 /**< Starting point for cipher processing,
707 					  * specified as number of bytes from start
708 					  * of data in the source buffer.
709 					  * The result of the cipher operation will be
710 					  * written back into the output buffer
711 					  * starting at this location.
712 					  *
713 					  * @note
714 					  * For SNOW 3G @ RTE_CRYPTO_CIPHER_SNOW3G_UEA2,
715 					  * KASUMI @ RTE_CRYPTO_CIPHER_KASUMI_F8
716 					  * and ZUC @ RTE_CRYPTO_CIPHER_ZUC_EEA3,
717 					  * this field should be in bits. For
718 					  * digest-encrypted cases this must be
719 					  * an 8-bit multiple.
720 					  */
721 					uint32_t length;
722 					 /**< The message length, in bytes, of the
723 					  * source buffer on which the cryptographic
724 					  * operation will be computed.
725 					  * This is also the same as the result length.
726 					  * For block ciphers, this must be a
727 					  * multiple of the block size,
728 					  * or for the AES-XTS a multiple of the data-unit length
729 					  * as described in xform.
730 					  *
731 					  * @note
732 					  * For SNOW 3G @ RTE_CRYPTO_AUTH_SNOW3G_UEA2,
733 					  * KASUMI @ RTE_CRYPTO_CIPHER_KASUMI_F8
734 					  * and ZUC @ RTE_CRYPTO_CIPHER_ZUC_EEA3,
735 					  * this field should be in bits. For
736 					  * digest-encrypted cases this must be
737 					  * an 8-bit multiple.
738 					  */
739 				} data; /**< Data offsets and length for ciphering */
740 			} cipher;
741 
742 			struct {
743 				struct {
744 					uint32_t offset;
745 					 /**< Starting point for hash processing,
746 					  * specified as number of bytes from start of
747 					  * packet in source buffer.
748 					  *
749 					  * @note
750 					  * For SNOW 3G @ RTE_CRYPTO_AUTH_SNOW3G_UIA2,
751 					  * KASUMI @ RTE_CRYPTO_AUTH_KASUMI_F9
752 					  * and ZUC @ RTE_CRYPTO_AUTH_ZUC_EIA3,
753 					  * this field should be in bits. For
754 					  * digest-encrypted cases this must be
755 					  * an 8-bit multiple.
756 					  *
757 					  * @note
758 					  * For KASUMI @ RTE_CRYPTO_AUTH_KASUMI_F9,
759 					  * this offset should be such that
760 					  * data to authenticate starts at COUNT.
761 					  *
762 					  * @note
763 					  * For DOCSIS security protocol, this
764 					  * offset is the DOCSIS header length
765 					  * and, therefore, also the CRC offset
766 					  * i.e. the number of bytes into the
767 					  * packet at which CRC calculation
768 					  * should begin.
769 					  */
770 					uint32_t length;
771 					 /**< The message length, in bytes, of the source
772 					  * buffer that the hash will be computed on.
773 					  *
774 					  * @note
775 					  * For SNOW 3G @ RTE_CRYPTO_AUTH_SNOW3G_UIA2,
776 					  * KASUMI @ RTE_CRYPTO_AUTH_KASUMI_F9
777 					  * and ZUC @ RTE_CRYPTO_AUTH_ZUC_EIA3,
778 					  * this field should be in bits. For
779 					  * digest-encrypted cases this must be
780 					  * an 8-bit multiple.
781 					  *
782 					  * @note
783 					  * For KASUMI @ RTE_CRYPTO_AUTH_KASUMI_F9,
784 					  * the length should include the COUNT,
785 					  * FRESH, message, direction bit and padding
786 					  * (to be multiple of 8 bits).
787 					  *
788 					  * @note
789 					  * For DOCSIS security protocol, this
790 					  * is the CRC length i.e. the number of
791 					  * bytes in the packet over which the
792 					  * CRC should be calculated
793 					  */
794 				} data;
795 				/**< Data offsets and length for authentication */
796 
797 				struct {
798 					uint8_t *data;
799 					/**< This points to the location where
800 					 * the digest result should be inserted
801 					 * (in the case of digest generation)
802 					 * or where the purported digest exists
803 					 * (in the case of digest verification).
804 					 *
805 					 * At session creation time, the client
806 					 * specified the digest result length with
807 					 * the digest_length member of the
808 					 * @ref rte_crypto_auth_xform structure.
809 					 * For physical crypto devices the caller
810 					 * must allocate at least digest_length of
811 					 * physically contiguous memory at this
812 					 * location.
813 					 *
814 					 * For digest generation, the digest result
815 					 * will overwrite any data at this location.
816 					 *
817 					 * @note
818 					 * Digest-encrypted case.
819 					 * Digest can be generated, appended to
820 					 * the end of raw data and encrypted
821 					 * together using chained digest
822 					 * generation
823 					 * (@ref RTE_CRYPTO_AUTH_OP_GENERATE)
824 					 * and encryption
825 					 * (@ref RTE_CRYPTO_CIPHER_OP_ENCRYPT)
826 					 * xforms. Similarly, authentication
827 					 * of the raw data against appended,
828 					 * decrypted digest, can be performed
829 					 * using decryption
830 					 * (@ref RTE_CRYPTO_CIPHER_OP_DECRYPT)
831 					 * and digest verification
832 					 * (@ref RTE_CRYPTO_AUTH_OP_VERIFY)
833 					 * chained xforms.
834 					 * To perform those operations, a few
835 					 * additional conditions must be met:
836 					 * - caller must allocate at least
837 					 * digest_length of memory at the end of
838 					 * source and (in case of out-of-place
839 					 * operations) destination buffer; those
840 					 * buffers can be linear or split using
841 					 * scatter-gather lists,
842 					 * - digest data pointer must point to
843 					 * the end of source or (in case of
844 					 * out-of-place operations) destination
845 					 * data, which is pointer to the
846 					 * data buffer + auth.data.offset +
847 					 * auth.data.length,
848 					 * - cipher.data.offset +
849 					 * cipher.data.length must be greater
850 					 * than auth.data.offset +
851 					 * auth.data.length and is typically
852 					 * equal to auth.data.offset +
853 					 * auth.data.length + digest_length.
854 					 * - for wireless algorithms, i.e.
855 					 * SNOW 3G, KASUMI and ZUC, as the
856 					 * cipher.data.length,
857 					 * cipher.data.offset,
858 					 * auth.data.length and
859 					 * auth.data.offset are in bits, they
860 					 * must be 8-bit multiples.
861 					 *
862 					 * Note, that for security reasons, it
863 					 * is PMDs' responsibility to not
864 					 * leave an unencrypted digest in any
865 					 * buffer after performing auth-cipher
866 					 * operations.
867 					 *
868 					 */
869 					rte_iova_t phys_addr;
870 					/**< Physical address of digest */
871 				} digest; /**< Digest parameters */
872 			} auth;
873 		};
874 	};
875 };
876 /* >8 End of structure rte_crypto_sym_op. */
877 
878 
879 /**
880  * Reset the fields of a symmetric operation to their default values.
881  *
882  * @param	op	The crypto operation to be reset.
883  */
884 static inline void
885 __rte_crypto_sym_op_reset(struct rte_crypto_sym_op *op)
886 {
887 	memset(op, 0, sizeof(*op));
888 }
889 
890 
891 /**
892  * Allocate space for symmetric crypto xforms in the private data space of the
893  * crypto operation. This also defaults the crypto xform type to
894  * RTE_CRYPTO_SYM_XFORM_NOT_SPECIFIED and configures the chaining of the xforms
895  * in the crypto operation
896  *
897  * @return
898  * - On success returns pointer to first crypto xform in crypto operations chain
899  * - On failure returns NULL
900  */
901 static inline struct rte_crypto_sym_xform *
902 __rte_crypto_sym_op_sym_xforms_alloc(struct rte_crypto_sym_op *sym_op,
903 		void *priv_data, uint8_t nb_xforms)
904 {
905 	struct rte_crypto_sym_xform *xform;
906 
907 	sym_op->xform = xform = (struct rte_crypto_sym_xform *)priv_data;
908 
909 	do {
910 		xform->type = RTE_CRYPTO_SYM_XFORM_NOT_SPECIFIED;
911 		xform = xform->next = --nb_xforms > 0 ? xform + 1 : NULL;
912 	} while (xform);
913 
914 	return sym_op->xform;
915 }
916 
917 
918 /**
919  * Attach a session to a symmetric crypto operation
920  *
921  * @param	sym_op	crypto operation
922  * @param	sess	cryptodev session
923  */
924 static inline int
925 __rte_crypto_sym_op_attach_sym_session(struct rte_crypto_sym_op *sym_op, void *sess)
926 {
927 	sym_op->session = sess;
928 
929 	return 0;
930 }
931 
932 /**
933  * Converts portion of mbuf data into a vector representation.
934  * Each segment will be represented as a separate entry in *vec* array.
935  * Expects that provided *ofs* + *len* not to exceed mbuf's *pkt_len*.
936  * @param mb
937  *   Pointer to the *rte_mbuf* object.
938  * @param ofs
939  *   Offset within mbuf data to start with.
940  * @param len
941  *   Length of data to represent.
942  * @param vec
943  *   Pointer to an output array of IO vectors.
944  * @param num
945  *   Size of an output array.
946  * @return
947  *   - number of successfully filled entries in *vec* array.
948  *   - negative number of elements in *vec* array required.
949  */
950 __rte_experimental
951 static inline int
952 rte_crypto_mbuf_to_vec(const struct rte_mbuf *mb, uint32_t ofs, uint32_t len,
953 	struct rte_crypto_vec vec[], uint32_t num)
954 {
955 	uint32_t i;
956 	struct rte_mbuf *nseg;
957 	uint32_t left;
958 	uint32_t seglen;
959 
960 	/* assuming that requested data starts in the first segment */
961 	RTE_ASSERT(mb->data_len > ofs);
962 
963 	if (mb->nb_segs > num)
964 		return -mb->nb_segs;
965 
966 	vec[0].base = rte_pktmbuf_mtod_offset(mb, void *, ofs);
967 	vec[0].iova = rte_pktmbuf_iova_offset(mb, ofs);
968 	vec[0].tot_len = mb->buf_len - rte_pktmbuf_headroom(mb) - ofs;
969 
970 	/* whole data lies in the first segment */
971 	seglen = mb->data_len - ofs;
972 	if (len <= seglen) {
973 		vec[0].len = len;
974 		return 1;
975 	}
976 
977 	/* data spread across segments */
978 	vec[0].len = seglen;
979 	left = len - seglen;
980 	for (i = 1, nseg = mb->next; nseg != NULL; nseg = nseg->next, i++) {
981 
982 		vec[i].base = rte_pktmbuf_mtod(nseg, void *);
983 		vec[i].iova = rte_pktmbuf_iova(nseg);
984 		vec[i].tot_len = mb->buf_len - rte_pktmbuf_headroom(mb) - ofs;
985 
986 		seglen = nseg->data_len;
987 		if (left <= seglen) {
988 			/* whole requested data is completed */
989 			vec[i].len = left;
990 			left = 0;
991 			i++;
992 			break;
993 		}
994 
995 		/* use whole segment */
996 		vec[i].len = seglen;
997 		left -= seglen;
998 	}
999 
1000 	RTE_ASSERT(left == 0);
1001 	return i;
1002 }
1003 
1004 
1005 #ifdef __cplusplus
1006 }
1007 #endif
1008 
1009 #endif /* _RTE_CRYPTO_SYM_H_ */
1010