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 */
108 /** Symmetric Cipher Direction */
109 enum rte_crypto_cipher_operation {
110 RTE_CRYPTO_CIPHER_OP_ENCRYPT,
111 /**< Encrypt cipher operation */
112 RTE_CRYPTO_CIPHER_OP_DECRYPT
113 /**< Decrypt cipher operation */
117 * Symmetric Cipher Setup Data.
119 * This structure contains data relating to Cipher (Encryption and Decryption)
120 * use to create a session.
122 struct rte_crypto_cipher_xform {
123 enum rte_crypto_cipher_operation op;
124 /**< This parameter determines if the cipher operation is an encrypt or
125 * a decrypt operation. For the RC4 algorithm and the F8/CTR modes,
126 * only encrypt operations are valid.
128 enum rte_crypto_cipher_algorithm algo;
129 /**< Cipher algorithm */
132 uint8_t *data; /**< pointer to key data */
133 size_t length; /**< key length in bytes */
137 * For the RTE_CRYPTO_CIPHER_AES_F8 mode of operation, key.data will
138 * point to a concatenation of the AES encryption key followed by a
139 * keymask. As per RFC3711, the keymask should be padded with trailing
140 * bytes to match the length of the encryption key used.
142 * For AES-XTS mode of operation, two keys must be provided and
143 * key.data must point to the two keys concatenated together (Key1 ||
144 * Key2). The cipher key length will contain the total size of both
147 * Cipher key length is in bytes. For AES it can be 128 bits (16 bytes),
148 * 192 bits (24 bytes) or 256 bits (32 bytes).
150 * For the CCM mode of operation, the only supported key length is 128
153 * For the RTE_CRYPTO_CIPHER_AES_F8 mode of operation, key.length
154 * should be set to the combined length of the encryption key and the
155 * keymask. Since the keymask and the encryption key are the same size,
156 * key.length should be set to 2 x the AES encryption key length.
158 * For the AES-XTS mode of operation:
159 * - Two keys must be provided and key.length refers to total length of
161 * - Each key can be either 128 bits (16 bytes) or 256 bits (32 bytes).
162 * - Both keys must have the same size.
166 /** Symmetric Authentication / Hash Algorithms */
167 enum rte_crypto_auth_algorithm {
168 RTE_CRYPTO_AUTH_NULL = 1,
169 /**< NULL hash algorithm. */
171 RTE_CRYPTO_AUTH_AES_CBC_MAC,
172 /**< AES-CBC-MAC algorithm. Only 128-bit keys are supported. */
173 RTE_CRYPTO_AUTH_AES_CCM,
174 /**< AES algorithm in CCM mode. This is an authenticated cipher. When
175 * this hash algorithm is used, the *RTE_CRYPTO_CIPHER_AES_CCM*
176 * element of the *rte_crypto_cipher_algorithm* enum MUST be used to
177 * set up the related rte_crypto_cipher_setup_data structure in the
178 * session context or the corresponding parameter in the crypto
179 * operation data structures op_params parameter MUST be set for a
180 * session-less crypto operation.
182 RTE_CRYPTO_AUTH_AES_CMAC,
183 /**< AES CMAC algorithm. */
184 RTE_CRYPTO_AUTH_AES_GCM,
185 /**< AES algorithm in GCM mode. When this hash algorithm
186 * is used, the RTE_CRYPTO_CIPHER_AES_GCM element of the
187 * rte_crypto_cipher_algorithm enum MUST be used to set up the related
188 * rte_crypto_cipher_setup_data structure in the session context, or
189 * the corresponding parameter in the crypto operation data structures
190 * op_params parameter MUST be set for a session-less crypto operation.
192 RTE_CRYPTO_AUTH_AES_GMAC,
193 /**< AES GMAC algorithm. When this hash algorithm
194 * is used, the RTE_CRYPTO_CIPHER_AES_GCM element of the
195 * rte_crypto_cipher_algorithm enum MUST be used to set up the related
196 * rte_crypto_cipher_setup_data structure in the session context, or
197 * the corresponding parameter in the crypto operation data structures
198 * op_params parameter MUST be set for a session-less crypto operation.
200 RTE_CRYPTO_AUTH_AES_XCBC_MAC,
201 /**< AES XCBC algorithm. */
203 RTE_CRYPTO_AUTH_KASUMI_F9,
204 /**< Kasumi algorithm in F9 mode. */
207 /**< MD5 algorithm */
208 RTE_CRYPTO_AUTH_MD5_HMAC,
209 /**< HMAC using MD5 algorithm */
211 RTE_CRYPTO_AUTH_SHA1,
212 /**< 128 bit SHA algorithm. */
213 RTE_CRYPTO_AUTH_SHA1_HMAC,
214 /**< HMAC using 128 bit SHA algorithm. */
215 RTE_CRYPTO_AUTH_SHA224,
216 /**< 224 bit SHA algorithm. */
217 RTE_CRYPTO_AUTH_SHA224_HMAC,
218 /**< HMAC using 224 bit SHA algorithm. */
219 RTE_CRYPTO_AUTH_SHA256,
220 /**< 256 bit SHA algorithm. */
221 RTE_CRYPTO_AUTH_SHA256_HMAC,
222 /**< HMAC using 256 bit SHA algorithm. */
223 RTE_CRYPTO_AUTH_SHA384,
224 /**< 384 bit SHA algorithm. */
225 RTE_CRYPTO_AUTH_SHA384_HMAC,
226 /**< HMAC using 384 bit SHA algorithm. */
227 RTE_CRYPTO_AUTH_SHA512,
228 /**< 512 bit SHA algorithm. */
229 RTE_CRYPTO_AUTH_SHA512_HMAC,
230 /**< HMAC using 512 bit SHA algorithm. */
232 RTE_CRYPTO_AUTH_SNOW3G_UIA2,
233 /**< SNOW3G algorithm in UIA2 mode. */
235 RTE_CRYPTO_AUTH_ZUC_EIA3,
236 /**< ZUC algorithm in EIA3 mode */
239 /** Symmetric Authentication / Hash Operations */
240 enum rte_crypto_auth_operation {
241 RTE_CRYPTO_AUTH_OP_VERIFY, /**< Verify authentication digest */
242 RTE_CRYPTO_AUTH_OP_GENERATE /**< Generate authentication digest */
246 * Authentication / Hash transform data.
248 * This structure contains data relating to an authentication/hash crypto
249 * transforms. The fields op, algo and digest_length are common to all
250 * authentication transforms and MUST be set.
252 struct rte_crypto_auth_xform {
253 enum rte_crypto_auth_operation op;
254 /**< Authentication operation type */
255 enum rte_crypto_auth_algorithm algo;
256 /**< Authentication algorithm selection */
259 uint8_t *data; /**< pointer to key data */
260 size_t length; /**< key length in bytes */
262 /**< Authentication key data.
263 * The authentication key length MUST be less than or equal to the
264 * block size of the algorithm. It is the callers responsibility to
265 * ensure that the key length is compliant with the standard being used
266 * (for example RFC 2104, FIPS 198a).
269 uint32_t digest_length;
270 /**< Length of the digest to be returned. If the verify option is set,
271 * this specifies the length of the digest to be compared for the
274 * If the value is less than the maximum length allowed by the hash,
275 * the result shall be truncated. If the value is greater than the
276 * maximum length allowed by the hash then an error will be generated
277 * by *rte_cryptodev_sym_session_create* or by the
278 * *rte_cryptodev_sym_enqueue_burst* if using session-less APIs.
281 uint32_t add_auth_data_length;
282 /**< The length of the additional authenticated data (AAD) in bytes.
283 * The maximum permitted value is 240 bytes, unless otherwise specified
286 * This field must be specified when the hash algorithm is one of the
289 * - For SNOW3G (@ref RTE_CRYPTO_AUTH_SNOW3G_UIA2), this is the
290 * length of the IV (which should be 16).
292 * - For GCM (@ref RTE_CRYPTO_AUTH_AES_GCM). In this case, this is
293 * the length of the Additional Authenticated Data (called A, in NIST
296 * - For CCM (@ref RTE_CRYPTO_AUTH_AES_CCM). In this case, this is
297 * the length of the associated data (called A, in NIST SP800-38C).
298 * Note that this does NOT include the length of any padding, or the
299 * 18 bytes reserved at the start of the above field to store the
300 * block B0 and the encoded length. The maximum permitted value in
301 * this case is 222 bytes.
304 * For AES-GMAC (@ref RTE_CRYPTO_AUTH_AES_GMAC) mode of operation
305 * this field is not used and should be set to 0. Instead the length
306 * of the AAD data is specified in the message length to hash field of
307 * the rte_crypto_sym_op_data structure.
311 /** Crypto transformation types */
312 enum rte_crypto_sym_xform_type {
313 RTE_CRYPTO_SYM_XFORM_NOT_SPECIFIED = 0, /**< No xform specified */
314 RTE_CRYPTO_SYM_XFORM_AUTH, /**< Authentication xform */
315 RTE_CRYPTO_SYM_XFORM_CIPHER /**< Cipher xform */
319 * Symmetric crypto transform structure.
321 * This is used to specify the crypto transforms required, multiple transforms
322 * can be chained together to specify a chain transforms such as authentication
323 * then cipher, or cipher then authentication. Each transform structure can
324 * hold a single transform, the type field is used to specify which transform
325 * is contained within the union
327 struct rte_crypto_sym_xform {
328 struct rte_crypto_sym_xform *next;
329 /**< next xform in chain */
330 enum rte_crypto_sym_xform_type type
333 struct rte_crypto_auth_xform auth;
334 /**< Authentication / hash xform */
335 struct rte_crypto_cipher_xform cipher;
341 * Crypto operation session type. This is used to specify whether a crypto
342 * operation has session structure attached for immutable parameters or if all
343 * operation information is included in the operation data structure.
345 enum rte_crypto_sym_op_sess_type {
346 RTE_CRYPTO_SYM_OP_WITH_SESSION, /**< Session based crypto operation */
347 RTE_CRYPTO_SYM_OP_SESSIONLESS /**< Session-less crypto operation */
351 struct rte_cryptodev_sym_session;
354 * Symmetric Cryptographic Operation.
356 * This structure contains data relating to performing symmetric cryptographic
357 * processing on a referenced mbuf data buffer.
359 * When a symmetric crypto operation is enqueued with the device for processing
360 * it must have a valid *rte_mbuf* structure attached, via m_src parameter,
361 * which contains the source data which the crypto operation is to be performed
364 struct rte_crypto_sym_op {
365 struct rte_mbuf *m_src; /**< source mbuf */
366 struct rte_mbuf *m_dst; /**< destination mbuf */
368 enum rte_crypto_sym_op_sess_type type;
371 struct rte_cryptodev_sym_session *session;
372 /**< Handle for the initialised session context */
373 struct rte_crypto_sym_xform *xform;
374 /**< Session-less API crypto operation parameters */
380 /**< Starting point for cipher processing, specified
381 * as number of bytes from start of data in the source
382 * buffer. The result of the cipher operation will be
383 * written back into the output buffer starting at
388 /**< The message length, in bytes, of the source buffer
389 * on which the cryptographic operation will be
390 * computed. This must be a multiple of the block size
391 * if a block cipher is being used. This is also the
392 * same as the result length.
395 * In the case of CCM @ref RTE_CRYPTO_AUTH_AES_CCM,
396 * this value should not include the length of the
397 * padding or the length of the MAC; the driver will
398 * compute the actual number of bytes over which the
399 * encryption will occur, which will include these
403 * For AES-GMAC @ref RTE_CRYPTO_AUTH_AES_GMAC, this
404 * field should be set to 0.
406 } data; /**< Data offsets and length for ciphering */
410 /**< Initialisation Vector or Counter.
412 * - For block ciphers in CBC or F8 mode, or for Kasumi
413 * in F8 mode, or for SNOW3G in UEA2 mode, this is the
414 * Initialisation Vector (IV) value.
416 * - For block ciphers in CTR mode, this is the counter.
418 * - For GCM mode, this is either the IV (if the length
419 * is 96 bits) or J0 (for other sizes), where J0 is as
420 * defined by NIST SP800-38D. Regardless of the IV
421 * length, a full 16 bytes needs to be allocated.
423 * - For CCM mode, the first byte is reserved, and the
424 * nonce should be written starting at &iv[1] (to allow
425 * space for the implementation to write in the flags
426 * in the first byte). Note that a full 16 bytes should
427 * be allocated, even though the length field will
428 * have a value less than this.
430 * - For AES-XTS, this is the 128bit tweak, i, from
431 * IEEE Std 1619-2007.
433 * For optimum performance, the data pointed to SHOULD
436 phys_addr_t phys_addr;
438 /**< Length of valid IV data.
440 * - For block ciphers in CBC or F8 mode, or for Kasumi
441 * in F8 mode, or for SNOW3G in UEA2 mode, this is the
442 * length of the IV (which must be the same as the
443 * block length of the cipher).
445 * - For block ciphers in CTR mode, this is the length
446 * of the counter (which must be the same as the block
447 * length of the cipher).
449 * - For GCM mode, this is either 12 (for 96-bit IVs)
450 * or 16, in which case data points to J0.
452 * - For CCM mode, this is the length of the nonce,
453 * which can be in the range 7 to 13 inclusive.
455 } iv; /**< Initialisation vector parameters */
461 /**< Starting point for hash processing, specified as
462 * number of bytes from start of packet in source
466 * For CCM and GCM modes of operation, this field is
467 * ignored. The field @ref aad field
468 * should be set instead.
470 * @note For AES-GMAC (@ref RTE_CRYPTO_AUTH_AES_GMAC)
471 * mode of operation, this field specifies the start
472 * of the AAD data in the source buffer.
476 /**< The message length, in bytes, of the source
477 * buffer that the hash will be computed on.
480 * For CCM and GCM modes of operation, this field is
481 * ignored. The field @ref aad field should be set
485 * For AES-GMAC @ref RTE_CRYPTO_AUTH_AES_GMAC mode
486 * of operation, this field specifies the length of
487 * the AAD data in the source buffer.
489 } data; /**< Data offsets and length for authentication */
493 /**< If this member of this structure is set this is a
494 * pointer to the location where the digest result
495 * should be inserted (in the case of digest generation)
496 * or where the purported digest exists (in the case of
497 * digest verification).
499 * At session creation time, the client specified the
500 * digest result length with the digest_length member
501 * of the @ref rte_crypto_auth_xform structure. For
502 * physical crypto devices the caller must allocate at
503 * least digest_length of physically contiguous memory
506 * For digest generation, the digest result will
507 * overwrite any data at this location.
510 * For GCM (@ref RTE_CRYPTO_AUTH_AES_GCM), for
511 * "digest result" read "authentication tag T".
513 * If this member is not set the digest result is
514 * understood to be in the destination buffer for
515 * digest generation, and in the source buffer for
516 * digest verification. The location of the digest
517 * result in this case is immediately following the
518 * region over which the digest is computed.
520 phys_addr_t phys_addr;
521 /**< Physical address of digest */
523 /**< Length of digest */
524 } digest; /**< Digest parameters */
528 /**< Pointer to Additional Authenticated Data (AAD)
529 * needed for authenticated cipher mechanisms (CCM and
530 * GCM), and to the IV for SNOW3G authentication
531 * (@ref RTE_CRYPTO_AUTH_SNOW3G_UIA2). For other
532 * authentication mechanisms this pointer is ignored.
534 * The length of the data pointed to by this field is
535 * set up for the session in the @ref
536 * rte_crypto_auth_xform structure as part of the @ref
537 * rte_cryptodev_sym_session_create function call.
538 * This length must not exceed 240 bytes.
540 * Specifically for CCM (@ref RTE_CRYPTO_AUTH_AES_CCM),
541 * the caller should setup this field as follows:
543 * - the nonce should be written starting at an offset
544 * of one byte into the array, leaving room for the
545 * implementation to write in the flags to the first
548 * - the additional authentication data itself should
549 * be written starting at an offset of 18 bytes into
550 * the array, leaving room for the length encoding in
551 * the first two bytes of the second block.
553 * - the array should be big enough to hold the above
554 * fields, plus any padding to round this up to the
555 * nearest multiple of the block size (16 bytes).
556 * Padding will be added by the implementation.
558 * Finally, for GCM (@ref RTE_CRYPTO_AUTH_AES_GCM), the
559 * caller should setup this field as follows:
561 * - the AAD is written in starting at byte 0
562 * - the array must be big enough to hold the AAD, plus
563 * any space to round this up to the nearest multiple
564 * of the block size (16 bytes).
567 * For AES-GMAC (@ref RTE_CRYPTO_AUTH_AES_GMAC) mode of
568 * operation, this field is not used and should be set
569 * to 0. Instead the AAD data should be placed in the
572 phys_addr_t phys_addr; /**< physical address */
573 uint16_t length; /**< Length of digest */
575 /**< Additional authentication parameters */
577 } __rte_cache_aligned;
581 * Reset the fields of a symmetric operation to their default values.
583 * @param op The crypto operation to be reset.
586 __rte_crypto_sym_op_reset(struct rte_crypto_sym_op *op)
588 memset(op, 0, sizeof(*op));
590 op->type = RTE_CRYPTO_SYM_OP_SESSIONLESS;
595 * Allocate space for symmetric crypto xforms in the private data space of the
596 * crypto operation. This also defaults the crypto xform type to
597 * RTE_CRYPTO_SYM_XFORM_NOT_SPECIFIED and configures the chaining of the xforms
598 * in the crypto operation
601 * - On success returns pointer to first crypto xform in crypto operations chain
602 * - On failure returns NULL
604 static inline struct rte_crypto_sym_xform *
605 __rte_crypto_sym_op_sym_xforms_alloc(struct rte_crypto_sym_op *sym_op,
606 void *priv_data, uint8_t nb_xforms)
608 struct rte_crypto_sym_xform *xform;
610 sym_op->xform = xform = (struct rte_crypto_sym_xform *)priv_data;
613 xform->type = RTE_CRYPTO_SYM_XFORM_NOT_SPECIFIED;
614 xform = xform->next = --nb_xforms > 0 ? xform + 1 : NULL;
617 return sym_op->xform;
622 * Attach a session to a symmetric crypto operation
624 * @param sym_op crypto operation
625 * @param sess cryptodev session
628 __rte_crypto_sym_op_attach_sym_session(struct rte_crypto_sym_op *sym_op,
629 struct rte_cryptodev_sym_session *sess)
631 sym_op->session = sess;
632 sym_op->type = RTE_CRYPTO_SYM_OP_WITH_SESSION;
642 #endif /* _RTE_CRYPTO_SYM_H_ */