crypto: s390 - add System z hardware support for XTS mode
[cascardo/linux.git] / arch / s390 / crypto / crypt_s390.h
1 /*
2  * Cryptographic API.
3  *
4  * Support for s390 cryptographic instructions.
5  *
6  *   Copyright IBM Corp. 2003,2007
7  *   Author(s): Thomas Spatzier
8  *              Jan Glauber (jan.glauber@de.ibm.com)
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  */
16 #ifndef _CRYPTO_ARCH_S390_CRYPT_S390_H
17 #define _CRYPTO_ARCH_S390_CRYPT_S390_H
18
19 #include <asm/errno.h>
20
21 #define CRYPT_S390_OP_MASK 0xFF00
22 #define CRYPT_S390_FUNC_MASK 0x00FF
23
24 #define CRYPT_S390_PRIORITY 300
25 #define CRYPT_S390_COMPOSITE_PRIORITY 400
26
27 #define CRYPT_S390_MSA  0x1
28 #define CRYPT_S390_MSA3 0x2
29 #define CRYPT_S390_MSA4 0x4
30
31 /* s390 cryptographic operations */
32 enum crypt_s390_operations {
33         CRYPT_S390_KM   = 0x0100,
34         CRYPT_S390_KMC  = 0x0200,
35         CRYPT_S390_KIMD = 0x0300,
36         CRYPT_S390_KLMD = 0x0400,
37         CRYPT_S390_KMAC = 0x0500
38 };
39
40 /*
41  * function codes for KM (CIPHER MESSAGE) instruction
42  * 0x80 is the decipher modifier bit
43  */
44 enum crypt_s390_km_func {
45         KM_QUERY            = CRYPT_S390_KM | 0x0,
46         KM_DEA_ENCRYPT      = CRYPT_S390_KM | 0x1,
47         KM_DEA_DECRYPT      = CRYPT_S390_KM | 0x1 | 0x80,
48         KM_TDEA_128_ENCRYPT = CRYPT_S390_KM | 0x2,
49         KM_TDEA_128_DECRYPT = CRYPT_S390_KM | 0x2 | 0x80,
50         KM_TDEA_192_ENCRYPT = CRYPT_S390_KM | 0x3,
51         KM_TDEA_192_DECRYPT = CRYPT_S390_KM | 0x3 | 0x80,
52         KM_AES_128_ENCRYPT  = CRYPT_S390_KM | 0x12,
53         KM_AES_128_DECRYPT  = CRYPT_S390_KM | 0x12 | 0x80,
54         KM_AES_192_ENCRYPT  = CRYPT_S390_KM | 0x13,
55         KM_AES_192_DECRYPT  = CRYPT_S390_KM | 0x13 | 0x80,
56         KM_AES_256_ENCRYPT  = CRYPT_S390_KM | 0x14,
57         KM_AES_256_DECRYPT  = CRYPT_S390_KM | 0x14 | 0x80,
58         KM_XTS_128_ENCRYPT  = CRYPT_S390_KM | 0x32,
59         KM_XTS_128_DECRYPT  = CRYPT_S390_KM | 0x32 | 0x80,
60         KM_XTS_256_ENCRYPT  = CRYPT_S390_KM | 0x34,
61         KM_XTS_256_DECRYPT  = CRYPT_S390_KM | 0x34 | 0x80,
62 };
63
64 /*
65  * function codes for KMC (CIPHER MESSAGE WITH CHAINING)
66  * instruction
67  */
68 enum crypt_s390_kmc_func {
69         KMC_QUERY            = CRYPT_S390_KMC | 0x0,
70         KMC_DEA_ENCRYPT      = CRYPT_S390_KMC | 0x1,
71         KMC_DEA_DECRYPT      = CRYPT_S390_KMC | 0x1 | 0x80,
72         KMC_TDEA_128_ENCRYPT = CRYPT_S390_KMC | 0x2,
73         KMC_TDEA_128_DECRYPT = CRYPT_S390_KMC | 0x2 | 0x80,
74         KMC_TDEA_192_ENCRYPT = CRYPT_S390_KMC | 0x3,
75         KMC_TDEA_192_DECRYPT = CRYPT_S390_KMC | 0x3 | 0x80,
76         KMC_AES_128_ENCRYPT  = CRYPT_S390_KMC | 0x12,
77         KMC_AES_128_DECRYPT  = CRYPT_S390_KMC | 0x12 | 0x80,
78         KMC_AES_192_ENCRYPT  = CRYPT_S390_KMC | 0x13,
79         KMC_AES_192_DECRYPT  = CRYPT_S390_KMC | 0x13 | 0x80,
80         KMC_AES_256_ENCRYPT  = CRYPT_S390_KMC | 0x14,
81         KMC_AES_256_DECRYPT  = CRYPT_S390_KMC | 0x14 | 0x80,
82         KMC_PRNG             = CRYPT_S390_KMC | 0x43,
83 };
84
85 /*
86  * function codes for KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST)
87  * instruction
88  */
89 enum crypt_s390_kimd_func {
90         KIMD_QUERY   = CRYPT_S390_KIMD | 0,
91         KIMD_SHA_1   = CRYPT_S390_KIMD | 1,
92         KIMD_SHA_256 = CRYPT_S390_KIMD | 2,
93         KIMD_SHA_512 = CRYPT_S390_KIMD | 3,
94 };
95
96 /*
97  * function codes for KLMD (COMPUTE LAST MESSAGE DIGEST)
98  * instruction
99  */
100 enum crypt_s390_klmd_func {
101         KLMD_QUERY   = CRYPT_S390_KLMD | 0,
102         KLMD_SHA_1   = CRYPT_S390_KLMD | 1,
103         KLMD_SHA_256 = CRYPT_S390_KLMD | 2,
104         KLMD_SHA_512 = CRYPT_S390_KLMD | 3,
105 };
106
107 /*
108  * function codes for KMAC (COMPUTE MESSAGE AUTHENTICATION CODE)
109  * instruction
110  */
111 enum crypt_s390_kmac_func {
112         KMAC_QUERY    = CRYPT_S390_KMAC | 0,
113         KMAC_DEA      = CRYPT_S390_KMAC | 1,
114         KMAC_TDEA_128 = CRYPT_S390_KMAC | 2,
115         KMAC_TDEA_192 = CRYPT_S390_KMAC | 3
116 };
117
118 /**
119  * crypt_s390_km:
120  * @func: the function code passed to KM; see crypt_s390_km_func
121  * @param: address of parameter block; see POP for details on each func
122  * @dest: address of destination memory area
123  * @src: address of source memory area
124  * @src_len: length of src operand in bytes
125  *
126  * Executes the KM (CIPHER MESSAGE) operation of the CPU.
127  *
128  * Returns -1 for failure, 0 for the query func, number of processed
129  * bytes for encryption/decryption funcs
130  */
131 static inline int crypt_s390_km(long func, void *param,
132                                 u8 *dest, const u8 *src, long src_len)
133 {
134         register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
135         register void *__param asm("1") = param;
136         register const u8 *__src asm("2") = src;
137         register long __src_len asm("3") = src_len;
138         register u8 *__dest asm("4") = dest;
139         int ret;
140
141         asm volatile(
142                 "0:     .insn   rre,0xb92e0000,%3,%1 \n" /* KM opcode */
143                 "1:     brc     1,0b \n" /* handle partial completion */
144                 "       la      %0,0\n"
145                 "2:\n"
146                 EX_TABLE(0b,2b) EX_TABLE(1b,2b)
147                 : "=d" (ret), "+a" (__src), "+d" (__src_len), "+a" (__dest)
148                 : "d" (__func), "a" (__param), "0" (-1) : "cc", "memory");
149         if (ret < 0)
150                 return ret;
151         return (func & CRYPT_S390_FUNC_MASK) ? src_len - __src_len : __src_len;
152 }
153
154 /**
155  * crypt_s390_kmc:
156  * @func: the function code passed to KM; see crypt_s390_kmc_func
157  * @param: address of parameter block; see POP for details on each func
158  * @dest: address of destination memory area
159  * @src: address of source memory area
160  * @src_len: length of src operand in bytes
161  *
162  * Executes the KMC (CIPHER MESSAGE WITH CHAINING) operation of the CPU.
163  *
164  * Returns -1 for failure, 0 for the query func, number of processed
165  * bytes for encryption/decryption funcs
166  */
167 static inline int crypt_s390_kmc(long func, void *param,
168                                  u8 *dest, const u8 *src, long src_len)
169 {
170         register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
171         register void *__param asm("1") = param;
172         register const u8 *__src asm("2") = src;
173         register long __src_len asm("3") = src_len;
174         register u8 *__dest asm("4") = dest;
175         int ret;
176
177         asm volatile(
178                 "0:     .insn   rre,0xb92f0000,%3,%1 \n" /* KMC opcode */
179                 "1:     brc     1,0b \n" /* handle partial completion */
180                 "       la      %0,0\n"
181                 "2:\n"
182                 EX_TABLE(0b,2b) EX_TABLE(1b,2b)
183                 : "=d" (ret), "+a" (__src), "+d" (__src_len), "+a" (__dest)
184                 : "d" (__func), "a" (__param), "0" (-1) : "cc", "memory");
185         if (ret < 0)
186                 return ret;
187         return (func & CRYPT_S390_FUNC_MASK) ? src_len - __src_len : __src_len;
188 }
189
190 /**
191  * crypt_s390_kimd:
192  * @func: the function code passed to KM; see crypt_s390_kimd_func
193  * @param: address of parameter block; see POP for details on each func
194  * @src: address of source memory area
195  * @src_len: length of src operand in bytes
196  *
197  * Executes the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST) operation
198  * of the CPU.
199  *
200  * Returns -1 for failure, 0 for the query func, number of processed
201  * bytes for digest funcs
202  */
203 static inline int crypt_s390_kimd(long func, void *param,
204                                   const u8 *src, long src_len)
205 {
206         register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
207         register void *__param asm("1") = param;
208         register const u8 *__src asm("2") = src;
209         register long __src_len asm("3") = src_len;
210         int ret;
211
212         asm volatile(
213                 "0:     .insn   rre,0xb93e0000,%1,%1 \n" /* KIMD opcode */
214                 "1:     brc     1,0b \n" /* handle partial completion */
215                 "       la      %0,0\n"
216                 "2:\n"
217                 EX_TABLE(0b,2b) EX_TABLE(1b,2b)
218                 : "=d" (ret), "+a" (__src), "+d" (__src_len)
219                 : "d" (__func), "a" (__param), "0" (-1) : "cc", "memory");
220         if (ret < 0)
221                 return ret;
222         return (func & CRYPT_S390_FUNC_MASK) ? src_len - __src_len : __src_len;
223 }
224
225 /**
226  * crypt_s390_klmd:
227  * @func: the function code passed to KM; see crypt_s390_klmd_func
228  * @param: address of parameter block; see POP for details on each func
229  * @src: address of source memory area
230  * @src_len: length of src operand in bytes
231  *
232  * Executes the KLMD (COMPUTE LAST MESSAGE DIGEST) operation of the CPU.
233  *
234  * Returns -1 for failure, 0 for the query func, number of processed
235  * bytes for digest funcs
236  */
237 static inline int crypt_s390_klmd(long func, void *param,
238                                   const u8 *src, long src_len)
239 {
240         register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
241         register void *__param asm("1") = param;
242         register const u8 *__src asm("2") = src;
243         register long __src_len asm("3") = src_len;
244         int ret;
245
246         asm volatile(
247                 "0:     .insn   rre,0xb93f0000,%1,%1 \n" /* KLMD opcode */
248                 "1:     brc     1,0b \n" /* handle partial completion */
249                 "       la      %0,0\n"
250                 "2:\n"
251                 EX_TABLE(0b,2b) EX_TABLE(1b,2b)
252                 : "=d" (ret), "+a" (__src), "+d" (__src_len)
253                 : "d" (__func), "a" (__param), "0" (-1) : "cc", "memory");
254         if (ret < 0)
255                 return ret;
256         return (func & CRYPT_S390_FUNC_MASK) ? src_len - __src_len : __src_len;
257 }
258
259 /**
260  * crypt_s390_kmac:
261  * @func: the function code passed to KM; see crypt_s390_klmd_func
262  * @param: address of parameter block; see POP for details on each func
263  * @src: address of source memory area
264  * @src_len: length of src operand in bytes
265  *
266  * Executes the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE) operation
267  * of the CPU.
268  *
269  * Returns -1 for failure, 0 for the query func, number of processed
270  * bytes for digest funcs
271  */
272 static inline int crypt_s390_kmac(long func, void *param,
273                                   const u8 *src, long src_len)
274 {
275         register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
276         register void *__param asm("1") = param;
277         register const u8 *__src asm("2") = src;
278         register long __src_len asm("3") = src_len;
279         int ret;
280
281         asm volatile(
282                 "0:     .insn   rre,0xb91e0000,%1,%1 \n" /* KLAC opcode */
283                 "1:     brc     1,0b \n" /* handle partial completion */
284                 "       la      %0,0\n"
285                 "2:\n"
286                 EX_TABLE(0b,2b) EX_TABLE(1b,2b)
287                 : "=d" (ret), "+a" (__src), "+d" (__src_len)
288                 : "d" (__func), "a" (__param), "0" (-1) : "cc", "memory");
289         if (ret < 0)
290                 return ret;
291         return (func & CRYPT_S390_FUNC_MASK) ? src_len - __src_len : __src_len;
292 }
293
294 /**
295  * crypt_s390_func_available:
296  * @func: the function code of the specific function; 0 if op in general
297  *
298  * Tests if a specific crypto function is implemented on the machine.
299  *
300  * Returns 1 if func available; 0 if func or op in general not available
301  */
302 static inline int crypt_s390_func_available(int func,
303                                             unsigned int facility_mask)
304 {
305         unsigned char status[16];
306         int ret;
307
308         if (facility_mask & CRYPT_S390_MSA && !test_facility(17))
309                 return 0;
310         if (facility_mask & CRYPT_S390_MSA3 && !test_facility(76))
311                 return 0;
312         if (facility_mask & CRYPT_S390_MSA4 && !test_facility(77))
313                 return 0;
314
315         switch (func & CRYPT_S390_OP_MASK) {
316         case CRYPT_S390_KM:
317                 ret = crypt_s390_km(KM_QUERY, &status, NULL, NULL, 0);
318                 break;
319         case CRYPT_S390_KMC:
320                 ret = crypt_s390_kmc(KMC_QUERY, &status, NULL, NULL, 0);
321                 break;
322         case CRYPT_S390_KIMD:
323                 ret = crypt_s390_kimd(KIMD_QUERY, &status, NULL, 0);
324                 break;
325         case CRYPT_S390_KLMD:
326                 ret = crypt_s390_klmd(KLMD_QUERY, &status, NULL, 0);
327                 break;
328         case CRYPT_S390_KMAC:
329                 ret = crypt_s390_kmac(KMAC_QUERY, &status, NULL, 0);
330                 break;
331         default:
332                 return 0;
333         }
334         if (ret < 0)
335                 return 0;
336         func &= CRYPT_S390_FUNC_MASK;
337         func &= 0x7f;           /* mask modifier bit */
338         return (status[func >> 3] & (0x80 >> (func & 7))) != 0;
339 }
340
341 /**
342  * crypt_s390_pcc:
343  * @func: the function code passed to KM; see crypt_s390_km_func
344  * @param: address of parameter block; see POP for details on each func
345  *
346  * Executes the PCC (PERFORM CRYPTOGRAPHIC COMPUTATION) operation of the CPU.
347  *
348  * Returns -1 for failure, 0 for success.
349  */
350 static inline int crypt_s390_pcc(long func, void *param)
351 {
352         register long __func asm("0") = func & 0x7f; /* encrypt or decrypt */
353         register void *__param asm("1") = param;
354         int ret = -1;
355
356         asm volatile(
357                 "0:     .insn   rre,0xb92c0000,0,0 \n" /* PCC opcode */
358                 "1:     brc     1,0b \n" /* handle partial completion */
359                 "       la      %0,0\n"
360                 "2:\n"
361                 EX_TABLE(0b,2b) EX_TABLE(1b,2b)
362                 : "+d" (ret)
363                 : "d" (__func), "a" (__param) : "cc", "memory");
364         return ret;
365 }
366
367
368 #endif  /* _CRYPTO_ARCH_S390_CRYPT_S390_H */