blob: 12cff6262566b5f4c1960b5497e37da7025ad7ad [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
Martin Schwidefsky6684af12006-09-20 15:58:32 +020051#define CCA_PVT_USAGE_ALL 0x80
52
53/**
54 * mapping for the cca public section
55 * In a private key, the modulus doesn't appear in the public
56 * section. So, an arbitrary public exponent of 0x010001 will be
57 * used, for a section length of 0x0F always.
58 */
59struct cca_public_sec {
60 unsigned char section_identifier;
61 unsigned char version;
62 unsigned short section_length;
63 unsigned char reserved[2];
64 unsigned short exponent_len;
65 unsigned short modulus_bit_len;
66 unsigned short modulus_byte_len; /* In a private key, this is 0 */
67} __attribute__((packed));
68
69/**
70 * mapping for the cca private CRT key 'token'
71 * The first three parts (the only parts considered in this release)
72 * are: the header, the private section and the public section.
73 * The header and public section are the same as for the
74 * struct cca_private_ext_ME
75 *
76 * Following the structure are the quantities p, q, dp, dq, u, pad,
77 * and modulus, in that order, where pad_len is the modulo 8
78 * complement of the residue modulo 8 of the sum of
79 * (p_len + q_len + dp_len + dq_len + u_len).
80 */
81struct cca_pvt_ext_CRT_sec {
82 unsigned char section_identifier;
83 unsigned char version;
84 unsigned short section_length;
85 unsigned char private_key_hash[20];
86 unsigned char reserved1[4];
87 unsigned char key_format;
88 unsigned char reserved2;
89 unsigned char key_name_hash[20];
90 unsigned char key_use_flags[4];
91 unsigned short p_len;
92 unsigned short q_len;
93 unsigned short dp_len;
94 unsigned short dq_len;
95 unsigned short u_len;
96 unsigned short mod_len;
97 unsigned char reserved3[4];
98 unsigned short pad_len;
99 unsigned char reserved4[52];
100 unsigned char confounder[8];
101} __attribute__((packed));
102
103#define CCA_PVT_EXT_CRT_SEC_ID_PVT 0x08
104#define CCA_PVT_EXT_CRT_SEC_FMT_CL 0x40
105
106/**
Martin Schwidefsky6684af12006-09-20 15:58:32 +0200107 * Set up private key fields of a type6 MEX message. The _pad variant
108 * strips leading zeroes from the b_key.
109 * Note that all numerics in the key token are big-endian,
110 * while the entries in the key block header are little-endian.
111 *
112 * @mex: pointer to user input data
113 * @p: pointer to memory area for the key
114 *
115 * Returns the size of the key area or -EFAULT
116 */
Heiko Carstens8ff34582017-05-09 12:19:16 +0200117static inline int zcrypt_type6_mex_key_en(struct ica_rsa_modexpo *mex, void *p)
Martin Schwidefsky6684af12006-09-20 15:58:32 +0200118{
119 static struct cca_token_hdr static_pub_hdr = {
120 .token_identifier = 0x1E,
121 };
122 static struct cca_public_sec static_pub_sec = {
123 .section_identifier = 0x04,
124 };
125 struct {
126 struct T6_keyBlock_hdr t6_hdr;
127 struct cca_token_hdr pubHdr;
128 struct cca_public_sec pubSec;
129 char exponent[0];
130 } __attribute__((packed)) *key = p;
131 unsigned char *temp;
132 int i;
133
134 memset(key, 0, sizeof(*key));
135
136 key->pubHdr = static_pub_hdr;
137 key->pubSec = static_pub_sec;
138
139 /* key parameter block */
140 temp = key->exponent;
141 if (copy_from_user(temp, mex->b_key, mex->inputdatalength))
142 return -EFAULT;
143 /* Strip leading zeroes from b_key. */
144 for (i = 0; i < mex->inputdatalength; i++)
145 if (temp[i])
146 break;
147 if (i >= mex->inputdatalength)
148 return -EINVAL;
149 memmove(temp, temp + i, mex->inputdatalength - i);
150 temp += mex->inputdatalength - i;
151 /* modulus */
152 if (copy_from_user(temp, mex->n_modulus, mex->inputdatalength))
153 return -EFAULT;
154
155 key->pubSec.modulus_bit_len = 8 * mex->inputdatalength;
156 key->pubSec.modulus_byte_len = mex->inputdatalength;
157 key->pubSec.exponent_len = mex->inputdatalength - i;
158 key->pubSec.section_length = sizeof(key->pubSec) +
159 2*mex->inputdatalength - i;
160 key->pubHdr.token_length =
161 key->pubSec.section_length + sizeof(key->pubHdr);
Heiko Carstens8ff34582017-05-09 12:19:16 +0200162 key->t6_hdr.ulen = key->pubHdr.token_length + 4;
163 key->t6_hdr.blen = key->pubHdr.token_length + 6;
Martin Schwidefsky6684af12006-09-20 15:58:32 +0200164 return sizeof(*key) + 2*mex->inputdatalength - i;
165}
166
167/**
168 * Set up private key fields of a type6 CRT message.
169 * Note that all numerics in the key token are big-endian,
170 * while the entries in the key block header are little-endian.
171 *
172 * @mex: pointer to user input data
173 * @p: pointer to memory area for the key
174 *
175 * Returns the size of the key area or -EFAULT
176 */
Heiko Carstens8ff34582017-05-09 12:19:16 +0200177static inline int zcrypt_type6_crt_key(struct ica_rsa_modexpo_crt *crt, void *p)
Martin Schwidefsky6684af12006-09-20 15:58:32 +0200178{
179 static struct cca_public_sec static_cca_pub_sec = {
180 .section_identifier = 4,
181 .section_length = 0x000f,
182 .exponent_len = 0x0003,
183 };
184 static char pk_exponent[3] = { 0x01, 0x00, 0x01 };
185 struct {
186 struct T6_keyBlock_hdr t6_hdr;
187 struct cca_token_hdr token;
188 struct cca_pvt_ext_CRT_sec pvt;
189 char key_parts[0];
190 } __attribute__((packed)) *key = p;
191 struct cca_public_sec *pub;
192 int short_len, long_len, pad_len, key_len, size;
193
194 memset(key, 0, sizeof(*key));
195
Ingo Tuchscherer1330a122015-09-04 09:57:24 +0200196 short_len = (crt->inputdatalength + 1) / 2;
Martin Schwidefsky6684af12006-09-20 15:58:32 +0200197 long_len = short_len + 8;
198 pad_len = -(3*long_len + 2*short_len) & 7;
199 key_len = 3*long_len + 2*short_len + pad_len + crt->inputdatalength;
200 size = sizeof(*key) + key_len + sizeof(*pub) + 3;
201
202 /* parameter block.key block */
Heiko Carstens8ff34582017-05-09 12:19:16 +0200203 key->t6_hdr.blen = size;
204 key->t6_hdr.ulen = size - 2;
Martin Schwidefsky6684af12006-09-20 15:58:32 +0200205
206 /* key token header */
207 key->token.token_identifier = CCA_TKN_HDR_ID_EXT;
208 key->token.token_length = size - 6;
209
210 /* private section */
211 key->pvt.section_identifier = CCA_PVT_EXT_CRT_SEC_ID_PVT;
212 key->pvt.section_length = sizeof(key->pvt) + key_len;
213 key->pvt.key_format = CCA_PVT_EXT_CRT_SEC_FMT_CL;
214 key->pvt.key_use_flags[0] = CCA_PVT_USAGE_ALL;
215 key->pvt.p_len = key->pvt.dp_len = key->pvt.u_len = long_len;
216 key->pvt.q_len = key->pvt.dq_len = short_len;
217 key->pvt.mod_len = crt->inputdatalength;
218 key->pvt.pad_len = pad_len;
219
220 /* key parts */
221 if (copy_from_user(key->key_parts, crt->np_prime, long_len) ||
222 copy_from_user(key->key_parts + long_len,
223 crt->nq_prime, short_len) ||
224 copy_from_user(key->key_parts + long_len + short_len,
225 crt->bp_key, long_len) ||
226 copy_from_user(key->key_parts + 2*long_len + short_len,
227 crt->bq_key, short_len) ||
228 copy_from_user(key->key_parts + 2*long_len + 2*short_len,
229 crt->u_mult_inv, long_len))
230 return -EFAULT;
231 memset(key->key_parts + 3*long_len + 2*short_len + pad_len,
232 0xff, crt->inputdatalength);
233 pub = (struct cca_public_sec *)(key->key_parts + key_len);
234 *pub = static_cca_pub_sec;
235 pub->modulus_bit_len = 8 * crt->inputdatalength;
Felix Beck1749a812008-04-17 07:46:28 +0200236 /*
Martin Schwidefsky6684af12006-09-20 15:58:32 +0200237 * In a private key, the modulus doesn't appear in the public
238 * section. So, an arbitrary public exponent of 0x010001 will be
239 * used.
240 */
241 memcpy((char *) (pub + 1), pk_exponent, 3);
242 return size;
243}
244
245#endif /* _ZCRYPT_CCA_KEY_H_ */