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