4 * Support for s390 cryptographic instructions.
6 * Copyright (C) 2003 IBM Deutschland GmbH, IBM Corporation
7 * Author(s): Thomas Spatzier (tspat@de.ibm.com)
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
15 #ifndef _CRYPTO_ARCH_S390_CRYPT_S390_H
16 #define _CRYPTO_ARCH_S390_CRYPT_S390_H
18 #include <asm/errno.h>
20 #define CRYPT_S390_OP_MASK 0xFF00
21 #define CRYPT_S390_FUNC_MASK 0x00FF
23 #define CRYPT_S390_PRIORITY 300
24 #define CRYPT_S390_COMPOSITE_PRIORITY 400
26 /* s930 cryptographic operations */
27 enum crypt_s390_operations
{
28 CRYPT_S390_KM
= 0x0100,
29 CRYPT_S390_KMC
= 0x0200,
30 CRYPT_S390_KIMD
= 0x0300,
31 CRYPT_S390_KLMD
= 0x0400,
32 CRYPT_S390_KMAC
= 0x0500
35 /* function codes for KM (CIPHER MESSAGE) instruction
36 * 0x80 is the decipher modifier bit
38 enum crypt_s390_km_func
{
39 KM_QUERY
= CRYPT_S390_KM
| 0x0,
40 KM_DEA_ENCRYPT
= CRYPT_S390_KM
| 0x1,
41 KM_DEA_DECRYPT
= CRYPT_S390_KM
| 0x1 | 0x80,
42 KM_TDEA_128_ENCRYPT
= CRYPT_S390_KM
| 0x2,
43 KM_TDEA_128_DECRYPT
= CRYPT_S390_KM
| 0x2 | 0x80,
44 KM_TDEA_192_ENCRYPT
= CRYPT_S390_KM
| 0x3,
45 KM_TDEA_192_DECRYPT
= CRYPT_S390_KM
| 0x3 | 0x80,
46 KM_AES_128_ENCRYPT
= CRYPT_S390_KM
| 0x12,
47 KM_AES_128_DECRYPT
= CRYPT_S390_KM
| 0x12 | 0x80,
48 KM_AES_192_ENCRYPT
= CRYPT_S390_KM
| 0x13,
49 KM_AES_192_DECRYPT
= CRYPT_S390_KM
| 0x13 | 0x80,
50 KM_AES_256_ENCRYPT
= CRYPT_S390_KM
| 0x14,
51 KM_AES_256_DECRYPT
= CRYPT_S390_KM
| 0x14 | 0x80,
54 /* function codes for KMC (CIPHER MESSAGE WITH CHAINING)
57 enum crypt_s390_kmc_func
{
58 KMC_QUERY
= CRYPT_S390_KMC
| 0x0,
59 KMC_DEA_ENCRYPT
= CRYPT_S390_KMC
| 0x1,
60 KMC_DEA_DECRYPT
= CRYPT_S390_KMC
| 0x1 | 0x80,
61 KMC_TDEA_128_ENCRYPT
= CRYPT_S390_KMC
| 0x2,
62 KMC_TDEA_128_DECRYPT
= CRYPT_S390_KMC
| 0x2 | 0x80,
63 KMC_TDEA_192_ENCRYPT
= CRYPT_S390_KMC
| 0x3,
64 KMC_TDEA_192_DECRYPT
= CRYPT_S390_KMC
| 0x3 | 0x80,
65 KMC_AES_128_ENCRYPT
= CRYPT_S390_KMC
| 0x12,
66 KMC_AES_128_DECRYPT
= CRYPT_S390_KMC
| 0x12 | 0x80,
67 KMC_AES_192_ENCRYPT
= CRYPT_S390_KMC
| 0x13,
68 KMC_AES_192_DECRYPT
= CRYPT_S390_KMC
| 0x13 | 0x80,
69 KMC_AES_256_ENCRYPT
= CRYPT_S390_KMC
| 0x14,
70 KMC_AES_256_DECRYPT
= CRYPT_S390_KMC
| 0x14 | 0x80,
73 /* function codes for KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST)
76 enum crypt_s390_kimd_func
{
77 KIMD_QUERY
= CRYPT_S390_KIMD
| 0,
78 KIMD_SHA_1
= CRYPT_S390_KIMD
| 1,
79 KIMD_SHA_256
= CRYPT_S390_KIMD
| 2,
82 /* function codes for KLMD (COMPUTE LAST MESSAGE DIGEST)
85 enum crypt_s390_klmd_func
{
86 KLMD_QUERY
= CRYPT_S390_KLMD
| 0,
87 KLMD_SHA_1
= CRYPT_S390_KLMD
| 1,
88 KLMD_SHA_256
= CRYPT_S390_KLMD
| 2,
91 /* function codes for KMAC (COMPUTE MESSAGE AUTHENTICATION CODE)
94 enum crypt_s390_kmac_func
{
95 KMAC_QUERY
= CRYPT_S390_KMAC
| 0,
96 KMAC_DEA
= CRYPT_S390_KMAC
| 1,
97 KMAC_TDEA_128
= CRYPT_S390_KMAC
| 2,
98 KMAC_TDEA_192
= CRYPT_S390_KMAC
| 3
101 /* status word for s390 crypto instructions' QUERY functions */
102 struct crypt_s390_query_status
{
108 * Standard fixup and ex_table sections for crypt_s390 inline functions.
109 * label 0: the s390 crypto operation
110 * label 1: just after 1 to catch illegal operation exception
111 * (unsupported model)
112 * label 6: the return point after fixup
113 * label 7: set error value if exception _in_ crypto operation
114 * label 8: set error value if illegal operation exception
115 * [ret] is the variable to receive the error code
116 * [ERR] is the error code value
119 #define __crypt_s390_fixup \
120 ".section .fixup,\"ax\" \n" \
121 "7: lhi %0,%h[e1] \n" \
124 "8: lhi %0,%h[e2] \n" \
130 ".section __ex_table,\"a\" \n" \
135 #else /* CONFIG_64BIT */
136 #define __crypt_s390_fixup \
137 ".section .fixup,\"ax\" \n" \
138 "7: lhi %0,%h[e1] \n" \
140 "8: lhi %0,%h[e2] \n" \
143 ".section __ex_table,\"a\" \n" \
148 #endif /* CONFIG_64BIT */
151 * Standard code for setting the result of s390 crypto instructions.
152 * %0: the register which will receive the result
153 * [result]: the register containing the result (e.g. second operand length
154 * to compute number of processed bytes].
157 #define __crypt_s390_set_result \
158 " lr %0,%[result] \n"
159 #else /* CONFIG_64BIT */
160 #define __crypt_s390_set_result \
161 " lgr %0,%[result] \n"
165 * Executes the KM (CIPHER MESSAGE) operation of the CPU.
166 * @param func: the function code passed to KM; see crypt_s390_km_func
167 * @param param: address of parameter block; see POP for details on each func
168 * @param dest: address of destination memory area
169 * @param src: address of source memory area
170 * @param src_len: length of src operand in bytes
171 * @returns < zero for failure, 0 for the query func, number of processed bytes
172 * for encryption/decryption funcs
175 crypt_s390_km(long func
, void* param
, u8
* dest
, const u8
* src
, long src_len
)
177 register long __func
asm("0") = func
& CRYPT_S390_FUNC_MASK
;
178 register void* __param
asm("1") = param
;
179 register u8
* __dest
asm("4") = dest
;
180 register const u8
* __src
asm("2") = src
;
181 register long __src_len
asm("3") = src_len
;
185 __asm__
__volatile__ (
186 "0: .insn rre,0xB92E0000,%1,%2 \n" /* KM opcode */
187 "1: brc 1,0b \n" /* handle partial completion */
188 __crypt_s390_set_result
191 : "+d" (ret
), "+a" (__dest
), "+a" (__src
),
192 [result
] "+d" (__src_len
)
193 : [e1
] "K" (-EFAULT
), [e2
] "K" (-ENOSYS
), "d" (__func
),
197 if (ret
>= 0 && func
& CRYPT_S390_FUNC_MASK
){
204 * Executes the KMC (CIPHER MESSAGE WITH CHAINING) operation of the CPU.
205 * @param func: the function code passed to KM; see crypt_s390_kmc_func
206 * @param param: address of parameter block; see POP for details on each func
207 * @param dest: address of destination memory area
208 * @param src: address of source memory area
209 * @param src_len: length of src operand in bytes
210 * @returns < zero for failure, 0 for the query func, number of processed bytes
211 * for encryption/decryption funcs
214 crypt_s390_kmc(long func
, void* param
, u8
* dest
, const u8
* src
, long src_len
)
216 register long __func
asm("0") = func
& CRYPT_S390_FUNC_MASK
;
217 register void* __param
asm("1") = param
;
218 register u8
* __dest
asm("4") = dest
;
219 register const u8
* __src
asm("2") = src
;
220 register long __src_len
asm("3") = src_len
;
224 __asm__
__volatile__ (
225 "0: .insn rre,0xB92F0000,%1,%2 \n" /* KMC opcode */
226 "1: brc 1,0b \n" /* handle partial completion */
227 __crypt_s390_set_result
230 : "+d" (ret
), "+a" (__dest
), "+a" (__src
),
231 [result
] "+d" (__src_len
)
232 : [e1
] "K" (-EFAULT
), [e2
] "K" (-ENOSYS
), "d" (__func
),
236 if (ret
>= 0 && func
& CRYPT_S390_FUNC_MASK
){
243 * Executes the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST) operation
245 * @param func: the function code passed to KM; see crypt_s390_kimd_func
246 * @param param: address of parameter block; see POP for details on each func
247 * @param src: address of source memory area
248 * @param src_len: length of src operand in bytes
249 * @returns < zero for failure, 0 for the query func, number of processed bytes
253 crypt_s390_kimd(long func
, void* param
, const u8
* src
, long src_len
)
255 register long __func
asm("0") = func
& CRYPT_S390_FUNC_MASK
;
256 register void* __param
asm("1") = param
;
257 register const u8
* __src
asm("2") = src
;
258 register long __src_len
asm("3") = src_len
;
262 __asm__
__volatile__ (
263 "0: .insn rre,0xB93E0000,%1,%1 \n" /* KIMD opcode */
264 "1: brc 1,0b \n" /* handle partical completion */
265 __crypt_s390_set_result
268 : "+d" (ret
), "+a" (__src
), [result
] "+d" (__src_len
)
269 : [e1
] "K" (-EFAULT
), [e2
] "K" (-ENOSYS
), "d" (__func
),
273 if (ret
>= 0 && (func
& CRYPT_S390_FUNC_MASK
)){
280 * Executes the KLMD (COMPUTE LAST MESSAGE DIGEST) operation of the CPU.
281 * @param func: the function code passed to KM; see crypt_s390_klmd_func
282 * @param param: address of parameter block; see POP for details on each func
283 * @param src: address of source memory area
284 * @param src_len: length of src operand in bytes
285 * @returns < zero for failure, 0 for the query func, number of processed bytes
289 crypt_s390_klmd(long func
, void* param
, const u8
* src
, long src_len
)
291 register long __func
asm("0") = func
& CRYPT_S390_FUNC_MASK
;
292 register void* __param
asm("1") = param
;
293 register const u8
* __src
asm("2") = src
;
294 register long __src_len
asm("3") = src_len
;
298 __asm__
__volatile__ (
299 "0: .insn rre,0xB93F0000,%1,%1 \n" /* KLMD opcode */
300 "1: brc 1,0b \n" /* handle partical completion */
301 __crypt_s390_set_result
304 : "+d" (ret
), "+a" (__src
), [result
] "+d" (__src_len
)
305 : [e1
] "K" (-EFAULT
), [e2
] "K" (-ENOSYS
), "d" (__func
),
309 if (ret
>= 0 && func
& CRYPT_S390_FUNC_MASK
){
316 * Executes the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE) operation
318 * @param func: the function code passed to KM; see crypt_s390_klmd_func
319 * @param param: address of parameter block; see POP for details on each func
320 * @param src: address of source memory area
321 * @param src_len: length of src operand in bytes
322 * @returns < zero for failure, 0 for the query func, number of processed bytes
326 crypt_s390_kmac(long func
, void* param
, const u8
* src
, long src_len
)
328 register long __func
asm("0") = func
& CRYPT_S390_FUNC_MASK
;
329 register void* __param
asm("1") = param
;
330 register const u8
* __src
asm("2") = src
;
331 register long __src_len
asm("3") = src_len
;
335 __asm__
__volatile__ (
336 "0: .insn rre,0xB91E0000,%5,%5 \n" /* KMAC opcode */
337 "1: brc 1,0b \n" /* handle partical completion */
338 __crypt_s390_set_result
341 : "+d" (ret
), "+a" (__src
), [result
] "+d" (__src_len
)
342 : [e1
] "K" (-EFAULT
), [e2
] "K" (-ENOSYS
), "d" (__func
),
346 if (ret
>= 0 && func
& CRYPT_S390_FUNC_MASK
){
353 * Tests if a specific crypto function is implemented on the machine.
354 * @param func: the function code of the specific function; 0 if op in general
355 * @return 1 if func available; 0 if func or op in general not available
358 crypt_s390_func_available(int func
)
362 struct crypt_s390_query_status status
= {
366 switch (func
& CRYPT_S390_OP_MASK
){
368 ret
= crypt_s390_km(KM_QUERY
, &status
, NULL
, NULL
, 0);
371 ret
= crypt_s390_kmc(KMC_QUERY
, &status
, NULL
, NULL
, 0);
373 case CRYPT_S390_KIMD
:
374 ret
= crypt_s390_kimd(KIMD_QUERY
, &status
, NULL
, 0);
376 case CRYPT_S390_KLMD
:
377 ret
= crypt_s390_klmd(KLMD_QUERY
, &status
, NULL
, 0);
379 case CRYPT_S390_KMAC
:
380 ret
= crypt_s390_kmac(KMAC_QUERY
, &status
, NULL
, 0);
387 func
&= CRYPT_S390_FUNC_MASK
;
388 func
&= 0x7f; //mask modifier bit
390 ret
= (status
.high
>> (64 - func
- 1)) & 0x1;
392 ret
= (status
.low
>> (128 - func
- 1)) & 0x1;
400 #endif // _CRYPTO_ARCH_S390_CRYPT_S390_H