1 /* SPDX-License-Identifier: GPL-2.0 */
3 * CP Assist for Cryptographic Functions (CPACF)
5 * Copyright IBM Corp. 2003, 2017
6 * Author(s): Thomas Spatzier
8 * Harald Freudenberger (freude@de.ibm.com)
9 * Martin Schwidefsky <schwidefsky@de.ibm.com>
11 #ifndef _ASM_S390_CPACF_H
12 #define _ASM_S390_CPACF_H
14 #include <asm/facility.h>
17 * Instruction opcodes for the CPACF instructions
19 #define CPACF_KMAC 0xb91e /* MSA */
20 #define CPACF_KM 0xb92e /* MSA */
21 #define CPACF_KMC 0xb92f /* MSA */
22 #define CPACF_KIMD 0xb93e /* MSA */
23 #define CPACF_KLMD 0xb93f /* MSA */
24 #define CPACF_PCKMO 0xb928 /* MSA3 */
25 #define CPACF_KMF 0xb92a /* MSA4 */
26 #define CPACF_KMO 0xb92b /* MSA4 */
27 #define CPACF_PCC 0xb92c /* MSA4 */
28 #define CPACF_KMCTR 0xb92d /* MSA4 */
29 #define CPACF_PRNO 0xb93c /* MSA5 */
30 #define CPACF_KMA 0xb929 /* MSA8 */
31 #define CPACF_KDSA 0xb93a /* MSA9 */
34 * En/decryption modifier bits
36 #define CPACF_ENCRYPT 0x00
37 #define CPACF_DECRYPT 0x80
40 * Function codes for the KM (CIPHER MESSAGE) instruction
42 #define CPACF_KM_QUERY 0x00
43 #define CPACF_KM_DEA 0x01
44 #define CPACF_KM_TDEA_128 0x02
45 #define CPACF_KM_TDEA_192 0x03
46 #define CPACF_KM_AES_128 0x12
47 #define CPACF_KM_AES_192 0x13
48 #define CPACF_KM_AES_256 0x14
49 #define CPACF_KM_PAES_128 0x1a
50 #define CPACF_KM_PAES_192 0x1b
51 #define CPACF_KM_PAES_256 0x1c
52 #define CPACF_KM_XTS_128 0x32
53 #define CPACF_KM_XTS_256 0x34
54 #define CPACF_KM_PXTS_128 0x3a
55 #define CPACF_KM_PXTS_256 0x3c
58 * Function codes for the KMC (CIPHER MESSAGE WITH CHAINING)
61 #define CPACF_KMC_QUERY 0x00
62 #define CPACF_KMC_DEA 0x01
63 #define CPACF_KMC_TDEA_128 0x02
64 #define CPACF_KMC_TDEA_192 0x03
65 #define CPACF_KMC_AES_128 0x12
66 #define CPACF_KMC_AES_192 0x13
67 #define CPACF_KMC_AES_256 0x14
68 #define CPACF_KMC_PAES_128 0x1a
69 #define CPACF_KMC_PAES_192 0x1b
70 #define CPACF_KMC_PAES_256 0x1c
71 #define CPACF_KMC_PRNG 0x43
74 * Function codes for the KMCTR (CIPHER MESSAGE WITH COUNTER)
77 #define CPACF_KMCTR_QUERY 0x00
78 #define CPACF_KMCTR_DEA 0x01
79 #define CPACF_KMCTR_TDEA_128 0x02
80 #define CPACF_KMCTR_TDEA_192 0x03
81 #define CPACF_KMCTR_AES_128 0x12
82 #define CPACF_KMCTR_AES_192 0x13
83 #define CPACF_KMCTR_AES_256 0x14
84 #define CPACF_KMCTR_PAES_128 0x1a
85 #define CPACF_KMCTR_PAES_192 0x1b
86 #define CPACF_KMCTR_PAES_256 0x1c
89 * Function codes for the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST)
92 #define CPACF_KIMD_QUERY 0x00
93 #define CPACF_KIMD_SHA_1 0x01
94 #define CPACF_KIMD_SHA_256 0x02
95 #define CPACF_KIMD_SHA_512 0x03
96 #define CPACF_KIMD_GHASH 0x41
99 * Function codes for the KLMD (COMPUTE LAST MESSAGE DIGEST)
102 #define CPACF_KLMD_QUERY 0x00
103 #define CPACF_KLMD_SHA_1 0x01
104 #define CPACF_KLMD_SHA_256 0x02
105 #define CPACF_KLMD_SHA_512 0x03
108 * function codes for the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE)
111 #define CPACF_KMAC_QUERY 0x00
112 #define CPACF_KMAC_DEA 0x01
113 #define CPACF_KMAC_TDEA_128 0x02
114 #define CPACF_KMAC_TDEA_192 0x03
117 * Function codes for the PCKMO (PERFORM CRYPTOGRAPHIC KEY MANAGEMENT)
120 #define CPACF_PCKMO_QUERY 0x00
121 #define CPACF_PCKMO_ENC_DES_KEY 0x01
122 #define CPACF_PCKMO_ENC_TDES_128_KEY 0x02
123 #define CPACF_PCKMO_ENC_TDES_192_KEY 0x03
124 #define CPACF_PCKMO_ENC_AES_128_KEY 0x12
125 #define CPACF_PCKMO_ENC_AES_192_KEY 0x13
126 #define CPACF_PCKMO_ENC_AES_256_KEY 0x14
129 * Function codes for the PRNO (PERFORM RANDOM NUMBER OPERATION)
132 #define CPACF_PRNO_QUERY 0x00
133 #define CPACF_PRNO_SHA512_DRNG_GEN 0x03
134 #define CPACF_PRNO_SHA512_DRNG_SEED 0x83
135 #define CPACF_PRNO_TRNG_Q_R2C_RATIO 0x70
136 #define CPACF_PRNO_TRNG 0x72
139 * Function codes for the KMA (CIPHER MESSAGE WITH AUTHENTICATION)
142 #define CPACF_KMA_QUERY 0x00
143 #define CPACF_KMA_GCM_AES_128 0x12
144 #define CPACF_KMA_GCM_AES_192 0x13
145 #define CPACF_KMA_GCM_AES_256 0x14
148 * Flags for the KMA (CIPHER MESSAGE WITH AUTHENTICATION) instruction
150 #define CPACF_KMA_LPC 0x100 /* Last-Plaintext/Ciphertext */
151 #define CPACF_KMA_LAAD 0x200 /* Last-AAD */
152 #define CPACF_KMA_HS 0x400 /* Hash-subkey Supplied */
154 typedef struct { unsigned char bytes
[16]; } cpacf_mask_t
;
157 * cpacf_query() - check if a specific CPACF function is available
158 * @opcode: the opcode of the crypto instruction
159 * @func: the function code to test for
161 * Executes the query function for the given crypto instruction @opcode
162 * and checks if @func is available
164 * Returns 1 if @func is available for @opcode, 0 otherwise
166 static inline void __cpacf_query(unsigned int opcode
, cpacf_mask_t
*mask
)
168 register unsigned long r0
asm("0") = 0; /* query function */
169 register unsigned long r1
asm("1") = (unsigned long) mask
;
172 " spm 0\n" /* pckmo doesn't change the cc */
173 /* Parameter regs are ignored, but must be nonzero and unique */
174 "0: .insn rrf,%[opc] << 16,2,4,6,0\n"
175 " brc 1,0b\n" /* handle partial completion */
177 : [fc
] "d" (r0
), [pba
] "a" (r1
), [opc
] "i" (opcode
)
181 static __always_inline
int __cpacf_check_opcode(unsigned int opcode
)
189 return test_facility(17); /* check for MSA */
191 return test_facility(76); /* check for MSA3 */
196 return test_facility(77); /* check for MSA4 */
198 return test_facility(57); /* check for MSA5 */
200 return test_facility(146); /* check for MSA8 */
206 static __always_inline
int cpacf_query(unsigned int opcode
, cpacf_mask_t
*mask
)
208 if (__cpacf_check_opcode(opcode
)) {
209 __cpacf_query(opcode
, mask
);
212 memset(mask
, 0, sizeof(*mask
));
216 static inline int cpacf_test_func(cpacf_mask_t
*mask
, unsigned int func
)
218 return (mask
->bytes
[func
>> 3] & (0x80 >> (func
& 7))) != 0;
221 static __always_inline
int cpacf_query_func(unsigned int opcode
, unsigned int func
)
225 if (cpacf_query(opcode
, &mask
))
226 return cpacf_test_func(&mask
, func
);
231 * cpacf_km() - executes the KM (CIPHER MESSAGE) instruction
232 * @func: the function code passed to KM; see CPACF_KM_xxx defines
233 * @param: address of parameter block; see POP for details on each func
234 * @dest: address of destination memory area
235 * @src: address of source memory area
236 * @src_len: length of src operand in bytes
238 * Returns 0 for the query func, number of processed bytes for
239 * encryption/decryption funcs
241 static inline int cpacf_km(unsigned long func
, void *param
,
242 u8
*dest
, const u8
*src
, long src_len
)
244 register unsigned long r0
asm("0") = (unsigned long) func
;
245 register unsigned long r1
asm("1") = (unsigned long) param
;
246 register unsigned long r2
asm("2") = (unsigned long) src
;
247 register unsigned long r3
asm("3") = (unsigned long) src_len
;
248 register unsigned long r4
asm("4") = (unsigned long) dest
;
251 "0: .insn rre,%[opc] << 16,%[dst],%[src]\n"
252 " brc 1,0b\n" /* handle partial completion */
253 : [src
] "+a" (r2
), [len
] "+d" (r3
), [dst
] "+a" (r4
)
254 : [fc
] "d" (r0
), [pba
] "a" (r1
), [opc
] "i" (CPACF_KM
)
261 * cpacf_kmc() - executes the KMC (CIPHER MESSAGE WITH CHAINING) instruction
262 * @func: the function code passed to KM; see CPACF_KMC_xxx defines
263 * @param: address of parameter block; see POP for details on each func
264 * @dest: address of destination memory area
265 * @src: address of source memory area
266 * @src_len: length of src operand in bytes
268 * Returns 0 for the query func, number of processed bytes for
269 * encryption/decryption funcs
271 static inline int cpacf_kmc(unsigned long func
, void *param
,
272 u8
*dest
, const u8
*src
, long src_len
)
274 register unsigned long r0
asm("0") = (unsigned long) func
;
275 register unsigned long r1
asm("1") = (unsigned long) param
;
276 register unsigned long r2
asm("2") = (unsigned long) src
;
277 register unsigned long r3
asm("3") = (unsigned long) src_len
;
278 register unsigned long r4
asm("4") = (unsigned long) dest
;
281 "0: .insn rre,%[opc] << 16,%[dst],%[src]\n"
282 " brc 1,0b\n" /* handle partial completion */
283 : [src
] "+a" (r2
), [len
] "+d" (r3
), [dst
] "+a" (r4
)
284 : [fc
] "d" (r0
), [pba
] "a" (r1
), [opc
] "i" (CPACF_KMC
)
291 * cpacf_kimd() - executes the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST)
293 * @func: the function code passed to KM; see CPACF_KIMD_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 static inline void cpacf_kimd(unsigned long func
, void *param
,
299 const u8
*src
, long src_len
)
301 register unsigned long r0
asm("0") = (unsigned long) func
;
302 register unsigned long r1
asm("1") = (unsigned long) param
;
303 register unsigned long r2
asm("2") = (unsigned long) src
;
304 register unsigned long r3
asm("3") = (unsigned long) src_len
;
307 "0: .insn rre,%[opc] << 16,0,%[src]\n"
308 " brc 1,0b\n" /* handle partial completion */
309 : [src
] "+a" (r2
), [len
] "+d" (r3
)
310 : [fc
] "d" (r0
), [pba
] "a" (r1
), [opc
] "i" (CPACF_KIMD
)
315 * cpacf_klmd() - executes the KLMD (COMPUTE LAST MESSAGE DIGEST) instruction
316 * @func: the function code passed to KM; see CPACF_KLMD_xxx defines
317 * @param: address of parameter block; see POP for details on each func
318 * @src: address of source memory area
319 * @src_len: length of src operand in bytes
321 static inline void cpacf_klmd(unsigned long func
, void *param
,
322 const u8
*src
, long src_len
)
324 register unsigned long r0
asm("0") = (unsigned long) func
;
325 register unsigned long r1
asm("1") = (unsigned long) param
;
326 register unsigned long r2
asm("2") = (unsigned long) src
;
327 register unsigned long r3
asm("3") = (unsigned long) src_len
;
330 "0: .insn rre,%[opc] << 16,0,%[src]\n"
331 " brc 1,0b\n" /* handle partial completion */
332 : [src
] "+a" (r2
), [len
] "+d" (r3
)
333 : [fc
] "d" (r0
), [pba
] "a" (r1
), [opc
] "i" (CPACF_KLMD
)
338 * cpacf_kmac() - executes the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE)
340 * @func: the function code passed to KM; see CPACF_KMAC_xxx defines
341 * @param: address of parameter block; see POP for details on each func
342 * @src: address of source memory area
343 * @src_len: length of src operand in bytes
345 * Returns 0 for the query func, number of processed bytes for digest funcs
347 static inline int cpacf_kmac(unsigned long func
, void *param
,
348 const u8
*src
, long src_len
)
350 register unsigned long r0
asm("0") = (unsigned long) func
;
351 register unsigned long r1
asm("1") = (unsigned long) param
;
352 register unsigned long r2
asm("2") = (unsigned long) src
;
353 register unsigned long r3
asm("3") = (unsigned long) src_len
;
356 "0: .insn rre,%[opc] << 16,0,%[src]\n"
357 " brc 1,0b\n" /* handle partial completion */
358 : [src
] "+a" (r2
), [len
] "+d" (r3
)
359 : [fc
] "d" (r0
), [pba
] "a" (r1
), [opc
] "i" (CPACF_KMAC
)
366 * cpacf_kmctr() - executes the KMCTR (CIPHER MESSAGE WITH COUNTER) instruction
367 * @func: the function code passed to KMCTR; see CPACF_KMCTR_xxx defines
368 * @param: address of parameter block; see POP for details on each func
369 * @dest: address of destination memory area
370 * @src: address of source memory area
371 * @src_len: length of src operand in bytes
372 * @counter: address of counter value
374 * Returns 0 for the query func, number of processed bytes for
375 * encryption/decryption funcs
377 static inline int cpacf_kmctr(unsigned long func
, void *param
, u8
*dest
,
378 const u8
*src
, long src_len
, u8
*counter
)
380 register unsigned long r0
asm("0") = (unsigned long) func
;
381 register unsigned long r1
asm("1") = (unsigned long) param
;
382 register unsigned long r2
asm("2") = (unsigned long) src
;
383 register unsigned long r3
asm("3") = (unsigned long) src_len
;
384 register unsigned long r4
asm("4") = (unsigned long) dest
;
385 register unsigned long r6
asm("6") = (unsigned long) counter
;
388 "0: .insn rrf,%[opc] << 16,%[dst],%[src],%[ctr],0\n"
389 " brc 1,0b\n" /* handle partial completion */
390 : [src
] "+a" (r2
), [len
] "+d" (r3
),
391 [dst
] "+a" (r4
), [ctr
] "+a" (r6
)
392 : [fc
] "d" (r0
), [pba
] "a" (r1
), [opc
] "i" (CPACF_KMCTR
)
399 * cpacf_prno() - executes the PRNO (PERFORM RANDOM NUMBER OPERATION)
401 * @func: the function code passed to PRNO; see CPACF_PRNO_xxx defines
402 * @param: address of parameter block; see POP for details on each func
403 * @dest: address of destination memory area
404 * @dest_len: size of destination memory area in bytes
405 * @seed: address of seed data
406 * @seed_len: size of seed data in bytes
408 static inline void cpacf_prno(unsigned long func
, void *param
,
409 u8
*dest
, unsigned long dest_len
,
410 const u8
*seed
, unsigned long seed_len
)
412 register unsigned long r0
asm("0") = (unsigned long) func
;
413 register unsigned long r1
asm("1") = (unsigned long) param
;
414 register unsigned long r2
asm("2") = (unsigned long) dest
;
415 register unsigned long r3
asm("3") = (unsigned long) dest_len
;
416 register unsigned long r4
asm("4") = (unsigned long) seed
;
417 register unsigned long r5
asm("5") = (unsigned long) seed_len
;
420 "0: .insn rre,%[opc] << 16,%[dst],%[seed]\n"
421 " brc 1,0b\n" /* handle partial completion */
422 : [dst
] "+a" (r2
), [dlen
] "+d" (r3
)
423 : [fc
] "d" (r0
), [pba
] "a" (r1
),
424 [seed
] "a" (r4
), [slen
] "d" (r5
), [opc
] "i" (CPACF_PRNO
)
429 * cpacf_trng() - executes the TRNG subfunction of the PRNO instruction
430 * @ucbuf: buffer for unconditioned data
431 * @ucbuf_len: amount of unconditioned data to fetch in bytes
432 * @cbuf: buffer for conditioned data
433 * @cbuf_len: amount of conditioned data to fetch in bytes
435 static inline void cpacf_trng(u8
*ucbuf
, unsigned long ucbuf_len
,
436 u8
*cbuf
, unsigned long cbuf_len
)
438 register unsigned long r0
asm("0") = (unsigned long) CPACF_PRNO_TRNG
;
439 register unsigned long r2
asm("2") = (unsigned long) ucbuf
;
440 register unsigned long r3
asm("3") = (unsigned long) ucbuf_len
;
441 register unsigned long r4
asm("4") = (unsigned long) cbuf
;
442 register unsigned long r5
asm("5") = (unsigned long) cbuf_len
;
445 "0: .insn rre,%[opc] << 16,%[ucbuf],%[cbuf]\n"
446 " brc 1,0b\n" /* handle partial completion */
447 : [ucbuf
] "+a" (r2
), [ucbuflen
] "+d" (r3
),
448 [cbuf
] "+a" (r4
), [cbuflen
] "+d" (r5
)
449 : [fc
] "d" (r0
), [opc
] "i" (CPACF_PRNO
)
454 * cpacf_pcc() - executes the PCC (PERFORM CRYPTOGRAPHIC COMPUTATION)
456 * @func: the function code passed to PCC; see CPACF_KM_xxx defines
457 * @param: address of parameter block; see POP for details on each func
459 static inline void cpacf_pcc(unsigned long func
, void *param
)
461 register unsigned long r0
asm("0") = (unsigned long) func
;
462 register unsigned long r1
asm("1") = (unsigned long) param
;
465 "0: .insn rre,%[opc] << 16,0,0\n" /* PCC opcode */
466 " brc 1,0b\n" /* handle partial completion */
468 : [fc
] "d" (r0
), [pba
] "a" (r1
), [opc
] "i" (CPACF_PCC
)
473 * cpacf_pckmo() - executes the PCKMO (PERFORM CRYPTOGRAPHIC KEY
474 * MANAGEMENT) instruction
475 * @func: the function code passed to PCKMO; see CPACF_PCKMO_xxx defines
476 * @param: address of parameter block; see POP for details on each func
480 static inline void cpacf_pckmo(long func
, void *param
)
482 register unsigned long r0
asm("0") = (unsigned long) func
;
483 register unsigned long r1
asm("1") = (unsigned long) param
;
486 " .insn rre,%[opc] << 16,0,0\n" /* PCKMO opcode */
488 : [fc
] "d" (r0
), [pba
] "a" (r1
), [opc
] "i" (CPACF_PCKMO
)
493 * cpacf_kma() - executes the KMA (CIPHER MESSAGE WITH AUTHENTICATION)
495 * @func: the function code passed to KMA; see CPACF_KMA_xxx defines
496 * @param: address of parameter block; see POP for details on each func
497 * @dest: address of destination memory area
498 * @src: address of source memory area
499 * @src_len: length of src operand in bytes
500 * @aad: address of additional authenticated data memory area
501 * @aad_len: length of aad operand in bytes
503 static inline void cpacf_kma(unsigned long func
, void *param
, u8
*dest
,
504 const u8
*src
, unsigned long src_len
,
505 const u8
*aad
, unsigned long aad_len
)
507 register unsigned long r0
asm("0") = (unsigned long) func
;
508 register unsigned long r1
asm("1") = (unsigned long) param
;
509 register unsigned long r2
asm("2") = (unsigned long) src
;
510 register unsigned long r3
asm("3") = (unsigned long) src_len
;
511 register unsigned long r4
asm("4") = (unsigned long) aad
;
512 register unsigned long r5
asm("5") = (unsigned long) aad_len
;
513 register unsigned long r6
asm("6") = (unsigned long) dest
;
516 "0: .insn rrf,%[opc] << 16,%[dst],%[src],%[aad],0\n"
517 " brc 1,0b\n" /* handle partial completion */
518 : [dst
] "+a" (r6
), [src
] "+a" (r2
), [slen
] "+d" (r3
),
519 [aad
] "+a" (r4
), [alen
] "+d" (r5
)
520 : [fc
] "d" (r0
), [pba
] "a" (r1
), [opc
] "i" (CPACF_KMA
)
524 #endif /* _ASM_S390_CPACF_H */