1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2021 NVIDIA Corporation & Affiliates
3 */
4
5 #include <rte_ip.h>
6 #include <rte_common.h>
7 #include <rte_errno.h>
8 #include <rte_log.h>
9
10 #include <mlx5_prm.h>
11 #include <mlx5_devx_cmds.h>
12
13 #include "mlx5_crypto_utils.h"
14 #include "mlx5_crypto.h"
15
16 static int
mlx5_crypto_dek_get_key(struct rte_crypto_sym_xform * xform,const uint8_t ** key,uint16_t * key_len)17 mlx5_crypto_dek_get_key(struct rte_crypto_sym_xform *xform,
18 const uint8_t **key,
19 uint16_t *key_len)
20 {
21 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
22 *key = xform->cipher.key.data;
23 *key_len = xform->cipher.key.length;
24 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
25 *key = xform->aead.key.data;
26 *key_len = xform->aead.key.length;
27 } else {
28 *key = NULL;
29 *key_len = 0;
30 DRV_LOG(ERR, "Xform dek type not supported.");
31 rte_errno = -EINVAL;
32 return -1;
33 }
34 return 0;
35 }
36
37 int
mlx5_crypto_dek_destroy(struct mlx5_crypto_priv * priv,struct mlx5_crypto_dek * dek)38 mlx5_crypto_dek_destroy(struct mlx5_crypto_priv *priv,
39 struct mlx5_crypto_dek *dek)
40 {
41 return mlx5_hlist_unregister(priv->dek_hlist, &dek->entry);
42 }
43
44 struct mlx5_crypto_dek *
mlx5_crypto_dek_prepare(struct mlx5_crypto_priv * priv,struct rte_crypto_sym_xform * xform)45 mlx5_crypto_dek_prepare(struct mlx5_crypto_priv *priv,
46 struct rte_crypto_sym_xform *xform)
47 {
48 const uint8_t *key;
49 uint16_t key_len;
50 struct mlx5_hlist *dek_hlist = priv->dek_hlist;
51 struct mlx5_crypto_dek_ctx dek_ctx = {
52 .xform = xform,
53 .priv = priv,
54 };
55 uint64_t key64;
56 struct mlx5_list_entry *entry;
57
58 if (mlx5_crypto_dek_get_key(xform, &key, &key_len))
59 return NULL;
60 key64 = __rte_raw_cksum(key, key_len, 0);
61 entry = mlx5_hlist_register(dek_hlist, key64, &dek_ctx);
62 return entry == NULL ? NULL :
63 container_of(entry, struct mlx5_crypto_dek, entry);
64 }
65
66 static struct mlx5_list_entry *
mlx5_crypto_dek_clone_cb(void * tool_ctx __rte_unused,struct mlx5_list_entry * oentry,void * cb_ctx __rte_unused)67 mlx5_crypto_dek_clone_cb(void *tool_ctx __rte_unused,
68 struct mlx5_list_entry *oentry,
69 void *cb_ctx __rte_unused)
70 {
71 struct mlx5_crypto_dek *entry = rte_zmalloc(__func__, sizeof(*entry),
72 RTE_CACHE_LINE_SIZE);
73
74 if (!entry) {
75 DRV_LOG(ERR, "Cannot allocate dek resource memory.");
76 rte_errno = ENOMEM;
77 return NULL;
78 }
79 memcpy(entry, oentry, sizeof(*entry));
80 return &entry->entry;
81 }
82
83 static void
mlx5_crypto_dek_clone_free_cb(void * tool_ctx __rte_unused,struct mlx5_list_entry * entry)84 mlx5_crypto_dek_clone_free_cb(void *tool_ctx __rte_unused,
85 struct mlx5_list_entry *entry)
86 {
87 struct mlx5_crypto_dek *dek = container_of(entry,
88 struct mlx5_crypto_dek, entry);
89
90 rte_free(dek);
91 }
92
93 static int
mlx5_crypto_dek_match_cb(void * tool_ctx __rte_unused,struct mlx5_list_entry * entry,void * cb_ctx)94 mlx5_crypto_dek_match_cb(void *tool_ctx __rte_unused,
95 struct mlx5_list_entry *entry, void *cb_ctx)
96 {
97 struct mlx5_crypto_dek_ctx *ctx = cb_ctx;
98 struct rte_crypto_sym_xform *xform = ctx->xform;
99 struct mlx5_crypto_dek *dek =
100 container_of(entry, typeof(*dek), entry);
101 uint32_t key_len = dek->size;
102 uint16_t xkey_len;
103 const uint8_t *key;
104
105 if (mlx5_crypto_dek_get_key(xform, &key, &xkey_len))
106 return -1;
107 if (key_len != xkey_len)
108 return -1;
109 return memcmp(key, dek->data, xkey_len);
110 }
111
112 static struct mlx5_list_entry *
mlx5_crypto_dek_create_cb(void * tool_ctx __rte_unused,void * cb_ctx)113 mlx5_crypto_dek_create_cb(void *tool_ctx __rte_unused, void *cb_ctx)
114 {
115 struct mlx5_crypto_dek_ctx *ctx = cb_ctx;
116 struct rte_crypto_sym_xform *xform = ctx->xform;
117 struct mlx5_crypto_dek *dek = rte_zmalloc(__func__, sizeof(*dek),
118 RTE_CACHE_LINE_SIZE);
119 struct mlx5_devx_dek_attr dek_attr = {
120 .pd = ctx->priv->cdev->pdn,
121 };
122 int ret = -1;
123
124 if (dek == NULL) {
125 DRV_LOG(ERR, "Failed to allocate dek memory.");
126 return NULL;
127 }
128 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
129 ret = mlx5_crypto_dek_fill_xts_attr(dek, &dek_attr, cb_ctx);
130 else if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD)
131 ret = mlx5_crypto_dek_fill_gcm_attr(dek, &dek_attr, cb_ctx);
132 if (ret)
133 goto fail;
134 dek->obj = mlx5_devx_cmd_create_dek_obj(ctx->priv->cdev->ctx,
135 &dek_attr);
136 if (dek->obj == NULL) {
137 DRV_LOG(ERR, "Failed to create dek obj.");
138 goto fail;
139 }
140 return &dek->entry;
141 fail:
142 rte_free(dek);
143 return NULL;
144 }
145
146
147 static void
mlx5_crypto_dek_remove_cb(void * tool_ctx __rte_unused,struct mlx5_list_entry * entry)148 mlx5_crypto_dek_remove_cb(void *tool_ctx __rte_unused,
149 struct mlx5_list_entry *entry)
150 {
151 struct mlx5_crypto_dek *dek =
152 container_of(entry, typeof(*dek), entry);
153
154 claim_zero(mlx5_devx_cmd_destroy(dek->obj));
155 rte_free(dek);
156 }
157
158 int
mlx5_crypto_dek_setup(struct mlx5_crypto_priv * priv)159 mlx5_crypto_dek_setup(struct mlx5_crypto_priv *priv)
160 {
161 priv->dek_hlist = mlx5_hlist_create("dek_hlist",
162 MLX5_CRYPTO_DEK_HTABLE_SZ,
163 0, 1, NULL, mlx5_crypto_dek_create_cb,
164 mlx5_crypto_dek_match_cb,
165 mlx5_crypto_dek_remove_cb,
166 mlx5_crypto_dek_clone_cb,
167 mlx5_crypto_dek_clone_free_cb);
168 if (priv->dek_hlist == NULL)
169 return -1;
170 return 0;
171 }
172
173 void
mlx5_crypto_dek_unset(struct mlx5_crypto_priv * priv)174 mlx5_crypto_dek_unset(struct mlx5_crypto_priv *priv)
175 {
176 if (priv->dek_hlist) {
177 mlx5_hlist_destroy(priv->dek_hlist);
178 priv->dek_hlist = NULL;
179 }
180 }
181