crypt_s390.h 11.7 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
2
3
/*
 * Cryptographic API.
 *
4
 * Support for s390 cryptographic instructions.
Linus Torvalds's avatar
Linus Torvalds committed
5
6
7
8
9
10
11
12
13
14
 *
 *   Copyright (C) 2003 IBM Deutschland GmbH, IBM Corporation
 *   Author(s): Thomas Spatzier (tspat@de.ibm.com)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 */
15
16
#ifndef _CRYPTO_ARCH_S390_CRYPT_S390_H
#define _CRYPTO_ARCH_S390_CRYPT_S390_H
Linus Torvalds's avatar
Linus Torvalds committed
17
18
19

#include <asm/errno.h>

20
21
#define CRYPT_S390_OP_MASK 0xFF00
#define CRYPT_S390_FUNC_MASK 0x00FF
Linus Torvalds's avatar
Linus Torvalds committed
22

23
24
25
26
27
28
29
/* s930 cryptographic operations */
enum crypt_s390_operations {
	CRYPT_S390_KM   = 0x0100,
	CRYPT_S390_KMC  = 0x0200,
	CRYPT_S390_KIMD = 0x0300,
	CRYPT_S390_KLMD = 0x0400,
	CRYPT_S390_KMAC = 0x0500
Linus Torvalds's avatar
Linus Torvalds committed
30
31
};

32
33
34
35
/* function codes for KM (CIPHER MESSAGE) instruction
 * 0x80 is the decipher modifier bit
 */
enum crypt_s390_km_func {
Jan Glauber's avatar
Jan Glauber committed
36
37
38
39
40
41
42
43
44
45
46
47
48
	KM_QUERY	    = CRYPT_S390_KM | 0x0,
	KM_DEA_ENCRYPT      = CRYPT_S390_KM | 0x1,
	KM_DEA_DECRYPT      = CRYPT_S390_KM | 0x1 | 0x80,
	KM_TDEA_128_ENCRYPT = CRYPT_S390_KM | 0x2,
	KM_TDEA_128_DECRYPT = CRYPT_S390_KM | 0x2 | 0x80,
	KM_TDEA_192_ENCRYPT = CRYPT_S390_KM | 0x3,
	KM_TDEA_192_DECRYPT = CRYPT_S390_KM | 0x3 | 0x80,
	KM_AES_128_ENCRYPT  = CRYPT_S390_KM | 0x12,
	KM_AES_128_DECRYPT  = CRYPT_S390_KM | 0x12 | 0x80,
	KM_AES_192_ENCRYPT  = CRYPT_S390_KM | 0x13,
	KM_AES_192_DECRYPT  = CRYPT_S390_KM | 0x13 | 0x80,
	KM_AES_256_ENCRYPT  = CRYPT_S390_KM | 0x14,
	KM_AES_256_DECRYPT  = CRYPT_S390_KM | 0x14 | 0x80,
Linus Torvalds's avatar
Linus Torvalds committed
49
50
};

51
52
53
54
/* function codes for KMC (CIPHER MESSAGE WITH CHAINING)
 * instruction
 */
enum crypt_s390_kmc_func {
Jan Glauber's avatar
Jan Glauber committed
55
56
57
58
59
60
61
62
63
64
65
66
67
	KMC_QUERY            = CRYPT_S390_KMC | 0x0,
	KMC_DEA_ENCRYPT      = CRYPT_S390_KMC | 0x1,
	KMC_DEA_DECRYPT      = CRYPT_S390_KMC | 0x1 | 0x80,
	KMC_TDEA_128_ENCRYPT = CRYPT_S390_KMC | 0x2,
	KMC_TDEA_128_DECRYPT = CRYPT_S390_KMC | 0x2 | 0x80,
	KMC_TDEA_192_ENCRYPT = CRYPT_S390_KMC | 0x3,
	KMC_TDEA_192_DECRYPT = CRYPT_S390_KMC | 0x3 | 0x80,
	KMC_AES_128_ENCRYPT  = CRYPT_S390_KMC | 0x12,
	KMC_AES_128_DECRYPT  = CRYPT_S390_KMC | 0x12 | 0x80,
	KMC_AES_192_ENCRYPT  = CRYPT_S390_KMC | 0x13,
	KMC_AES_192_DECRYPT  = CRYPT_S390_KMC | 0x13 | 0x80,
	KMC_AES_256_ENCRYPT  = CRYPT_S390_KMC | 0x14,
	KMC_AES_256_DECRYPT  = CRYPT_S390_KMC | 0x14 | 0x80,
Linus Torvalds's avatar
Linus Torvalds committed
68
69
};

