1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 28c419778STudor Ambarus /* 38c419778STudor Ambarus * caam - Freescale FSL CAAM support for Public Key Cryptography descriptors 48c419778STudor Ambarus * 58c419778STudor Ambarus * Copyright 2016 Freescale Semiconductor, Inc. 68c419778STudor Ambarus * 78c419778STudor Ambarus * There is no Shared Descriptor for PKC so that the Job Descriptor must carry 88c419778STudor Ambarus * all the desired key parameters, input and output pointers. 98c419778STudor Ambarus */ 108c419778STudor Ambarus 118c419778STudor Ambarus #ifndef _PKC_DESC_H_ 128c419778STudor Ambarus #define _PKC_DESC_H_ 138c419778STudor Ambarus #include "compat.h" 148c419778STudor Ambarus #include "pdb.h" 158c419778STudor Ambarus 168c419778STudor Ambarus /** 1752e26d77SRadu Alexe * caam_priv_key_form - CAAM RSA private key representation 184a651b12SRadu Alexe * CAAM RSA private key may have either of three forms. 1952e26d77SRadu Alexe * 2052e26d77SRadu Alexe * 1. The first representation consists of the pair (n, d), where the 2152e26d77SRadu Alexe * components have the following meanings: 2252e26d77SRadu Alexe * n the RSA modulus 2352e26d77SRadu Alexe * d the RSA private exponent 2452e26d77SRadu Alexe * 2552e26d77SRadu Alexe * 2. The second representation consists of the triplet (p, q, d), where the 2652e26d77SRadu Alexe * components have the following meanings: 2752e26d77SRadu Alexe * p the first prime factor of the RSA modulus n 2852e26d77SRadu Alexe * q the second prime factor of the RSA modulus n 2952e26d77SRadu Alexe * d the RSA private exponent 304a651b12SRadu Alexe * 314a651b12SRadu Alexe * 3. The third representation consists of the quintuple (p, q, dP, dQ, qInv), 324a651b12SRadu Alexe * where the components have the following meanings: 334a651b12SRadu Alexe * p the first prime factor of the RSA modulus n 344a651b12SRadu Alexe * q the second prime factor of the RSA modulus n 354a651b12SRadu Alexe * dP the first factors's CRT exponent 364a651b12SRadu Alexe * dQ the second factors's CRT exponent 374a651b12SRadu Alexe * qInv the (first) CRT coefficient 384a651b12SRadu Alexe * 394a651b12SRadu Alexe * The benefit of using the third or the second key form is lower computational 404a651b12SRadu Alexe * cost for the decryption and signature operations. 4152e26d77SRadu Alexe */ 4252e26d77SRadu Alexe enum caam_priv_key_form { 4352e26d77SRadu Alexe FORM1, 4452e26d77SRadu Alexe FORM2, 454a651b12SRadu Alexe FORM3 4652e26d77SRadu Alexe }; 4752e26d77SRadu Alexe 4852e26d77SRadu Alexe /** 498c419778STudor Ambarus * caam_rsa_key - CAAM RSA key structure. Keys are allocated in DMA zone. 508c419778STudor Ambarus * @n : RSA modulus raw byte stream 518c419778STudor Ambarus * @e : RSA public exponent raw byte stream 528c419778STudor Ambarus * @d : RSA private exponent raw byte stream 5352e26d77SRadu Alexe * @p : RSA prime factor p of RSA modulus n 5452e26d77SRadu Alexe * @q : RSA prime factor q of RSA modulus n 554a651b12SRadu Alexe * @dp : RSA CRT exponent of p 564a651b12SRadu Alexe * @dp : RSA CRT exponent of q 574a651b12SRadu Alexe * @qinv : RSA CRT coefficient 5852e26d77SRadu Alexe * @tmp1 : CAAM uses this temporary buffer as internal state buffer. 5952e26d77SRadu Alexe * It is assumed to be as long as p. 6052e26d77SRadu Alexe * @tmp2 : CAAM uses this temporary buffer as internal state buffer. 6152e26d77SRadu Alexe * It is assumed to be as long as q. 628c419778STudor Ambarus * @n_sz : length in bytes of RSA modulus n 638c419778STudor Ambarus * @e_sz : length in bytes of RSA public exponent 648c419778STudor Ambarus * @d_sz : length in bytes of RSA private exponent 6552e26d77SRadu Alexe * @p_sz : length in bytes of RSA prime factor p of RSA modulus n 6652e26d77SRadu Alexe * @q_sz : length in bytes of RSA prime factor q of RSA modulus n 6752e26d77SRadu Alexe * @priv_form : CAAM RSA private key representation 688c419778STudor Ambarus */ 698c419778STudor Ambarus struct caam_rsa_key { 708c419778STudor Ambarus u8 *n; 718c419778STudor Ambarus u8 *e; 728c419778STudor Ambarus u8 *d; 7352e26d77SRadu Alexe u8 *p; 7452e26d77SRadu Alexe u8 *q; 754a651b12SRadu Alexe u8 *dp; 764a651b12SRadu Alexe u8 *dq; 774a651b12SRadu Alexe u8 *qinv; 7852e26d77SRadu Alexe u8 *tmp1; 7952e26d77SRadu Alexe u8 *tmp2; 808c419778STudor Ambarus size_t n_sz; 818c419778STudor Ambarus size_t e_sz; 828c419778STudor Ambarus size_t d_sz; 8352e26d77SRadu Alexe size_t p_sz; 8452e26d77SRadu Alexe size_t q_sz; 8552e26d77SRadu Alexe enum caam_priv_key_form priv_form; 868c419778STudor Ambarus }; 878c419778STudor Ambarus 888c419778STudor Ambarus /** 898c419778STudor Ambarus * caam_rsa_ctx - per session context. 908c419778STudor Ambarus * @key : RSA key in DMA zone 918c419778STudor Ambarus * @dev : device structure 92c3725f7cSIuliana Prodan * @padding_dma : dma address of padding, for adding it to the input 938c419778STudor Ambarus */ 948c419778STudor Ambarus struct caam_rsa_ctx { 958c419778STudor Ambarus struct caam_rsa_key key; 968c419778STudor Ambarus struct device *dev; 97c3725f7cSIuliana Prodan dma_addr_t padding_dma; 983b2614cbSIuliana Prodan 998c419778STudor Ambarus }; 1008c419778STudor Ambarus 1018c419778STudor Ambarus /** 1028a2a0dd3SHoria Geantă * caam_rsa_req_ctx - per request context. 1038a2a0dd3SHoria Geantă * @src : input scatterlist (stripped of leading zeros) 1043b2614cbSIuliana Prodan * @fixup_src : input scatterlist (that might be stripped of leading zeros) 1053b2614cbSIuliana Prodan * @fixup_src_len : length of the fixup_src input scatterlist 106*bf537950SIuliana Prodan * @edesc : s/w-extended rsa descriptor 107*bf537950SIuliana Prodan * @akcipher_op_done : callback used when operation is done 1088a2a0dd3SHoria Geantă */ 1098a2a0dd3SHoria Geantă struct caam_rsa_req_ctx { 1108a2a0dd3SHoria Geantă struct scatterlist src[2]; 1113b2614cbSIuliana Prodan struct scatterlist *fixup_src; 1123b2614cbSIuliana Prodan unsigned int fixup_src_len; 113*bf537950SIuliana Prodan struct rsa_edesc *edesc; 114*bf537950SIuliana Prodan void (*akcipher_op_done)(struct device *jrdev, u32 *desc, u32 err, 115*bf537950SIuliana Prodan void *context); 1168a2a0dd3SHoria Geantă }; 1178a2a0dd3SHoria Geantă 1188a2a0dd3SHoria Geantă /** 1198c419778STudor Ambarus * rsa_edesc - s/w-extended rsa descriptor 120eff9771dSIuliana Prodan * @src_nents : number of segments in input s/w scatterlist 121eff9771dSIuliana Prodan * @dst_nents : number of segments in output s/w scatterlist 122eff9771dSIuliana Prodan * @mapped_src_nents: number of segments in input h/w link table 123eff9771dSIuliana Prodan * @mapped_dst_nents: number of segments in output h/w link table 1248c419778STudor Ambarus * @sec4_sg_bytes : length of h/w link table 125*bf537950SIuliana Prodan * @bklog : stored to determine if the request needs backlog 1268c419778STudor Ambarus * @sec4_sg_dma : dma address of h/w link table 1278c419778STudor Ambarus * @sec4_sg : pointer to h/w link table 1288c419778STudor Ambarus * @pdb : specific RSA Protocol Data Block (PDB) 1298c419778STudor Ambarus * @hw_desc : descriptor followed by link tables if any 1308c419778STudor Ambarus */ 1318c419778STudor Ambarus struct rsa_edesc { 1328c419778STudor Ambarus int src_nents; 1338c419778STudor Ambarus int dst_nents; 134eff9771dSIuliana Prodan int mapped_src_nents; 135eff9771dSIuliana Prodan int mapped_dst_nents; 1368c419778STudor Ambarus int sec4_sg_bytes; 137*bf537950SIuliana Prodan bool bklog; 1388c419778STudor Ambarus dma_addr_t sec4_sg_dma; 1398c419778STudor Ambarus struct sec4_sg_entry *sec4_sg; 1408c419778STudor Ambarus union { 1418c419778STudor Ambarus struct rsa_pub_pdb pub; 1428c419778STudor Ambarus struct rsa_priv_f1_pdb priv_f1; 14352e26d77SRadu Alexe struct rsa_priv_f2_pdb priv_f2; 1444a651b12SRadu Alexe struct rsa_priv_f3_pdb priv_f3; 1458c419778STudor Ambarus } pdb; 1468c419778STudor Ambarus u32 hw_desc[]; 1478c419778STudor Ambarus }; 1488c419778STudor Ambarus 1498c419778STudor Ambarus /* Descriptor construction primitives. */ 1508c419778STudor Ambarus void init_rsa_pub_desc(u32 *desc, struct rsa_pub_pdb *pdb); 1518c419778STudor Ambarus void init_rsa_priv_f1_desc(u32 *desc, struct rsa_priv_f1_pdb *pdb); 15252e26d77SRadu Alexe void init_rsa_priv_f2_desc(u32 *desc, struct rsa_priv_f2_pdb *pdb); 1534a651b12SRadu Alexe void init_rsa_priv_f3_desc(u32 *desc, struct rsa_priv_f3_pdb *pdb); 1548c419778STudor Ambarus 1558c419778STudor Ambarus #endif 156