Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Cryptographic API. | |
3 | * | |
c1e26e1e | 4 | * Support for s390 cryptographic instructions. |
1da177e4 | 5 | * |
86aa9fc2 JG |
6 | * Copyright IBM Corp. 2003,2007 |
7 | * Author(s): Thomas Spatzier | |
8 | * Jan Glauber (jan.glauber@de.ibm.com) | |
1da177e4 LT |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify it | |
11 | * under the terms of the GNU General Public License as published by the Free | |
12 | * Software Foundation; either version 2 of the License, or (at your option) | |
13 | * any later version. | |
14 | * | |
15 | */ | |
c1e26e1e JG |
16 | #ifndef _CRYPTO_ARCH_S390_CRYPT_S390_H |
17 | #define _CRYPTO_ARCH_S390_CRYPT_S390_H | |
1da177e4 LT |
18 | |
19 | #include <asm/errno.h> | |
20 | ||
c1e26e1e JG |
21 | #define CRYPT_S390_OP_MASK 0xFF00 |
22 | #define CRYPT_S390_FUNC_MASK 0x00FF | |
1da177e4 | 23 | |
65b75c36 | 24 | #define CRYPT_S390_PRIORITY 300 |
a9e62fad | 25 | #define CRYPT_S390_COMPOSITE_PRIORITY 400 |
65b75c36 | 26 | |
bccdbdc9 | 27 | /* s390 cryptographic operations */ |
c1e26e1e JG |
28 | enum 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 | |
1da177e4 LT |
34 | }; |
35 | ||
86aa9fc2 JG |
36 | /* |
37 | * function codes for KM (CIPHER MESSAGE) instruction | |
c1e26e1e JG |
38 | * 0x80 is the decipher modifier bit |
39 | */ | |
40 | enum crypt_s390_km_func { | |
bf754ae8 JG |
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, | |
1da177e4 LT |
54 | }; |
55 | ||
86aa9fc2 JG |
56 | /* |
57 | * function codes for KMC (CIPHER MESSAGE WITH CHAINING) | |
c1e26e1e JG |
58 | * instruction |
59 | */ | |
60 | enum crypt_s390_kmc_func { | |
bf754ae8 JG |
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, | |
1b278294 | 74 | KMC_PRNG = CRYPT_S390_KMC | 0x43, |
1da177e4 LT |
75 | }; |
76 | ||
86aa9fc2 JG |
77 | /* |
78 | * function codes for KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST) | |
c1e26e1e JG |
79 | * instruction |
80 | */ | |
81 | enum crypt_s390_kimd_func { | |
82 | KIMD_QUERY = CRYPT_S390_KIMD | 0, | |
83 | KIMD_SHA_1 = CRYPT_S390_KIMD | 1, | |
0a497c17 | 84 | KIMD_SHA_256 = CRYPT_S390_KIMD | 2, |
291dc7c0 | 85 | KIMD_SHA_512 = CRYPT_S390_KIMD | 3, |
1da177e4 LT |
86 | }; |
87 | ||
86aa9fc2 JG |
88 | /* |
89 | * function codes for KLMD (COMPUTE LAST MESSAGE DIGEST) | |
c1e26e1e JG |
90 | * instruction |
91 | */ | |
92 | enum crypt_s390_klmd_func { | |
93 | KLMD_QUERY = CRYPT_S390_KLMD | 0, | |
94 | KLMD_SHA_1 = CRYPT_S390_KLMD | 1, | |
0a497c17 | 95 | KLMD_SHA_256 = CRYPT_S390_KLMD | 2, |
291dc7c0 | 96 | KLMD_SHA_512 = CRYPT_S390_KLMD | 3, |
1da177e4 LT |
97 | }; |
98 | ||
86aa9fc2 JG |
99 | /* |
100 | * function codes for KMAC (COMPUTE MESSAGE AUTHENTICATION CODE) | |
c1e26e1e JG |
101 | * instruction |
102 | */ | |
103 | enum 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 | |
1da177e4 LT |
108 | }; |
109 | ||
86aa9fc2 JG |
110 | /** |
111 | * crypt_s390_km: | |
112 | * @func: the function code passed to KM; see crypt_s390_km_func | |
113 | * @param: address of parameter block; see POP for details on each func | |
114 | * @dest: address of destination memory area | |
115 | * @src: address of source memory area | |
116 | * @src_len: length of src operand in bytes | |
117 | * | |
c1e26e1e | 118 | * Executes the KM (CIPHER MESSAGE) operation of the CPU. |
86aa9fc2 JG |
119 | * |
120 | * Returns -1 for failure, 0 for the query func, number of processed | |
121 | * bytes for encryption/decryption funcs | |
1da177e4 | 122 | */ |
86aa9fc2 JG |
123 | static inline int crypt_s390_km(long func, void *param, |
124 | u8 *dest, const u8 *src, long src_len) | |
1da177e4 | 125 | { |
c1e26e1e | 126 | register long __func asm("0") = func & CRYPT_S390_FUNC_MASK; |
86aa9fc2 JG |
127 | register void *__param asm("1") = param; |
128 | register const u8 *__src asm("2") = src; | |
1da177e4 | 129 | register long __src_len asm("3") = src_len; |
86aa9fc2 | 130 | register u8 *__dest asm("4") = dest; |
1da177e4 LT |
131 | int ret; |
132 | ||
94c12cc7 MS |
133 | asm volatile( |
134 | "0: .insn rre,0xb92e0000,%3,%1 \n" /* KM opcode */ | |
c1e26e1e | 135 | "1: brc 1,0b \n" /* handle partial completion */ |
86aa9fc2 JG |
136 | " la %0,0\n" |
137 | "2:\n" | |
138 | EX_TABLE(0b,2b) EX_TABLE(1b,2b) | |
94c12cc7 | 139 | : "=d" (ret), "+a" (__src), "+d" (__src_len), "+a" (__dest) |
86aa9fc2 | 140 | : "d" (__func), "a" (__param), "0" (-1) : "cc", "memory"); |
94c12cc7 MS |
141 | if (ret < 0) |
142 | return ret; | |
143 | return (func & CRYPT_S390_FUNC_MASK) ? src_len - __src_len : __src_len; | |
1da177e4 LT |
144 | } |
145 | ||
86aa9fc2 JG |
146 | /** |
147 | * crypt_s390_kmc: | |
148 | * @func: the function code passed to KM; see crypt_s390_kmc_func | |
149 | * @param: address of parameter block; see POP for details on each func | |
150 | * @dest: address of destination memory area | |
151 | * @src: address of source memory area | |
152 | * @src_len: length of src operand in bytes | |
153 | * | |
c1e26e1e | 154 | * Executes the KMC (CIPHER MESSAGE WITH CHAINING) operation of the CPU. |
86aa9fc2 JG |
155 | * |
156 | * Returns -1 for failure, 0 for the query func, number of processed | |
157 | * bytes for encryption/decryption funcs | |
1da177e4 | 158 | */ |
86aa9fc2 JG |
159 | static inline int crypt_s390_kmc(long func, void *param, |
160 | u8 *dest, const u8 *src, long src_len) | |
1da177e4 | 161 | { |
c1e26e1e | 162 | register long __func asm("0") = func & CRYPT_S390_FUNC_MASK; |
86aa9fc2 JG |
163 | register void *__param asm("1") = param; |
164 | register const u8 *__src asm("2") = src; | |
1da177e4 | 165 | register long __src_len asm("3") = src_len; |
86aa9fc2 | 166 | register u8 *__dest asm("4") = dest; |
1da177e4 LT |
167 | int ret; |
168 | ||
94c12cc7 MS |
169 | asm volatile( |
170 | "0: .insn rre,0xb92f0000,%3,%1 \n" /* KMC opcode */ | |
c1e26e1e | 171 | "1: brc 1,0b \n" /* handle partial completion */ |
86aa9fc2 JG |
172 | " la %0,0\n" |
173 | "2:\n" | |
174 | EX_TABLE(0b,2b) EX_TABLE(1b,2b) | |
94c12cc7 | 175 | : "=d" (ret), "+a" (__src), "+d" (__src_len), "+a" (__dest) |
86aa9fc2 | 176 | : "d" (__func), "a" (__param), "0" (-1) : "cc", "memory"); |
94c12cc7 MS |
177 | if (ret < 0) |
178 | return ret; | |
179 | return (func & CRYPT_S390_FUNC_MASK) ? src_len - __src_len : __src_len; | |
1da177e4 LT |
180 | } |
181 | ||
86aa9fc2 JG |
182 | /** |
183 | * crypt_s390_kimd: | |
184 | * @func: the function code passed to KM; see crypt_s390_kimd_func | |
185 | * @param: address of parameter block; see POP for details on each func | |
186 | * @src: address of source memory area | |
187 | * @src_len: length of src operand in bytes | |
188 | * | |
1da177e4 | 189 | * Executes the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST) operation |
c1e26e1e | 190 | * of the CPU. |
86aa9fc2 JG |
191 | * |
192 | * Returns -1 for failure, 0 for the query func, number of processed | |
193 | * bytes for digest funcs | |
1da177e4 | 194 | */ |
86aa9fc2 JG |
195 | static inline int crypt_s390_kimd(long func, void *param, |
196 | const u8 *src, long src_len) | |
1da177e4 | 197 | { |
c1e26e1e | 198 | register long __func asm("0") = func & CRYPT_S390_FUNC_MASK; |
86aa9fc2 JG |
199 | register void *__param asm("1") = param; |
200 | register const u8 *__src asm("2") = src; | |
1da177e4 LT |
201 | register long __src_len asm("3") = src_len; |
202 | int ret; | |
203 | ||
94c12cc7 MS |
204 | asm volatile( |
205 | "0: .insn rre,0xb93e0000,%1,%1 \n" /* KIMD opcode */ | |
206 | "1: brc 1,0b \n" /* handle partial completion */ | |
86aa9fc2 JG |
207 | " la %0,0\n" |
208 | "2:\n" | |
209 | EX_TABLE(0b,2b) EX_TABLE(1b,2b) | |
94c12cc7 | 210 | : "=d" (ret), "+a" (__src), "+d" (__src_len) |
86aa9fc2 | 211 | : "d" (__func), "a" (__param), "0" (-1) : "cc", "memory"); |
94c12cc7 MS |
212 | if (ret < 0) |
213 | return ret; | |
214 | return (func & CRYPT_S390_FUNC_MASK) ? src_len - __src_len : __src_len; | |
1da177e4 LT |
215 | } |
216 | ||
86aa9fc2 JG |
217 | /** |
218 | * crypt_s390_klmd: | |
219 | * @func: the function code passed to KM; see crypt_s390_klmd_func | |
220 | * @param: address of parameter block; see POP for details on each func | |
221 | * @src: address of source memory area | |
222 | * @src_len: length of src operand in bytes | |
223 | * | |
c1e26e1e | 224 | * Executes the KLMD (COMPUTE LAST MESSAGE DIGEST) operation of the CPU. |
86aa9fc2 JG |
225 | * |
226 | * Returns -1 for failure, 0 for the query func, number of processed | |
227 | * bytes for digest funcs | |
1da177e4 | 228 | */ |
86aa9fc2 JG |
229 | static inline int crypt_s390_klmd(long func, void *param, |
230 | const u8 *src, long src_len) | |
1da177e4 | 231 | { |
c1e26e1e | 232 | register long __func asm("0") = func & CRYPT_S390_FUNC_MASK; |
86aa9fc2 JG |
233 | register void *__param asm("1") = param; |
234 | register const u8 *__src asm("2") = src; | |
1da177e4 LT |
235 | register long __src_len asm("3") = src_len; |
236 | int ret; | |
237 | ||
94c12cc7 MS |
238 | asm volatile( |
239 | "0: .insn rre,0xb93f0000,%1,%1 \n" /* KLMD opcode */ | |
240 | "1: brc 1,0b \n" /* handle partial completion */ | |
86aa9fc2 JG |
241 | " la %0,0\n" |
242 | "2:\n" | |
243 | EX_TABLE(0b,2b) EX_TABLE(1b,2b) | |
94c12cc7 | 244 | : "=d" (ret), "+a" (__src), "+d" (__src_len) |
86aa9fc2 | 245 | : "d" (__func), "a" (__param), "0" (-1) : "cc", "memory"); |
94c12cc7 MS |
246 | if (ret < 0) |
247 | return ret; | |
248 | return (func & CRYPT_S390_FUNC_MASK) ? src_len - __src_len : __src_len; | |
1da177e4 LT |
249 | } |
250 | ||
86aa9fc2 JG |
251 | /** |
252 | * crypt_s390_kmac: | |
253 | * @func: the function code passed to KM; see crypt_s390_klmd_func | |
254 | * @param: address of parameter block; see POP for details on each func | |
255 | * @src: address of source memory area | |
256 | * @src_len: length of src operand in bytes | |
257 | * | |
1da177e4 | 258 | * Executes the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE) operation |
c1e26e1e | 259 | * of the CPU. |
86aa9fc2 JG |
260 | * |
261 | * Returns -1 for failure, 0 for the query func, number of processed | |
262 | * bytes for digest funcs | |
1da177e4 | 263 | */ |
86aa9fc2 JG |
264 | static inline int crypt_s390_kmac(long func, void *param, |
265 | const u8 *src, long src_len) | |
1da177e4 | 266 | { |
c1e26e1e | 267 | register long __func asm("0") = func & CRYPT_S390_FUNC_MASK; |
86aa9fc2 JG |
268 | register void *__param asm("1") = param; |
269 | register const u8 *__src asm("2") = src; | |
1da177e4 LT |
270 | register long __src_len asm("3") = src_len; |
271 | int ret; | |
272 | ||
94c12cc7 MS |
273 | asm volatile( |
274 | "0: .insn rre,0xb91e0000,%1,%1 \n" /* KLAC opcode */ | |
275 | "1: brc 1,0b \n" /* handle partial completion */ | |
86aa9fc2 JG |
276 | " la %0,0\n" |
277 | "2:\n" | |
278 | EX_TABLE(0b,2b) EX_TABLE(1b,2b) | |
94c12cc7 | 279 | : "=d" (ret), "+a" (__src), "+d" (__src_len) |
86aa9fc2 | 280 | : "d" (__func), "a" (__param), "0" (-1) : "cc", "memory"); |
94c12cc7 MS |
281 | if (ret < 0) |
282 | return ret; | |
283 | return (func & CRYPT_S390_FUNC_MASK) ? src_len - __src_len : __src_len; | |
1da177e4 LT |
284 | } |
285 | ||
286 | /** | |
86aa9fc2 JG |
287 | * crypt_s390_func_available: |
288 | * @func: the function code of the specific function; 0 if op in general | |
289 | * | |
c1e26e1e | 290 | * Tests if a specific crypto function is implemented on the machine. |
86aa9fc2 JG |
291 | * |
292 | * Returns 1 if func available; 0 if func or op in general not available | |
1da177e4 | 293 | */ |
86aa9fc2 | 294 | static inline int crypt_s390_func_available(int func) |
1da177e4 | 295 | { |
86aa9fc2 | 296 | unsigned char status[16]; |
1da177e4 LT |
297 | int ret; |
298 | ||
a72f0dbf JG |
299 | /* check if CPACF facility (bit 17) is available */ |
300 | if (!(stfl() & 1ULL << (31 - 17))) | |
301 | return 0; | |
302 | ||
86aa9fc2 JG |
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; | |
1da177e4 | 321 | } |
86aa9fc2 JG |
322 | if (ret < 0) |
323 | return 0; | |
324 | func &= CRYPT_S390_FUNC_MASK; | |
325 | func &= 0x7f; /* mask modifier bit */ | |
326 | return (status[func >> 3] & (0x80 >> (func & 7))) != 0; | |
1da177e4 LT |
327 | } |
328 | ||
86aa9fc2 | 329 | #endif /* _CRYPTO_ARCH_S390_CRYPT_S390_H */ |