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