File Index
Files and directories
Sign in
FILE

cpacf.h

File preview with syntax highlighting.

linux-6.19.14/arch/s390/include/asm/cpacf.h C
  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
216typedef struct { unsigned char bytes[16]; } cpacf_mask_t;
217typedef 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 */
224void __cpacf_bad_opcode(void);
225
226static __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
239static __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
252static __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
300static __always_inline void __cpacf_query(unsigned int opcode,
301					  cpacf_mask_t *mask)
302{
303	__cpacf_query_insn(opcode, mask, CPACF_FC_QUERY);
304}
305
306static __always_inline int __cpacf_check_opcode(unsigned int opcode)
307{
308	switch (opcode) {
309	case CPACF_KMAC:
310	case CPACF_KM:
311	case CPACF_KMC:
312	case CPACF_KIMD:
313	case CPACF_KLMD:
314		return test_facility(17);	/* check for MSA */
315	case CPACF_PCKMO:
316		return test_facility(76);	/* check for MSA3 */
317	case CPACF_KMF:
318	case CPACF_KMO:
319	case CPACF_PCC:
320	case CPACF_KMCTR:
321		return test_facility(77);	/* check for MSA4 */
322	case CPACF_PRNO:
323		return test_facility(57);	/* check for MSA5 */
324	case CPACF_KMA:
325		return test_facility(146);	/* check for MSA8 */
326	case CPACF_KDSA:
327		return test_facility(155);	/* check for MSA9 */
328	default:
329		__cpacf_bad_opcode();
330		return 0;
331	}
332}
333
334/**
335 * cpacf_query() - Query the function code mask for this CPACF opcode
336 * @opcode: the opcode of the crypto instruction
337 * @mask: ptr to struct cpacf_mask_t
338 *
339 * Executes the query function for the given crypto instruction @opcode
340 * and checks if @func is available
341 *
342 * On success 1 is returned and the mask is filled with the function
343 * code mask for this CPACF opcode, otherwise 0 is returned.
344 */
345static __always_inline int cpacf_query(unsigned int opcode, cpacf_mask_t *mask)
346{
347	if (__cpacf_check_opcode(opcode)) {
348		__cpacf_query(opcode, mask);
349		return 1;
350	}
351	memset(mask, 0, sizeof(*mask));
352	return 0;
353}
354
355static inline int cpacf_test_func(cpacf_mask_t *mask, unsigned int func)
356{
357	return (mask->bytes[func >> 3] & (0x80 >> (func & 7))) != 0;
358}
359
360static __always_inline int cpacf_query_func(unsigned int opcode,
361					    unsigned int func)
362{
363	cpacf_mask_t mask;
364
365	if (cpacf_query(opcode, &mask))
366		return cpacf_test_func(&mask, func);
367	return 0;
368}
369
370static __always_inline void __cpacf_qai(unsigned int opcode, cpacf_qai_t *qai)
371{
372	__cpacf_query_insn(opcode, qai, CPACF_FC_QUERY_AUTH_INFO);
373}
374
375/**
376 * cpacf_qai() - Get the query authentication information for a CPACF opcode
377 * @opcode: the opcode of the crypto instruction
378 * @mask: ptr to struct cpacf_qai_t
379 *
380 * Executes the query authentication information function for the given crypto
381 * instruction @opcode and checks if @func is available
382 *
383 * On success 1 is returned and the mask is filled with the query authentication
384 * information for this CPACF opcode, otherwise 0 is returned.
385 */
386static __always_inline int cpacf_qai(unsigned int opcode, cpacf_qai_t *qai)
387{
388	if (cpacf_query_func(opcode, CPACF_FC_QUERY_AUTH_INFO)) {
389		__cpacf_qai(opcode, qai);
390		return 1;
391	}
392	memset(qai, 0, sizeof(*qai));
393	return 0;
394}
395
396/**
397 * cpacf_km() - executes the KM (CIPHER MESSAGE) instruction
398 * @func: the function code passed to KM; see CPACF_KM_xxx defines
399 * @param: address of parameter block; see POP for details on each func
400 * @dest: address of destination memory area
401 * @src: address of source memory area
402 * @src_len: length of src operand in bytes
403 *
404 * Returns 0 for the query func, number of processed bytes for
405 * encryption/decryption funcs
406 */
407static inline int cpacf_km(unsigned long func, void *param,
408			   u8 *dest, const u8 *src, long src_len)
409{
410	union register_pair d, s;
411
412	d.even = (unsigned long)dest;
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,%[dst],%[src]\n"
419		"	brc	1,0b" /* handle partial completion */
420		: [src] "+&d" (s.pair), [dst] "+&d" (d.pair)
421		: [fc] "d" (func), [pba] "d" ((unsigned long)param),
422		  [opc] "i" (CPACF_KM)
423		: "cc", "memory", "0", "1");
424
425	return src_len - s.odd;
426}
427
428/**
429 * cpacf_kmc() - executes the KMC (CIPHER MESSAGE WITH CHAINING) instruction
430 * @func: the function code passed to KM; see CPACF_KMC_xxx defines
431 * @param: address of parameter block; see POP for details on each func
432 * @dest: address of destination memory area
433 * @src: address of source memory area
434 * @src_len: length of src operand in bytes
435 *
436 * Returns 0 for the query func, number of processed bytes for
437 * encryption/decryption funcs
438 */
439static inline int cpacf_kmc(unsigned long func, void *param,
440			    u8 *dest, const u8 *src, long src_len)
441{
442	union register_pair d, s;
443
444	d.even = (unsigned long)dest;
445	s.even = (unsigned long)src;
446	s.odd  = (unsigned long)src_len;
447	asm volatile(
448		"	lgr	0,%[fc]\n"
449		"	lgr	1,%[pba]\n"
450		"0:	.insn	rre,%[opc] << 16,%[dst],%[src]\n"
451		"	brc	1,0b" /* handle partial completion */
452		: [src] "+&d" (s.pair), [dst] "+&d" (d.pair)
453		: [fc] "d" (func), [pba] "d" ((unsigned long)param),
454		  [opc] "i" (CPACF_KMC)
455		: "cc", "memory", "0", "1");
456
457	return src_len - s.odd;
458}
459
460/**
461 * cpacf_kimd() - executes the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST)
462 *		  instruction
463 * @func: the function code passed to KM; see CPACF_KIMD_xxx defines
464 * @param: address of parameter block; see POP for details on each func
465 * @src: address of source memory area
466 * @src_len: length of src operand in bytes
467 */
468static inline void cpacf_kimd(unsigned long func, void *param,
469			      const u8 *src, long src_len)
470{
471	union register_pair s;
472
473	s.even = (unsigned long)src;
474	s.odd  = (unsigned long)src_len;
475	asm volatile(
476		"	lgr	0,%[fc]\n"
477		"	lgr	1,%[pba]\n"
478		"0:	.insn	rrf,%[opc] << 16,0,%[src],8,0\n"
479		"	brc	1,0b" /* handle partial completion */
480		: [src] "+&d" (s.pair)
481		: [fc] "d" (func), [pba] "d" ((unsigned long)(param)),
482		  [opc] "i" (CPACF_KIMD)
483		: "cc", "memory", "0", "1");
484}
485
486/**
487 * cpacf_klmd() - executes the KLMD (COMPUTE LAST MESSAGE DIGEST) instruction
488 * @func: the function code passed to KM; see CPACF_KLMD_xxx defines
489 * @param: address of parameter block; see POP for details on each func
490 * @src: address of source memory area
491 * @src_len: length of src operand in bytes
492 */
493static inline void cpacf_klmd(unsigned long func, void *param,
494			      const u8 *src, long src_len)
495{
496	union register_pair s;
497
498	s.even = (unsigned long)src;
499	s.odd  = (unsigned long)src_len;
500	asm volatile(
501		"	lgr	0,%[fc]\n"
502		"	lgr	1,%[pba]\n"
503		"0:	.insn	rrf,%[opc] << 16,0,%[src],8,0\n"
504		"	brc	1,0b" /* handle partial completion */
505		: [src] "+&d" (s.pair)
506		: [fc] "d" (func), [pba] "d" ((unsigned long)param),
507		  [opc] "i" (CPACF_KLMD)
508		: "cc", "memory", "0", "1");
509}
510
511/**
512 * _cpacf_kmac() - executes the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE)
513 * instruction and updates flags in gr0
514 * @gr0: pointer to gr0 (fc and flags) passed to KMAC; see CPACF_KMAC_xxx defines
515 * @param: address of parameter block; see POP for details on each func
516 * @src: address of source memory area
517 * @src_len: length of src operand in bytes
518 *
519 * Returns 0 for the query func, number of processed bytes for digest funcs
520 */
521static inline int _cpacf_kmac(unsigned long *gr0, void *param,
522			      const u8 *src, long src_len)
523{
524	union register_pair s;
525
526	s.even = (unsigned long)src;
527	s.odd  = (unsigned long)src_len;
528	asm volatile(
529		"	lgr	0,%[r0]\n"
530		"	lgr	1,%[pba]\n"
531		"0:	.insn	rre,%[opc] << 16,0,%[src]\n"
532		"	brc	1,0b\n" /* handle partial completion */
533		"	lgr	%[r0],0"
534		: [r0] "+d" (*gr0), [src] "+&d" (s.pair)
535		: [pba] "d" ((unsigned long)param),
536		  [opc] "i" (CPACF_KMAC)
537		: "cc", "memory", "0", "1");
538
539	return src_len - s.odd;
540}
541
542/**
543 * cpacf_kmac() - executes the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE)
544 * instruction
545 * @func: function code passed to KMAC; see CPACF_KMAC_xxx defines
546 * @param: address of parameter block; see POP for details on each func
547 * @src: address of source memory area
548 * @src_len: length of src operand in bytes
549 *
550 * Returns 0 for the query func, number of processed bytes for digest funcs
551 */
552static inline int cpacf_kmac(unsigned long func, void *param,
553			     const u8 *src, long src_len)
554{
555	return _cpacf_kmac(&func, param, src, src_len);
556}
557
558/**
559 * cpacf_kmctr() - executes the KMCTR (CIPHER MESSAGE WITH COUNTER) instruction
560 * @func: the function code passed to KMCTR; see CPACF_KMCTR_xxx defines
561 * @param: address of parameter block; see POP for details on each func
562 * @dest: address of destination memory area
563 * @src: address of source memory area
564 * @src_len: length of src operand in bytes
565 * @counter: address of counter value
566 *
567 * Returns 0 for the query func, number of processed bytes for
568 * encryption/decryption funcs
569 */
570static inline int cpacf_kmctr(unsigned long func, void *param, u8 *dest,
571			      const u8 *src, long src_len, u8 *counter)
572{
573	union register_pair d, s, c;
574
575	d.even = (unsigned long)dest;
576	s.even = (unsigned long)src;
577	s.odd  = (unsigned long)src_len;
578	c.even = (unsigned long)counter;
579	asm volatile(
580		"	lgr	0,%[fc]\n"
581		"	lgr	1,%[pba]\n"
582		"0:	.insn	rrf,%[opc] << 16,%[dst],%[src],%[ctr],0\n"
583		"	brc	1,0b" /* handle partial completion */
584		: [src] "+&d" (s.pair), [dst] "+&d" (d.pair),
585		  [ctr] "+&d" (c.pair)
586		: [fc] "d" (func), [pba] "d" ((unsigned long)param),
587		  [opc] "i" (CPACF_KMCTR)
588		: "cc", "memory", "0", "1");
589
590	return src_len - s.odd;
591}
592
593/**
594 * cpacf_prno() - executes the PRNO (PERFORM RANDOM NUMBER OPERATION)
595 *		  instruction
596 * @func: the function code passed to PRNO; see CPACF_PRNO_xxx defines
597 * @param: address of parameter block; see POP for details on each func
598 * @dest: address of destination memory area
599 * @dest_len: size of destination memory area in bytes
600 * @seed: address of seed data
601 * @seed_len: size of seed data in bytes
602 */
603static inline void cpacf_prno(unsigned long func, void *param,
604			      u8 *dest, unsigned long dest_len,
605			      const u8 *seed, unsigned long seed_len)
606{
607	union register_pair d, s;
608
609	d.even = (unsigned long)dest;
610	d.odd  = (unsigned long)dest_len;
611	s.even = (unsigned long)seed;
612	s.odd  = (unsigned long)seed_len;
613	asm volatile (
614		"	lgr	0,%[fc]\n"
615		"	lgr	1,%[pba]\n"
616		"0:	.insn	rre,%[opc] << 16,%[dst],%[seed]\n"
617		"	brc	1,0b"	  /* handle partial completion */
618		: [dst] "+&d" (d.pair)
619		: [fc] "d" (func), [pba] "d" ((unsigned long)param),
620		  [seed] "d" (s.pair), [opc] "i" (CPACF_PRNO)
621		: "cc", "memory", "0", "1");
622}
623
624/**
625 * cpacf_trng() - executes the TRNG subfunction of the PRNO instruction
626 * @ucbuf: buffer for unconditioned data
627 * @ucbuf_len: amount of unconditioned data to fetch in bytes
628 * @cbuf: buffer for conditioned data
629 * @cbuf_len: amount of conditioned data to fetch in bytes
630 */
631static inline void cpacf_trng(u8 *ucbuf, unsigned long ucbuf_len,
632			      u8 *cbuf, unsigned long cbuf_len)
633{
634	union register_pair u, c;
635
636	u.even = (unsigned long)ucbuf;
637	u.odd  = (unsigned long)ucbuf_len;
638	c.even = (unsigned long)cbuf;
639	c.odd  = (unsigned long)cbuf_len;
640	asm volatile (
641		"	lghi	0,%[fc]\n"
642		"0:	.insn	rre,%[opc] << 16,%[ucbuf],%[cbuf]\n"
643		"	brc	1,0b"	  /* handle partial completion */
644		: [ucbuf] "+&d" (u.pair), [cbuf] "+&d" (c.pair)
645		: [fc] "K" (CPACF_PRNO_TRNG), [opc] "i" (CPACF_PRNO)
646		: "cc", "memory", "0");
647	kmsan_unpoison_memory(ucbuf, ucbuf_len);
648	kmsan_unpoison_memory(cbuf, cbuf_len);
649}
650
651/**
652 * cpacf_pcc() - executes the PCC (PERFORM CRYPTOGRAPHIC COMPUTATION)
653 *		 instruction
654 * @func: the function code passed to PCC; see CPACF_KM_xxx defines
655 * @param: address of parameter block; see POP for details on each func
656 *
657 * Returns the condition code, this is
658 * 0 - cc code 0 (normal completion)
659 * 1 - cc code 1 (protected key wkvp mismatch or src operand out of range)
660 * 2 - cc code 2 (something invalid, scalar multiply infinity, ...)
661 * Condition code 3 (partial completion) is handled within the asm code
662 * and never returned.
663 */
664static inline int cpacf_pcc(unsigned long func, void *param)
665{
666	int cc;
667
668	asm volatile(
669		"	lgr	0,%[fc]\n"
670		"	lgr	1,%[pba]\n"
671		"0:	.insn	rre,%[opc] << 16,0,0\n" /* PCC opcode */
672		"	brc	1,0b\n" /* handle partial completion */
673		CC_IPM(cc)
674		: CC_OUT(cc, cc)
675		: [fc] "d" (func), [pba] "d" ((unsigned long)param),
676		  [opc] "i" (CPACF_PCC)
677		: CC_CLOBBER_LIST("memory", "0", "1"));
678
679	return CC_TRANSFORM(cc);
680}
681
682/**
683 * cpacf_pckmo() - executes the PCKMO (PERFORM CRYPTOGRAPHIC KEY
684 *		  MANAGEMENT) instruction
685 * @func: the function code passed to PCKMO; see CPACF_PCKMO_xxx defines
686 * @param: address of parameter block; see POP for details on each func
687 *
688 * Returns 0.
689 */
690static inline void cpacf_pckmo(long func, void *param)
691{
692	asm volatile(
693		"	lgr	0,%[fc]\n"
694		"	lgr	1,%[pba]\n"
695		"       .insn   rre,%[opc] << 16,0,0" /* PCKMO opcode */
696		:
697		: [fc] "d" (func), [pba] "d" ((unsigned long)param),
698		  [opc] "i" (CPACF_PCKMO)
699		: "cc", "memory", "0", "1");
700}
701
702/**
703 * cpacf_kma() - executes the KMA (CIPHER MESSAGE WITH AUTHENTICATION)
704 *		 instruction
705 * @func: the function code passed to KMA; see CPACF_KMA_xxx defines
706 * @param: address of parameter block; see POP for details on each func
707 * @dest: address of destination memory area
708 * @src: address of source memory area
709 * @src_len: length of src operand in bytes
710 * @aad: address of additional authenticated data memory area
711 * @aad_len: length of aad operand in bytes
712 */
713static inline void cpacf_kma(unsigned long func, void *param, u8 *dest,
714			     const u8 *src, unsigned long src_len,
715			     const u8 *aad, unsigned long aad_len)
716{
717	union register_pair d, s, a;
718
719	d.even = (unsigned long)dest;
720	s.even = (unsigned long)src;
721	s.odd  = (unsigned long)src_len;
722	a.even = (unsigned long)aad;
723	a.odd  = (unsigned long)aad_len;
724	asm volatile(
725		"	lgr	0,%[fc]\n"
726		"	lgr	1,%[pba]\n"
727		"0:	.insn	rrf,%[opc] << 16,%[dst],%[src],%[aad],0\n"
728		"	brc	1,0b"	/* handle partial completion */
729		: [dst] "+&d" (d.pair), [src] "+&d" (s.pair),
730		  [aad] "+&d" (a.pair)
731		: [fc] "d" (func), [pba] "d" ((unsigned long)param),
732		  [opc] "i" (CPACF_KMA)
733		: "cc", "memory", "0", "1");
734}
735
736#endif	/* _ASM_S390_CPACF_H */
~/ Read-only file preview.