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