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_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 */
31 * Decryption modifier bit
33 #define CPACF_DECRYPT 0x80
36 * Function codes for the KM (CIPHER MESSAGE) instruction
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
49 * Function codes for the KMC (CIPHER MESSAGE WITH CHAINING)
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
62 * Function codes for the KMCTR (CIPHER MESSAGE WITH COUNTER)
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
74 * Function codes for the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST)
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
84 * Function codes for the KLMD (COMPUTE LAST MESSAGE DIGEST)
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
93 * function codes for the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE)
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
102 * Function codes for the PPNO (PERFORM PSEUDORANDOM NUMBER OPERATION)
105 #define CPACF_PPNO_QUERY 0x00
106 #define CPACF_PPNO_SHA512_DRNG_GEN 0x03
107 #define CPACF_PPNO_SHA512_DRNG_SEED 0x83
109 typedef struct { unsigned char bytes
[16]; } cpacf_mask_t
;
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
116 * Executes the query function for the given crypto instruction @opcode
117 * and checks if @func is available
119 * Returns 1 if @func is available for @opcode, 0 otherwise
121 static inline void __cpacf_query(unsigned int opcode
, cpacf_mask_t
*mask
)
123 register unsigned long r0
asm("0") = 0; /* query function */
124 register unsigned long r1
asm("1") = (unsigned long) mask
;
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 */
132 : [fc
] "d" (r0
), [pba
] "a" (r1
), [opc
] "i" (opcode
)
136 static inline int __cpacf_check_opcode(unsigned int opcode
)
144 return test_facility(17); /* check for MSA */
146 return test_facility(76); /* check for MSA3 */
151 return test_facility(77); /* check for MSA4 */
153 return test_facility(57); /* check for MSA5 */
159 static inline int cpacf_query(unsigned int opcode
, cpacf_mask_t
*mask
)
161 if (__cpacf_check_opcode(opcode
)) {
162 __cpacf_query(opcode
, mask
);
165 memset(mask
, 0, sizeof(*mask
));
169 static inline int cpacf_test_func(cpacf_mask_t
*mask
, unsigned int func
)
171 return (mask
->bytes
[func
>> 3] & (0x80 >> (func
& 7))) != 0;
174 static inline int cpacf_query_func(unsigned int opcode
, unsigned int func
)
178 if (cpacf_query(opcode
, &mask
))
179 return cpacf_test_func(&mask
, func
);
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
191 * Returns 0 for the query func, number of processed bytes for
192 * encryption/decryption funcs
194 static inline int cpacf_km(unsigned long func
, void *param
,
195 u8
*dest
, const u8
*src
, long src_len
)
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
;
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
)
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
221 * Returns 0 for the query func, number of processed bytes for
222 * encryption/decryption funcs
224 static inline int cpacf_kmc(unsigned long func
, void *param
,
225 u8
*dest
, const u8
*src
, long src_len
)
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
;
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
)
244 * cpacf_kimd() - executes the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST)
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
251 static inline void cpacf_kimd(unsigned long func
, void *param
,
252 const u8
*src
, long src_len
)
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
;
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
)
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
274 static inline void cpacf_klmd(unsigned long func
, void *param
,
275 const u8
*src
, long src_len
)
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
;
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
)
291 * cpacf_kmac() - executes the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE)
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
298 * Returns 0 for the query func, number of processed bytes for digest funcs
300 static inline int cpacf_kmac(unsigned long func
, void *param
,
301 const u8
*src
, long src_len
)
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
;
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
)
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
327 * Returns 0 for the query func, number of processed bytes for
328 * encryption/decryption funcs
330 static inline int cpacf_kmctr(unsigned long func
, void *param
, u8
*dest
,
331 const u8
*src
, long src_len
, u8
*counter
)
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
;
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
)
352 * cpacf_ppno() - executes the PPNO (PERFORM PSEUDORANDOM NUMBER OPERATION)
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
361 static inline void cpacf_ppno(unsigned long func
, void *param
,
362 u8
*dest
, long dest_len
,
363 const u8
*seed
, long seed_len
)
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
;
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
)
382 * cpacf_pcc() - executes the PCC (PERFORM CRYPTOGRAPHIC COMPUTATION)
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
387 static inline void cpacf_pcc(unsigned long func
, void *param
)
389 register unsigned long r0
asm("0") = (unsigned long) func
;
390 register unsigned long r1
asm("1") = (unsigned long) param
;
393 "0: .insn rre,%[opc] << 16,0,0\n" /* PCC opcode */
394 " brc 1,0b\n" /* handle partial completion */
396 : [fc
] "d" (r0
), [pba
] "a" (r1
), [opc
] "i" (CPACF_PCC
)
400 #endif /* _ASM_S390_CPACF_H */