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