Branch data Line data Source code
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
17 : 0 : mlx5_crypto_dek_get_key(struct rte_crypto_sym_xform *xform,
18 : : const uint8_t **key,
19 : : uint16_t *key_len)
20 : : {
21 [ # # ]: 0 : if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
22 : 0 : *key = xform->cipher.key.data;
23 : 0 : *key_len = xform->cipher.key.length;
24 [ # # ]: 0 : } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
25 : 0 : *key = xform->aead.key.data;
26 : 0 : *key_len = xform->aead.key.length;
27 : : } else {
28 : 0 : *key = NULL;
29 : 0 : *key_len = 0;
30 : 0 : DRV_LOG(ERR, "Xform dek type not supported.");
31 : 0 : rte_errno = -EINVAL;
32 : 0 : return -1;
33 : : }
34 : : return 0;
35 : : }
36 : :
37 : : int
38 : 0 : mlx5_crypto_dek_destroy(struct mlx5_crypto_priv *priv,
39 : : struct mlx5_crypto_dek *dek)
40 : : {
41 : 0 : return mlx5_hlist_unregister(priv->dek_hlist, &dek->entry);
42 : : }
43 : :
44 : : struct mlx5_crypto_dek *
45 : 0 : 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 : 0 : struct mlx5_hlist *dek_hlist = priv->dek_hlist;
51 : 0 : 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 [ # # ]: 0 : if (mlx5_crypto_dek_get_key(xform, &key, &key_len))
59 : : return NULL;
60 : 0 : key64 = __rte_raw_cksum(key, key_len, 0);
61 : 0 : entry = mlx5_hlist_register(dek_hlist, key64, &dek_ctx);
62 [ # # ]: 0 : return entry == NULL ? NULL :
63 : : container_of(entry, struct mlx5_crypto_dek, entry);
64 : : }
65 : :
66 : : static struct mlx5_list_entry *
67 : 0 : mlx5_crypto_dek_clone_cb(void *tool_ctx __rte_unused,
68 : : struct mlx5_list_entry *oentry,
69 : : void *cb_ctx __rte_unused)
70 : : {
71 : 0 : struct mlx5_crypto_dek *entry = rte_zmalloc(__func__, sizeof(*entry),
72 : : RTE_CACHE_LINE_SIZE);
73 : :
74 [ # # ]: 0 : if (!entry) {
75 : 0 : DRV_LOG(ERR, "Cannot allocate dek resource memory.");
76 : 0 : rte_errno = ENOMEM;
77 : 0 : return NULL;
78 : : }
79 : : memcpy(entry, oentry, sizeof(*entry));
80 : 0 : return &entry->entry;
81 : : }
82 : :
83 : : static void
84 : 0 : 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 : 0 : rte_free(dek);
91 : 0 : }
92 : :
93 : : static int
94 : 0 : 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 : 0 : struct rte_crypto_sym_xform *xform = ctx->xform;
99 : : struct mlx5_crypto_dek *dek =
100 : : container_of(entry, typeof(*dek), entry);
101 : 0 : uint32_t key_len = dek->size;
102 : : uint16_t xkey_len;
103 : : const uint8_t *key;
104 : :
105 [ # # ]: 0 : if (mlx5_crypto_dek_get_key(xform, &key, &xkey_len))
106 : : return -1;
107 [ # # ]: 0 : if (key_len != xkey_len)
108 : : return -1;
109 : 0 : return memcmp(key, dek->data, xkey_len);
110 : : }
111 : :
112 : : static struct mlx5_list_entry *
113 : 0 : mlx5_crypto_dek_create_cb(void *tool_ctx __rte_unused, void *cb_ctx)
114 : : {
115 : : struct mlx5_crypto_dek_ctx *ctx = cb_ctx;
116 : 0 : struct rte_crypto_sym_xform *xform = ctx->xform;
117 : 0 : struct mlx5_crypto_dek *dek = rte_zmalloc(__func__, sizeof(*dek),
118 : : RTE_CACHE_LINE_SIZE);
119 : 0 : struct mlx5_devx_dek_attr dek_attr = {
120 : 0 : .pd = ctx->priv->cdev->pdn,
121 : : };
122 : : int ret = -1;
123 : :
124 [ # # ]: 0 : if (dek == NULL) {
125 : 0 : DRV_LOG(ERR, "Failed to allocate dek memory.");
126 : 0 : return NULL;
127 : : }
128 [ # # ]: 0 : if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
129 : 0 : ret = mlx5_crypto_dek_fill_xts_attr(dek, &dek_attr, cb_ctx);
130 [ # # ]: 0 : else if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD)
131 : 0 : ret = mlx5_crypto_dek_fill_gcm_attr(dek, &dek_attr, cb_ctx);
132 [ # # ]: 0 : if (ret)
133 : 0 : goto fail;
134 : 0 : dek->obj = mlx5_devx_cmd_create_dek_obj(ctx->priv->cdev->ctx,
135 : : &dek_attr);
136 [ # # ]: 0 : if (dek->obj == NULL) {
137 : 0 : DRV_LOG(ERR, "Failed to create dek obj.");
138 : 0 : goto fail;
139 : : }
140 : 0 : return &dek->entry;
141 : 0 : fail:
142 : 0 : rte_free(dek);
143 : 0 : return NULL;
144 : : }
145 : :
146 : :
147 : : static void
148 : 0 : 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 : 0 : claim_zero(mlx5_devx_cmd_destroy(dek->obj));
155 : 0 : rte_free(dek);
156 : 0 : }
157 : :
158 : : int
159 : 0 : mlx5_crypto_dek_setup(struct mlx5_crypto_priv *priv)
160 : : {
161 : 0 : 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 [ # # ]: 0 : if (priv->dek_hlist == NULL)
169 : 0 : return -1;
170 : : return 0;
171 : : }
172 : :
173 : : void
174 : 0 : mlx5_crypto_dek_unset(struct mlx5_crypto_priv *priv)
175 : : {
176 [ # # ]: 0 : if (priv->dek_hlist) {
177 : 0 : mlx5_hlist_destroy(priv->dek_hlist);
178 : 0 : priv->dek_hlist = NULL;
179 : : }
180 : 0 : }
|