70
71
72
73
74
75
/* function codes for KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST)
 * instruction
 */
enum crypt_s390_kimd_func {
	KIMD_QUERY   = CRYPT_S390_KIMD | 0,
	KIMD_SHA_1   = CRYPT_S390_KIMD | 1,
Jan Glauber's avatar
Jan Glauber committed
76
	KIMD_SHA_256 = CRYPT_S390_KIMD | 2,
Linus Torvalds's avatar
Linus Torvalds committed
77
78
};

79
80
81
82
83
84
/* function codes for KLMD (COMPUTE LAST MESSAGE DIGEST)
 * instruction
 */
enum crypt_s390_klmd_func {
	KLMD_QUERY   = CRYPT_S390_KLMD | 0,
	KLMD_SHA_1   = CRYPT_S390_KLMD | 1,
Jan Glauber's avatar
Jan Glauber committed
85
	KLMD_SHA_256 = CRYPT_S390_KLMD | 2,
Linus Torvalds's avatar
Linus Torvalds committed
86
87
};

88
89
90
91
92
93
94
95
/* function codes for KMAC (COMPUTE MESSAGE AUTHENTICATION CODE)
 * instruction
 */
enum crypt_s390_kmac_func {
	KMAC_QUERY    = CRYPT_S390_KMAC | 0,
	KMAC_DEA      = CRYPT_S390_KMAC | 1,
	KMAC_TDEA_128 = CRYPT_S390_KMAC | 2,
	KMAC_TDEA_192 = CRYPT_S390_KMAC | 3
Linus Torvalds's avatar
Linus Torvalds committed
96
97
};

98
99
/* status word for s390 crypto instructions' QUERY functions */
struct crypt_s390_query_status {
Linus Torvalds's avatar
Linus Torvalds committed
100
101
102
103
104
	u64 high;
	u64 low;
};

/*
105
106
107
108
 * Standard fixup and ex_table sections for crypt_s390 inline functions.
 * label 0: the s390 crypto operation
 * label 1: just after 1 to catch illegal operation exception
 *          (unsupported model)
Linus Torvalds's avatar
Linus Torvalds committed
109
110
111
112
113
114
 * label 6: the return point after fixup
 * label 7: set error value if exception _in_ crypto operation
 * label 8: set error value if illegal operation exception
 * [ret] is the variable to receive the error code
 * [ERR] is the error code value
 */
115
#ifndef CONFIG_64BIT
116
#define __crypt_s390_fixup \
Linus Torvalds's avatar
Linus Torvalds committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
	".section .fixup,\"ax\" \n"	\
	"7:	lhi	%0,%h[e1] \n"	\
	"	bras	1,9f \n"	\
	"	.long	6b \n"		\
	"8:	lhi	%0,%h[e2] \n"	\
	"	bras	1,9f \n"	\
	"	.long	6b \n"		\
	"9:	l	1,0(1) \n"	\
	"	br	1 \n"		\
	".previous \n"			\
	".section __ex_table,\"a\" \n"	\
	"	.align	4 \n"		\
	"	.long	0b,7b \n"	\
	"	.long	1b,8b \n"	\
	".previous"
132
#else /* CONFIG_64BIT */
133
#define __crypt_s390_fixup \
Linus Torvalds's avatar
Linus Torvalds committed
134
135
136
137
138
139
140
141
142
143
144
	".section .fixup,\"ax\" \n"	\
	"7:	lhi	%0,%h[e1] \n"	\
	"	jg	6b \n"		\
	"8:	lhi	%0,%h[e2] \n"	\
	"	jg	6b \n"		\
	".previous\n"			\
	".section __ex_table,\"a\" \n"	\
	"	.align	8 \n"		\
	"	.quad	0b,7b \n"	\
	"	.quad	1b,8b \n"	\
	".previous"
145
#endif /* CONFIG_64BIT */
Linus Torvalds's avatar
Linus Torvalds committed
146
147

