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