Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 2 | /* |
| 3 | * caam - Freescale FSL CAAM support for Public Key Cryptography descriptors |
| 4 | * |
| 5 | * Copyright 2016 Freescale Semiconductor, Inc. |
| 6 | * |
| 7 | * There is no Shared Descriptor for PKC so that the Job Descriptor must carry |
| 8 | * all the desired key parameters, input and output pointers. |
| 9 | */ |
| 10 | |
| 11 | #ifndef _PKC_DESC_H_ |
| 12 | #define _PKC_DESC_H_ |
| 13 | #include "compat.h" |
| 14 | #include "pdb.h" |
| 15 | |
| 16 | /** |
Radu Alexe | 52e26d7 | 2017-04-25 16:26:38 +0300 | [diff] [blame] | 17 | * caam_priv_key_form - CAAM RSA private key representation |
Radu Alexe | 4a651b1 | 2017-04-25 16:26:39 +0300 | [diff] [blame] | 18 | * CAAM RSA private key may have either of three forms. |
Radu Alexe | 52e26d7 | 2017-04-25 16:26:38 +0300 | [diff] [blame] | 19 | * |
| 20 | * 1. The first representation consists of the pair (n, d), where the |
| 21 | * components have the following meanings: |
| 22 | * n the RSA modulus |
| 23 | * d the RSA private exponent |
| 24 | * |
| 25 | * 2. The second representation consists of the triplet (p, q, d), where the |
| 26 | * components have the following meanings: |
| 27 | * p the first prime factor of the RSA modulus n |
| 28 | * q the second prime factor of the RSA modulus n |
| 29 | * d the RSA private exponent |
Radu Alexe | 4a651b1 | 2017-04-25 16:26:39 +0300 | [diff] [blame] | 30 | * |
| 31 | * 3. The third representation consists of the quintuple (p, q, dP, dQ, qInv), |
| 32 | * where the components have the following meanings: |
| 33 | * p the first prime factor of the RSA modulus n |
| 34 | * q the second prime factor of the RSA modulus n |
| 35 | * dP the first factors's CRT exponent |
| 36 | * dQ the second factors's CRT exponent |
| 37 | * qInv the (first) CRT coefficient |
| 38 | * |
| 39 | * The benefit of using the third or the second key form is lower computational |
| 40 | * cost for the decryption and signature operations. |
Radu Alexe | 52e26d7 | 2017-04-25 16:26:38 +0300 | [diff] [blame] | 41 | */ |
| 42 | enum caam_priv_key_form { |
| 43 | FORM1, |
| 44 | FORM2, |
Radu Alexe | 4a651b1 | 2017-04-25 16:26:39 +0300 | [diff] [blame] | 45 | FORM3 |
Radu Alexe | 52e26d7 | 2017-04-25 16:26:38 +0300 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | /** |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 49 | * caam_rsa_key - CAAM RSA key structure. Keys are allocated in DMA zone. |
| 50 | * @n : RSA modulus raw byte stream |
| 51 | * @e : RSA public exponent raw byte stream |
| 52 | * @d : RSA private exponent raw byte stream |
Radu Alexe | 52e26d7 | 2017-04-25 16:26:38 +0300 | [diff] [blame] | 53 | * @p : RSA prime factor p of RSA modulus n |
| 54 | * @q : RSA prime factor q of RSA modulus n |
Radu Alexe | 4a651b1 | 2017-04-25 16:26:39 +0300 | [diff] [blame] | 55 | * @dp : RSA CRT exponent of p |
| 56 | * @dp : RSA CRT exponent of q |
| 57 | * @qinv : RSA CRT coefficient |
Radu Alexe | 52e26d7 | 2017-04-25 16:26:38 +0300 | [diff] [blame] | 58 | * @tmp1 : CAAM uses this temporary buffer as internal state buffer. |
| 59 | * It is assumed to be as long as p. |
| 60 | * @tmp2 : CAAM uses this temporary buffer as internal state buffer. |
| 61 | * It is assumed to be as long as q. |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 62 | * @n_sz : length in bytes of RSA modulus n |
| 63 | * @e_sz : length in bytes of RSA public exponent |
| 64 | * @d_sz : length in bytes of RSA private exponent |
Radu Alexe | 52e26d7 | 2017-04-25 16:26:38 +0300 | [diff] [blame] | 65 | * @p_sz : length in bytes of RSA prime factor p of RSA modulus n |
| 66 | * @q_sz : length in bytes of RSA prime factor q of RSA modulus n |
| 67 | * @priv_form : CAAM RSA private key representation |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 68 | */ |
| 69 | struct caam_rsa_key { |
| 70 | u8 *n; |
| 71 | u8 *e; |
| 72 | u8 *d; |
Radu Alexe | 52e26d7 | 2017-04-25 16:26:38 +0300 | [diff] [blame] | 73 | u8 *p; |
| 74 | u8 *q; |
Radu Alexe | 4a651b1 | 2017-04-25 16:26:39 +0300 | [diff] [blame] | 75 | u8 *dp; |
| 76 | u8 *dq; |
| 77 | u8 *qinv; |
Radu Alexe | 52e26d7 | 2017-04-25 16:26:38 +0300 | [diff] [blame] | 78 | u8 *tmp1; |
| 79 | u8 *tmp2; |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 80 | size_t n_sz; |
| 81 | size_t e_sz; |
| 82 | size_t d_sz; |
Radu Alexe | 52e26d7 | 2017-04-25 16:26:38 +0300 | [diff] [blame] | 83 | size_t p_sz; |
| 84 | size_t q_sz; |
| 85 | enum caam_priv_key_form priv_form; |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | /** |
| 89 | * caam_rsa_ctx - per session context. |
| 90 | * @key : RSA key in DMA zone |
| 91 | * @dev : device structure |
| 92 | */ |
| 93 | struct caam_rsa_ctx { |
| 94 | struct caam_rsa_key key; |
| 95 | struct device *dev; |
| 96 | }; |
| 97 | |
| 98 | /** |
Horia Geantă | 8a2a0dd | 2018-04-16 08:07:05 -0500 | [diff] [blame] | 99 | * caam_rsa_req_ctx - per request context. |
| 100 | * @src: input scatterlist (stripped of leading zeros) |
| 101 | */ |
| 102 | struct caam_rsa_req_ctx { |
| 103 | struct scatterlist src[2]; |
| 104 | }; |
| 105 | |
| 106 | /** |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 107 | * rsa_edesc - s/w-extended rsa descriptor |
| 108 | * @src_nents : number of segments in input scatterlist |
| 109 | * @dst_nents : number of segments in output scatterlist |
| 110 | * @sec4_sg_bytes : length of h/w link table |
| 111 | * @sec4_sg_dma : dma address of h/w link table |
| 112 | * @sec4_sg : pointer to h/w link table |
| 113 | * @pdb : specific RSA Protocol Data Block (PDB) |
| 114 | * @hw_desc : descriptor followed by link tables if any |
| 115 | */ |
| 116 | struct rsa_edesc { |
| 117 | int src_nents; |
| 118 | int dst_nents; |
| 119 | int sec4_sg_bytes; |
| 120 | dma_addr_t sec4_sg_dma; |
| 121 | struct sec4_sg_entry *sec4_sg; |
| 122 | union { |
| 123 | struct rsa_pub_pdb pub; |
| 124 | struct rsa_priv_f1_pdb priv_f1; |
Radu Alexe | 52e26d7 | 2017-04-25 16:26:38 +0300 | [diff] [blame] | 125 | struct rsa_priv_f2_pdb priv_f2; |
Radu Alexe | 4a651b1 | 2017-04-25 16:26:39 +0300 | [diff] [blame] | 126 | struct rsa_priv_f3_pdb priv_f3; |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 127 | } pdb; |
| 128 | u32 hw_desc[]; |
| 129 | }; |
| 130 | |
| 131 | /* Descriptor construction primitives. */ |
| 132 | void init_rsa_pub_desc(u32 *desc, struct rsa_pub_pdb *pdb); |
| 133 | void init_rsa_priv_f1_desc(u32 *desc, struct rsa_priv_f1_pdb *pdb); |
Radu Alexe | 52e26d7 | 2017-04-25 16:26:38 +0300 | [diff] [blame] | 134 | void init_rsa_priv_f2_desc(u32 *desc, struct rsa_priv_f2_pdb *pdb); |
Radu Alexe | 4a651b1 | 2017-04-25 16:26:39 +0300 | [diff] [blame] | 135 | void init_rsa_priv_f3_desc(u32 *desc, struct rsa_priv_f3_pdb *pdb); |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 136 | |
| 137 | #endif |