/*
148
 * Standard code for setting the result of s390 crypto instructions.
Linus Torvalds's avatar
Linus Torvalds committed
149
150
151
152
 * %0: the register which will receive the result
 * [result]: the register containing the result (e.g. second operand length
 * to compute number of processed bytes].
 */
153
#ifndef CONFIG_64BIT
154
#define __crypt_s390_set_result \
Linus Torvalds's avatar
Linus Torvalds committed
155
	"	lr	%0,%[result] \n"
156
#else /* CONFIG_64BIT */
157
#define __crypt_s390_set_result \
Linus Torvalds's avatar
Linus Torvalds committed
158
159
160
161
	"	lgr	%0,%[result] \n"
#endif

/*
162
163
 * Executes the KM (CIPHER MESSAGE) operation of the CPU.
 * @param func: the function code passed to KM; see crypt_s390_km_func
Linus Torvalds's avatar
Linus Torvalds committed
164
165
166
167
168
169
170
171
 * @param param: address of parameter block; see POP for details on each func
 * @param dest: address of destination memory area
 * @param src: address of source memory area
 * @param src_len: length of src operand in bytes
 * @returns < zero for failure, 0 for the query func, number of processed bytes
 * 	for encryption/decryption funcs
 */
static inline int
172
crypt_s390_km(long func, void* param, u8* dest, const u8* src, long src_len)
Linus Torvalds's avatar
Linus Torvalds committed
173
{
174
	register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
Linus Torvalds's avatar
Linus Torvalds committed
175
176
177
178
179
180
181
182
	register void* __param asm("1") = param;
	register u8* __dest asm("4") = dest;
	register const u8* __src asm("2") = src;
	register long __src_len asm("3") = src_len;
	int ret;

	ret = 0;
	__asm__ __volatile__ (
183
184
185
		"0:	.insn	rre,0xB92E0000,%1,%2 \n" /* KM opcode */
		"1:	brc	1,0b \n" /* handle partial completion */
		__crypt_s390_set_result
Linus Torvalds's avatar
Linus Torvalds committed
186
		"6:	\n"
187
		__crypt_s390_fixup
Linus Torvalds's avatar
Linus Torvalds committed
188
189
190
191
192
193
		: "+d" (ret), "+a" (__dest), "+a" (__src),
		  [result] "+d" (__src_len)
		: [e1] "K" (-EFAULT), [e2] "K" (-ENOSYS), "d" (__func),
		  "a" (__param)
		: "cc", "memory"
	);
194
	if (ret >= 0 && func & CRYPT_S390_FUNC_MASK){
Linus Torvalds's avatar
Linus Torvalds committed
195
196
197
198
199
200
		ret = src_len - ret;
	}
	return ret;
}

/*
201
202
 * Executes the KMC (CIPHER MESSAGE WITH CHAINING) operation of the CPU.
 * @param func: the function code passed to KM; see crypt_s390_kmc_func
Linus Torvalds's avatar
Linus Torvalds committed
203
204
205
206
207
208
209
210
 * @param param: address of parameter block; see POP for details on each func
 * @param dest: address of destination memory area
 * @param src: address of source memory area
 * @param src_len: length of src operand in bytes
 * @returns < zero for failure, 0 for the query func, number of processed bytes
 * 	for encryption/decryption funcs
 */
static inline int
211
crypt_s390_kmc(long func, void* param, u8* dest, const u8* src, long src_len)
Linus Torvalds's avatar
Linus Torvalds committed
212
{
213
	register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
Linus Torvalds's avatar
Linus Torvalds committed
214
215
216
217
218
219
220
221
	register void* __param asm("1") = param;
	register u8* __dest asm("4") = dest;
	register const u8* __src asm("2") = src;
	register long __src_len asm("3") = src_len;
	int ret;

	ret = 0;
	__asm__ __volatile__ (
222
223
224
		"0:	.insn	rre,0xB92F0000,%1,%2 \n" /* KMC opcode */
		"1:	brc	1,0b \n" /* handle partial completion */
		__crypt_s390_set_result
Linus Torvalds's avatar
Linus Torvalds committed
225
		"6:	\n"
226
		__crypt_s390_fixup
Linus Torvalds's avatar
Linus Torvalds committed
227
228
229
230
231
232
		: "+d" (ret), "+a" (__dest), "+a" (__src),
		  [result] "+d" (__src_len)
		: [e1] "K" (-EFAULT), [e2] "K" (-ENOSYS), "d" (__func),
		  "a" (__param)
		: "cc", "memory"
	);
233
	if (ret >= 0 && func & CRYPT_S390_FUNC_MASK){
Linus Torvalds's avatar
Linus Torvalds committed
234
235
236
237
238
239
240
		ret = src_len - ret;
	}
	return ret;
}

