Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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_PCC 0xb92c /* MSA4 */
24#define CPACF_KMCTR 0xb92d /* MSA4 */
25#define CPACF_PPNO 0xb93c /* MSA5 */
26
27/*
28 * Function codes for the KM (CIPHER MESSAGE)
29 * instruction (0x80 is the decipher modifier bit)
30 */
31#define CPACF_KM_QUERY 0x00
32#define CPACF_KM_DEA_ENC 0x01
33#define CPACF_KM_DEA_DEC 0x81
34#define CPACF_KM_TDEA_128_ENC 0x02
35#define CPACF_KM_TDEA_128_DEC 0x82
36#define CPACF_KM_TDEA_192_ENC 0x03
37#define CPACF_KM_TDEA_192_DEC 0x83
38#define CPACF_KM_AES_128_ENC 0x12
39#define CPACF_KM_AES_128_DEC 0x92
40#define CPACF_KM_AES_192_ENC 0x13
41#define CPACF_KM_AES_192_DEC 0x93
42#define CPACF_KM_AES_256_ENC 0x14
43#define CPACF_KM_AES_256_DEC 0x94
44#define CPACF_KM_XTS_128_ENC 0x32
45#define CPACF_KM_XTS_128_DEC 0xb2
46#define CPACF_KM_XTS_256_ENC 0x34
47#define CPACF_KM_XTS_256_DEC 0xb4
48
49/*
50 * Function codes for the KMC (CIPHER MESSAGE WITH CHAINING)
51 * instruction (0x80 is the decipher modifier bit)
52 */
53#define CPACF_KMC_QUERY 0x00
54#define CPACF_KMC_DEA_ENC 0x01
55#define CPACF_KMC_DEA_DEC 0x81
56#define CPACF_KMC_TDEA_128_ENC 0x02
57#define CPACF_KMC_TDEA_128_DEC 0x82
58#define CPACF_KMC_TDEA_192_ENC 0x03
59#define CPACF_KMC_TDEA_192_DEC 0x83
60#define CPACF_KMC_AES_128_ENC 0x12
61#define CPACF_KMC_AES_128_DEC 0x92
62#define CPACF_KMC_AES_192_ENC 0x13
63#define CPACF_KMC_AES_192_DEC 0x93
64#define CPACF_KMC_AES_256_ENC 0x14
65#define CPACF_KMC_AES_256_DEC 0x94
66#define CPACF_KMC_PRNG 0x43
67
68/*
69 * Function codes for the KMCTR (CIPHER MESSAGE WITH COUNTER)
70 * instruction (0x80 is the decipher modifier bit)
71 */
72#define CPACF_KMCTR_QUERY 0x00
73#define CPACF_KMCTR_DEA_ENC 0x01
74#define CPACF_KMCTR_DEA_DEC 0x81
75#define CPACF_KMCTR_TDEA_128_ENC 0x02
76#define CPACF_KMCTR_TDEA_128_DEC 0x82
77#define CPACF_KMCTR_TDEA_192_ENC 0x03
78#define CPACF_KMCTR_TDEA_192_DEC 0x83
79#define CPACF_KMCTR_AES_128_ENC 0x12
80#define CPACF_KMCTR_AES_128_DEC 0x92
81#define CPACF_KMCTR_AES_192_ENC 0x13
82#define CPACF_KMCTR_AES_192_DEC 0x93
83#define CPACF_KMCTR_AES_256_ENC 0x14
84#define CPACF_KMCTR_AES_256_DEC 0x94
85
86/*
87 * Function codes for the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST)
88 * instruction (0x80 is the decipher modifier bit)
89 */
90#define CPACF_KIMD_QUERY 0x00
91#define CPACF_KIMD_SHA_1 0x01
92#define CPACF_KIMD_SHA_256 0x02
93#define CPACF_KIMD_SHA_512 0x03
94#define CPACF_KIMD_GHASH 0x41
95
96/*
97 * Function codes for the KLMD (COMPUTE LAST MESSAGE DIGEST)
98 * instruction (0x80 is the decipher modifier bit)
99 */
100#define CPACF_KLMD_QUERY 0x00
101#define CPACF_KLMD_SHA_1 0x01
102#define CPACF_KLMD_SHA_256 0x02
103#define CPACF_KLMD_SHA_512 0x03
104
105/*
106 * function codes for the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE)
107 * instruction (0x80 is the decipher modifier bit)
108 */
109#define CPACF_KMAC_QUERY 0x00
110#define CPACF_KMAC_DEA 0x01
111#define CPACF_KMAC_TDEA_128 0x02
112#define CPACF_KMAC_TDEA_192 0x03
113
114/*
115 * Function codes for the PPNO (PERFORM PSEUDORANDOM NUMBER OPERATION)
116 * instruction (0x80 is the decipher modifier bit)
117 */
118#define CPACF_PPNO_QUERY 0x00
119#define CPACF_PPNO_SHA512_DRNG_GEN 0x03
120#define CPACF_PPNO_SHA512_DRNG_SEED 0x83
121
122/**
123 * cpacf_query() - check if a specific CPACF function is available
124 * @opcode: the opcode of the crypto instruction
125 * @func: the function code to test for
126 *
127 * Executes the query function for the given crypto instruction @opcode
128 * and checks if @func is available
129 *
130 * Returns 1 if @func is available for @opcode, 0 otherwise
131 */
132static inline void __cpacf_query(unsigned int opcode, unsigned char *status)
133{
134 typedef struct { unsigned char _[16]; } status_type;
135 register unsigned long r0 asm("0") = 0; /* query function */
136 register unsigned long r1 asm("1") = (unsigned long) status;
137
138 asm volatile(
139 /* Parameter registers are ignored, but may not be 0 */
140 "0: .insn rrf,%[opc] << 16,2,2,2,0\n"
141 " brc 1,0b\n" /* handle partial completion */
142 : "=m" (*(status_type *) status)
143 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (opcode)
144 : "cc");
145}
146
147static inline int cpacf_query(unsigned int opcode, unsigned int func)
148{
149 unsigned char status[16];
150
151 switch (opcode) {
152 case CPACF_KMAC:
153 case CPACF_KM:
154 case CPACF_KMC:
155 case CPACF_KIMD:
156 case CPACF_KLMD:
157 if (!test_facility(17)) /* check for MSA */
158 return 0;
159 break;
160 case CPACF_PCC:
161 case CPACF_KMCTR:
162 if (!test_facility(77)) /* check for MSA4 */
163 return 0;
164 break;
165 case CPACF_PPNO:
166 if (!test_facility(57)) /* check for MSA5 */
167 return 0;
168 break;
169 default:
170 BUG();
171 }
172 __cpacf_query(opcode, status);
173 return (status[func >> 3] & (0x80 >> (func & 7))) != 0;
174}
175
176/**
177 * cpacf_km() - executes the KM (CIPHER MESSAGE) instruction
178 * @func: the function code passed to KM; see CPACF_KM_xxx defines
179 * @param: address of parameter block; see POP for details on each func
180 * @dest: address of destination memory area
181 * @src: address of source memory area
182 * @src_len: length of src operand in bytes
183 *
184 * Returns 0 for the query func, number of processed bytes for
185 * encryption/decryption funcs
186 */
187static inline int cpacf_km(long func, void *param,
188 u8 *dest, const u8 *src, long src_len)
189{
190 register unsigned long r0 asm("0") = (unsigned long) func;
191 register unsigned long r1 asm("1") = (unsigned long) param;
192 register unsigned long r2 asm("2") = (unsigned long) src;
193 register unsigned long r3 asm("3") = (unsigned long) src_len;
194 register unsigned long r4 asm("4") = (unsigned long) dest;
195
196 asm volatile(
197 "0: .insn rre,%[opc] << 16,%[dst],%[src]\n"
198 " brc 1,0b\n" /* handle partial completion */
199 : [src] "+a" (r2), [len] "+d" (r3), [dst] "+a" (r4)
200 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KM)
201 : "cc", "memory");
202
203 return src_len - r3;
204}
205
206/**
207 * cpacf_kmc() - executes the KMC (CIPHER MESSAGE WITH CHAINING) instruction
208 * @func: the function code passed to KM; see CPACF_KMC_xxx defines
209 * @param: address of parameter block; see POP for details on each func
210 * @dest: address of destination memory area
211 * @src: address of source memory area
212 * @src_len: length of src operand in bytes
213 *
214 * Returns 0 for the query func, number of processed bytes for
215 * encryption/decryption funcs
216 */
217static inline int cpacf_kmc(long func, void *param,
218 u8 *dest, const u8 *src, long src_len)
219{
220 register unsigned long r0 asm("0") = (unsigned long) func;
221 register unsigned long r1 asm("1") = (unsigned long) param;
222 register unsigned long r2 asm("2") = (unsigned long) src;
223 register unsigned long r3 asm("3") = (unsigned long) src_len;
224 register unsigned long r4 asm("4") = (unsigned long) dest;
225
226 asm volatile(
227 "0: .insn rre,%[opc] << 16,%[dst],%[src]\n"
228 " brc 1,0b\n" /* handle partial completion */
229 : [src] "+a" (r2), [len] "+d" (r3), [dst] "+a" (r4)
230 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KMC)
231 : "cc", "memory");
232
233 return src_len - r3;
234}
235
236/**
237 * cpacf_kimd() - executes the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST)
238 * instruction
239 * @func: the function code passed to KM; see CPACF_KIMD_xxx defines
240 * @param: address of parameter block; see POP for details on each func
241 * @src: address of source memory area
242 * @src_len: length of src operand in bytes
243 *
244 * Returns 0 for the query func, number of processed bytes for digest funcs
245 */
246static inline int cpacf_kimd(long func, void *param,
247 const u8 *src, long src_len)
248{
249 register unsigned long r0 asm("0") = (unsigned long) func;
250 register unsigned long r1 asm("1") = (unsigned long) param;
251 register unsigned long r2 asm("2") = (unsigned long) src;
252 register unsigned long r3 asm("3") = (unsigned long) src_len;
253
254 asm volatile(
255 "0: .insn rre,%[opc] << 16,0,%[src]\n"
256 " brc 1,0b\n" /* handle partial completion */
257 : [src] "+a" (r2), [len] "+d" (r3)
258 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KIMD)
259 : "cc", "memory");
260
261 return src_len - r3;
262}
263
264/**
265 * cpacf_klmd() - executes the KLMD (COMPUTE LAST MESSAGE DIGEST) instruction
266 * @func: the function code passed to KM; see CPACF_KLMD_xxx defines
267 * @param: address of parameter block; see POP for details on each func
268 * @src: address of source memory area
269 * @src_len: length of src operand in bytes
270 *
271 * Returns 0 for the query func, number of processed bytes for digest funcs
272 */
273static inline int cpacf_klmd(long func, void *param,
274 const u8 *src, long src_len)
275{
276 register unsigned long r0 asm("0") = (unsigned long) func;
277 register unsigned long r1 asm("1") = (unsigned long) param;
278 register unsigned long r2 asm("2") = (unsigned long) src;
279 register unsigned long r3 asm("3") = (unsigned long) src_len;
280
281 asm volatile(
282 "0: .insn rre,%[opc] << 16,0,%[src]\n"
283 " brc 1,0b\n" /* handle partial completion */
284 : [src] "+a" (r2), [len] "+d" (r3)
285 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KLMD)
286 : "cc", "memory");
287
288 return src_len - r3;
289}
290
291/**
292 * cpacf_kmac() - executes the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE)
293 * instruction
294 * @func: the function code passed to KM; see CPACF_KMAC_xxx defines
295 * @param: address of parameter block; see POP for details on each func
296 * @src: address of source memory area
297 * @src_len: length of src operand in bytes
298 *
299 * Returns 0 for the query func, number of processed bytes for digest funcs
300 */
301static inline int cpacf_kmac(long func, void *param,
302 const u8 *src, long src_len)
303{
304 register unsigned long r0 asm("0") = (unsigned long) func;
305 register unsigned long r1 asm("1") = (unsigned long) param;
306 register unsigned long r2 asm("2") = (unsigned long) src;
307 register unsigned long r3 asm("3") = (unsigned long) src_len;
308
309 asm volatile(
310 "0: .insn rre,%[opc] << 16,0,%[src]\n"
311 " brc 1,0b\n" /* handle partial completion */
312 : [src] "+a" (r2), [len] "+d" (r3)
313 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KMAC)
314 : "cc", "memory");
315
316 return src_len - r3;
317}
318
319/**
320 * cpacf_kmctr() - executes the KMCTR (CIPHER MESSAGE WITH COUNTER) instruction
321 * @func: the function code passed to KMCTR; see CPACF_KMCTR_xxx defines
322 * @param: address of parameter block; see POP for details on each func
323 * @dest: address of destination memory area
324 * @src: address of source memory area
325 * @src_len: length of src operand in bytes
326 * @counter: address of counter value
327 *
328 * Returns 0 for the query func, number of processed bytes for
329 * encryption/decryption funcs
330 */
331static inline int cpacf_kmctr(long func, void *param, u8 *dest,
332 const u8 *src, long src_len, u8 *counter)
333{
334 register unsigned long r0 asm("0") = (unsigned long) func;
335 register unsigned long r1 asm("1") = (unsigned long) param;
336 register unsigned long r2 asm("2") = (unsigned long) src;
337 register unsigned long r3 asm("3") = (unsigned long) src_len;
338 register unsigned long r4 asm("4") = (unsigned long) dest;
339 register unsigned long r6 asm("6") = (unsigned long) counter;
340
341 asm volatile(
342 "0: .insn rrf,%[opc] << 16,%[dst],%[src],%[ctr],0\n"
343 " brc 1,0b\n" /* handle partial completion */
344 : [src] "+a" (r2), [len] "+d" (r3),
345 [dst] "+a" (r4), [ctr] "+a" (r6)
346 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KMCTR)
347 : "cc", "memory");
348
349 return src_len - r3;
350}
351
352/**
353 * cpacf_ppno() - executes the PPNO (PERFORM PSEUDORANDOM NUMBER OPERATION)
354 * instruction
355 * @func: the function code passed to PPNO; see CPACF_PPNO_xxx defines
356 * @param: address of parameter block; see POP for details on each func
357 * @dest: address of destination memory area
358 * @dest_len: size of destination memory area in bytes
359 * @seed: address of seed data
360 * @seed_len: size of seed data in bytes
361 *
362 * Returns 0 for the query func, number of random bytes stored in
363 * dest buffer for generate function
364 */
365static inline int cpacf_ppno(long func, void *param,
366 u8 *dest, long dest_len,
367 const u8 *seed, long seed_len)
368{
369 register unsigned long r0 asm("0") = (unsigned long) func;
370 register unsigned long r1 asm("1") = (unsigned long) param;
371 register unsigned long r2 asm("2") = (unsigned long) dest;
372 register unsigned long r3 asm("3") = (unsigned long) dest_len;
373 register unsigned long r4 asm("4") = (unsigned long) seed;
374 register unsigned long r5 asm("5") = (unsigned long) seed_len;
375
376 asm volatile (
377 "0: .insn rre,%[opc] << 16,%[dst],%[seed]\n"
378 " brc 1,0b\n" /* handle partial completion */
379 : [dst] "+a" (r2), [dlen] "+d" (r3)
380 : [fc] "d" (r0), [pba] "a" (r1),
381 [seed] "a" (r4), [slen] "d" (r5), [opc] "i" (CPACF_PPNO)
382 : "cc", "memory");
383
384 return dest_len - r3;
385}
386
387/**
388 * cpacf_pcc() - executes the PCC (PERFORM CRYPTOGRAPHIC COMPUTATION)
389 * instruction
390 * @func: the function code passed to PCC; see CPACF_KM_xxx defines
391 * @param: address of parameter block; see POP for details on each func
392 *
393 * Returns 0.
394 */
395static inline int cpacf_pcc(long func, void *param)
396{
397 register unsigned long r0 asm("0") = (unsigned long) func;
398 register unsigned long r1 asm("1") = (unsigned long) param;
399
400 asm volatile(
401 "0: .insn rre,%[opc] << 16,0,0\n" /* PCC opcode */
402 " brc 1,0b\n" /* handle partial completion */
403 :
404 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_PCC)
405 : "cc", "memory");
406
407 return 0;
408}
409
410#endif /* _ASM_S390_CPACF_H */