Merge branch 'akpm' (patches from Andrew)
[cascardo/linux.git] / arch / s390 / include / asm / cpacf.h
1 /*
2  * CP Assist for Cryptographic Functions (CPACF)
3  *
4  * Copyright IBM Corp. 2003, 2016
5  * Author(s): Thomas Spatzier
6  *            Jan Glauber
7  *            Harald Freudenberger (freude@de.ibm.com)
8  *            Martin Schwidefsky <schwidefsky@de.ibm.com>
9  */
10 #ifndef _ASM_S390_CPACF_H
11 #define _ASM_S390_CPACF_H
12
13 #include <asm/facility.h>
14
15 /*
16  * Instruction opcodes for the CPACF instructions
17  */
18 #define CPACF_KMAC              0xb91e          /* MSA  */
19 #define CPACF_KM                0xb92e          /* MSA  */
20 #define CPACF_KMC               0xb92f          /* MSA  */
21 #define CPACF_KIMD              0xb93e          /* MSA  */
22 #define CPACF_KLMD              0xb93f          /* MSA  */
23 #define CPACF_PCKMO             0xb928          /* MSA3 */
24 #define CPACF_KMF               0xb92a          /* MSA4 */
25 #define CPACF_KMO               0xb92b          /* MSA4 */
26 #define CPACF_PCC               0xb92c          /* MSA4 */
27 #define CPACF_KMCTR             0xb92d          /* MSA4 */
28 #define CPACF_PPNO              0xb93c          /* MSA5 */
29
30 /*
31  * Function codes for the KM (CIPHER MESSAGE)
32  * instruction (0x80 is the decipher modifier bit)
33  */
34 #define CPACF_KM_QUERY          0x00
35 #define CPACF_KM_DEA_ENC        0x01
36 #define CPACF_KM_DEA_DEC        0x81
37 #define CPACF_KM_TDEA_128_ENC   0x02
38 #define CPACF_KM_TDEA_128_DEC   0x82
39 #define CPACF_KM_TDEA_192_ENC   0x03
40 #define CPACF_KM_TDEA_192_DEC   0x83
41 #define CPACF_KM_AES_128_ENC    0x12
42 #define CPACF_KM_AES_128_DEC    0x92
43 #define CPACF_KM_AES_192_ENC    0x13
44 #define CPACF_KM_AES_192_DEC    0x93
45 #define CPACF_KM_AES_256_ENC    0x14
46 #define CPACF_KM_AES_256_DEC    0x94
47 #define CPACF_KM_XTS_128_ENC    0x32
48 #define CPACF_KM_XTS_128_DEC    0xb2
49 #define CPACF_KM_XTS_256_ENC    0x34
50 #define CPACF_KM_XTS_256_DEC    0xb4
51
52 /*
53  * Function codes for the KMC (CIPHER MESSAGE WITH CHAINING)
54  * instruction (0x80 is the decipher modifier bit)
55  */
56 #define CPACF_KMC_QUERY         0x00
57 #define CPACF_KMC_DEA_ENC       0x01
58 #define CPACF_KMC_DEA_DEC       0x81
59 #define CPACF_KMC_TDEA_128_ENC  0x02
60 #define CPACF_KMC_TDEA_128_DEC  0x82
61 #define CPACF_KMC_TDEA_192_ENC  0x03
62 #define CPACF_KMC_TDEA_192_DEC  0x83
63 #define CPACF_KMC_AES_128_ENC   0x12
64 #define CPACF_KMC_AES_128_DEC   0x92
65 #define CPACF_KMC_AES_192_ENC   0x13
66 #define CPACF_KMC_AES_192_DEC   0x93
67 #define CPACF_KMC_AES_256_ENC   0x14
68 #define CPACF_KMC_AES_256_DEC   0x94
69 #define CPACF_KMC_PRNG          0x43
70
71 /*
72  * Function codes for the KMCTR (CIPHER MESSAGE WITH COUNTER)
73  * instruction (0x80 is the decipher modifier bit)
74  */
75 #define CPACF_KMCTR_QUERY        0x00
76 #define CPACF_KMCTR_DEA_ENC      0x01
77 #define CPACF_KMCTR_DEA_DEC      0x81
78 #define CPACF_KMCTR_TDEA_128_ENC 0x02
79 #define CPACF_KMCTR_TDEA_128_DEC 0x82
80 #define CPACF_KMCTR_TDEA_192_ENC 0x03
81 #define CPACF_KMCTR_TDEA_192_DEC 0x83
82 #define CPACF_KMCTR_AES_128_ENC  0x12
83 #define CPACF_KMCTR_AES_128_DEC  0x92
84 #define CPACF_KMCTR_AES_192_ENC  0x13
85 #define CPACF_KMCTR_AES_192_DEC  0x93
86 #define CPACF_KMCTR_AES_256_ENC  0x14
87 #define CPACF_KMCTR_AES_256_DEC  0x94
88
89 /*
90  * Function codes for the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST)
91  * instruction (0x80 is the decipher modifier bit)
92  */
93 #define CPACF_KIMD_QUERY        0x00
94 #define CPACF_KIMD_SHA_1        0x01
95 #define CPACF_KIMD_SHA_256      0x02
96 #define CPACF_KIMD_SHA_512      0x03
97 #define CPACF_KIMD_GHASH        0x41
98
99 /*
100  * Function codes for the KLMD (COMPUTE LAST MESSAGE DIGEST)
101  * instruction (0x80 is the decipher modifier bit)
102  */
103 #define CPACF_KLMD_QUERY        0x00
104 #define CPACF_KLMD_SHA_1        0x01
105 #define CPACF_KLMD_SHA_256      0x02
106 #define CPACF_KLMD_SHA_512      0x03
107
108 /*
109  * function codes for the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE)
110  * instruction (0x80 is the decipher modifier bit)
111  */
112 #define CPACF_KMAC_QUERY        0x00
113 #define CPACF_KMAC_DEA          0x01
114 #define CPACF_KMAC_TDEA_128     0x02
115 #define CPACF_KMAC_TDEA_192     0x03
116
117 /*
118  * Function codes for the PPNO (PERFORM PSEUDORANDOM NUMBER OPERATION)
119  * instruction (0x80 is the decipher modifier bit)
120  */
121 #define CPACF_PPNO_QUERY                0x00
122 #define CPACF_PPNO_SHA512_DRNG_GEN      0x03
123 #define CPACF_PPNO_SHA512_DRNG_SEED     0x83
124
125 /**
126  * cpacf_query() - check if a specific CPACF function is available
127  * @opcode: the opcode of the crypto instruction
128  * @func: the function code to test for
129  *
130  * Executes the query function for the given crypto instruction @opcode
131  * and checks if @func is available
132  *
133  * Returns 1 if @func is available for @opcode, 0 otherwise
134  */
135 static inline void __cpacf_query(unsigned int opcode, unsigned char *status)
136 {
137         typedef struct { unsigned char _[16]; } status_type;
138         register unsigned long r0 asm("0") = 0; /* query function */
139         register unsigned long r1 asm("1") = (unsigned long) status;
140
141         asm volatile(
142                 "       spm 0\n" /* pckmo doesn't change the cc */
143                 /* Parameter registers are ignored, but may not be 0 */
144                 "0:     .insn   rrf,%[opc] << 16,2,2,2,0\n"
145                 "       brc     1,0b\n" /* handle partial completion */
146                 : "=m" (*(status_type *) status)
147                 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (opcode)
148                 : "cc");
149 }
150
151 static inline int cpacf_query(unsigned int opcode, unsigned int func)
152 {
153         unsigned char status[16];
154
155         switch (opcode) {
156         case CPACF_KMAC:
157         case CPACF_KM:
158         case CPACF_KMC:
159         case CPACF_KIMD:
160         case CPACF_KLMD:
161                 if (!test_facility(17)) /* check for MSA */
162                         return 0;
163                 break;
164         case CPACF_PCKMO:
165                 if (!test_facility(76)) /* check for MSA3 */
166                         return 0;
167                 break;
168         case CPACF_KMF:
169         case CPACF_KMO:
170         case CPACF_PCC:
171         case CPACF_KMCTR:
172                 if (!test_facility(77)) /* check for MSA4 */
173                         return 0;
174                 break;
175         case CPACF_PPNO:
176                 if (!test_facility(57)) /* check for MSA5 */
177                         return 0;
178                 break;
179         default:
180                 BUG();
181         }
182         __cpacf_query(opcode, status);
183         return (status[func >> 3] & (0x80 >> (func & 7))) != 0;
184 }
185
186 /**
187  * cpacf_km() - executes the KM (CIPHER MESSAGE) instruction
188  * @func: the function code passed to KM; see CPACF_KM_xxx defines
189  * @param: address of parameter block; see POP for details on each func
190  * @dest: address of destination memory area
191  * @src: address of source memory area
192  * @src_len: length of src operand in bytes
193  *
194  * Returns 0 for the query func, number of processed bytes for
195  * encryption/decryption funcs
196  */
197 static inline int cpacf_km(long func, void *param,
198                            u8 *dest, const u8 *src, long src_len)
199 {
200         register unsigned long r0 asm("0") = (unsigned long) func;
201         register unsigned long r1 asm("1") = (unsigned long) param;
202         register unsigned long r2 asm("2") = (unsigned long) src;
203         register unsigned long r3 asm("3") = (unsigned long) src_len;
204         register unsigned long r4 asm("4") = (unsigned long) dest;
205
206         asm volatile(
207                 "0:     .insn   rre,%[opc] << 16,%[dst],%[src]\n"
208                 "       brc     1,0b\n" /* handle partial completion */
209                 : [src] "+a" (r2), [len] "+d" (r3), [dst] "+a" (r4)
210                 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KM)
211                 : "cc", "memory");
212
213         return src_len - r3;
214 }
215
216 /**
217  * cpacf_kmc() - executes the KMC (CIPHER MESSAGE WITH CHAINING) instruction
218  * @func: the function code passed to KM; see CPACF_KMC_xxx defines
219  * @param: address of parameter block; see POP for details on each func
220  * @dest: address of destination memory area
221  * @src: address of source memory area
222  * @src_len: length of src operand in bytes
223  *
224  * Returns 0 for the query func, number of processed bytes for
225  * encryption/decryption funcs
226  */
227 static inline int cpacf_kmc(long func, void *param,
228                             u8 *dest, const u8 *src, long src_len)
229 {
230         register unsigned long r0 asm("0") = (unsigned long) func;
231         register unsigned long r1 asm("1") = (unsigned long) param;
232         register unsigned long r2 asm("2") = (unsigned long) src;
233         register unsigned long r3 asm("3") = (unsigned long) src_len;
234         register unsigned long r4 asm("4") = (unsigned long) dest;
235
236         asm volatile(
237                 "0:     .insn   rre,%[opc] << 16,%[dst],%[src]\n"
238                 "       brc     1,0b\n" /* handle partial completion */
239                 : [src] "+a" (r2), [len] "+d" (r3), [dst] "+a" (r4)
240                 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KMC)
241                 : "cc", "memory");
242
243         return src_len - r3;
244 }
245
246 /**
247  * cpacf_kimd() - executes the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST)
248  *                instruction
249  * @func: the function code passed to KM; see CPACF_KIMD_xxx defines
250  * @param: address of parameter block; see POP for details on each func
251  * @src: address of source memory area
252  * @src_len: length of src operand in bytes
253  *
254  * Returns 0 for the query func, number of processed bytes for digest funcs
255  */
256 static inline int cpacf_kimd(long func, void *param,
257                              const u8 *src, long src_len)
258 {
259         register unsigned long r0 asm("0") = (unsigned long) func;
260         register unsigned long r1 asm("1") = (unsigned long) param;
261         register unsigned long r2 asm("2") = (unsigned long) src;
262         register unsigned long r3 asm("3") = (unsigned long) src_len;
263
264         asm volatile(
265                 "0:     .insn   rre,%[opc] << 16,0,%[src]\n"
266                 "       brc     1,0b\n" /* handle partial completion */
267                 : [src] "+a" (r2), [len] "+d" (r3)
268                 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KIMD)
269                 : "cc", "memory");
270
271         return src_len - r3;
272 }
273
274 /**
275  * cpacf_klmd() - executes the KLMD (COMPUTE LAST MESSAGE DIGEST) instruction
276  * @func: the function code passed to KM; see CPACF_KLMD_xxx defines
277  * @param: address of parameter block; see POP for details on each func
278  * @src: address of source memory area
279  * @src_len: length of src operand in bytes
280  *
281  * Returns 0 for the query func, number of processed bytes for digest funcs
282  */
283 static inline int cpacf_klmd(long func, void *param,
284                              const u8 *src, long src_len)
285 {
286         register unsigned long r0 asm("0") = (unsigned long) func;
287         register unsigned long r1 asm("1") = (unsigned long) param;
288         register unsigned long r2 asm("2") = (unsigned long) src;
289         register unsigned long r3 asm("3") = (unsigned long) src_len;
290
291         asm volatile(
292                 "0:     .insn   rre,%[opc] << 16,0,%[src]\n"
293                 "       brc     1,0b\n" /* handle partial completion */
294                 : [src] "+a" (r2), [len] "+d" (r3)
295                 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KLMD)
296                 : "cc", "memory");
297
298         return src_len - r3;
299 }
300
301 /**
302  * cpacf_kmac() - executes the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE)
303  *                instruction
304  * @func: the function code passed to KM; see CPACF_KMAC_xxx defines
305  * @param: address of parameter block; see POP for details on each func
306  * @src: address of source memory area
307  * @src_len: length of src operand in bytes
308  *
309  * Returns 0 for the query func, number of processed bytes for digest funcs
310  */
311 static inline int cpacf_kmac(long func, void *param,
312                              const u8 *src, long src_len)
313 {
314         register unsigned long r0 asm("0") = (unsigned long) func;
315         register unsigned long r1 asm("1") = (unsigned long) param;
316         register unsigned long r2 asm("2") = (unsigned long) src;
317         register unsigned long r3 asm("3") = (unsigned long) src_len;
318
319         asm volatile(
320                 "0:     .insn   rre,%[opc] << 16,0,%[src]\n"
321                 "       brc     1,0b\n" /* handle partial completion */
322                 : [src] "+a" (r2), [len] "+d" (r3)
323                 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KMAC)
324                 : "cc", "memory");
325
326         return src_len - r3;
327 }
328
329 /**
330  * cpacf_kmctr() - executes the KMCTR (CIPHER MESSAGE WITH COUNTER) instruction
331  * @func: the function code passed to KMCTR; see CPACF_KMCTR_xxx defines
332  * @param: address of parameter block; see POP for details on each func
333  * @dest: address of destination memory area
334  * @src: address of source memory area
335  * @src_len: length of src operand in bytes
336  * @counter: address of counter value
337  *
338  * Returns 0 for the query func, number of processed bytes for
339  * encryption/decryption funcs
340  */
341 static inline int cpacf_kmctr(long func, void *param, u8 *dest,
342                               const u8 *src, long src_len, u8 *counter)
343 {
344         register unsigned long r0 asm("0") = (unsigned long) func;
345         register unsigned long r1 asm("1") = (unsigned long) param;
346         register unsigned long r2 asm("2") = (unsigned long) src;
347         register unsigned long r3 asm("3") = (unsigned long) src_len;
348         register unsigned long r4 asm("4") = (unsigned long) dest;
349         register unsigned long r6 asm("6") = (unsigned long) counter;
350
351         asm volatile(
352                 "0:     .insn   rrf,%[opc] << 16,%[dst],%[src],%[ctr],0\n"
353                 "       brc     1,0b\n" /* handle partial completion */
354                 : [src] "+a" (r2), [len] "+d" (r3),
355                   [dst] "+a" (r4), [ctr] "+a" (r6)
356                 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KMCTR)
357                 : "cc", "memory");
358
359         return src_len - r3;
360 }
361
362 /**
363  * cpacf_ppno() - executes the PPNO (PERFORM PSEUDORANDOM NUMBER OPERATION)
364  *                instruction
365  * @func: the function code passed to PPNO; see CPACF_PPNO_xxx defines
366  * @param: address of parameter block; see POP for details on each func
367  * @dest: address of destination memory area
368  * @dest_len: size of destination memory area in bytes
369  * @seed: address of seed data
370  * @seed_len: size of seed data in bytes
371  *
372  * Returns 0 for the query func, number of random bytes stored in
373  * dest buffer for generate function
374  */
375 static inline int cpacf_ppno(long func, void *param,
376                              u8 *dest, long dest_len,
377                              const u8 *seed, long seed_len)
378 {
379         register unsigned long r0 asm("0") = (unsigned long) func;
380         register unsigned long r1 asm("1") = (unsigned long) param;
381         register unsigned long r2 asm("2") = (unsigned long) dest;
382         register unsigned long r3 asm("3") = (unsigned long) dest_len;
383         register unsigned long r4 asm("4") = (unsigned long) seed;
384         register unsigned long r5 asm("5") = (unsigned long) seed_len;
385
386         asm volatile (
387                 "0:     .insn   rre,%[opc] << 16,%[dst],%[seed]\n"
388                 "       brc     1,0b\n"   /* handle partial completion */
389                 : [dst] "+a" (r2), [dlen] "+d" (r3)
390                 : [fc] "d" (r0), [pba] "a" (r1),
391                   [seed] "a" (r4), [slen] "d" (r5), [opc] "i" (CPACF_PPNO)
392                 : "cc", "memory");
393
394         return dest_len - r3;
395 }
396
397 /**
398  * cpacf_pcc() - executes the PCC (PERFORM CRYPTOGRAPHIC COMPUTATION)
399  *               instruction
400  * @func: the function code passed to PCC; see CPACF_KM_xxx defines
401  * @param: address of parameter block; see POP for details on each func
402  *
403  * Returns 0.
404  */
405 static inline int cpacf_pcc(long func, void *param)
406 {
407         register unsigned long r0 asm("0") = (unsigned long) func;
408         register unsigned long r1 asm("1") = (unsigned long) param;
409
410         asm volatile(
411                 "0:     .insn   rre,%[opc] << 16,0,0\n" /* PCC opcode */
412                 "       brc     1,0b\n" /* handle partial completion */
413                 :
414                 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_PCC)
415                 : "cc", "memory");
416
417         return 0;
418 }
419
420 #endif  /* _ASM_S390_CPACF_H */