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
25 /* s930 cryptographic operations */
26 enum crypt_s390_operations {
27 CRYPT_S390_KM = 0x0100,
28 CRYPT_S390_KMC = 0x0200,
29 CRYPT_S390_KIMD = 0x0300,
30 CRYPT_S390_KLMD = 0x0400,
31 CRYPT_S390_KMAC = 0x0500
34 /* function codes for KM (CIPHER MESSAGE) instruction
35 * 0x80 is the decipher modifier bit
37 enum crypt_s390_km_func {
38 KM_QUERY = CRYPT_S390_KM | 0x0,
39 KM_DEA_ENCRYPT = CRYPT_S390_KM | 0x1,
40 KM_DEA_DECRYPT = CRYPT_S390_KM | 0x1 | 0x80,
41 KM_TDEA_128_ENCRYPT = CRYPT_S390_KM | 0x2,
42 KM_TDEA_128_DECRYPT = CRYPT_S390_KM | 0x2 | 0x80,
43 KM_TDEA_192_ENCRYPT = CRYPT_S390_KM | 0x3,
44 KM_TDEA_192_DECRYPT = CRYPT_S390_KM | 0x3 | 0x80,
45 KM_AES_128_ENCRYPT = CRYPT_S390_KM | 0x12,
46 KM_AES_128_DECRYPT = CRYPT_S390_KM | 0x12 | 0x80,
47 KM_AES_192_ENCRYPT = CRYPT_S390_KM | 0x13,
48 KM_AES_192_DECRYPT = CRYPT_S390_KM | 0x13 | 0x80,
49 KM_AES_256_ENCRYPT = CRYPT_S390_KM | 0x14,
50 KM_AES_256_DECRYPT = CRYPT_S390_KM | 0x14 | 0x80,
53 /* function codes for KMC (CIPHER MESSAGE WITH CHAINING)
56 enum crypt_s390_kmc_func {
57 KMC_QUERY = CRYPT_S390_KMC | 0x0,
58 KMC_DEA_ENCRYPT = CRYPT_S390_KMC | 0x1,
59 KMC_DEA_DECRYPT = CRYPT_S390_KMC | 0x1 | 0x80,
60 KMC_TDEA_128_ENCRYPT = CRYPT_S390_KMC | 0x2,
61 KMC_TDEA_128_DECRYPT = CRYPT_S390_KMC | 0x2 | 0x80,
62 KMC_TDEA_192_ENCRYPT = CRYPT_S390_KMC | 0x3,
63 KMC_TDEA_192_DECRYPT = CRYPT_S390_KMC | 0x3 | 0x80,
64 KMC_AES_128_ENCRYPT = CRYPT_S390_KMC | 0x12,
65 KMC_AES_128_DECRYPT = CRYPT_S390_KMC | 0x12 | 0x80,
66 KMC_AES_192_ENCRYPT = CRYPT_S390_KMC | 0x13,
67 KMC_AES_192_DECRYPT = CRYPT_S390_KMC | 0x13 | 0x80,
68 KMC_AES_256_ENCRYPT = CRYPT_S390_KMC | 0x14,
69 KMC_AES_256_DECRYPT = CRYPT_S390_KMC | 0x14 | 0x80,
72 /* function codes for KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST)
75 enum crypt_s390_kimd_func {
76 KIMD_QUERY = CRYPT_S390_KIMD | 0,
77 KIMD_SHA_1 = CRYPT_S390_KIMD | 1,
78 KIMD_SHA_256 = CRYPT_S390_KIMD | 2,
81 /* function codes for KLMD (COMPUTE LAST MESSAGE DIGEST)
84 enum crypt_s390_klmd_func {
85 KLMD_QUERY = CRYPT_S390_KLMD | 0,
86 KLMD_SHA_1 = CRYPT_S390_KLMD | 1,
87 KLMD_SHA_256 = CRYPT_S390_KLMD | 2,
90 /* function codes for KMAC (COMPUTE MESSAGE AUTHENTICATION CODE)
93 enum crypt_s390_kmac_func {
94 KMAC_QUERY = CRYPT_S390_KMAC | 0,
95 KMAC_DEA = CRYPT_S390_KMAC | 1,
96 KMAC_TDEA_128 = CRYPT_S390_KMAC | 2,
97 KMAC_TDEA_192 = CRYPT_S390_KMAC | 3
100 /* status word for s390 crypto instructions' QUERY functions */
101 struct crypt_s390_query_status {
107 * Standard fixup and ex_table sections for crypt_s390 inline functions.
108 * label 0: the s390 crypto operation
109 * label 1: just after 1 to catch illegal operation exception
110 * (unsupported model)
111 * label 6: the return point after fixup
112 * label 7: set error value if exception _in_ crypto operation
113 * label 8: set error value if illegal operation exception
114 * [ret] is the variable to receive the error code
115 * [ERR] is the error code value
118 #define __crypt_s390_fixup \
119 ".section .fixup,\"ax\" \n" \
120 "7: lhi %0,%h[e1] \n" \
123 "8: lhi %0,%h[e2] \n" \
129 ".section __ex_table,\"a\" \n" \
134 #else /* CONFIG_64BIT */
135 #define __crypt_s390_fixup \
136 ".section .fixup,\"ax\" \n" \
137 "7: lhi %0,%h[e1] \n" \
139 "8: lhi %0,%h[e2] \n" \
142 ".section __ex_table,\"a\" \n" \
147 #endif /* CONFIG_64BIT */
150 * Standard code for setting the result of s390 crypto instructions.
151 * %0: the register which will receive the result
152 * [result]: the register containing the result (e.g. second operand length
153 * to compute number of processed bytes].
156 #define __crypt_s390_set_result \
157 " lr %0,%[result] \n"
158 #else /* CONFIG_64BIT */
159 #define __crypt_s390_set_result \
160 " lgr %0,%[result] \n"
164 * Executes the KM (CIPHER MESSAGE) operation of the CPU.
165 * @param func: the function code passed to KM; see crypt_s390_km_func
166 * @param param: address of parameter block; see POP for details on each func
167 * @param dest: address of destination memory area
168 * @param src: address of source memory area
169 * @param src_len: length of src operand in bytes
170 * @returns < zero for failure, 0 for the query func, number of processed bytes
171 * for encryption/decryption funcs
174 crypt_s390_km(long func, void* param, u8* dest, const u8* src, long src_len)
176 register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
177 register void* __param asm("1") = param;
178 register u8* __dest asm("4") = dest;
179 register const u8* __src asm("2") = src;
180 register long __src_len asm("3") = src_len;
184 __asm__ __volatile__ (
185 "0: .insn rre,0xB92E0000,%1,%2 \n" /* KM opcode */
186 "1: brc 1,0b \n" /* handle partial completion */
187 __crypt_s390_set_result
190 : "+d" (ret), "+a" (__dest), "+a" (__src),
191 [result] "+d" (__src_len)
192 : [e1] "K" (-EFAULT), [e2] "K" (-ENOSYS), "d" (__func),
196 if (ret >= 0 && func & CRYPT_S390_FUNC_MASK){
203 * Executes the KMC (CIPHER MESSAGE WITH CHAINING) operation of the CPU.
204 * @param func: the function code passed to KM; see crypt_s390_kmc_func
205 * @param param: address of parameter block; see POP for details on each func
206 * @param dest: address of destination memory area
207 * @param src: address of source memory area
208 * @param src_len: length of src operand in bytes
209 * @returns < zero for failure, 0 for the query func, number of processed bytes
210 * for encryption/decryption funcs
213 crypt_s390_kmc(long func, void* param, u8* dest, const u8* src, long src_len)
215 register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
216 register void* __param asm("1") = param;
217 register u8* __dest asm("4") = dest;
218 register const u8* __src asm("2") = src;
219 register long __src_len asm("3") = src_len;
223 __asm__ __volatile__ (
224 "0: .insn rre,0xB92F0000,%1,%2 \n" /* KMC opcode */
225 "1: brc 1,0b \n" /* handle partial completion */
226 __crypt_s390_set_result
229 : "+d" (ret), "+a" (__dest), "+a" (__src),
230 [result] "+d" (__src_len)
231 : [e1] "K" (-EFAULT), [e2] "K" (-ENOSYS), "d" (__func),
235 if (ret >= 0 && func & CRYPT_S390_FUNC_MASK){
242 * Executes the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST) operation
244 * @param func: the function code passed to KM; see crypt_s390_kimd_func
245 * @param param: address of parameter block; see POP for details on each func
246 * @param src: address of source memory area
247 * @param src_len: length of src operand in bytes
248 * @returns < zero for failure, 0 for the query func, number of processed bytes
252 crypt_s390_kimd(long func, void* param, const u8* src, long src_len)
254 register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
255 register void* __param asm("1") = param;
256 register const u8* __src asm("2") = src;
257 register long __src_len asm("3") = src_len;
261 __asm__ __volatile__ (
262 "0: .insn rre,0xB93E0000,%1,%1 \n" /* KIMD opcode */
263 "1: brc 1,0b \n" /* handle partical completion */
264 __crypt_s390_set_result
267 : "+d" (ret), "+a" (__src), [result] "+d" (__src_len)
268 : [e1] "K" (-EFAULT), [e2] "K" (-ENOSYS), "d" (__func),
272 if (ret >= 0 && (func & CRYPT_S390_FUNC_MASK)){
279 * Executes the KLMD (COMPUTE LAST MESSAGE DIGEST) operation of the CPU.
280 * @param func: the function code passed to KM; see crypt_s390_klmd_func
281 * @param param: address of parameter block; see POP for details on each func
282 * @param src: address of source memory area
283 * @param src_len: length of src operand in bytes
284 * @returns < zero for failure, 0 for the query func, number of processed bytes
288 crypt_s390_klmd(long func, void* param, const u8* src, long src_len)
290 register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
291 register void* __param asm("1") = param;
292 register const u8* __src asm("2") = src;
293 register long __src_len asm("3") = src_len;
297 __asm__ __volatile__ (
298 "0: .insn rre,0xB93F0000,%1,%1 \n" /* KLMD opcode */
299 "1: brc 1,0b \n" /* handle partical completion */
300 __crypt_s390_set_result
303 : "+d" (ret), "+a" (__src), [result] "+d" (__src_len)
304 : [e1] "K" (-EFAULT), [e2] "K" (-ENOSYS), "d" (__func),
308 if (ret >= 0 && func & CRYPT_S390_FUNC_MASK){
315 * Executes the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE) operation
317 * @param func: the function code passed to KM; see crypt_s390_klmd_func
318 * @param param: address of parameter block; see POP for details on each func
319 * @param src: address of source memory area
320 * @param src_len: length of src operand in bytes
321 * @returns < zero for failure, 0 for the query func, number of processed bytes
325 crypt_s390_kmac(long func, void* param, const u8* src, long src_len)
327 register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
328 register void* __param asm("1") = param;
329 register const u8* __src asm("2") = src;
330 register long __src_len asm("3") = src_len;
334 __asm__ __volatile__ (
335 "0: .insn rre,0xB91E0000,%5,%5 \n" /* KMAC opcode */
336 "1: brc 1,0b \n" /* handle partical completion */
337 __crypt_s390_set_result
340 : "+d" (ret), "+a" (__src), [result] "+d" (__src_len)
341 : [e1] "K" (-EFAULT), [e2] "K" (-ENOSYS), "d" (__func),
345 if (ret >= 0 && func & CRYPT_S390_FUNC_MASK){
352 * Tests if a specific crypto function is implemented on the machine.
353 * @param func: the function code of the specific function; 0 if op in general
354 * @return 1 if func available; 0 if func or op in general not available
357 crypt_s390_func_available(int func)
361 struct crypt_s390_query_status status = {
365 switch (func & CRYPT_S390_OP_MASK){
367 ret = crypt_s390_km(KM_QUERY, &status, NULL, NULL, 0);
370 ret = crypt_s390_kmc(KMC_QUERY, &status, NULL, NULL, 0);
372 case CRYPT_S390_KIMD:
373 ret = crypt_s390_kimd(KIMD_QUERY, &status, NULL, 0);
375 case CRYPT_S390_KLMD:
376 ret = crypt_s390_klmd(KLMD_QUERY, &status, NULL, 0);
378 case CRYPT_S390_KMAC:
379 ret = crypt_s390_kmac(KMAC_QUERY, &status, NULL, 0);
386 func &= CRYPT_S390_FUNC_MASK;
387 func &= 0x7f; //mask modifier bit
389 ret = (status.high >> (64 - func - 1)) & 0x1;
391 ret = (status.low >> (128 - func - 1)) & 0x1;
399 #endif // _CRYPTO_ARCH_S390_CRYPT_S390_H