Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2019 - 2022 Intel Corporation
3 : : */
4 : :
5 : : #include <stdarg.h>
6 : :
7 : : #include <cryptodev_pmd.h>
8 : :
9 : : #include "qat_device.h"
10 : : #include "qat_logs.h"
11 : :
12 : : #include "qat_asym.h"
13 : : #include "icp_qat_fw_pke.h"
14 : : #include "icp_qat_fw.h"
15 : : #include "qat_pke.h"
16 : : #include "qat_ec.h"
17 : :
18 : : #define RSA_MODULUS_2048_BITS 2048
19 : :
20 : : uint8_t qat_asym_driver_id;
21 : :
22 : : struct qat_crypto_gen_dev_ops qat_asym_gen_dev_ops[QAT_N_GENS];
23 : :
24 : : /* An rte_driver is needed in the registration of both the device and the driver
25 : : * with cryptodev.
26 : : * The actual qat pci's rte_driver can't be used as its name represents
27 : : * the whole pci device with all services. Think of this as a holder for a name
28 : : * for the crypto part of the pci device.
29 : : */
30 : : static const char qat_asym_drv_name[] = RTE_STR(CRYPTODEV_NAME_QAT_ASYM_PMD);
31 : : static const struct rte_driver cryptodev_qat_asym_driver = {
32 : : .name = qat_asym_drv_name,
33 : : .alias = qat_asym_drv_name
34 : : };
35 : :
36 : : /*
37 : : * Macros with suffix _F are used with some of predefinded identifiers:
38 : : * - cookie->input_buffer
39 : : * - qat_func_alignsize
40 : : */
41 : : #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
42 : : #define HEXDUMP(name, where, size) QAT_DP_HEXDUMP_LOG(DEBUG, name, \
43 : : where, size)
44 : : #define HEXDUMP_OFF(name, where, size, idx) QAT_DP_HEXDUMP_LOG(DEBUG, name, \
45 : : &where[idx * size], size)
46 : :
47 : : #define HEXDUMP_OFF_F(name, idx) QAT_DP_HEXDUMP_LOG(DEBUG, name, \
48 : : &cookie->input_buffer[idx * qat_func_alignsize], \
49 : : qat_func_alignsize)
50 : : #else
51 : : #define HEXDUMP(name, where, size)
52 : : #define HEXDUMP_OFF(name, where, size, idx)
53 : : #define HEXDUMP_OFF_F(name, idx)
54 : : #endif
55 : :
56 : : #define CHECK_IF_NOT_EMPTY(param, name, pname, status) \
57 : : do { \
58 : : if (param.length == 0) { \
59 : : QAT_LOG(ERR, \
60 : : "Invalid " name \
61 : : " input parameter, zero length " pname \
62 : : ); \
63 : : status = -EINVAL; \
64 : : } else if (check_zero(param)) { \
65 : : QAT_LOG(ERR, \
66 : : "Invalid " name " input parameter, empty " \
67 : : pname ", length = %d", \
68 : : (int)param.length \
69 : : ); \
70 : : status = -EINVAL; \
71 : : } \
72 : : } while (0)
73 : :
74 : : #define SET_PKE_LN(what, how, idx) \
75 : : rte_memcpy(cookie->input_array[idx] + how - \
76 : : what.length, \
77 : : what.data, \
78 : : what.length)
79 : :
80 : : #define SET_PKE_LN_EC(curve, p, idx) \
81 : : rte_memcpy(cookie->input_array[idx] + \
82 : : qat_func_alignsize - curve.bytesize, \
83 : : curve.p.data, curve.bytesize)
84 : :
85 : : #define SET_PKE_9A_IN(what, idx) \
86 : : rte_memcpy(&cookie->input_buffer[idx * \
87 : : qat_func_alignsize] + \
88 : : qat_func_alignsize - what.length, \
89 : : what.data, what.length)
90 : :
91 : : #define SET_PKE_9A_EC(curve, p, idx) \
92 : : rte_memcpy(&cookie->input_buffer[idx * \
93 : : qat_func_alignsize] + \
94 : : qat_func_alignsize - curve.bytesize, \
95 : : curve.p.data, curve.bytesize)
96 : :
97 : : #define PARAM_CLR(what) \
98 : : do { \
99 : : memset(what.data, 0, what.length); \
100 : : rte_free(what.data); \
101 : : } while (0)
102 : :
103 : : static void
104 : : request_init(struct icp_qat_fw_pke_request *qat_req)
105 : : {
106 : : memset(qat_req, 0, sizeof(*qat_req));
107 : 0 : qat_req->pke_hdr.service_type = ICP_QAT_FW_COMN_REQ_CPM_FW_PKE;
108 : 0 : qat_req->pke_hdr.hdr_flags =
109 : : ICP_QAT_FW_COMN_HDR_FLAGS_BUILD
110 : : (ICP_QAT_FW_COMN_REQ_FLAG_SET);
111 : : }
112 : :
113 : : static void
114 : 0 : cleanup_arrays(struct qat_asym_op_cookie *cookie,
115 : : int in_count, int out_count, int alg_size)
116 : : {
117 : : int i;
118 : :
119 [ # # ]: 0 : for (i = 0; i < in_count; i++)
120 : 0 : memset(cookie->input_array[i], 0x0, alg_size);
121 [ # # ]: 0 : for (i = 0; i < out_count; i++)
122 : 0 : memset(cookie->output_array[i], 0x0, alg_size);
123 : 0 : }
124 : :
125 : : static void
126 : 0 : cleanup_crt(struct qat_asym_op_cookie *cookie,
127 : : int alg_size)
128 : : {
129 : : int i;
130 : :
131 : 0 : memset(cookie->input_array[0], 0x0, alg_size);
132 [ # # ]: 0 : for (i = 1; i < QAT_ASYM_RSA_QT_NUM_IN_PARAMS; i++)
133 : 0 : memset(cookie->input_array[i], 0x0, alg_size / 2);
134 [ # # ]: 0 : for (i = 0; i < QAT_ASYM_RSA_NUM_OUT_PARAMS; i++)
135 : 0 : memset(cookie->output_array[i], 0x0, alg_size);
136 : 0 : }
137 : :
138 : : static void
139 : 0 : cleanup(struct qat_asym_op_cookie *cookie,
140 : : const struct rte_crypto_asym_xform *xform)
141 : : {
142 [ # # ]: 0 : if (xform->xform_type == RTE_CRYPTO_ASYM_XFORM_MODEX)
143 : 0 : cleanup_arrays(cookie, QAT_ASYM_MODEXP_NUM_IN_PARAMS,
144 : : QAT_ASYM_MODEXP_NUM_OUT_PARAMS,
145 : 0 : cookie->alg_bytesize);
146 [ # # ]: 0 : else if (xform->xform_type == RTE_CRYPTO_ASYM_XFORM_MODINV)
147 : 0 : cleanup_arrays(cookie, QAT_ASYM_MODINV_NUM_IN_PARAMS,
148 : : QAT_ASYM_MODINV_NUM_OUT_PARAMS,
149 : 0 : cookie->alg_bytesize);
150 [ # # ]: 0 : else if (xform->xform_type == RTE_CRYPTO_ASYM_XFORM_RSA) {
151 [ # # ]: 0 : if (xform->rsa.key_type == RTE_RSA_KEY_TYPE_QT)
152 : 0 : cleanup_crt(cookie, cookie->alg_bytesize);
153 : : else {
154 : 0 : cleanup_arrays(cookie, QAT_ASYM_RSA_NUM_IN_PARAMS,
155 : : QAT_ASYM_RSA_NUM_OUT_PARAMS,
156 : 0 : cookie->alg_bytesize);
157 : : }
158 : : } else {
159 : 0 : cleanup_arrays(cookie, QAT_ASYM_MAX_PARAMS,
160 : : QAT_ASYM_MAX_PARAMS,
161 : : QAT_PKE_MAX_LN_SIZE);
162 : : }
163 : 0 : }
164 : :
165 : : static int
166 : 0 : check_zero(rte_crypto_param n)
167 : : {
168 : 0 : int i, len = n.length;
169 : :
170 [ # # ]: 0 : if (len < 8) {
171 [ # # ]: 0 : for (i = len - 1; i >= 0; i--) {
172 [ # # ]: 0 : if (n.data[i] != 0x0)
173 : : return 0;
174 : : }
175 [ # # # # ]: 0 : } else if (len == 8 && *(uint64_t *)&n.data[len - 8] == 0) {
176 : : return 1;
177 [ # # ]: 0 : } else if (*(uint64_t *)&n.data[len - 8] == 0) {
178 [ # # ]: 0 : for (i = len - 9; i >= 0; i--) {
179 [ # # ]: 0 : if (n.data[i] != 0x0)
180 : : return 0;
181 : : }
182 : : } else
183 : : return 0;
184 : :
185 : : return 1;
186 : : }
187 : :
188 : : static struct qat_asym_function
189 : 0 : get_asym_function(const struct rte_crypto_asym_xform *xform)
190 : : {
191 : : struct qat_asym_function qat_function;
192 : :
193 [ # # # ]: 0 : switch (xform->xform_type) {
194 : : case RTE_CRYPTO_ASYM_XFORM_MODEX:
195 : : qat_function = get_modexp_function(xform);
196 : : break;
197 : 0 : case RTE_CRYPTO_ASYM_XFORM_MODINV:
198 : 0 : qat_function = get_modinv_function(xform);
199 : 0 : break;
200 : : default:
201 : : qat_function.func_id = 0;
202 : : break;
203 : : }
204 : :
205 : 0 : return qat_function;
206 : : }
207 : :
208 : : static int
209 : 0 : modexp_set_input(struct icp_qat_fw_pke_request *qat_req,
210 : : struct qat_asym_op_cookie *cookie,
211 : : const struct rte_crypto_asym_op *asym_op,
212 : : const struct rte_crypto_asym_xform *xform)
213 : : {
214 : : struct qat_asym_function qat_function;
215 : : uint32_t alg_bytesize, func_id, in_bytesize;
216 : : int status = 0;
217 : :
218 [ # # # # ]: 0 : CHECK_IF_NOT_EMPTY(xform->modex.modulus, "mod exp",
219 : : "modulus", status);
220 [ # # # # ]: 0 : CHECK_IF_NOT_EMPTY(xform->modex.exponent, "mod exp",
221 : : "exponent", status);
222 [ # # ]: 0 : if (status)
223 : 0 : return status;
224 : :
225 [ # # ]: 0 : if (asym_op->modex.base.length > xform->modex.exponent.length &&
226 [ # # ]: 0 : asym_op->modex.base.length > xform->modex.modulus.length) {
227 : 0 : in_bytesize = asym_op->modex.base.length;
228 [ # # ]: 0 : } else if (xform->modex.exponent.length > xform->modex.modulus.length)
229 : 0 : in_bytesize = xform->modex.exponent.length;
230 : : else
231 : 0 : in_bytesize = xform->modex.modulus.length;
232 : :
233 : 0 : qat_function = get_modexp_function2(in_bytesize);
234 : : func_id = qat_function.func_id;
235 [ # # ]: 0 : if (qat_function.func_id == 0) {
236 : 0 : QAT_LOG(ERR, "Cannot obtain functionality id");
237 : 0 : return -EINVAL;
238 : : }
239 : 0 : alg_bytesize = qat_function.bytesize;
240 : :
241 [ # # ]: 0 : SET_PKE_LN(asym_op->modex.base, alg_bytesize, 0);
242 [ # # ]: 0 : SET_PKE_LN(xform->modex.exponent, alg_bytesize, 1);
243 [ # # ]: 0 : SET_PKE_LN(xform->modex.modulus, alg_bytesize, 2);
244 : :
245 : 0 : cookie->alg_bytesize = alg_bytesize;
246 : 0 : qat_req->pke_hdr.cd_pars.func_id = func_id;
247 : 0 : qat_req->input_param_count = QAT_ASYM_MODEXP_NUM_IN_PARAMS;
248 : 0 : qat_req->output_param_count = QAT_ASYM_MODEXP_NUM_OUT_PARAMS;
249 : :
250 : : HEXDUMP("ModExp base", cookie->input_array[0], alg_bytesize);
251 : : HEXDUMP("ModExp exponent", cookie->input_array[1], alg_bytesize);
252 : : HEXDUMP("ModExp modulus", cookie->input_array[2], alg_bytesize);
253 : :
254 : 0 : return status;
255 : : }
256 : :
257 : : static uint8_t
258 : 0 : modexp_collect(struct rte_crypto_asym_op *asym_op,
259 : : const struct qat_asym_op_cookie *cookie,
260 : : const struct rte_crypto_asym_xform *xform)
261 : : {
262 : 0 : rte_crypto_param n = xform->modex.modulus;
263 : 0 : uint32_t alg_bytesize = cookie->alg_bytesize;
264 : 0 : uint8_t *modexp_result = asym_op->modex.result.data;
265 : :
266 [ # # ]: 0 : if (n.length > alg_bytesize) {
267 : 0 : QAT_LOG(ERR, "Incorrect length of modexp modulus");
268 : 0 : return RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
269 : : }
270 : 0 : rte_memcpy(modexp_result,
271 : 0 : cookie->output_array[0] + alg_bytesize
272 [ # # ]: 0 : - n.length, n.length);
273 : : HEXDUMP("ModExp result", cookie->output_array[0],
274 : : alg_bytesize);
275 : : return RTE_CRYPTO_OP_STATUS_SUCCESS;
276 : : }
277 : :
278 : : static int
279 : 0 : modinv_set_input(struct icp_qat_fw_pke_request *qat_req,
280 : : struct qat_asym_op_cookie *cookie,
281 : : const struct rte_crypto_asym_op *asym_op,
282 : : const struct rte_crypto_asym_xform *xform)
283 : : {
284 : : struct qat_asym_function qat_function;
285 : : uint32_t alg_bytesize, func_id;
286 : : int status = 0;
287 : :
288 [ # # # # ]: 0 : CHECK_IF_NOT_EMPTY(xform->modex.modulus, "mod inv",
289 : : "modulus", status);
290 : : if (status)
291 : 0 : return status;
292 : :
293 : 0 : qat_function = get_asym_function(xform);
294 : 0 : func_id = qat_function.func_id;
295 [ # # ]: 0 : if (func_id == 0) {
296 : 0 : QAT_LOG(ERR, "Cannot obtain functionality id");
297 : 0 : return -EINVAL;
298 : : }
299 : 0 : alg_bytesize = qat_function.bytesize;
300 : :
301 [ # # ]: 0 : SET_PKE_LN(asym_op->modinv.base, alg_bytesize, 0);
302 [ # # ]: 0 : SET_PKE_LN(xform->modinv.modulus, alg_bytesize, 1);
303 : :
304 : 0 : cookie->alg_bytesize = alg_bytesize;
305 : 0 : qat_req->pke_hdr.cd_pars.func_id = func_id;
306 : 0 : qat_req->input_param_count =
307 : : QAT_ASYM_MODINV_NUM_IN_PARAMS;
308 : 0 : qat_req->output_param_count =
309 : : QAT_ASYM_MODINV_NUM_OUT_PARAMS;
310 : :
311 : : HEXDUMP("ModInv base", cookie->input_array[0], alg_bytesize);
312 : : HEXDUMP("ModInv modulus", cookie->input_array[1], alg_bytesize);
313 : :
314 : 0 : return 0;
315 : : }
316 : :
317 : : static uint8_t
318 : 0 : modinv_collect(struct rte_crypto_asym_op *asym_op,
319 : : const struct qat_asym_op_cookie *cookie,
320 : : const struct rte_crypto_asym_xform *xform)
321 : : {
322 : 0 : rte_crypto_param n = xform->modinv.modulus;
323 : 0 : uint8_t *modinv_result = asym_op->modinv.result.data;
324 : 0 : uint32_t alg_bytesize = cookie->alg_bytesize;
325 : :
326 [ # # ]: 0 : if (n.length > alg_bytesize) {
327 : 0 : QAT_LOG(ERR, "Incorrect length of modinv modulus");
328 : 0 : return RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
329 : : }
330 : 0 : rte_memcpy(modinv_result + (asym_op->modinv.result.length
331 : 0 : - n.length),
332 : 0 : cookie->output_array[0] + alg_bytesize
333 [ # # ]: 0 : - n.length, n.length);
334 : : HEXDUMP("ModInv result", cookie->output_array[0],
335 : : alg_bytesize);
336 : : return RTE_CRYPTO_OP_STATUS_SUCCESS;
337 : : }
338 : :
339 : : static int
340 [ # # ]: 0 : rsa_set_pub_input(struct icp_qat_fw_pke_request *qat_req,
341 : : struct qat_asym_op_cookie *cookie,
342 : : const struct rte_crypto_asym_op *asym_op,
343 : : const struct rte_crypto_asym_xform *xform)
344 : : {
345 : : struct qat_asym_function qat_function;
346 : : uint32_t alg_bytesize, func_id;
347 : : int status = 0;
348 : :
349 : : qat_function = get_rsa_enc_function(xform);
350 : : func_id = qat_function.func_id;
351 [ # # ]: 0 : if (func_id == 0) {
352 : 0 : QAT_LOG(ERR, "Cannot obtain functionality id");
353 : 0 : return -EINVAL;
354 : : }
355 : : alg_bytesize = qat_function.bytesize;
356 : :
357 [ # # ]: 0 : if (asym_op->rsa.op_type == RTE_CRYPTO_ASYM_OP_ENCRYPT) {
358 [ # # ]: 0 : switch (asym_op->rsa.padding.type) {
359 : 0 : case RTE_CRYPTO_RSA_PADDING_NONE:
360 [ # # ]: 0 : SET_PKE_LN(asym_op->rsa.message, alg_bytesize, 0);
361 : : break;
362 : 0 : default:
363 : 0 : QAT_LOG(ERR,
364 : : "Invalid RSA padding (Encryption)"
365 : : );
366 : 0 : return -EINVAL;
367 : : }
368 : : HEXDUMP("RSA Message", cookie->input_array[0], alg_bytesize);
369 : : } else {
370 [ # # ]: 0 : switch (asym_op->rsa.padding.type) {
371 : 0 : case RTE_CRYPTO_RSA_PADDING_NONE:
372 [ # # ]: 0 : SET_PKE_LN(asym_op->rsa.sign, alg_bytesize, 0);
373 : : break;
374 : 0 : default:
375 : 0 : QAT_LOG(ERR,
376 : : "Invalid RSA padding (Verify)");
377 : 0 : return -EINVAL;
378 : : }
379 : : HEXDUMP("RSA Signature", cookie->input_array[0],
380 : : alg_bytesize);
381 : : }
382 : :
383 [ # # ]: 0 : SET_PKE_LN(xform->rsa.e, alg_bytesize, 1);
384 [ # # ]: 0 : SET_PKE_LN(xform->rsa.n, alg_bytesize, 2);
385 : :
386 : 0 : cookie->alg_bytesize = alg_bytesize;
387 : 0 : qat_req->pke_hdr.cd_pars.func_id = func_id;
388 : :
389 : : HEXDUMP("RSA Public Key", cookie->input_array[1], alg_bytesize);
390 : : HEXDUMP("RSA Modulus", cookie->input_array[2], alg_bytesize);
391 : :
392 : 0 : return status;
393 : : }
394 : :
395 : : static int
396 : 0 : rsa_set_priv_input(struct icp_qat_fw_pke_request *qat_req,
397 : : struct qat_asym_op_cookie *cookie,
398 : : const struct rte_crypto_asym_op *asym_op,
399 : : const struct rte_crypto_asym_xform *xform)
400 : : {
401 : : struct qat_asym_function qat_function;
402 : : uint32_t alg_bytesize, func_id;
403 : : int status = 0;
404 : :
405 [ # # ]: 0 : if (xform->rsa.key_type == RTE_RSA_KEY_TYPE_QT) {
406 : : qat_function = get_rsa_crt_function(xform);
407 : : func_id = qat_function.func_id;
408 [ # # ]: 0 : if (func_id == 0) {
409 : 0 : QAT_LOG(ERR, "Cannot obtain functionality id");
410 : 0 : return -EINVAL;
411 : : }
412 : : alg_bytesize = qat_function.bytesize;
413 : 0 : qat_req->input_param_count =
414 : : QAT_ASYM_RSA_QT_NUM_IN_PARAMS;
415 : :
416 [ # # ]: 0 : SET_PKE_LN(xform->rsa.qt.p, (alg_bytesize >> 1), 1);
417 [ # # ]: 0 : SET_PKE_LN(xform->rsa.qt.q, (alg_bytesize >> 1), 2);
418 [ # # ]: 0 : SET_PKE_LN(xform->rsa.qt.dP, (alg_bytesize >> 1), 3);
419 [ # # ]: 0 : SET_PKE_LN(xform->rsa.qt.dQ, (alg_bytesize >> 1), 4);
420 [ # # ]: 0 : SET_PKE_LN(xform->rsa.qt.qInv, (alg_bytesize >> 1), 5);
421 : :
422 : : HEXDUMP("RSA p", cookie->input_array[1],
423 : : alg_bytesize);
424 : : HEXDUMP("RSA q", cookie->input_array[2],
425 : : alg_bytesize);
426 : : HEXDUMP("RSA dP", cookie->input_array[3],
427 : : alg_bytesize);
428 : : HEXDUMP("RSA dQ", cookie->input_array[4],
429 : : alg_bytesize);
430 : : HEXDUMP("RSA qInv", cookie->input_array[5],
431 : : alg_bytesize);
432 [ # # ]: 0 : } else if (xform->rsa.key_type ==
433 : : RTE_RSA_KEY_TYPE_EXP) {
434 : : qat_function = get_rsa_dec_function(xform);
435 : : func_id = qat_function.func_id;
436 [ # # ]: 0 : if (func_id == 0) {
437 : 0 : QAT_LOG(ERR, "Cannot obtain functionality id");
438 : 0 : return -EINVAL;
439 : : }
440 : : alg_bytesize = qat_function.bytesize;
441 : :
442 [ # # ]: 0 : SET_PKE_LN(xform->rsa.d, alg_bytesize, 1);
443 [ # # ]: 0 : SET_PKE_LN(xform->rsa.n, alg_bytesize, 2);
444 : :
445 : : HEXDUMP("RSA d", cookie->input_array[1],
446 : : alg_bytesize);
447 : : HEXDUMP("RSA n", cookie->input_array[2],
448 : : alg_bytesize);
449 : : } else {
450 : 0 : QAT_LOG(ERR, "Invalid RSA key type");
451 : 0 : return -EINVAL;
452 : : }
453 : :
454 [ # # ]: 0 : if (asym_op->rsa.op_type ==
455 : : RTE_CRYPTO_ASYM_OP_DECRYPT) {
456 [ # # ]: 0 : switch (asym_op->rsa.padding.type) {
457 : 0 : case RTE_CRYPTO_RSA_PADDING_NONE:
458 [ # # ]: 0 : SET_PKE_LN(asym_op->rsa.cipher, alg_bytesize, 0);
459 : : HEXDUMP("RSA ciphertext", cookie->input_array[0],
460 : : alg_bytesize);
461 : : break;
462 : 0 : default:
463 : 0 : QAT_LOG(ERR,
464 : : "Invalid padding of RSA (Decrypt)");
465 : 0 : return -(EINVAL);
466 : : }
467 : :
468 [ # # ]: 0 : } else if (asym_op->rsa.op_type ==
469 : : RTE_CRYPTO_ASYM_OP_SIGN) {
470 [ # # ]: 0 : switch (asym_op->rsa.padding.type) {
471 : 0 : case RTE_CRYPTO_RSA_PADDING_NONE:
472 [ # # ]: 0 : SET_PKE_LN(asym_op->rsa.message, alg_bytesize, 0);
473 : : HEXDUMP("RSA text to be signed", cookie->input_array[0],
474 : : alg_bytesize);
475 : : break;
476 : 0 : default:
477 : 0 : QAT_LOG(ERR,
478 : : "Invalid padding of RSA (Signature)");
479 : 0 : return -(EINVAL);
480 : : }
481 : : }
482 : :
483 : 0 : cookie->alg_bytesize = alg_bytesize;
484 : 0 : qat_req->pke_hdr.cd_pars.func_id = func_id;
485 : 0 : return status;
486 : : }
487 : :
488 : : static int
489 : 0 : rsa_set_input(struct icp_qat_fw_pke_request *qat_req,
490 : : struct qat_asym_op_cookie *cookie,
491 : : const struct rte_crypto_asym_op *asym_op,
492 : : const struct rte_crypto_asym_xform *xform)
493 : : {
494 : 0 : qat_req->input_param_count =
495 : : QAT_ASYM_RSA_NUM_IN_PARAMS;
496 : 0 : qat_req->output_param_count =
497 : : QAT_ASYM_RSA_NUM_OUT_PARAMS;
498 : :
499 [ # # ]: 0 : if (asym_op->rsa.op_type == RTE_CRYPTO_ASYM_OP_ENCRYPT ||
500 : : asym_op->rsa.op_type ==
501 : : RTE_CRYPTO_ASYM_OP_VERIFY) {
502 : 0 : return rsa_set_pub_input(qat_req, cookie, asym_op, xform);
503 : : } else {
504 : 0 : return rsa_set_priv_input(qat_req, cookie, asym_op, xform);
505 : : }
506 : : }
507 : :
508 : : static uint8_t
509 : 0 : rsa_collect(struct rte_crypto_asym_op *asym_op,
510 : : const struct qat_asym_op_cookie *cookie)
511 : : {
512 : 0 : uint32_t alg_bytesize = cookie->alg_bytesize;
513 : :
514 [ # # ]: 0 : if (asym_op->rsa.op_type == RTE_CRYPTO_ASYM_OP_ENCRYPT ||
515 : : asym_op->rsa.op_type == RTE_CRYPTO_ASYM_OP_VERIFY) {
516 : :
517 [ # # ]: 0 : if (asym_op->rsa.op_type ==
518 : : RTE_CRYPTO_ASYM_OP_ENCRYPT) {
519 : 0 : rte_memcpy(asym_op->rsa.cipher.data,
520 [ # # ]: 0 : cookie->output_array[0],
521 : : alg_bytesize);
522 : 0 : asym_op->rsa.cipher.length = alg_bytesize;
523 : : HEXDUMP("RSA Encrypted data", cookie->output_array[0],
524 : : alg_bytesize);
525 : : } else {
526 [ # # ]: 0 : switch (asym_op->rsa.padding.type) {
527 : 0 : case RTE_CRYPTO_RSA_PADDING_NONE:
528 : 0 : rte_memcpy(asym_op->rsa.cipher.data,
529 [ # # ]: 0 : cookie->output_array[0],
530 : : alg_bytesize);
531 : 0 : asym_op->rsa.cipher.length = alg_bytesize;
532 : : HEXDUMP("RSA signature",
533 : : cookie->output_array[0],
534 : : alg_bytesize);
535 : 0 : break;
536 : 0 : default:
537 : 0 : QAT_LOG(ERR, "Padding not supported");
538 : 0 : return RTE_CRYPTO_OP_STATUS_ERROR;
539 : : }
540 : : }
541 : : } else {
542 [ # # ]: 0 : if (asym_op->rsa.op_type == RTE_CRYPTO_ASYM_OP_DECRYPT) {
543 [ # # ]: 0 : switch (asym_op->rsa.padding.type) {
544 : 0 : case RTE_CRYPTO_RSA_PADDING_NONE:
545 : 0 : rte_memcpy(asym_op->rsa.message.data,
546 [ # # ]: 0 : cookie->output_array[0],
547 : : alg_bytesize);
548 : 0 : asym_op->rsa.message.length = alg_bytesize;
549 : : HEXDUMP("RSA Decrypted Message",
550 : : cookie->output_array[0],
551 : : alg_bytesize);
552 : : break;
553 : 0 : default:
554 : 0 : QAT_LOG(ERR, "Padding not supported");
555 : 0 : return RTE_CRYPTO_OP_STATUS_ERROR;
556 : : }
557 : : } else {
558 : 0 : rte_memcpy(asym_op->rsa.sign.data,
559 [ # # ]: 0 : cookie->output_array[0],
560 : : alg_bytesize);
561 : 0 : asym_op->rsa.sign.length = alg_bytesize;
562 : : HEXDUMP("RSA Signature", cookie->output_array[0],
563 : : alg_bytesize);
564 : : }
565 : : }
566 : : return RTE_CRYPTO_OP_STATUS_SUCCESS;
567 : : }
568 : :
569 : : static int
570 : 0 : ecdsa_set_input(struct icp_qat_fw_pke_request *qat_req,
571 : : struct qat_asym_op_cookie *cookie,
572 : : const struct rte_crypto_asym_op *asym_op,
573 : : const struct rte_crypto_asym_xform *xform)
574 : : {
575 : : struct qat_asym_function qat_function;
576 : : uint32_t qat_func_alignsize, func_id;
577 : : int curve_id;
578 : :
579 : 0 : curve_id = pick_curve(xform);
580 [ # # ]: 0 : if (curve_id < 0) {
581 : 0 : QAT_LOG(DEBUG, "Incorrect elliptic curve");
582 : 0 : return -EINVAL;
583 : : }
584 : :
585 [ # # # ]: 0 : switch (asym_op->ecdsa.op_type) {
586 : 0 : case RTE_CRYPTO_ASYM_OP_SIGN:
587 : 0 : qat_function = get_ecdsa_function(xform);
588 : : func_id = qat_function.func_id;
589 [ # # ]: 0 : if (func_id == 0) {
590 : 0 : QAT_LOG(ERR, "Cannot obtain functionality id");
591 : 0 : return -EINVAL;
592 : : }
593 : 0 : qat_func_alignsize =
594 : 0 : RTE_ALIGN_CEIL(qat_function.bytesize, 8);
595 : :
596 [ # # ]: 0 : SET_PKE_9A_IN(xform->ec.pkey, 0);
597 [ # # ]: 0 : SET_PKE_9A_IN(asym_op->ecdsa.message, 1);
598 [ # # ]: 0 : SET_PKE_9A_IN(asym_op->ecdsa.k, 2);
599 [ # # ]: 0 : SET_PKE_9A_EC(curve[curve_id], b, 3);
600 [ # # ]: 0 : SET_PKE_9A_EC(curve[curve_id], a, 4);
601 [ # # ]: 0 : SET_PKE_9A_EC(curve[curve_id], p, 5);
602 [ # # ]: 0 : SET_PKE_9A_EC(curve[curve_id], n, 6);
603 [ # # ]: 0 : SET_PKE_9A_EC(curve[curve_id], y, 7);
604 [ # # ]: 0 : SET_PKE_9A_EC(curve[curve_id], x, 8);
605 : :
606 : 0 : cookie->alg_bytesize = curve[curve_id].bytesize;
607 : 0 : cookie->qat_func_alignsize = qat_func_alignsize;
608 : 0 : qat_req->pke_hdr.cd_pars.func_id = func_id;
609 : 0 : qat_req->input_param_count =
610 : : QAT_ASYM_ECDSA_RS_SIGN_IN_PARAMS;
611 : 0 : qat_req->output_param_count =
612 : : QAT_ASYM_ECDSA_RS_SIGN_OUT_PARAMS;
613 : :
614 : : HEXDUMP_OFF_F("ECDSA d", 0);
615 : : HEXDUMP_OFF_F("ECDSA e", 1);
616 : : HEXDUMP_OFF_F("ECDSA k", 2);
617 : : HEXDUMP_OFF_F("ECDSA b", 3);
618 : : HEXDUMP_OFF_F("ECDSA a", 4);
619 : : HEXDUMP_OFF_F("ECDSA n", 5);
620 : : HEXDUMP_OFF_F("ECDSA y", 6);
621 : : HEXDUMP_OFF_F("ECDSA x", 7);
622 : 0 : break;
623 : 0 : case RTE_CRYPTO_ASYM_OP_VERIFY:
624 : 0 : qat_function = get_ecdsa_verify_function(xform);
625 : : func_id = qat_function.func_id;
626 [ # # ]: 0 : if (func_id == 0) {
627 : 0 : QAT_LOG(ERR, "Cannot obtain functionality id");
628 : 0 : return -EINVAL;
629 : : }
630 : 0 : qat_func_alignsize = RTE_ALIGN_CEIL(qat_function.bytesize, 8);
631 : :
632 [ # # ]: 0 : SET_PKE_9A_IN(asym_op->ecdsa.message, 10);
633 [ # # ]: 0 : SET_PKE_9A_IN(asym_op->ecdsa.s, 9);
634 [ # # ]: 0 : SET_PKE_9A_IN(asym_op->ecdsa.r, 8);
635 [ # # ]: 0 : SET_PKE_9A_EC(curve[curve_id], n, 7);
636 [ # # ]: 0 : SET_PKE_9A_EC(curve[curve_id], x, 6);
637 [ # # ]: 0 : SET_PKE_9A_EC(curve[curve_id], y, 5);
638 [ # # ]: 0 : SET_PKE_9A_IN(xform->ec.q.x, 4);
639 [ # # ]: 0 : SET_PKE_9A_IN(xform->ec.q.y, 3);
640 [ # # ]: 0 : SET_PKE_9A_EC(curve[curve_id], a, 2);
641 [ # # ]: 0 : SET_PKE_9A_EC(curve[curve_id], b, 1);
642 [ # # ]: 0 : SET_PKE_9A_EC(curve[curve_id], p, 0);
643 : :
644 : 0 : cookie->alg_bytesize = curve[curve_id].bytesize;
645 : 0 : cookie->qat_func_alignsize = qat_func_alignsize;
646 : 0 : qat_req->pke_hdr.cd_pars.func_id = func_id;
647 : 0 : qat_req->input_param_count =
648 : : QAT_ASYM_ECDSA_RS_VERIFY_IN_PARAMS;
649 : 0 : qat_req->output_param_count =
650 : : QAT_ASYM_ECDSA_RS_VERIFY_OUT_PARAMS;
651 : :
652 : : HEXDUMP_OFF_F("p", 0);
653 : : HEXDUMP_OFF_F("b", 1);
654 : : HEXDUMP_OFF_F("a", 2);
655 : : HEXDUMP_OFF_F("y", 3);
656 : : HEXDUMP_OFF_F("x", 4);
657 : : HEXDUMP_OFF_F("yG", 5);
658 : : HEXDUMP_OFF_F("xG", 6);
659 : : HEXDUMP_OFF_F("n", 7);
660 : : HEXDUMP_OFF_F("r", 8);
661 : : HEXDUMP_OFF_F("s", 9);
662 : : HEXDUMP_OFF_F("e", 10);
663 : 0 : break;
664 : : default:
665 : : return -1;
666 : : }
667 : :
668 : : return 0;
669 : : }
670 : :
671 : : static uint8_t
672 : 0 : ecdsa_collect(struct rte_crypto_asym_op *asym_op,
673 : : const struct qat_asym_op_cookie *cookie)
674 : : {
675 : 0 : uint32_t alg_bytesize = cookie->alg_bytesize;
676 : 0 : uint32_t qat_func_alignsize = cookie->qat_func_alignsize;
677 : 0 : uint32_t ltrim = qat_func_alignsize - alg_bytesize;
678 : :
679 [ # # ]: 0 : if (asym_op->rsa.op_type == RTE_CRYPTO_ASYM_OP_SIGN) {
680 : 0 : uint8_t *r = asym_op->ecdsa.r.data;
681 : 0 : uint8_t *s = asym_op->ecdsa.s.data;
682 : :
683 : 0 : asym_op->ecdsa.r.length = alg_bytesize;
684 : 0 : asym_op->ecdsa.s.length = alg_bytesize;
685 [ # # ]: 0 : rte_memcpy(r, &cookie->output_array[0][ltrim], alg_bytesize);
686 [ # # ]: 0 : rte_memcpy(s, &cookie->output_array[1][ltrim], alg_bytesize);
687 : :
688 : : HEXDUMP("R", cookie->output_array[0],
689 : : qat_func_alignsize);
690 : : HEXDUMP("S", cookie->output_array[1],
691 : : qat_func_alignsize);
692 : : }
693 : 0 : return RTE_CRYPTO_OP_STATUS_SUCCESS;
694 : : }
695 : :
696 : : static int
697 [ # # ]: 0 : ecpm_set_input(struct icp_qat_fw_pke_request *qat_req,
698 : : struct qat_asym_op_cookie *cookie,
699 : : const struct rte_crypto_asym_op *asym_op,
700 : : const struct rte_crypto_asym_xform *xform)
701 : : {
702 : : struct qat_asym_function qat_function;
703 : : uint32_t qat_func_alignsize, func_id;
704 : : int curve_id;
705 : :
706 : : curve_id = pick_curve(xform);
707 : : if (curve_id < 0) {
708 : 0 : QAT_LOG(DEBUG, "Incorrect elliptic curve");
709 : 0 : return -EINVAL;
710 : : }
711 : :
712 : : qat_function = get_ecpm_function(xform);
713 : : func_id = qat_function.func_id;
714 [ # # ]: 0 : if (func_id == 0) {
715 : 0 : QAT_LOG(ERR, "Cannot obtain functionality id");
716 : 0 : return -EINVAL;
717 : : }
718 : 0 : qat_func_alignsize = RTE_ALIGN_CEIL(qat_function.bytesize, 8);
719 : :
720 [ # # ]: 0 : SET_PKE_LN(asym_op->ecpm.scalar, qat_func_alignsize, 0);
721 [ # # ]: 0 : SET_PKE_LN(asym_op->ecpm.p.x, qat_func_alignsize, 1);
722 [ # # ]: 0 : SET_PKE_LN(asym_op->ecpm.p.y, qat_func_alignsize, 2);
723 [ # # ]: 0 : SET_PKE_LN_EC(curve[curve_id], a, 3);
724 [ # # ]: 0 : SET_PKE_LN_EC(curve[curve_id], b, 4);
725 [ # # ]: 0 : SET_PKE_LN_EC(curve[curve_id], p, 5);
726 [ # # ]: 0 : SET_PKE_LN_EC(curve[curve_id], h, 6);
727 : :
728 : 0 : cookie->alg_bytesize = curve[curve_id].bytesize;
729 : 0 : cookie->qat_func_alignsize = qat_func_alignsize;
730 : 0 : qat_req->pke_hdr.cd_pars.func_id = func_id;
731 : 0 : qat_req->input_param_count =
732 : : QAT_ASYM_ECPM_IN_PARAMS;
733 : 0 : qat_req->output_param_count =
734 : : QAT_ASYM_ECPM_OUT_PARAMS;
735 : :
736 : : HEXDUMP("k", cookie->input_array[0], qat_func_alignsize);
737 : : HEXDUMP("xG", cookie->input_array[1], qat_func_alignsize);
738 : : HEXDUMP("yG", cookie->input_array[2], qat_func_alignsize);
739 : : HEXDUMP("a", cookie->input_array[3], qat_func_alignsize);
740 : : HEXDUMP("b", cookie->input_array[4], qat_func_alignsize);
741 : : HEXDUMP("q", cookie->input_array[5], qat_func_alignsize);
742 : : HEXDUMP("h", cookie->input_array[6], qat_func_alignsize);
743 : :
744 : 0 : return 0;
745 : : }
746 : :
747 : : static uint8_t
748 : 0 : ecpm_collect(struct rte_crypto_asym_op *asym_op,
749 : : const struct qat_asym_op_cookie *cookie)
750 : : {
751 : 0 : uint8_t *x = asym_op->ecpm.r.x.data;
752 : 0 : uint8_t *y = asym_op->ecpm.r.y.data;
753 : 0 : uint32_t alg_bytesize = cookie->alg_bytesize;
754 : 0 : uint32_t qat_func_alignsize = cookie->qat_func_alignsize;
755 : 0 : uint32_t ltrim = qat_func_alignsize - alg_bytesize;
756 : :
757 : 0 : asym_op->ecpm.r.x.length = alg_bytesize;
758 : 0 : asym_op->ecpm.r.y.length = alg_bytesize;
759 [ # # ]: 0 : rte_memcpy(x, &cookie->output_array[0][ltrim], alg_bytesize);
760 [ # # ]: 0 : rte_memcpy(y, &cookie->output_array[1][ltrim], alg_bytesize);
761 : :
762 : : HEXDUMP("rX", cookie->output_array[0],
763 : : qat_func_alignsize);
764 : : HEXDUMP("rY", cookie->output_array[1],
765 : : qat_func_alignsize);
766 : 0 : return RTE_CRYPTO_OP_STATUS_SUCCESS;
767 : : }
768 : :
769 : : static int
770 [ # # ]: 0 : ecdh_set_input(struct icp_qat_fw_pke_request *qat_req,
771 : : struct qat_asym_op_cookie *cookie,
772 : : const struct rte_crypto_asym_op *asym_op,
773 : : const struct rte_crypto_asym_xform *xform)
774 : : {
775 : : struct qat_asym_function qat_function;
776 : : uint32_t qat_func_alignsize, func_id;
777 : : int curve_id;
778 : :
779 : : curve_id = pick_curve(xform);
780 : : if (curve_id < 0) {
781 : 0 : QAT_LOG(DEBUG, "Incorrect elliptic curve");
782 : 0 : return -EINVAL;
783 : : }
784 : :
785 : : qat_function = get_ecpm_function(xform);
786 : : func_id = qat_function.func_id;
787 [ # # ]: 0 : if (func_id == 0) {
788 : 0 : QAT_LOG(ERR, "Cannot obtain functionality id");
789 : 0 : return -EINVAL;
790 : : }
791 : 0 : qat_func_alignsize = RTE_ALIGN_CEIL(qat_function.bytesize, 8);
792 : :
793 [ # # ]: 0 : if (asym_op->ecdh.ke_type == RTE_CRYPTO_ASYM_KE_PUB_KEY_GENERATE) {
794 [ # # ]: 0 : SET_PKE_LN(asym_op->ecdh.priv_key, qat_func_alignsize, 0);
795 [ # # ]: 0 : SET_PKE_LN_EC(curve[curve_id], x, 1);
796 [ # # ]: 0 : SET_PKE_LN_EC(curve[curve_id], y, 2);
797 : : } else {
798 [ # # ]: 0 : SET_PKE_LN(asym_op->ecdh.priv_key, qat_func_alignsize, 0);
799 [ # # ]: 0 : SET_PKE_LN(asym_op->ecdh.pub_key.x, qat_func_alignsize, 1);
800 [ # # ]: 0 : SET_PKE_LN(asym_op->ecdh.pub_key.y, qat_func_alignsize, 2);
801 : : }
802 [ # # ]: 0 : SET_PKE_LN_EC(curve[curve_id], a, 3);
803 [ # # ]: 0 : SET_PKE_LN_EC(curve[curve_id], b, 4);
804 [ # # ]: 0 : SET_PKE_LN_EC(curve[curve_id], p, 5);
805 [ # # ]: 0 : SET_PKE_LN_EC(curve[curve_id], h, 6);
806 : :
807 : 0 : cookie->alg_bytesize = curve[curve_id].bytesize;
808 : 0 : cookie->qat_func_alignsize = qat_func_alignsize;
809 : 0 : qat_req->pke_hdr.cd_pars.func_id = func_id;
810 : 0 : qat_req->input_param_count =
811 : : QAT_ASYM_ECPM_IN_PARAMS;
812 : 0 : qat_req->output_param_count =
813 : : QAT_ASYM_ECPM_OUT_PARAMS;
814 : :
815 : : HEXDUMP("k", cookie->input_array[0], qat_func_alignsize);
816 : : HEXDUMP("xG", cookie->input_array[1], qat_func_alignsize);
817 : : HEXDUMP("yG", cookie->input_array[2], qat_func_alignsize);
818 : : HEXDUMP("a", cookie->input_array[3], qat_func_alignsize);
819 : : HEXDUMP("b", cookie->input_array[4], qat_func_alignsize);
820 : : HEXDUMP("q", cookie->input_array[5], qat_func_alignsize);
821 : : HEXDUMP("h", cookie->input_array[6], qat_func_alignsize);
822 : :
823 : 0 : return 0;
824 : : }
825 : :
826 : : static int
827 [ # # ]: 0 : ecdh_verify_set_input(struct icp_qat_fw_pke_request *qat_req,
828 : : struct qat_asym_op_cookie *cookie,
829 : : const struct rte_crypto_asym_op *asym_op,
830 : : const struct rte_crypto_asym_xform *xform)
831 : : {
832 : : struct qat_asym_function qat_function;
833 : : uint32_t qat_func_alignsize, func_id;
834 : : int curve_id;
835 : :
836 : : curve_id = pick_curve(xform);
837 : : if (curve_id < 0) {
838 : 0 : QAT_LOG(DEBUG, "Incorrect elliptic curve");
839 : 0 : return -EINVAL;
840 : : }
841 : :
842 : : qat_function = get_ec_verify_function(xform);
843 : : func_id = qat_function.func_id;
844 [ # # ]: 0 : if (func_id == 0) {
845 : 0 : QAT_LOG(ERR, "Cannot obtain functionality id");
846 : 0 : return -EINVAL;
847 : : }
848 : 0 : qat_func_alignsize = RTE_ALIGN_CEIL(qat_function.bytesize, 8);
849 : :
850 [ # # ]: 0 : SET_PKE_LN(asym_op->ecdh.pub_key.x, qat_func_alignsize, 0);
851 [ # # ]: 0 : SET_PKE_LN(asym_op->ecdh.pub_key.y, qat_func_alignsize, 1);
852 [ # # ]: 0 : SET_PKE_LN_EC(curve[curve_id], p, 2);
853 [ # # ]: 0 : SET_PKE_LN_EC(curve[curve_id], a, 3);
854 [ # # ]: 0 : SET_PKE_LN_EC(curve[curve_id], b, 4);
855 : :
856 : 0 : cookie->alg_bytesize = curve[curve_id].bytesize;
857 : 0 : cookie->qat_func_alignsize = qat_func_alignsize;
858 : 0 : qat_req->pke_hdr.cd_pars.func_id = func_id;
859 : 0 : qat_req->input_param_count =
860 : : 5;
861 : 0 : qat_req->output_param_count =
862 : : 0;
863 : :
864 : : HEXDUMP("x", cookie->input_array[0], qat_func_alignsize);
865 : : HEXDUMP("y", cookie->input_array[1], qat_func_alignsize);
866 : : HEXDUMP("p", cookie->input_array[2], qat_func_alignsize);
867 : : HEXDUMP("a", cookie->input_array[3], qat_func_alignsize);
868 : : HEXDUMP("b", cookie->input_array[4], qat_func_alignsize);
869 : :
870 : 0 : return 0;
871 : : }
872 : :
873 : : static uint8_t
874 : 0 : ecdh_collect(struct rte_crypto_asym_op *asym_op,
875 : : const struct qat_asym_op_cookie *cookie)
876 : : {
877 : : uint8_t *x, *y;
878 : 0 : uint32_t alg_bytesize = cookie->alg_bytesize;
879 : 0 : uint32_t qat_func_alignsize = cookie->qat_func_alignsize;
880 : 0 : uint32_t ltrim = qat_func_alignsize - alg_bytesize;
881 : :
882 [ # # ]: 0 : if (asym_op->ecdh.ke_type == RTE_CRYPTO_ASYM_KE_PUB_KEY_VERIFY)
883 : : return RTE_CRYPTO_OP_STATUS_SUCCESS;
884 : :
885 [ # # ]: 0 : if (asym_op->ecdh.ke_type == RTE_CRYPTO_ASYM_KE_PUB_KEY_GENERATE) {
886 : 0 : asym_op->ecdh.pub_key.x.length = alg_bytesize;
887 : 0 : asym_op->ecdh.pub_key.y.length = alg_bytesize;
888 : 0 : x = asym_op->ecdh.pub_key.x.data;
889 : 0 : y = asym_op->ecdh.pub_key.y.data;
890 : : } else {
891 : 0 : asym_op->ecdh.shared_secret.x.length = alg_bytesize;
892 : 0 : asym_op->ecdh.shared_secret.y.length = alg_bytesize;
893 : 0 : x = asym_op->ecdh.shared_secret.x.data;
894 : 0 : y = asym_op->ecdh.shared_secret.y.data;
895 : : }
896 : :
897 [ # # ]: 0 : rte_memcpy(x, &cookie->output_array[0][ltrim], alg_bytesize);
898 [ # # ]: 0 : rte_memcpy(y, &cookie->output_array[1][ltrim], alg_bytesize);
899 : :
900 : : HEXDUMP("X", cookie->output_array[0],
901 : : qat_func_alignsize);
902 : : HEXDUMP("Y", cookie->output_array[1],
903 : : qat_func_alignsize);
904 : : return RTE_CRYPTO_OP_STATUS_SUCCESS;
905 : : }
906 : :
907 : : static int
908 : 0 : sm2_ecdsa_sign_set_input(struct icp_qat_fw_pke_request *qat_req,
909 : : struct qat_asym_op_cookie *cookie,
910 : : const struct rte_crypto_asym_op *asym_op,
911 : : const struct rte_crypto_asym_xform *xform)
912 : : {
913 : : const struct qat_asym_function qat_function =
914 : : get_sm2_ecdsa_sign_function();
915 : : const uint32_t qat_func_alignsize =
916 : : qat_function.bytesize;
917 : :
918 [ # # ]: 0 : SET_PKE_LN(asym_op->sm2.k, qat_func_alignsize, 0);
919 [ # # ]: 0 : SET_PKE_LN(asym_op->sm2.message, qat_func_alignsize, 1);
920 [ # # ]: 0 : SET_PKE_LN(xform->ec.pkey, qat_func_alignsize, 2);
921 : :
922 : 0 : cookie->alg_bytesize = qat_function.bytesize;
923 : 0 : cookie->qat_func_alignsize = qat_function.bytesize;
924 : 0 : qat_req->pke_hdr.cd_pars.func_id = qat_function.func_id;
925 : 0 : qat_req->input_param_count = 3;
926 : 0 : qat_req->output_param_count = 2;
927 : :
928 : 0 : return RTE_CRYPTO_OP_STATUS_SUCCESS;
929 : : }
930 : :
931 : : static int
932 : 0 : sm2_ecdsa_verify_set_input(struct icp_qat_fw_pke_request *qat_req,
933 : : struct qat_asym_op_cookie *cookie,
934 : : const struct rte_crypto_asym_op *asym_op,
935 : : const struct rte_crypto_asym_xform *xform)
936 : : {
937 : : const struct qat_asym_function qat_function =
938 : : get_sm2_ecdsa_verify_function();
939 : : const uint32_t qat_func_alignsize =
940 : : qat_function.bytesize;
941 : :
942 [ # # ]: 0 : SET_PKE_LN(asym_op->sm2.message, qat_func_alignsize, 0);
943 [ # # ]: 0 : SET_PKE_LN(asym_op->sm2.r, qat_func_alignsize, 1);
944 [ # # ]: 0 : SET_PKE_LN(asym_op->sm2.s, qat_func_alignsize, 2);
945 [ # # ]: 0 : SET_PKE_LN(xform->ec.q.x, qat_func_alignsize, 3);
946 [ # # ]: 0 : SET_PKE_LN(xform->ec.q.y, qat_func_alignsize, 4);
947 : :
948 : 0 : cookie->alg_bytesize = qat_function.bytesize;
949 : 0 : cookie->qat_func_alignsize = qat_function.bytesize;
950 : 0 : qat_req->pke_hdr.cd_pars.func_id = qat_function.func_id;
951 : 0 : qat_req->input_param_count = 5;
952 : 0 : qat_req->output_param_count = 0;
953 : :
954 : 0 : return RTE_CRYPTO_OP_STATUS_SUCCESS;
955 : : }
956 : :
957 : : static uint8_t
958 : 0 : sm2_ecdsa_sign_collect(struct rte_crypto_asym_op *asym_op,
959 : : const struct qat_asym_op_cookie *cookie)
960 : : {
961 : 0 : uint32_t alg_bytesize = cookie->alg_bytesize;
962 : :
963 [ # # ]: 0 : if (asym_op->sm2.op_type == RTE_CRYPTO_ASYM_OP_VERIFY)
964 : : return RTE_CRYPTO_OP_STATUS_SUCCESS;
965 : :
966 [ # # ]: 0 : rte_memcpy(asym_op->sm2.r.data, cookie->output_array[0], alg_bytesize);
967 [ # # ]: 0 : rte_memcpy(asym_op->sm2.s.data, cookie->output_array[1], alg_bytesize);
968 : 0 : asym_op->sm2.r.length = alg_bytesize;
969 : 0 : asym_op->sm2.s.length = alg_bytesize;
970 : :
971 : : HEXDUMP("SM2 R", cookie->output_array[0],
972 : : alg_bytesize);
973 : : HEXDUMP("SM2 S", cookie->output_array[1],
974 : : alg_bytesize);
975 : 0 : return RTE_CRYPTO_OP_STATUS_SUCCESS;
976 : : }
977 : :
978 : : static int
979 : 0 : asym_set_input(struct icp_qat_fw_pke_request *qat_req,
980 : : struct qat_asym_op_cookie *cookie,
981 : : const struct rte_crypto_asym_op *asym_op,
982 : : const struct rte_crypto_asym_xform *xform)
983 : : {
984 [ # # # # : 0 : switch (xform->xform_type) {
# # # # ]
985 : 0 : case RTE_CRYPTO_ASYM_XFORM_MODEX:
986 : 0 : return modexp_set_input(qat_req, cookie, asym_op, xform);
987 : 0 : case RTE_CRYPTO_ASYM_XFORM_MODINV:
988 : 0 : return modinv_set_input(qat_req, cookie, asym_op, xform);
989 : 0 : case RTE_CRYPTO_ASYM_XFORM_RSA:{
990 [ # # # # ]: 0 : if (unlikely((xform->rsa.n.length < RSA_MODULUS_2048_BITS)
991 : : && (qat_legacy_capa == 0)))
992 : : return RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
993 : 0 : return rsa_set_input(qat_req, cookie, asym_op, xform);
994 : : }
995 : 0 : case RTE_CRYPTO_ASYM_XFORM_ECDSA:
996 : 0 : return ecdsa_set_input(qat_req, cookie, asym_op, xform);
997 : 0 : case RTE_CRYPTO_ASYM_XFORM_ECPM:
998 : 0 : return ecpm_set_input(qat_req, cookie, asym_op, xform);
999 : 0 : case RTE_CRYPTO_ASYM_XFORM_ECDH:
1000 [ # # ]: 0 : if (asym_op->ecdh.ke_type ==
1001 : : RTE_CRYPTO_ASYM_KE_PUB_KEY_VERIFY) {
1002 : 0 : return ecdh_verify_set_input(qat_req, cookie,
1003 : : asym_op, xform);
1004 : : } else {
1005 : 0 : return ecdh_set_input(qat_req, cookie,
1006 : : asym_op, xform);
1007 : : }
1008 : 0 : case RTE_CRYPTO_ASYM_XFORM_SM2:
1009 [ # # ]: 0 : if (asym_op->sm2.op_type ==
1010 : : RTE_CRYPTO_ASYM_OP_VERIFY) {
1011 : 0 : return sm2_ecdsa_verify_set_input(qat_req, cookie,
1012 : : asym_op, xform);
1013 : : } else {
1014 : 0 : return sm2_ecdsa_sign_set_input(qat_req, cookie,
1015 : : asym_op, xform);
1016 : : }
1017 : 0 : default:
1018 : 0 : QAT_LOG(ERR, "Invalid/unsupported asymmetric crypto xform");
1019 : 0 : return -EINVAL;
1020 : : }
1021 : : return 1;
1022 : : }
1023 : :
1024 : : static int
1025 : 0 : qat_asym_build_request(void *in_op, uint8_t *out_msg, void *op_cookie,
1026 : : __rte_unused uint64_t *opaque,
1027 : : __rte_unused enum qat_device_gen qat_dev_gen)
1028 : : {
1029 : : struct rte_crypto_op *op = (struct rte_crypto_op *)in_op;
1030 : 0 : struct rte_crypto_asym_op *asym_op = op->asym;
1031 : : struct icp_qat_fw_pke_request *qat_req =
1032 : : (struct icp_qat_fw_pke_request *)out_msg;
1033 : : struct qat_asym_op_cookie *cookie =
1034 : : (struct qat_asym_op_cookie *)op_cookie;
1035 : : struct rte_crypto_asym_xform *xform;
1036 : : struct qat_asym_session *qat_session = (struct qat_asym_session *)
1037 : 0 : op->asym->session->sess_private_data;
1038 : : int err = 0;
1039 : :
1040 : 0 : op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
1041 [ # # # ]: 0 : switch (op->sess_type) {
1042 : : case RTE_CRYPTO_OP_WITH_SESSION:
1043 : : request_init(qat_req);
1044 : : if (unlikely(qat_session == NULL)) {
1045 : : QAT_DP_LOG(ERR,
1046 : : "Session was not created for this device");
1047 : : op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
1048 : : goto error;
1049 : : }
1050 : 0 : xform = &qat_session->xform;
1051 : 0 : break;
1052 : : case RTE_CRYPTO_OP_SESSIONLESS:
1053 : : request_init(qat_req);
1054 : 0 : xform = op->asym->xform;
1055 : 0 : break;
1056 : 0 : default:
1057 : 0 : QAT_DP_LOG(ERR, "Invalid session/xform settings");
1058 : 0 : op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
1059 : 0 : goto error;
1060 : : }
1061 : 0 : err = asym_set_input(qat_req, cookie, asym_op, xform);
1062 [ # # ]: 0 : if (err) {
1063 : 0 : op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
1064 : 0 : goto error;
1065 : : }
1066 : :
1067 : 0 : qat_req->pke_mid.opaque = (uint64_t)(uintptr_t)op;
1068 : 0 : qat_req->pke_mid.src_data_addr = cookie->input_addr;
1069 : 0 : qat_req->pke_mid.dest_data_addr = cookie->output_addr;
1070 : :
1071 : : HEXDUMP("qat_req:", qat_req, sizeof(struct icp_qat_fw_pke_request));
1072 : :
1073 : 0 : return 0;
1074 : 0 : error:
1075 : 0 : qat_req->pke_mid.opaque = (uint64_t)(uintptr_t)op;
1076 : : HEXDUMP("qat_req:", qat_req, sizeof(struct icp_qat_fw_pke_request));
1077 : 0 : qat_req->output_param_count = 0;
1078 : 0 : qat_req->input_param_count = 0;
1079 : 0 : qat_req->pke_hdr.service_type = ICP_QAT_FW_COMN_REQ_NULL;
1080 : 0 : cookie->error |= err;
1081 : :
1082 : 0 : return 0;
1083 : : }
1084 : :
1085 : : static uint8_t
1086 : 0 : qat_asym_collect_response(struct rte_crypto_op *op,
1087 : : struct qat_asym_op_cookie *cookie,
1088 : : struct rte_crypto_asym_xform *xform)
1089 : : {
1090 : 0 : struct rte_crypto_asym_op *asym_op = op->asym;
1091 : :
1092 [ # # # # : 0 : switch (xform->xform_type) {
# # # # ]
1093 : 0 : case RTE_CRYPTO_ASYM_XFORM_MODEX:
1094 : 0 : return modexp_collect(asym_op, cookie, xform);
1095 : 0 : case RTE_CRYPTO_ASYM_XFORM_MODINV:
1096 : 0 : return modinv_collect(asym_op, cookie, xform);
1097 : 0 : case RTE_CRYPTO_ASYM_XFORM_RSA:
1098 : 0 : return rsa_collect(asym_op, cookie);
1099 : 0 : case RTE_CRYPTO_ASYM_XFORM_ECDSA:
1100 : 0 : return ecdsa_collect(asym_op, cookie);
1101 : 0 : case RTE_CRYPTO_ASYM_XFORM_ECPM:
1102 : 0 : return ecpm_collect(asym_op, cookie);
1103 : 0 : case RTE_CRYPTO_ASYM_XFORM_ECDH:
1104 : 0 : return ecdh_collect(asym_op, cookie);
1105 : 0 : case RTE_CRYPTO_ASYM_XFORM_SM2:
1106 : 0 : return sm2_ecdsa_sign_collect(asym_op, cookie);
1107 : 0 : default:
1108 : 0 : QAT_LOG(ERR, "Not supported xform type");
1109 : 0 : return RTE_CRYPTO_OP_STATUS_ERROR;
1110 : : }
1111 : : }
1112 : :
1113 : : static int
1114 : 0 : qat_asym_process_response(void **out_op, uint8_t *resp,
1115 : : void *op_cookie, __rte_unused uint64_t *dequeue_err_count)
1116 : : {
1117 : : struct icp_qat_fw_pke_resp *resp_msg =
1118 : : (struct icp_qat_fw_pke_resp *)resp;
1119 : 0 : struct rte_crypto_op *op = (struct rte_crypto_op *)(uintptr_t)
1120 : 0 : (resp_msg->opaque);
1121 : : struct qat_asym_op_cookie *cookie = op_cookie;
1122 : : struct rte_crypto_asym_xform *xform = NULL;
1123 : : struct qat_asym_session *qat_session = (struct qat_asym_session *)
1124 : 0 : op->asym->session->sess_private_data;
1125 : :
1126 : 0 : *out_op = op;
1127 [ # # ]: 0 : if (cookie->error) {
1128 : 0 : cookie->error = 0;
1129 [ # # ]: 0 : if (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
1130 : 0 : op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1131 : 0 : QAT_DP_LOG(DEBUG, "Cookie status returned error");
1132 : : } else {
1133 [ # # ]: 0 : if (ICP_QAT_FW_PKE_RESP_PKE_STAT_GET(
1134 : : resp_msg->pke_resp_hdr.resp_status.pke_resp_flags)) {
1135 [ # # ]: 0 : if (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
1136 : 0 : op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1137 : 0 : QAT_DP_LOG(DEBUG, "Asymmetric response status"
1138 : : " returned error");
1139 : : }
1140 [ # # ]: 0 : if (resp_msg->pke_resp_hdr.resp_status.comn_err_code) {
1141 [ # # ]: 0 : if (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
1142 : 0 : op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1143 : 0 : QAT_DP_LOG(ERR, "Asymmetric common status"
1144 : : " returned error");
1145 : : }
1146 : : }
1147 : :
1148 [ # # # ]: 0 : switch (op->sess_type) {
1149 : 0 : case RTE_CRYPTO_OP_WITH_SESSION:
1150 : 0 : xform = &qat_session->xform;
1151 : 0 : break;
1152 : 0 : case RTE_CRYPTO_OP_SESSIONLESS:
1153 : 0 : xform = op->asym->xform;
1154 : 0 : break;
1155 : 0 : default:
1156 : 0 : QAT_DP_LOG(ERR,
1157 : : "Invalid session/xform settings in response ring!");
1158 : 0 : op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1159 : : }
1160 [ # # ]: 0 : if (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
1161 : 0 : op->status = qat_asym_collect_response(op, cookie, xform);
1162 : : HEXDUMP("resp_msg:", resp_msg, sizeof(struct icp_qat_fw_pke_resp));
1163 [ # # ]: 0 : if (likely(xform != NULL))
1164 : 0 : cleanup(cookie, xform);
1165 : :
1166 : 0 : return 1;
1167 : : }
1168 : :
1169 : : static int
1170 : 0 : session_set_modexp(struct qat_asym_session *qat_session,
1171 : : struct rte_crypto_asym_xform *xform)
1172 : : {
1173 : 0 : uint8_t *modulus = xform->modex.modulus.data;
1174 : 0 : uint8_t *exponent = xform->modex.exponent.data;
1175 : :
1176 : 0 : qat_session->xform.modex.modulus.data =
1177 : 0 : rte_malloc(NULL, xform->modex.modulus.length, 0);
1178 [ # # ]: 0 : if (qat_session->xform.modex.modulus.data == NULL)
1179 : : return -ENOMEM;
1180 : 0 : qat_session->xform.modex.modulus.length = xform->modex.modulus.length;
1181 : 0 : qat_session->xform.modex.exponent.data = rte_malloc(NULL,
1182 : : xform->modex.exponent.length, 0);
1183 [ # # ]: 0 : if (qat_session->xform.modex.exponent.data == NULL) {
1184 : 0 : rte_free(qat_session->xform.modex.exponent.data);
1185 : 0 : return -ENOMEM;
1186 : : }
1187 : 0 : qat_session->xform.modex.exponent.length = xform->modex.exponent.length;
1188 : :
1189 [ # # ]: 0 : rte_memcpy(qat_session->xform.modex.modulus.data, modulus,
1190 : : xform->modex.modulus.length);
1191 [ # # ]: 0 : rte_memcpy(qat_session->xform.modex.exponent.data, exponent,
1192 : : xform->modex.exponent.length);
1193 : :
1194 : : return 0;
1195 : : }
1196 : :
1197 : : static int
1198 : 0 : session_set_modinv(struct qat_asym_session *qat_session,
1199 : : struct rte_crypto_asym_xform *xform)
1200 : : {
1201 : 0 : uint8_t *modulus = xform->modinv.modulus.data;
1202 : :
1203 : 0 : qat_session->xform.modinv.modulus.data =
1204 : 0 : rte_malloc(NULL, xform->modinv.modulus.length, 0);
1205 [ # # ]: 0 : if (qat_session->xform.modinv.modulus.data == NULL)
1206 : : return -ENOMEM;
1207 : 0 : qat_session->xform.modinv.modulus.length = xform->modinv.modulus.length;
1208 : :
1209 [ # # ]: 0 : rte_memcpy(qat_session->xform.modinv.modulus.data, modulus,
1210 : : xform->modinv.modulus.length);
1211 : :
1212 : : return 0;
1213 : : }
1214 : :
1215 : : static int
1216 : 0 : session_set_rsa(struct qat_asym_session *qat_session,
1217 : : struct rte_crypto_asym_xform *xform)
1218 : : {
1219 : 0 : uint8_t *n = xform->rsa.n.data;
1220 : 0 : uint8_t *e = xform->rsa.e.data;
1221 : : int ret = 0;
1222 : :
1223 : 0 : qat_session->xform.rsa.key_type = xform->rsa.key_type;
1224 : :
1225 : 0 : qat_session->xform.rsa.n.data =
1226 : 0 : rte_malloc(NULL, xform->rsa.n.length, 0);
1227 [ # # ]: 0 : if (qat_session->xform.rsa.n.data == NULL)
1228 : : return -ENOMEM;
1229 : 0 : qat_session->xform.rsa.n.length =
1230 : 0 : xform->rsa.n.length;
1231 : :
1232 : 0 : qat_session->xform.rsa.e.data =
1233 : 0 : rte_malloc(NULL, xform->rsa.e.length, 0);
1234 [ # # ]: 0 : if (qat_session->xform.rsa.e.data == NULL) {
1235 : : ret = -ENOMEM;
1236 : 0 : goto err;
1237 : : }
1238 : 0 : qat_session->xform.rsa.e.length =
1239 : 0 : xform->rsa.e.length;
1240 : :
1241 [ # # ]: 0 : if (xform->rsa.key_type == RTE_RSA_KEY_TYPE_QT) {
1242 : 0 : uint8_t *p = xform->rsa.qt.p.data;
1243 : 0 : uint8_t *q = xform->rsa.qt.q.data;
1244 : 0 : uint8_t *dP = xform->rsa.qt.dP.data;
1245 : 0 : uint8_t *dQ = xform->rsa.qt.dQ.data;
1246 : 0 : uint8_t *qInv = xform->rsa.qt.qInv.data;
1247 : :
1248 : 0 : qat_session->xform.rsa.qt.p.data =
1249 : 0 : rte_malloc(NULL, xform->rsa.qt.p.length, 0);
1250 [ # # ]: 0 : if (qat_session->xform.rsa.qt.p.data == NULL) {
1251 : : ret = -ENOMEM;
1252 : 0 : goto err;
1253 : : }
1254 : 0 : qat_session->xform.rsa.qt.p.length =
1255 : 0 : xform->rsa.qt.p.length;
1256 : :
1257 : 0 : qat_session->xform.rsa.qt.q.data =
1258 : 0 : rte_malloc(NULL, xform->rsa.qt.q.length, 0);
1259 [ # # ]: 0 : if (qat_session->xform.rsa.qt.q.data == NULL) {
1260 : : ret = -ENOMEM;
1261 : 0 : goto err;
1262 : : }
1263 : 0 : qat_session->xform.rsa.qt.q.length =
1264 : 0 : xform->rsa.qt.q.length;
1265 : :
1266 : 0 : qat_session->xform.rsa.qt.dP.data =
1267 : 0 : rte_malloc(NULL, xform->rsa.qt.dP.length, 0);
1268 [ # # ]: 0 : if (qat_session->xform.rsa.qt.dP.data == NULL) {
1269 : : ret = -ENOMEM;
1270 : 0 : goto err;
1271 : : }
1272 : 0 : qat_session->xform.rsa.qt.dP.length =
1273 : 0 : xform->rsa.qt.dP.length;
1274 : :
1275 : 0 : qat_session->xform.rsa.qt.dQ.data =
1276 : 0 : rte_malloc(NULL, xform->rsa.qt.dQ.length, 0);
1277 [ # # ]: 0 : if (qat_session->xform.rsa.qt.dQ.data == NULL) {
1278 : : ret = -ENOMEM;
1279 : 0 : goto err;
1280 : : }
1281 : 0 : qat_session->xform.rsa.qt.dQ.length =
1282 : 0 : xform->rsa.qt.dQ.length;
1283 : :
1284 : 0 : qat_session->xform.rsa.qt.qInv.data =
1285 : 0 : rte_malloc(NULL, xform->rsa.qt.qInv.length, 0);
1286 [ # # ]: 0 : if (qat_session->xform.rsa.qt.qInv.data == NULL) {
1287 : : ret = -ENOMEM;
1288 : 0 : goto err;
1289 : : }
1290 : 0 : qat_session->xform.rsa.qt.qInv.length =
1291 : 0 : xform->rsa.qt.qInv.length;
1292 : :
1293 [ # # ]: 0 : rte_memcpy(qat_session->xform.rsa.qt.p.data, p,
1294 : : xform->rsa.qt.p.length);
1295 [ # # ]: 0 : rte_memcpy(qat_session->xform.rsa.qt.q.data, q,
1296 : : xform->rsa.qt.q.length);
1297 [ # # ]: 0 : rte_memcpy(qat_session->xform.rsa.qt.dP.data, dP,
1298 : : xform->rsa.qt.dP.length);
1299 [ # # ]: 0 : rte_memcpy(qat_session->xform.rsa.qt.dQ.data, dQ,
1300 : : xform->rsa.qt.dQ.length);
1301 [ # # ]: 0 : rte_memcpy(qat_session->xform.rsa.qt.qInv.data, qInv,
1302 : : xform->rsa.qt.qInv.length);
1303 : :
1304 : : } else {
1305 : 0 : uint8_t *d = xform->rsa.d.data;
1306 : :
1307 : 0 : qat_session->xform.rsa.d.data =
1308 : 0 : rte_malloc(NULL, xform->rsa.d.length, 0);
1309 [ # # ]: 0 : if (qat_session->xform.rsa.d.data == NULL) {
1310 : : ret = -ENOMEM;
1311 : 0 : goto err;
1312 : : }
1313 : 0 : qat_session->xform.rsa.d.length =
1314 : 0 : xform->rsa.d.length;
1315 [ # # ]: 0 : rte_memcpy(qat_session->xform.rsa.d.data, d,
1316 : : xform->rsa.d.length);
1317 : : }
1318 : :
1319 [ # # ]: 0 : rte_memcpy(qat_session->xform.rsa.n.data, n,
1320 : : xform->rsa.n.length);
1321 [ # # ]: 0 : rte_memcpy(qat_session->xform.rsa.e.data, e,
1322 : : xform->rsa.e.length);
1323 : :
1324 : : return 0;
1325 : :
1326 : 0 : err:
1327 : 0 : rte_free(qat_session->xform.rsa.n.data);
1328 : 0 : rte_free(qat_session->xform.rsa.e.data);
1329 : 0 : rte_free(qat_session->xform.rsa.d.data);
1330 : 0 : rte_free(qat_session->xform.rsa.qt.p.data);
1331 : 0 : rte_free(qat_session->xform.rsa.qt.q.data);
1332 : 0 : rte_free(qat_session->xform.rsa.qt.dP.data);
1333 : 0 : rte_free(qat_session->xform.rsa.qt.dQ.data);
1334 : 0 : rte_free(qat_session->xform.rsa.qt.qInv.data);
1335 : 0 : return ret;
1336 : : }
1337 : :
1338 : : static void
1339 : : session_set_ec(struct qat_asym_session *qat_session,
1340 : : struct rte_crypto_asym_xform *xform)
1341 : : {
1342 : 0 : qat_session->xform.ec.curve_id = xform->ec.curve_id;
1343 : : }
1344 : :
1345 : : int
1346 : 0 : qat_asym_session_configure(struct rte_cryptodev *dev __rte_unused,
1347 : : struct rte_crypto_asym_xform *xform,
1348 : : struct rte_cryptodev_asym_session *session)
1349 : : {
1350 : : struct qat_asym_session *qat_session;
1351 : : int ret = 0;
1352 : :
1353 [ # # # # : 0 : qat_session = (struct qat_asym_session *) session->sess_private_data;
# # ]
1354 : : memset(qat_session, 0, sizeof(*qat_session));
1355 : :
1356 : 0 : qat_session->xform.xform_type = xform->xform_type;
1357 [ # # # # : 0 : switch (xform->xform_type) {
# # ]
1358 : 0 : case RTE_CRYPTO_ASYM_XFORM_MODEX:
1359 : 0 : ret = session_set_modexp(qat_session, xform);
1360 : 0 : break;
1361 : 0 : case RTE_CRYPTO_ASYM_XFORM_MODINV:
1362 : 0 : ret = session_set_modinv(qat_session, xform);
1363 : 0 : break;
1364 : 0 : case RTE_CRYPTO_ASYM_XFORM_RSA: {
1365 [ # # # # ]: 0 : if (unlikely((xform->rsa.n.length < RSA_MODULUS_2048_BITS)
1366 : : && (qat_legacy_capa == 0))) {
1367 : : ret = -ENOTSUP;
1368 : : return ret;
1369 : : }
1370 : 0 : ret = session_set_rsa(qat_session, xform);
1371 : : }
1372 : 0 : break;
1373 : : case RTE_CRYPTO_ASYM_XFORM_ECDSA:
1374 : : case RTE_CRYPTO_ASYM_XFORM_ECPM:
1375 : : case RTE_CRYPTO_ASYM_XFORM_ECDH:
1376 : : session_set_ec(qat_session, xform);
1377 : : break;
1378 : : case RTE_CRYPTO_ASYM_XFORM_SM2:
1379 : : break;
1380 : : default:
1381 : : ret = -ENOTSUP;
1382 : : }
1383 : :
1384 [ # # ]: 0 : if (ret) {
1385 : 0 : QAT_LOG(ERR, "Unsupported xform type");
1386 : 0 : return ret;
1387 : : }
1388 : :
1389 : : return 0;
1390 : : }
1391 : :
1392 : : unsigned int
1393 : 0 : qat_asym_session_get_private_size(struct rte_cryptodev *dev __rte_unused)
1394 : : {
1395 : 0 : return RTE_ALIGN_CEIL(sizeof(struct qat_asym_session), 8);
1396 : : }
1397 : :
1398 : : static void
1399 : 0 : session_clear_modexp(struct rte_crypto_modex_xform *modex)
1400 : : {
1401 : 0 : PARAM_CLR(modex->modulus);
1402 : 0 : PARAM_CLR(modex->exponent);
1403 : 0 : }
1404 : :
1405 : : static void
1406 : 0 : session_clear_modinv(struct rte_crypto_modinv_xform *modinv)
1407 : : {
1408 : 0 : PARAM_CLR(modinv->modulus);
1409 : 0 : }
1410 : :
1411 : : static void
1412 : 0 : session_clear_rsa(struct rte_crypto_rsa_xform *rsa)
1413 : : {
1414 : 0 : PARAM_CLR(rsa->n);
1415 : 0 : PARAM_CLR(rsa->e);
1416 [ # # ]: 0 : if (rsa->key_type == RTE_RSA_KEY_TYPE_EXP) {
1417 : 0 : PARAM_CLR(rsa->d);
1418 : : } else {
1419 : 0 : PARAM_CLR(rsa->qt.p);
1420 : 0 : PARAM_CLR(rsa->qt.q);
1421 : 0 : PARAM_CLR(rsa->qt.dP);
1422 : 0 : PARAM_CLR(rsa->qt.dQ);
1423 : 0 : PARAM_CLR(rsa->qt.qInv);
1424 : : }
1425 : 0 : }
1426 : :
1427 : : static void
1428 : 0 : session_clear_xform(struct qat_asym_session *qat_session)
1429 : : {
1430 [ # # # # ]: 0 : switch (qat_session->xform.xform_type) {
1431 : 0 : case RTE_CRYPTO_ASYM_XFORM_MODEX:
1432 : 0 : session_clear_modexp(&qat_session->xform.modex);
1433 : 0 : break;
1434 : 0 : case RTE_CRYPTO_ASYM_XFORM_MODINV:
1435 : 0 : session_clear_modinv(&qat_session->xform.modinv);
1436 : 0 : break;
1437 : 0 : case RTE_CRYPTO_ASYM_XFORM_RSA:
1438 : 0 : session_clear_rsa(&qat_session->xform.rsa);
1439 : 0 : break;
1440 : : default:
1441 : : break;
1442 : : }
1443 : 0 : }
1444 : :
1445 : : void
1446 : 0 : qat_asym_session_clear(struct rte_cryptodev *dev,
1447 : : struct rte_cryptodev_asym_session *session)
1448 : : {
1449 : 0 : void *sess_priv = session->sess_private_data;
1450 : : struct qat_asym_session *qat_session =
1451 : : (struct qat_asym_session *)sess_priv;
1452 : :
1453 : : if (sess_priv) {
1454 : 0 : session_clear_xform(qat_session);
1455 : 0 : memset(qat_session, 0, qat_asym_session_get_private_size(dev));
1456 : : }
1457 : 0 : }
1458 : :
1459 : : static uint16_t
1460 : 0 : qat_asym_crypto_enqueue_op_burst(void *qp, struct rte_crypto_op **ops,
1461 : : uint16_t nb_ops)
1462 : : {
1463 : 0 : return qat_enqueue_op_burst(qp, qat_asym_build_request, (void **)ops,
1464 : : nb_ops);
1465 : : }
1466 : :
1467 : : static uint16_t
1468 : 0 : qat_asym_crypto_dequeue_op_burst(void *qp, struct rte_crypto_op **ops,
1469 : : uint16_t nb_ops)
1470 : : {
1471 : 0 : return qat_dequeue_op_burst(qp, (void **)ops, qat_asym_process_response,
1472 : : nb_ops);
1473 : : }
1474 : :
1475 : : void
1476 : 0 : qat_asym_init_op_cookie(void *op_cookie)
1477 : : {
1478 : : int j;
1479 : : struct qat_asym_op_cookie *cookie = op_cookie;
1480 : :
1481 : 0 : cookie->input_addr = rte_mempool_virt2iova(cookie) +
1482 : : offsetof(struct qat_asym_op_cookie,
1483 : : input_params_ptrs);
1484 : :
1485 : 0 : cookie->output_addr = rte_mempool_virt2iova(cookie) +
1486 : : offsetof(struct qat_asym_op_cookie,
1487 : : output_params_ptrs);
1488 : :
1489 [ # # ]: 0 : for (j = 0; j < 8; j++) {
1490 : 0 : cookie->input_params_ptrs[j] =
1491 : 0 : rte_mempool_virt2iova(cookie) +
1492 : : offsetof(struct qat_asym_op_cookie,
1493 : : input_array[j]);
1494 : 0 : cookie->output_params_ptrs[j] =
1495 : 0 : rte_mempool_virt2iova(cookie) +
1496 : : offsetof(struct qat_asym_op_cookie,
1497 : : output_array[j]);
1498 : : }
1499 : 0 : }
1500 : :
1501 : : int
1502 : 0 : qat_asym_dev_create(struct qat_pci_device *qat_pci_dev,
1503 : : const struct qat_dev_cmd_param *qat_dev_cmd_param)
1504 : : {
1505 : : struct qat_cryptodev_private *internals;
1506 : : struct rte_cryptodev *cryptodev;
1507 : : struct qat_device_info *qat_dev_instance =
1508 : 0 : &qat_pci_devs[qat_pci_dev->qat_dev_id];
1509 : 0 : struct rte_cryptodev_pmd_init_params init_params = {
1510 : : .name = "",
1511 : 0 : .socket_id = qat_dev_instance->pci_dev->device.numa_node,
1512 : : .private_data_size = sizeof(struct qat_cryptodev_private)
1513 : : };
1514 : : const struct qat_crypto_gen_dev_ops *gen_dev_ops =
1515 : 0 : &qat_asym_gen_dev_ops[qat_pci_dev->qat_dev_gen];
1516 : : char name[RTE_CRYPTODEV_NAME_MAX_LEN];
1517 : : char capa_memz_name[RTE_CRYPTODEV_NAME_MAX_LEN];
1518 : : int i = 0;
1519 : : uint16_t slice_map = 0;
1520 : :
1521 : : snprintf(name, RTE_CRYPTODEV_NAME_MAX_LEN, "%s_%s",
1522 : 0 : qat_pci_dev->name, "asym");
1523 : 0 : QAT_LOG(DEBUG, "Creating QAT ASYM device %s\n", name);
1524 : :
1525 [ # # ]: 0 : if (gen_dev_ops->cryptodev_ops == NULL) {
1526 : 0 : QAT_LOG(ERR, "Device %s does not support asymmetric crypto",
1527 : : name);
1528 : 0 : return -(EFAULT);
1529 : : }
1530 : :
1531 [ # # ]: 0 : if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
1532 : 0 : qat_pci_dev->qat_asym_driver_id =
1533 : : qat_asym_driver_id;
1534 [ # # ]: 0 : } else if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1535 [ # # ]: 0 : if (qat_pci_dev->qat_asym_driver_id !=
1536 : : qat_asym_driver_id) {
1537 : 0 : QAT_LOG(ERR,
1538 : : "Device %s have different driver id than corresponding device in primary process",
1539 : : name);
1540 : 0 : return -(EFAULT);
1541 : : }
1542 : : }
1543 : :
1544 : : /* Populate subset device to use in cryptodev device creation */
1545 : 0 : qat_dev_instance->asym_rte_dev.driver = &cryptodev_qat_asym_driver;
1546 : 0 : qat_dev_instance->asym_rte_dev.numa_node =
1547 : 0 : qat_dev_instance->pci_dev->device.numa_node;
1548 : 0 : qat_dev_instance->asym_rte_dev.devargs = NULL;
1549 : :
1550 : 0 : cryptodev = rte_cryptodev_pmd_create(name,
1551 : : &(qat_dev_instance->asym_rte_dev), &init_params);
1552 : :
1553 [ # # ]: 0 : if (cryptodev == NULL)
1554 : : return -ENODEV;
1555 : :
1556 : 0 : qat_dev_instance->asym_rte_dev.name = cryptodev->data->name;
1557 : 0 : cryptodev->driver_id = qat_asym_driver_id;
1558 : 0 : cryptodev->dev_ops = gen_dev_ops->cryptodev_ops;
1559 : :
1560 : 0 : cryptodev->enqueue_burst = qat_asym_crypto_enqueue_op_burst;
1561 : 0 : cryptodev->dequeue_burst = qat_asym_crypto_dequeue_op_burst;
1562 : :
1563 : 0 : cryptodev->feature_flags = gen_dev_ops->get_feature_flags(qat_pci_dev);
1564 : :
1565 [ # # ]: 0 : if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1566 : : return 0;
1567 : :
1568 : : snprintf(capa_memz_name, RTE_CRYPTODEV_NAME_MAX_LEN,
1569 : : "QAT_ASYM_CAPA_GEN_%d",
1570 : 0 : qat_pci_dev->qat_dev_gen);
1571 : :
1572 : 0 : internals = cryptodev->data->dev_private;
1573 : 0 : internals->qat_dev = qat_pci_dev;
1574 : 0 : internals->dev_id = cryptodev->data->dev_id;
1575 : :
1576 : : while (1) {
1577 [ # # ]: 0 : if (qat_dev_cmd_param[i].name == NULL)
1578 : : break;
1579 [ # # ]: 0 : if (!strcmp(qat_dev_cmd_param[i].name, ASYM_ENQ_THRESHOLD_NAME))
1580 : 0 : internals->min_enq_burst_threshold =
1581 : 0 : qat_dev_cmd_param[i].val;
1582 [ # # ]: 0 : if (!strcmp(qat_dev_cmd_param[i].name, QAT_CMD_SLICE_MAP))
1583 : 0 : slice_map = qat_dev_cmd_param[i].val;
1584 : 0 : i++;
1585 : : }
1586 : :
1587 [ # # ]: 0 : if (slice_map & ICP_ACCEL_MASK_PKE_SLICE) {
1588 : 0 : QAT_LOG(ERR, "Device %s does not support PKE slice",
1589 : : name);
1590 : 0 : rte_cryptodev_pmd_destroy(cryptodev);
1591 : : memset(&qat_dev_instance->asym_rte_dev, 0,
1592 : : sizeof(qat_dev_instance->asym_rte_dev));
1593 : 0 : return -1;
1594 : : }
1595 : :
1596 [ # # ]: 0 : if (gen_dev_ops->get_capabilities(internals,
1597 : : capa_memz_name, slice_map) < 0) {
1598 : 0 : QAT_LOG(ERR,
1599 : : "Device cannot obtain capabilities, destroying PMD for %s",
1600 : : name);
1601 : 0 : rte_cryptodev_pmd_destroy(cryptodev);
1602 : : memset(&qat_dev_instance->asym_rte_dev, 0,
1603 : : sizeof(qat_dev_instance->asym_rte_dev));
1604 : 0 : return -1;
1605 : : }
1606 : :
1607 : 0 : qat_pci_dev->asym_dev = internals;
1608 : 0 : internals->service_type = QAT_SERVICE_ASYMMETRIC;
1609 : 0 : QAT_LOG(DEBUG, "Created QAT ASYM device %s as cryptodev instance %d",
1610 : : cryptodev->data->name, internals->dev_id);
1611 : 0 : return 0;
1612 : : }
1613 : :
1614 : : int
1615 : 0 : qat_asym_dev_destroy(struct qat_pci_device *qat_pci_dev)
1616 : : {
1617 : : struct rte_cryptodev *cryptodev;
1618 : :
1619 [ # # ]: 0 : if (qat_pci_dev == NULL)
1620 : : return -ENODEV;
1621 [ # # ]: 0 : if (qat_pci_dev->asym_dev == NULL)
1622 : : return 0;
1623 [ # # ]: 0 : if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1624 : 0 : rte_memzone_free(qat_pci_dev->asym_dev->capa_mz);
1625 : :
1626 : : /* free crypto device */
1627 : 0 : cryptodev = rte_cryptodev_pmd_get_dev(
1628 : 0 : qat_pci_dev->asym_dev->dev_id);
1629 : 0 : rte_cryptodev_pmd_destroy(cryptodev);
1630 : 0 : qat_pci_devs[qat_pci_dev->qat_dev_id].asym_rte_dev.name = NULL;
1631 : 0 : qat_pci_dev->asym_dev = NULL;
1632 : :
1633 : 0 : return 0;
1634 : : }
1635 : :
1636 : : static struct cryptodev_driver qat_crypto_drv;
1637 : 235 : RTE_PMD_REGISTER_CRYPTO_DRIVER(qat_crypto_drv,
1638 : : cryptodev_qat_asym_driver,
1639 : : qat_asym_driver_id);
|