zcrypt_cca_key.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright IBM Corp. 2001, 2006
  4. * Author(s): Robert Burroughs
  5. * Eric Rossman (edrossma@us.ibm.com)
  6. *
  7. * Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
  8. * Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
  9. */
  10. #ifndef _ZCRYPT_CCA_KEY_H_
  11. #define _ZCRYPT_CCA_KEY_H_
  12. struct t6_keyblock_hdr {
  13. unsigned short blen;
  14. unsigned short ulen;
  15. unsigned short flags;
  16. };
  17. /**
  18. * mapping for the cca private ME key token.
  19. * Three parts of interest here: the header, the private section and
  20. * the public section.
  21. *
  22. * mapping for the cca key token header
  23. */
  24. struct cca_token_hdr {
  25. unsigned char token_identifier;
  26. unsigned char version;
  27. unsigned short token_length;
  28. unsigned char reserved[4];
  29. } __packed;
  30. #define CCA_TKN_HDR_ID_EXT 0x1E
  31. #define CCA_PVT_USAGE_ALL 0x80
  32. /**
  33. * mapping for the cca public section
  34. * In a private key, the modulus doesn't appear in the public
  35. * section. So, an arbitrary public exponent of 0x010001 will be
  36. * used, for a section length of 0x0F always.
  37. */
  38. struct cca_public_sec {
  39. unsigned char section_identifier;
  40. unsigned char version;
  41. unsigned short section_length;
  42. unsigned char reserved[2];
  43. unsigned short exponent_len;
  44. unsigned short modulus_bit_len;
  45. unsigned short modulus_byte_len; /* In a private key, this is 0 */
  46. } __packed;
  47. /**
  48. * mapping for the cca private CRT key 'token'
  49. * The first three parts (the only parts considered in this release)
  50. * are: the header, the private section and the public section.
  51. * The header and public section are the same as for the
  52. * struct cca_private_ext_ME
  53. *
  54. * Following the structure are the quantities p, q, dp, dq, u, pad,
  55. * and modulus, in that order, where pad_len is the modulo 8
  56. * complement of the residue modulo 8 of the sum of
  57. * (p_len + q_len + dp_len + dq_len + u_len).
  58. */
  59. struct cca_pvt_ext_crt_sec {
  60. unsigned char section_identifier;
  61. unsigned char version;
  62. unsigned short section_length;
  63. unsigned char private_key_hash[20];
  64. unsigned char reserved1[4];
  65. unsigned char key_format;
  66. unsigned char reserved2;
  67. unsigned char key_name_hash[20];
  68. unsigned char key_use_flags[4];
  69. unsigned short p_len;
  70. unsigned short q_len;
  71. unsigned short dp_len;
  72. unsigned short dq_len;
  73. unsigned short u_len;
  74. unsigned short mod_len;
  75. unsigned char reserved3[4];
  76. unsigned short pad_len;
  77. unsigned char reserved4[52];
  78. unsigned char confounder[8];
  79. } __packed;
  80. #define CCA_PVT_EXT_CRT_SEC_ID_PVT 0x08
  81. #define CCA_PVT_EXT_CRT_SEC_FMT_CL 0x40
  82. /**
  83. * Set up private key fields of a type6 MEX message.
  84. *
  85. * @mex: pointer to user input data
  86. * @p: pointer to memory area for the key
  87. *
  88. * Returns the size of the key area or negative errno value.
  89. */
  90. static inline int zcrypt_type6_mex_key_en(struct ica_rsa_modexpo *mex, void *p)
  91. {
  92. static struct cca_token_hdr static_pub_hdr = {
  93. .token_identifier = 0x1E,
  94. };
  95. static struct cca_public_sec static_pub_sec = {
  96. .section_identifier = 0x04,
  97. };
  98. struct {
  99. struct t6_keyblock_hdr t6_hdr;
  100. struct cca_token_hdr pubhdr;
  101. struct cca_public_sec pubsec;
  102. char exponent[];
  103. } __packed *key = p;
  104. unsigned char *ptr;
  105. /*
  106. * The inputdatalength was a selection criteria in the dispatching
  107. * function zcrypt_rsa_modexpo(). However, do a plausibility check
  108. * here to make sure the following copy_from_user() can't be utilized
  109. * to compromise the system.
  110. */
  111. if (WARN_ON_ONCE(mex->inputdatalength > 512))
  112. return -EINVAL;
  113. memset(key, 0, sizeof(*key));
  114. key->pubhdr = static_pub_hdr;
  115. key->pubsec = static_pub_sec;
  116. /* key parameter block */
  117. ptr = key->exponent;
  118. if (copy_from_user(ptr, mex->b_key, mex->inputdatalength))
  119. return -EFAULT;
  120. ptr += mex->inputdatalength;
  121. /* modulus */
  122. if (copy_from_user(ptr, mex->n_modulus, mex->inputdatalength))
  123. return -EFAULT;
  124. key->pubsec.modulus_bit_len = 8 * mex->inputdatalength;
  125. key->pubsec.modulus_byte_len = mex->inputdatalength;
  126. key->pubsec.exponent_len = mex->inputdatalength;
  127. key->pubsec.section_length = sizeof(key->pubsec) +
  128. 2 * mex->inputdatalength;
  129. key->pubhdr.token_length =
  130. key->pubsec.section_length + sizeof(key->pubhdr);
  131. key->t6_hdr.ulen = key->pubhdr.token_length + 4;
  132. key->t6_hdr.blen = key->pubhdr.token_length + 6;
  133. return sizeof(*key) + 2 * mex->inputdatalength;
  134. }
  135. /**
  136. * Set up private key fields of a type6 CRT message.
  137. *
  138. * @mex: pointer to user input data
  139. * @p: pointer to memory area for the key
  140. *
  141. * Returns the size of the key area or -EFAULT
  142. */
  143. static inline int zcrypt_type6_crt_key(struct ica_rsa_modexpo_crt *crt, void *p)
  144. {
  145. static struct cca_public_sec static_cca_pub_sec = {
  146. .section_identifier = 4,
  147. .section_length = 0x000f,
  148. .exponent_len = 0x0003,
  149. };
  150. static char pk_exponent[3] = { 0x01, 0x00, 0x01 };
  151. struct {
  152. struct t6_keyblock_hdr t6_hdr;
  153. struct cca_token_hdr token;
  154. struct cca_pvt_ext_crt_sec pvt;
  155. char key_parts[];
  156. } __packed *key = p;
  157. struct cca_public_sec *pub;
  158. int short_len, long_len, pad_len, key_len, size;
  159. /*
  160. * The inputdatalength was a selection criteria in the dispatching
  161. * function zcrypt_rsa_crt(). However, do a plausibility check
  162. * here to make sure the following copy_from_user() can't be utilized
  163. * to compromise the system.
  164. */
  165. if (WARN_ON_ONCE(crt->inputdatalength > 512))
  166. return -EINVAL;
  167. memset(key, 0, sizeof(*key));
  168. short_len = (crt->inputdatalength + 1) / 2;
  169. long_len = short_len + 8;
  170. pad_len = -(3 * long_len + 2 * short_len) & 7;
  171. key_len = 3 * long_len + 2 * short_len + pad_len + crt->inputdatalength;
  172. size = sizeof(*key) + key_len + sizeof(*pub) + 3;
  173. /* parameter block.key block */
  174. key->t6_hdr.blen = size;
  175. key->t6_hdr.ulen = size - 2;
  176. /* key token header */
  177. key->token.token_identifier = CCA_TKN_HDR_ID_EXT;
  178. key->token.token_length = size - 6;
  179. /* private section */
  180. key->pvt.section_identifier = CCA_PVT_EXT_CRT_SEC_ID_PVT;
  181. key->pvt.section_length = sizeof(key->pvt) + key_len;
  182. key->pvt.key_format = CCA_PVT_EXT_CRT_SEC_FMT_CL;
  183. key->pvt.key_use_flags[0] = CCA_PVT_USAGE_ALL;
  184. key->pvt.p_len = key->pvt.dp_len = key->pvt.u_len = long_len;
  185. key->pvt.q_len = key->pvt.dq_len = short_len;
  186. key->pvt.mod_len = crt->inputdatalength;
  187. key->pvt.pad_len = pad_len;
  188. /* key parts */
  189. if (copy_from_user(key->key_parts, crt->np_prime, long_len) ||
  190. copy_from_user(key->key_parts + long_len,
  191. crt->nq_prime, short_len) ||
  192. copy_from_user(key->key_parts + long_len + short_len,
  193. crt->bp_key, long_len) ||
  194. copy_from_user(key->key_parts + 2 * long_len + short_len,
  195. crt->bq_key, short_len) ||
  196. copy_from_user(key->key_parts + 2 * long_len + 2 * short_len,
  197. crt->u_mult_inv, long_len))
  198. return -EFAULT;
  199. memset(key->key_parts + 3 * long_len + 2 * short_len + pad_len,
  200. 0xff, crt->inputdatalength);
  201. pub = (struct cca_public_sec *)(key->key_parts + key_len);
  202. *pub = static_cca_pub_sec;
  203. pub->modulus_bit_len = 8 * crt->inputdatalength;
  204. /*
  205. * In a private key, the modulus doesn't appear in the public
  206. * section. So, an arbitrary public exponent of 0x010001 will be
  207. * used.
  208. */
  209. memcpy((char *)(pub + 1), pk_exponent, 3);
  210. return size;
  211. }
  212. #endif /* _ZCRYPT_CCA_KEY_H_ */