1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Cavium Networks
3  */
4 
5 #ifndef _RTE_CRYPTO_ASYM_H_
6 #define _RTE_CRYPTO_ASYM_H_
7 
8 /**
9  * @file rte_crypto_asym.h
10  *
11  * RTE Definitions for Asymmetric Cryptography
12  *
13  * Defines asymmetric algorithms and modes, as well as supported
14  * asymmetric crypto operations.
15  */
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 #include <string.h>
22 #include <stdint.h>
23 
24 #include <rte_memory.h>
25 #include <rte_mempool.h>
26 #include <rte_common.h>
27 
28 typedef struct rte_crypto_param_t {
29 	uint8_t *data;
30 	/**< pointer to buffer holding data */
31 	rte_iova_t iova;
32 	/**< IO address of data buffer */
33 	size_t length;
34 	/**< length of data in bytes */
35 } rte_crypto_param;
36 
37 /** asym xform type name strings */
38 extern const char *
39 rte_crypto_asym_xform_strings[];
40 
41 /** asym operations type name strings */
42 extern const char *
43 rte_crypto_asym_op_strings[];
44 
45 /**
46  * Asymmetric crypto transformation types.
47  * Each xform type maps to one asymmetric algorithm
48  * performing specific operation
49  *
50  */
51 enum rte_crypto_asym_xform_type {
52 	RTE_CRYPTO_ASYM_XFORM_UNSPECIFIED = 0,
53 	/**< Invalid xform. */
54 	RTE_CRYPTO_ASYM_XFORM_NONE,
55 	/**< Xform type None.
56 	 * May be supported by PMD to support
57 	 * passthrough op for debugging purpose.
58 	 * if xform_type none , op_type is disregarded.
59 	 */
60 	RTE_CRYPTO_ASYM_XFORM_RSA,
61 	/**< RSA. Performs Encrypt, Decrypt, Sign and Verify.
62 	 * Refer to rte_crypto_asym_op_type
63 	 */
64 	RTE_CRYPTO_ASYM_XFORM_DH,
65 	/**< Diffie-Hellman.
66 	 * Performs Key Generate and Shared Secret Compute.
67 	 * Refer to rte_crypto_asym_op_type
68 	 */
69 	RTE_CRYPTO_ASYM_XFORM_DSA,
70 	/**< Digital Signature Algorithm
71 	 * Performs Signature Generation and Verification.
72 	 * Refer to rte_crypto_asym_op_type
73 	 */
74 	RTE_CRYPTO_ASYM_XFORM_MODINV,
75 	/**< Modular Inverse
76 	 * Perform Modulus inverse b^(-1) mod n
77 	 */
78 	RTE_CRYPTO_ASYM_XFORM_MODEX,
79 	/**< Modular Exponentiation
80 	 * Perform Modular Exponentiation b^e mod n
81 	 */
82 	RTE_CRYPTO_ASYM_XFORM_TYPE_LIST_END
83 	/**< End of list */
84 };
85 
86 /**
87  * Asymmetric crypto operation type variants
88  */
89 enum rte_crypto_asym_op_type {
90 	RTE_CRYPTO_ASYM_OP_ENCRYPT,
91 	/**< Asymmetric Encrypt operation */
92 	RTE_CRYPTO_ASYM_OP_DECRYPT,
93 	/**< Asymmetric Decrypt operation */
94 	RTE_CRYPTO_ASYM_OP_SIGN,
95 	/**< Signature Generation operation */
96 	RTE_CRYPTO_ASYM_OP_VERIFY,
97 	/**< Signature Verification operation */
98 	RTE_CRYPTO_ASYM_OP_PRIVATE_KEY_GENERATE,
99 	/**< DH Private Key generation operation */
100 	RTE_CRYPTO_ASYM_OP_PUBLIC_KEY_GENERATE,
101 	/**< DH Public Key generation operation */
102 	RTE_CRYPTO_ASYM_OP_SHARED_SECRET_COMPUTE,
103 	/**< DH Shared Secret compute operation */
104 	RTE_CRYPTO_ASYM_OP_LIST_END
105 };
106 
107 /**
108  * Padding types for RSA signature.
109  */
110 enum rte_crypto_rsa_padding_type {
111 	RTE_CRYPTO_RSA_PADDING_NONE = 0,
112 	/**< RSA no padding scheme */
113 	RTE_CRYPTO_RSA_PKCS1_V1_5_BT0,
114 	/**< RSA PKCS#1 V1.5 Block Type 0 padding scheme
115 	 * as described in rfc2313
116 	 */
117 	RTE_CRYPTO_RSA_PKCS1_V1_5_BT1,
118 	/**< RSA PKCS#1 V1.5 Block Type 01 padding scheme
119 	 * as described in rfc2313
120 	 */
121 	RTE_CRYPTO_RSA_PKCS1_V1_5_BT2,
122 	/**< RSA PKCS#1 V1.5 Block Type 02 padding scheme
123 	 * as described in rfc2313
124 	 */
125 	RTE_CRYPTO_RSA_PADDING_OAEP,
126 	/**< RSA PKCS#1 OAEP padding scheme */
127 	RTE_CRYPTO_RSA_PADDING_PSS,
128 	/**< RSA PKCS#1 PSS padding scheme */
129 	RTE_CRYPTO_RSA_PADDING_TYPE_LIST_END
130 };
131 
132 /**
133  * RSA private key type enumeration
134  *
135  * enumerates private key format required to perform RSA crypto
136  * transform.
137  *
138  */
139 enum rte_crypto_rsa_priv_key_type {
140 	RTE_RSA_KEY_TYPE_EXP,
141 	/**< RSA private key is an exponent */
142 	RTE_RSA_KET_TYPE_QT,
143 	/**< RSA private key is in quintuple format
144 	 * See rte_crypto_rsa_priv_key_qt
145 	 */
146 };
147 
148 /**
149  * Structure describing RSA private key in quintuple format.
150  * See PKCS V1.5 RSA Cryptography Standard.
151  */
152 struct rte_crypto_rsa_priv_key_qt {
153 	rte_crypto_param p;
154 	/**< p - Private key component P
155 	 * Private key component of RSA parameter  required for CRT method
156 	 * of private key operations in Octet-string network byte order
157 	 * format.
158 	 */
159 
160 	rte_crypto_param q;
161 	/**< q - Private key component Q
162 	 * Private key component of RSA parameter  required for CRT method
163 	 * of private key operations in Octet-string network byte order
164 	 * format.
165 	 */
166 
167 	rte_crypto_param dP;
168 	/**< dP - Private CRT component
169 	 * Private CRT component of RSA parameter  required for CRT method
170 	 * RSA private key operations in Octet-string network byte order
171 	 * format.
172 	 * dP = d mod ( p - 1 )
173 	 */
174 
175 	rte_crypto_param dQ;
176 	/**< dQ - Private CRT component
177 	 * Private CRT component of RSA parameter  required for CRT method
178 	 * RSA private key operations in Octet-string network byte order
179 	 * format.
180 	 * dQ = d mod ( q - 1 )
181 	 */
182 
183 	rte_crypto_param qInv;
184 	/**< qInv - Private CRT component
185 	 * Private CRT component of RSA parameter  required for CRT method
186 	 * RSA private key operations in Octet-string network byte order
187 	 * format.
188 	 * qInv = inv q mod p
189 	 */
190 };
191 
192 /**
193  * Asymmetric RSA transform data
194  *
195  * Structure describing RSA xform params
196  *
197  */
198 struct rte_crypto_rsa_xform {
199 	rte_crypto_param n;
200 	/**< n - Prime modulus
201 	 * Prime modulus data of RSA operation in Octet-string network
202 	 * byte order format.
203 	 */
204 
205 	rte_crypto_param e;
206 	/**< e - Public key exponent
207 	 * Public key exponent used for RSA public key operations in Octet-
208 	 * string network byte order format.
209 	 */
210 
211 	enum rte_crypto_rsa_priv_key_type key_type;
212 
213 	__extension__
214 	union {
215 		rte_crypto_param d;
216 		/**< d - Private key exponent
217 		 * Private key exponent used for RSA
218 		 * private key operations in
219 		 * Octet-string  network byte order format.
220 		 */
221 
222 		struct rte_crypto_rsa_priv_key_qt qt;
223 		/**< qt - Private key in quintuple format */
224 	};
225 };
226 
227 /**
228  * Asymmetric Modular exponentiation transform data
229  *
230  * Structure describing modular exponentiation xform param
231  *
232  */
233 struct rte_crypto_modex_xform {
234 	rte_crypto_param modulus;
235 	/**< modulus
236 	 * Prime modulus of the modexp transform operation in octet-string
237 	 * network byte order format.
238 	 */
239 
240 	rte_crypto_param exponent;
241 	/**< exponent
242 	 * Private exponent of the modexp transform operation in
243 	 * octet-string network byte order format.
244 	 */
245 };
246 
247 /**
248  * Asymmetric modular inverse transform operation
249  *
250  * Structure describing modulus inverse xform params
251  *
252  */
253 struct rte_crypto_modinv_xform {
254 	rte_crypto_param modulus;
255 	/**<
256 	 * Pointer to the prime modulus data for modular
257 	 * inverse operation in octet-string network byte
258 	 * order format.
259 	 */
260 };
261 
262 /**
263  * Asymmetric DH transform data
264  *
265  * Structure describing deffie-hellman xform params
266  *
267  */
268 struct rte_crypto_dh_xform {
269 	enum rte_crypto_asym_op_type type;
270 	/**< Setup xform for key generate or shared secret compute */
271 
272 	rte_crypto_param p;
273 	/**< p : Prime modulus data
274 	 * DH prime modulus data in octet-string network byte order format.
275 	 *
276 	 */
277 
278 	rte_crypto_param g;
279 	/**< g : Generator
280 	 * DH group generator data in octet-string network byte order
281 	 * format.
282 	 *
283 	 */
284 };
285 
286 /**
287  * Asymmetric Digital Signature transform operation
288  *
289  * Structure describing DSA xform params
290  *
291  */
292 struct rte_crypto_dsa_xform {
293 	rte_crypto_param p;
294 	/**< p - Prime modulus
295 	 * Prime modulus data for DSA operation in Octet-string network byte
296 	 * order format.
297 	 */
298 	rte_crypto_param q;
299 	/**< q : Order of the subgroup.
300 	 * Order of the subgroup data in Octet-string network byte order
301 	 * format.
302 	 * (p-1) % q = 0
303 	 */
304 	rte_crypto_param g;
305 	/**< g: Generator of the subgroup
306 	 * Generator  data in Octet-string network byte order format.
307 	 */
308 	rte_crypto_param x;
309 	/**< x: Private key of the signer in octet-string network
310 	 * byte order format.
311 	 * Used when app has pre-defined private key.
312 	 * Valid only when xform chain is DSA ONLY.
313 	 * if xform chain is DH private key generate + DSA, then DSA sign
314 	 * compute will use internally generated key.
315 	 */
316 };
317 
318 /**
319  * Operations params for modular operations:
320  * exponentiation and invert
321  *
322  */
323 struct rte_crypto_mod_op_param {
324 	rte_crypto_param base;
325 	/**<
326 	 * Pointer to base of modular exponentiation/inversion data in
327 	 * Octet-string network byte order format.
328 	 */
329 };
330 
331 /**
332  * Asymmetric crypto transform data
333  *
334  * Structure describing asym xforms.
335  */
336 struct rte_crypto_asym_xform {
337 	struct rte_crypto_asym_xform *next;
338 	/**< Pointer to next xform to set up xform chain.*/
339 	enum rte_crypto_asym_xform_type xform_type;
340 	/**< Asymmetric crypto transform */
341 
342 	__extension__
343 	union {
344 		struct rte_crypto_rsa_xform rsa;
345 		/**< RSA xform parameters */
346 
347 		struct rte_crypto_modex_xform modex;
348 		/**< Modular Exponentiation xform parameters */
349 
350 		struct rte_crypto_modinv_xform modinv;
351 		/**< Modulus Inverse xform parameters */
352 
353 		struct rte_crypto_dh_xform dh;
354 		/**< DH xform parameters */
355 
356 		struct rte_crypto_dsa_xform dsa;
357 		/**< DSA xform parameters */
358 	};
359 };
360 
361 struct rte_cryptodev_asym_session;
362 
363 /**
364  * RSA operation params
365  *
366  */
367 struct rte_crypto_rsa_op_param {
368 	enum rte_crypto_asym_op_type op_type;
369 	/**< Type of RSA operation for transform */;
370 
371 	rte_crypto_param message;
372 	/**<
373 	 * Pointer to data
374 	 * - to be encrypted for RSA public encrypt.
375 	 * - to be decrypted for RSA private decrypt.
376 	 * - to be signed for RSA sign generation.
377 	 * - to be authenticated for RSA sign verification.
378 	 */
379 
380 	rte_crypto_param sign;
381 	/**<
382 	 * Pointer to RSA signature data. If operation is RSA
383 	 * sign @ref RTE_CRYPTO_ASYM_OP_SIGN, buffer will be
384 	 * over-written with generated signature.
385 	 *
386 	 * Length of the signature data will be equal to the
387 	 * RSA prime modulus length.
388 	 */
389 
390 	enum rte_crypto_rsa_padding_type pad;
391 	/**< RSA padding scheme to be used for transform */
392 
393 	enum rte_crypto_auth_algorithm md;
394 	/**< Hash algorithm to be used for data hash if padding
395 	 * scheme is either OAEP or PSS. Valid hash algorithms
396 	 * are:
397 	 * MD5, SHA1, SHA224, SHA256, SHA384, SHA512
398 	 */
399 
400 	enum rte_crypto_auth_algorithm mgf1md;
401 	/**<
402 	 * Hash algorithm to be used for mask generation if
403 	 * padding scheme is either OAEP or PSS. If padding
404 	 * scheme is unspecified data hash algorithm is used
405 	 * for mask generation. Valid hash algorithms are:
406 	 * MD5, SHA1, SHA224, SHA256, SHA384, SHA512
407 	 */
408 };
409 
410 /**
411  * Diffie-Hellman Operations params.
412  * @note:
413  */
414 struct rte_crypto_dh_op_param {
415 	rte_crypto_param pub_key;
416 	/**<
417 	 * Output generated public key when xform type is
418 	 * DH PUB_KEY_GENERATION.
419 	 * Input peer public key when xform type is DH
420 	 * SHARED_SECRET_COMPUTATION
421 	 * pub_key is in octet-string network byte order format.
422 	 *
423 	 */
424 
425 	rte_crypto_param priv_key;
426 	/**<
427 	 * Output generated private key if xform type is
428 	 * DH PRIVATE_KEY_GENERATION
429 	 * Input when xform type is DH SHARED_SECRET_COMPUTATION.
430 	 * priv_key is in octet-string network byte order format.
431 	 *
432 	 */
433 
434 	rte_crypto_param shared_secret;
435 	/**<
436 	 * Output with calculated shared secret
437 	 * when dh xform set up with op type = SHARED_SECRET_COMPUTATION.
438 	 * shared_secret is an octet-string network byte order format.
439 	 *
440 	 */
441 };
442 
443 /**
444  * DSA Operations params
445  *
446  */
447 struct rte_crypto_dsa_op_param {
448 	enum rte_crypto_asym_op_type op_type;
449 	/**< Signature Generation or Verification */
450 	rte_crypto_param message;
451 	/**< input message to be signed or verified */
452 	rte_crypto_param r;
453 	/**< dsa sign component 'r' value
454 	 *
455 	 * output if op_type = sign generate,
456 	 * input if op_type = sign verify
457 	 */
458 	rte_crypto_param s;
459 	/**< dsa sign component 's' value
460 	 *
461 	 * output if op_type = sign generate,
462 	 * input if op_type = sign verify
463 	 */
464 	rte_crypto_param y;
465 	/**< y : Public key of the signer.
466 	 * Public key data of the signer in Octet-string network byte order
467 	 * format.
468 	 * y = g^x mod p
469 	 */
470 };
471 
472 /**
473  * Asymmetric Cryptographic Operation.
474  *
475  * Structure describing asymmetric crypto operation params.
476  *
477  */
478 struct rte_crypto_asym_op {
479 	struct rte_cryptodev_asym_session *session;
480 	/**< Handle for the initialised session context */
481 
482 	__extension__
483 	union {
484 		struct rte_crypto_rsa_op_param rsa;
485 		struct rte_crypto_mod_op_param modex;
486 		struct rte_crypto_mod_op_param modinv;
487 		struct rte_crypto_dh_op_param dh;
488 		struct rte_crypto_dsa_op_param dsa;
489 	};
490 } __rte_cache_aligned;
491 
492 #ifdef __cplusplus
493 }
494 #endif
495 
496 #endif /* _RTE_CRYPTO_ASYM_H_ */
497