xref: /openbmc/linux/arch/s390/include/asm/cpacf.h (revision edc63a3785b48455e05793e848f0174e21f38d09)
1 /*
2  * CP Assist for Cryptographic Functions (CPACF)
3  *
4  * Copyright IBM Corp. 2003, 2016
5  * Author(s): Thomas Spatzier
6  *	      Jan Glauber
7  *	      Harald Freudenberger (freude@de.ibm.com)
8  *	      Martin Schwidefsky <schwidefsky@de.ibm.com>
9  */
10 #ifndef _ASM_S390_CPACF_H
11 #define _ASM_S390_CPACF_H
12 
13 #include <asm/facility.h>
14 
15 /*
16  * Instruction opcodes for the CPACF instructions
17  */
18 #define CPACF_KMAC		0xb91e		/* MSA	*/
19 #define CPACF_KM		0xb92e		/* MSA	*/
20 #define CPACF_KMC		0xb92f		/* MSA	*/
21 #define CPACF_KIMD		0xb93e		/* MSA	*/
22 #define CPACF_KLMD		0xb93f		/* MSA	*/
23 #define CPACF_PCKMO		0xb928		/* MSA3 */
24 #define CPACF_KMF		0xb92a		/* MSA4 */
25 #define CPACF_KMO		0xb92b		/* MSA4 */
26 #define CPACF_PCC		0xb92c		/* MSA4 */
27 #define CPACF_KMCTR		0xb92d		/* MSA4 */
28 #define CPACF_PPNO		0xb93c		/* MSA5 */
29 
30 /*
31  * Decryption modifier bit
32  */
33 #define CPACF_DECRYPT		0x80
34 
35 /*
36  * Function codes for the KM (CIPHER MESSAGE) instruction
37  */
38 #define CPACF_KM_QUERY		0x00
39 #define CPACF_KM_DEA		0x01
40 #define CPACF_KM_TDEA_128	0x02
41 #define CPACF_KM_TDEA_192	0x03
42 #define CPACF_KM_AES_128	0x12
43 #define CPACF_KM_AES_192	0x13
44 #define CPACF_KM_AES_256	0x14
45 #define CPACF_KM_XTS_128	0x32
46 #define CPACF_KM_XTS_256	0x34
47 
48 /*
49  * Function codes for the KMC (CIPHER MESSAGE WITH CHAINING)
50  * instruction
51  */
52 #define CPACF_KMC_QUERY		0x00
53 #define CPACF_KMC_DEA		0x01
54 #define CPACF_KMC_TDEA_128	0x02
55 #define CPACF_KMC_TDEA_192	0x03
56 #define CPACF_KMC_AES_128	0x12
57 #define CPACF_KMC_AES_192	0x13
58 #define CPACF_KMC_AES_256	0x14
59 #define CPACF_KMC_PRNG		0x43
60 
61 /*
62  * Function codes for the KMCTR (CIPHER MESSAGE WITH COUNTER)
63  * instruction
64  */
65 #define CPACF_KMCTR_QUERY	0x00
66 #define CPACF_KMCTR_DEA		0x01
67 #define CPACF_KMCTR_TDEA_128	0x02
68 #define CPACF_KMCTR_TDEA_192	0x03
69 #define CPACF_KMCTR_AES_128	0x12
70 #define CPACF_KMCTR_AES_192	0x13
71 #define CPACF_KMCTR_AES_256	0x14
72 
73 /*
74  * Function codes for the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST)
75  * instruction
76  */
77 #define CPACF_KIMD_QUERY	0x00
78 #define CPACF_KIMD_SHA_1	0x01
79 #define CPACF_KIMD_SHA_256	0x02
80 #define CPACF_KIMD_SHA_512	0x03
81 #define CPACF_KIMD_GHASH	0x41
82 
83 /*
84  * Function codes for the KLMD (COMPUTE LAST MESSAGE DIGEST)
85  * instruction
86  */
87 #define CPACF_KLMD_QUERY	0x00
88 #define CPACF_KLMD_SHA_1	0x01
89 #define CPACF_KLMD_SHA_256	0x02
90 #define CPACF_KLMD_SHA_512	0x03
91 
92 /*
93  * function codes for the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE)
94  * instruction
95  */
96 #define CPACF_KMAC_QUERY	0x00
97 #define CPACF_KMAC_DEA		0x01
98 #define CPACF_KMAC_TDEA_128	0x02
99 #define CPACF_KMAC_TDEA_192	0x03
100 
101 /*
102  * Function codes for the PPNO (PERFORM PSEUDORANDOM NUMBER OPERATION)
103  * instruction
104  */
105 #define CPACF_PPNO_QUERY		0x00
106 #define CPACF_PPNO_SHA512_DRNG_GEN	0x03
107 #define CPACF_PPNO_SHA512_DRNG_SEED	0x83
108 
109 /**
110  * cpacf_query() - check if a specific CPACF function is available
111  * @opcode: the opcode of the crypto instruction
112  * @func: the function code to test for
113  *
114  * Executes the query function for the given crypto instruction @opcode
115  * and checks if @func is available
116  *
117  * Returns 1 if @func is available for @opcode, 0 otherwise
118  */
119 static inline void __cpacf_query(unsigned int opcode, unsigned char *status)
120 {
121 	typedef struct { unsigned char _[16]; } status_type;
122 	register unsigned long r0 asm("0") = 0;	/* query function */
123 	register unsigned long r1 asm("1") = (unsigned long) status;
124 
125 	asm volatile(
126 		"	spm 0\n" /* pckmo doesn't change the cc */
127 		/* Parameter registers are ignored, but may not be 0 */
128 		"0:	.insn	rrf,%[opc] << 16,2,2,2,0\n"
129 		"	brc	1,0b\n"	/* handle partial completion */
130 		: "=m" (*(status_type *) status)
131 		: [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (opcode)
132 		: "cc");
133 }
134 
135 static inline int cpacf_query(unsigned int opcode, unsigned int func)
136 {
137 	unsigned char status[16];
138 
139 	switch (opcode) {
140 	case CPACF_KMAC:
141 	case CPACF_KM:
142 	case CPACF_KMC:
143 	case CPACF_KIMD:
144 	case CPACF_KLMD:
145 		if (!test_facility(17))	/* check for MSA */
146 			return 0;
147 		break;
148 	case CPACF_PCKMO:
149 		if (!test_facility(76))	/* check for MSA3 */
150 			return 0;
151 		break;
152 	case CPACF_KMF:
153 	case CPACF_KMO:
154 	case CPACF_PCC:
155 	case CPACF_KMCTR:
156 		if (!test_facility(77))	/* check for MSA4 */
157 			return 0;
158 		break;
159 	case CPACF_PPNO:
160 		if (!test_facility(57))	/* check for MSA5 */
161 			return 0;
162 		break;
163 	default:
164 		BUG();
165 	}
166 	__cpacf_query(opcode, status);
167 	return (status[func >> 3] & (0x80 >> (func & 7))) != 0;
168 }
169 
170 /**
171  * cpacf_km() - executes the KM (CIPHER MESSAGE) instruction
172  * @func: the function code passed to KM; see CPACF_KM_xxx defines
173  * @param: address of parameter block; see POP for details on each func
174  * @dest: address of destination memory area
175  * @src: address of source memory area
176  * @src_len: length of src operand in bytes
177  *
178  * Returns 0 for the query func, number of processed bytes for
179  * encryption/decryption funcs
180  */
181 static inline int cpacf_km(unsigned long func, void *param,
182 			   u8 *dest, const u8 *src, long src_len)
183 {
184 	register unsigned long r0 asm("0") = (unsigned long) func;
185 	register unsigned long r1 asm("1") = (unsigned long) param;
186 	register unsigned long r2 asm("2") = (unsigned long) src;
187 	register unsigned long r3 asm("3") = (unsigned long) src_len;
188 	register unsigned long r4 asm("4") = (unsigned long) dest;
189 
190 	asm volatile(
191 		"0:	.insn	rre,%[opc] << 16,%[dst],%[src]\n"
192 		"	brc	1,0b\n" /* handle partial completion */
193 		: [src] "+a" (r2), [len] "+d" (r3), [dst] "+a" (r4)
194 		: [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KM)
195 		: "cc", "memory");
196 
197 	return src_len - r3;
198 }
199 
200 /**
201  * cpacf_kmc() - executes the KMC (CIPHER MESSAGE WITH CHAINING) instruction
202  * @func: the function code passed to KM; see CPACF_KMC_xxx defines
203  * @param: address of parameter block; see POP for details on each func
204  * @dest: address of destination memory area
205  * @src: address of source memory area
206  * @src_len: length of src operand in bytes
207  *
208  * Returns 0 for the query func, number of processed bytes for
209  * encryption/decryption funcs
210  */
211 static inline int cpacf_kmc(unsigned long func, void *param,
212 			    u8 *dest, const u8 *src, long src_len)
213 {
214 	register unsigned long r0 asm("0") = (unsigned long) func;
215 	register unsigned long r1 asm("1") = (unsigned long) param;
216 	register unsigned long r2 asm("2") = (unsigned long) src;
217 	register unsigned long r3 asm("3") = (unsigned long) src_len;
218 	register unsigned long r4 asm("4") = (unsigned long) dest;
219 
220 	asm volatile(
221 		"0:	.insn	rre,%[opc] << 16,%[dst],%[src]\n"
222 		"	brc	1,0b\n" /* handle partial completion */
223 		: [src] "+a" (r2), [len] "+d" (r3), [dst] "+a" (r4)
224 		: [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KMC)
225 		: "cc", "memory");
226 
227 	return src_len - r3;
228 }
229 
230 /**
231  * cpacf_kimd() - executes the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST)
232  *		  instruction
233  * @func: the function code passed to KM; see CPACF_KIMD_xxx defines
234  * @param: address of parameter block; see POP for details on each func
235  * @src: address of source memory area
236  * @src_len: length of src operand in bytes
237  *
238  * Returns 0 for the query func, number of processed bytes for digest funcs
239  */
240 static inline int cpacf_kimd(unsigned long func, void *param,
241 			     const u8 *src, long src_len)
242 {
243 	register unsigned long r0 asm("0") = (unsigned long) func;
244 	register unsigned long r1 asm("1") = (unsigned long) param;
245 	register unsigned long r2 asm("2") = (unsigned long) src;
246 	register unsigned long r3 asm("3") = (unsigned long) src_len;
247 
248 	asm volatile(
249 		"0:	.insn	rre,%[opc] << 16,0,%[src]\n"
250 		"	brc	1,0b\n" /* handle partial completion */
251 		: [src] "+a" (r2), [len] "+d" (r3)
252 		: [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KIMD)
253 		: "cc", "memory");
254 
255 	return src_len - r3;
256 }
257 
258 /**
259  * cpacf_klmd() - executes the KLMD (COMPUTE LAST MESSAGE DIGEST) instruction
260  * @func: the function code passed to KM; see CPACF_KLMD_xxx defines
261  * @param: address of parameter block; see POP for details on each func
262  * @src: address of source memory area
263  * @src_len: length of src operand in bytes
264  *
265  * Returns 0 for the query func, number of processed bytes for digest funcs
266  */
267 static inline int cpacf_klmd(unsigned long func, void *param,
268 			     const u8 *src, long src_len)
269 {
270 	register unsigned long r0 asm("0") = (unsigned long) func;
271 	register unsigned long r1 asm("1") = (unsigned long) param;
272 	register unsigned long r2 asm("2") = (unsigned long) src;
273 	register unsigned long r3 asm("3") = (unsigned long) src_len;
274 
275 	asm volatile(
276 		"0:	.insn	rre,%[opc] << 16,0,%[src]\n"
277 		"	brc	1,0b\n" /* handle partial completion */
278 		: [src] "+a" (r2), [len] "+d" (r3)
279 		: [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KLMD)
280 		: "cc", "memory");
281 
282 	return src_len - r3;
283 }
284 
285 /**
286  * cpacf_kmac() - executes the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE)
287  *		  instruction
288  * @func: the function code passed to KM; see CPACF_KMAC_xxx defines
289  * @param: address of parameter block; see POP for details on each func
290  * @src: address of source memory area
291  * @src_len: length of src operand in bytes
292  *
293  * Returns 0 for the query func, number of processed bytes for digest funcs
294  */
295 static inline int cpacf_kmac(unsigned long func, void *param,
296 			     const u8 *src, long src_len)
297 {
298 	register unsigned long r0 asm("0") = (unsigned long) func;
299 	register unsigned long r1 asm("1") = (unsigned long) param;
300 	register unsigned long r2 asm("2") = (unsigned long) src;
301 	register unsigned long r3 asm("3") = (unsigned long) src_len;
302 
303 	asm volatile(
304 		"0:	.insn	rre,%[opc] << 16,0,%[src]\n"
305 		"	brc	1,0b\n" /* handle partial completion */
306 		: [src] "+a" (r2), [len] "+d" (r3)
307 		: [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KMAC)
308 		: "cc", "memory");
309 
310 	return src_len - r3;
311 }
312 
313 /**
314  * cpacf_kmctr() - executes the KMCTR (CIPHER MESSAGE WITH COUNTER) instruction
315  * @func: the function code passed to KMCTR; see CPACF_KMCTR_xxx defines
316  * @param: address of parameter block; see POP for details on each func
317  * @dest: address of destination memory area
318  * @src: address of source memory area
319  * @src_len: length of src operand in bytes
320  * @counter: address of counter value
321  *
322  * Returns 0 for the query func, number of processed bytes for
323  * encryption/decryption funcs
324  */
325 static inline int cpacf_kmctr(unsigned long func, void *param, u8 *dest,
326 			      const u8 *src, long src_len, u8 *counter)
327 {
328 	register unsigned long r0 asm("0") = (unsigned long) func;
329 	register unsigned long r1 asm("1") = (unsigned long) param;
330 	register unsigned long r2 asm("2") = (unsigned long) src;
331 	register unsigned long r3 asm("3") = (unsigned long) src_len;
332 	register unsigned long r4 asm("4") = (unsigned long) dest;
333 	register unsigned long r6 asm("6") = (unsigned long) counter;
334 
335 	asm volatile(
336 		"0:	.insn	rrf,%[opc] << 16,%[dst],%[src],%[ctr],0\n"
337 		"	brc	1,0b\n" /* handle partial completion */
338 		: [src] "+a" (r2), [len] "+d" (r3),
339 		  [dst] "+a" (r4), [ctr] "+a" (r6)
340 		: [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KMCTR)
341 		: "cc", "memory");
342 
343 	return src_len - r3;
344 }
345 
346 /**
347  * cpacf_ppno() - executes the PPNO (PERFORM PSEUDORANDOM NUMBER OPERATION)
348  *		  instruction
349  * @func: the function code passed to PPNO; see CPACF_PPNO_xxx defines
350  * @param: address of parameter block; see POP for details on each func
351  * @dest: address of destination memory area
352  * @dest_len: size of destination memory area in bytes
353  * @seed: address of seed data
354  * @seed_len: size of seed data in bytes
355  *
356  * Returns 0 for the query func, number of random bytes stored in
357  * dest buffer for generate function
358  */
359 static inline int cpacf_ppno(unsigned long func, void *param,
360 			     u8 *dest, long dest_len,
361 			     const u8 *seed, long seed_len)
362 {
363 	register unsigned long r0 asm("0") = (unsigned long) func;
364 	register unsigned long r1 asm("1") = (unsigned long) param;
365 	register unsigned long r2 asm("2") = (unsigned long) dest;
366 	register unsigned long r3 asm("3") = (unsigned long) dest_len;
367 	register unsigned long r4 asm("4") = (unsigned long) seed;
368 	register unsigned long r5 asm("5") = (unsigned long) seed_len;
369 
370 	asm volatile (
371 		"0:	.insn	rre,%[opc] << 16,%[dst],%[seed]\n"
372 		"	brc	1,0b\n"	  /* handle partial completion */
373 		: [dst] "+a" (r2), [dlen] "+d" (r3)
374 		: [fc] "d" (r0), [pba] "a" (r1),
375 		  [seed] "a" (r4), [slen] "d" (r5), [opc] "i" (CPACF_PPNO)
376 		: "cc", "memory");
377 
378 	return dest_len - r3;
379 }
380 
381 /**
382  * cpacf_pcc() - executes the PCC (PERFORM CRYPTOGRAPHIC COMPUTATION)
383  *		 instruction
384  * @func: the function code passed to PCC; see CPACF_KM_xxx defines
385  * @param: address of parameter block; see POP for details on each func
386  *
387  * Returns 0.
388  */
389 static inline int cpacf_pcc(unsigned long func, void *param)
390 {
391 	register unsigned long r0 asm("0") = (unsigned long) func;
392 	register unsigned long r1 asm("1") = (unsigned long) param;
393 
394 	asm volatile(
395 		"0:	.insn	rre,%[opc] << 16,0,0\n" /* PCC opcode */
396 		"	brc	1,0b\n" /* handle partial completion */
397 		:
398 		: [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_PCC)
399 		: "cc", "memory");
400 
401 	return 0;
402 }
403 
404 #endif	/* _ASM_S390_CPACF_H */
405