/*
 * Executes the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST) operation
241
242
 * of the CPU.
 * @param func: the function code passed to KM; see crypt_s390_kimd_func
Linus Torvalds's avatar
Linus Torvalds committed
243
244
245
246
247
248
249
 * @param param: address of parameter block; see POP for details on each func
 * @param src: address of source memory area
 * @param src_len: length of src operand in bytes
 * @returns < zero for failure, 0 for the query func, number of processed bytes
 * 	for digest funcs
 */
static inline int
250
crypt_s390_kimd(long func, void* param, const u8* src, long src_len)
Linus Torvalds's avatar
Linus Torvalds committed
251
{
252
	register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
Linus Torvalds's avatar
Linus Torvalds committed
253
254
255
256
257
258
259
	register void* __param asm("1") = param;
	register const u8* __src asm("2") = src;
	register long __src_len asm("3") = src_len;
	int ret;

	ret = 0;
	__asm__ __volatile__ (
260
261
262
		"0:	.insn	rre,0xB93E0000,%1,%1 \n" /* KIMD opcode */
		"1:	brc	1,0b \n" /* handle partical completion */
		__crypt_s390_set_result
Linus Torvalds's avatar
Linus Torvalds committed
263
		"6:	\n"
264
		__crypt_s390_fixup
Linus Torvalds's avatar
Linus Torvalds committed
265
266
267
268
269
		: "+d" (ret), "+a" (__src), [result] "+d" (__src_len)
		: [e1] "K" (-EFAULT), [e2] "K" (-ENOSYS), "d" (__func),
		  "a" (__param)
		: "cc", "memory"
	);
270
	if (ret >= 0 && (func & CRYPT_S390_FUNC_MASK)){
Linus Torvalds's avatar
Linus Torvalds committed
271
272
273
274
275
276
		ret = src_len - ret;
	}
	return ret;
}

/*
277
278
 * Executes the KLMD (COMPUTE LAST MESSAGE DIGEST) operation of the CPU.
 * @param func: the function code passed to KM; see crypt_s390_klmd_func
Linus Torvalds's avatar
Linus Torvalds committed
279
280
281
282
283
284
285
 * @param param: address of parameter block; see POP for details on each func
 * @param src: address of source memory area
 * @param src_len: length of src operand in bytes
 * @returns < zero for failure, 0 for the query func, number of processed bytes
 * 	for digest funcs
 */
static inline int
286
crypt_s390_klmd(long func, void* param, const u8* src, long src_len)
Linus Torvalds's avatar
Linus Torvalds committed
287
{
288
	register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
Linus Torvalds's avatar
Linus Torvalds committed
289
290
291
292
293
294
295
	register void* __param asm("1") = param;
	register const u8* __src asm("2") = src;
	register long __src_len asm("3") = src_len;
	int ret;

	ret = 0;
	__asm__ __volatile__ (
296
297
298
		"0:	.insn	rre,0xB93F0000,%1,%1 \n" /* KLMD opcode */
		"1:	brc	1,0b \n" /* handle partical completion */
		__crypt_s390_set_result
Linus Torvalds's avatar
Linus Torvalds committed
299
		"6:	\n"
300
		__crypt_s390_fixup
Linus Torvalds's avatar
Linus Torvalds committed
301
302
303
304
305
		: "+d" (ret), "+a" (__src), [result] "+d" (__src_len)
		: [e1] "K" (-EFAULT), [e2] "K" (-ENOSYS), "d" (__func),
		  "a" (__param)
		: "cc", "memory"
	);
306
	if (ret >= 0 && func & CRYPT_S390_FUNC_MASK){
Linus Torvalds's avatar
Linus Torvalds committed
307
308
309
310
311
312
313
		ret = src_len - ret;
	}
	return ret;
}

