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 : : rte_free_sensitive(what.data)
106 : :
107 : : static void
108 : : request_init(struct icp_qat_fw_pke_request *qat_req)
109 : : {
110 : : memset(qat_req, 0, sizeof(*qat_req));
111 : 0 : qat_req->pke_hdr.service_type = ICP_QAT_FW_COMN_REQ_CPM_FW_PKE;
112 : 0 : qat_req->pke_hdr.hdr_flags =
113 : : ICP_QAT_FW_COMN_HDR_FLAGS_BUILD
114 : : (ICP_QAT_FW_COMN_REQ_FLAG_SET);
115 : : }
116 : :
117 : : static void
118 : 0 : cleanup_arrays(struct qat_asym_op_cookie *cookie,
119 : : int in_count, int out_count, int alg_size)
120 : : {
121 : : int i;
122 : :
123 [ # # ]: 0 : for (i = 0; i < in_count; i++)
124 : 0 : memset(cookie->input_array[i], 0x0, alg_size);
125 [ # # ]: 0 : for (i = 0; i < out_count; i++)
126 : 0 : memset(cookie->output_array[i], 0x0, alg_size);
127 : 0 : }
128 : :
129 : : static void
130 : 0 : cleanup_crt(struct qat_asym_op_cookie *cookie,
131 : : int alg_size)
132 : : {
133 : : int i;
134 : :
135 : 0 : memset(cookie->input_array[0], 0x0, alg_size);
136 [ # # ]: 0 : for (i = 1; i < QAT_ASYM_RSA_QT_NUM_IN_PARAMS; i++)
137 : 0 : memset(cookie->input_array[i], 0x0, alg_size / 2);
138 [ # # ]: 0 : for (i = 0; i < QAT_ASYM_RSA_NUM_OUT_PARAMS; i++)
139 : 0 : memset(cookie->output_array[i], 0x0, alg_size);
140 : 0 : }
141 : :
142 : : static void
143 : 0 : cleanup(struct qat_asym_op_cookie *cookie,
144 : : const struct rte_crypto_asym_xform *xform)
145 : : {
146 [ # # ]: 0 : if (xform->xform_type == RTE_CRYPTO_ASYM_XFORM_MODEX)
147 : 0 : cleanup_arrays(cookie, QAT_ASYM_MODEXP_NUM_IN_PARAMS,
148 : : QAT_ASYM_MODEXP_NUM_OUT_PARAMS,
149 : 0 : cookie->alg_bytesize);
150 [ # # ]: 0 : else if (xform->xform_type == RTE_CRYPTO_ASYM_XFORM_MODINV)
151 : 0 : cleanup_arrays(cookie, QAT_ASYM_MODINV_NUM_IN_PARAMS,
152 : : QAT_ASYM_MODINV_NUM_OUT_PARAMS,
153 : 0 : cookie->alg_bytesize);
154 [ # # ]: 0 : else if (xform->xform_type == RTE_CRYPTO_ASYM_XFORM_RSA) {
155 [ # # ]: 0 : if (xform->rsa.key_type == RTE_RSA_KEY_TYPE_QT)
156 : 0 : cleanup_crt(cookie, cookie->alg_bytesize);
157 : : else {
158 : 0 : cleanup_arrays(cookie, QAT_ASYM_RSA_NUM_IN_PARAMS,
159 : : QAT_ASYM_RSA_NUM_OUT_PARAMS,
160 : 0 : cookie->alg_bytesize);
161 : : }
162 : : } else {
163 : 0 : cleanup_arrays(cookie, QAT_ASYM_MAX_PARAMS,
164 : : QAT_ASYM_MAX_PARAMS,
165 : : QAT_PKE_MAX_LN_SIZE);
166 : : }
167 : 0 : }
168 : :
169 : : static int
170 : 0 : check_zero(rte_crypto_param n)
171 : : {
172 : 0 : int i, len = n.length;
173 : :
174 [ # # ]: 0 : if (len < 8) {
175 [ # # ]: 0 : for (i = len - 1; i >= 0; i--) {
176 [ # # ]: 0 : if (n.data[i] != 0x0)
177 : : return 0;
178 : : }
179 [ # # # # ]: 0 : } else if (len == 8 && *(uint64_t *)&n.data[len - 8] == 0) {
180 : : return 1;
181 [ # # ]: 0 : } else if (*(uint64_t *)&n.data[len - 8] == 0) {
182 [ # # ]: 0 : for (i = len - 9; i >= 0; i--) {
183 [ # # ]: 0 : if (n.data[i] != 0x0)
184 : : return 0;
185 : : }
186 : : } else
187 : : return 0;
188 : :
189 : : return 1;
190 : : }
191 : :
192 : : static struct qat_asym_function
193 : 0 : get_asym_function(const struct rte_crypto_asym_xform *xform)
194 : : {
195 : : struct qat_asym_function qat_function;
196 : :
197 [ # # # ]: 0 : switch (xform->xform_type) {
198 : : case RTE_CRYPTO_ASYM_XFORM_MODEX:
199 : : qat_function = get_modexp_function(xform);
200 : : break;
201 : 0 : case RTE_CRYPTO_ASYM_XFORM_MODINV:
202 : 0 : qat_function = get_modinv_function(xform);
203 : 0 : break;
204 : : default:
205 : : qat_function.func_id = 0;
206 : : break;
207 : : }
208 : :
209 : 0 : return qat_function;
210 : : }
211 : :
212 : : static int
213 : 0 : modexp_set_input(struct icp_qat_fw_pke_request *qat_req,
214 : : struct qat_asym_op_cookie *cookie,
215 : : const struct rte_crypto_asym_op *asym_op,
216 : : const struct rte_crypto_asym_xform *xform)
217 : : {
218 : : struct qat_asym_function qat_function;
219 : : uint32_t alg_bytesize, func_id, in_bytesize;
220 : : int status = 0;
221 : :
222 [ # # # # ]: 0 : CHECK_IF_NOT_EMPTY(xform->modex.modulus, "mod exp",
223 : : "modulus", status);
224 [ # # # # ]: 0 : CHECK_IF_NOT_EMPTY(xform->modex.exponent, "mod exp",
225 : : "exponent", status);
226 [ # # ]: 0 : if (status)
227 : 0 : return status;
228 : :
229 [ # # ]: 0 : if (asym_op->modex.base.length > xform->modex.exponent.length &&
230 [ # # ]: 0 : asym_op->modex.base.length > xform->modex.modulus.length) {
231 : 0 : in_bytesize = asym_op->modex.base.length;
232 [ # # ]: 0 : } else if (xform->modex.exponent.length > xform->modex.modulus.length)
233 : 0 : in_bytesize = xform->modex.exponent.length;
234 : : else
235 : 0 : in_bytesize = xform->modex.modulus.length;
236 : :
237 : 0 : qat_function = get_modexp_function2(in_bytesize);
238 : : func_id = qat_function.func_id;
239 [ # # ]: 0 : if (qat_function.func_id == 0) {
240 : 0 : QAT_LOG(ERR, "Cannot obtain functionality id");
241 : 0 : return -EINVAL;
242 : : }
243 : 0 : alg_bytesize = qat_function.bytesize;
244 : :
245 [ # # ]: 0 : SET_PKE_LN(asym_op->modex.base, alg_bytesize, 0);
246 [ # # ]: 0 : SET_PKE_LN(xform->modex.exponent, alg_bytesize, 1);
247 [ # # ]: 0 : SET_PKE_LN(xform->modex.modulus, alg_bytesize, 2);
248 : :
249 : 0 : cookie->alg_bytesize = alg_bytesize;
250 : 0 : qat_req->pke_hdr.cd_pars.func_id = func_id;
251 : 0 : qat_req->input_param_count = QAT_ASYM_MODEXP_NUM_IN_PARAMS;
252 : 0 : qat_req->output_param_count = QAT_ASYM_MODEXP_NUM_OUT_PARAMS;
253 : :
254 : : HEXDUMP("ModExp base", cookie->input_array[0], alg_bytesize);
255 : : HEXDUMP("ModExp exponent", cookie->input_array[1], alg_bytesize);
256 : : HEXDUMP("ModExp modulus", cookie->input_array[2], alg_bytesize);
257 : :
258 : 0 : return status;
259 : : }
260 : :
261 : : static uint8_t
262 : 0 : modexp_collect(struct rte_crypto_asym_op *asym_op,
263 : : const struct qat_asym_op_cookie *cookie,
264 : : const struct rte_crypto_asym_xform *xform)
265 : : {
266 : 0 : rte_crypto_param n = xform->modex.modulus;
267 : 0 : uint32_t alg_bytesize = cookie->alg_bytesize;
268 : 0 : uint8_t *modexp_result = asym_op->modex.result.data;
269 : :
270 [ # # ]: 0 : if (n.length > alg_bytesize) {
271 : 0 : QAT_LOG(ERR, "Incorrect length of modexp modulus");
272 : 0 : return RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
273 : : }
274 : 0 : rte_memcpy(modexp_result,
275 : 0 : cookie->output_array[0] + alg_bytesize
276 [ # # ]: 0 : - n.length, n.length);
277 : 0 : asym_op->modex.result.length = alg_bytesize;
278 : : HEXDUMP("ModExp result", cookie->output_array[0],
279 : : alg_bytesize);
280 : 0 : return RTE_CRYPTO_OP_STATUS_SUCCESS;
281 : : }
282 : :
283 : : static int
284 : 0 : modinv_set_input(struct icp_qat_fw_pke_request *qat_req,
285 : : struct qat_asym_op_cookie *cookie,
286 : : const struct rte_crypto_asym_op *asym_op,
287 : : const struct rte_crypto_asym_xform *xform)
288 : : {
289 : : struct qat_asym_function qat_function;
290 : : uint32_t alg_bytesize, func_id;
291 : : int status = 0;
292 : :
293 [ # # # # ]: 0 : CHECK_IF_NOT_EMPTY(xform->modex.modulus, "mod inv",
294 : : "modulus", status);
295 : : if (status)
296 : 0 : return status;
297 : :
298 : 0 : qat_function = get_asym_function(xform);
299 : 0 : func_id = qat_function.func_id;
300 [ # # ]: 0 : if (func_id == 0) {
301 : 0 : QAT_LOG(ERR, "Cannot obtain functionality id");
302 : 0 : return -EINVAL;
303 : : }
304 : 0 : alg_bytesize = qat_function.bytesize;
305 : :
306 [ # # ]: 0 : SET_PKE_LN(asym_op->modinv.base, alg_bytesize, 0);
307 [ # # ]: 0 : SET_PKE_LN(xform->modinv.modulus, alg_bytesize, 1);
308 : :
309 : 0 : cookie->alg_bytesize = alg_bytesize;
310 : 0 : qat_req->pke_hdr.cd_pars.func_id = func_id;
311 : 0 : qat_req->input_param_count =
312 : : QAT_ASYM_MODINV_NUM_IN_PARAMS;
313 : 0 : qat_req->output_param_count =
314 : : QAT_ASYM_MODINV_NUM_OUT_PARAMS;
315 : :
316 : : HEXDUMP("ModInv base", cookie->input_array[0], alg_bytesize);
317 : : HEXDUMP("ModInv modulus", cookie->input_array[1], alg_bytesize);
318 : :
319 : 0 : return 0;
320 : : }
321 : :
322 : : static uint8_t
323 : 0 : modinv_collect(struct rte_crypto_asym_op *asym_op,
324 : : const struct qat_asym_op_cookie *cookie,
325 : : const struct rte_crypto_asym_xform *xform)
326 : : {
327 : 0 : rte_crypto_param n = xform->modinv.modulus;
328 : 0 : uint8_t *modinv_result = asym_op->modinv.result.data;
329 : 0 : uint32_t alg_bytesize = cookie->alg_bytesize;
330 : :
331 [ # # ]: 0 : if (n.length > alg_bytesize) {
332 : 0 : QAT_LOG(ERR, "Incorrect length of modinv modulus");
333 : 0 : return RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
334 : : }
335 : 0 : rte_memcpy(modinv_result + (asym_op->modinv.result.length
336 : 0 : - n.length),
337 : 0 : cookie->output_array[0] + alg_bytesize
338 [ # # ]: 0 : - n.length, n.length);
339 : 0 : asym_op->modinv.result.length = alg_bytesize;
340 : : HEXDUMP("ModInv result", cookie->output_array[0],
341 : : alg_bytesize);
342 : 0 : return RTE_CRYPTO_OP_STATUS_SUCCESS;
343 : : }
344 : :
345 : : static int
346 [ # # ]: 0 : rsa_set_pub_input(struct icp_qat_fw_pke_request *qat_req,
347 : : struct qat_asym_op_cookie *cookie,
348 : : const struct rte_crypto_asym_op *asym_op,
349 : : const struct rte_crypto_asym_xform *xform)
350 : : {
351 : : struct qat_asym_function qat_function;
352 : : uint32_t alg_bytesize, func_id;
353 : : int status = 0;
354 : :
355 : : qat_function = get_rsa_enc_function(xform);
356 : : func_id = qat_function.func_id;
357 [ # # ]: 0 : if (func_id == 0) {
358 : 0 : QAT_LOG(ERR, "Cannot obtain functionality id");
359 : 0 : return -EINVAL;
360 : : }
361 : : alg_bytesize = qat_function.bytesize;
362 : :
363 [ # # ]: 0 : if (asym_op->rsa.op_type == RTE_CRYPTO_ASYM_OP_ENCRYPT) {
364 [ # # ]: 0 : switch (xform->rsa.padding.type) {
365 : 0 : case RTE_CRYPTO_RSA_PADDING_NONE:
366 [ # # ]: 0 : SET_PKE_LN(asym_op->rsa.message, alg_bytesize, 0);
367 : : break;
368 : 0 : default:
369 : 0 : QAT_LOG(ERR,
370 : : "Invalid RSA padding (Encryption)"
371 : : );
372 : 0 : return -EINVAL;
373 : : }
374 : : HEXDUMP("RSA Message", cookie->input_array[0], alg_bytesize);
375 : : } else {
376 [ # # ]: 0 : switch (xform->rsa.padding.type) {
377 : 0 : case RTE_CRYPTO_RSA_PADDING_NONE:
378 [ # # ]: 0 : SET_PKE_LN(asym_op->rsa.sign, alg_bytesize, 0);
379 : : break;
380 : 0 : default:
381 : 0 : QAT_LOG(ERR,
382 : : "Invalid RSA padding (Verify)");
383 : 0 : return -EINVAL;
384 : : }
385 : : HEXDUMP("RSA Signature", cookie->input_array[0],
386 : : alg_bytesize);
387 : : }
388 : :
389 [ # # ]: 0 : SET_PKE_LN(xform->rsa.e, alg_bytesize, 1);
390 [ # # ]: 0 : SET_PKE_LN(xform->rsa.n, alg_bytesize, 2);
391 : :
392 : 0 : cookie->alg_bytesize = alg_bytesize;
393 : 0 : qat_req->pke_hdr.cd_pars.func_id = func_id;
394 : :
395 : : HEXDUMP("RSA Public Key", cookie->input_array[1], alg_bytesize);
396 : : HEXDUMP("RSA Modulus", cookie->input_array[2], alg_bytesize);
397 : :
398 : 0 : return status;
399 : : }
400 : :
401 : : static int
402 : 0 : rsa_set_priv_input(struct icp_qat_fw_pke_request *qat_req,
403 : : struct qat_asym_op_cookie *cookie,
404 : : const struct rte_crypto_asym_op *asym_op,
405 : : const struct rte_crypto_asym_xform *xform)
406 : : {
407 : : struct qat_asym_function qat_function;
408 : : uint32_t alg_bytesize, func_id;
409 : : int status = 0;
410 : :
411 [ # # ]: 0 : if (xform->rsa.key_type == RTE_RSA_KEY_TYPE_QT) {
412 : : qat_function = get_rsa_crt_function(xform);
413 : : func_id = qat_function.func_id;
414 [ # # ]: 0 : if (func_id == 0) {
415 : 0 : QAT_LOG(ERR, "Cannot obtain functionality id");
416 : 0 : return -EINVAL;
417 : : }
418 : : alg_bytesize = qat_function.bytesize;
419 : 0 : qat_req->input_param_count =
420 : : QAT_ASYM_RSA_QT_NUM_IN_PARAMS;
421 : :
422 [ # # ]: 0 : SET_PKE_LN(xform->rsa.qt.p, (alg_bytesize >> 1), 1);
423 [ # # ]: 0 : SET_PKE_LN(xform->rsa.qt.q, (alg_bytesize >> 1), 2);
424 [ # # ]: 0 : SET_PKE_LN(xform->rsa.qt.dP, (alg_bytesize >> 1), 3);
425 [ # # ]: 0 : SET_PKE_LN(xform->rsa.qt.dQ, (alg_bytesize >> 1), 4);
426 [ # # ]: 0 : SET_PKE_LN(xform->rsa.qt.qInv, (alg_bytesize >> 1), 5);
427 : :
428 : : HEXDUMP("RSA p", cookie->input_array[1],
429 : : alg_bytesize);
430 : : HEXDUMP("RSA q", cookie->input_array[2],
431 : : alg_bytesize);
432 : : HEXDUMP("RSA dP", cookie->input_array[3],
433 : : alg_bytesize);
434 : : HEXDUMP("RSA dQ", cookie->input_array[4],
435 : : alg_bytesize);
436 : : HEXDUMP("RSA qInv", cookie->input_array[5],
437 : : alg_bytesize);
438 [ # # ]: 0 : } else if (xform->rsa.key_type ==
439 : : RTE_RSA_KEY_TYPE_EXP) {
440 : : qat_function = get_rsa_dec_function(xform);
441 : : func_id = qat_function.func_id;
442 [ # # ]: 0 : if (func_id == 0) {
443 : 0 : QAT_LOG(ERR, "Cannot obtain functionality id");
444 : 0 : return -EINVAL;
445 : : }
446 : : alg_bytesize = qat_function.bytesize;
447 : :
448 [ # # ]: 0 : SET_PKE_LN(xform->rsa.d, alg_bytesize, 1);
449 [ # # ]: 0 : SET_PKE_LN(xform->rsa.n, alg_bytesize, 2);
450 : :
451 : : HEXDUMP("RSA d", cookie->input_array[1],
452 : : alg_bytesize);
453 : : HEXDUMP("RSA n", cookie->input_array[2],
454 : : alg_bytesize);
455 : : } else {
456 : 0 : QAT_LOG(ERR, "Invalid RSA key type");
457 : 0 : return -EINVAL;
458 : : }
459 : :
460 [ # # ]: 0 : if (asym_op->rsa.op_type ==
461 : : RTE_CRYPTO_ASYM_OP_DECRYPT) {
462 [ # # ]: 0 : switch (xform->rsa.padding.type) {
463 : 0 : case RTE_CRYPTO_RSA_PADDING_NONE:
464 [ # # ]: 0 : SET_PKE_LN(asym_op->rsa.cipher, alg_bytesize, 0);
465 : : HEXDUMP("RSA ciphertext", cookie->input_array[0],
466 : : alg_bytesize);
467 : : break;
468 : 0 : default:
469 : 0 : QAT_LOG(ERR,
470 : : "Invalid padding of RSA (Decrypt)");
471 : 0 : return -(EINVAL);
472 : : }
473 : :
474 [ # # ]: 0 : } else if (asym_op->rsa.op_type ==
475 : : RTE_CRYPTO_ASYM_OP_SIGN) {
476 [ # # ]: 0 : switch (xform->rsa.padding.type) {
477 : 0 : case RTE_CRYPTO_RSA_PADDING_NONE:
478 [ # # ]: 0 : SET_PKE_LN(asym_op->rsa.message, alg_bytesize, 0);
479 : : HEXDUMP("RSA text to be signed", cookie->input_array[0],
480 : : alg_bytesize);
481 : : break;
482 : 0 : default:
483 : 0 : QAT_LOG(ERR,
484 : : "Invalid padding of RSA (Signature)");
485 : 0 : return -(EINVAL);
486 : : }
487 : : }
488 : :
489 : 0 : cookie->alg_bytesize = alg_bytesize;
490 : 0 : qat_req->pke_hdr.cd_pars.func_id = func_id;
491 : 0 : return status;
492 : : }
493 : :
494 : : static int
495 : 0 : rsa_set_input(struct icp_qat_fw_pke_request *qat_req,
496 : : struct qat_asym_op_cookie *cookie,
497 : : const struct rte_crypto_asym_op *asym_op,
498 : : const struct rte_crypto_asym_xform *xform)
499 : : {
500 : 0 : qat_req->input_param_count =
501 : : QAT_ASYM_RSA_NUM_IN_PARAMS;
502 : 0 : qat_req->output_param_count =
503 : : QAT_ASYM_RSA_NUM_OUT_PARAMS;
504 : :
505 [ # # ]: 0 : if (asym_op->rsa.op_type == RTE_CRYPTO_ASYM_OP_ENCRYPT ||
506 : : asym_op->rsa.op_type ==
507 : : RTE_CRYPTO_ASYM_OP_VERIFY) {
508 : 0 : return rsa_set_pub_input(qat_req, cookie, asym_op, xform);
509 : : } else {
510 : 0 : return rsa_set_priv_input(qat_req, cookie, asym_op, xform);
511 : : }
512 : : }
513 : :
514 : : static uint8_t
515 : 0 : rsa_collect(struct rte_crypto_asym_op *asym_op,
516 : : const struct qat_asym_op_cookie *cookie,
517 : : const struct rte_crypto_asym_xform *xform)
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 (xform->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 (xform->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 : : uint8_t legacy_alg)
991 : : {
992 [ # # # # : 0 : switch (xform->xform_type) {
# # # # ]
993 : 0 : case RTE_CRYPTO_ASYM_XFORM_MODEX:
994 : 0 : return modexp_set_input(qat_req, cookie, asym_op, xform);
995 : 0 : case RTE_CRYPTO_ASYM_XFORM_MODINV:
996 : 0 : return modinv_set_input(qat_req, cookie, asym_op, xform);
997 : 0 : case RTE_CRYPTO_ASYM_XFORM_RSA:{
998 [ # # # # ]: 0 : if (unlikely((xform->rsa.n.length < RSA_MODULUS_2048_BITS)
999 : : && (legacy_alg == 0)))
1000 : : return RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
1001 : 0 : return rsa_set_input(qat_req, cookie, asym_op, xform);
1002 : : }
1003 : 0 : case RTE_CRYPTO_ASYM_XFORM_ECDSA:
1004 : 0 : return ecdsa_set_input(qat_req, cookie, asym_op, xform);
1005 : 0 : case RTE_CRYPTO_ASYM_XFORM_ECPM:
1006 : 0 : return ecpm_set_input(qat_req, cookie, asym_op, xform);
1007 : 0 : case RTE_CRYPTO_ASYM_XFORM_ECDH:
1008 [ # # ]: 0 : if (asym_op->ecdh.ke_type ==
1009 : : RTE_CRYPTO_ASYM_KE_PUB_KEY_VERIFY) {
1010 : 0 : return ecdh_verify_set_input(qat_req, cookie,
1011 : : asym_op, xform);
1012 : : } else {
1013 : 0 : return ecdh_set_input(qat_req, cookie,
1014 : : asym_op, xform);
1015 : : }
1016 : 0 : case RTE_CRYPTO_ASYM_XFORM_SM2:
1017 [ # # ]: 0 : if (asym_op->sm2.op_type ==
1018 : : RTE_CRYPTO_ASYM_OP_VERIFY) {
1019 : 0 : return sm2_ecdsa_verify_set_input(qat_req, cookie,
1020 : : asym_op, xform);
1021 : : } else {
1022 : 0 : return sm2_ecdsa_sign_set_input(qat_req, cookie,
1023 : : asym_op, xform);
1024 : : }
1025 : 0 : default:
1026 : 0 : QAT_LOG(ERR, "Invalid/unsupported asymmetric crypto xform");
1027 : 0 : return -EINVAL;
1028 : : }
1029 : : return 1;
1030 : : }
1031 : :
1032 : : static int
1033 : 0 : qat_asym_build_request(void *in_op,
1034 : : uint8_t *out_msg,
1035 : : void *op_cookie,
1036 : : struct qat_qp *qp)
1037 : : {
1038 : : struct rte_crypto_op *op = (struct rte_crypto_op *)in_op;
1039 : 0 : struct rte_crypto_asym_op *asym_op = op->asym;
1040 : : struct icp_qat_fw_pke_request *qat_req =
1041 : : (struct icp_qat_fw_pke_request *)out_msg;
1042 : : struct qat_asym_op_cookie *cookie =
1043 : : (struct qat_asym_op_cookie *)op_cookie;
1044 : : struct rte_crypto_asym_xform *xform;
1045 : : struct qat_asym_session *qat_session = (struct qat_asym_session *)
1046 : 0 : op->asym->session->sess_private_data;
1047 : : int err = 0;
1048 : :
1049 : 0 : op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
1050 [ # # # ]: 0 : switch (op->sess_type) {
1051 : : case RTE_CRYPTO_OP_WITH_SESSION:
1052 : : request_init(qat_req);
1053 : : if (unlikely(qat_session == NULL)) {
1054 : : QAT_DP_LOG(ERR,
1055 : : "Session was not created for this device");
1056 : : op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
1057 : : goto error;
1058 : : }
1059 : 0 : xform = &qat_session->xform;
1060 : 0 : break;
1061 : : case RTE_CRYPTO_OP_SESSIONLESS:
1062 : : request_init(qat_req);
1063 : 0 : xform = op->asym->xform;
1064 : 0 : break;
1065 : 0 : default:
1066 : 0 : QAT_DP_LOG(ERR, "Invalid session/xform settings");
1067 : 0 : op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
1068 : 0 : goto error;
1069 : : }
1070 : 0 : err = asym_set_input(qat_req, cookie, asym_op, xform,
1071 : 0 : qp->qat_dev->options.legacy_alg);
1072 [ # # ]: 0 : if (err) {
1073 : 0 : op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
1074 : 0 : goto error;
1075 : : }
1076 : :
1077 : 0 : qat_req->pke_mid.opaque = (uint64_t)(uintptr_t)op;
1078 : 0 : qat_req->pke_mid.src_data_addr = cookie->input_addr;
1079 : 0 : qat_req->pke_mid.dest_data_addr = cookie->output_addr;
1080 : :
1081 : : HEXDUMP("qat_req:", qat_req, sizeof(struct icp_qat_fw_pke_request));
1082 : :
1083 : 0 : return 0;
1084 : 0 : error:
1085 : 0 : qat_req->pke_mid.opaque = (uint64_t)(uintptr_t)op;
1086 : : HEXDUMP("qat_req:", qat_req, sizeof(struct icp_qat_fw_pke_request));
1087 : 0 : qat_req->output_param_count = 0;
1088 : 0 : qat_req->input_param_count = 0;
1089 : 0 : qat_req->pke_hdr.service_type = ICP_QAT_FW_COMN_REQ_NULL;
1090 : 0 : cookie->error |= err;
1091 : :
1092 : 0 : return 0;
1093 : : }
1094 : :
1095 : : static uint8_t
1096 : 0 : qat_asym_collect_response(struct rte_crypto_op *op,
1097 : : struct qat_asym_op_cookie *cookie,
1098 : : struct rte_crypto_asym_xform *xform)
1099 : : {
1100 : 0 : struct rte_crypto_asym_op *asym_op = op->asym;
1101 : :
1102 [ # # # # : 0 : switch (xform->xform_type) {
# # # # ]
1103 : 0 : case RTE_CRYPTO_ASYM_XFORM_MODEX:
1104 : 0 : return modexp_collect(asym_op, cookie, xform);
1105 : 0 : case RTE_CRYPTO_ASYM_XFORM_MODINV:
1106 : 0 : return modinv_collect(asym_op, cookie, xform);
1107 : 0 : case RTE_CRYPTO_ASYM_XFORM_RSA:
1108 : 0 : return rsa_collect(asym_op, cookie, xform);
1109 : 0 : case RTE_CRYPTO_ASYM_XFORM_ECDSA:
1110 : 0 : return ecdsa_collect(asym_op, cookie);
1111 : 0 : case RTE_CRYPTO_ASYM_XFORM_ECPM:
1112 : 0 : return ecpm_collect(asym_op, cookie);
1113 : 0 : case RTE_CRYPTO_ASYM_XFORM_ECDH:
1114 : 0 : return ecdh_collect(asym_op, cookie);
1115 : 0 : case RTE_CRYPTO_ASYM_XFORM_SM2:
1116 : 0 : return sm2_ecdsa_sign_collect(asym_op, cookie);
1117 : 0 : default:
1118 : 0 : QAT_LOG(ERR, "Not supported xform type");
1119 : 0 : return RTE_CRYPTO_OP_STATUS_ERROR;
1120 : : }
1121 : : }
1122 : :
1123 : : static int
1124 : 0 : qat_asym_process_response(void **out_op, uint8_t *resp,
1125 : : void *op_cookie, __rte_unused uint64_t *dequeue_err_count)
1126 : : {
1127 : : struct icp_qat_fw_pke_resp *resp_msg =
1128 : : (struct icp_qat_fw_pke_resp *)resp;
1129 : 0 : struct rte_crypto_op *op = (struct rte_crypto_op *)(uintptr_t)
1130 : 0 : (resp_msg->opaque);
1131 : : struct qat_asym_op_cookie *cookie = op_cookie;
1132 : : struct rte_crypto_asym_xform *xform = NULL;
1133 : : struct qat_asym_session *qat_session = (struct qat_asym_session *)
1134 : 0 : op->asym->session->sess_private_data;
1135 : :
1136 : 0 : *out_op = op;
1137 [ # # ]: 0 : if (cookie->error) {
1138 : 0 : cookie->error = 0;
1139 [ # # ]: 0 : if (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
1140 : 0 : op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1141 : 0 : QAT_DP_LOG(DEBUG, "Cookie status returned error");
1142 : : } else {
1143 [ # # ]: 0 : if (ICP_QAT_FW_PKE_RESP_PKE_STAT_GET(
1144 : : resp_msg->pke_resp_hdr.resp_status.pke_resp_flags)) {
1145 [ # # ]: 0 : if (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
1146 : 0 : op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1147 : 0 : QAT_DP_LOG(DEBUG, "Asymmetric response status"
1148 : : " returned error");
1149 : : }
1150 [ # # ]: 0 : if (resp_msg->pke_resp_hdr.resp_status.comn_err_code) {
1151 [ # # ]: 0 : if (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
1152 : 0 : op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1153 : 0 : QAT_DP_LOG(ERR, "Asymmetric common status"
1154 : : " returned error");
1155 : : }
1156 : : }
1157 : :
1158 [ # # # ]: 0 : switch (op->sess_type) {
1159 : 0 : case RTE_CRYPTO_OP_WITH_SESSION:
1160 : 0 : xform = &qat_session->xform;
1161 : 0 : break;
1162 : 0 : case RTE_CRYPTO_OP_SESSIONLESS:
1163 : 0 : xform = op->asym->xform;
1164 : 0 : break;
1165 : 0 : default:
1166 : 0 : QAT_DP_LOG(ERR,
1167 : : "Invalid session/xform settings in response ring!");
1168 : 0 : op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1169 : : }
1170 [ # # ]: 0 : if (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
1171 : 0 : op->status = qat_asym_collect_response(op, cookie, xform);
1172 : : HEXDUMP("resp_msg:", resp_msg, sizeof(struct icp_qat_fw_pke_resp));
1173 [ # # ]: 0 : if (likely(xform != NULL))
1174 : 0 : cleanup(cookie, xform);
1175 : :
1176 : 0 : return 1;
1177 : : }
1178 : :
1179 : : static int
1180 : 0 : session_set_modexp(struct qat_asym_session *qat_session,
1181 : : struct rte_crypto_asym_xform *xform)
1182 : : {
1183 : 0 : uint8_t *modulus = xform->modex.modulus.data;
1184 : 0 : uint8_t *exponent = xform->modex.exponent.data;
1185 : :
1186 : 0 : qat_session->xform.modex.modulus.data =
1187 : 0 : rte_malloc(NULL, xform->modex.modulus.length, 0);
1188 [ # # ]: 0 : if (qat_session->xform.modex.modulus.data == NULL)
1189 : : return -ENOMEM;
1190 : 0 : qat_session->xform.modex.modulus.length = xform->modex.modulus.length;
1191 : 0 : qat_session->xform.modex.exponent.data = rte_malloc(NULL,
1192 : : xform->modex.exponent.length, 0);
1193 [ # # ]: 0 : if (qat_session->xform.modex.exponent.data == NULL) {
1194 : 0 : rte_free(qat_session->xform.modex.exponent.data);
1195 : 0 : return -ENOMEM;
1196 : : }
1197 : 0 : qat_session->xform.modex.exponent.length = xform->modex.exponent.length;
1198 : :
1199 [ # # ]: 0 : rte_memcpy(qat_session->xform.modex.modulus.data, modulus,
1200 : : xform->modex.modulus.length);
1201 [ # # ]: 0 : rte_memcpy(qat_session->xform.modex.exponent.data, exponent,
1202 : : xform->modex.exponent.length);
1203 : :
1204 : : return 0;
1205 : : }
1206 : :
1207 : : static int
1208 : 0 : session_set_modinv(struct qat_asym_session *qat_session,
1209 : : struct rte_crypto_asym_xform *xform)
1210 : : {
1211 : 0 : uint8_t *modulus = xform->modinv.modulus.data;
1212 : :
1213 : 0 : qat_session->xform.modinv.modulus.data =
1214 : 0 : rte_malloc(NULL, xform->modinv.modulus.length, 0);
1215 [ # # ]: 0 : if (qat_session->xform.modinv.modulus.data == NULL)
1216 : : return -ENOMEM;
1217 : 0 : qat_session->xform.modinv.modulus.length = xform->modinv.modulus.length;
1218 : :
1219 [ # # ]: 0 : rte_memcpy(qat_session->xform.modinv.modulus.data, modulus,
1220 : : xform->modinv.modulus.length);
1221 : :
1222 : : return 0;
1223 : : }
1224 : :
1225 : : static int
1226 : 0 : session_set_rsa(struct qat_asym_session *qat_session,
1227 : : struct rte_crypto_asym_xform *xform)
1228 : : {
1229 : 0 : uint8_t *n = xform->rsa.n.data;
1230 : 0 : uint8_t *e = xform->rsa.e.data;
1231 : : int ret = 0;
1232 : :
1233 : 0 : qat_session->xform.rsa.key_type = xform->rsa.key_type;
1234 : :
1235 : 0 : qat_session->xform.rsa.n.data =
1236 : 0 : rte_malloc(NULL, xform->rsa.n.length, 0);
1237 [ # # ]: 0 : if (qat_session->xform.rsa.n.data == NULL)
1238 : : return -ENOMEM;
1239 : 0 : qat_session->xform.rsa.n.length =
1240 : 0 : xform->rsa.n.length;
1241 : :
1242 : 0 : qat_session->xform.rsa.e.data =
1243 : 0 : rte_malloc(NULL, xform->rsa.e.length, 0);
1244 [ # # ]: 0 : if (qat_session->xform.rsa.e.data == NULL) {
1245 : : ret = -ENOMEM;
1246 : 0 : goto err;
1247 : : }
1248 : 0 : qat_session->xform.rsa.e.length =
1249 : 0 : xform->rsa.e.length;
1250 : :
1251 [ # # ]: 0 : if (xform->rsa.key_type == RTE_RSA_KEY_TYPE_QT) {
1252 : 0 : uint8_t *p = xform->rsa.qt.p.data;
1253 : 0 : uint8_t *q = xform->rsa.qt.q.data;
1254 : 0 : uint8_t *dP = xform->rsa.qt.dP.data;
1255 : 0 : uint8_t *dQ = xform->rsa.qt.dQ.data;
1256 : 0 : uint8_t *qInv = xform->rsa.qt.qInv.data;
1257 : :
1258 : 0 : qat_session->xform.rsa.qt.p.data =
1259 : 0 : rte_malloc(NULL, xform->rsa.qt.p.length, 0);
1260 [ # # ]: 0 : if (qat_session->xform.rsa.qt.p.data == NULL) {
1261 : : ret = -ENOMEM;
1262 : 0 : goto err;
1263 : : }
1264 : 0 : qat_session->xform.rsa.qt.p.length =
1265 : 0 : xform->rsa.qt.p.length;
1266 : :
1267 : 0 : qat_session->xform.rsa.qt.q.data =
1268 : 0 : rte_malloc(NULL, xform->rsa.qt.q.length, 0);
1269 [ # # ]: 0 : if (qat_session->xform.rsa.qt.q.data == NULL) {
1270 : : ret = -ENOMEM;
1271 : 0 : goto err;
1272 : : }
1273 : 0 : qat_session->xform.rsa.qt.q.length =
1274 : 0 : xform->rsa.qt.q.length;
1275 : :
1276 : 0 : qat_session->xform.rsa.qt.dP.data =
1277 : 0 : rte_malloc(NULL, xform->rsa.qt.dP.length, 0);
1278 [ # # ]: 0 : if (qat_session->xform.rsa.qt.dP.data == NULL) {
1279 : : ret = -ENOMEM;
1280 : 0 : goto err;
1281 : : }
1282 : 0 : qat_session->xform.rsa.qt.dP.length =
1283 : 0 : xform->rsa.qt.dP.length;
1284 : :
1285 : 0 : qat_session->xform.rsa.qt.dQ.data =
1286 : 0 : rte_malloc(NULL, xform->rsa.qt.dQ.length, 0);
1287 [ # # ]: 0 : if (qat_session->xform.rsa.qt.dQ.data == NULL) {
1288 : : ret = -ENOMEM;
1289 : 0 : goto err;
1290 : : }
1291 : 0 : qat_session->xform.rsa.qt.dQ.length =
1292 : 0 : xform->rsa.qt.dQ.length;
1293 : :
1294 : 0 : qat_session->xform.rsa.qt.qInv.data =
1295 : 0 : rte_malloc(NULL, xform->rsa.qt.qInv.length, 0);
1296 [ # # ]: 0 : if (qat_session->xform.rsa.qt.qInv.data == NULL) {
1297 : : ret = -ENOMEM;
1298 : 0 : goto err;
1299 : : }
1300 : 0 : qat_session->xform.rsa.qt.qInv.length =
1301 : 0 : xform->rsa.qt.qInv.length;
1302 : :
1303 [ # # ]: 0 : rte_memcpy(qat_session->xform.rsa.qt.p.data, p,
1304 : : xform->rsa.qt.p.length);
1305 [ # # ]: 0 : rte_memcpy(qat_session->xform.rsa.qt.q.data, q,
1306 : : xform->rsa.qt.q.length);
1307 [ # # ]: 0 : rte_memcpy(qat_session->xform.rsa.qt.dP.data, dP,
1308 : : xform->rsa.qt.dP.length);
1309 [ # # ]: 0 : rte_memcpy(qat_session->xform.rsa.qt.dQ.data, dQ,
1310 : : xform->rsa.qt.dQ.length);
1311 [ # # ]: 0 : rte_memcpy(qat_session->xform.rsa.qt.qInv.data, qInv,
1312 : : xform->rsa.qt.qInv.length);
1313 : :
1314 : : } else {
1315 : 0 : uint8_t *d = xform->rsa.d.data;
1316 : :
1317 : 0 : qat_session->xform.rsa.d.data =
1318 : 0 : rte_malloc(NULL, xform->rsa.d.length, 0);
1319 [ # # ]: 0 : if (qat_session->xform.rsa.d.data == NULL) {
1320 : : ret = -ENOMEM;
1321 : 0 : goto err;
1322 : : }
1323 : 0 : qat_session->xform.rsa.d.length =
1324 : 0 : xform->rsa.d.length;
1325 [ # # ]: 0 : rte_memcpy(qat_session->xform.rsa.d.data, d,
1326 : : xform->rsa.d.length);
1327 : : }
1328 : :
1329 [ # # ]: 0 : rte_memcpy(qat_session->xform.rsa.n.data, n,
1330 : : xform->rsa.n.length);
1331 [ # # ]: 0 : rte_memcpy(qat_session->xform.rsa.e.data, e,
1332 : : xform->rsa.e.length);
1333 : :
1334 : : return 0;
1335 : :
1336 : 0 : err:
1337 : 0 : rte_free(qat_session->xform.rsa.n.data);
1338 : 0 : rte_free(qat_session->xform.rsa.e.data);
1339 : 0 : rte_free(qat_session->xform.rsa.d.data);
1340 : 0 : rte_free(qat_session->xform.rsa.qt.p.data);
1341 : 0 : rte_free(qat_session->xform.rsa.qt.q.data);
1342 : 0 : rte_free(qat_session->xform.rsa.qt.dP.data);
1343 : 0 : rte_free(qat_session->xform.rsa.qt.dQ.data);
1344 : 0 : rte_free(qat_session->xform.rsa.qt.qInv.data);
1345 : 0 : return ret;
1346 : : }
1347 : :
1348 : : static int
1349 : 0 : session_set_ec(struct qat_asym_session *qat_session,
1350 : : struct rte_crypto_asym_xform *xform)
1351 : : {
1352 : 0 : uint8_t *pkey = xform->ec.pkey.data;
1353 : 0 : uint8_t *q_x = xform->ec.q.x.data;
1354 : 0 : uint8_t *q_y = xform->ec.q.y.data;
1355 : :
1356 : 0 : qat_session->xform.ec.pkey.data =
1357 : 0 : rte_malloc(NULL, xform->ec.pkey.length, 0);
1358 [ # # # # ]: 0 : if (qat_session->xform.ec.pkey.length &&
1359 : : qat_session->xform.ec.pkey.data == NULL)
1360 : : return -ENOMEM;
1361 : 0 : qat_session->xform.ec.q.x.data = rte_malloc(NULL,
1362 : : xform->ec.q.x.length, 0);
1363 [ # # # # ]: 0 : if (qat_session->xform.ec.q.x.length &&
1364 : : qat_session->xform.ec.q.x.data == NULL) {
1365 : 0 : rte_free(qat_session->xform.ec.pkey.data);
1366 : 0 : return -ENOMEM;
1367 : : }
1368 : 0 : qat_session->xform.ec.q.y.data = rte_malloc(NULL,
1369 : : xform->ec.q.y.length, 0);
1370 [ # # # # ]: 0 : if (qat_session->xform.ec.q.y.length &&
1371 : : qat_session->xform.ec.q.y.data == NULL) {
1372 : 0 : rte_free(qat_session->xform.ec.pkey.data);
1373 : 0 : rte_free(qat_session->xform.ec.q.x.data);
1374 : 0 : return -ENOMEM;
1375 : : }
1376 : :
1377 : 0 : memcpy(qat_session->xform.ec.pkey.data, pkey,
1378 : : xform->ec.pkey.length);
1379 : 0 : qat_session->xform.ec.pkey.length = xform->ec.pkey.length;
1380 : 0 : memcpy(qat_session->xform.ec.q.x.data, q_x,
1381 : : xform->ec.q.x.length);
1382 : 0 : qat_session->xform.ec.q.x.length = xform->ec.q.x.length;
1383 : 0 : memcpy(qat_session->xform.ec.q.y.data, q_y,
1384 : : xform->ec.q.y.length);
1385 : 0 : qat_session->xform.ec.q.y.length = xform->ec.q.y.length;
1386 : 0 : qat_session->xform.ec.curve_id = xform->ec.curve_id;
1387 : :
1388 : 0 : return 0;
1389 : :
1390 : : }
1391 : :
1392 : : int
1393 : 0 : qat_asym_session_configure(struct rte_cryptodev *dev __rte_unused,
1394 : : struct rte_crypto_asym_xform *xform,
1395 : : struct rte_cryptodev_asym_session *session)
1396 : : {
1397 : : struct qat_cryptodev_private *crypto_qat;
1398 : : struct qat_asym_session *qat_session;
1399 : : int ret = 0;
1400 : :
1401 : 0 : crypto_qat = dev->data->dev_private;
1402 [ # # # # : 0 : qat_session = (struct qat_asym_session *) session->sess_private_data;
# # ]
1403 : : memset(qat_session, 0, sizeof(*qat_session));
1404 : :
1405 : 0 : qat_session->xform.xform_type = xform->xform_type;
1406 [ # # # # : 0 : switch (xform->xform_type) {
# # ]
1407 : 0 : case RTE_CRYPTO_ASYM_XFORM_MODEX:
1408 : 0 : ret = session_set_modexp(qat_session, xform);
1409 : 0 : break;
1410 : 0 : case RTE_CRYPTO_ASYM_XFORM_MODINV:
1411 : 0 : ret = session_set_modinv(qat_session, xform);
1412 : 0 : break;
1413 : 0 : case RTE_CRYPTO_ASYM_XFORM_RSA: {
1414 [ # # # # ]: 0 : if (unlikely((xform->rsa.n.length < RSA_MODULUS_2048_BITS)
1415 : : && (crypto_qat->qat_dev->options.legacy_alg == 0))) {
1416 : : ret = -ENOTSUP;
1417 : : return ret;
1418 : : }
1419 : 0 : ret = session_set_rsa(qat_session, xform);
1420 : : }
1421 : 0 : break;
1422 : 0 : case RTE_CRYPTO_ASYM_XFORM_ECDSA:
1423 : : case RTE_CRYPTO_ASYM_XFORM_ECPM:
1424 : : case RTE_CRYPTO_ASYM_XFORM_ECDH:
1425 : 0 : ret = session_set_ec(qat_session, xform);
1426 : 0 : break;
1427 : : case RTE_CRYPTO_ASYM_XFORM_SM2:
1428 : : break;
1429 : : default:
1430 : : ret = -ENOTSUP;
1431 : : }
1432 : :
1433 [ # # ]: 0 : if (ret) {
1434 : 0 : QAT_LOG(ERR, "Unsupported xform type");
1435 : 0 : return ret;
1436 : : }
1437 : :
1438 : : return 0;
1439 : : }
1440 : :
1441 : : unsigned int
1442 : 0 : qat_asym_session_get_private_size(struct rte_cryptodev *dev __rte_unused)
1443 : : {
1444 : 0 : return RTE_ALIGN_CEIL(sizeof(struct qat_asym_session), 8);
1445 : : }
1446 : :
1447 : : static void
1448 : : session_clear_modexp(struct rte_crypto_modex_xform *modex)
1449 : : {
1450 : 0 : PARAM_CLR(modex->modulus);
1451 : 0 : PARAM_CLR(modex->exponent);
1452 : 0 : }
1453 : :
1454 : : static void
1455 : : session_clear_modinv(struct rte_crypto_modinv_xform *modinv)
1456 : : {
1457 : 0 : PARAM_CLR(modinv->modulus);
1458 : 0 : }
1459 : :
1460 : : static void
1461 : 0 : session_clear_rsa(struct rte_crypto_rsa_xform *rsa)
1462 : : {
1463 : 0 : PARAM_CLR(rsa->n);
1464 : 0 : PARAM_CLR(rsa->e);
1465 [ # # ]: 0 : if (rsa->key_type == RTE_RSA_KEY_TYPE_EXP) {
1466 : 0 : PARAM_CLR(rsa->d);
1467 : : } else {
1468 : 0 : PARAM_CLR(rsa->qt.p);
1469 : 0 : PARAM_CLR(rsa->qt.q);
1470 : 0 : PARAM_CLR(rsa->qt.dP);
1471 : 0 : PARAM_CLR(rsa->qt.dQ);
1472 : 0 : PARAM_CLR(rsa->qt.qInv);
1473 : : }
1474 : 0 : }
1475 : :
1476 : : static void
1477 : 0 : session_clear_xform(struct qat_asym_session *qat_session)
1478 : : {
1479 [ # # # # ]: 0 : switch (qat_session->xform.xform_type) {
1480 : 0 : case RTE_CRYPTO_ASYM_XFORM_MODEX:
1481 : : session_clear_modexp(&qat_session->xform.modex);
1482 : : break;
1483 : 0 : case RTE_CRYPTO_ASYM_XFORM_MODINV:
1484 : : session_clear_modinv(&qat_session->xform.modinv);
1485 : : break;
1486 : 0 : case RTE_CRYPTO_ASYM_XFORM_RSA:
1487 : 0 : session_clear_rsa(&qat_session->xform.rsa);
1488 : 0 : break;
1489 : : default:
1490 : : break;
1491 : : }
1492 : 0 : }
1493 : :
1494 : : void
1495 : 0 : qat_asym_session_clear(struct rte_cryptodev *dev,
1496 : : struct rte_cryptodev_asym_session *session)
1497 : : {
1498 : 0 : void *sess_priv = session->sess_private_data;
1499 : : struct qat_asym_session *qat_session =
1500 : : (struct qat_asym_session *)sess_priv;
1501 : :
1502 : : if (sess_priv) {
1503 : 0 : session_clear_xform(qat_session);
1504 : 0 : memset(qat_session, 0, qat_asym_session_get_private_size(dev));
1505 : : }
1506 : 0 : }
1507 : :
1508 : : static uint16_t
1509 : 0 : qat_asym_crypto_enqueue_op_burst(void *qp, struct rte_crypto_op **ops,
1510 : : uint16_t nb_ops)
1511 : : {
1512 : 0 : return qat_enqueue_op_burst(qp, qat_asym_build_request, (void **)ops,
1513 : : nb_ops);
1514 : : }
1515 : :
1516 : : static uint16_t
1517 : 0 : qat_asym_crypto_dequeue_op_burst(void *qp, struct rte_crypto_op **ops,
1518 : : uint16_t nb_ops)
1519 : : {
1520 : 0 : return qat_dequeue_op_burst(qp, (void **)ops, qat_asym_process_response,
1521 : : nb_ops);
1522 : : }
1523 : :
1524 : : void
1525 : 0 : qat_asym_init_op_cookie(void *op_cookie)
1526 : : {
1527 : : int j;
1528 : : struct qat_asym_op_cookie *cookie = op_cookie;
1529 : :
1530 : 0 : cookie->input_addr = rte_mempool_virt2iova(cookie) +
1531 : : offsetof(struct qat_asym_op_cookie,
1532 : : input_params_ptrs);
1533 : :
1534 : 0 : cookie->output_addr = rte_mempool_virt2iova(cookie) +
1535 : : offsetof(struct qat_asym_op_cookie,
1536 : : output_params_ptrs);
1537 : :
1538 [ # # ]: 0 : for (j = 0; j < 8; j++) {
1539 : 0 : cookie->input_params_ptrs[j] =
1540 : 0 : rte_mempool_virt2iova(cookie) +
1541 : : offsetof(struct qat_asym_op_cookie,
1542 : : input_array[j]);
1543 : 0 : cookie->output_params_ptrs[j] =
1544 : 0 : rte_mempool_virt2iova(cookie) +
1545 : : offsetof(struct qat_asym_op_cookie,
1546 : : output_array[j]);
1547 : : }
1548 : 0 : }
1549 : :
1550 : : static int
1551 : 0 : qat_asym_dev_create(struct qat_pci_device *qat_pci_dev)
1552 : : {
1553 : : struct qat_cryptodev_private *internals;
1554 : : struct rte_cryptodev *cryptodev;
1555 : : struct qat_device_info *qat_dev_instance =
1556 : 0 : &qat_pci_devs[qat_pci_dev->qat_dev_id];
1557 : 0 : struct rte_cryptodev_pmd_init_params init_params = {
1558 : : .name = "",
1559 : 0 : .socket_id = qat_dev_instance->pci_dev->device.numa_node,
1560 : : .private_data_size = sizeof(struct qat_cryptodev_private)
1561 : : };
1562 : : const struct qat_crypto_gen_dev_ops *gen_dev_ops =
1563 : 0 : &qat_asym_gen_dev_ops[qat_pci_dev->qat_dev_gen];
1564 : : char name[RTE_CRYPTODEV_NAME_MAX_LEN];
1565 : : char capa_memz_name[RTE_CRYPTODEV_NAME_MAX_LEN];
1566 : 0 : uint16_t sub_id = qat_dev_instance->pci_dev->id.subsystem_device_id;
1567 : : char *cmdline = NULL;
1568 : :
1569 : : snprintf(name, RTE_CRYPTODEV_NAME_MAX_LEN, "%s_%s",
1570 : 0 : qat_pci_dev->name, "asym");
1571 : 0 : QAT_LOG(DEBUG, "Creating QAT ASYM device %s", name);
1572 : :
1573 [ # # # # ]: 0 : if (qat_pci_dev->qat_dev_gen == QAT_VQAT &&
1574 : : sub_id != ADF_VQAT_ASYM_PCI_SUBSYSTEM_ID) {
1575 : 0 : QAT_LOG(ERR, "Device (vqat instance) %s does not support asymmetric crypto",
1576 : : name);
1577 : 0 : return -EFAULT;
1578 : : }
1579 [ # # ]: 0 : if (gen_dev_ops->cryptodev_ops == NULL) {
1580 : 0 : QAT_LOG(ERR, "Device %s does not support asymmetric crypto",
1581 : : name);
1582 : 0 : return -(EFAULT);
1583 : : }
1584 : :
1585 [ # # ]: 0 : if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
1586 : 0 : qat_pci_dev->qat_asym_driver_id =
1587 : : qat_asym_driver_id;
1588 [ # # ]: 0 : } else if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1589 [ # # ]: 0 : if (qat_pci_dev->qat_asym_driver_id !=
1590 : : qat_asym_driver_id) {
1591 : 0 : QAT_LOG(ERR,
1592 : : "Device %s have different driver id than corresponding device in primary process",
1593 : : name);
1594 : 0 : return -(EFAULT);
1595 : : }
1596 : : }
1597 : :
1598 : : /* Populate subset device to use in cryptodev device creation */
1599 : 0 : qat_dev_instance->asym_rte_dev.driver = &cryptodev_qat_asym_driver;
1600 : 0 : qat_dev_instance->asym_rte_dev.numa_node =
1601 : 0 : qat_dev_instance->pci_dev->device.numa_node;
1602 : 0 : qat_dev_instance->asym_rte_dev.devargs = NULL;
1603 : :
1604 : 0 : cryptodev = rte_cryptodev_pmd_create(name,
1605 : : &(qat_dev_instance->asym_rte_dev), &init_params);
1606 : :
1607 [ # # ]: 0 : if (cryptodev == NULL)
1608 : : return -ENODEV;
1609 : :
1610 : 0 : qat_dev_instance->asym_rte_dev.name = cryptodev->data->name;
1611 : 0 : cryptodev->driver_id = qat_asym_driver_id;
1612 : 0 : cryptodev->dev_ops = gen_dev_ops->cryptodev_ops;
1613 : :
1614 : 0 : cryptodev->enqueue_burst = qat_asym_crypto_enqueue_op_burst;
1615 : 0 : cryptodev->dequeue_burst = qat_asym_crypto_dequeue_op_burst;
1616 : :
1617 : 0 : cryptodev->feature_flags = gen_dev_ops->get_feature_flags(qat_pci_dev);
1618 : :
1619 [ # # ]: 0 : if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1620 : : return 0;
1621 : :
1622 : : snprintf(capa_memz_name, RTE_CRYPTODEV_NAME_MAX_LEN,
1623 : : "QAT_ASYM_CAPA_GEN_%d",
1624 : 0 : qat_pci_dev->qat_dev_gen);
1625 : :
1626 : 0 : internals = cryptodev->data->dev_private;
1627 : 0 : internals->qat_dev = qat_pci_dev;
1628 : 0 : internals->dev_id = cryptodev->data->dev_id;
1629 : :
1630 : 0 : cmdline = qat_dev_cmdline_get_val(qat_pci_dev,
1631 : : ASYM_ENQ_THRESHOLD_NAME);
1632 [ # # ]: 0 : if (cmdline) {
1633 [ # # ]: 0 : internals->min_enq_burst_threshold =
1634 : : atoi(cmdline) > MAX_QP_THRESHOLD_SIZE ?
1635 : : MAX_QP_THRESHOLD_SIZE :
1636 : : atoi(cmdline);
1637 : : }
1638 : :
1639 [ # # ]: 0 : if (qat_pci_dev->options.slice_map & ICP_ACCEL_MASK_PKE_SLICE) {
1640 : 0 : QAT_LOG(ERR, "Device %s does not support PKE slice",
1641 : : name);
1642 : 0 : rte_cryptodev_pmd_destroy(cryptodev);
1643 : : memset(&qat_dev_instance->asym_rte_dev, 0,
1644 : : sizeof(qat_dev_instance->asym_rte_dev));
1645 : 0 : return -1;
1646 : : }
1647 : :
1648 [ # # ]: 0 : if (gen_dev_ops->get_capabilities(internals,
1649 : : capa_memz_name, qat_pci_dev->options.slice_map) < 0) {
1650 : 0 : QAT_LOG(ERR,
1651 : : "Device cannot obtain capabilities, destroying PMD for %s",
1652 : : name);
1653 : 0 : rte_cryptodev_pmd_destroy(cryptodev);
1654 : : memset(&qat_dev_instance->asym_rte_dev, 0,
1655 : : sizeof(qat_dev_instance->asym_rte_dev));
1656 : 0 : return -1;
1657 : : }
1658 : :
1659 : 0 : qat_pci_dev->pmd[QAT_SERVICE_ASYMMETRIC] = internals;
1660 : 0 : internals->service_type = QAT_SERVICE_ASYMMETRIC;
1661 : 0 : QAT_LOG(DEBUG, "Created QAT ASYM device %s as cryptodev instance %d",
1662 : : cryptodev->data->name, internals->dev_id);
1663 : 0 : return 0;
1664 : : }
1665 : :
1666 : : static int
1667 : 0 : qat_asym_dev_destroy(struct qat_pci_device *qat_pci_dev)
1668 : : {
1669 : : struct rte_cryptodev *cryptodev;
1670 : : struct qat_cryptodev_private *dev;
1671 : :
1672 [ # # ]: 0 : if (qat_pci_dev == NULL)
1673 : : return -ENODEV;
1674 : 0 : dev = qat_pci_dev->pmd[QAT_SERVICE_ASYMMETRIC];
1675 [ # # ]: 0 : if (dev == NULL)
1676 : : return 0;
1677 [ # # ]: 0 : if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1678 : 0 : rte_memzone_free(dev->capa_mz);
1679 : : /* free crypto device */
1680 : 0 : cryptodev = rte_cryptodev_pmd_get_dev(dev->dev_id);
1681 : 0 : rte_cryptodev_pmd_destroy(cryptodev);
1682 : 0 : qat_pci_devs[qat_pci_dev->qat_dev_id].asym_rte_dev.name = NULL;
1683 : 0 : qat_pci_dev->pmd[QAT_SERVICE_ASYMMETRIC] = NULL;
1684 : :
1685 : 0 : return 0;
1686 : : }
1687 : :
1688 : : static struct cryptodev_driver qat_crypto_drv;
1689 : 254 : RTE_PMD_REGISTER_CRYPTO_DRIVER(qat_crypto_drv,
1690 : : cryptodev_qat_asym_driver,
1691 : : qat_asym_driver_id);
1692 : :
1693 : 254 : RTE_INIT(qat_asym_init)
1694 : : {
1695 : 254 : qat_cmdline_defines[QAT_SERVICE_ASYMMETRIC] = arguments;
1696 : 254 : qat_service[QAT_SERVICE_ASYMMETRIC].name = "asymmetric crypto";
1697 : 254 : qat_service[QAT_SERVICE_ASYMMETRIC].dev_create = qat_asym_dev_create;
1698 : 254 : qat_service[QAT_SERVICE_ASYMMETRIC].dev_destroy = qat_asym_dev_destroy;
1699 : 254 : }
|