Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
zcrypt_cca_key.h
Go to the documentation of this file.
1 /*
2  * zcrypt 2.1.0
3  *
4  * Copyright IBM Corp. 2001, 2006
5  * Author(s): Robert Burroughs
6  * Eric Rossman ([email protected])
7  *
8  * Hotplug & misc device support: Jochen Roehrig ([email protected])
9  * Major cleanup & driver split: Martin Schwidefsky <[email protected]>
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 
26 #ifndef _ZCRYPT_CCA_KEY_H_
27 #define _ZCRYPT_CCA_KEY_H_
28 
30  unsigned short blen;
31  unsigned short ulen;
32  unsigned short flags;
33 };
34 
42 struct cca_token_hdr {
43  unsigned char token_identifier;
44  unsigned char version;
45  unsigned short token_length;
46  unsigned char reserved[4];
47 } __attribute__((packed));
48 
49 #define CCA_TKN_HDR_ID_EXT 0x1E
50 
55  unsigned char section_identifier;
56  unsigned char version;
57  unsigned short section_length;
58  unsigned char private_key_hash[20];
59  unsigned char reserved1[4];
60  unsigned char key_format;
61  unsigned char reserved2;
62  unsigned char key_name_hash[20];
63  unsigned char key_use_flags[4];
64  unsigned char reserved3[6];
65  unsigned char reserved4[24];
66  unsigned char confounder[24];
67  unsigned char exponent[128];
68  unsigned char modulus[128];
69 } __attribute__((packed));
70 
71 #define CCA_PVT_USAGE_ALL 0x80
72 
80  unsigned char section_identifier;
81  unsigned char version;
82  unsigned short section_length;
83  unsigned char reserved[2];
84  unsigned short exponent_len;
85  unsigned short modulus_bit_len;
86  unsigned short modulus_byte_len; /* In a private key, this is 0 */
87 } __attribute__((packed));
88 
102  unsigned char section_identifier;
103  unsigned char version;
104  unsigned short section_length;
105  unsigned char private_key_hash[20];
106  unsigned char reserved1[4];
107  unsigned char key_format;
108  unsigned char reserved2;
109  unsigned char key_name_hash[20];
110  unsigned char key_use_flags[4];
111  unsigned short p_len;
112  unsigned short q_len;
113  unsigned short dp_len;
114  unsigned short dq_len;
115  unsigned short u_len;
116  unsigned short mod_len;
117  unsigned char reserved3[4];
118  unsigned short pad_len;
119  unsigned char reserved4[52];
120  unsigned char confounder[8];
121 } __attribute__((packed));
122 
123 #define CCA_PVT_EXT_CRT_SEC_ID_PVT 0x08
124 #define CCA_PVT_EXT_CRT_SEC_FMT_CL 0x40
125 
136 static inline int zcrypt_type6_mex_key_de(struct ica_rsa_modexpo *mex,
137  void *p, int big_endian)
138 {
139  static struct cca_token_hdr static_pvt_me_hdr = {
140  .token_identifier = 0x1E,
141  .token_length = 0x0183,
142  };
143  static struct cca_private_ext_ME_sec static_pvt_me_sec = {
144  .section_identifier = 0x02,
145  .section_length = 0x016C,
146  .key_use_flags = {0x80,0x00,0x00,0x00},
147  };
148  static struct cca_public_sec static_pub_me_sec = {
149  .section_identifier = 0x04,
150  .section_length = 0x000F,
151  .exponent_len = 0x0003,
152  };
153  static char pk_exponent[3] = { 0x01, 0x00, 0x01 };
154  struct {
155  struct T6_keyBlock_hdr t6_hdr;
156  struct cca_token_hdr pvtMeHdr;
157  struct cca_private_ext_ME_sec pvtMeSec;
158  struct cca_public_sec pubMeSec;
159  char exponent[3];
160  } __attribute__((packed)) *key = p;
161  unsigned char *temp;
162 
163  memset(key, 0, sizeof(*key));
164 
165  if (big_endian) {
166  key->t6_hdr.blen = cpu_to_be16(0x189);
167  key->t6_hdr.ulen = cpu_to_be16(0x189 - 2);
168  } else {
169  key->t6_hdr.blen = cpu_to_le16(0x189);
170  key->t6_hdr.ulen = cpu_to_le16(0x189 - 2);
171  }
172  key->pvtMeHdr = static_pvt_me_hdr;
173  key->pvtMeSec = static_pvt_me_sec;
174  key->pubMeSec = static_pub_me_sec;
175  /*
176  * In a private key, the modulus doesn't appear in the public
177  * section. So, an arbitrary public exponent of 0x010001 will be
178  * used.
179  */
180  memcpy(key->exponent, pk_exponent, 3);
181 
182  /* key parameter block */
183  temp = key->pvtMeSec.exponent +
184  sizeof(key->pvtMeSec.exponent) - mex->inputdatalength;
185  if (copy_from_user(temp, mex->b_key, mex->inputdatalength))
186  return -EFAULT;
187 
188  /* modulus */
189  temp = key->pvtMeSec.modulus +
190  sizeof(key->pvtMeSec.modulus) - mex->inputdatalength;
191  if (copy_from_user(temp, mex->n_modulus, mex->inputdatalength))
192  return -EFAULT;
193  key->pubMeSec.modulus_bit_len = 8 * mex->inputdatalength;
194  return sizeof(*key);
195 }
196 
208 static inline int zcrypt_type6_mex_key_en(struct ica_rsa_modexpo *mex,
209  void *p, int big_endian)
210 {
211  static struct cca_token_hdr static_pub_hdr = {
212  .token_identifier = 0x1E,
213  };
214  static struct cca_public_sec static_pub_sec = {
215  .section_identifier = 0x04,
216  };
217  struct {
218  struct T6_keyBlock_hdr t6_hdr;
219  struct cca_token_hdr pubHdr;
220  struct cca_public_sec pubSec;
221  char exponent[0];
222  } __attribute__((packed)) *key = p;
223  unsigned char *temp;
224  int i;
225 
226  memset(key, 0, sizeof(*key));
227 
228  key->pubHdr = static_pub_hdr;
229  key->pubSec = static_pub_sec;
230 
231  /* key parameter block */
232  temp = key->exponent;
233  if (copy_from_user(temp, mex->b_key, mex->inputdatalength))
234  return -EFAULT;
235  /* Strip leading zeroes from b_key. */
236  for (i = 0; i < mex->inputdatalength; i++)
237  if (temp[i])
238  break;
239  if (i >= mex->inputdatalength)
240  return -EINVAL;
241  memmove(temp, temp + i, mex->inputdatalength - i);
242  temp += mex->inputdatalength - i;
243  /* modulus */
244  if (copy_from_user(temp, mex->n_modulus, mex->inputdatalength))
245  return -EFAULT;
246 
247  key->pubSec.modulus_bit_len = 8 * mex->inputdatalength;
248  key->pubSec.modulus_byte_len = mex->inputdatalength;
249  key->pubSec.exponent_len = mex->inputdatalength - i;
250  key->pubSec.section_length = sizeof(key->pubSec) +
251  2*mex->inputdatalength - i;
252  key->pubHdr.token_length =
253  key->pubSec.section_length + sizeof(key->pubHdr);
254  if (big_endian) {
255  key->t6_hdr.ulen = cpu_to_be16(key->pubHdr.token_length + 4);
256  key->t6_hdr.blen = cpu_to_be16(key->pubHdr.token_length + 6);
257  } else {
258  key->t6_hdr.ulen = cpu_to_le16(key->pubHdr.token_length + 4);
259  key->t6_hdr.blen = cpu_to_le16(key->pubHdr.token_length + 6);
260  }
261  return sizeof(*key) + 2*mex->inputdatalength - i;
262 }
263 
274 static inline int zcrypt_type6_crt_key(struct ica_rsa_modexpo_crt *crt,
275  void *p, int big_endian)
276 {
277  static struct cca_public_sec static_cca_pub_sec = {
278  .section_identifier = 4,
279  .section_length = 0x000f,
280  .exponent_len = 0x0003,
281  };
282  static char pk_exponent[3] = { 0x01, 0x00, 0x01 };
283  struct {
284  struct T6_keyBlock_hdr t6_hdr;
285  struct cca_token_hdr token;
286  struct cca_pvt_ext_CRT_sec pvt;
287  char key_parts[0];
288  } __attribute__((packed)) *key = p;
289  struct cca_public_sec *pub;
290  int short_len, long_len, pad_len, key_len, size;
291 
292  memset(key, 0, sizeof(*key));
293 
294  short_len = crt->inputdatalength / 2;
295  long_len = short_len + 8;
296  pad_len = -(3*long_len + 2*short_len) & 7;
297  key_len = 3*long_len + 2*short_len + pad_len + crt->inputdatalength;
298  size = sizeof(*key) + key_len + sizeof(*pub) + 3;
299 
300  /* parameter block.key block */
301  if (big_endian) {
302  key->t6_hdr.blen = cpu_to_be16(size);
303  key->t6_hdr.ulen = cpu_to_be16(size - 2);
304  } else {
305  key->t6_hdr.blen = cpu_to_le16(size);
306  key->t6_hdr.ulen = cpu_to_le16(size - 2);
307  }
308 
309  /* key token header */
310  key->token.token_identifier = CCA_TKN_HDR_ID_EXT;
311  key->token.token_length = size - 6;
312 
313  /* private section */
314  key->pvt.section_identifier = CCA_PVT_EXT_CRT_SEC_ID_PVT;
315  key->pvt.section_length = sizeof(key->pvt) + key_len;
316  key->pvt.key_format = CCA_PVT_EXT_CRT_SEC_FMT_CL;
317  key->pvt.key_use_flags[0] = CCA_PVT_USAGE_ALL;
318  key->pvt.p_len = key->pvt.dp_len = key->pvt.u_len = long_len;
319  key->pvt.q_len = key->pvt.dq_len = short_len;
320  key->pvt.mod_len = crt->inputdatalength;
321  key->pvt.pad_len = pad_len;
322 
323  /* key parts */
324  if (copy_from_user(key->key_parts, crt->np_prime, long_len) ||
325  copy_from_user(key->key_parts + long_len,
326  crt->nq_prime, short_len) ||
327  copy_from_user(key->key_parts + long_len + short_len,
328  crt->bp_key, long_len) ||
329  copy_from_user(key->key_parts + 2*long_len + short_len,
330  crt->bq_key, short_len) ||
331  copy_from_user(key->key_parts + 2*long_len + 2*short_len,
332  crt->u_mult_inv, long_len))
333  return -EFAULT;
334  memset(key->key_parts + 3*long_len + 2*short_len + pad_len,
335  0xff, crt->inputdatalength);
336  pub = (struct cca_public_sec *)(key->key_parts + key_len);
337  *pub = static_cca_pub_sec;
338  pub->modulus_bit_len = 8 * crt->inputdatalength;
339  /*
340  * In a private key, the modulus doesn't appear in the public
341  * section. So, an arbitrary public exponent of 0x010001 will be
342  * used.
343  */
344  memcpy((char *) (pub + 1), pk_exponent, 3);
345  return size;
346 }
347 
348 #endif /* _ZCRYPT_CCA_KEY_H_ */