4 * Copyright(c) 2016 Intel Corporation. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
16 * * Neither the name of Intel Corporation nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #ifndef _RTE_CRYPTO_SYM_H_
34 #define _RTE_CRYPTO_SYM_H_
37 * @file rte_crypto_sym.h
39 * RTE Definitions for Symmetric Cryptography
41 * Defines symmetric cipher and authentication algorithms and modes, as well
42 * as supported symmetric crypto operation combinations.
52 #include <rte_memory.h>
53 #include <rte_mempool.h>
56 /** Symmetric Cipher Algorithms */
57 enum rte_crypto_cipher_algorithm {
58 RTE_CRYPTO_CIPHER_NULL = 1,
59 /**< NULL cipher algorithm. No mode applies to the NULL algorithm. */
61 RTE_CRYPTO_CIPHER_3DES_CBC,
62 /**< Triple DES algorithm in CBC mode */
63 RTE_CRYPTO_CIPHER_3DES_CTR,
64 /**< Triple DES algorithm in CTR mode */
65 RTE_CRYPTO_CIPHER_3DES_ECB,
66 /**< Triple DES algorithm in ECB mode */
68 RTE_CRYPTO_CIPHER_AES_CBC,
69 /**< AES algorithm in CBC mode */
70 RTE_CRYPTO_CIPHER_AES_CCM,
71 /**< AES algorithm in CCM mode. When this cipher algorithm is used the
72 * *RTE_CRYPTO_AUTH_AES_CCM* element of the
73 * *rte_crypto_hash_algorithm* enum MUST be used to set up the related
74 * *rte_crypto_auth_xform* structure in the session context or in
75 * the op_params of the crypto operation structure in the case of a
76 * session-less crypto operation
78 RTE_CRYPTO_CIPHER_AES_CTR,
79 /**< AES algorithm in Counter mode */
80 RTE_CRYPTO_CIPHER_AES_ECB,
81 /**< AES algorithm in ECB mode */
82 RTE_CRYPTO_CIPHER_AES_F8,
83 /**< AES algorithm in F8 mode */
84 RTE_CRYPTO_CIPHER_AES_GCM,
85 /**< AES algorithm in GCM mode. When this cipher algorithm is used the
86 * *RTE_CRYPTO_AUTH_AES_GCM* element of the
87 * *rte_crypto_auth_algorithm* enum MUST be used to set up the related
88 * *rte_crypto_auth_setup_data* structure in the session context or in
89 * the op_params of the crypto operation structure in the case of a
90 * session-less crypto operation.
92 RTE_CRYPTO_CIPHER_AES_XTS,
93 /**< AES algorithm in XTS mode */
95 RTE_CRYPTO_CIPHER_ARC4,
96 /**< (A)RC4 cipher algorithm */
98 RTE_CRYPTO_CIPHER_KASUMI_F8,
99 /**< Kasumi algorithm in F8 mode */
101 RTE_CRYPTO_CIPHER_SNOW3G_UEA2,
102 /**< SNOW3G algorithm in UEA2 mode */
104 RTE_CRYPTO_CIPHER_ZUC_EEA3,
105 /**< ZUC algorithm in EEA3 mode */
107 RTE_CRYPTO_CIPHER_LIST_END
110 /** Symmetric Cipher Direction */
111 enum rte_crypto_cipher_operation {
112 RTE_CRYPTO_CIPHER_OP_ENCRYPT,
113 /**< Encrypt cipher operation */
114 RTE_CRYPTO_CIPHER_OP_DECRYPT
115 /**< Decrypt cipher operation */
119 * Symmetric Cipher Setup Data.
121 * This structure contains data relating to Cipher (Encryption and Decryption)
122 * use to create a session.
124 struct rte_crypto_cipher_xform {
125 enum rte_crypto_cipher_operation op;
126 /**< This parameter determines if the cipher operation is an encrypt or
127 * a decrypt operation. For the RC4 algorithm and the F8/CTR modes,
128 * only encrypt operations are valid.
130 enum rte_crypto_cipher_algorithm algo;
131 /**< Cipher algorithm */
134 uint8_t *data; /**< pointer to key data */
135 size_t length; /**< key length in bytes */
139 * For the RTE_CRYPTO_CIPHER_AES_F8 mode of operation, key.data will
140 * point to a concatenation of the AES encryption key followed by a
141 * keymask. As per RFC3711, the keymask should be padded with trailing
142 * bytes to match the length of the encryption key used.
144 * For AES-XTS mode of operation, two keys must be provided and
145 * key.data must point to the two keys concatenated together (Key1 ||
146 * Key2). The cipher key length will contain the total size of both
149 * Cipher key length is in bytes. For AES it can be 128 bits (16 bytes),
150 * 192 bits (24 bytes) or 256 bits (32 bytes).
152 * For the CCM mode of operation, the only supported key length is 128
155 * For the RTE_CRYPTO_CIPHER_AES_F8 mode of operation, key.length
156 * should be set to the combined length of the encryption key and the
157 * keymask. Since the keymask and the encryption key are the same size,
158 * key.length should be set to 2 x the AES encryption key length.
160 * For the AES-XTS mode of operation:
161 * - Two keys must be provided and key.length refers to total length of
163 * - Each key can be either 128 bits (16 bytes) or 256 bits (32 bytes).
164 * - Both keys must have the same size.
168 /** Symmetric Authentication / Hash Algorithms */
169 enum rte_crypto_auth_algorithm {
170 RTE_CRYPTO_AUTH_NULL = 1,
171 /**< NULL hash algorithm. */
173 RTE_CRYPTO_AUTH_AES_CBC_MAC,
174 /**< AES-CBC-MAC algorithm. Only 128-bit keys are supported. */
175 RTE_CRYPTO_AUTH_AES_CCM,
176 /**< AES algorithm in CCM mode. This is an authenticated cipher. When
177 * this hash algorithm is used, the *RTE_CRYPTO_CIPHER_AES_CCM*
178 * element of the *rte_crypto_cipher_algorithm* enum MUST be used to
179 * set up the related rte_crypto_cipher_setup_data structure in the
180 * session context or the corresponding parameter in the crypto
181 * operation data structures op_params parameter MUST be set for a
182 * session-less crypto operation.
184 RTE_CRYPTO_AUTH_AES_CMAC,
185 /**< AES CMAC algorithm. */
186 RTE_CRYPTO_AUTH_AES_GCM,
187 /**< AES algorithm in GCM mode. When this hash algorithm
188 * is used, the RTE_CRYPTO_CIPHER_AES_GCM element of the
189 * rte_crypto_cipher_algorithm enum MUST be used to set up the related
190 * rte_crypto_cipher_setup_data structure in the session context, or
191 * the corresponding parameter in the crypto operation data structures
192 * op_params parameter MUST be set for a session-less crypto operation.
194 RTE_CRYPTO_AUTH_AES_GMAC,
195 /**< AES GMAC algorithm. When this hash algorithm
196 * is used, the RTE_CRYPTO_CIPHER_AES_GCM element of the
197 * rte_crypto_cipher_algorithm enum MUST be used to set up the related
198 * rte_crypto_cipher_setup_data structure in the session context, or
199 * the corresponding parameter in the crypto operation data structures
200 * op_params parameter MUST be set for a session-less crypto operation.
202 RTE_CRYPTO_AUTH_AES_XCBC_MAC,
203 /**< AES XCBC algorithm. */
205 RTE_CRYPTO_AUTH_KASUMI_F9,
206 /**< Kasumi algorithm in F9 mode. */
209 /**< MD5 algorithm */
210 RTE_CRYPTO_AUTH_MD5_HMAC,
211 /**< HMAC using MD5 algorithm */
213 RTE_CRYPTO_AUTH_SHA1,
214 /**< 128 bit SHA algorithm. */
215 RTE_CRYPTO_AUTH_SHA1_HMAC,
216 /**< HMAC using 128 bit SHA algorithm. */
217 RTE_CRYPTO_AUTH_SHA224,
218 /**< 224 bit SHA algorithm. */
219 RTE_CRYPTO_AUTH_SHA224_HMAC,
220 /**< HMAC using 224 bit SHA algorithm. */
221 RTE_CRYPTO_AUTH_SHA256,
222 /**< 256 bit SHA algorithm. */
223 RTE_CRYPTO_AUTH_SHA256_HMAC,
224 /**< HMAC using 256 bit SHA algorithm. */
225 RTE_CRYPTO_AUTH_SHA384,
226 /**< 384 bit SHA algorithm. */
227 RTE_CRYPTO_AUTH_SHA384_HMAC,
228 /**< HMAC using 384 bit SHA algorithm. */
229 RTE_CRYPTO_AUTH_SHA512,
230 /**< 512 bit SHA algorithm. */
231 RTE_CRYPTO_AUTH_SHA512_HMAC,
232 /**< HMAC using 512 bit SHA algorithm. */
234 RTE_CRYPTO_AUTH_SNOW3G_UIA2,
235 /**< SNOW3G algorithm in UIA2 mode. */
237 RTE_CRYPTO_AUTH_ZUC_EIA3,
238 /**< ZUC algorithm in EIA3 mode */
240 RTE_CRYPTO_AUTH_LIST_END
243 /** Symmetric Authentication / Hash Operations */
244 enum rte_crypto_auth_operation {
245 RTE_CRYPTO_AUTH_OP_VERIFY, /**< Verify authentication digest */
246 RTE_CRYPTO_AUTH_OP_GENERATE /**< Generate authentication digest */
250 * Authentication / Hash transform data.
252 * This structure contains data relating to an authentication/hash crypto
253 * transforms. The fields op, algo and digest_length are common to all
254 * authentication transforms and MUST be set.
256 struct rte_crypto_auth_xform {
257 enum rte_crypto_auth_operation op;
258 /**< Authentication operation type */
259 enum rte_crypto_auth_algorithm algo;
260 /**< Authentication algorithm selection */
263 uint8_t *data; /**< pointer to key data */
264 size_t length; /**< key length in bytes */
266 /**< Authentication key data.
267 * The authentication key length MUST be less than or equal to the
268 * block size of the algorithm. It is the callers responsibility to
269 * ensure that the key length is compliant with the standard being used
270 * (for example RFC 2104, FIPS 198a).
273 uint32_t digest_length;
274 /**< Length of the digest to be returned. If the verify option is set,
275 * this specifies the length of the digest to be compared for the
278 * If the value is less than the maximum length allowed by the hash,
279 * the result shall be truncated. If the value is greater than the
280 * maximum length allowed by the hash then an error will be generated
281 * by *rte_cryptodev_sym_session_create* or by the
282 * *rte_cryptodev_sym_enqueue_burst* if using session-less APIs.
285 uint32_t add_auth_data_length;
286 /**< The length of the additional authenticated data (AAD) in bytes.
287 * The maximum permitted value is 240 bytes, unless otherwise specified
290 * This field must be specified when the hash algorithm is one of the
293 * - For SNOW3G (@ref RTE_CRYPTO_AUTH_SNOW3G_UIA2), this is the
294 * length of the IV (which should be 16).
296 * - For GCM (@ref RTE_CRYPTO_AUTH_AES_GCM). In this case, this is
297 * the length of the Additional Authenticated Data (called A, in NIST
300 * - For CCM (@ref RTE_CRYPTO_AUTH_AES_CCM). In this case, this is
301 * the length of the associated data (called A, in NIST SP800-38C).
302 * Note that this does NOT include the length of any padding, or the
303 * 18 bytes reserved at the start of the above field to store the
304 * block B0 and the encoded length. The maximum permitted value in
305 * this case is 222 bytes.
308 * For AES-GMAC (@ref RTE_CRYPTO_AUTH_AES_GMAC) mode of operation
309 * this field is not used and should be set to 0. Instead the length
310 * of the AAD data is specified in the message length to hash field of
311 * the rte_crypto_sym_op_data structure.
315 /** Crypto transformation types */
316 enum rte_crypto_sym_xform_type {
317 RTE_CRYPTO_SYM_XFORM_NOT_SPECIFIED = 0, /**< No xform specified */
318 RTE_CRYPTO_SYM_XFORM_AUTH, /**< Authentication xform */
319 RTE_CRYPTO_SYM_XFORM_CIPHER /**< Cipher xform */
323 * Symmetric crypto transform structure.
325 * This is used to specify the crypto transforms required, multiple transforms
326 * can be chained together to specify a chain transforms such as authentication
327 * then cipher, or cipher then authentication. Each transform structure can
328 * hold a single transform, the type field is used to specify which transform
329 * is contained within the union
331 struct rte_crypto_sym_xform {
332 struct rte_crypto_sym_xform *next;
333 /**< next xform in chain */
334 enum rte_crypto_sym_xform_type type
337 struct rte_crypto_auth_xform auth;
338 /**< Authentication / hash xform */
339 struct rte_crypto_cipher_xform cipher;
345 * Crypto operation session type. This is used to specify whether a crypto
346 * operation has session structure attached for immutable parameters or if all
347 * operation information is included in the operation data structure.
349 enum rte_crypto_sym_op_sess_type {
350 RTE_CRYPTO_SYM_OP_WITH_SESSION, /**< Session based crypto operation */
351 RTE_CRYPTO_SYM_OP_SESSIONLESS /**< Session-less crypto operation */
355 struct rte_cryptodev_sym_session;
358 * Symmetric Cryptographic Operation.
360 * This structure contains data relating to performing symmetric cryptographic
361 * processing on a referenced mbuf data buffer.
363 * When a symmetric crypto operation is enqueued with the device for processing
364 * it must have a valid *rte_mbuf* structure attached, via m_src parameter,
365 * which contains the source data which the crypto operation is to be performed
368 struct rte_crypto_sym_op {
369 struct rte_mbuf *m_src; /**< source mbuf */
370 struct rte_mbuf *m_dst; /**< destination mbuf */
372 enum rte_crypto_sym_op_sess_type sess_type;
375 struct rte_cryptodev_sym_session *session;
376 /**< Handle for the initialised session context */
377 struct rte_crypto_sym_xform *xform;
378 /**< Session-less API crypto operation parameters */
384 /**< Starting point for cipher processing, specified
385 * as number of bytes from start of data in the source
386 * buffer. The result of the cipher operation will be
387 * written back into the output buffer starting at
391 * For Snow3G @ RTE_CRYPTO_CIPHER_SNOW3G_UEA2
392 * and KASUMI @ RTE_CRYPTO_CIPHER_KASUMI_F8,
393 * this field should be in bits.
397 /**< The message length, in bytes, of the source buffer
398 * on which the cryptographic operation will be
399 * computed. This must be a multiple of the block size
400 * if a block cipher is being used. This is also the
401 * same as the result length.
404 * In the case of CCM @ref RTE_CRYPTO_AUTH_AES_CCM,
405 * this value should not include the length of the
406 * padding or the length of the MAC; the driver will
407 * compute the actual number of bytes over which the
408 * encryption will occur, which will include these
412 * For AES-GMAC @ref RTE_CRYPTO_AUTH_AES_GMAC, this
413 * field should be set to 0.
416 * For Snow3G @ RTE_CRYPTO_AUTH_SNOW3G_UEA2
417 * and KASUMI @ RTE_CRYPTO_CIPHER_KASUMI_F8,
418 * this field should be in bits.
420 } data; /**< Data offsets and length for ciphering */
424 /**< Initialisation Vector or Counter.
426 * - For block ciphers in CBC or F8 mode, or for Kasumi
427 * in F8 mode, or for SNOW3G in UEA2 mode, this is the
428 * Initialisation Vector (IV) value.
430 * - For block ciphers in CTR mode, this is the counter.
432 * - For GCM mode, this is either the IV (if the length
433 * is 96 bits) or J0 (for other sizes), where J0 is as
434 * defined by NIST SP800-38D. Regardless of the IV
435 * length, a full 16 bytes needs to be allocated.
437 * - For CCM mode, the first byte is reserved, and the
438 * nonce should be written starting at &iv[1] (to allow
439 * space for the implementation to write in the flags
440 * in the first byte). Note that a full 16 bytes should
441 * be allocated, even though the length field will
442 * have a value less than this.
444 * - For AES-XTS, this is the 128bit tweak, i, from
445 * IEEE Std 1619-2007.
447 * For optimum performance, the data pointed to SHOULD
450 phys_addr_t phys_addr;
452 /**< Length of valid IV data.
454 * - For block ciphers in CBC or F8 mode, or for Kasumi
455 * in F8 mode, or for SNOW3G in UEA2 mode, this is the
456 * length of the IV (which must be the same as the
457 * block length of the cipher).
459 * - For block ciphers in CTR mode, this is the length
460 * of the counter (which must be the same as the block
461 * length of the cipher).
463 * - For GCM mode, this is either 12 (for 96-bit IVs)
464 * or 16, in which case data points to J0.
466 * - For CCM mode, this is the length of the nonce,
467 * which can be in the range 7 to 13 inclusive.
469 } iv; /**< Initialisation vector parameters */
475 /**< Starting point for hash processing, specified as
476 * number of bytes from start of packet in source
480 * For CCM and GCM modes of operation, this field is
481 * ignored. The field @ref aad field
482 * should be set instead.
484 * @note For AES-GMAC (@ref RTE_CRYPTO_AUTH_AES_GMAC)
485 * mode of operation, this field specifies the start
486 * of the AAD data in the source buffer.
489 * For Snow3G @ RTE_CRYPTO_AUTH_SNOW3G_UIA2
490 * and KASUMI @ RTE_CRYPTO_AUTH_KASUMI_F9,
491 * this field should be in bits.
495 /**< The message length, in bytes, of the source
496 * buffer that the hash will be computed on.
499 * For CCM and GCM modes of operation, this field is
500 * ignored. The field @ref aad field should be set
504 * For AES-GMAC @ref RTE_CRYPTO_AUTH_AES_GMAC mode
505 * of operation, this field specifies the length of
506 * the AAD data in the source buffer.
509 * For Snow3G @ RTE_CRYPTO_AUTH_SNOW3G_UIA2
510 * and KASUMI @ RTE_CRYPTO_AUTH_KASUMI_F9,
511 * this field should be in bits.
513 } data; /**< Data offsets and length for authentication */
517 /**< If this member of this structure is set this is a
518 * pointer to the location where the digest result
519 * should be inserted (in the case of digest generation)
520 * or where the purported digest exists (in the case of
521 * digest verification).
523 * At session creation time, the client specified the
524 * digest result length with the digest_length member
525 * of the @ref rte_crypto_auth_xform structure. For
526 * physical crypto devices the caller must allocate at
527 * least digest_length of physically contiguous memory
530 * For digest generation, the digest result will
531 * overwrite any data at this location.
534 * For GCM (@ref RTE_CRYPTO_AUTH_AES_GCM), for
535 * "digest result" read "authentication tag T".
537 * If this member is not set the digest result is
538 * understood to be in the destination buffer for
539 * digest generation, and in the source buffer for
540 * digest verification. The location of the digest
541 * result in this case is immediately following the
542 * region over which the digest is computed.
544 phys_addr_t phys_addr;
545 /**< Physical address of digest */
547 /**< Length of digest */
548 } digest; /**< Digest parameters */
552 /**< Pointer to Additional Authenticated Data (AAD)
553 * needed for authenticated cipher mechanisms (CCM and
554 * GCM), and to the IV for SNOW3G authentication
555 * (@ref RTE_CRYPTO_AUTH_SNOW3G_UIA2). For other
556 * authentication mechanisms this pointer is ignored.
558 * The length of the data pointed to by this field is
559 * set up for the session in the @ref
560 * rte_crypto_auth_xform structure as part of the @ref
561 * rte_cryptodev_sym_session_create function call.
562 * This length must not exceed 240 bytes.
564 * Specifically for CCM (@ref RTE_CRYPTO_AUTH_AES_CCM),
565 * the caller should setup this field as follows:
567 * - the nonce should be written starting at an offset
568 * of one byte into the array, leaving room for the
569 * implementation to write in the flags to the first
572 * - the additional authentication data itself should
573 * be written starting at an offset of 18 bytes into
574 * the array, leaving room for the length encoding in
575 * the first two bytes of the second block.
577 * - the array should be big enough to hold the above
578 * fields, plus any padding to round this up to the
579 * nearest multiple of the block size (16 bytes).
580 * Padding will be added by the implementation.
582 * Finally, for GCM (@ref RTE_CRYPTO_AUTH_AES_GCM), the
583 * caller should setup this field as follows:
585 * - the AAD is written in starting at byte 0
586 * - the array must be big enough to hold the AAD, plus
587 * any space to round this up to the nearest multiple
588 * of the block size (16 bytes).
591 * For AES-GMAC (@ref RTE_CRYPTO_AUTH_AES_GMAC) mode of
592 * operation, this field is not used and should be set
593 * to 0. Instead the AAD data should be placed in the
596 phys_addr_t phys_addr; /**< physical address */
597 uint16_t length; /**< Length of digest */
599 /**< Additional authentication parameters */
601 } __rte_cache_aligned;
605 * Reset the fields of a symmetric operation to their default values.
607 * @param op The crypto operation to be reset.
610 __rte_crypto_sym_op_reset(struct rte_crypto_sym_op *op)
612 memset(op, 0, sizeof(*op));
614 op->sess_type = RTE_CRYPTO_SYM_OP_SESSIONLESS;
619 * Allocate space for symmetric crypto xforms in the private data space of the
620 * crypto operation. This also defaults the crypto xform type to
621 * RTE_CRYPTO_SYM_XFORM_NOT_SPECIFIED and configures the chaining of the xforms
622 * in the crypto operation
625 * - On success returns pointer to first crypto xform in crypto operations chain
626 * - On failure returns NULL
628 static inline struct rte_crypto_sym_xform *
629 __rte_crypto_sym_op_sym_xforms_alloc(struct rte_crypto_sym_op *sym_op,
630 void *priv_data, uint8_t nb_xforms)
632 struct rte_crypto_sym_xform *xform;
634 sym_op->xform = xform = (struct rte_crypto_sym_xform *)priv_data;
637 xform->type = RTE_CRYPTO_SYM_XFORM_NOT_SPECIFIED;
638 xform = xform->next = --nb_xforms > 0 ? xform + 1 : NULL;
641 return sym_op->xform;
646 * Attach a session to a symmetric crypto operation
648 * @param sym_op crypto operation
649 * @param sess cryptodev session
652 __rte_crypto_sym_op_attach_sym_session(struct rte_crypto_sym_op *sym_op,
653 struct rte_cryptodev_sym_session *sess)
655 sym_op->session = sess;
656 sym_op->sess_type = RTE_CRYPTO_SYM_OP_WITH_SESSION;
666 #endif /* _RTE_CRYPTO_SYM_H_ */