2 * CP Assist for Cryptographic Functions (CPACF)
4 * Copyright IBM Corp. 2003, 2016
5 * Author(s): Thomas Spatzier
7 * Harald Freudenberger (freude@de.ibm.com)
8 * Martin Schwidefsky <schwidefsky@de.ibm.com>
10 #ifndef _ASM_S390_CPACF_H
11 #define _ASM_S390_CPACF_H
13 #include <asm/facility.h>
16 * Instruction opcodes for the CPACF instructions
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 */
28 * Function codes for the KM (CIPHER MESSAGE)
29 * instruction (0x80 is the decipher modifier bit)
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
50 * Function codes for the KMC (CIPHER MESSAGE WITH CHAINING)
51 * instruction (0x80 is the decipher modifier bit)
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
69 * Function codes for the KMCTR (CIPHER MESSAGE WITH COUNTER)
70 * instruction (0x80 is the decipher modifier bit)
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
87 * Function codes for the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST)
88 * instruction (0x80 is the decipher modifier bit)
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
97 * Function codes for the KLMD (COMPUTE LAST MESSAGE DIGEST)
98 * instruction (0x80 is the decipher modifier bit)
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
106 * function codes for the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE)
107 * instruction (0x80 is the decipher modifier bit)
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
115 * Function codes for the PPNO (PERFORM PSEUDORANDOM NUMBER OPERATION)
116 * instruction (0x80 is the decipher modifier bit)
118 #define CPACF_PPNO_QUERY 0x00
119 #define CPACF_PPNO_SHA512_DRNG_GEN 0x03
120 #define CPACF_PPNO_SHA512_DRNG_SEED 0x83
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
127 * Executes the query function for the given crypto instruction @opcode
128 * and checks if @func is available
130 * Returns 1 if @func is available for @opcode, 0 otherwise
132 static inline void __cpacf_query(unsigned int opcode, unsigned char *status)
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;
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)
147 static inline int cpacf_query(unsigned int opcode, unsigned int func)
149 unsigned char status[16];
157 if (!test_facility(17)) /* check for MSA */
162 if (!test_facility(77)) /* check for MSA4 */
166 if (!test_facility(57)) /* check for MSA5 */
172 __cpacf_query(opcode, status);
173 return (status[func >> 3] & (0x80 >> (func & 7))) != 0;
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
184 * Returns 0 for the query func, number of processed bytes for
185 * encryption/decryption funcs
187 static inline int cpacf_km(long func, void *param,
188 u8 *dest, const u8 *src, long src_len)
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;
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)
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
214 * Returns 0 for the query func, number of processed bytes for
215 * encryption/decryption funcs
217 static inline int cpacf_kmc(long func, void *param,
218 u8 *dest, const u8 *src, long src_len)
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;
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)
237 * cpacf_kimd() - executes the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST)
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
244 * Returns 0 for the query func, number of processed bytes for digest funcs
246 static inline int cpacf_kimd(long func, void *param,
247 const u8 *src, long src_len)
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;
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)
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
271 * Returns 0 for the query func, number of processed bytes for digest funcs
273 static inline int cpacf_klmd(long func, void *param,
274 const u8 *src, long src_len)
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;
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)
292 * cpacf_kmac() - executes the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE)
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
299 * Returns 0 for the query func, number of processed bytes for digest funcs
301 static inline int cpacf_kmac(long func, void *param,
302 const u8 *src, long src_len)
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;
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)
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
328 * Returns 0 for the query func, number of processed bytes for
329 * encryption/decryption funcs
331 static inline int cpacf_kmctr(long func, void *param, u8 *dest,
332 const u8 *src, long src_len, u8 *counter)
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;
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)
353 * cpacf_ppno() - executes the PPNO (PERFORM PSEUDORANDOM NUMBER OPERATION)
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
362 * Returns 0 for the query func, number of random bytes stored in
363 * dest buffer for generate function
365 static inline int cpacf_ppno(long func, void *param,
366 u8 *dest, long dest_len,
367 const u8 *seed, long seed_len)
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;
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)
384 return dest_len - r3;
388 * cpacf_pcc() - executes the PCC (PERFORM CRYPTOGRAPHIC COMPUTATION)
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
395 static inline int cpacf_pcc(long func, void *param)
397 register unsigned long r0 asm("0") = (unsigned long) func;
398 register unsigned long r1 asm("1") = (unsigned long) param;
401 "0: .insn rre,%[opc] << 16,0,0\n" /* PCC opcode */
402 " brc 1,0b\n" /* handle partial completion */
404 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_PCC)
410 #endif /* _ASM_S390_CPACF_H */