]> err.no Git - linux-2.6/blob - arch/s390/crypto/crypt_s390.h
d1d330797f75ded308f5b8bc94c2b3b0f39587ee
[linux-2.6] / arch / s390 / crypto / crypt_s390.h
1 /*
2  * Cryptographic API.
3  *
4  * Support for s390 cryptographic instructions.
5  *
6  *   Copyright (C) 2003 IBM Deutschland GmbH, IBM Corporation
7  *   Author(s): Thomas Spatzier (tspat@de.ibm.com)
8  *
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)
12  * any later version.
13  *
14  */
15 #ifndef _CRYPTO_ARCH_S390_CRYPT_S390_H
16 #define _CRYPTO_ARCH_S390_CRYPT_S390_H
17
18 #include <asm/errno.h>
19
20 #define CRYPT_S390_OP_MASK 0xFF00
21 #define CRYPT_S390_FUNC_MASK 0x00FF
22
23 #define CRYPT_S390_PRIORITY 300
24
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
32 };
33
34 /* function codes for KM (CIPHER MESSAGE) instruction
35  * 0x80 is the decipher modifier bit
36  */
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,
51 };
52
53 /* function codes for KMC (CIPHER MESSAGE WITH CHAINING)
54  * instruction
55  */
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,
70 };
71
72 /* function codes for KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST)
73  * instruction
74  */
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,
79 };
80
81 /* function codes for KLMD (COMPUTE LAST MESSAGE DIGEST)
82  * instruction
83  */
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,
88 };
89
90 /* function codes for KMAC (COMPUTE MESSAGE AUTHENTICATION CODE)
91  * instruction
92  */
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
98 };
99
100 /* status word for s390 crypto instructions' QUERY functions */
101 struct crypt_s390_query_status {
102         u64 high;
103         u64 low;
104 };
105
106 /*
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
116  */
117 #ifndef CONFIG_64BIT
118 #define __crypt_s390_fixup \
119         ".section .fixup,\"ax\" \n"     \
120         "7:     lhi     %0,%h[e1] \n"   \
121         "       bras    1,9f \n"        \
122         "       .long   6b \n"          \
123         "8:     lhi     %0,%h[e2] \n"   \
124         "       bras    1,9f \n"        \
125         "       .long   6b \n"          \
126         "9:     l       1,0(1) \n"      \
127         "       br      1 \n"           \
128         ".previous \n"                  \
129         ".section __ex_table,\"a\" \n"  \
130         "       .align  4 \n"           \
131         "       .long   0b,7b \n"       \
132         "       .long   1b,8b \n"       \
133         ".previous"
134 #else /* CONFIG_64BIT */
135 #define __crypt_s390_fixup \
136         ".section .fixup,\"ax\" \n"     \
137         "7:     lhi     %0,%h[e1] \n"   \
138         "       jg      6b \n"          \
139         "8:     lhi     %0,%h[e2] \n"   \
140         "       jg      6b \n"          \
141         ".previous\n"                   \
142         ".section __ex_table,\"a\" \n"  \
143         "       .align  8 \n"           \
144         "       .quad   0b,7b \n"       \
145         "       .quad   1b,8b \n"       \
146         ".previous"
147 #endif /* CONFIG_64BIT */
148
149 /*
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].
154  */
155 #ifndef CONFIG_64BIT
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"
161 #endif
162
163 /*
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
172  */
173 static inline int
174 crypt_s390_km(long func, void* param, u8* dest, const u8* src, long src_len)
175 {
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;
181         int ret;
182
183         ret = 0;
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
188                 "6:     \n"
189                 __crypt_s390_fixup
190                 : "+d" (ret), "+a" (__dest), "+a" (__src),
191                   [result] "+d" (__src_len)
192                 : [e1] "K" (-EFAULT), [e2] "K" (-ENOSYS), "d" (__func),
193                   "a" (__param)
194                 : "cc", "memory"
195         );
196         if (ret >= 0 && func & CRYPT_S390_FUNC_MASK){
197                 ret = src_len - ret;
198         }
199         return ret;
200 }
201
202 /*
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
211  */
212 static inline int
213 crypt_s390_kmc(long func, void* param, u8* dest, const u8* src, long src_len)
214 {
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;
220         int ret;
221
222         ret = 0;
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
227                 "6:     \n"
228                 __crypt_s390_fixup
229                 : "+d" (ret), "+a" (__dest), "+a" (__src),
230                   [result] "+d" (__src_len)
231                 : [e1] "K" (-EFAULT), [e2] "K" (-ENOSYS), "d" (__func),
232                   "a" (__param)
233                 : "cc", "memory"
234         );
235         if (ret >= 0 && func & CRYPT_S390_FUNC_MASK){
236                 ret = src_len - ret;
237         }
238         return ret;
239 }
240
241 /*
242  * Executes the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST) operation
243  * of the CPU.
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
249  *      for digest funcs
250  */
251 static inline int
252 crypt_s390_kimd(long func, void* param, const u8* src, long src_len)
253 {
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;
258         int ret;
259
260         ret = 0;
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
265                 "6:     \n"
266                 __crypt_s390_fixup
267                 : "+d" (ret), "+a" (__src), [result] "+d" (__src_len)
268                 : [e1] "K" (-EFAULT), [e2] "K" (-ENOSYS), "d" (__func),
269                   "a" (__param)
270                 : "cc", "memory"
271         );
272         if (ret >= 0 && (func & CRYPT_S390_FUNC_MASK)){
273                 ret = src_len - ret;
274         }
275         return ret;
276 }
277
278 /*
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
285  *      for digest funcs
286  */
287 static inline int
288 crypt_s390_klmd(long func, void* param, const u8* src, long src_len)
289 {
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;
294         int ret;
295
296         ret = 0;
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
301                 "6:     \n"
302                 __crypt_s390_fixup
303                 : "+d" (ret), "+a" (__src), [result] "+d" (__src_len)
304                 : [e1] "K" (-EFAULT), [e2] "K" (-ENOSYS), "d" (__func),
305                   "a" (__param)
306                 : "cc", "memory"
307         );
308         if (ret >= 0 && func & CRYPT_S390_FUNC_MASK){
309                 ret = src_len - ret;
310         }
311         return ret;
312 }
313
314 /*
315  * Executes the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE) operation
316  * of the CPU.
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
322  *      for digest funcs
323  */
324 static inline int
325 crypt_s390_kmac(long func, void* param, const u8* src, long src_len)
326 {
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;
331         int ret;
332
333         ret = 0;
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
338                 "6:     \n"
339                 __crypt_s390_fixup
340                 : "+d" (ret), "+a" (__src), [result] "+d" (__src_len)
341                 : [e1] "K" (-EFAULT), [e2] "K" (-ENOSYS), "d" (__func),
342                   "a" (__param)
343                 : "cc", "memory"
344         );
345         if (ret >= 0 && func & CRYPT_S390_FUNC_MASK){
346                 ret = src_len - ret;
347         }
348         return ret;
349 }
350
351 /**
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
355  */
356 static inline int
357 crypt_s390_func_available(int func)
358 {
359         int ret;
360
361         struct crypt_s390_query_status status = {
362                 .high = 0,
363                 .low = 0
364         };
365         switch (func & CRYPT_S390_OP_MASK){
366                 case CRYPT_S390_KM:
367                         ret = crypt_s390_km(KM_QUERY, &status, NULL, NULL, 0);
368                         break;
369                 case CRYPT_S390_KMC:
370                         ret = crypt_s390_kmc(KMC_QUERY, &status, NULL, NULL, 0);
371                         break;
372                 case CRYPT_S390_KIMD:
373                         ret = crypt_s390_kimd(KIMD_QUERY, &status, NULL, 0);
374                         break;
375                 case CRYPT_S390_KLMD:
376                         ret = crypt_s390_klmd(KLMD_QUERY, &status, NULL, 0);
377                         break;
378                 case CRYPT_S390_KMAC:
379                         ret = crypt_s390_kmac(KMAC_QUERY, &status, NULL, 0);
380                         break;
381                 default:
382                         ret = 0;
383                         return ret;
384         }
385         if (ret >= 0){
386                 func &= CRYPT_S390_FUNC_MASK;
387                 func &= 0x7f; //mask modifier bit
388                 if (func < 64){
389                         ret = (status.high >> (64 - func - 1)) & 0x1;
390                 } else {
391                         ret = (status.low >> (128 - func - 1)) & 0x1;
392                 }
393         } else {
394                 ret = 0;
395         }
396         return ret;
397 }
398
399 #endif // _CRYPTO_ARCH_S390_CRYPT_S390_H