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