]> err.no Git - linux-2.6/blob - arch/s390/crypto/crypt_s390.h
[S390] move crypto options and some cleanup.
[linux-2.6] / 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 /* s930 cryptographic operations */
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
34 };
35
36 /*
37  * function codes for KM (CIPHER MESSAGE) instruction
38  * 0x80 is the decipher modifier bit
39  */
40 enum crypt_s390_km_func {
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,
54 };
55
56 /*
57  * function codes for KMC (CIPHER MESSAGE WITH CHAINING)
58  * instruction
59  */
60 enum crypt_s390_kmc_func {
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,
74 };
75
76 /*
77  * function codes for KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST)
78  * instruction
79  */
80 enum crypt_s390_kimd_func {
81         KIMD_QUERY   = CRYPT_S390_KIMD | 0,
82         KIMD_SHA_1   = CRYPT_S390_KIMD | 1,
83         KIMD_SHA_256 = CRYPT_S390_KIMD | 2,
84 };
85
86 /*
87  * function codes for KLMD (COMPUTE LAST MESSAGE DIGEST)
88  * instruction
89  */
90 enum crypt_s390_klmd_func {
91         KLMD_QUERY   = CRYPT_S390_KLMD | 0,
92         KLMD_SHA_1   = CRYPT_S390_KLMD | 1,
93         KLMD_SHA_256 = CRYPT_S390_KLMD | 2,
94 };
95
96 /*
97  * function codes for KMAC (COMPUTE MESSAGE AUTHENTICATION CODE)
98  * instruction
99  */
100 enum crypt_s390_kmac_func {
101         KMAC_QUERY    = CRYPT_S390_KMAC | 0,
102         KMAC_DEA      = CRYPT_S390_KMAC | 1,
103         KMAC_TDEA_128 = CRYPT_S390_KMAC | 2,
104         KMAC_TDEA_192 = CRYPT_S390_KMAC | 3
105 };
106
107 /**
108  * crypt_s390_km:
109  * @func: the function code passed to KM; see crypt_s390_km_func
110  * @param: address of parameter block; see POP for details on each func
111  * @dest: address of destination memory area
112  * @src: address of source memory area
113  * @src_len: length of src operand in bytes
114  *
115  * Executes the KM (CIPHER MESSAGE) operation of the CPU.
116  *
117  * Returns -1 for failure, 0 for the query func, number of processed
118  * bytes for encryption/decryption funcs
119  */
120 static inline int crypt_s390_km(long func, void *param,
121                                 u8 *dest, const u8 *src, long src_len)
122 {
123         register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
124         register void *__param asm("1") = param;
125         register const u8 *__src asm("2") = src;
126         register long __src_len asm("3") = src_len;
127         register u8 *__dest asm("4") = dest;
128         int ret;
129
130         asm volatile(
131                 "0:     .insn   rre,0xb92e0000,%3,%1 \n" /* KM opcode */
132                 "1:     brc     1,0b \n" /* handle partial completion */
133                 "       la      %0,0\n"
134                 "2:\n"
135                 EX_TABLE(0b,2b) EX_TABLE(1b,2b)
136                 : "=d" (ret), "+a" (__src), "+d" (__src_len), "+a" (__dest)
137                 : "d" (__func), "a" (__param), "0" (-1) : "cc", "memory");
138         if (ret < 0)
139                 return ret;
140         return (func & CRYPT_S390_FUNC_MASK) ? src_len - __src_len : __src_len;
141 }
142
143 /**
144  * crypt_s390_kmc:
145  * @func: the function code passed to KM; see crypt_s390_kmc_func
146  * @param: address of parameter block; see POP for details on each func
147  * @dest: address of destination memory area
148  * @src: address of source memory area
149  * @src_len: length of src operand in bytes
150  *
151  * Executes the KMC (CIPHER MESSAGE WITH CHAINING) operation of the CPU.
152  *
153  * Returns -1 for failure, 0 for the query func, number of processed
154  * bytes for encryption/decryption funcs
155  */
156 static inline int crypt_s390_kmc(long func, void *param,
157                                  u8 *dest, const u8 *src, long src_len)
158 {
159         register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
160         register void *__param asm("1") = param;
161         register const u8 *__src asm("2") = src;
162         register long __src_len asm("3") = src_len;
163         register u8 *__dest asm("4") = dest;
164         int ret;
165
166         asm volatile(
167                 "0:     .insn   rre,0xb92f0000,%3,%1 \n" /* KMC opcode */
168                 "1:     brc     1,0b \n" /* handle partial completion */
169                 "       la      %0,0\n"
170                 "2:\n"
171                 EX_TABLE(0b,2b) EX_TABLE(1b,2b)
172                 : "=d" (ret), "+a" (__src), "+d" (__src_len), "+a" (__dest)
173                 : "d" (__func), "a" (__param), "0" (-1) : "cc", "memory");
174         if (ret < 0)
175                 return ret;
176         return (func & CRYPT_S390_FUNC_MASK) ? src_len - __src_len : __src_len;
177 }
178
179 /**
180  * crypt_s390_kimd:
181  * @func: the function code passed to KM; see crypt_s390_kimd_func
182  * @param: address of parameter block; see POP for details on each func
183  * @src: address of source memory area
184  * @src_len: length of src operand in bytes
185  *
186  * Executes the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST) operation
187  * of the CPU.
188  *
189  * Returns -1 for failure, 0 for the query func, number of processed
190  * bytes for digest funcs
191  */
192 static inline int crypt_s390_kimd(long func, void *param,
193                                   const u8 *src, long src_len)
194 {
195         register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
196         register void *__param asm("1") = param;
197         register const u8 *__src asm("2") = src;
198         register long __src_len asm("3") = src_len;
199         int ret;
200
201         asm volatile(
202                 "0:     .insn   rre,0xb93e0000,%1,%1 \n" /* KIMD opcode */
203                 "1:     brc     1,0b \n" /* handle partial completion */
204                 "       la      %0,0\n"
205                 "2:\n"
206                 EX_TABLE(0b,2b) EX_TABLE(1b,2b)
207                 : "=d" (ret), "+a" (__src), "+d" (__src_len)
208                 : "d" (__func), "a" (__param), "0" (-1) : "cc", "memory");
209         if (ret < 0)
210                 return ret;
211         return (func & CRYPT_S390_FUNC_MASK) ? src_len - __src_len : __src_len;
212 }
213
214 /**
215  * crypt_s390_klmd:
216  * @func: the function code passed to KM; see crypt_s390_klmd_func
217  * @param: address of parameter block; see POP for details on each func
218  * @src: address of source memory area
219  * @src_len: length of src operand in bytes
220  *
221  * Executes the KLMD (COMPUTE LAST MESSAGE DIGEST) operation of the CPU.
222  *
223  * Returns -1 for failure, 0 for the query func, number of processed
224  * bytes for digest funcs
225  */
226 static inline int crypt_s390_klmd(long func, void *param,
227                                   const u8 *src, long src_len)
228 {
229         register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
230         register void *__param asm("1") = param;
231         register const u8 *__src asm("2") = src;
232         register long __src_len asm("3") = src_len;
233         int ret;
234
235         asm volatile(
236                 "0:     .insn   rre,0xb93f0000,%1,%1 \n" /* KLMD opcode */
237                 "1:     brc     1,0b \n" /* handle partial completion */
238                 "       la      %0,0\n"
239                 "2:\n"
240                 EX_TABLE(0b,2b) EX_TABLE(1b,2b)
241                 : "=d" (ret), "+a" (__src), "+d" (__src_len)
242                 : "d" (__func), "a" (__param), "0" (-1) : "cc", "memory");
243         if (ret < 0)
244                 return ret;
245         return (func & CRYPT_S390_FUNC_MASK) ? src_len - __src_len : __src_len;
246 }
247
248 /**
249  * crypt_s390_kmac:
250  * @func: the function code passed to KM; see crypt_s390_klmd_func
251  * @param: address of parameter block; see POP for details on each func
252  * @src: address of source memory area
253  * @src_len: length of src operand in bytes
254  *
255  * Executes the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE) operation
256  * of the CPU.
257  *
258  * Returns -1 for failure, 0 for the query func, number of processed
259  * bytes for digest funcs
260  */
261 static inline int crypt_s390_kmac(long func, void *param,
262                                   const u8 *src, long src_len)
263 {
264         register long __func asm("0") = func & CRYPT_S390_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;
268         int ret;
269
270         asm volatile(
271                 "0:     .insn   rre,0xb91e0000,%1,%1 \n" /* KLAC opcode */
272                 "1:     brc     1,0b \n" /* handle partial completion */
273                 "       la      %0,0\n"
274                 "2:\n"
275                 EX_TABLE(0b,2b) EX_TABLE(1b,2b)
276                 : "=d" (ret), "+a" (__src), "+d" (__src_len)
277                 : "d" (__func), "a" (__param), "0" (-1) : "cc", "memory");
278         if (ret < 0)
279                 return ret;
280         return (func & CRYPT_S390_FUNC_MASK) ? src_len - __src_len : __src_len;
281 }
282
283 /**
284  * crypt_s390_func_available:
285  * @func: the function code of the specific function; 0 if op in general
286  *
287  * Tests if a specific crypto function is implemented on the machine.
288  *
289  * Returns 1 if func available; 0 if func or op in general not available
290  */
291 static inline int crypt_s390_func_available(int func)
292 {
293         unsigned char status[16];
294         int ret;
295
296         switch (func & CRYPT_S390_OP_MASK) {
297         case CRYPT_S390_KM:
298                 ret = crypt_s390_km(KM_QUERY, &status, NULL, NULL, 0);
299                 break;
300         case CRYPT_S390_KMC:
301                 ret = crypt_s390_kmc(KMC_QUERY, &status, NULL, NULL, 0);
302                 break;
303         case CRYPT_S390_KIMD:
304                 ret = crypt_s390_kimd(KIMD_QUERY, &status, NULL, 0);
305                 break;
306         case CRYPT_S390_KLMD:
307                 ret = crypt_s390_klmd(KLMD_QUERY, &status, NULL, 0);
308                 break;
309         case CRYPT_S390_KMAC:
310                 ret = crypt_s390_kmac(KMAC_QUERY, &status, NULL, 0);
311                 break;
312         default:
313                 return 0;
314         }
315         if (ret < 0)
316                 return 0;
317         func &= CRYPT_S390_FUNC_MASK;
318         func &= 0x7f;           /* mask modifier bit */
319         return (status[func >> 3] & (0x80 >> (func & 7))) != 0;
320 }
321
322 #endif  /* _CRYPTO_ARCH_S390_CRYPT_S390_H */