caampkc.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* SPDX-License-Identifier: GPL-2.0 */
  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. #ifndef _PKC_DESC_H_
  11. #define _PKC_DESC_H_
  12. #include "compat.h"
  13. #include "pdb.h"
  14. #include <crypto/engine.h>
  15. /**
  16. * caam_priv_key_form - CAAM RSA private key representation
  17. * CAAM RSA private key may have either of three forms.
  18. *
  19. * 1. The first representation consists of the pair (n, d), where the
  20. * components have the following meanings:
  21. * n the RSA modulus
  22. * d the RSA private exponent
  23. *
  24. * 2. The second representation consists of the triplet (p, q, d), where the
  25. * components have the following meanings:
  26. * p the first prime factor of the RSA modulus n
  27. * q the second prime factor of the RSA modulus n
  28. * d the RSA private exponent
  29. *
  30. * 3. The third representation consists of the quintuple (p, q, dP, dQ, qInv),
  31. * where the components have the following meanings:
  32. * p the first prime factor of the RSA modulus n
  33. * q the second prime factor of the RSA modulus n
  34. * dP the first factors's CRT exponent
  35. * dQ the second factors's CRT exponent
  36. * qInv the (first) CRT coefficient
  37. *
  38. * The benefit of using the third or the second key form is lower computational
  39. * cost for the decryption and signature operations.
  40. */
  41. enum caam_priv_key_form {
  42. FORM1,
  43. FORM2,
  44. FORM3
  45. };
  46. /**
  47. * caam_rsa_key - CAAM RSA key structure. Keys are allocated in DMA zone.
  48. * @n : RSA modulus raw byte stream
  49. * @e : RSA public exponent raw byte stream
  50. * @d : RSA private exponent raw byte stream
  51. * @p : RSA prime factor p of RSA modulus n
  52. * @q : RSA prime factor q of RSA modulus n
  53. * @dp : RSA CRT exponent of p
  54. * @dp : RSA CRT exponent of q
  55. * @qinv : RSA CRT coefficient
  56. * @tmp1 : CAAM uses this temporary buffer as internal state buffer.
  57. * It is assumed to be as long as p.
  58. * @tmp2 : CAAM uses this temporary buffer as internal state buffer.
  59. * It is assumed to be as long as q.
  60. * @n_sz : length in bytes of RSA modulus n
  61. * @e_sz : length in bytes of RSA public exponent
  62. * @d_sz : length in bytes of RSA private exponent
  63. * @p_sz : length in bytes of RSA prime factor p of RSA modulus n
  64. * @q_sz : length in bytes of RSA prime factor q of RSA modulus n
  65. * @priv_form : CAAM RSA private key representation
  66. */
  67. struct caam_rsa_key {
  68. u8 *n;
  69. u8 *e;
  70. u8 *d;
  71. u8 *p;
  72. u8 *q;
  73. u8 *dp;
  74. u8 *dq;
  75. u8 *qinv;
  76. u8 *tmp1;
  77. u8 *tmp2;
  78. size_t n_sz;
  79. size_t e_sz;
  80. size_t d_sz;
  81. size_t p_sz;
  82. size_t q_sz;
  83. enum caam_priv_key_form priv_form;
  84. };
  85. /**
  86. * caam_rsa_ctx - per session context.
  87. * @enginectx : crypto engine context
  88. * @key : RSA key in DMA zone
  89. * @dev : device structure
  90. * @padding_dma : dma address of padding, for adding it to the input
  91. */
  92. struct caam_rsa_ctx {
  93. struct crypto_engine_ctx enginectx;
  94. struct caam_rsa_key key;
  95. struct device *dev;
  96. dma_addr_t padding_dma;
  97. };
  98. /**
  99. * caam_rsa_req_ctx - per request context.
  100. * @src : input scatterlist (stripped of leading zeros)
  101. * @fixup_src : input scatterlist (that might be stripped of leading zeros)
  102. * @fixup_src_len : length of the fixup_src input scatterlist
  103. * @edesc : s/w-extended rsa descriptor
  104. * @akcipher_op_done : callback used when operation is done
  105. */
  106. struct caam_rsa_req_ctx {
  107. struct scatterlist src[2];
  108. struct scatterlist *fixup_src;
  109. unsigned int fixup_src_len;
  110. struct rsa_edesc *edesc;
  111. void (*akcipher_op_done)(struct device *jrdev, u32 *desc, u32 err,
  112. void *context);
  113. };
  114. /**
  115. * rsa_edesc - s/w-extended rsa descriptor
  116. * @src_nents : number of segments in input s/w scatterlist
  117. * @dst_nents : number of segments in output s/w scatterlist
  118. * @mapped_src_nents: number of segments in input h/w link table
  119. * @mapped_dst_nents: number of segments in output h/w link table
  120. * @sec4_sg_bytes : length of h/w link table
  121. * @bklog : stored to determine if the request needs backlog
  122. * @sec4_sg_dma : dma address of h/w link table
  123. * @sec4_sg : pointer to h/w link table
  124. * @pdb : specific RSA Protocol Data Block (PDB)
  125. * @hw_desc : descriptor followed by link tables if any
  126. */
  127. struct rsa_edesc {
  128. int src_nents;
  129. int dst_nents;
  130. int mapped_src_nents;
  131. int mapped_dst_nents;
  132. int sec4_sg_bytes;
  133. bool bklog;
  134. dma_addr_t sec4_sg_dma;
  135. struct sec4_sg_entry *sec4_sg;
  136. union {
  137. struct rsa_pub_pdb pub;
  138. struct rsa_priv_f1_pdb priv_f1;
  139. struct rsa_priv_f2_pdb priv_f2;
  140. struct rsa_priv_f3_pdb priv_f3;
  141. } pdb;
  142. u32 hw_desc[];
  143. };
  144. /* Descriptor construction primitives. */
  145. void init_rsa_pub_desc(u32 *desc, struct rsa_pub_pdb *pdb);
  146. void init_rsa_priv_f1_desc(u32 *desc, struct rsa_priv_f1_pdb *pdb);
  147. void init_rsa_priv_f2_desc(u32 *desc, struct rsa_priv_f2_pdb *pdb);
  148. void init_rsa_priv_f3_desc(u32 *desc, struct rsa_priv_f3_pdb *pdb);
  149. #endif