xref: /linux/arch/s390/include/asm/cpacf.h (revision 32e8bd64)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * CP Assist for Cryptographic Functions (CPACF)
4  *
5  * Copyright IBM Corp. 2003, 2023
6  * Author(s): Thomas Spatzier
7  *	      Jan Glauber
8  *	      Harald Freudenberger (freude@de.ibm.com)
9  *	      Martin Schwidefsky <schwidefsky@de.ibm.com>
10  */
11 #ifndef _ASM_S390_CPACF_H
12 #define _ASM_S390_CPACF_H
13 
14 #include <asm/facility.h>
15 
16 /*
17  * Instruction opcodes for the CPACF instructions
18  */
19 #define CPACF_KMAC		0xb91e		/* MSA	*/
20 #define CPACF_KM		0xb92e		/* MSA	*/
21 #define CPACF_KMC		0xb92f		/* MSA	*/
22 #define CPACF_KIMD		0xb93e		/* MSA	*/
23 #define CPACF_KLMD		0xb93f		/* MSA	*/
24 #define CPACF_PCKMO		0xb928		/* MSA3 */
25 #define CPACF_KMF		0xb92a		/* MSA4 */
26 #define CPACF_KMO		0xb92b		/* MSA4 */
27 #define CPACF_PCC		0xb92c		/* MSA4 */
28 #define CPACF_KMCTR		0xb92d		/* MSA4 */
29 #define CPACF_PRNO		0xb93c		/* MSA5 */
30 #define CPACF_KMA		0xb929		/* MSA8 */
31 #define CPACF_KDSA		0xb93a		/* MSA9 */
32 
33 /*
34  * En/decryption modifier bits
35  */
36 #define CPACF_ENCRYPT		0x00
37 #define CPACF_DECRYPT		0x80
38 
39 /*
40  * Function codes for the KM (CIPHER MESSAGE) instruction
41  */
42 #define CPACF_KM_QUERY		0x00
43 #define CPACF_KM_DEA		0x01
44 #define CPACF_KM_TDEA_128	0x02
45 #define CPACF_KM_TDEA_192	0x03
46 #define CPACF_KM_AES_128	0x12
47 #define CPACF_KM_AES_192	0x13
48 #define CPACF_KM_AES_256	0x14
49 #define CPACF_KM_PAES_128	0x1a
50 #define CPACF_KM_PAES_192	0x1b
51 #define CPACF_KM_PAES_256	0x1c
52 #define CPACF_KM_XTS_128	0x32
53 #define CPACF_KM_XTS_256	0x34
54 #define CPACF_KM_PXTS_128	0x3a
55 #define CPACF_KM_PXTS_256	0x3c
56 
57 /*
58  * Function codes for the KMC (CIPHER MESSAGE WITH CHAINING)
59  * instruction
60  */
61 #define CPACF_KMC_QUERY		0x00
62 #define CPACF_KMC_DEA		0x01
63 #define CPACF_KMC_TDEA_128	0x02
64 #define CPACF_KMC_TDEA_192	0x03
65 #define CPACF_KMC_AES_128	0x12
66 #define CPACF_KMC_AES_192	0x13
67 #define CPACF_KMC_AES_256	0x14
68 #define CPACF_KMC_PAES_128	0x1a
69 #define CPACF_KMC_PAES_192	0x1b
70 #define CPACF_KMC_PAES_256	0x1c
71 #define CPACF_KMC_PRNG		0x43
72 
73 /*
74  * Function codes for the KMCTR (CIPHER MESSAGE WITH COUNTER)
75  * instruction
76  */
77 #define CPACF_KMCTR_QUERY	0x00
78 #define CPACF_KMCTR_DEA		0x01
79 #define CPACF_KMCTR_TDEA_128	0x02
80 #define CPACF_KMCTR_TDEA_192	0x03
81 #define CPACF_KMCTR_AES_128	0x12
82 #define CPACF_KMCTR_AES_192	0x13
83 #define CPACF_KMCTR_AES_256	0x14
84 #define CPACF_KMCTR_PAES_128	0x1a
85 #define CPACF_KMCTR_PAES_192	0x1b
86 #define CPACF_KMCTR_PAES_256	0x1c
87 
88 /*
89  * Function codes for the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST)
90  * instruction
91  */
92 #define CPACF_KIMD_QUERY	0x00
93 #define CPACF_KIMD_SHA_1	0x01
94 #define CPACF_KIMD_SHA_256	0x02
95 #define CPACF_KIMD_SHA_512	0x03
96 #define CPACF_KIMD_SHA3_224	0x20
97 #define CPACF_KIMD_SHA3_256	0x21
98 #define CPACF_KIMD_SHA3_384	0x22
99 #define CPACF_KIMD_SHA3_512	0x23
100 #define CPACF_KIMD_GHASH	0x41
101 
102 /*
103  * Function codes for the KLMD (COMPUTE LAST MESSAGE DIGEST)
104  * instruction
105  */
106 #define CPACF_KLMD_QUERY	0x00
107 #define CPACF_KLMD_SHA_1	0x01
108 #define CPACF_KLMD_SHA_256	0x02
109 #define CPACF_KLMD_SHA_512	0x03
110 #define CPACF_KLMD_SHA3_224	0x20
111 #define CPACF_KLMD_SHA3_256	0x21
112 #define CPACF_KLMD_SHA3_384	0x22
113 #define CPACF_KLMD_SHA3_512	0x23
114 
115 /*
116  * function codes for the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE)
117  * instruction
118  */
119 #define CPACF_KMAC_QUERY	0x00
120 #define CPACF_KMAC_DEA		0x01
121 #define CPACF_KMAC_TDEA_128	0x02
122 #define CPACF_KMAC_TDEA_192	0x03
123 
124 /*
125  * Function codes for the PCKMO (PERFORM CRYPTOGRAPHIC KEY MANAGEMENT)
126  * instruction
127  */
128 #define CPACF_PCKMO_QUERY		0x00
129 #define CPACF_PCKMO_ENC_DES_KEY		0x01
130 #define CPACF_PCKMO_ENC_TDES_128_KEY	0x02
131 #define CPACF_PCKMO_ENC_TDES_192_KEY	0x03
132 #define CPACF_PCKMO_ENC_AES_128_KEY	0x12
133 #define CPACF_PCKMO_ENC_AES_192_KEY	0x13
134 #define CPACF_PCKMO_ENC_AES_256_KEY	0x14
135 #define CPACF_PCKMO_ENC_ECC_P256_KEY	0x20
136 #define CPACF_PCKMO_ENC_ECC_P384_KEY	0x21
137 #define CPACF_PCKMO_ENC_ECC_P521_KEY	0x22
138 #define CPACF_PCKMO_ENC_ECC_ED25519_KEY	0x28
139 #define CPACF_PCKMO_ENC_ECC_ED448_KEY	0x29
140 
141 /*
142  * Function codes for the PRNO (PERFORM RANDOM NUMBER OPERATION)
143  * instruction
144  */
145 #define CPACF_PRNO_QUERY		0x00
146 #define CPACF_PRNO_SHA512_DRNG_GEN	0x03
147 #define CPACF_PRNO_SHA512_DRNG_SEED	0x83
148 #define CPACF_PRNO_TRNG_Q_R2C_RATIO	0x70
149 #define CPACF_PRNO_TRNG			0x72
150 
151 /*
152  * Function codes for the KMA (CIPHER MESSAGE WITH AUTHENTICATION)
153  * instruction
154  */
155 #define CPACF_KMA_QUERY		0x00
156 #define CPACF_KMA_GCM_AES_128	0x12
157 #define CPACF_KMA_GCM_AES_192	0x13
158 #define CPACF_KMA_GCM_AES_256	0x14
159 
160 /*
161  * Flags for the KMA (CIPHER MESSAGE WITH AUTHENTICATION) instruction
162  */
163 #define CPACF_KMA_LPC	0x100	/* Last-Plaintext/Ciphertext */
164 #define CPACF_KMA_LAAD	0x200	/* Last-AAD */
165 #define CPACF_KMA_HS	0x400	/* Hash-subkey Supplied */
166 
167 typedef struct { unsigned char bytes[16]; } cpacf_mask_t;
168 
169 /*
170  * Prototype for a not existing function to produce a link
171  * error if __cpacf_query() or __cpacf_check_opcode() is used
172  * with an invalid compile time const opcode.
173  */
174 void __cpacf_bad_opcode(void);
175 
__cpacf_query_rre(u32 opc,u8 r1,u8 r2,cpacf_mask_t * mask)176 static __always_inline void __cpacf_query_rre(u32 opc, u8 r1, u8 r2,
177 					      cpacf_mask_t *mask)
178 {
179 	asm volatile(
180 		"	la	%%r1,%[mask]\n"
181 		"	xgr	%%r0,%%r0\n"
182 		"	.insn	rre,%[opc] << 16,%[r1],%[r2]\n"
183 		: [mask] "=R" (*mask)
184 		: [opc] "i" (opc),
185 		  [r1] "i" (r1), [r2] "i" (r2)
186 		: "cc", "r0", "r1");
187 }
188 
__cpacf_query_rrf(u32 opc,u8 r1,u8 r2,u8 r3,u8 m4,cpacf_mask_t * mask)189 static __always_inline void __cpacf_query_rrf(u32 opc,
190 					      u8 r1, u8 r2, u8 r3, u8 m4,
191 					      cpacf_mask_t *mask)
192 {
193 	asm volatile(
194 		"	la	%%r1,%[mask]\n"
195 		"	xgr	%%r0,%%r0\n"
196 		"	.insn	rrf,%[opc] << 16,%[r1],%[r2],%[r3],%[m4]\n"
197 		: [mask] "=R" (*mask)
198 		: [opc] "i" (opc), [r1] "i" (r1), [r2] "i" (r2),
199 		  [r3] "i" (r3), [m4] "i" (m4)
200 		: "cc", "r0", "r1");
201 }
202 
__cpacf_query(unsigned int opcode,cpacf_mask_t * mask)203 static __always_inline void __cpacf_query(unsigned int opcode,
204 					  cpacf_mask_t *mask)
205 {
206 	switch (opcode) {
207 	case CPACF_KDSA:
208 		__cpacf_query_rre(CPACF_KDSA, 0, 2, mask);
209 		break;
210 	case CPACF_KIMD:
211 		__cpacf_query_rre(CPACF_KIMD, 0, 2, mask);
212 		break;
213 	case CPACF_KLMD:
214 		__cpacf_query_rre(CPACF_KLMD, 0, 2, mask);
215 		break;
216 	case CPACF_KM:
217 		__cpacf_query_rre(CPACF_KM, 2, 4, mask);
218 		break;
219 	case CPACF_KMA:
220 		__cpacf_query_rrf(CPACF_KMA, 2, 4, 6, 0, mask);
221 		break;
222 	case CPACF_KMAC:
223 		__cpacf_query_rre(CPACF_KMAC, 0, 2, mask);
224 		break;
225 	case CPACF_KMC:
226 		__cpacf_query_rre(CPACF_KMC, 2, 4, mask);
227 		break;
228 	case CPACF_KMCTR:
229 		__cpacf_query_rrf(CPACF_KMCTR, 2, 4, 6, 0, mask);
230 		break;
231 	case CPACF_KMF:
232 		__cpacf_query_rre(CPACF_KMF, 2, 4, mask);
233 		break;
234 	case CPACF_KMO:
235 		__cpacf_query_rre(CPACF_KMO, 2, 4, mask);
236 		break;
237 	case CPACF_PCC:
238 		__cpacf_query_rre(CPACF_PCC, 0, 0, mask);
239 		break;
240 	case CPACF_PCKMO:
241 		__cpacf_query_rre(CPACF_PCKMO, 0, 0, mask);
242 		break;
243 	case CPACF_PRNO:
244 		__cpacf_query_rre(CPACF_PRNO, 2, 4, mask);
245 		break;
246 	default:
247 		__cpacf_bad_opcode();
248 	}
249 }
250 
__cpacf_check_opcode(unsigned int opcode)251 static __always_inline int __cpacf_check_opcode(unsigned int opcode)
252 {
253 	switch (opcode) {
254 	case CPACF_KMAC:
255 	case CPACF_KM:
256 	case CPACF_KMC:
257 	case CPACF_KIMD:
258 	case CPACF_KLMD:
259 		return test_facility(17);	/* check for MSA */
260 	case CPACF_PCKMO:
261 		return test_facility(76);	/* check for MSA3 */
262 	case CPACF_KMF:
263 	case CPACF_KMO:
264 	case CPACF_PCC:
265 	case CPACF_KMCTR:
266 		return test_facility(77);	/* check for MSA4 */
267 	case CPACF_PRNO:
268 		return test_facility(57);	/* check for MSA5 */
269 	case CPACF_KMA:
270 		return test_facility(146);	/* check for MSA8 */
271 	default:
272 		__cpacf_bad_opcode();
273 		return 0;
274 	}
275 }
276 
277 /**
278  * cpacf_query() - check if a specific CPACF function is available
279  * @opcode: the opcode of the crypto instruction
280  * @func: the function code to test for
281  *
282  * Executes the query function for the given crypto instruction @opcode
283  * and checks if @func is available
284  *
285  * Returns 1 if @func is available for @opcode, 0 otherwise
286  */
cpacf_query(unsigned int opcode,cpacf_mask_t * mask)287 static __always_inline int cpacf_query(unsigned int opcode, cpacf_mask_t *mask)
288 {
289 	if (__cpacf_check_opcode(opcode)) {
290 		__cpacf_query(opcode, mask);
291 		return 1;
292 	}
293 	memset(mask, 0, sizeof(*mask));
294 	return 0;
295 }
296 
cpacf_test_func(cpacf_mask_t * mask,unsigned int func)297 static inline int cpacf_test_func(cpacf_mask_t *mask, unsigned int func)
298 {
299 	return (mask->bytes[func >> 3] & (0x80 >> (func & 7))) != 0;
300 }
301 
cpacf_query_func(unsigned int opcode,unsigned int func)302 static __always_inline int cpacf_query_func(unsigned int opcode, unsigned int func)
303 {
304 	cpacf_mask_t mask;
305 
306 	if (cpacf_query(opcode, &mask))
307 		return cpacf_test_func(&mask, func);
308 	return 0;
309 }
310 
311 /**
312  * cpacf_km() - executes the KM (CIPHER MESSAGE) instruction
313  * @func: the function code passed to KM; see CPACF_KM_xxx defines
314  * @param: address of parameter block; see POP for details on each func
315  * @dest: address of destination memory area
316  * @src: address of source memory area
317  * @src_len: length of src operand in bytes
318  *
319  * Returns 0 for the query func, number of processed bytes for
320  * encryption/decryption funcs
321  */
cpacf_km(unsigned long func,void * param,u8 * dest,const u8 * src,long src_len)322 static inline int cpacf_km(unsigned long func, void *param,
323 			   u8 *dest, const u8 *src, long src_len)
324 {
325 	union register_pair d, s;
326 
327 	d.even = (unsigned long)dest;
328 	s.even = (unsigned long)src;
329 	s.odd  = (unsigned long)src_len;
330 	asm volatile(
331 		"	lgr	0,%[fc]\n"
332 		"	lgr	1,%[pba]\n"
333 		"0:	.insn	rre,%[opc] << 16,%[dst],%[src]\n"
334 		"	brc	1,0b\n" /* handle partial completion */
335 		: [src] "+&d" (s.pair), [dst] "+&d" (d.pair)
336 		: [fc] "d" (func), [pba] "d" ((unsigned long)param),
337 		  [opc] "i" (CPACF_KM)
338 		: "cc", "memory", "0", "1");
339 
340 	return src_len - s.odd;
341 }
342 
343 /**
344  * cpacf_kmc() - executes the KMC (CIPHER MESSAGE WITH CHAINING) instruction
345  * @func: the function code passed to KM; see CPACF_KMC_xxx defines
346  * @param: address of parameter block; see POP for details on each func
347  * @dest: address of destination memory area
348  * @src: address of source memory area
349  * @src_len: length of src operand in bytes
350  *
351  * Returns 0 for the query func, number of processed bytes for
352  * encryption/decryption funcs
353  */
cpacf_kmc(unsigned long func,void * param,u8 * dest,const u8 * src,long src_len)354 static inline int cpacf_kmc(unsigned long func, void *param,
355 			    u8 *dest, const u8 *src, long src_len)
356 {
357 	union register_pair d, s;
358 
359 	d.even = (unsigned long)dest;
360 	s.even = (unsigned long)src;
361 	s.odd  = (unsigned long)src_len;
362 	asm volatile(
363 		"	lgr	0,%[fc]\n"
364 		"	lgr	1,%[pba]\n"
365 		"0:	.insn	rre,%[opc] << 16,%[dst],%[src]\n"
366 		"	brc	1,0b\n" /* handle partial completion */
367 		: [src] "+&d" (s.pair), [dst] "+&d" (d.pair)
368 		: [fc] "d" (func), [pba] "d" ((unsigned long)param),
369 		  [opc] "i" (CPACF_KMC)
370 		: "cc", "memory", "0", "1");
371 
372 	return src_len - s.odd;
373 }
374 
375 /**
376  * cpacf_kimd() - executes the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST)
377  *		  instruction
378  * @func: the function code passed to KM; see CPACF_KIMD_xxx defines
379  * @param: address of parameter block; see POP for details on each func
380  * @src: address of source memory area
381  * @src_len: length of src operand in bytes
382  */
cpacf_kimd(unsigned long func,void * param,const u8 * src,long src_len)383 static inline void cpacf_kimd(unsigned long func, void *param,
384 			      const u8 *src, long src_len)
385 {
386 	union register_pair s;
387 
388 	s.even = (unsigned long)src;
389 	s.odd  = (unsigned long)src_len;
390 	asm volatile(
391 		"	lgr	0,%[fc]\n"
392 		"	lgr	1,%[pba]\n"
393 		"0:	.insn	rre,%[opc] << 16,0,%[src]\n"
394 		"	brc	1,0b\n" /* handle partial completion */
395 		: [src] "+&d" (s.pair)
396 		: [fc] "d" (func), [pba] "d" ((unsigned long)(param)),
397 		  [opc] "i" (CPACF_KIMD)
398 		: "cc", "memory", "0", "1");
399 }
400 
401 /**
402  * cpacf_klmd() - executes the KLMD (COMPUTE LAST MESSAGE DIGEST) instruction
403  * @func: the function code passed to KM; see CPACF_KLMD_xxx defines
404  * @param: address of parameter block; see POP for details on each func
405  * @src: address of source memory area
406  * @src_len: length of src operand in bytes
407  */
cpacf_klmd(unsigned long func,void * param,const u8 * src,long src_len)408 static inline void cpacf_klmd(unsigned long func, void *param,
409 			      const u8 *src, long src_len)
410 {
411 	union register_pair s;
412 
413 	s.even = (unsigned long)src;
414 	s.odd  = (unsigned long)src_len;
415 	asm volatile(
416 		"	lgr	0,%[fc]\n"
417 		"	lgr	1,%[pba]\n"
418 		"0:	.insn	rre,%[opc] << 16,0,%[src]\n"
419 		"	brc	1,0b\n" /* handle partial completion */
420 		: [src] "+&d" (s.pair)
421 		: [fc] "d" (func), [pba] "d" ((unsigned long)param),
422 		  [opc] "i" (CPACF_KLMD)
423 		: "cc", "memory", "0", "1");
424 }
425 
426 /**
427  * cpacf_kmac() - executes the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE)
428  *		  instruction
429  * @func: the function code passed to KM; see CPACF_KMAC_xxx defines
430  * @param: address of parameter block; see POP for details on each func
431  * @src: address of source memory area
432  * @src_len: length of src operand in bytes
433  *
434  * Returns 0 for the query func, number of processed bytes for digest funcs
435  */
cpacf_kmac(unsigned long func,void * param,const u8 * src,long src_len)436 static inline int cpacf_kmac(unsigned long func, void *param,
437 			     const u8 *src, long src_len)
438 {
439 	union register_pair s;
440 
441 	s.even = (unsigned long)src;
442 	s.odd  = (unsigned long)src_len;
443 	asm volatile(
444 		"	lgr	0,%[fc]\n"
445 		"	lgr	1,%[pba]\n"
446 		"0:	.insn	rre,%[opc] << 16,0,%[src]\n"
447 		"	brc	1,0b\n" /* handle partial completion */
448 		: [src] "+&d" (s.pair)
449 		: [fc] "d" (func), [pba] "d" ((unsigned long)param),
450 		  [opc] "i" (CPACF_KMAC)
451 		: "cc", "memory", "0", "1");
452 
453 	return src_len - s.odd;
454 }
455 
456 /**
457  * cpacf_kmctr() - executes the KMCTR (CIPHER MESSAGE WITH COUNTER) instruction
458  * @func: the function code passed to KMCTR; see CPACF_KMCTR_xxx defines
459  * @param: address of parameter block; see POP for details on each func
460  * @dest: address of destination memory area
461  * @src: address of source memory area
462  * @src_len: length of src operand in bytes
463  * @counter: address of counter value
464  *
465  * Returns 0 for the query func, number of processed bytes for
466  * encryption/decryption funcs
467  */
cpacf_kmctr(unsigned long func,void * param,u8 * dest,const u8 * src,long src_len,u8 * counter)468 static inline int cpacf_kmctr(unsigned long func, void *param, u8 *dest,
469 			      const u8 *src, long src_len, u8 *counter)
470 {
471 	union register_pair d, s, c;
472 
473 	d.even = (unsigned long)dest;
474 	s.even = (unsigned long)src;
475 	s.odd  = (unsigned long)src_len;
476 	c.even = (unsigned long)counter;
477 	asm volatile(
478 		"	lgr	0,%[fc]\n"
479 		"	lgr	1,%[pba]\n"
480 		"0:	.insn	rrf,%[opc] << 16,%[dst],%[src],%[ctr],0\n"
481 		"	brc	1,0b\n" /* handle partial completion */
482 		: [src] "+&d" (s.pair), [dst] "+&d" (d.pair),
483 		  [ctr] "+&d" (c.pair)
484 		: [fc] "d" (func), [pba] "d" ((unsigned long)param),
485 		  [opc] "i" (CPACF_KMCTR)
486 		: "cc", "memory", "0", "1");
487 
488 	return src_len - s.odd;
489 }
490 
491 /**
492  * cpacf_prno() - executes the PRNO (PERFORM RANDOM NUMBER OPERATION)
493  *		  instruction
494  * @func: the function code passed to PRNO; see CPACF_PRNO_xxx defines
495  * @param: address of parameter block; see POP for details on each func
496  * @dest: address of destination memory area
497  * @dest_len: size of destination memory area in bytes
498  * @seed: address of seed data
499  * @seed_len: size of seed data in bytes
500  */
cpacf_prno(unsigned long func,void * param,u8 * dest,unsigned long dest_len,const u8 * seed,unsigned long seed_len)501 static inline void cpacf_prno(unsigned long func, void *param,
502 			      u8 *dest, unsigned long dest_len,
503 			      const u8 *seed, unsigned long seed_len)
504 {
505 	union register_pair d, s;
506 
507 	d.even = (unsigned long)dest;
508 	d.odd  = (unsigned long)dest_len;
509 	s.even = (unsigned long)seed;
510 	s.odd  = (unsigned long)seed_len;
511 	asm volatile (
512 		"	lgr	0,%[fc]\n"
513 		"	lgr	1,%[pba]\n"
514 		"0:	.insn	rre,%[opc] << 16,%[dst],%[seed]\n"
515 		"	brc	1,0b\n"	  /* handle partial completion */
516 		: [dst] "+&d" (d.pair)
517 		: [fc] "d" (func), [pba] "d" ((unsigned long)param),
518 		  [seed] "d" (s.pair), [opc] "i" (CPACF_PRNO)
519 		: "cc", "memory", "0", "1");
520 }
521 
522 /**
523  * cpacf_trng() - executes the TRNG subfunction of the PRNO instruction
524  * @ucbuf: buffer for unconditioned data
525  * @ucbuf_len: amount of unconditioned data to fetch in bytes
526  * @cbuf: buffer for conditioned data
527  * @cbuf_len: amount of conditioned data to fetch in bytes
528  */
cpacf_trng(u8 * ucbuf,unsigned long ucbuf_len,u8 * cbuf,unsigned long cbuf_len)529 static inline void cpacf_trng(u8 *ucbuf, unsigned long ucbuf_len,
530 			      u8 *cbuf, unsigned long cbuf_len)
531 {
532 	union register_pair u, c;
533 
534 	u.even = (unsigned long)ucbuf;
535 	u.odd  = (unsigned long)ucbuf_len;
536 	c.even = (unsigned long)cbuf;
537 	c.odd  = (unsigned long)cbuf_len;
538 	asm volatile (
539 		"	lghi	0,%[fc]\n"
540 		"0:	.insn	rre,%[opc] << 16,%[ucbuf],%[cbuf]\n"
541 		"	brc	1,0b\n"	  /* handle partial completion */
542 		: [ucbuf] "+&d" (u.pair), [cbuf] "+&d" (c.pair)
543 		: [fc] "K" (CPACF_PRNO_TRNG), [opc] "i" (CPACF_PRNO)
544 		: "cc", "memory", "0");
545 }
546 
547 /**
548  * cpacf_pcc() - executes the PCC (PERFORM CRYPTOGRAPHIC COMPUTATION)
549  *		 instruction
550  * @func: the function code passed to PCC; see CPACF_KM_xxx defines
551  * @param: address of parameter block; see POP for details on each func
552  */
cpacf_pcc(unsigned long func,void * param)553 static inline void cpacf_pcc(unsigned long func, void *param)
554 {
555 	asm volatile(
556 		"	lgr	0,%[fc]\n"
557 		"	lgr	1,%[pba]\n"
558 		"0:	.insn	rre,%[opc] << 16,0,0\n" /* PCC opcode */
559 		"	brc	1,0b\n" /* handle partial completion */
560 		:
561 		: [fc] "d" (func), [pba] "d" ((unsigned long)param),
562 		  [opc] "i" (CPACF_PCC)
563 		: "cc", "memory", "0", "1");
564 }
565 
566 /**
567  * cpacf_pckmo() - executes the PCKMO (PERFORM CRYPTOGRAPHIC KEY
568  *		  MANAGEMENT) instruction
569  * @func: the function code passed to PCKMO; see CPACF_PCKMO_xxx defines
570  * @param: address of parameter block; see POP for details on each func
571  *
572  * Returns 0.
573  */
cpacf_pckmo(long func,void * param)574 static inline void cpacf_pckmo(long func, void *param)
575 {
576 	asm volatile(
577 		"	lgr	0,%[fc]\n"
578 		"	lgr	1,%[pba]\n"
579 		"       .insn   rre,%[opc] << 16,0,0\n" /* PCKMO opcode */
580 		:
581 		: [fc] "d" (func), [pba] "d" ((unsigned long)param),
582 		  [opc] "i" (CPACF_PCKMO)
583 		: "cc", "memory", "0", "1");
584 }
585 
586 /**
587  * cpacf_kma() - executes the KMA (CIPHER MESSAGE WITH AUTHENTICATION)
588  *		 instruction
589  * @func: the function code passed to KMA; see CPACF_KMA_xxx defines
590  * @param: address of parameter block; see POP for details on each func
591  * @dest: address of destination memory area
592  * @src: address of source memory area
593  * @src_len: length of src operand in bytes
594  * @aad: address of additional authenticated data memory area
595  * @aad_len: length of aad operand in bytes
596  */
cpacf_kma(unsigned long func,void * param,u8 * dest,const u8 * src,unsigned long src_len,const u8 * aad,unsigned long aad_len)597 static inline void cpacf_kma(unsigned long func, void *param, u8 *dest,
598 			     const u8 *src, unsigned long src_len,
599 			     const u8 *aad, unsigned long aad_len)
600 {
601 	union register_pair d, s, a;
602 
603 	d.even = (unsigned long)dest;
604 	s.even = (unsigned long)src;
605 	s.odd  = (unsigned long)src_len;
606 	a.even = (unsigned long)aad;
607 	a.odd  = (unsigned long)aad_len;
608 	asm volatile(
609 		"	lgr	0,%[fc]\n"
610 		"	lgr	1,%[pba]\n"
611 		"0:	.insn	rrf,%[opc] << 16,%[dst],%[src],%[aad],0\n"
612 		"	brc	1,0b\n"	/* handle partial completion */
613 		: [dst] "+&d" (d.pair), [src] "+&d" (s.pair),
614 		  [aad] "+&d" (a.pair)
615 		: [fc] "d" (func), [pba] "d" ((unsigned long)param),
616 		  [opc] "i" (CPACF_KMA)
617 		: "cc", "memory", "0", "1");
618 }
619 
620 #endif	/* _ASM_S390_CPACF_H */
621