zcrypt_cca_key.h 7.6 KB

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