sctp: walk the list of asoc safely
[linux/fpc-iii.git] / drivers / s390 / crypto / zcrypt_cca_key.h
blobe5b5c02c9d67819a66b73d87b9857641cdec24f1
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3 * zcrypt 2.1.0
5 * Copyright IBM Corp. 2001, 2006
6 * Author(s): Robert Burroughs
7 * Eric Rossman (edrossma@us.ibm.com)
9 * Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
10 * Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
13 #ifndef _ZCRYPT_CCA_KEY_H_
14 #define _ZCRYPT_CCA_KEY_H_
16 struct T6_keyBlock_hdr {
17 unsigned short blen;
18 unsigned short ulen;
19 unsigned short flags;
22 /**
23 * mapping for the cca private ME key token.
24 * Three parts of interest here: the header, the private section and
25 * the public section.
27 * mapping for the cca key token header
29 struct cca_token_hdr {
30 unsigned char token_identifier;
31 unsigned char version;
32 unsigned short token_length;
33 unsigned char reserved[4];
34 } __packed;
36 #define CCA_TKN_HDR_ID_EXT 0x1E
38 #define CCA_PVT_USAGE_ALL 0x80
40 /**
41 * mapping for the cca public section
42 * In a private key, the modulus doesn't appear in the public
43 * section. So, an arbitrary public exponent of 0x010001 will be
44 * used, for a section length of 0x0F always.
46 struct cca_public_sec {
47 unsigned char section_identifier;
48 unsigned char version;
49 unsigned short section_length;
50 unsigned char reserved[2];
51 unsigned short exponent_len;
52 unsigned short modulus_bit_len;
53 unsigned short modulus_byte_len; /* In a private key, this is 0 */
54 } __packed;
56 /**
57 * mapping for the cca private CRT key 'token'
58 * The first three parts (the only parts considered in this release)
59 * are: the header, the private section and the public section.
60 * The header and public section are the same as for the
61 * struct cca_private_ext_ME
63 * Following the structure are the quantities p, q, dp, dq, u, pad,
64 * and modulus, in that order, where pad_len is the modulo 8
65 * complement of the residue modulo 8 of the sum of
66 * (p_len + q_len + dp_len + dq_len + u_len).
68 struct cca_pvt_ext_CRT_sec {
69 unsigned char section_identifier;
70 unsigned char version;
71 unsigned short section_length;
72 unsigned char private_key_hash[20];
73 unsigned char reserved1[4];
74 unsigned char key_format;
75 unsigned char reserved2;
76 unsigned char key_name_hash[20];
77 unsigned char key_use_flags[4];
78 unsigned short p_len;
79 unsigned short q_len;
80 unsigned short dp_len;
81 unsigned short dq_len;
82 unsigned short u_len;
83 unsigned short mod_len;
84 unsigned char reserved3[4];
85 unsigned short pad_len;
86 unsigned char reserved4[52];
87 unsigned char confounder[8];
88 } __packed;
90 #define CCA_PVT_EXT_CRT_SEC_ID_PVT 0x08
91 #define CCA_PVT_EXT_CRT_SEC_FMT_CL 0x40
93 /**
94 * Set up private key fields of a type6 MEX message. The _pad variant
95 * strips leading zeroes from the b_key.
96 * Note that all numerics in the key token are big-endian,
97 * while the entries in the key block header are little-endian.
99 * @mex: pointer to user input data
100 * @p: pointer to memory area for the key
102 * Returns the size of the key area or negative errno value.
104 static inline int zcrypt_type6_mex_key_en(struct ica_rsa_modexpo *mex, void *p)
106 static struct cca_token_hdr static_pub_hdr = {
107 .token_identifier = 0x1E,
109 static struct cca_public_sec static_pub_sec = {
110 .section_identifier = 0x04,
112 struct {
113 struct T6_keyBlock_hdr t6_hdr;
114 struct cca_token_hdr pubHdr;
115 struct cca_public_sec pubSec;
116 char exponent[0];
117 } __packed *key = p;
118 unsigned char *temp;
119 int i;
122 * The inputdatalength was a selection criteria in the dispatching
123 * function zcrypt_rsa_modexpo(). However, do a plausibility check
124 * here to make sure the following copy_from_user() can't be utilized
125 * to compromise the system.
127 if (WARN_ON_ONCE(mex->inputdatalength > 512))
128 return -EINVAL;
130 memset(key, 0, sizeof(*key));
132 key->pubHdr = static_pub_hdr;
133 key->pubSec = static_pub_sec;
135 /* key parameter block */
136 temp = key->exponent;
137 if (copy_from_user(temp, mex->b_key, mex->inputdatalength))
138 return -EFAULT;
139 /* Strip leading zeroes from b_key. */
140 for (i = 0; i < mex->inputdatalength; i++)
141 if (temp[i])
142 break;
143 if (i >= mex->inputdatalength)
144 return -EINVAL;
145 memmove(temp, temp + i, mex->inputdatalength - i);
146 temp += mex->inputdatalength - i;
147 /* modulus */
148 if (copy_from_user(temp, mex->n_modulus, mex->inputdatalength))
149 return -EFAULT;
151 key->pubSec.modulus_bit_len = 8 * mex->inputdatalength;
152 key->pubSec.modulus_byte_len = mex->inputdatalength;
153 key->pubSec.exponent_len = mex->inputdatalength - i;
154 key->pubSec.section_length = sizeof(key->pubSec) +
155 2*mex->inputdatalength - i;
156 key->pubHdr.token_length =
157 key->pubSec.section_length + sizeof(key->pubHdr);
158 key->t6_hdr.ulen = key->pubHdr.token_length + 4;
159 key->t6_hdr.blen = key->pubHdr.token_length + 6;
160 return sizeof(*key) + 2*mex->inputdatalength - i;
164 * Set up private key fields of a type6 CRT message.
165 * Note that all numerics in the key token are big-endian,
166 * while the entries in the key block header are little-endian.
168 * @mex: pointer to user input data
169 * @p: pointer to memory area for the key
171 * Returns the size of the key area or -EFAULT
173 static inline int zcrypt_type6_crt_key(struct ica_rsa_modexpo_crt *crt, void *p)
175 static struct cca_public_sec static_cca_pub_sec = {
176 .section_identifier = 4,
177 .section_length = 0x000f,
178 .exponent_len = 0x0003,
180 static char pk_exponent[3] = { 0x01, 0x00, 0x01 };
181 struct {
182 struct T6_keyBlock_hdr t6_hdr;
183 struct cca_token_hdr token;
184 struct cca_pvt_ext_CRT_sec pvt;
185 char key_parts[0];
186 } __packed *key = p;
187 struct cca_public_sec *pub;
188 int short_len, long_len, pad_len, key_len, size;
191 * The inputdatalength was a selection criteria in the dispatching
192 * function zcrypt_rsa_crt(). However, do a plausibility check
193 * here to make sure the following copy_from_user() can't be utilized
194 * to compromise the system.
196 if (WARN_ON_ONCE(crt->inputdatalength > 512))
197 return -EINVAL;
199 memset(key, 0, sizeof(*key));
201 short_len = (crt->inputdatalength + 1) / 2;
202 long_len = short_len + 8;
203 pad_len = -(3*long_len + 2*short_len) & 7;
204 key_len = 3*long_len + 2*short_len + pad_len + crt->inputdatalength;
205 size = sizeof(*key) + key_len + sizeof(*pub) + 3;
207 /* parameter block.key block */
208 key->t6_hdr.blen = size;
209 key->t6_hdr.ulen = size - 2;
211 /* key token header */
212 key->token.token_identifier = CCA_TKN_HDR_ID_EXT;
213 key->token.token_length = size - 6;
215 /* private section */
216 key->pvt.section_identifier = CCA_PVT_EXT_CRT_SEC_ID_PVT;
217 key->pvt.section_length = sizeof(key->pvt) + key_len;
218 key->pvt.key_format = CCA_PVT_EXT_CRT_SEC_FMT_CL;
219 key->pvt.key_use_flags[0] = CCA_PVT_USAGE_ALL;
220 key->pvt.p_len = key->pvt.dp_len = key->pvt.u_len = long_len;
221 key->pvt.q_len = key->pvt.dq_len = short_len;
222 key->pvt.mod_len = crt->inputdatalength;
223 key->pvt.pad_len = pad_len;
225 /* key parts */
226 if (copy_from_user(key->key_parts, crt->np_prime, long_len) ||
227 copy_from_user(key->key_parts + long_len,
228 crt->nq_prime, short_len) ||
229 copy_from_user(key->key_parts + long_len + short_len,
230 crt->bp_key, long_len) ||
231 copy_from_user(key->key_parts + 2*long_len + short_len,
232 crt->bq_key, short_len) ||
233 copy_from_user(key->key_parts + 2*long_len + 2*short_len,
234 crt->u_mult_inv, long_len))
235 return -EFAULT;
236 memset(key->key_parts + 3*long_len + 2*short_len + pad_len,
237 0xff, crt->inputdatalength);
238 pub = (struct cca_public_sec *)(key->key_parts + key_len);
239 *pub = static_cca_pub_sec;
240 pub->modulus_bit_len = 8 * crt->inputdatalength;
242 * In a private key, the modulus doesn't appear in the public
243 * section. So, an arbitrary public exponent of 0x010001 will be
244 * used.
246 memcpy((char *) (pub + 1), pk_exponent, 3);
247 return size;
250 #endif /* _ZCRYPT_CCA_KEY_H_ */