xref: /linux/arch/s390/include/asm/cpacf.h (revision 8b2ada27dc1045e8191673bf769a1136ce8a0127)
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 typedef struct { unsigned char bytes[16]; } cpacf_mask_t;
110 
111 /**
112  * cpacf_query() - check if a specific CPACF function is available
113  * @opcode: the opcode of the crypto instruction
114  * @func: the function code to test for
115  *
116  * Executes the query function for the given crypto instruction @opcode
117  * and checks if @func is available
118  *
119  * Returns 1 if @func is available for @opcode, 0 otherwise
120  */
121 static inline void __cpacf_query(unsigned int opcode, cpacf_mask_t *mask)
122 {
123 	register unsigned long r0 asm("0") = 0;	/* query function */
124 	register unsigned long r1 asm("1") = (unsigned long) mask;
125 
126 	asm volatile(
127 		"	spm 0\n" /* pckmo doesn't change the cc */
128 		/* Parameter registers are ignored, but may not be 0 */
129 		"0:	.insn	rrf,%[opc] << 16,2,2,2,0\n"
130 		"	brc	1,0b\n"	/* handle partial completion */
131 		: "=m" (*mask)
132 		: [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (opcode)
133 		: "cc");
134 }
135 
136 static inline int __cpacf_check_opcode(unsigned int opcode)
137 {
138 	switch (opcode) {
139 	case CPACF_KMAC:
140 	case CPACF_KM:
141 	case CPACF_KMC:
142 	case CPACF_KIMD:
143 	case CPACF_KLMD:
144 		return test_facility(17);	/* check for MSA */
145 	case CPACF_PCKMO:
146 		return test_facility(76);	/* check for MSA3 */
147 	case CPACF_KMF:
148 	case CPACF_KMO:
149 	case CPACF_PCC:
150 	case CPACF_KMCTR:
151 		return test_facility(77);	/* check for MSA4 */
152 	case CPACF_PPNO:
153 		return test_facility(57);	/* check for MSA5 */
154 	default:
155 		BUG();
156 	}
157 }
158 
159 static inline int cpacf_query(unsigned int opcode, cpacf_mask_t *mask)
160 {
161 	if (__cpacf_check_opcode(opcode)) {
162 		__cpacf_query(opcode, mask);
163 		return 1;
164 	}
165 	memset(mask, 0, sizeof(*mask));
166 	return 0;
167 }
168 
169 static inline int cpacf_test_func(cpacf_mask_t *mask, unsigned int func)
170 {
171 	return (mask->bytes[func >> 3] & (0x80 >> (func & 7))) != 0;
172 }
173 
174 static inline int cpacf_query_func(unsigned int opcode, unsigned int func)
175 {
176 	cpacf_mask_t mask;
177 
178 	if (cpacf_query(opcode, &mask))
179 		return cpacf_test_func(&mask, func);
180 	return 0;
181 }
182 
183 /**
184  * cpacf_km() - executes the KM (CIPHER MESSAGE) instruction
185  * @func: the function code passed to KM; see CPACF_KM_xxx defines
186  * @param: address of parameter block; see POP for details on each func
187  * @dest: address of destination memory area
188  * @src: address of source memory area
189  * @src_len: length of src operand in bytes
190  *
191  * Returns 0 for the query func, number of processed bytes for
192  * encryption/decryption funcs
193  */
194 static inline int cpacf_km(unsigned long func, void *param,
195 			   u8 *dest, const u8 *src, long src_len)
196 {
197 	register unsigned long r0 asm("0") = (unsigned long) func;
198 	register unsigned long r1 asm("1") = (unsigned long) param;
199 	register unsigned long r2 asm("2") = (unsigned long) src;
200 	register unsigned long r3 asm("3") = (unsigned long) src_len;
201 	register unsigned long r4 asm("4") = (unsigned long) dest;
202 
203 	asm volatile(
204 		"0:	.insn	rre,%[opc] << 16,%[dst],%[src]\n"
205 		"	brc	1,0b\n" /* handle partial completion */
206 		: [src] "+a" (r2), [len] "+d" (r3), [dst] "+a" (r4)
207 		: [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KM)
208 		: "cc", "memory");
209 
210 	return src_len - r3;
211 }
212 
213 /**
214  * cpacf_kmc() - executes the KMC (CIPHER MESSAGE WITH CHAINING) instruction
215  * @func: the function code passed to KM; see CPACF_KMC_xxx defines
216  * @param: address of parameter block; see POP for details on each func
217  * @dest: address of destination memory area
218  * @src: address of source memory area
219  * @src_len: length of src operand in bytes
220  *
221  * Returns 0 for the query func, number of processed bytes for
222  * encryption/decryption funcs
223  */
224 static inline int cpacf_kmc(unsigned long func, void *param,
225 			    u8 *dest, const u8 *src, long src_len)
226 {
227 	register unsigned long r0 asm("0") = (unsigned long) func;
228 	register unsigned long r1 asm("1") = (unsigned long) param;
229 	register unsigned long r2 asm("2") = (unsigned long) src;
230 	register unsigned long r3 asm("3") = (unsigned long) src_len;
231 	register unsigned long r4 asm("4") = (unsigned long) dest;
232 
233 	asm volatile(
234 		"0:	.insn	rre,%[opc] << 16,%[dst],%[src]\n"
235 		"	brc	1,0b\n" /* handle partial completion */
236 		: [src] "+a" (r2), [len] "+d" (r3), [dst] "+a" (r4)
237 		: [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KMC)
238 		: "cc", "memory");
239 
240 	return src_len - r3;
241 }
242 
243 /**
244  * cpacf_kimd() - executes the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST)
245  *		  instruction
246  * @func: the function code passed to KM; see CPACF_KIMD_xxx defines
247  * @param: address of parameter block; see POP for details on each func
248  * @src: address of source memory area
249  * @src_len: length of src operand in bytes
250  */
251 static inline void cpacf_kimd(unsigned long func, void *param,
252 			      const u8 *src, long src_len)
253 {
254 	register unsigned long r0 asm("0") = (unsigned long) func;
255 	register unsigned long r1 asm("1") = (unsigned long) param;
256 	register unsigned long r2 asm("2") = (unsigned long) src;
257 	register unsigned long r3 asm("3") = (unsigned long) src_len;
258 
259 	asm volatile(
260 		"0:	.insn	rre,%[opc] << 16,0,%[src]\n"
261 		"	brc	1,0b\n" /* handle partial completion */
262 		: [src] "+a" (r2), [len] "+d" (r3)
263 		: [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KIMD)
264 		: "cc", "memory");
265 }
266 
267 /**
268  * cpacf_klmd() - executes the KLMD (COMPUTE LAST MESSAGE DIGEST) instruction
269  * @func: the function code passed to KM; see CPACF_KLMD_xxx defines
270  * @param: address of parameter block; see POP for details on each func
271  * @src: address of source memory area
272  * @src_len: length of src operand in bytes
273  */
274 static inline void cpacf_klmd(unsigned long func, void *param,
275 			      const u8 *src, long src_len)
276 {
277 	register unsigned long r0 asm("0") = (unsigned long) func;
278 	register unsigned long r1 asm("1") = (unsigned long) param;
279 	register unsigned long r2 asm("2") = (unsigned long) src;
280 	register unsigned long r3 asm("3") = (unsigned long) src_len;
281 
282 	asm volatile(
283 		"0:	.insn	rre,%[opc] << 16,0,%[src]\n"
284 		"	brc	1,0b\n" /* handle partial completion */
285 		: [src] "+a" (r2), [len] "+d" (r3)
286 		: [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KLMD)
287 		: "cc", "memory");
288 }
289 
290 /**
291  * cpacf_kmac() - executes the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE)
292  *		  instruction
293  * @func: the function code passed to KM; see CPACF_KMAC_xxx defines
294  * @param: address of parameter block; see POP for details on each func
295  * @src: address of source memory area
296  * @src_len: length of src operand in bytes
297  *
298  * Returns 0 for the query func, number of processed bytes for digest funcs
299  */
300 static inline int cpacf_kmac(unsigned long func, void *param,
301 			     const u8 *src, long src_len)
302 {
303 	register unsigned long r0 asm("0") = (unsigned long) func;
304 	register unsigned long r1 asm("1") = (unsigned long) param;
305 	register unsigned long r2 asm("2") = (unsigned long) src;
306 	register unsigned long r3 asm("3") = (unsigned long) src_len;
307 
308 	asm volatile(
309 		"0:	.insn	rre,%[opc] << 16,0,%[src]\n"
310 		"	brc	1,0b\n" /* handle partial completion */
311 		: [src] "+a" (r2), [len] "+d" (r3)
312 		: [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KMAC)
313 		: "cc", "memory");
314 
315 	return src_len - r3;
316 }
317 
318 /**
319  * cpacf_kmctr() - executes the KMCTR (CIPHER MESSAGE WITH COUNTER) instruction
320  * @func: the function code passed to KMCTR; see CPACF_KMCTR_xxx defines
321  * @param: address of parameter block; see POP for details on each func
322  * @dest: address of destination memory area
323  * @src: address of source memory area
324  * @src_len: length of src operand in bytes
325  * @counter: address of counter value
326  *
327  * Returns 0 for the query func, number of processed bytes for
328  * encryption/decryption funcs
329  */
330 static inline int cpacf_kmctr(unsigned long func, void *param, u8 *dest,
331 			      const u8 *src, long src_len, u8 *counter)
332 {
333 	register unsigned long r0 asm("0") = (unsigned long) func;
334 	register unsigned long r1 asm("1") = (unsigned long) param;
335 	register unsigned long r2 asm("2") = (unsigned long) src;
336 	register unsigned long r3 asm("3") = (unsigned long) src_len;
337 	register unsigned long r4 asm("4") = (unsigned long) dest;
338 	register unsigned long r6 asm("6") = (unsigned long) counter;
339 
340 	asm volatile(
341 		"0:	.insn	rrf,%[opc] << 16,%[dst],%[src],%[ctr],0\n"
342 		"	brc	1,0b\n" /* handle partial completion */
343 		: [src] "+a" (r2), [len] "+d" (r3),
344 		  [dst] "+a" (r4), [ctr] "+a" (r6)
345 		: [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KMCTR)
346 		: "cc", "memory");
347 
348 	return src_len - r3;
349 }
350 
351 /**
352  * cpacf_ppno() - executes the PPNO (PERFORM PSEUDORANDOM NUMBER OPERATION)
353  *		  instruction
354  * @func: the function code passed to PPNO; see CPACF_PPNO_xxx defines
355  * @param: address of parameter block; see POP for details on each func
356  * @dest: address of destination memory area
357  * @dest_len: size of destination memory area in bytes
358  * @seed: address of seed data
359  * @seed_len: size of seed data in bytes
360  */
361 static inline void cpacf_ppno(unsigned long func, void *param,
362 			      u8 *dest, long dest_len,
363 			      const u8 *seed, long seed_len)
364 {
365 	register unsigned long r0 asm("0") = (unsigned long) func;
366 	register unsigned long r1 asm("1") = (unsigned long) param;
367 	register unsigned long r2 asm("2") = (unsigned long) dest;
368 	register unsigned long r3 asm("3") = (unsigned long) dest_len;
369 	register unsigned long r4 asm("4") = (unsigned long) seed;
370 	register unsigned long r5 asm("5") = (unsigned long) seed_len;
371 
372 	asm volatile (
373 		"0:	.insn	rre,%[opc] << 16,%[dst],%[seed]\n"
374 		"	brc	1,0b\n"	  /* handle partial completion */
375 		: [dst] "+a" (r2), [dlen] "+d" (r3)
376 		: [fc] "d" (r0), [pba] "a" (r1),
377 		  [seed] "a" (r4), [slen] "d" (r5), [opc] "i" (CPACF_PPNO)
378 		: "cc", "memory");
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 static inline void cpacf_pcc(unsigned long func, void *param)
388 {
389 	register unsigned long r0 asm("0") = (unsigned long) func;
390 	register unsigned long r1 asm("1") = (unsigned long) param;
391 
392 	asm volatile(
393 		"0:	.insn	rre,%[opc] << 16,0,0\n" /* PCC opcode */
394 		"	brc	1,0b\n" /* handle partial completion */
395 		:
396 		: [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_PCC)
397 		: "cc", "memory");
398 }
399 
400 #endif	/* _ASM_S390_CPACF_H */
401