Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[sfrench/cifs-2.6.git] / drivers / s390 / crypto / zcrypt_cca_key.h
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  zcrypt 2.1.0
4  *
5  *  Copyright IBM Corp. 2001, 2006
6  *  Author(s): Robert Burroughs
7  *             Eric Rossman (edrossma@us.ibm.com)
8  *
9  *  Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
10  *  Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
11  */
12
13 #ifndef _ZCRYPT_CCA_KEY_H_
14 #define _ZCRYPT_CCA_KEY_H_
15
16 struct T6_keyBlock_hdr {
17         unsigned short blen;
18         unsigned short ulen;
19         unsigned short flags;
20 };
21
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.
26  *
27  * mapping for the cca key token header
28  */
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 } __attribute__((packed));
35
36 #define CCA_TKN_HDR_ID_EXT 0x1E
37
38 #define CCA_PVT_USAGE_ALL 0x80
39
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.
45  */
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 } __attribute__((packed));
55
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
62  *
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).
67  */
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 } __attribute__((packed));
89
90 #define CCA_PVT_EXT_CRT_SEC_ID_PVT 0x08
91 #define CCA_PVT_EXT_CRT_SEC_FMT_CL 0x40
92
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.
98  *
99  * @mex: pointer to user input data
100  * @p: pointer to memory area for the key
101  *
102  * Returns the size of the key area or -EFAULT
103  */
104 static inline int zcrypt_type6_mex_key_en(struct ica_rsa_modexpo *mex, void *p)
105 {
106         static struct cca_token_hdr static_pub_hdr = {
107                 .token_identifier       =  0x1E,
108         };
109         static struct cca_public_sec static_pub_sec = {
110                 .section_identifier     =  0x04,
111         };
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         } __attribute__((packed)) *key = p;
118         unsigned char *temp;
119         int i;
120
121         memset(key, 0, sizeof(*key));
122
123         key->pubHdr = static_pub_hdr;
124         key->pubSec = static_pub_sec;
125
126         /* key parameter block */
127         temp = key->exponent;
128         if (copy_from_user(temp, mex->b_key, mex->inputdatalength))
129                 return -EFAULT;
130         /* Strip leading zeroes from b_key. */
131         for (i = 0; i < mex->inputdatalength; i++)
132                 if (temp[i])
133                         break;
134         if (i >= mex->inputdatalength)
135                 return -EINVAL;
136         memmove(temp, temp + i, mex->inputdatalength - i);
137         temp += mex->inputdatalength - i;
138         /* modulus */
139         if (copy_from_user(temp, mex->n_modulus, mex->inputdatalength))
140                 return -EFAULT;
141
142         key->pubSec.modulus_bit_len = 8 * mex->inputdatalength;
143         key->pubSec.modulus_byte_len = mex->inputdatalength;
144         key->pubSec.exponent_len = mex->inputdatalength - i;
145         key->pubSec.section_length = sizeof(key->pubSec) +
146                                         2*mex->inputdatalength - i;
147         key->pubHdr.token_length =
148                 key->pubSec.section_length + sizeof(key->pubHdr);
149         key->t6_hdr.ulen = key->pubHdr.token_length + 4;
150         key->t6_hdr.blen = key->pubHdr.token_length + 6;
151         return sizeof(*key) + 2*mex->inputdatalength - i;
152 }
153
154 /**
155  * Set up private key fields of a type6 CRT message.
156  * Note that all numerics in the key token are big-endian,
157  * while the entries in the key block header are little-endian.
158  *
159  * @mex: pointer to user input data
160  * @p: pointer to memory area for the key
161  *
162  * Returns the size of the key area or -EFAULT
163  */
164 static inline int zcrypt_type6_crt_key(struct ica_rsa_modexpo_crt *crt, void *p)
165 {
166         static struct cca_public_sec static_cca_pub_sec = {
167                 .section_identifier = 4,
168                 .section_length = 0x000f,
169                 .exponent_len = 0x0003,
170         };
171         static char pk_exponent[3] = { 0x01, 0x00, 0x01 };
172         struct {
173                 struct T6_keyBlock_hdr t6_hdr;
174                 struct cca_token_hdr token;
175                 struct cca_pvt_ext_CRT_sec pvt;
176                 char key_parts[0];
177         } __attribute__((packed)) *key = p;
178         struct cca_public_sec *pub;
179         int short_len, long_len, pad_len, key_len, size;
180
181         memset(key, 0, sizeof(*key));
182
183         short_len = (crt->inputdatalength + 1) / 2;
184         long_len = short_len + 8;
185         pad_len = -(3*long_len + 2*short_len) & 7;
186         key_len = 3*long_len + 2*short_len + pad_len + crt->inputdatalength;
187         size = sizeof(*key) + key_len + sizeof(*pub) + 3;
188
189         /* parameter block.key block */
190         key->t6_hdr.blen = size;
191         key->t6_hdr.ulen = size - 2;
192
193         /* key token header */
194         key->token.token_identifier = CCA_TKN_HDR_ID_EXT;
195         key->token.token_length = size - 6;
196
197         /* private section */
198         key->pvt.section_identifier = CCA_PVT_EXT_CRT_SEC_ID_PVT;
199         key->pvt.section_length = sizeof(key->pvt) + key_len;
200         key->pvt.key_format = CCA_PVT_EXT_CRT_SEC_FMT_CL;
201         key->pvt.key_use_flags[0] = CCA_PVT_USAGE_ALL;
202         key->pvt.p_len = key->pvt.dp_len = key->pvt.u_len = long_len;
203         key->pvt.q_len = key->pvt.dq_len = short_len;
204         key->pvt.mod_len = crt->inputdatalength;
205         key->pvt.pad_len = pad_len;
206
207         /* key parts */
208         if (copy_from_user(key->key_parts, crt->np_prime, long_len) ||
209             copy_from_user(key->key_parts + long_len,
210                                         crt->nq_prime, short_len) ||
211             copy_from_user(key->key_parts + long_len + short_len,
212                                         crt->bp_key, long_len) ||
213             copy_from_user(key->key_parts + 2*long_len + short_len,
214                                         crt->bq_key, short_len) ||
215             copy_from_user(key->key_parts + 2*long_len + 2*short_len,
216                                         crt->u_mult_inv, long_len))
217                 return -EFAULT;
218         memset(key->key_parts + 3*long_len + 2*short_len + pad_len,
219                0xff, crt->inputdatalength);
220         pub = (struct cca_public_sec *)(key->key_parts + key_len);
221         *pub = static_cca_pub_sec;
222         pub->modulus_bit_len = 8 * crt->inputdatalength;
223         /*
224          * In a private key, the modulus doesn't appear in the public
225          * section. So, an arbitrary public exponent of 0x010001 will be
226          * used.
227          */
228         memcpy((char *) (pub + 1), pk_exponent, 3);
229         return size;
230 }
231
232 #endif /* _ZCRYPT_CCA_KEY_H_ */