blob: 771cbb91c11e78bdaaa61e33d6c7ed028aa6cf63 [file] [log] [blame]
Martin Schwidefsky6684af12006-09-20 15:58:32 +02001/*
Ralph Wuerthner54321142006-09-20 15:58:36 +02002 * zcrypt 2.1.0
Martin Schwidefsky6684af12006-09-20 15:58:32 +02003 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02004 * Copyright IBM Corp. 2001, 2006
Martin Schwidefsky6684af12006-09-20 15:58:32 +02005 * 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
26#ifndef _ZCRYPT_CCA_KEY_H_
27#define _ZCRYPT_CCA_KEY_H_
28
29struct T6_keyBlock_hdr {
30 unsigned short blen;
31 unsigned short ulen;
32 unsigned short flags;
33};
34
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.
39 *
40 * mapping for the cca key token header
41 */
42struct 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
51/**
52 * mapping for the cca private ME section
53 */
54struct 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));
70
71#define CCA_PVT_USAGE_ALL 0x80
72
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.
78 */
79struct 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));
88
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
95 *
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).
100 */
101struct 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));
122
123#define CCA_PVT_EXT_CRT_SEC_ID_PVT 0x08
124#define CCA_PVT_EXT_CRT_SEC_FMT_CL 0x40
125
126/**
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.
130 *
131 * @mex: pointer to user input data
132 * @p: pointer to memory area for the key
133 *
134 * Returns the size of the key area or -EFAULT
135 */
Heiko Carstens8ff34582017-05-09 12:19:16 +0200136static inline int zcrypt_type6_mex_key_de(struct ica_rsa_modexpo *mex, void *p)
Martin Schwidefsky6684af12006-09-20 15:58:32 +0200137{
138 static struct cca_token_hdr static_pvt_me_hdr = {
139 .token_identifier = 0x1E,
140 .token_length = 0x0183,
141 };
142 static struct cca_private_ext_ME_sec static_pvt_me_sec = {
143 .section_identifier = 0x02,
144 .section_length = 0x016C,
145 .key_use_flags = {0x80,0x00,0x00,0x00},
146 };
147 static struct cca_public_sec static_pub_me_sec = {
148 .section_identifier = 0x04,
149 .section_length = 0x000F,
150 .exponent_len = 0x0003,
151 };
152 static char pk_exponent[3] = { 0x01, 0x00, 0x01 };
153 struct {
154 struct T6_keyBlock_hdr t6_hdr;
155 struct cca_token_hdr pvtMeHdr;
156 struct cca_private_ext_ME_sec pvtMeSec;
157 struct cca_public_sec pubMeSec;
158 char exponent[3];
159 } __attribute__((packed)) *key = p;
160 unsigned char *temp;
161
162 memset(key, 0, sizeof(*key));
163
Heiko Carstens8ff34582017-05-09 12:19:16 +0200164 key->t6_hdr.blen = 0x189;
165 key->t6_hdr.ulen = 0x189 - 2;
Martin Schwidefsky6684af12006-09-20 15:58:32 +0200166 key->pvtMeHdr = static_pvt_me_hdr;
167 key->pvtMeSec = static_pvt_me_sec;
168 key->pubMeSec = static_pub_me_sec;
Felix Beck1749a812008-04-17 07:46:28 +0200169 /*
Martin Schwidefsky6684af12006-09-20 15:58:32 +0200170 * In a private key, the modulus doesn't appear in the public
171 * section. So, an arbitrary public exponent of 0x010001 will be
172 * used.
173 */
174 memcpy(key->exponent, pk_exponent, 3);
175
176 /* key parameter block */
177 temp = key->pvtMeSec.exponent +
178 sizeof(key->pvtMeSec.exponent) - mex->inputdatalength;
179 if (copy_from_user(temp, mex->b_key, mex->inputdatalength))
180 return -EFAULT;
181
182 /* modulus */
183 temp = key->pvtMeSec.modulus +
184 sizeof(key->pvtMeSec.modulus) - mex->inputdatalength;
185 if (copy_from_user(temp, mex->n_modulus, mex->inputdatalength))
186 return -EFAULT;
187 key->pubMeSec.modulus_bit_len = 8 * mex->inputdatalength;
188 return sizeof(*key);
189}
190
191/**
192 * Set up private key fields of a type6 MEX message. The _pad variant
193 * strips leading zeroes from the b_key.
194 * Note that all numerics in the key token are big-endian,
195 * while the entries in the key block header are little-endian.
196 *
197 * @mex: pointer to user input data
198 * @p: pointer to memory area for the key
199 *
200 * Returns the size of the key area or -EFAULT
201 */
Heiko Carstens8ff34582017-05-09 12:19:16 +0200202static inline int zcrypt_type6_mex_key_en(struct ica_rsa_modexpo *mex, void *p)
Martin Schwidefsky6684af12006-09-20 15:58:32 +0200203{
204 static struct cca_token_hdr static_pub_hdr = {
205 .token_identifier = 0x1E,
206 };
207 static struct cca_public_sec static_pub_sec = {
208 .section_identifier = 0x04,
209 };
210 struct {
211 struct T6_keyBlock_hdr t6_hdr;
212 struct cca_token_hdr pubHdr;
213 struct cca_public_sec pubSec;
214 char exponent[0];
215 } __attribute__((packed)) *key = p;
216 unsigned char *temp;
217 int i;
218
219 memset(key, 0, sizeof(*key));
220
221 key->pubHdr = static_pub_hdr;
222 key->pubSec = static_pub_sec;
223
224 /* key parameter block */
225 temp = key->exponent;
226 if (copy_from_user(temp, mex->b_key, mex->inputdatalength))
227 return -EFAULT;
228 /* Strip leading zeroes from b_key. */
229 for (i = 0; i < mex->inputdatalength; i++)
230 if (temp[i])
231 break;
232 if (i >= mex->inputdatalength)
233 return -EINVAL;
234 memmove(temp, temp + i, mex->inputdatalength - i);
235 temp += mex->inputdatalength - i;
236 /* modulus */
237 if (copy_from_user(temp, mex->n_modulus, mex->inputdatalength))
238 return -EFAULT;
239
240 key->pubSec.modulus_bit_len = 8 * mex->inputdatalength;
241 key->pubSec.modulus_byte_len = mex->inputdatalength;
242 key->pubSec.exponent_len = mex->inputdatalength - i;
243 key->pubSec.section_length = sizeof(key->pubSec) +
244 2*mex->inputdatalength - i;
245 key->pubHdr.token_length =
246 key->pubSec.section_length + sizeof(key->pubHdr);
Heiko Carstens8ff34582017-05-09 12:19:16 +0200247 key->t6_hdr.ulen = key->pubHdr.token_length + 4;
248 key->t6_hdr.blen = key->pubHdr.token_length + 6;
Martin Schwidefsky6684af12006-09-20 15:58:32 +0200249 return sizeof(*key) + 2*mex->inputdatalength - i;
250}
251
252/**
253 * Set up private key fields of a type6 CRT message.
254 * Note that all numerics in the key token are big-endian,
255 * while the entries in the key block header are little-endian.
256 *
257 * @mex: pointer to user input data
258 * @p: pointer to memory area for the key
259 *
260 * Returns the size of the key area or -EFAULT
261 */
Heiko Carstens8ff34582017-05-09 12:19:16 +0200262static inline int zcrypt_type6_crt_key(struct ica_rsa_modexpo_crt *crt, void *p)
Martin Schwidefsky6684af12006-09-20 15:58:32 +0200263{
264 static struct cca_public_sec static_cca_pub_sec = {
265 .section_identifier = 4,
266 .section_length = 0x000f,
267 .exponent_len = 0x0003,
268 };
269 static char pk_exponent[3] = { 0x01, 0x00, 0x01 };
270 struct {
271 struct T6_keyBlock_hdr t6_hdr;
272 struct cca_token_hdr token;
273 struct cca_pvt_ext_CRT_sec pvt;
274 char key_parts[0];
275 } __attribute__((packed)) *key = p;
276 struct cca_public_sec *pub;
277 int short_len, long_len, pad_len, key_len, size;
278
279 memset(key, 0, sizeof(*key));
280
Ingo Tuchscherer1330a122015-09-04 09:57:24 +0200281 short_len = (crt->inputdatalength + 1) / 2;
Martin Schwidefsky6684af12006-09-20 15:58:32 +0200282 long_len = short_len + 8;
283 pad_len = -(3*long_len + 2*short_len) & 7;
284 key_len = 3*long_len + 2*short_len + pad_len + crt->inputdatalength;
285 size = sizeof(*key) + key_len + sizeof(*pub) + 3;
286
287 /* parameter block.key block */
Heiko Carstens8ff34582017-05-09 12:19:16 +0200288 key->t6_hdr.blen = size;
289 key->t6_hdr.ulen = size - 2;
Martin Schwidefsky6684af12006-09-20 15:58:32 +0200290
291 /* key token header */
292 key->token.token_identifier = CCA_TKN_HDR_ID_EXT;
293 key->token.token_length = size - 6;
294
295 /* private section */
296 key->pvt.section_identifier = CCA_PVT_EXT_CRT_SEC_ID_PVT;
297 key->pvt.section_length = sizeof(key->pvt) + key_len;
298 key->pvt.key_format = CCA_PVT_EXT_CRT_SEC_FMT_CL;
299 key->pvt.key_use_flags[0] = CCA_PVT_USAGE_ALL;
300 key->pvt.p_len = key->pvt.dp_len = key->pvt.u_len = long_len;
301 key->pvt.q_len = key->pvt.dq_len = short_len;
302 key->pvt.mod_len = crt->inputdatalength;
303 key->pvt.pad_len = pad_len;
304
305 /* key parts */
306 if (copy_from_user(key->key_parts, crt->np_prime, long_len) ||
307 copy_from_user(key->key_parts + long_len,
308 crt->nq_prime, short_len) ||
309 copy_from_user(key->key_parts + long_len + short_len,
310 crt->bp_key, long_len) ||
311 copy_from_user(key->key_parts + 2*long_len + short_len,
312 crt->bq_key, short_len) ||
313 copy_from_user(key->key_parts + 2*long_len + 2*short_len,
314 crt->u_mult_inv, long_len))
315 return -EFAULT;
316 memset(key->key_parts + 3*long_len + 2*short_len + pad_len,
317 0xff, crt->inputdatalength);
318 pub = (struct cca_public_sec *)(key->key_parts + key_len);
319 *pub = static_cca_pub_sec;
320 pub->modulus_bit_len = 8 * crt->inputdatalength;
Felix Beck1749a812008-04-17 07:46:28 +0200321 /*
Martin Schwidefsky6684af12006-09-20 15:58:32 +0200322 * In a private key, the modulus doesn't appear in the public
323 * section. So, an arbitrary public exponent of 0x010001 will be
324 * used.
325 */
326 memcpy((char *) (pub + 1), pk_exponent, 3);
327 return size;
328}
329
330#endif /* _ZCRYPT_CCA_KEY_H_ */