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