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>
54 #include <rte_common.h>
57 /** Symmetric Cipher Algorithms */
58 enum rte_crypto_cipher_algorithm {
59 RTE_CRYPTO_CIPHER_NULL = 1,
60 /**< NULL cipher algorithm. No mode applies to the NULL algorithm. */
62 RTE_CRYPTO_CIPHER_3DES_CBC,
63 /**< Triple DES algorithm in CBC mode */
64 RTE_CRYPTO_CIPHER_3DES_CTR,
65 /**< Triple DES algorithm in CTR mode */
66 RTE_CRYPTO_CIPHER_3DES_ECB,
67 /**< Triple DES algorithm in ECB mode */
69 RTE_CRYPTO_CIPHER_AES_CBC,
70 /**< AES algorithm in CBC mode */
71 RTE_CRYPTO_CIPHER_AES_CCM,
72 /**< AES algorithm in CCM mode. When this cipher algorithm is used the
73 * *RTE_CRYPTO_AUTH_AES_CCM* element of the
74 * *rte_crypto_hash_algorithm* enum MUST be used to set up the related
75 * *rte_crypto_auth_xform* structure in the session context or in
76 * the op_params of the crypto operation structure in the case of a
77 * session-less crypto operation
79 RTE_CRYPTO_CIPHER_AES_CTR,
80 /**< AES algorithm in Counter mode */
81 RTE_CRYPTO_CIPHER_AES_ECB,
82 /**< AES algorithm in ECB mode */
83 RTE_CRYPTO_CIPHER_AES_F8,
84 /**< AES algorithm in F8 mode */
85 RTE_CRYPTO_CIPHER_AES_GCM,
86 /**< AES algorithm in GCM mode. When this cipher algorithm is used the
87 * *RTE_CRYPTO_AUTH_AES_GCM* element of the
88 * *rte_crypto_auth_algorithm* enum MUST be used to set up the related
89 * *rte_crypto_auth_setup_data* structure in the session context or in
90 * the op_params of the crypto operation structure in the case of a
91 * session-less crypto operation.
93 RTE_CRYPTO_CIPHER_AES_XTS,
94 /**< AES algorithm in XTS mode */
96 RTE_CRYPTO_CIPHER_ARC4,
97 /**< (A)RC4 cipher algorithm */
99 RTE_CRYPTO_CIPHER_KASUMI_F8,
100 /**< Kasumi algorithm in F8 mode */
102 RTE_CRYPTO_CIPHER_SNOW3G_UEA2,
103 /**< SNOW3G algorithm in UEA2 mode */
105 RTE_CRYPTO_CIPHER_ZUC_EEA3,
106 /**< ZUC algorithm in EEA3 mode */
108 RTE_CRYPTO_CIPHER_LIST_END
111 /** Symmetric Cipher Direction */
112 enum rte_crypto_cipher_operation {
113 RTE_CRYPTO_CIPHER_OP_ENCRYPT,
114 /**< Encrypt cipher operation */
115 RTE_CRYPTO_CIPHER_OP_DECRYPT
116 /**< Decrypt cipher operation */
120 * Symmetric Cipher Setup Data.
122 * This structure contains data relating to Cipher (Encryption and Decryption)
123 * use to create a session.
125 struct rte_crypto_cipher_xform {
126 enum rte_crypto_cipher_operation op;
127 /**< This parameter determines if the cipher operation is an encrypt or
128 * a decrypt operation. For the RC4 algorithm and the F8/CTR modes,
129 * only encrypt operations are valid.
131 enum rte_crypto_cipher_algorithm algo;
132 /**< Cipher algorithm */
135 uint8_t *data; /**< pointer to key data */
136 size_t length; /**< key length in bytes */
140 * For the RTE_CRYPTO_CIPHER_AES_F8 mode of operation, key.data will
141 * point to a concatenation of the AES encryption key followed by a
142 * keymask. As per RFC3711, the keymask should be padded with trailing
143 * bytes to match the length of the encryption key used.
145 * For AES-XTS mode of operation, two keys must be provided and
146 * key.data must point to the two keys concatenated together (Key1 ||
147 * Key2). The cipher key length will contain the total size of both
150 * Cipher key length is in bytes. For AES it can be 128 bits (16 bytes),
151 * 192 bits (24 bytes) or 256 bits (32 bytes).
153 * For the CCM mode of operation, the only supported key length is 128
156 * For the RTE_CRYPTO_CIPHER_AES_F8 mode of operation, key.length
157 * should be set to the combined length of the encryption key and the
158 * keymask. Since the keymask and the encryption key are the same size,
159 * key.length should be set to 2 x the AES encryption key length.
161 * For the AES-XTS mode of operation:
162 * - Two keys must be provided and key.length refers to total length of
164 * - Each key can be either 128 bits (16 bytes) or 256 bits (32 bytes).
165 * - Both keys must have the same size.
169 /** Symmetric Authentication / Hash Algorithms */
170 enum rte_crypto_auth_algorithm {
171 RTE_CRYPTO_AUTH_NULL = 1,
172 /**< NULL hash algorithm. */
174 RTE_CRYPTO_AUTH_AES_CBC_MAC,
175 /**< AES-CBC-MAC algorithm. Only 128-bit keys are supported. */
176 RTE_CRYPTO_AUTH_AES_CCM,
177 /**< AES algorithm in CCM mode. This is an authenticated cipher. When
178 * this hash algorithm is used, the *RTE_CRYPTO_CIPHER_AES_CCM*
179 * element of the *rte_crypto_cipher_algorithm* enum MUST be used to
180 * set up the related rte_crypto_cipher_setup_data structure in the
181 * session context or the corresponding parameter in the crypto
182 * operation data structures op_params parameter MUST be set for a
183 * session-less crypto operation.
185 RTE_CRYPTO_AUTH_AES_CMAC,
186 /**< AES CMAC algorithm. */
187 RTE_CRYPTO_AUTH_AES_GCM,
188 /**< AES algorithm in GCM mode. When this hash algorithm
189 * is used, the RTE_CRYPTO_CIPHER_AES_GCM element of the
190 * rte_crypto_cipher_algorithm enum MUST be used to set up the related
191 * rte_crypto_cipher_setup_data structure in the session context, or
192 * the corresponding parameter in the crypto operation data structures
193 * op_params parameter MUST be set for a session-less crypto operation.
195 RTE_CRYPTO_AUTH_AES_GMAC,
196 /**< AES GMAC algorithm. When this hash algorithm
197 * is used, the RTE_CRYPTO_CIPHER_AES_GCM element of the
198 * rte_crypto_cipher_algorithm enum MUST be used to set up the related
199 * rte_crypto_cipher_setup_data structure in the session context, or
200 * the corresponding parameter in the crypto operation data structures
201 * op_params parameter MUST be set for a session-less crypto operation.
203 RTE_CRYPTO_AUTH_AES_XCBC_MAC,
204 /**< AES XCBC algorithm. */
206 RTE_CRYPTO_AUTH_KASUMI_F9,
207 /**< Kasumi algorithm in F9 mode. */
210 /**< MD5 algorithm */
211 RTE_CRYPTO_AUTH_MD5_HMAC,
212 /**< HMAC using MD5 algorithm */
214 RTE_CRYPTO_AUTH_SHA1,
215 /**< 128 bit SHA algorithm. */
216 RTE_CRYPTO_AUTH_SHA1_HMAC,
217 /**< HMAC using 128 bit SHA algorithm. */
218 RTE_CRYPTO_AUTH_SHA224,
219 /**< 224 bit SHA algorithm. */
220 RTE_CRYPTO_AUTH_SHA224_HMAC,
221 /**< HMAC using 224 bit SHA algorithm. */
222 RTE_CRYPTO_AUTH_SHA256,
223 /**< 256 bit SHA algorithm. */
224 RTE_CRYPTO_AUTH_SHA256_HMAC,
225 /**< HMAC using 256 bit SHA algorithm. */
226 RTE_CRYPTO_AUTH_SHA384,
227 /**< 384 bit SHA algorithm. */
228 RTE_CRYPTO_AUTH_SHA384_HMAC,
229 /**< HMAC using 384 bit SHA algorithm. */
230 RTE_CRYPTO_AUTH_SHA512,
231 /**< 512 bit SHA algorithm. */
232 RTE_CRYPTO_AUTH_SHA512_HMAC,
233 /**< HMAC using 512 bit SHA algorithm. */
235 RTE_CRYPTO_AUTH_SNOW3G_UIA2,
236 /**< SNOW3G algorithm in UIA2 mode. */
238 RTE_CRYPTO_AUTH_ZUC_EIA3,
239 /**< ZUC algorithm in EIA3 mode */
241 RTE_CRYPTO_AUTH_LIST_END
244 /** Symmetric Authentication / Hash Operations */
245 enum rte_crypto_auth_operation {
246 RTE_CRYPTO_AUTH_OP_VERIFY, /**< Verify authentication digest */
247 RTE_CRYPTO_AUTH_OP_GENERATE /**< Generate authentication digest */
251 * Authentication / Hash transform data.
253 * This structure contains data relating to an authentication/hash crypto
254 * transforms. The fields op, algo and digest_length are common to all
255 * authentication transforms and MUST be set.
257 struct rte_crypto_auth_xform {
258 enum rte_crypto_auth_operation op;
259 /**< Authentication operation type */
260 enum rte_crypto_auth_algorithm algo;
261 /**< Authentication algorithm selection */
264 uint8_t *data; /**< pointer to key data */
265 size_t length; /**< key length in bytes */
267 /**< Authentication key data.
268 * The authentication key length MUST be less than or equal to the
269 * block size of the algorithm. It is the callers responsibility to
270 * ensure that the key length is compliant with the standard being used
271 * (for example RFC 2104, FIPS 198a).
274 uint32_t digest_length;
275 /**< Length of the digest to be returned. If the verify option is set,
276 * this specifies the length of the digest to be compared for the
279 * If the value is less than the maximum length allowed by the hash,
280 * the result shall be truncated. If the value is greater than the
281 * maximum length allowed by the hash then an error will be generated
282 * by *rte_cryptodev_sym_session_create* or by the
283 * *rte_cryptodev_sym_enqueue_burst* if using session-less APIs.
286 uint32_t add_auth_data_length;
287 /**< The length of the additional authenticated data (AAD) in bytes.
288 * The maximum permitted value is 240 bytes, unless otherwise specified
291 * This field must be specified when the hash algorithm is one of the
294 * - For SNOW3G (@ref RTE_CRYPTO_AUTH_SNOW3G_UIA2), this is the
295 * length of the IV (which should be 16).
297 * - For GCM (@ref RTE_CRYPTO_AUTH_AES_GCM). In this case, this is
298 * the length of the Additional Authenticated Data (called A, in NIST
301 * - For CCM (@ref RTE_CRYPTO_AUTH_AES_CCM). In this case, this is
302 * the length of the associated data (called A, in NIST SP800-38C).
303 * Note that this does NOT include the length of any padding, or the
304 * 18 bytes reserved at the start of the above field to store the
305 * block B0 and the encoded length. The maximum permitted value in
306 * this case is 222 bytes.
309 * For AES-GMAC (@ref RTE_CRYPTO_AUTH_AES_GMAC) mode of operation
310 * this field is not used and should be set to 0. Instead the length
311 * of the AAD data is specified in the message length to hash field of
312 * the rte_crypto_sym_op_data structure.
316 /** Crypto transformation types */
317 enum rte_crypto_sym_xform_type {
318 RTE_CRYPTO_SYM_XFORM_NOT_SPECIFIED = 0, /**< No xform specified */
319 RTE_CRYPTO_SYM_XFORM_AUTH, /**< Authentication xform */
320 RTE_CRYPTO_SYM_XFORM_CIPHER /**< Cipher xform */
324 * Symmetric crypto transform structure.
326 * This is used to specify the crypto transforms required, multiple transforms
327 * can be chained together to specify a chain transforms such as authentication
328 * then cipher, or cipher then authentication. Each transform structure can
329 * hold a single transform, the type field is used to specify which transform
330 * is contained within the union
332 struct rte_crypto_sym_xform {
333 struct rte_crypto_sym_xform *next;
334 /**< next xform in chain */
335 enum rte_crypto_sym_xform_type type
339 struct rte_crypto_auth_xform auth;
340 /**< Authentication / hash xform */
341 struct rte_crypto_cipher_xform cipher;
347 * Crypto operation session type. This is used to specify whether a crypto
348 * operation has session structure attached for immutable parameters or if all
349 * operation information is included in the operation data structure.
351 enum rte_crypto_sym_op_sess_type {
352 RTE_CRYPTO_SYM_OP_WITH_SESSION, /**< Session based crypto operation */
353 RTE_CRYPTO_SYM_OP_SESSIONLESS /**< Session-less crypto operation */
357 struct rte_cryptodev_sym_session;
360 * Symmetric Cryptographic Operation.
362 * This structure contains data relating to performing symmetric cryptographic
363 * processing on a referenced mbuf data buffer.
365 * When a symmetric crypto operation is enqueued with the device for processing
366 * it must have a valid *rte_mbuf* structure attached, via m_src parameter,
367 * which contains the source data which the crypto operation is to be performed
370 struct rte_crypto_sym_op {
371 struct rte_mbuf *m_src; /**< source mbuf */
372 struct rte_mbuf *m_dst; /**< destination mbuf */
374 enum rte_crypto_sym_op_sess_type sess_type;
378 struct rte_cryptodev_sym_session *session;
379 /**< Handle for the initialised session context */
380 struct rte_crypto_sym_xform *xform;
381 /**< Session-less API crypto operation parameters */
387 /**< Starting point for cipher processing, specified
388 * as number of bytes from start of data in the source
389 * buffer. The result of the cipher operation will be
390 * written back into the output buffer starting at
394 * For Snow3G @ RTE_CRYPTO_CIPHER_SNOW3G_UEA2
395 * and KASUMI @ RTE_CRYPTO_CIPHER_KASUMI_F8,
396 * this field should be in bits.
400 /**< The message length, in bytes, of the source buffer
401 * on which the cryptographic operation will be
402 * computed. This must be a multiple of the block size
403 * if a block cipher is being used. This is also the
404 * same as the result length.
407 * In the case of CCM @ref RTE_CRYPTO_AUTH_AES_CCM,
408 * this value should not include the length of the
409 * padding or the length of the MAC; the driver will
410 * compute the actual number of bytes over which the
411 * encryption will occur, which will include these
415 * For AES-GMAC @ref RTE_CRYPTO_AUTH_AES_GMAC, this
416 * field should be set to 0.
419 * For Snow3G @ RTE_CRYPTO_AUTH_SNOW3G_UEA2
420 * and KASUMI @ RTE_CRYPTO_CIPHER_KASUMI_F8,
421 * this field should be in bits.
423 } data; /**< Data offsets and length for ciphering */
427 /**< Initialisation Vector or Counter.
429 * - For block ciphers in CBC or F8 mode, or for Kasumi
430 * in F8 mode, or for SNOW3G in UEA2 mode, this is the
431 * Initialisation Vector (IV) value.
433 * - For block ciphers in CTR mode, this is the counter.
435 * - For GCM mode, this is either the IV (if the length
436 * is 96 bits) or J0 (for other sizes), where J0 is as
437 * defined by NIST SP800-38D. Regardless of the IV
438 * length, a full 16 bytes needs to be allocated.
440 * - For CCM mode, the first byte is reserved, and the
441 * nonce should be written starting at &iv[1] (to allow
442 * space for the implementation to write in the flags
443 * in the first byte). Note that a full 16 bytes should
444 * be allocated, even though the length field will
445 * have a value less than this.
447 * - For AES-XTS, this is the 128bit tweak, i, from
448 * IEEE Std 1619-2007.
450 * For optimum performance, the data pointed to SHOULD
453 phys_addr_t phys_addr;
455 /**< Length of valid IV data.
457 * - For block ciphers in CBC or F8 mode, or for Kasumi
458 * in F8 mode, or for SNOW3G in UEA2 mode, this is the
459 * length of the IV (which must be the same as the
460 * block length of the cipher).
462 * - For block ciphers in CTR mode, this is the length
463 * of the counter (which must be the same as the block
464 * length of the cipher).
466 * - For GCM mode, this is either 12 (for 96-bit IVs)
467 * or 16, in which case data points to J0.
469 * - For CCM mode, this is the length of the nonce,
470 * which can be in the range 7 to 13 inclusive.
472 } iv; /**< Initialisation vector parameters */
478 /**< Starting point for hash processing, specified as
479 * number of bytes from start of packet in source
483 * For CCM and GCM modes of operation, this field is
484 * ignored. The field @ref aad field
485 * should be set instead.
487 * @note For AES-GMAC (@ref RTE_CRYPTO_AUTH_AES_GMAC)
488 * mode of operation, this field specifies the start
489 * of the AAD data in the source buffer.
492 * For Snow3G @ RTE_CRYPTO_AUTH_SNOW3G_UIA2
493 * and KASUMI @ RTE_CRYPTO_AUTH_KASUMI_F9,
494 * this field should be in bits.
498 /**< The message length, in bytes, of the source
499 * buffer that the hash will be computed on.
502 * For CCM and GCM modes of operation, this field is
503 * ignored. The field @ref aad field should be set
507 * For AES-GMAC @ref RTE_CRYPTO_AUTH_AES_GMAC mode
508 * of operation, this field specifies the length of
509 * the AAD data in the source buffer.
512 * For Snow3G @ RTE_CRYPTO_AUTH_SNOW3G_UIA2
513 * and KASUMI @ RTE_CRYPTO_AUTH_KASUMI_F9,
514 * this field should be in bits.
516 } data; /**< Data offsets and length for authentication */
520 /**< If this member of this structure is set this is a
521 * pointer to the location where the digest result
522 * should be inserted (in the case of digest generation)
523 * or where the purported digest exists (in the case of
524 * digest verification).
526 * At session creation time, the client specified the
527 * digest result length with the digest_length member
528 * of the @ref rte_crypto_auth_xform structure. For
529 * physical crypto devices the caller must allocate at
530 * least digest_length of physically contiguous memory
533 * For digest generation, the digest result will
534 * overwrite any data at this location.
537 * For GCM (@ref RTE_CRYPTO_AUTH_AES_GCM), for
538 * "digest result" read "authentication tag T".
540 * If this member is not set the digest result is
541 * understood to be in the destination buffer for
542 * digest generation, and in the source buffer for
543 * digest verification. The location of the digest
544 * result in this case is immediately following the
545 * region over which the digest is computed.
547 phys_addr_t phys_addr;
548 /**< Physical address of digest */
550 /**< Length of digest */
551 } digest; /**< Digest parameters */
555 /**< Pointer to Additional Authenticated Data (AAD)
556 * needed for authenticated cipher mechanisms (CCM and
557 * GCM), and to the IV for SNOW3G authentication
558 * (@ref RTE_CRYPTO_AUTH_SNOW3G_UIA2). For other
559 * authentication mechanisms this pointer is ignored.
561 * The length of the data pointed to by this field is
562 * set up for the session in the @ref
563 * rte_crypto_auth_xform structure as part of the @ref
564 * rte_cryptodev_sym_session_create function call.
565 * This length must not exceed 240 bytes.
567 * Specifically for CCM (@ref RTE_CRYPTO_AUTH_AES_CCM),
568 * the caller should setup this field as follows:
570 * - the nonce should be written starting at an offset
571 * of one byte into the array, leaving room for the
572 * implementation to write in the flags to the first
575 * - the additional authentication data itself should
576 * be written starting at an offset of 18 bytes into
577 * the array, leaving room for the length encoding in
578 * the first two bytes of the second block.
580 * - the array should be big enough to hold the above
581 * fields, plus any padding to round this up to the
582 * nearest multiple of the block size (16 bytes).
583 * Padding will be added by the implementation.
585 * Finally, for GCM (@ref RTE_CRYPTO_AUTH_AES_GCM), the
586 * caller should setup this field as follows:
588 * - the AAD is written in starting at byte 0
589 * - the array must be big enough to hold the AAD, plus
590 * any space to round this up to the nearest multiple
591 * of the block size (16 bytes).
594 * For AES-GMAC (@ref RTE_CRYPTO_AUTH_AES_GMAC) mode of
595 * operation, this field is not used and should be set
596 * to 0. Instead the AAD data should be placed in the
599 phys_addr_t phys_addr; /**< physical address */
600 uint16_t length; /**< Length of digest */
602 /**< Additional authentication parameters */
604 } __rte_cache_aligned;
608 * Reset the fields of a symmetric operation to their default values.
610 * @param op The crypto operation to be reset.
613 __rte_crypto_sym_op_reset(struct rte_crypto_sym_op *op)
615 memset(op, 0, sizeof(*op));
617 op->sess_type = RTE_CRYPTO_SYM_OP_SESSIONLESS;
622 * Allocate space for symmetric crypto xforms in the private data space of the
623 * crypto operation. This also defaults the crypto xform type to
624 * RTE_CRYPTO_SYM_XFORM_NOT_SPECIFIED and configures the chaining of the xforms
625 * in the crypto operation
628 * - On success returns pointer to first crypto xform in crypto operations chain
629 * - On failure returns NULL
631 static inline struct rte_crypto_sym_xform *
632 __rte_crypto_sym_op_sym_xforms_alloc(struct rte_crypto_sym_op *sym_op,
633 void *priv_data, uint8_t nb_xforms)
635 struct rte_crypto_sym_xform *xform;
637 sym_op->xform = xform = (struct rte_crypto_sym_xform *)priv_data;
640 xform->type = RTE_CRYPTO_SYM_XFORM_NOT_SPECIFIED;
641 xform = xform->next = --nb_xforms > 0 ? xform + 1 : NULL;
644 return sym_op->xform;
649 * Attach a session to a symmetric crypto operation
651 * @param sym_op crypto operation
652 * @param sess cryptodev session
655 __rte_crypto_sym_op_attach_sym_session(struct rte_crypto_sym_op *sym_op,
656 struct rte_cryptodev_sym_session *sess)
658 sym_op->session = sess;
659 sym_op->sess_type = RTE_CRYPTO_SYM_OP_WITH_SESSION;
669 #endif /* _RTE_CRYPTO_SYM_H_ */