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