1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#ifndef _CRYPTO_ARCH_S390_CRYPT_S390_H
17#define _CRYPTO_ARCH_S390_CRYPT_S390_H
18
19#include <asm/errno.h>
20
21#define CRYPT_S390_OP_MASK 0xFF00
22#define CRYPT_S390_FUNC_MASK 0x00FF
23
24#define CRYPT_S390_PRIORITY 300
25#define CRYPT_S390_COMPOSITE_PRIORITY 400
26
27
28enum crypt_s390_operations {
29 CRYPT_S390_KM = 0x0100,
30 CRYPT_S390_KMC = 0x0200,
31 CRYPT_S390_KIMD = 0x0300,
32 CRYPT_S390_KLMD = 0x0400,
33 CRYPT_S390_KMAC = 0x0500
34};
35
36
37
38
39
40enum crypt_s390_km_func {
41 KM_QUERY = CRYPT_S390_KM | 0x0,
42 KM_DEA_ENCRYPT = CRYPT_S390_KM | 0x1,
43 KM_DEA_DECRYPT = CRYPT_S390_KM | 0x1 | 0x80,
44 KM_TDEA_128_ENCRYPT = CRYPT_S390_KM | 0x2,
45 KM_TDEA_128_DECRYPT = CRYPT_S390_KM | 0x2 | 0x80,
46 KM_TDEA_192_ENCRYPT = CRYPT_S390_KM | 0x3,
47 KM_TDEA_192_DECRYPT = CRYPT_S390_KM | 0x3 | 0x80,
48 KM_AES_128_ENCRYPT = CRYPT_S390_KM | 0x12,
49 KM_AES_128_DECRYPT = CRYPT_S390_KM | 0x12 | 0x80,
50 KM_AES_192_ENCRYPT = CRYPT_S390_KM | 0x13,
51 KM_AES_192_DECRYPT = CRYPT_S390_KM | 0x13 | 0x80,
52 KM_AES_256_ENCRYPT = CRYPT_S390_KM | 0x14,
53 KM_AES_256_DECRYPT = CRYPT_S390_KM | 0x14 | 0x80,
54};
55
56
57
58
59
60enum crypt_s390_kmc_func {
61 KMC_QUERY = CRYPT_S390_KMC | 0x0,
62 KMC_DEA_ENCRYPT = CRYPT_S390_KMC | 0x1,
63 KMC_DEA_DECRYPT = CRYPT_S390_KMC | 0x1 | 0x80,
64 KMC_TDEA_128_ENCRYPT = CRYPT_S390_KMC | 0x2,
65 KMC_TDEA_128_DECRYPT = CRYPT_S390_KMC | 0x2 | 0x80,
66 KMC_TDEA_192_ENCRYPT = CRYPT_S390_KMC | 0x3,
67 KMC_TDEA_192_DECRYPT = CRYPT_S390_KMC | 0x3 | 0x80,
68 KMC_AES_128_ENCRYPT = CRYPT_S390_KMC | 0x12,
69 KMC_AES_128_DECRYPT = CRYPT_S390_KMC | 0x12 | 0x80,
70 KMC_AES_192_ENCRYPT = CRYPT_S390_KMC | 0x13,
71 KMC_AES_192_DECRYPT = CRYPT_S390_KMC | 0x13 | 0x80,
72 KMC_AES_256_ENCRYPT = CRYPT_S390_KMC | 0x14,
73 KMC_AES_256_DECRYPT = CRYPT_S390_KMC | 0x14 | 0x80,
74 KMC_PRNG = CRYPT_S390_KMC | 0x43,
75};
76
77
78
79
80
81enum crypt_s390_kimd_func {
82 KIMD_QUERY = CRYPT_S390_KIMD | 0,
83 KIMD_SHA_1 = CRYPT_S390_KIMD | 1,
84 KIMD_SHA_256 = CRYPT_S390_KIMD | 2,
85 KIMD_SHA_512 = CRYPT_S390_KIMD | 3,
86};
87
88
89
90
91
92enum crypt_s390_klmd_func {
93 KLMD_QUERY = CRYPT_S390_KLMD | 0,
94 KLMD_SHA_1 = CRYPT_S390_KLMD | 1,
95 KLMD_SHA_256 = CRYPT_S390_KLMD | 2,
96 KLMD_SHA_512 = CRYPT_S390_KLMD | 3,
97};
98
99
100
101
102
103enum crypt_s390_kmac_func {
104 KMAC_QUERY = CRYPT_S390_KMAC | 0,
105 KMAC_DEA = CRYPT_S390_KMAC | 1,
106 KMAC_TDEA_128 = CRYPT_S390_KMAC | 2,
107 KMAC_TDEA_192 = CRYPT_S390_KMAC | 3
108};
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123static inline int crypt_s390_km(long func, void *param,
124 u8 *dest, const u8 *src, long src_len)
125{
126 register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
127 register void *__param asm("1") = param;
128 register const u8 *__src asm("2") = src;
129 register long __src_len asm("3") = src_len;
130 register u8 *__dest asm("4") = dest;
131 int ret;
132
133 asm volatile(
134 "0: .insn rre,0xb92e0000,%3,%1 \n"
135 "1: brc 1,0b \n"
136 " la %0,0\n"
137 "2:\n"
138 EX_TABLE(0b,2b) EX_TABLE(1b,2b)
139 : "=d" (ret), "+a" (__src), "+d" (__src_len), "+a" (__dest)
140 : "d" (__func), "a" (__param), "0" (-1) : "cc", "memory");
141 if (ret < 0)
142 return ret;
143 return (func & CRYPT_S390_FUNC_MASK) ? src_len - __src_len : __src_len;
144}
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159static inline int crypt_s390_kmc(long func, void *param,
160 u8 *dest, const u8 *src, long src_len)
161{
162 register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
163 register void *__param asm("1") = param;
164 register const u8 *__src asm("2") = src;
165 register long __src_len asm("3") = src_len;
166 register u8 *__dest asm("4") = dest;
167 int ret;
168
169 asm volatile(
170 "0: .insn rre,0xb92f0000,%3,%1 \n"
171 "1: brc 1,0b \n"
172 " la %0,0\n"
173 "2:\n"
174 EX_TABLE(0b,2b) EX_TABLE(1b,2b)
175 : "=d" (ret), "+a" (__src), "+d" (__src_len), "+a" (__dest)
176 : "d" (__func), "a" (__param), "0" (-1) : "cc", "memory");
177 if (ret < 0)
178 return ret;
179 return (func & CRYPT_S390_FUNC_MASK) ? src_len - __src_len : __src_len;
180}
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195static inline int crypt_s390_kimd(long func, void *param,
196 const u8 *src, long src_len)
197{
198 register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
199 register void *__param asm("1") = param;
200 register const u8 *__src asm("2") = src;
201 register long __src_len asm("3") = src_len;
202 int ret;
203
204 asm volatile(
205 "0: .insn rre,0xb93e0000,%1,%1 \n"
206 "1: brc 1,0b \n"
207 " la %0,0\n"
208 "2:\n"
209 EX_TABLE(0b,2b) EX_TABLE(1b,2b)
210 : "=d" (ret), "+a" (__src), "+d" (__src_len)
211 : "d" (__func), "a" (__param), "0" (-1) : "cc", "memory");
212 if (ret < 0)
213 return ret;
214 return (func & CRYPT_S390_FUNC_MASK) ? src_len - __src_len : __src_len;
215}
216
217
218
219
220
221
222
223
224
225
226
227
228
229static inline int crypt_s390_klmd(long func, void *param,
230 const u8 *src, long src_len)
231{
232 register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
233 register void *__param asm("1") = param;
234 register const u8 *__src asm("2") = src;
235 register long __src_len asm("3") = src_len;
236 int ret;
237
238 asm volatile(
239 "0: .insn rre,0xb93f0000,%1,%1 \n"
240 "1: brc 1,0b \n"
241 " la %0,0\n"
242 "2:\n"
243 EX_TABLE(0b,2b) EX_TABLE(1b,2b)
244 : "=d" (ret), "+a" (__src), "+d" (__src_len)
245 : "d" (__func), "a" (__param), "0" (-1) : "cc", "memory");
246 if (ret < 0)
247 return ret;
248 return (func & CRYPT_S390_FUNC_MASK) ? src_len - __src_len : __src_len;
249}
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264static inline int crypt_s390_kmac(long func, void *param,
265 const u8 *src, long src_len)
266{
267 register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
268 register void *__param asm("1") = param;
269 register const u8 *__src asm("2") = src;
270 register long __src_len asm("3") = src_len;
271 int ret;
272
273 asm volatile(
274 "0: .insn rre,0xb91e0000,%1,%1 \n"
275 "1: brc 1,0b \n"
276 " la %0,0\n"
277 "2:\n"
278 EX_TABLE(0b,2b) EX_TABLE(1b,2b)
279 : "=d" (ret), "+a" (__src), "+d" (__src_len)
280 : "d" (__func), "a" (__param), "0" (-1) : "cc", "memory");
281 if (ret < 0)
282 return ret;
283 return (func & CRYPT_S390_FUNC_MASK) ? src_len - __src_len : __src_len;
284}
285
286
287
288
289
290
291
292
293
294static inline int crypt_s390_func_available(int func)
295{
296 unsigned char status[16];
297 int ret;
298
299
300 if (!test_facility(17))
301 return 0;
302
303 switch (func & CRYPT_S390_OP_MASK) {
304 case CRYPT_S390_KM:
305 ret = crypt_s390_km(KM_QUERY, &status, NULL, NULL, 0);
306 break;
307 case CRYPT_S390_KMC:
308 ret = crypt_s390_kmc(KMC_QUERY, &status, NULL, NULL, 0);
309 break;
310 case CRYPT_S390_KIMD:
311 ret = crypt_s390_kimd(KIMD_QUERY, &status, NULL, 0);
312 break;
313 case CRYPT_S390_KLMD:
314 ret = crypt_s390_klmd(KLMD_QUERY, &status, NULL, 0);
315 break;
316 case CRYPT_S390_KMAC:
317 ret = crypt_s390_kmac(KMAC_QUERY, &status, NULL, 0);
318 break;
319 default:
320 return 0;
321 }
322 if (ret < 0)
323 return 0;
324 func &= CRYPT_S390_FUNC_MASK;
325 func &= 0x7f;
326 return (status[func >> 3] & (0x80 >> (func & 7))) != 0;
327}
328
329#endif
330