4 * Support for z990 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_Z990_H
16 #define _CRYPTO_ARCH_S390_CRYPT_Z990_H
18 #include <asm/errno.h>
20 #define CRYPT_Z990_OP_MASK 0xFF00
21 #define CRYPT_Z990_FUNC_MASK 0x00FF
24 /*z990 cryptographic operations*/
25 enum crypt_z990_operations
{
26 CRYPT_Z990_KM
= 0x0100,
27 CRYPT_Z990_KMC
= 0x0200,
28 CRYPT_Z990_KIMD
= 0x0300,
29 CRYPT_Z990_KLMD
= 0x0400,
30 CRYPT_Z990_KMAC
= 0x0500
33 /*function codes for KM (CIPHER MESSAGE) instruction*/
34 enum crypt_z990_km_func
{
35 KM_QUERY
= CRYPT_Z990_KM
| 0,
36 KM_DEA_ENCRYPT
= CRYPT_Z990_KM
| 1,
37 KM_DEA_DECRYPT
= CRYPT_Z990_KM
| 1 | 0x80, //modifier bit->decipher
38 KM_TDEA_128_ENCRYPT
= CRYPT_Z990_KM
| 2,
39 KM_TDEA_128_DECRYPT
= CRYPT_Z990_KM
| 2 | 0x80,
40 KM_TDEA_192_ENCRYPT
= CRYPT_Z990_KM
| 3,
41 KM_TDEA_192_DECRYPT
= CRYPT_Z990_KM
| 3 | 0x80,
44 /*function codes for KMC (CIPHER MESSAGE WITH CHAINING) instruction*/
45 enum crypt_z990_kmc_func
{
46 KMC_QUERY
= CRYPT_Z990_KMC
| 0,
47 KMC_DEA_ENCRYPT
= CRYPT_Z990_KMC
| 1,
48 KMC_DEA_DECRYPT
= CRYPT_Z990_KMC
| 1 | 0x80, //modifier bit->decipher
49 KMC_TDEA_128_ENCRYPT
= CRYPT_Z990_KMC
| 2,
50 KMC_TDEA_128_DECRYPT
= CRYPT_Z990_KMC
| 2 | 0x80,
51 KMC_TDEA_192_ENCRYPT
= CRYPT_Z990_KMC
| 3,
52 KMC_TDEA_192_DECRYPT
= CRYPT_Z990_KMC
| 3 | 0x80,
55 /*function codes for KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST) instruction*/
56 enum crypt_z990_kimd_func
{
57 KIMD_QUERY
= CRYPT_Z990_KIMD
| 0,
58 KIMD_SHA_1
= CRYPT_Z990_KIMD
| 1,
61 /*function codes for KLMD (COMPUTE LAST MESSAGE DIGEST) instruction*/
62 enum crypt_z990_klmd_func
{
63 KLMD_QUERY
= CRYPT_Z990_KLMD
| 0,
64 KLMD_SHA_1
= CRYPT_Z990_KLMD
| 1,
67 /*function codes for KMAC (COMPUTE MESSAGE AUTHENTICATION CODE) instruction*/
68 enum crypt_z990_kmac_func
{
69 KMAC_QUERY
= CRYPT_Z990_KMAC
| 0,
70 KMAC_DEA
= CRYPT_Z990_KMAC
| 1,
71 KMAC_TDEA_128
= CRYPT_Z990_KMAC
| 2,
72 KMAC_TDEA_192
= CRYPT_Z990_KMAC
| 3
75 /*status word for z990 crypto instructions' QUERY functions*/
76 struct crypt_z990_query_status
{
82 * Standard fixup and ex_table sections for crypt_z990 inline functions.
83 * label 0: the z990 crypto operation
84 * label 1: just after 1 to catch illegal operation exception on non-z990
85 * label 6: the return point after fixup
86 * label 7: set error value if exception _in_ crypto operation
87 * label 8: set error value if illegal operation exception
88 * [ret] is the variable to receive the error code
89 * [ERR] is the error code value
92 #define __crypt_z990_fixup \
93 ".section .fixup,\"ax\" \n" \
94 "7: lhi %0,%h[e1] \n" \
97 "8: lhi %0,%h[e2] \n" \
103 ".section __ex_table,\"a\" \n" \
108 #else /* __s390x__ */
109 #define __crypt_z990_fixup \
110 ".section .fixup,\"ax\" \n" \
111 "7: lhi %0,%h[e1] \n" \
113 "8: lhi %0,%h[e2] \n" \
116 ".section __ex_table,\"a\" \n" \
121 #endif /* __s390x__ */
124 * Standard code for setting the result of z990 crypto instructions.
125 * %0: the register which will receive the result
126 * [result]: the register containing the result (e.g. second operand length
127 * to compute number of processed bytes].
130 #define __crypt_z990_set_result \
131 " lr %0,%[result] \n"
132 #else /* __s390x__ */
133 #define __crypt_z990_set_result \
134 " lgr %0,%[result] \n"
138 * Executes the KM (CIPHER MESSAGE) operation of the z990 CPU.
139 * @param func: the function code passed to KM; see crypt_z990_km_func
140 * @param param: address of parameter block; see POP for details on each func
141 * @param dest: address of destination memory area
142 * @param src: address of source memory area
143 * @param src_len: length of src operand in bytes
144 * @returns < zero for failure, 0 for the query func, number of processed bytes
145 * for encryption/decryption funcs
148 crypt_z990_km(long func
, void* param
, u8
* dest
, const u8
* src
, long src_len
)
150 register long __func
asm("0") = func
& CRYPT_Z990_FUNC_MASK
;
151 register void* __param
asm("1") = param
;
152 register u8
* __dest
asm("4") = dest
;
153 register const u8
* __src
asm("2") = src
;
154 register long __src_len
asm("3") = src_len
;
158 __asm__
__volatile__ (
159 "0: .insn rre,0xB92E0000,%1,%2 \n" //KM opcode
160 "1: brc 1,0b \n" //handle partial completion
161 __crypt_z990_set_result
164 : "+d" (ret
), "+a" (__dest
), "+a" (__src
),
165 [result
] "+d" (__src_len
)
166 : [e1
] "K" (-EFAULT
), [e2
] "K" (-ENOSYS
), "d" (__func
),
170 if (ret
>= 0 && func
& CRYPT_Z990_FUNC_MASK
){
177 * Executes the KMC (CIPHER MESSAGE WITH CHAINING) operation of the z990 CPU.
178 * @param func: the function code passed to KM; see crypt_z990_kmc_func
179 * @param param: address of parameter block; see POP for details on each func
180 * @param dest: address of destination memory area
181 * @param src: address of source memory area
182 * @param src_len: length of src operand in bytes
183 * @returns < zero for failure, 0 for the query func, number of processed bytes
184 * for encryption/decryption funcs
187 crypt_z990_kmc(long func
, void* param
, u8
* dest
, const u8
* src
, long src_len
)
189 register long __func
asm("0") = func
& CRYPT_Z990_FUNC_MASK
;
190 register void* __param
asm("1") = param
;
191 register u8
* __dest
asm("4") = dest
;
192 register const u8
* __src
asm("2") = src
;
193 register long __src_len
asm("3") = src_len
;
197 __asm__
__volatile__ (
198 "0: .insn rre,0xB92F0000,%1,%2 \n" //KMC opcode
199 "1: brc 1,0b \n" //handle partial completion
200 __crypt_z990_set_result
203 : "+d" (ret
), "+a" (__dest
), "+a" (__src
),
204 [result
] "+d" (__src_len
)
205 : [e1
] "K" (-EFAULT
), [e2
] "K" (-ENOSYS
), "d" (__func
),
209 if (ret
>= 0 && func
& CRYPT_Z990_FUNC_MASK
){
216 * Executes the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST) operation
218 * @param func: the function code passed to KM; see crypt_z990_kimd_func
219 * @param param: address of parameter block; see POP for details on each func
220 * @param src: address of source memory area
221 * @param src_len: length of src operand in bytes
222 * @returns < zero for failure, 0 for the query func, number of processed bytes
226 crypt_z990_kimd(long func
, void* param
, const u8
* src
, long src_len
)
228 register long __func
asm("0") = func
& CRYPT_Z990_FUNC_MASK
;
229 register void* __param
asm("1") = param
;
230 register const u8
* __src
asm("2") = src
;
231 register long __src_len
asm("3") = src_len
;
235 __asm__
__volatile__ (
236 "0: .insn rre,0xB93E0000,%1,%1 \n" //KIMD opcode
237 "1: brc 1,0b \n" /*handle partical completion of kimd*/
238 __crypt_z990_set_result
241 : "+d" (ret
), "+a" (__src
), [result
] "+d" (__src_len
)
242 : [e1
] "K" (-EFAULT
), [e2
] "K" (-ENOSYS
), "d" (__func
),
246 if (ret
>= 0 && (func
& CRYPT_Z990_FUNC_MASK
)){
253 * Executes the KLMD (COMPUTE LAST MESSAGE DIGEST) operation of the z990 CPU.
254 * @param func: the function code passed to KM; see crypt_z990_klmd_func
255 * @param param: address of parameter block; see POP for details on each func
256 * @param src: address of source memory area
257 * @param src_len: length of src operand in bytes
258 * @returns < zero for failure, 0 for the query func, number of processed bytes
262 crypt_z990_klmd(long func
, void* param
, const u8
* src
, long src_len
)
264 register long __func
asm("0") = func
& CRYPT_Z990_FUNC_MASK
;
265 register void* __param
asm("1") = param
;
266 register const u8
* __src
asm("2") = src
;
267 register long __src_len
asm("3") = src_len
;
271 __asm__
__volatile__ (
272 "0: .insn rre,0xB93F0000,%1,%1 \n" //KLMD opcode
273 "1: brc 1,0b \n" /*handle partical completion of klmd*/
274 __crypt_z990_set_result
277 : "+d" (ret
), "+a" (__src
), [result
] "+d" (__src_len
)
278 : [e1
] "K" (-EFAULT
), [e2
] "K" (-ENOSYS
), "d" (__func
),
282 if (ret
>= 0 && func
& CRYPT_Z990_FUNC_MASK
){
289 * Executes the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE) operation
291 * @param func: the function code passed to KM; see crypt_z990_klmd_func
292 * @param param: address of parameter block; see POP for details on each func
293 * @param src: address of source memory area
294 * @param src_len: length of src operand in bytes
295 * @returns < zero for failure, 0 for the query func, number of processed bytes
299 crypt_z990_kmac(long func
, void* param
, const u8
* src
, long src_len
)
301 register long __func
asm("0") = func
& CRYPT_Z990_FUNC_MASK
;
302 register void* __param
asm("1") = param
;
303 register const u8
* __src
asm("2") = src
;
304 register long __src_len
asm("3") = src_len
;
308 __asm__
__volatile__ (
309 "0: .insn rre,0xB91E0000,%5,%5 \n" //KMAC opcode
310 "1: brc 1,0b \n" /*handle partical completion of klmd*/
311 __crypt_z990_set_result
314 : "+d" (ret
), "+a" (__src
), [result
] "+d" (__src_len
)
315 : [e1
] "K" (-EFAULT
), [e2
] "K" (-ENOSYS
), "d" (__func
),
319 if (ret
>= 0 && func
& CRYPT_Z990_FUNC_MASK
){
326 * Tests if a specific z990 crypto function is implemented on the machine.
327 * @param func: the function code of the specific function; 0 if op in general
328 * @return 1 if func available; 0 if func or op in general not available
331 crypt_z990_func_available(int func
)
335 struct crypt_z990_query_status status
= {
339 switch (func
& CRYPT_Z990_OP_MASK
){
341 ret
= crypt_z990_km(KM_QUERY
, &status
, NULL
, NULL
, 0);
344 ret
= crypt_z990_kmc(KMC_QUERY
, &status
, NULL
, NULL
, 0);
346 case CRYPT_Z990_KIMD
:
347 ret
= crypt_z990_kimd(KIMD_QUERY
, &status
, NULL
, 0);
349 case CRYPT_Z990_KLMD
:
350 ret
= crypt_z990_klmd(KLMD_QUERY
, &status
, NULL
, 0);
352 case CRYPT_Z990_KMAC
:
353 ret
= crypt_z990_kmac(KMAC_QUERY
, &status
, NULL
, 0);
360 func
&= CRYPT_Z990_FUNC_MASK
;
361 func
&= 0x7f; //mask modifier bit
363 ret
= (status
.high
>> (64 - func
- 1)) & 0x1;
365 ret
= (status
.low
>> (128 - func
- 1)) & 0x1;
374 #endif // _CRYPTO_ARCH_S390_CRYPT_Z990_H