zcrypt_cca_key.h 7.5 KB

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