zcrypt_cca_key.h 11 KB

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