]>
git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - arch/s390/include/asm/cpacf.h
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_PRNO 0xb93c /* MSA5 */
29 #define CPACF_KMA 0xb929 /* MSA8 */
32 * En/decryption modifier bits
34 #define CPACF_ENCRYPT 0x00
35 #define CPACF_DECRYPT 0x80
38 * Function codes for the KM (CIPHER MESSAGE) instruction
40 #define CPACF_KM_QUERY 0x00
41 #define CPACF_KM_DEA 0x01
42 #define CPACF_KM_TDEA_128 0x02
43 #define CPACF_KM_TDEA_192 0x03
44 #define CPACF_KM_AES_128 0x12
45 #define CPACF_KM_AES_192 0x13
46 #define CPACF_KM_AES_256 0x14
47 #define CPACF_KM_PAES_128 0x1a
48 #define CPACF_KM_PAES_192 0x1b
49 #define CPACF_KM_PAES_256 0x1c
50 #define CPACF_KM_XTS_128 0x32
51 #define CPACF_KM_XTS_256 0x34
52 #define CPACF_KM_PXTS_128 0x3a
53 #define CPACF_KM_PXTS_256 0x3c
56 * Function codes for the KMC (CIPHER MESSAGE WITH CHAINING)
59 #define CPACF_KMC_QUERY 0x00
60 #define CPACF_KMC_DEA 0x01
61 #define CPACF_KMC_TDEA_128 0x02
62 #define CPACF_KMC_TDEA_192 0x03
63 #define CPACF_KMC_AES_128 0x12
64 #define CPACF_KMC_AES_192 0x13
65 #define CPACF_KMC_AES_256 0x14
66 #define CPACF_KMC_PAES_128 0x1a
67 #define CPACF_KMC_PAES_192 0x1b
68 #define CPACF_KMC_PAES_256 0x1c
69 #define CPACF_KMC_PRNG 0x43
72 * Function codes for the KMCTR (CIPHER MESSAGE WITH COUNTER)
75 #define CPACF_KMCTR_QUERY 0x00
76 #define CPACF_KMCTR_DEA 0x01
77 #define CPACF_KMCTR_TDEA_128 0x02
78 #define CPACF_KMCTR_TDEA_192 0x03
79 #define CPACF_KMCTR_AES_128 0x12
80 #define CPACF_KMCTR_AES_192 0x13
81 #define CPACF_KMCTR_AES_256 0x14
82 #define CPACF_KMCTR_PAES_128 0x1a
83 #define CPACF_KMCTR_PAES_192 0x1b
84 #define CPACF_KMCTR_PAES_256 0x1c
87 * Function codes for the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST)
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)
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)
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 PCKMO (PERFORM CRYPTOGRAPHIC KEY MANAGEMENT)
118 #define CPACF_PCKMO_QUERY 0x00
119 #define CPACF_PCKMO_ENC_DES_KEY 0x01
120 #define CPACF_PCKMO_ENC_TDES_128_KEY 0x02
121 #define CPACF_PCKMO_ENC_TDES_192_KEY 0x03
122 #define CPACF_PCKMO_ENC_AES_128_KEY 0x12
123 #define CPACF_PCKMO_ENC_AES_192_KEY 0x13
124 #define CPACF_PCKMO_ENC_AES_256_KEY 0x14
127 * Function codes for the PRNO (PERFORM RANDOM NUMBER OPERATION)
130 #define CPACF_PRNO_QUERY 0x00
131 #define CPACF_PRNO_SHA512_DRNG_GEN 0x03
132 #define CPACF_PRNO_SHA512_DRNG_SEED 0x83
133 #define CPACF_PRNO_TRNG_Q_R2C_RATIO 0x70
134 #define CPACF_PRNO_TRNG 0x72
136 typedef struct { unsigned char bytes
[16]; } cpacf_mask_t
;
139 * cpacf_query() - check if a specific CPACF function is available
140 * @opcode: the opcode of the crypto instruction
141 * @func: the function code to test for
143 * Executes the query function for the given crypto instruction @opcode
144 * and checks if @func is available
146 * Returns 1 if @func is available for @opcode, 0 otherwise
148 static inline void __cpacf_query(unsigned int opcode
, cpacf_mask_t
*mask
)
150 register unsigned long r0
asm("0") = 0; /* query function */
151 register unsigned long r1
asm("1") = (unsigned long) mask
;
154 " spm 0\n" /* pckmo doesn't change the cc */
155 /* Parameter regs are ignored, but must be nonzero and unique */
156 "0: .insn rrf,%[opc] << 16,2,4,6,0\n"
157 " brc 1,0b\n" /* handle partial completion */
159 : [fc
] "d" (r0
), [pba
] "a" (r1
), [opc
] "i" (opcode
)
163 static inline int __cpacf_check_opcode(unsigned int opcode
)
171 return test_facility(17); /* check for MSA */
173 return test_facility(76); /* check for MSA3 */
178 return test_facility(77); /* check for MSA4 */
180 return test_facility(57); /* check for MSA5 */
186 static inline int cpacf_query(unsigned int opcode
, cpacf_mask_t
*mask
)
188 if (__cpacf_check_opcode(opcode
)) {
189 __cpacf_query(opcode
, mask
);
192 memset(mask
, 0, sizeof(*mask
));
196 static inline int cpacf_test_func(cpacf_mask_t
*mask
, unsigned int func
)
198 return (mask
->bytes
[func
>> 3] & (0x80 >> (func
& 7))) != 0;
201 static inline int cpacf_query_func(unsigned int opcode
, unsigned int func
)
205 if (cpacf_query(opcode
, &mask
))
206 return cpacf_test_func(&mask
, func
);
211 * cpacf_km() - executes the KM (CIPHER MESSAGE) instruction
212 * @func: the function code passed to KM; see CPACF_KM_xxx defines
213 * @param: address of parameter block; see POP for details on each func
214 * @dest: address of destination memory area
215 * @src: address of source memory area
216 * @src_len: length of src operand in bytes
218 * Returns 0 for the query func, number of processed bytes for
219 * encryption/decryption funcs
221 static inline int cpacf_km(unsigned long func
, void *param
,
222 u8
*dest
, const u8
*src
, long src_len
)
224 register unsigned long r0
asm("0") = (unsigned long) func
;
225 register unsigned long r1
asm("1") = (unsigned long) param
;
226 register unsigned long r2
asm("2") = (unsigned long) src
;
227 register unsigned long r3
asm("3") = (unsigned long) src_len
;
228 register unsigned long r4
asm("4") = (unsigned long) dest
;
231 "0: .insn rre,%[opc] << 16,%[dst],%[src]\n"
232 " brc 1,0b\n" /* handle partial completion */
233 : [src
] "+a" (r2
), [len
] "+d" (r3
), [dst
] "+a" (r4
)
234 : [fc
] "d" (r0
), [pba
] "a" (r1
), [opc
] "i" (CPACF_KM
)
241 * cpacf_kmc() - executes the KMC (CIPHER MESSAGE WITH CHAINING) instruction
242 * @func: the function code passed to KM; see CPACF_KMC_xxx defines
243 * @param: address of parameter block; see POP for details on each func
244 * @dest: address of destination memory area
245 * @src: address of source memory area
246 * @src_len: length of src operand in bytes
248 * Returns 0 for the query func, number of processed bytes for
249 * encryption/decryption funcs
251 static inline int cpacf_kmc(unsigned long func
, void *param
,
252 u8
*dest
, 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
;
258 register unsigned long r4
asm("4") = (unsigned long) dest
;
261 "0: .insn rre,%[opc] << 16,%[dst],%[src]\n"
262 " brc 1,0b\n" /* handle partial completion */
263 : [src
] "+a" (r2
), [len
] "+d" (r3
), [dst
] "+a" (r4
)
264 : [fc
] "d" (r0
), [pba
] "a" (r1
), [opc
] "i" (CPACF_KMC
)
271 * cpacf_kimd() - executes the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST)
273 * @func: the function code passed to KM; see CPACF_KIMD_xxx defines
274 * @param: address of parameter block; see POP for details on each func
275 * @src: address of source memory area
276 * @src_len: length of src operand in bytes
278 static inline void cpacf_kimd(unsigned long func
, void *param
,
279 const u8
*src
, long src_len
)
281 register unsigned long r0
asm("0") = (unsigned long) func
;
282 register unsigned long r1
asm("1") = (unsigned long) param
;
283 register unsigned long r2
asm("2") = (unsigned long) src
;
284 register unsigned long r3
asm("3") = (unsigned long) src_len
;
287 "0: .insn rre,%[opc] << 16,0,%[src]\n"
288 " brc 1,0b\n" /* handle partial completion */
289 : [src
] "+a" (r2
), [len
] "+d" (r3
)
290 : [fc
] "d" (r0
), [pba
] "a" (r1
), [opc
] "i" (CPACF_KIMD
)
295 * cpacf_klmd() - executes the KLMD (COMPUTE LAST MESSAGE DIGEST) instruction
296 * @func: the function code passed to KM; see CPACF_KLMD_xxx defines
297 * @param: address of parameter block; see POP for details on each func
298 * @src: address of source memory area
299 * @src_len: length of src operand in bytes
301 static inline void cpacf_klmd(unsigned 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_KLMD
)
318 * cpacf_kmac() - executes the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE)
320 * @func: the function code passed to KM; see CPACF_KMAC_xxx defines
321 * @param: address of parameter block; see POP for details on each func
322 * @src: address of source memory area
323 * @src_len: length of src operand in bytes
325 * Returns 0 for the query func, number of processed bytes for digest funcs
327 static inline int cpacf_kmac(unsigned long func
, void *param
,
328 const u8
*src
, long src_len
)
330 register unsigned long r0
asm("0") = (unsigned long) func
;
331 register unsigned long r1
asm("1") = (unsigned long) param
;
332 register unsigned long r2
asm("2") = (unsigned long) src
;
333 register unsigned long r3
asm("3") = (unsigned long) src_len
;
336 "0: .insn rre,%[opc] << 16,0,%[src]\n"
337 " brc 1,0b\n" /* handle partial completion */
338 : [src
] "+a" (r2
), [len
] "+d" (r3
)
339 : [fc
] "d" (r0
), [pba
] "a" (r1
), [opc
] "i" (CPACF_KMAC
)
346 * cpacf_kmctr() - executes the KMCTR (CIPHER MESSAGE WITH COUNTER) instruction
347 * @func: the function code passed to KMCTR; see CPACF_KMCTR_xxx defines
348 * @param: address of parameter block; see POP for details on each func
349 * @dest: address of destination memory area
350 * @src: address of source memory area
351 * @src_len: length of src operand in bytes
352 * @counter: address of counter value
354 * Returns 0 for the query func, number of processed bytes for
355 * encryption/decryption funcs
357 static inline int cpacf_kmctr(unsigned long func
, void *param
, u8
*dest
,
358 const u8
*src
, long src_len
, u8
*counter
)
360 register unsigned long r0
asm("0") = (unsigned long) func
;
361 register unsigned long r1
asm("1") = (unsigned long) param
;
362 register unsigned long r2
asm("2") = (unsigned long) src
;
363 register unsigned long r3
asm("3") = (unsigned long) src_len
;
364 register unsigned long r4
asm("4") = (unsigned long) dest
;
365 register unsigned long r6
asm("6") = (unsigned long) counter
;
368 "0: .insn rrf,%[opc] << 16,%[dst],%[src],%[ctr],0\n"
369 " brc 1,0b\n" /* handle partial completion */
370 : [src
] "+a" (r2
), [len
] "+d" (r3
),
371 [dst
] "+a" (r4
), [ctr
] "+a" (r6
)
372 : [fc
] "d" (r0
), [pba
] "a" (r1
), [opc
] "i" (CPACF_KMCTR
)
379 * cpacf_prno() - executes the PRNO (PERFORM RANDOM NUMBER OPERATION)
381 * @func: the function code passed to PRNO; see CPACF_PRNO_xxx defines
382 * @param: address of parameter block; see POP for details on each func
383 * @dest: address of destination memory area
384 * @dest_len: size of destination memory area in bytes
385 * @seed: address of seed data
386 * @seed_len: size of seed data in bytes
388 static inline void cpacf_prno(unsigned long func
, void *param
,
389 u8
*dest
, unsigned long dest_len
,
390 const u8
*seed
, unsigned long seed_len
)
392 register unsigned long r0
asm("0") = (unsigned long) func
;
393 register unsigned long r1
asm("1") = (unsigned long) param
;
394 register unsigned long r2
asm("2") = (unsigned long) dest
;
395 register unsigned long r3
asm("3") = (unsigned long) dest_len
;
396 register unsigned long r4
asm("4") = (unsigned long) seed
;
397 register unsigned long r5
asm("5") = (unsigned long) seed_len
;
400 "0: .insn rre,%[opc] << 16,%[dst],%[seed]\n"
401 " brc 1,0b\n" /* handle partial completion */
402 : [dst
] "+a" (r2
), [dlen
] "+d" (r3
)
403 : [fc
] "d" (r0
), [pba
] "a" (r1
),
404 [seed
] "a" (r4
), [slen
] "d" (r5
), [opc
] "i" (CPACF_PRNO
)
409 * cpacf_trng() - executes the TRNG subfunction of the PRNO instruction
410 * @ucbuf: buffer for unconditioned data
411 * @ucbuf_len: amount of unconditioned data to fetch in bytes
412 * @cbuf: buffer for conditioned data
413 * @cbuf_len: amount of conditioned data to fetch in bytes
415 static inline void cpacf_trng(u8
*ucbuf
, unsigned long ucbuf_len
,
416 u8
*cbuf
, unsigned long cbuf_len
)
418 register unsigned long r0
asm("0") = (unsigned long) CPACF_PRNO_TRNG
;
419 register unsigned long r2
asm("2") = (unsigned long) ucbuf
;
420 register unsigned long r3
asm("3") = (unsigned long) ucbuf_len
;
421 register unsigned long r4
asm("4") = (unsigned long) cbuf
;
422 register unsigned long r5
asm("5") = (unsigned long) cbuf_len
;
425 "0: .insn rre,%[opc] << 16,%[ucbuf],%[cbuf]\n"
426 " brc 1,0b\n" /* handle partial completion */
427 : [ucbuf
] "+a" (r2
), [ucbuflen
] "+d" (r3
),
428 [cbuf
] "+a" (r4
), [cbuflen
] "+d" (r5
)
429 : [fc
] "d" (r0
), [opc
] "i" (CPACF_PRNO
)
434 * cpacf_pcc() - executes the PCC (PERFORM CRYPTOGRAPHIC COMPUTATION)
436 * @func: the function code passed to PCC; see CPACF_KM_xxx defines
437 * @param: address of parameter block; see POP for details on each func
439 static inline void cpacf_pcc(unsigned long func
, void *param
)
441 register unsigned long r0
asm("0") = (unsigned long) func
;
442 register unsigned long r1
asm("1") = (unsigned long) param
;
445 "0: .insn rre,%[opc] << 16,0,0\n" /* PCC opcode */
446 " brc 1,0b\n" /* handle partial completion */
448 : [fc
] "d" (r0
), [pba
] "a" (r1
), [opc
] "i" (CPACF_PCC
)
453 * cpacf_pckmo() - executes the PCKMO (PERFORM CRYPTOGRAPHIC KEY
454 * MANAGEMENT) instruction
455 * @func: the function code passed to PCKMO; see CPACF_PCKMO_xxx defines
456 * @param: address of parameter block; see POP for details on each func
460 static inline void cpacf_pckmo(long func
, void *param
)
462 register unsigned long r0
asm("0") = (unsigned long) func
;
463 register unsigned long r1
asm("1") = (unsigned long) param
;
466 " .insn rre,%[opc] << 16,0,0\n" /* PCKMO opcode */
468 : [fc
] "d" (r0
), [pba
] "a" (r1
), [opc
] "i" (CPACF_PCKMO
)
472 #endif /* _ASM_S390_CPACF_H */