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