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