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