xref: /dpdk/drivers/common/qat/qat_adf/qat_pke.h (revision 8545c6d2)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2021-2022 Intel Corporation
3  */
4 
5 #ifndef _QAT_PKE_FUNCTIONALITY_ARRAYS_H_
6 #define _QAT_PKE_FUNCTIONALITY_ARRAYS_H_
7 
8 #include "icp_qat_fw_mmp_ids.h"
9 
10 /*
11  * Modular exponentiation functionality IDs
12  */
13 
14 struct qat_asym_function {
15 	uint32_t func_id;
16 	uint32_t bytesize;
17 };
18 
19 static struct qat_asym_function
get_modexp_function2(uint32_t bytesize)20 get_modexp_function2(uint32_t bytesize)
21 {
22 	struct qat_asym_function qat_function = { };
23 
24 	if (bytesize <= 64) {
25 		qat_function.func_id = MATHS_MODEXP_L512;
26 		qat_function.bytesize = 64;
27 	} else if (bytesize <= 128) {
28 		qat_function.func_id = MATHS_MODEXP_L1024;
29 		qat_function.bytesize = 128;
30 	} else if (bytesize <= 192) {
31 		qat_function.func_id = MATHS_MODEXP_L1536;
32 		qat_function.bytesize = 192;
33 	} else if (bytesize <= 256) {
34 		qat_function.func_id = MATHS_MODEXP_L2048;
35 		qat_function.bytesize = 256;
36 	} else if (bytesize <= 320) {
37 		qat_function.func_id = MATHS_MODEXP_L2560;
38 		qat_function.bytesize = 320;
39 	} else if (bytesize <= 384) {
40 		qat_function.func_id = MATHS_MODEXP_L3072;
41 		qat_function.bytesize = 384;
42 	} else if (bytesize <= 448) {
43 		qat_function.func_id = MATHS_MODEXP_L3584;
44 		qat_function.bytesize = 448;
45 	} else if (bytesize <= 512) {
46 		qat_function.func_id = MATHS_MODEXP_L4096;
47 		qat_function.bytesize = 512;
48 	}
49 	return qat_function;
50 }
51 
52 static struct qat_asym_function
get_modexp_function(struct rte_crypto_asym_xform * xform)53 get_modexp_function(struct rte_crypto_asym_xform *xform)
54 {
55 	return get_modexp_function2(xform->modex.modulus.length);
56 }
57 
58 static struct qat_asym_function
get_modinv_function(struct rte_crypto_asym_xform * xform)59 get_modinv_function(struct rte_crypto_asym_xform *xform)
60 {
61 	struct qat_asym_function qat_function = { };
62 
63 	if (xform->modinv.modulus.data[
64 		xform->modinv.modulus.length - 1] & 0x01) {
65 		if (xform->modex.modulus.length <= 16) {
66 			qat_function.func_id = MATHS_MODINV_ODD_L128;
67 			qat_function.bytesize = 16;
68 		} else if (xform->modex.modulus.length <= 24) {
69 			qat_function.func_id = MATHS_MODINV_ODD_L192;
70 			qat_function.bytesize = 24;
71 		} else if (xform->modex.modulus.length <= 32) {
72 			qat_function.func_id = MATHS_MODINV_ODD_L256;
73 			qat_function.bytesize = 32;
74 		} else if (xform->modex.modulus.length <= 48) {
75 			qat_function.func_id = MATHS_MODINV_ODD_L384;
76 			qat_function.bytesize = 48;
77 		} else if (xform->modex.modulus.length <= 64) {
78 			qat_function.func_id = MATHS_MODINV_ODD_L512;
79 			qat_function.bytesize = 64;
80 		} else if (xform->modex.modulus.length <= 96) {
81 			qat_function.func_id = MATHS_MODINV_ODD_L768;
82 			qat_function.bytesize = 96;
83 		} else if (xform->modex.modulus.length <= 128) {
84 			qat_function.func_id = MATHS_MODINV_ODD_L1024;
85 			qat_function.bytesize = 128;
86 		} else if (xform->modex.modulus.length <= 192) {
87 			qat_function.func_id = MATHS_MODINV_ODD_L1536;
88 			qat_function.bytesize = 192;
89 		} else if (xform->modex.modulus.length <= 256) {
90 			qat_function.func_id = MATHS_MODINV_ODD_L2048;
91 			qat_function.bytesize = 256;
92 		} else if (xform->modex.modulus.length <= 384) {
93 			qat_function.func_id = MATHS_MODINV_ODD_L3072;
94 			qat_function.bytesize = 384;
95 		} else if (xform->modex.modulus.length <= 512) {
96 			qat_function.func_id = MATHS_MODINV_ODD_L4096;
97 			qat_function.bytesize = 512;
98 		}
99 	} else {
100 		if (xform->modex.modulus.length <= 16) {
101 			qat_function.func_id = MATHS_MODINV_EVEN_L128;
102 			qat_function.bytesize = 16;
103 		} else if (xform->modex.modulus.length <= 24) {
104 			qat_function.func_id = MATHS_MODINV_EVEN_L192;
105 			qat_function.bytesize = 24;
106 		} else if (xform->modex.modulus.length <= 32) {
107 			qat_function.func_id = MATHS_MODINV_EVEN_L256;
108 			qat_function.bytesize = 32;
109 		} else if (xform->modex.modulus.length <= 48) {
110 			qat_function.func_id = MATHS_MODINV_EVEN_L384;
111 			qat_function.bytesize = 48;
112 		} else if (xform->modex.modulus.length <= 64) {
113 			qat_function.func_id = MATHS_MODINV_EVEN_L512;
114 			qat_function.bytesize = 64;
115 		} else if (xform->modex.modulus.length <= 96) {
116 			qat_function.func_id = MATHS_MODINV_EVEN_L768;
117 			qat_function.bytesize = 96;
118 		} else if (xform->modex.modulus.length <= 128) {
119 			qat_function.func_id = MATHS_MODINV_EVEN_L1024;
120 			qat_function.bytesize = 128;
121 		} else if (xform->modex.modulus.length <= 192) {
122 			qat_function.func_id = MATHS_MODINV_EVEN_L1536;
123 			qat_function.bytesize = 192;
124 		} else if (xform->modex.modulus.length <= 256) {
125 			qat_function.func_id = MATHS_MODINV_EVEN_L2048;
126 			qat_function.bytesize = 256;
127 		} else if (xform->modex.modulus.length <= 384) {
128 			qat_function.func_id = MATHS_MODINV_EVEN_L3072;
129 			qat_function.bytesize = 384;
130 		} else if (xform->modex.modulus.length <= 512) {
131 			qat_function.func_id = MATHS_MODINV_EVEN_L4096;
132 			qat_function.bytesize = 512;
133 		}
134 	}
135 
136 	return qat_function;
137 }
138 
139 static struct qat_asym_function
get_rsa_enc_function(struct rte_crypto_asym_xform * xform)140 get_rsa_enc_function(struct rte_crypto_asym_xform *xform)
141 {
142 	struct qat_asym_function qat_function = { };
143 
144 	if (xform->rsa.n.length <= 64) {
145 		qat_function.func_id = PKE_RSA_EP_512;
146 		qat_function.bytesize = 64;
147 	} else if (xform->rsa.n.length <= 128) {
148 		qat_function.func_id = PKE_RSA_EP_1024;
149 		qat_function.bytesize = 128;
150 	} else if (xform->rsa.n.length <= 192) {
151 		qat_function.func_id = PKE_RSA_EP_1536;
152 		qat_function.bytesize = 192;
153 	} else if (xform->rsa.n.length <= 256) {
154 		qat_function.func_id = PKE_RSA_EP_2048;
155 		qat_function.bytesize = 256;
156 	} else if (xform->rsa.n.length <= 384) {
157 		qat_function.func_id = PKE_RSA_EP_3072;
158 		qat_function.bytesize = 384;
159 	} else if (xform->rsa.n.length <= 512) {
160 		qat_function.func_id = PKE_RSA_EP_4096;
161 		qat_function.bytesize = 512;
162 	}
163 	return qat_function;
164 }
165 
166 static struct qat_asym_function
get_rsa_dec_function(struct rte_crypto_asym_xform * xform)167 get_rsa_dec_function(struct rte_crypto_asym_xform *xform)
168 {
169 	struct qat_asym_function qat_function = { };
170 
171 	if (xform->rsa.n.length <= 64) {
172 		qat_function.func_id = PKE_RSA_DP1_512;
173 		qat_function.bytesize = 64;
174 	} else if (xform->rsa.n.length <= 128) {
175 		qat_function.func_id = PKE_RSA_DP1_1024;
176 		qat_function.bytesize = 128;
177 	} else if (xform->rsa.n.length <= 192) {
178 		qat_function.func_id = PKE_RSA_DP1_1536;
179 		qat_function.bytesize = 192;
180 	} else if (xform->rsa.n.length <= 256) {
181 		qat_function.func_id = PKE_RSA_DP1_2048;
182 		qat_function.bytesize = 256;
183 	} else if (xform->rsa.n.length <= 384) {
184 		qat_function.func_id = PKE_RSA_DP1_3072;
185 		qat_function.bytesize = 384;
186 	} else if (xform->rsa.n.length <= 512) {
187 		qat_function.func_id = PKE_RSA_DP1_4096;
188 		qat_function.bytesize = 512;
189 	}
190 	return qat_function;
191 }
192 
193 static struct qat_asym_function
get_rsa_crt_function(struct rte_crypto_asym_xform * xform)194 get_rsa_crt_function(struct rte_crypto_asym_xform *xform)
195 {
196 	struct qat_asym_function qat_function = { };
197 	int nlen = xform->rsa.qt.p.length * 2;
198 
199 	if (nlen <= 64) {
200 		qat_function.func_id = PKE_RSA_DP2_512;
201 		qat_function.bytesize = 64;
202 	} else if (nlen <= 128) {
203 		qat_function.func_id = PKE_RSA_DP2_1024;
204 		qat_function.bytesize = 128;
205 	} else if (nlen <= 192) {
206 		qat_function.func_id = PKE_RSA_DP2_1536;
207 		qat_function.bytesize = 192;
208 	} else if (nlen <= 256) {
209 		qat_function.func_id = PKE_RSA_DP2_2048;
210 		qat_function.bytesize = 256;
211 	} else if (nlen <= 384) {
212 		qat_function.func_id = PKE_RSA_DP2_3072;
213 		qat_function.bytesize = 384;
214 	} else if (nlen <= 512) {
215 		qat_function.func_id = PKE_RSA_DP2_4096;
216 		qat_function.bytesize = 512;
217 	}
218 	return qat_function;
219 }
220 
221 static struct qat_asym_function
get_ecdsa_verify_function(struct rte_crypto_asym_xform * xform)222 get_ecdsa_verify_function(struct rte_crypto_asym_xform *xform)
223 {
224 	struct qat_asym_function qat_function;
225 
226 	switch (xform->ec.curve_id) {
227 	case RTE_CRYPTO_EC_GROUP_SECP256R1:
228 		qat_function.func_id = PKE_ECDSA_VERIFY_GFP_L256;
229 		qat_function.bytesize = 32;
230 		break;
231 	case RTE_CRYPTO_EC_GROUP_SECP521R1:
232 		qat_function.func_id = PKE_ECDSA_VERIFY_GFP_521;
233 		qat_function.bytesize = 66;
234 		break;
235 	default:
236 		qat_function.func_id = 0;
237 	}
238 	return qat_function;
239 }
240 
241 static struct qat_asym_function
get_ecdsa_function(struct rte_crypto_asym_xform * xform)242 get_ecdsa_function(struct rte_crypto_asym_xform *xform)
243 {
244 	struct qat_asym_function qat_function;
245 
246 	switch (xform->ec.curve_id) {
247 	case RTE_CRYPTO_EC_GROUP_SECP256R1:
248 		qat_function.func_id = PKE_ECDSA_SIGN_RS_GFP_L256;
249 		qat_function.bytesize = 32;
250 		break;
251 	case RTE_CRYPTO_EC_GROUP_SECP521R1:
252 		qat_function.func_id = PKE_ECDSA_SIGN_RS_GFP_521;
253 		qat_function.bytesize = 66;
254 		break;
255 	default:
256 		qat_function.func_id = 0;
257 	}
258 	return qat_function;
259 }
260 
261 static struct qat_asym_function
get_ecpm_function(struct rte_crypto_asym_xform * xform)262 get_ecpm_function(struct rte_crypto_asym_xform *xform)
263 {
264 	struct qat_asym_function qat_function;
265 
266 	switch (xform->ec.curve_id) {
267 	case RTE_CRYPTO_EC_GROUP_SECP256R1:
268 		qat_function.func_id = MATHS_POINT_MULTIPLICATION_GFP_L256;
269 		qat_function.bytesize = 32;
270 		break;
271 	case RTE_CRYPTO_EC_GROUP_SECP521R1:
272 		qat_function.func_id = MATHS_POINT_MULTIPLICATION_GFP_521;
273 		qat_function.bytesize = 66;
274 		break;
275 	default:
276 		qat_function.func_id = 0;
277 	}
278 	return qat_function;
279 }
280 
281 #endif
282