Merge branch 'r6040-next'
[linux/fpc-iii.git] / drivers / s390 / crypto / zcrypt_cca_key.h
blobca0cdbe463686bc52559e2ac948863df219340e8
1 /*
2 * zcrypt 2.1.0
4 * Copyright IBM Corp. 2001, 2006
5 * Author(s): Robert Burroughs
6 * Eric Rossman (edrossma@us.ibm.com)
8 * Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
9 * Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
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.
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.
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.
26 #ifndef _ZCRYPT_CCA_KEY_H_
27 #define _ZCRYPT_CCA_KEY_H_
29 struct T6_keyBlock_hdr {
30 unsigned short blen;
31 unsigned short ulen;
32 unsigned short flags;
35 /**
36 * mapping for the cca private ME key token.
37 * Three parts of interest here: the header, the private section and
38 * the public section.
40 * mapping for the cca key token header
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));
49 #define CCA_TKN_HDR_ID_EXT 0x1E
51 /**
52 * mapping for the cca private ME section
54 struct cca_private_ext_ME_sec {
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));
71 #define CCA_PVT_USAGE_ALL 0x80
73 /**
74 * mapping for the cca public section
75 * In a private key, the modulus doesn't appear in the public
76 * section. So, an arbitrary public exponent of 0x010001 will be
77 * used, for a section length of 0x0F always.
79 struct cca_public_sec {
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));
89 /**
90 * mapping for the cca private CRT key 'token'
91 * The first three parts (the only parts considered in this release)
92 * are: the header, the private section and the public section.
93 * The header and public section are the same as for the
94 * struct cca_private_ext_ME
96 * Following the structure are the quantities p, q, dp, dq, u, pad,
97 * and modulus, in that order, where pad_len is the modulo 8
98 * complement of the residue modulo 8 of the sum of
99 * (p_len + q_len + dp_len + dq_len + u_len).
101 struct cca_pvt_ext_CRT_sec {
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));
123 #define CCA_PVT_EXT_CRT_SEC_ID_PVT 0x08
124 #define CCA_PVT_EXT_CRT_SEC_FMT_CL 0x40
127 * Set up private key fields of a type6 MEX message.
128 * Note that all numerics in the key token are big-endian,
129 * while the entries in the key block header are little-endian.
131 * @mex: pointer to user input data
132 * @p: pointer to memory area for the key
134 * Returns the size of the key area or -EFAULT
136 static inline int zcrypt_type6_mex_key_de(struct ica_rsa_modexpo *mex,
137 void *p, int big_endian)
139 static struct cca_token_hdr static_pvt_me_hdr = {
140 .token_identifier = 0x1E,
141 .token_length = 0x0183,
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},
148 static struct cca_public_sec static_pub_me_sec = {
149 .section_identifier = 0x04,
150 .section_length = 0x000F,
151 .exponent_len = 0x0003,
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;
163 memset(key, 0, sizeof(*key));
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);
172 key->pvtMeHdr = static_pvt_me_hdr;
173 key->pvtMeSec = static_pvt_me_sec;
174 key->pubMeSec = static_pub_me_sec;
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.
180 memcpy(key->exponent, pk_exponent, 3);
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;
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);
198 * Set up private key fields of a type6 MEX message. The _pad variant
199 * strips leading zeroes from the b_key.
200 * Note that all numerics in the key token are big-endian,
201 * while the entries in the key block header are little-endian.
203 * @mex: pointer to user input data
204 * @p: pointer to memory area for the key
206 * Returns the size of the key area or -EFAULT
208 static inline int zcrypt_type6_mex_key_en(struct ica_rsa_modexpo *mex,
209 void *p, int big_endian)
211 static struct cca_token_hdr static_pub_hdr = {
212 .token_identifier = 0x1E,
214 static struct cca_public_sec static_pub_sec = {
215 .section_identifier = 0x04,
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;
226 memset(key, 0, sizeof(*key));
228 key->pubHdr = static_pub_hdr;
229 key->pubSec = static_pub_sec;
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;
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);
261 return sizeof(*key) + 2*mex->inputdatalength - i;
265 * Set up private key fields of a type6 CRT message.
266 * Note that all numerics in the key token are big-endian,
267 * while the entries in the key block header are little-endian.
269 * @mex: pointer to user input data
270 * @p: pointer to memory area for the key
272 * Returns the size of the key area or -EFAULT
274 static inline int zcrypt_type6_crt_key(struct ica_rsa_modexpo_crt *crt,
275 void *p, int big_endian)
277 static struct cca_public_sec static_cca_pub_sec = {
278 .section_identifier = 4,
279 .section_length = 0x000f,
280 .exponent_len = 0x0003,
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;
292 memset(key, 0, sizeof(*key));
294 short_len = (crt->inputdatalength + 1) / 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;
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);
309 /* key token header */
310 key->token.token_identifier = CCA_TKN_HDR_ID_EXT;
311 key->token.token_length = size - 6;
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;
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;
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.
344 memcpy((char *) (pub + 1), pk_exponent, 3);
345 return size;
348 #endif /* _ZCRYPT_CCA_KEY_H_ */