zcrypt_cca_key.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. The _pad variant
  84. * strips leading zeroes from the b_key.
  85. * Note that all numerics in the key token are big-endian,
  86. * while the entries in the key block header are little-endian.
  87. *
  88. * @mex: pointer to user input data
  89. * @p: pointer to memory area for the key
  90. *
  91. * Returns the size of the key area or negative errno value.
  92. */
  93. static inline int zcrypt_type6_mex_key_en(struct ica_rsa_modexpo *mex, void *p)
  94. {
  95. static struct cca_token_hdr static_pub_hdr = {
  96. .token_identifier = 0x1E,
  97. };
  98. static struct cca_public_sec static_pub_sec = {
  99. .section_identifier = 0x04,
  100. };
  101. struct {
  102. struct T6_keyBlock_hdr t6_hdr;
  103. struct cca_token_hdr pubHdr;
  104. struct cca_public_sec pubSec;
  105. char exponent[0];
  106. } __packed *key = p;
  107. unsigned char *temp;
  108. int i;
  109. /*
  110. * The inputdatalength was a selection criteria in the dispatching
  111. * function zcrypt_rsa_modexpo(). However, do a plausibility check
  112. * here to make sure the following copy_from_user() can't be utilized
  113. * to compromise the system.
  114. */
  115. if (WARN_ON_ONCE(mex->inputdatalength > 512))
  116. return -EINVAL;
  117. memset(key, 0, sizeof(*key));
  118. key->pubHdr = static_pub_hdr;
  119. key->pubSec = static_pub_sec;
  120. /* key parameter block */
  121. temp = key->exponent;
  122. if (copy_from_user(temp, mex->b_key, mex->inputdatalength))
  123. return -EFAULT;
  124. /* Strip leading zeroes from b_key. */
  125. for (i = 0; i < mex->inputdatalength; i++)
  126. if (temp[i])
  127. break;
  128. if (i >= mex->inputdatalength)
  129. return -EINVAL;
  130. memmove(temp, temp + i, mex->inputdatalength - i);
  131. temp += mex->inputdatalength - i;
  132. /* modulus */
  133. if (copy_from_user(temp, mex->n_modulus, mex->inputdatalength))
  134. return -EFAULT;
  135. key->pubSec.modulus_bit_len = 8 * mex->inputdatalength;
  136. key->pubSec.modulus_byte_len = mex->inputdatalength;
  137. key->pubSec.exponent_len = mex->inputdatalength - i;
  138. key->pubSec.section_length = sizeof(key->pubSec) +
  139. 2*mex->inputdatalength - i;
  140. key->pubHdr.token_length =
  141. key->pubSec.section_length + sizeof(key->pubHdr);
  142. key->t6_hdr.ulen = key->pubHdr.token_length + 4;
  143. key->t6_hdr.blen = key->pubHdr.token_length + 6;
  144. return sizeof(*key) + 2*mex->inputdatalength - i;
  145. }
  146. /**
  147. * Set up private key fields of a type6 CRT message.
  148. * Note that all numerics in the key token are big-endian,
  149. * while the entries in the key block header are little-endian.
  150. *
  151. * @mex: pointer to user input data
  152. * @p: pointer to memory area for the key
  153. *
  154. * Returns the size of the key area or -EFAULT
  155. */
  156. static inline int zcrypt_type6_crt_key(struct ica_rsa_modexpo_crt *crt, void *p)
  157. {
  158. static struct cca_public_sec static_cca_pub_sec = {
  159. .section_identifier = 4,
  160. .section_length = 0x000f,
  161. .exponent_len = 0x0003,
  162. };
  163. static char pk_exponent[3] = { 0x01, 0x00, 0x01 };
  164. struct {
  165. struct T6_keyBlock_hdr t6_hdr;
  166. struct cca_token_hdr token;
  167. struct cca_pvt_ext_CRT_sec pvt;
  168. char key_parts[0];
  169. } __packed *key = p;
  170. struct cca_public_sec *pub;
  171. int short_len, long_len, pad_len, key_len, size;
  172. /*
  173. * The inputdatalength was a selection criteria in the dispatching
  174. * function zcrypt_rsa_crt(). However, do a plausibility check
  175. * here to make sure the following copy_from_user() can't be utilized
  176. * to compromise the system.
  177. */
  178. if (WARN_ON_ONCE(crt->inputdatalength > 512))
  179. return -EINVAL;
  180. memset(key, 0, sizeof(*key));
  181. short_len = (crt->inputdatalength + 1) / 2;
  182. long_len = short_len + 8;
  183. pad_len = -(3*long_len + 2*short_len) & 7;
  184. key_len = 3*long_len + 2*short_len + pad_len + crt->inputdatalength;
  185. size = sizeof(*key) + key_len + sizeof(*pub) + 3;
  186. /* parameter block.key block */
  187. key->t6_hdr.blen = size;
  188. key->t6_hdr.ulen = size - 2;
  189. /* key token header */
  190. key->token.token_identifier = CCA_TKN_HDR_ID_EXT;
  191. key->token.token_length = size - 6;
  192. /* private section */
  193. key->pvt.section_identifier = CCA_PVT_EXT_CRT_SEC_ID_PVT;
  194. key->pvt.section_length = sizeof(key->pvt) + key_len;
  195. key->pvt.key_format = CCA_PVT_EXT_CRT_SEC_FMT_CL;
  196. key->pvt.key_use_flags[0] = CCA_PVT_USAGE_ALL;
  197. key->pvt.p_len = key->pvt.dp_len = key->pvt.u_len = long_len;
  198. key->pvt.q_len = key->pvt.dq_len = short_len;
  199. key->pvt.mod_len = crt->inputdatalength;
  200. key->pvt.pad_len = pad_len;
  201. /* key parts */
  202. if (copy_from_user(key->key_parts, crt->np_prime, long_len) ||
  203. copy_from_user(key->key_parts + long_len,
  204. crt->nq_prime, short_len) ||
  205. copy_from_user(key->key_parts + long_len + short_len,
  206. crt->bp_key, long_len) ||
  207. copy_from_user(key->key_parts + 2*long_len + short_len,
  208. crt->bq_key, short_len) ||
  209. copy_from_user(key->key_parts + 2*long_len + 2*short_len,
  210. crt->u_mult_inv, long_len))
  211. return -EFAULT;
  212. memset(key->key_parts + 3*long_len + 2*short_len + pad_len,
  213. 0xff, crt->inputdatalength);
  214. pub = (struct cca_public_sec *)(key->key_parts + key_len);
  215. *pub = static_cca_pub_sec;
  216. pub->modulus_bit_len = 8 * crt->inputdatalength;
  217. /*
  218. * In a private key, the modulus doesn't appear in the public
  219. * section. So, an arbitrary public exponent of 0x010001 will be
  220. * used.
  221. */
  222. memcpy((char *) (pub + 1), pk_exponent, 3);
  223. return size;
  224. }
  225. #endif /* _ZCRYPT_CCA_KEY_H_ */