1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Advanced Micro Devices, Inc. All rights reserved.
10 #include <sys/queue.h>
11 #include <sys/types.h>
14 #include <rte_hexdump.h>
15 #include <rte_memzone.h>
16 #include <rte_malloc.h>
17 #include <rte_memory.h>
18 #include <rte_spinlock.h>
19 #include <rte_string_fns.h>
20 #include <rte_cryptodev_pmd.h>
23 #include "ccp_crypto.h"
25 #include "ccp_pmd_private.h"
27 static enum ccp_cmd_order
28 ccp_get_cmd_id(const struct rte_crypto_sym_xform *xform)
30 enum ccp_cmd_order res = CCP_CMD_NOT_SUPPORTED;
34 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
35 if (xform->next == NULL)
37 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
38 return CCP_CMD_HASH_CIPHER;
40 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
41 if (xform->next == NULL)
42 return CCP_CMD_CIPHER;
43 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
44 return CCP_CMD_CIPHER_HASH;
46 if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD)
47 return CCP_CMD_COMBINED;
51 /* configure session */
53 ccp_configure_session_cipher(struct ccp_session *sess,
54 const struct rte_crypto_sym_xform *xform)
56 const struct rte_crypto_cipher_xform *cipher_xform = NULL;
58 cipher_xform = &xform->cipher;
60 /* set cipher direction */
61 if (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
62 sess->cipher.dir = CCP_CIPHER_DIR_ENCRYPT;
64 sess->cipher.dir = CCP_CIPHER_DIR_DECRYPT;
67 sess->cipher.key_length = cipher_xform->key.length;
68 rte_memcpy(sess->cipher.key, cipher_xform->key.data,
69 cipher_xform->key.length);
71 /* set iv parameters */
72 sess->iv.offset = cipher_xform->iv.offset;
73 sess->iv.length = cipher_xform->iv.length;
75 switch (cipher_xform->algo) {
77 CCP_LOG_ERR("Unsupported cipher algo");
82 switch (sess->cipher.engine) {
84 CCP_LOG_ERR("Invalid CCP Engine");
91 ccp_configure_session_auth(struct ccp_session *sess,
92 const struct rte_crypto_sym_xform *xform)
94 const struct rte_crypto_auth_xform *auth_xform = NULL;
96 auth_xform = &xform->auth;
98 sess->auth.digest_length = auth_xform->digest_length;
99 if (auth_xform->op == RTE_CRYPTO_AUTH_OP_GENERATE)
100 sess->auth.op = CCP_AUTH_OP_GENERATE;
102 sess->auth.op = CCP_AUTH_OP_VERIFY;
103 switch (auth_xform->algo) {
105 CCP_LOG_ERR("Unsupported hash algo");
112 ccp_configure_session_aead(struct ccp_session *sess,
113 const struct rte_crypto_sym_xform *xform)
115 const struct rte_crypto_aead_xform *aead_xform = NULL;
117 aead_xform = &xform->aead;
119 sess->cipher.key_length = aead_xform->key.length;
120 rte_memcpy(sess->cipher.key, aead_xform->key.data,
121 aead_xform->key.length);
123 if (aead_xform->op == RTE_CRYPTO_AEAD_OP_ENCRYPT) {
124 sess->cipher.dir = CCP_CIPHER_DIR_ENCRYPT;
125 sess->auth.op = CCP_AUTH_OP_GENERATE;
127 sess->cipher.dir = CCP_CIPHER_DIR_DECRYPT;
128 sess->auth.op = CCP_AUTH_OP_VERIFY;
130 sess->auth.aad_length = aead_xform->aad_length;
131 sess->auth.digest_length = aead_xform->digest_length;
133 /* set iv parameters */
134 sess->iv.offset = aead_xform->iv.offset;
135 sess->iv.length = aead_xform->iv.length;
137 switch (aead_xform->algo) {
139 CCP_LOG_ERR("Unsupported aead algo");
146 ccp_set_session_parameters(struct ccp_session *sess,
147 const struct rte_crypto_sym_xform *xform)
149 const struct rte_crypto_sym_xform *cipher_xform = NULL;
150 const struct rte_crypto_sym_xform *auth_xform = NULL;
151 const struct rte_crypto_sym_xform *aead_xform = NULL;
154 sess->cmd_id = ccp_get_cmd_id(xform);
156 switch (sess->cmd_id) {
158 cipher_xform = xform;
163 case CCP_CMD_CIPHER_HASH:
164 cipher_xform = xform;
165 auth_xform = xform->next;
167 case CCP_CMD_HASH_CIPHER:
169 cipher_xform = xform->next;
171 case CCP_CMD_COMBINED:
175 CCP_LOG_ERR("Unsupported cmd_id");
179 /* Default IV length = 0 */
182 ret = ccp_configure_session_cipher(sess, cipher_xform);
184 CCP_LOG_ERR("Invalid/unsupported cipher parameters");
189 ret = ccp_configure_session_auth(sess, auth_xform);
191 CCP_LOG_ERR("Invalid/unsupported auth parameters");
196 ret = ccp_configure_session_aead(sess, aead_xform);
198 CCP_LOG_ERR("Invalid/unsupported aead parameters");