cryptodev: restore crypto op alignment and layout
[dpdk.git] / lib / librte_cryptodev / rte_crypto_asym.h
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 Multiplicative Inverse
76          * Perform Modular Multiplicative 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 descibed in rfc2313
116          */
117         RTE_CRYPTO_RSA_PKCS1_V1_5_BT1,
118         /**< RSA PKCS#1 V1.5 Block Type 01 padding scheme
119          * as descibed in rfc2313
120          */
121         RTE_CRYPTO_RSA_PKCS1_V1_5_BT2,
122         /**< RSA PKCS#1 V1.5 Block Type 02 padding scheme
123          * as descibed 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 exponentation xform param
231  *
232  */
233 struct rte_crypto_modex_xform {
234         rte_crypto_param modulus;
235         /**< modulus
236          * Pointer to the modulus data for modexp transform operation
237          * in octet-string network byte order format
238          *
239          * In case this number is equal to zero the driver shall set
240          * the crypto op status field to RTE_CRYPTO_OP_STATUS_ERROR
241          */
242
243         rte_crypto_param exponent;
244         /**< exponent
245          * Exponent of the modexp transform operation in
246          * octet-string network byte order format
247          */
248 };
249
250 /**
251  * Asymmetric modular multiplicative inverse transform operation
252  *
253  * Structure describing modular multiplicative inverse transform
254  *
255  */
256 struct rte_crypto_modinv_xform {
257         rte_crypto_param modulus;
258         /**<
259          * Pointer to the modulus data for modular multiplicative inverse
260          * operation in octet-string network byte order format
261          *
262          * In case this number is equal to zero the driver shall set
263          * the crypto op status field to RTE_CRYPTO_OP_STATUS_ERROR
264          *
265          * This number shall be relatively prime to base
266          * in corresponding Modular Multiplicative Inverse
267          * rte_crypto_mod_op_param
268          */
269 };
270
271 /**
272  * Asymmetric DH transform data
273  *
274  * Structure describing deffie-hellman xform params
275  *
276  */
277 struct rte_crypto_dh_xform {
278         enum rte_crypto_asym_op_type type;
279         /**< Setup xform for key generate or shared secret compute */
280
281         rte_crypto_param p;
282         /**< p : Prime modulus data
283          * DH prime modulous data in octet-string network byte order format.
284          *
285          */
286
287         rte_crypto_param g;
288         /**< g : Generator
289          * DH group generator data in octet-string network byte order
290          * format.
291          *
292          */
293 };
294
295 /**
296  * Asymmetric Digital Signature transform operation
297  *
298  * Structure describing DSA xform params
299  *
300  */
301 struct rte_crypto_dsa_xform {
302         rte_crypto_param p;
303         /**< p - Prime modulus
304          * Prime modulus data for DSA operation in Octet-string network byte
305          * order format.
306          */
307         rte_crypto_param q;
308         /**< q : Order of the subgroup.
309          * Order of the subgroup data in Octet-string network byte order
310          * format.
311          * (p-1) % q = 0
312          */
313         rte_crypto_param g;
314         /**< g: Generator of the subgroup
315          * Generator  data in Octet-string network byte order format.
316          */
317         rte_crypto_param x;
318         /**< x: Private key of the signer in octet-string network
319          * byte order format.
320          * Used when app has pre-defined private key.
321          * Valid only when xform chain is DSA ONLY.
322          * if xform chain is DH private key generate + DSA, then DSA sign
323          * compute will use internally generated key.
324          */
325 };
326
327 /**
328  * Operations params for modular operations:
329  * exponentiation and multiplicative inverse
330  *
331  */
332 struct rte_crypto_mod_op_param {
333         rte_crypto_param base;
334         /**<
335          * Pointer to base of modular exponentiation/multiplicative
336          * inverse data in octet-string network byte order format
337          *
338          * In case Multiplicative Inverse is used this number shall
339          * be relatively prime to modulus in corresponding Modular
340          * Multiplicative Inverse rte_crypto_modinv_xform
341          */
342 };
343
344 /**
345  * Asymmetric crypto transform data
346  *
347  * Structure describing asym xforms.
348  */
349 struct rte_crypto_asym_xform {
350         struct rte_crypto_asym_xform *next;
351         /**< Pointer to next xform to set up xform chain.*/
352         enum rte_crypto_asym_xform_type xform_type;
353         /**< Asymmetric crypto transform */
354
355         __extension__
356         union {
357                 struct rte_crypto_rsa_xform rsa;
358                 /**< RSA xform parameters */
359
360                 struct rte_crypto_modex_xform modex;
361                 /**< Modular Exponentiation xform parameters */
362
363                 struct rte_crypto_modinv_xform modinv;
364                 /**< Modular Multiplicative Inverse xform parameters */
365
366                 struct rte_crypto_dh_xform dh;
367                 /**< DH xform parameters */
368
369                 struct rte_crypto_dsa_xform dsa;
370                 /**< DSA xform parameters */
371         };
372 };
373
374 struct rte_cryptodev_asym_session;
375
376 /**
377  * RSA operation params
378  *
379  */
380 struct rte_crypto_rsa_op_param {
381         enum rte_crypto_asym_op_type op_type;
382         /**< Type of RSA operation for transform */;
383
384         rte_crypto_param message;
385         /**<
386          * Pointer to data
387          * - to be encrypted for RSA public encrypt.
388          * - to be decrypted for RSA private decrypt.
389          * - to be signed for RSA sign generation.
390          * - to be authenticated for RSA sign verification.
391          */
392
393         rte_crypto_param sign;
394         /**<
395          * Pointer to RSA signature data. If operation is RSA
396          * sign @ref RTE_CRYPTO_ASYM_OP_SIGN, buffer will be
397          * over-written with generated signature.
398          *
399          * Length of the signature data will be equal to the
400          * RSA prime modulus length.
401          */
402
403         enum rte_crypto_rsa_padding_type pad;
404         /**< RSA padding scheme to be used for transform */
405
406         enum rte_crypto_auth_algorithm md;
407         /**< Hash algorithm to be used for data hash if padding
408          * scheme is either OAEP or PSS. Valid hash algorithms
409          * are:
410          * MD5, SHA1, SHA224, SHA256, SHA384, SHA512
411          */
412
413         enum rte_crypto_auth_algorithm mgf1md;
414         /**<
415          * Hash algorithm to be used for mask generation if
416          * padding scheme is either OAEP or PSS. If padding
417          * scheme is unspecified data hash algorithm is used
418          * for mask generation. Valid hash algorithms are:
419          * MD5, SHA1, SHA224, SHA256, SHA384, SHA512
420          */
421 };
422
423 /**
424  * Diffie-Hellman Operations params.
425  * @note:
426  */
427 struct rte_crypto_dh_op_param {
428         rte_crypto_param pub_key;
429         /**<
430          * Output generated public key when xform type is
431          * DH PUB_KEY_GENERATION.
432          * Input peer public key when xform type is DH
433          * SHARED_SECRET_COMPUTATION
434          * pub_key is in octet-string network byte order format.
435          *
436          */
437
438         rte_crypto_param priv_key;
439         /**<
440          * Output generated private key if xform type is
441          * DH PRIVATE_KEY_GENERATION
442          * Input when xform type is DH SHARED_SECRET_COMPUTATION.
443          * priv_key is in octet-string network byte order format.
444          *
445          */
446
447         rte_crypto_param shared_secret;
448         /**<
449          * Output with calculated shared secret
450          * when dh xform set up with op type = SHARED_SECRET_COMPUTATION.
451          * shared_secret is an octet-string network byte order format.
452          *
453          */
454 };
455
456 /**
457  * DSA Operations params
458  *
459  */
460 struct rte_crypto_dsa_op_param {
461         enum rte_crypto_asym_op_type op_type;
462         /**< Signature Generation or Verification */
463         rte_crypto_param message;
464         /**< input message to be signed or verified */
465         rte_crypto_param r;
466         /**< dsa sign component 'r' value
467          *
468          * output if op_type = sign generate,
469          * input if op_type = sign verify
470          */
471         rte_crypto_param s;
472         /**< dsa sign component 's' value
473          *
474          * output if op_type = sign generate,
475          * input if op_type = sign verify
476          */
477         rte_crypto_param y;
478         /**< y : Public key of the signer.
479          * Public key data of the signer in Octet-string network byte order
480          * format.
481          * y = g^x mod p
482          */
483 };
484
485 /**
486  * Asymmetric Cryptographic Operation.
487  *
488  * Structure describing asymmetric crypto operation params.
489  *
490  */
491 struct rte_crypto_asym_op {
492         struct rte_cryptodev_asym_session *session;
493         /**< Handle for the initialised session context */
494
495         __extension__
496         union {
497                 struct rte_crypto_rsa_op_param rsa;
498                 struct rte_crypto_mod_op_param modex;
499                 struct rte_crypto_mod_op_param modinv;
500                 struct rte_crypto_dh_op_param dh;
501                 struct rte_crypto_dsa_op_param dsa;
502         };
503 };
504
505 #ifdef __cplusplus
506 }
507 #endif
508
509 #endif /* _RTE_CRYPTO_ASYM_H_ */