/*
 * Executes the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE) operation
314
315
 * of the CPU.
 * @param func: the function code passed to KM; see crypt_s390_klmd_func
Linus Torvalds's avatar
Linus Torvalds committed
316
317
318
319
320
321
322
 * @param param: address of parameter block; see POP for details on each func
 * @param src: address of source memory area
 * @param src_len: length of src operand in bytes
 * @returns < zero for failure, 0 for the query func, number of processed bytes
 * 	for digest funcs
 */
static inline int
323
crypt_s390_kmac(long func, void* param, const u8* src, long src_len)
Linus Torvalds's avatar
Linus Torvalds committed
324
{
325
	register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
Linus Torvalds's avatar
Linus Torvalds committed
326
327
328
329
330
331
332
	register void* __param asm("1") = param;
	register const u8* __src asm("2") = src;
	register long __src_len asm("3") = src_len;
	int ret;

	ret = 0;
	__asm__ __volatile__ (
333
334
335
		"0:	.insn	rre,0xB91E0000,%5,%5 \n" /* KMAC opcode */
		"1:	brc	1,0b \n" /* handle partical completion */
		__crypt_s390_set_result
Linus Torvalds's avatar
Linus Torvalds committed
336
		"6:	\n"
337
		__crypt_s390_fixup
Linus Torvalds's avatar
Linus Torvalds committed
338
339
340
341
342
		: "+d" (ret), "+a" (__src), [result] "+d" (__src_len)
		: [e1] "K" (-EFAULT), [e2] "K" (-ENOSYS), "d" (__func),
		  "a" (__param)
		: "cc", "memory"
	);
343
	if (ret >= 0 && func & CRYPT_S390_FUNC_MASK){
Linus Torvalds's avatar
Linus Torvalds committed
344
345
346
347
348
349
		ret = src_len - ret;
	}
	return ret;
}

/**
350
 * Tests if a specific crypto function is implemented on the machine.
Linus Torvalds's avatar
Linus Torvalds committed
351
352
353
354
 * @param func:	the function code of the specific function; 0 if op in general
 * @return	1 if func available; 0 if func or op in general not available
 */
static inline int
355
crypt_s390_func_available(int func)
Linus Torvalds's avatar
Linus Torvalds committed
356
357
358
{
	int ret;

359
	struct crypt_s390_query_status status = {
Linus Torvalds's avatar
Linus Torvalds committed
360
361
362
		.high = 0,
		.low = 0
	};
363
364
365
	switch (func & CRYPT_S390_OP_MASK){
		case CRYPT_S390_KM:
			ret = crypt_s390_km(KM_QUERY, &status, NULL, NULL, 0);
Linus Torvalds's avatar
Linus Torvalds committed
366
			break;
367
368
		case CRYPT_S390_KMC:
			ret = crypt_s390_kmc(KMC_QUERY, &status, NULL, NULL, 0);
Linus Torvalds's avatar
Linus Torvalds committed
369
			break;
370
371
		case CRYPT_S390_KIMD:
			ret = crypt_s390_kimd(KIMD_QUERY, &status, NULL, 0);
Linus Torvalds's avatar
Linus Torvalds committed
372
			break;
373
374
		case CRYPT_S390_KLMD:
			ret = crypt_s390_klmd(KLMD_QUERY, &status, NULL, 0);
Linus Torvalds's avatar
Linus Torvalds committed
375
			break;
376
377
		case CRYPT_S390_KMAC:
			ret = crypt_s390_kmac(KMAC_QUERY, &status, NULL, 0);
Linus Torvalds's avatar
Linus Torvalds committed
378
379
380
381
382
383
			break;
		default:
			ret = 0;
			return ret;
	}
	if (ret >= 0){
384
		func &= CRYPT_S390_FUNC_MASK;
Linus Torvalds's avatar
Linus Torvalds committed
385
386
387
388
389
390
391
392
393
394
395
396
		func &= 0x7f; //mask modifier bit
		if (func < 64){
			ret = (status.high >> (64 - func - 1)) & 0x1;
		} else {
			ret = (status.low >> (128 - func - 1)) & 0x1;
		}
	} else {
		ret = 0;
	}
	return ret;
}

397
#endif // _CRYPTO_ARCH_S390_CRYPT_S390_H