]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - arch/s390/include/asm/cpacf.h
s390/crypto: Renaming PPNO to PRNO.
[mirror_ubuntu-focal-kernel.git] / arch / s390 / include / asm / cpacf.h
CommitLineData
c7d4d259
MS
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 */
1afd43e0
DH
23#define CPACF_PCKMO 0xb928 /* MSA3 */
24#define CPACF_KMF 0xb92a /* MSA4 */
25#define CPACF_KMO 0xb92b /* MSA4 */
c7d4d259
MS
26#define CPACF_PCC 0xb92c /* MSA4 */
27#define CPACF_KMCTR 0xb92d /* MSA4 */
985a9d20 28#define CPACF_PRNO 0xb93c /* MSA5 */
c7d4d259
MS
29
30/*
27937843 31 * En/decryption modifier bits
edc63a37 32 */
27937843 33#define CPACF_ENCRYPT 0x00
edc63a37
MS
34#define CPACF_DECRYPT 0x80
35
36/*
37 * Function codes for the KM (CIPHER MESSAGE) instruction
c7d4d259
MS
38 */
39#define CPACF_KM_QUERY 0x00
edc63a37
MS
40#define CPACF_KM_DEA 0x01
41#define CPACF_KM_TDEA_128 0x02
42#define CPACF_KM_TDEA_192 0x03
43#define CPACF_KM_AES_128 0x12
44#define CPACF_KM_AES_192 0x13
45#define CPACF_KM_AES_256 0x14
27937843
MS
46#define CPACF_KM_PAES_128 0x1a
47#define CPACF_KM_PAES_192 0x1b
48#define CPACF_KM_PAES_256 0x1c
edc63a37
MS
49#define CPACF_KM_XTS_128 0x32
50#define CPACF_KM_XTS_256 0x34
27937843
MS
51#define CPACF_KM_PXTS_128 0x3a
52#define CPACF_KM_PXTS_256 0x3c
c7d4d259
MS
53
54/*
55 * Function codes for the KMC (CIPHER MESSAGE WITH CHAINING)
edc63a37 56 * instruction
c7d4d259
MS
57 */
58#define CPACF_KMC_QUERY 0x00
edc63a37
MS
59#define CPACF_KMC_DEA 0x01
60#define CPACF_KMC_TDEA_128 0x02
61#define CPACF_KMC_TDEA_192 0x03
62#define CPACF_KMC_AES_128 0x12
63#define CPACF_KMC_AES_192 0x13
64#define CPACF_KMC_AES_256 0x14
27937843
MS
65#define CPACF_KMC_PAES_128 0x1a
66#define CPACF_KMC_PAES_192 0x1b
67#define CPACF_KMC_PAES_256 0x1c
c7d4d259
MS
68#define CPACF_KMC_PRNG 0x43
69
70/*
71 * Function codes for the KMCTR (CIPHER MESSAGE WITH COUNTER)
edc63a37 72 * instruction
c7d4d259 73 */
edc63a37
MS
74#define CPACF_KMCTR_QUERY 0x00
75#define CPACF_KMCTR_DEA 0x01
76#define CPACF_KMCTR_TDEA_128 0x02
77#define CPACF_KMCTR_TDEA_192 0x03
78#define CPACF_KMCTR_AES_128 0x12
79#define CPACF_KMCTR_AES_192 0x13
80#define CPACF_KMCTR_AES_256 0x14
27937843
MS
81#define CPACF_KMCTR_PAES_128 0x1a
82#define CPACF_KMCTR_PAES_192 0x1b
83#define CPACF_KMCTR_PAES_256 0x1c
c7d4d259
MS
84
85/*
86 * Function codes for the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST)
edc63a37 87 * instruction
c7d4d259
MS
88 */
89#define CPACF_KIMD_QUERY 0x00
90#define CPACF_KIMD_SHA_1 0x01
91#define CPACF_KIMD_SHA_256 0x02
92#define CPACF_KIMD_SHA_512 0x03
93#define CPACF_KIMD_GHASH 0x41
94
95/*
96 * Function codes for the KLMD (COMPUTE LAST MESSAGE DIGEST)
edc63a37 97 * instruction
c7d4d259
MS
98 */
99#define CPACF_KLMD_QUERY 0x00
100#define CPACF_KLMD_SHA_1 0x01
101#define CPACF_KLMD_SHA_256 0x02
102#define CPACF_KLMD_SHA_512 0x03
103
104/*
105 * function codes for the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE)
edc63a37 106 * instruction
c7d4d259
MS
107 */
108#define CPACF_KMAC_QUERY 0x00
109#define CPACF_KMAC_DEA 0x01
110#define CPACF_KMAC_TDEA_128 0x02
111#define CPACF_KMAC_TDEA_192 0x03
112
77606708
HF
113/*
114 * Function codes for the PCKMO (PERFORM CRYPTOGRAPHIC KEY MANAGEMENT)
115 * instruction
116 */
117#define CPACF_PCKMO_QUERY 0x00
118#define CPACF_PCKMO_ENC_DES_KEY 0x01
119#define CPACF_PCKMO_ENC_TDES_128_KEY 0x02
120#define CPACF_PCKMO_ENC_TDES_192_KEY 0x03
121#define CPACF_PCKMO_ENC_AES_128_KEY 0x12
122#define CPACF_PCKMO_ENC_AES_192_KEY 0x13
123#define CPACF_PCKMO_ENC_AES_256_KEY 0x14
124
c7d4d259 125/*
985a9d20 126 * Function codes for the PRNO (PERFORM RANDOM NUMBER OPERATION)
edc63a37 127 * instruction
c7d4d259 128 */
985a9d20
HF
129#define CPACF_PRNO_QUERY 0x00
130#define CPACF_PRNO_SHA512_DRNG_GEN 0x03
131#define CPACF_PRNO_SHA512_DRNG_SEED 0x83
c7d4d259 132
69c0e360
MS
133typedef struct { unsigned char bytes[16]; } cpacf_mask_t;
134
c7d4d259
MS
135/**
136 * cpacf_query() - check if a specific CPACF function is available
137 * @opcode: the opcode of the crypto instruction
138 * @func: the function code to test for
139 *
140 * Executes the query function for the given crypto instruction @opcode
141 * and checks if @func is available
142 *
143 * Returns 1 if @func is available for @opcode, 0 otherwise
144 */
69c0e360 145static inline void __cpacf_query(unsigned int opcode, cpacf_mask_t *mask)
c7d4d259 146{
c7d4d259 147 register unsigned long r0 asm("0") = 0; /* query function */
69c0e360 148 register unsigned long r1 asm("1") = (unsigned long) mask;
c7d4d259
MS
149
150 asm volatile(
1afd43e0 151 " spm 0\n" /* pckmo doesn't change the cc */
c7d4d259
MS
152 /* Parameter registers are ignored, but may not be 0 */
153 "0: .insn rrf,%[opc] << 16,2,2,2,0\n"
154 " brc 1,0b\n" /* handle partial completion */
69c0e360 155 : "=m" (*mask)
c7d4d259
MS
156 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (opcode)
157 : "cc");
158}
159
69c0e360 160static inline int __cpacf_check_opcode(unsigned int opcode)
c7d4d259 161{
c7d4d259
MS
162 switch (opcode) {
163 case CPACF_KMAC:
164 case CPACF_KM:
165 case CPACF_KMC:
166 case CPACF_KIMD:
167 case CPACF_KLMD:
69c0e360 168 return test_facility(17); /* check for MSA */
1afd43e0 169 case CPACF_PCKMO:
69c0e360 170 return test_facility(76); /* check for MSA3 */
1afd43e0
DH
171 case CPACF_KMF:
172 case CPACF_KMO:
c7d4d259
MS
173 case CPACF_PCC:
174 case CPACF_KMCTR:
69c0e360 175 return test_facility(77); /* check for MSA4 */
985a9d20 176 case CPACF_PRNO:
69c0e360 177 return test_facility(57); /* check for MSA5 */
c7d4d259
MS
178 default:
179 BUG();
180 }
69c0e360
MS
181}
182
183static inline int cpacf_query(unsigned int opcode, cpacf_mask_t *mask)
184{
185 if (__cpacf_check_opcode(opcode)) {
186 __cpacf_query(opcode, mask);
187 return 1;
188 }
189 memset(mask, 0, sizeof(*mask));
190 return 0;
191}
192
193static inline int cpacf_test_func(cpacf_mask_t *mask, unsigned int func)
194{
195 return (mask->bytes[func >> 3] & (0x80 >> (func & 7))) != 0;
196}
197
198static inline int cpacf_query_func(unsigned int opcode, unsigned int func)
199{
200 cpacf_mask_t mask;
201
202 if (cpacf_query(opcode, &mask))
203 return cpacf_test_func(&mask, func);
204 return 0;
c7d4d259
MS
205}
206
207/**
208 * cpacf_km() - executes the KM (CIPHER MESSAGE) instruction
209 * @func: the function code passed to KM; see CPACF_KM_xxx defines
210 * @param: address of parameter block; see POP for details on each func
211 * @dest: address of destination memory area
212 * @src: address of source memory area
213 * @src_len: length of src operand in bytes
214 *
215 * Returns 0 for the query func, number of processed bytes for
216 * encryption/decryption funcs
217 */
edc63a37 218static inline int cpacf_km(unsigned long func, void *param,
c7d4d259
MS
219 u8 *dest, const u8 *src, long src_len)
220{
221 register unsigned long r0 asm("0") = (unsigned long) func;
222 register unsigned long r1 asm("1") = (unsigned long) param;
223 register unsigned long r2 asm("2") = (unsigned long) src;
224 register unsigned long r3 asm("3") = (unsigned long) src_len;
225 register unsigned long r4 asm("4") = (unsigned long) dest;
226
227 asm volatile(
228 "0: .insn rre,%[opc] << 16,%[dst],%[src]\n"
229 " brc 1,0b\n" /* handle partial completion */
230 : [src] "+a" (r2), [len] "+d" (r3), [dst] "+a" (r4)
231 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KM)
232 : "cc", "memory");
233
234 return src_len - r3;
235}
236
237/**
238 * cpacf_kmc() - executes the KMC (CIPHER MESSAGE WITH CHAINING) instruction
239 * @func: the function code passed to KM; see CPACF_KMC_xxx defines
240 * @param: address of parameter block; see POP for details on each func
241 * @dest: address of destination memory area
242 * @src: address of source memory area
243 * @src_len: length of src operand in bytes
244 *
245 * Returns 0 for the query func, number of processed bytes for
246 * encryption/decryption funcs
247 */
edc63a37 248static inline int cpacf_kmc(unsigned long func, void *param,
c7d4d259
MS
249 u8 *dest, const u8 *src, long src_len)
250{
251 register unsigned long r0 asm("0") = (unsigned long) func;
252 register unsigned long r1 asm("1") = (unsigned long) param;
253 register unsigned long r2 asm("2") = (unsigned long) src;
254 register unsigned long r3 asm("3") = (unsigned long) src_len;
255 register unsigned long r4 asm("4") = (unsigned long) dest;
256
257 asm volatile(
258 "0: .insn rre,%[opc] << 16,%[dst],%[src]\n"
259 " brc 1,0b\n" /* handle partial completion */
260 : [src] "+a" (r2), [len] "+d" (r3), [dst] "+a" (r4)
261 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KMC)
262 : "cc", "memory");
263
264 return src_len - r3;
265}
266
267/**
268 * cpacf_kimd() - executes the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST)
269 * instruction
270 * @func: the function code passed to KM; see CPACF_KIMD_xxx defines
271 * @param: address of parameter block; see POP for details on each func
272 * @src: address of source memory area
273 * @src_len: length of src operand in bytes
c7d4d259 274 */
0177db01
MS
275static inline void cpacf_kimd(unsigned long func, void *param,
276 const u8 *src, long src_len)
c7d4d259
MS
277{
278 register unsigned long r0 asm("0") = (unsigned long) func;
279 register unsigned long r1 asm("1") = (unsigned long) param;
280 register unsigned long r2 asm("2") = (unsigned long) src;
281 register unsigned long r3 asm("3") = (unsigned long) src_len;
282
283 asm volatile(
284 "0: .insn rre,%[opc] << 16,0,%[src]\n"
285 " brc 1,0b\n" /* handle partial completion */
286 : [src] "+a" (r2), [len] "+d" (r3)
287 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KIMD)
288 : "cc", "memory");
c7d4d259
MS
289}
290
291/**
292 * cpacf_klmd() - executes the KLMD (COMPUTE LAST MESSAGE DIGEST) instruction
293 * @func: the function code passed to KM; see CPACF_KLMD_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
c7d4d259 297 */
0177db01
MS
298static inline void cpacf_klmd(unsigned long func, void *param,
299 const u8 *src, long src_len)
c7d4d259
MS
300{
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;
305
306 asm volatile(
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_KLMD)
311 : "cc", "memory");
c7d4d259
MS
312}
313
314/**
315 * cpacf_kmac() - executes the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE)
316 * instruction
317 * @func: the function code passed to KM; see CPACF_KMAC_xxx defines
318 * @param: address of parameter block; see POP for details on each func
319 * @src: address of source memory area
320 * @src_len: length of src operand in bytes
321 *
322 * Returns 0 for the query func, number of processed bytes for digest funcs
323 */
edc63a37 324static inline int cpacf_kmac(unsigned long func, void *param,
c7d4d259
MS
325 const u8 *src, long src_len)
326{
327 register unsigned long r0 asm("0") = (unsigned long) func;
328 register unsigned long r1 asm("1") = (unsigned long) param;
329 register unsigned long r2 asm("2") = (unsigned long) src;
330 register unsigned long r3 asm("3") = (unsigned long) src_len;
331
332 asm volatile(
333 "0: .insn rre,%[opc] << 16,0,%[src]\n"
334 " brc 1,0b\n" /* handle partial completion */
335 : [src] "+a" (r2), [len] "+d" (r3)
336 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KMAC)
337 : "cc", "memory");
338
339 return src_len - r3;
340}
341
342/**
343 * cpacf_kmctr() - executes the KMCTR (CIPHER MESSAGE WITH COUNTER) instruction
344 * @func: the function code passed to KMCTR; see CPACF_KMCTR_xxx defines
345 * @param: address of parameter block; see POP for details on each func
346 * @dest: address of destination memory area
347 * @src: address of source memory area
348 * @src_len: length of src operand in bytes
349 * @counter: address of counter value
350 *
351 * Returns 0 for the query func, number of processed bytes for
352 * encryption/decryption funcs
353 */
edc63a37 354static inline int cpacf_kmctr(unsigned long func, void *param, u8 *dest,
c7d4d259
MS
355 const u8 *src, long src_len, u8 *counter)
356{
357 register unsigned long r0 asm("0") = (unsigned long) func;
358 register unsigned long r1 asm("1") = (unsigned long) param;
359 register unsigned long r2 asm("2") = (unsigned long) src;
360 register unsigned long r3 asm("3") = (unsigned long) src_len;
361 register unsigned long r4 asm("4") = (unsigned long) dest;
362 register unsigned long r6 asm("6") = (unsigned long) counter;
363
364 asm volatile(
365 "0: .insn rrf,%[opc] << 16,%[dst],%[src],%[ctr],0\n"
366 " brc 1,0b\n" /* handle partial completion */
367 : [src] "+a" (r2), [len] "+d" (r3),
368 [dst] "+a" (r4), [ctr] "+a" (r6)
369 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KMCTR)
370 : "cc", "memory");
371
372 return src_len - r3;
373}
374
375/**
985a9d20 376 * cpacf_prno() - executes the PRNO (PERFORM RANDOM NUMBER OPERATION)
c7d4d259 377 * instruction
985a9d20 378 * @func: the function code passed to PRNO; see CPACF_PRNO_xxx defines
c7d4d259
MS
379 * @param: address of parameter block; see POP for details on each func
380 * @dest: address of destination memory area
381 * @dest_len: size of destination memory area in bytes
382 * @seed: address of seed data
383 * @seed_len: size of seed data in bytes
c7d4d259 384 */
985a9d20 385static inline void cpacf_prno(unsigned long func, void *param,
0177db01
MS
386 u8 *dest, long dest_len,
387 const u8 *seed, long seed_len)
c7d4d259
MS
388{
389 register unsigned long r0 asm("0") = (unsigned long) func;
390 register unsigned long r1 asm("1") = (unsigned long) param;
391 register unsigned long r2 asm("2") = (unsigned long) dest;
392 register unsigned long r3 asm("3") = (unsigned long) dest_len;
393 register unsigned long r4 asm("4") = (unsigned long) seed;
394 register unsigned long r5 asm("5") = (unsigned long) seed_len;
395
396 asm volatile (
397 "0: .insn rre,%[opc] << 16,%[dst],%[seed]\n"
398 " brc 1,0b\n" /* handle partial completion */
399 : [dst] "+a" (r2), [dlen] "+d" (r3)
400 : [fc] "d" (r0), [pba] "a" (r1),
985a9d20 401 [seed] "a" (r4), [slen] "d" (r5), [opc] "i" (CPACF_PRNO)
c7d4d259 402 : "cc", "memory");
c7d4d259
MS
403}
404
405/**
406 * cpacf_pcc() - executes the PCC (PERFORM CRYPTOGRAPHIC COMPUTATION)
407 * instruction
408 * @func: the function code passed to PCC; see CPACF_KM_xxx defines
409 * @param: address of parameter block; see POP for details on each func
c7d4d259 410 */
0177db01 411static inline void cpacf_pcc(unsigned long func, void *param)
c7d4d259
MS
412{
413 register unsigned long r0 asm("0") = (unsigned long) func;
414 register unsigned long r1 asm("1") = (unsigned long) param;
415
416 asm volatile(
417 "0: .insn rre,%[opc] << 16,0,0\n" /* PCC opcode */
418 " brc 1,0b\n" /* handle partial completion */
419 :
420 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_PCC)
421 : "cc", "memory");
c7d4d259
MS
422}
423
77606708
HF
424/**
425 * cpacf_pckmo() - executes the PCKMO (PERFORM CRYPTOGRAPHIC KEY
426 * MANAGEMENT) instruction
427 * @func: the function code passed to PCKMO; see CPACF_PCKMO_xxx defines
428 * @param: address of parameter block; see POP for details on each func
429 *
430 * Returns 0.
431 */
432static inline void cpacf_pckmo(long func, void *param)
433{
434 register unsigned long r0 asm("0") = (unsigned long) func;
435 register unsigned long r1 asm("1") = (unsigned long) param;
436
437 asm volatile(
438 " .insn rre,%[opc] << 16,0,0\n" /* PCKMO opcode */
439 :
440 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_PCKMO)
441 : "cc", "memory");
442}
443
c7d4d259 444#endif /* _ASM_S390_CPACF_H */