xref: /linux/arch/s390/include/asm/cpacf.h (revision 7ffe939dd9c71e149f31817394b70b52b4067304)
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  * En/decryption modifier bits
32  */
33 #define CPACF_ENCRYPT		0x00
34 #define CPACF_DECRYPT		0x80
35 
36 /*
37  * Function codes for the KM (CIPHER MESSAGE) instruction
38  */
39 #define CPACF_KM_QUERY		0x00
40 #define CPACF_KM_DEA		0x01
41 #define CPACF_KM_TDEA_128	0x02
42 #define CPACF_KM_TDEA_192	0x03
43 #define CPACF_KM_AES_128	0x12
44 #define CPACF_KM_AES_192	0x13
45 #define CPACF_KM_AES_256	0x14
46 #define CPACF_KM_PAES_128	0x1a
47 #define CPACF_KM_PAES_192	0x1b
48 #define CPACF_KM_PAES_256	0x1c
49 #define CPACF_KM_XTS_128	0x32
50 #define CPACF_KM_XTS_256	0x34
51 #define CPACF_KM_PXTS_128	0x3a
52 #define CPACF_KM_PXTS_256	0x3c
53 
54 /*
55  * Function codes for the KMC (CIPHER MESSAGE WITH CHAINING)
56  * instruction
57  */
58 #define CPACF_KMC_QUERY		0x00
59 #define CPACF_KMC_DEA		0x01
60 #define CPACF_KMC_TDEA_128	0x02
61 #define CPACF_KMC_TDEA_192	0x03
62 #define CPACF_KMC_AES_128	0x12
63 #define CPACF_KMC_AES_192	0x13
64 #define CPACF_KMC_AES_256	0x14
65 #define CPACF_KMC_PAES_128	0x1a
66 #define CPACF_KMC_PAES_192	0x1b
67 #define CPACF_KMC_PAES_256	0x1c
68 #define CPACF_KMC_PRNG		0x43
69 
70 /*
71  * Function codes for the KMCTR (CIPHER MESSAGE WITH COUNTER)
72  * instruction
73  */
74 #define CPACF_KMCTR_QUERY	0x00
75 #define CPACF_KMCTR_DEA		0x01
76 #define CPACF_KMCTR_TDEA_128	0x02
77 #define CPACF_KMCTR_TDEA_192	0x03
78 #define CPACF_KMCTR_AES_128	0x12
79 #define CPACF_KMCTR_AES_192	0x13
80 #define CPACF_KMCTR_AES_256	0x14
81 #define CPACF_KMCTR_PAES_128	0x1a
82 #define CPACF_KMCTR_PAES_192	0x1b
83 #define CPACF_KMCTR_PAES_256	0x1c
84 
85 /*
86  * Function codes for the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST)
87  * instruction
88  */
89 #define CPACF_KIMD_QUERY	0x00
90 #define CPACF_KIMD_SHA_1	0x01
91 #define CPACF_KIMD_SHA_256	0x02
92 #define CPACF_KIMD_SHA_512	0x03
93 #define CPACF_KIMD_GHASH	0x41
94 
95 /*
96  * Function codes for the KLMD (COMPUTE LAST MESSAGE DIGEST)
97  * instruction
98  */
99 #define CPACF_KLMD_QUERY	0x00
100 #define CPACF_KLMD_SHA_1	0x01
101 #define CPACF_KLMD_SHA_256	0x02
102 #define CPACF_KLMD_SHA_512	0x03
103 
104 /*
105  * function codes for the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE)
106  * instruction
107  */
108 #define CPACF_KMAC_QUERY	0x00
109 #define CPACF_KMAC_DEA		0x01
110 #define CPACF_KMAC_TDEA_128	0x02
111 #define CPACF_KMAC_TDEA_192	0x03
112 
113 /*
114  * Function codes for the PCKMO (PERFORM CRYPTOGRAPHIC KEY MANAGEMENT)
115  * instruction
116  */
117 #define CPACF_PCKMO_QUERY		0x00
118 #define CPACF_PCKMO_ENC_DES_KEY		0x01
119 #define CPACF_PCKMO_ENC_TDES_128_KEY	0x02
120 #define CPACF_PCKMO_ENC_TDES_192_KEY	0x03
121 #define CPACF_PCKMO_ENC_AES_128_KEY	0x12
122 #define CPACF_PCKMO_ENC_AES_192_KEY	0x13
123 #define CPACF_PCKMO_ENC_AES_256_KEY	0x14
124 
125 /*
126  * Function codes for the PPNO (PERFORM PSEUDORANDOM NUMBER OPERATION)
127  * instruction
128  */
129 #define CPACF_PPNO_QUERY		0x00
130 #define CPACF_PPNO_SHA512_DRNG_GEN	0x03
131 #define CPACF_PPNO_SHA512_DRNG_SEED	0x83
132 
133 typedef struct { unsigned char bytes[16]; } cpacf_mask_t;
134 
135 /**
136  * cpacf_query() - check if a specific CPACF function is available
137  * @opcode: the opcode of the crypto instruction
138  * @func: the function code to test for
139  *
140  * Executes the query function for the given crypto instruction @opcode
141  * and checks if @func is available
142  *
143  * Returns 1 if @func is available for @opcode, 0 otherwise
144  */
145 static inline void __cpacf_query(unsigned int opcode, cpacf_mask_t *mask)
146 {
147 	register unsigned long r0 asm("0") = 0;	/* query function */
148 	register unsigned long r1 asm("1") = (unsigned long) mask;
149 
150 	asm volatile(
151 		"	spm 0\n" /* pckmo doesn't change the cc */
152 		/* Parameter registers are ignored, but may not be 0 */
153 		"0:	.insn	rrf,%[opc] << 16,2,2,2,0\n"
154 		"	brc	1,0b\n"	/* handle partial completion */
155 		: "=m" (*mask)
156 		: [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (opcode)
157 		: "cc");
158 }
159 
160 static inline int __cpacf_check_opcode(unsigned int opcode)
161 {
162 	switch (opcode) {
163 	case CPACF_KMAC:
164 	case CPACF_KM:
165 	case CPACF_KMC:
166 	case CPACF_KIMD:
167 	case CPACF_KLMD:
168 		return test_facility(17);	/* check for MSA */
169 	case CPACF_PCKMO:
170 		return test_facility(76);	/* check for MSA3 */
171 	case CPACF_KMF:
172 	case CPACF_KMO:
173 	case CPACF_PCC:
174 	case CPACF_KMCTR:
175 		return test_facility(77);	/* check for MSA4 */
176 	case CPACF_PPNO:
177 		return test_facility(57);	/* check for MSA5 */
178 	default:
179 		BUG();
180 	}
181 }
182 
183 static inline int cpacf_query(unsigned int opcode, cpacf_mask_t *mask)
184 {
185 	if (__cpacf_check_opcode(opcode)) {
186 		__cpacf_query(opcode, mask);
187 		return 1;
188 	}
189 	memset(mask, 0, sizeof(*mask));
190 	return 0;
191 }
192 
193 static inline int cpacf_test_func(cpacf_mask_t *mask, unsigned int func)
194 {
195 	return (mask->bytes[func >> 3] & (0x80 >> (func & 7))) != 0;
196 }
197 
198 static inline int cpacf_query_func(unsigned int opcode, unsigned int func)
199 {
200 	cpacf_mask_t mask;
201 
202 	if (cpacf_query(opcode, &mask))
203 		return cpacf_test_func(&mask, func);
204 	return 0;
205 }
206 
207 /**
208  * cpacf_km() - executes the KM (CIPHER MESSAGE) instruction
209  * @func: the function code passed to KM; see CPACF_KM_xxx defines
210  * @param: address of parameter block; see POP for details on each func
211  * @dest: address of destination memory area
212  * @src: address of source memory area
213  * @src_len: length of src operand in bytes
214  *
215  * Returns 0 for the query func, number of processed bytes for
216  * encryption/decryption funcs
217  */
218 static inline int cpacf_km(unsigned long func, void *param,
219 			   u8 *dest, const u8 *src, long src_len)
220 {
221 	register unsigned long r0 asm("0") = (unsigned long) func;
222 	register unsigned long r1 asm("1") = (unsigned long) param;
223 	register unsigned long r2 asm("2") = (unsigned long) src;
224 	register unsigned long r3 asm("3") = (unsigned long) src_len;
225 	register unsigned long r4 asm("4") = (unsigned long) dest;
226 
227 	asm volatile(
228 		"0:	.insn	rre,%[opc] << 16,%[dst],%[src]\n"
229 		"	brc	1,0b\n" /* handle partial completion */
230 		: [src] "+a" (r2), [len] "+d" (r3), [dst] "+a" (r4)
231 		: [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KM)
232 		: "cc", "memory");
233 
234 	return src_len - r3;
235 }
236 
237 /**
238  * cpacf_kmc() - executes the KMC (CIPHER MESSAGE WITH CHAINING) instruction
239  * @func: the function code passed to KM; see CPACF_KMC_xxx defines
240  * @param: address of parameter block; see POP for details on each func
241  * @dest: address of destination memory area
242  * @src: address of source memory area
243  * @src_len: length of src operand in bytes
244  *
245  * Returns 0 for the query func, number of processed bytes for
246  * encryption/decryption funcs
247  */
248 static inline int cpacf_kmc(unsigned long func, void *param,
249 			    u8 *dest, const u8 *src, long src_len)
250 {
251 	register unsigned long r0 asm("0") = (unsigned long) func;
252 	register unsigned long r1 asm("1") = (unsigned long) param;
253 	register unsigned long r2 asm("2") = (unsigned long) src;
254 	register unsigned long r3 asm("3") = (unsigned long) src_len;
255 	register unsigned long r4 asm("4") = (unsigned long) dest;
256 
257 	asm volatile(
258 		"0:	.insn	rre,%[opc] << 16,%[dst],%[src]\n"
259 		"	brc	1,0b\n" /* handle partial completion */
260 		: [src] "+a" (r2), [len] "+d" (r3), [dst] "+a" (r4)
261 		: [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KMC)
262 		: "cc", "memory");
263 
264 	return src_len - r3;
265 }
266 
267 /**
268  * cpacf_kimd() - executes the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST)
269  *		  instruction
270  * @func: the function code passed to KM; see CPACF_KIMD_xxx defines
271  * @param: address of parameter block; see POP for details on each func
272  * @src: address of source memory area
273  * @src_len: length of src operand in bytes
274  */
275 static inline void cpacf_kimd(unsigned long func, void *param,
276 			      const u8 *src, long src_len)
277 {
278 	register unsigned long r0 asm("0") = (unsigned long) func;
279 	register unsigned long r1 asm("1") = (unsigned long) param;
280 	register unsigned long r2 asm("2") = (unsigned long) src;
281 	register unsigned long r3 asm("3") = (unsigned long) src_len;
282 
283 	asm volatile(
284 		"0:	.insn	rre,%[opc] << 16,0,%[src]\n"
285 		"	brc	1,0b\n" /* handle partial completion */
286 		: [src] "+a" (r2), [len] "+d" (r3)
287 		: [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KIMD)
288 		: "cc", "memory");
289 }
290 
291 /**
292  * cpacf_klmd() - executes the KLMD (COMPUTE LAST MESSAGE DIGEST) instruction
293  * @func: the function code passed to KM; see CPACF_KLMD_xxx defines
294  * @param: address of parameter block; see POP for details on each func
295  * @src: address of source memory area
296  * @src_len: length of src operand in bytes
297  */
298 static inline void cpacf_klmd(unsigned long func, void *param,
299 			      const u8 *src, long src_len)
300 {
301 	register unsigned long r0 asm("0") = (unsigned long) func;
302 	register unsigned long r1 asm("1") = (unsigned long) param;
303 	register unsigned long r2 asm("2") = (unsigned long) src;
304 	register unsigned long r3 asm("3") = (unsigned long) src_len;
305 
306 	asm volatile(
307 		"0:	.insn	rre,%[opc] << 16,0,%[src]\n"
308 		"	brc	1,0b\n" /* handle partial completion */
309 		: [src] "+a" (r2), [len] "+d" (r3)
310 		: [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KLMD)
311 		: "cc", "memory");
312 }
313 
314 /**
315  * cpacf_kmac() - executes the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE)
316  *		  instruction
317  * @func: the function code passed to KM; see CPACF_KMAC_xxx defines
318  * @param: address of parameter block; see POP for details on each func
319  * @src: address of source memory area
320  * @src_len: length of src operand in bytes
321  *
322  * Returns 0 for the query func, number of processed bytes for digest funcs
323  */
324 static inline int cpacf_kmac(unsigned long func, void *param,
325 			     const u8 *src, long src_len)
326 {
327 	register unsigned long r0 asm("0") = (unsigned long) func;
328 	register unsigned long r1 asm("1") = (unsigned long) param;
329 	register unsigned long r2 asm("2") = (unsigned long) src;
330 	register unsigned long r3 asm("3") = (unsigned long) src_len;
331 
332 	asm volatile(
333 		"0:	.insn	rre,%[opc] << 16,0,%[src]\n"
334 		"	brc	1,0b\n" /* handle partial completion */
335 		: [src] "+a" (r2), [len] "+d" (r3)
336 		: [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KMAC)
337 		: "cc", "memory");
338 
339 	return src_len - r3;
340 }
341 
342 /**
343  * cpacf_kmctr() - executes the KMCTR (CIPHER MESSAGE WITH COUNTER) instruction
344  * @func: the function code passed to KMCTR; see CPACF_KMCTR_xxx defines
345  * @param: address of parameter block; see POP for details on each func
346  * @dest: address of destination memory area
347  * @src: address of source memory area
348  * @src_len: length of src operand in bytes
349  * @counter: address of counter value
350  *
351  * Returns 0 for the query func, number of processed bytes for
352  * encryption/decryption funcs
353  */
354 static inline int cpacf_kmctr(unsigned long func, void *param, u8 *dest,
355 			      const u8 *src, long src_len, u8 *counter)
356 {
357 	register unsigned long r0 asm("0") = (unsigned long) func;
358 	register unsigned long r1 asm("1") = (unsigned long) param;
359 	register unsigned long r2 asm("2") = (unsigned long) src;
360 	register unsigned long r3 asm("3") = (unsigned long) src_len;
361 	register unsigned long r4 asm("4") = (unsigned long) dest;
362 	register unsigned long r6 asm("6") = (unsigned long) counter;
363 
364 	asm volatile(
365 		"0:	.insn	rrf,%[opc] << 16,%[dst],%[src],%[ctr],0\n"
366 		"	brc	1,0b\n" /* handle partial completion */
367 		: [src] "+a" (r2), [len] "+d" (r3),
368 		  [dst] "+a" (r4), [ctr] "+a" (r6)
369 		: [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KMCTR)
370 		: "cc", "memory");
371 
372 	return src_len - r3;
373 }
374 
375 /**
376  * cpacf_ppno() - executes the PPNO (PERFORM PSEUDORANDOM NUMBER OPERATION)
377  *		  instruction
378  * @func: the function code passed to PPNO; see CPACF_PPNO_xxx defines
379  * @param: address of parameter block; see POP for details on each func
380  * @dest: address of destination memory area
381  * @dest_len: size of destination memory area in bytes
382  * @seed: address of seed data
383  * @seed_len: size of seed data in bytes
384  */
385 static inline void cpacf_ppno(unsigned long func, void *param,
386 			      u8 *dest, long dest_len,
387 			      const u8 *seed, long seed_len)
388 {
389 	register unsigned long r0 asm("0") = (unsigned long) func;
390 	register unsigned long r1 asm("1") = (unsigned long) param;
391 	register unsigned long r2 asm("2") = (unsigned long) dest;
392 	register unsigned long r3 asm("3") = (unsigned long) dest_len;
393 	register unsigned long r4 asm("4") = (unsigned long) seed;
394 	register unsigned long r5 asm("5") = (unsigned long) seed_len;
395 
396 	asm volatile (
397 		"0:	.insn	rre,%[opc] << 16,%[dst],%[seed]\n"
398 		"	brc	1,0b\n"	  /* handle partial completion */
399 		: [dst] "+a" (r2), [dlen] "+d" (r3)
400 		: [fc] "d" (r0), [pba] "a" (r1),
401 		  [seed] "a" (r4), [slen] "d" (r5), [opc] "i" (CPACF_PPNO)
402 		: "cc", "memory");
403 }
404 
405 /**
406  * cpacf_pcc() - executes the PCC (PERFORM CRYPTOGRAPHIC COMPUTATION)
407  *		 instruction
408  * @func: the function code passed to PCC; see CPACF_KM_xxx defines
409  * @param: address of parameter block; see POP for details on each func
410  */
411 static inline void cpacf_pcc(unsigned long func, void *param)
412 {
413 	register unsigned long r0 asm("0") = (unsigned long) func;
414 	register unsigned long r1 asm("1") = (unsigned long) param;
415 
416 	asm volatile(
417 		"0:	.insn	rre,%[opc] << 16,0,0\n" /* PCC opcode */
418 		"	brc	1,0b\n" /* handle partial completion */
419 		:
420 		: [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_PCC)
421 		: "cc", "memory");
422 }
423 
424 /**
425  * cpacf_pckmo() - executes the PCKMO (PERFORM CRYPTOGRAPHIC KEY
426  *		  MANAGEMENT) instruction
427  * @func: the function code passed to PCKMO; see CPACF_PCKMO_xxx defines
428  * @param: address of parameter block; see POP for details on each func
429  *
430  * Returns 0.
431  */
432 static inline void cpacf_pckmo(long func, void *param)
433 {
434 	register unsigned long r0 asm("0") = (unsigned long) func;
435 	register unsigned long r1 asm("1") = (unsigned long) param;
436 
437 	asm volatile(
438 		"       .insn   rre,%[opc] << 16,0,0\n" /* PCKMO opcode */
439 		:
440 		: [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_PCKMO)
441 		: "cc", "memory");
442 }
443 
444 #endif	/* _ASM_S390_CPACF_H */
445