1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * CP Assist for Cryptographic Functions (CPACF)
4 *
5 * Copyright IBM Corp. 2003, 2023
6 * Author(s): Thomas Spatzier
7 * Jan Glauber
8 * Harald Freudenberger (freude@de.ibm.com)
9 * Martin Schwidefsky <schwidefsky@de.ibm.com>
10 */
11 #ifndef _ASM_S390_CPACF_H
12 #define _ASM_S390_CPACF_H
13
14 #include <asm/facility.h>
15 #include <linux/kmsan-checks.h>
16
17 /*
18 * Instruction opcodes for the CPACF instructions
19 */
20 #define CPACF_KMAC 0xb91e /* MSA */
21 #define CPACF_KM 0xb92e /* MSA */
22 #define CPACF_KMC 0xb92f /* MSA */
23 #define CPACF_KIMD 0xb93e /* MSA */
24 #define CPACF_KLMD 0xb93f /* MSA */
25 #define CPACF_PCKMO 0xb928 /* MSA3 */
26 #define CPACF_KMF 0xb92a /* MSA4 */
27 #define CPACF_KMO 0xb92b /* MSA4 */
28 #define CPACF_PCC 0xb92c /* MSA4 */
29 #define CPACF_KMCTR 0xb92d /* MSA4 */
30 #define CPACF_PRNO 0xb93c /* MSA5 */
31 #define CPACF_KMA 0xb929 /* MSA8 */
32 #define CPACF_KDSA 0xb93a /* MSA9 */
33
34 /*
35 * En/decryption modifier bits
36 */
37 #define CPACF_ENCRYPT 0x00
38 #define CPACF_DECRYPT 0x80
39
40 /*
41 * Function codes for the KM (CIPHER MESSAGE) instruction
42 */
43 #define CPACF_KM_QUERY 0x00
44 #define CPACF_KM_DEA 0x01
45 #define CPACF_KM_TDEA_128 0x02
46 #define CPACF_KM_TDEA_192 0x03
47 #define CPACF_KM_AES_128 0x12
48 #define CPACF_KM_AES_192 0x13
49 #define CPACF_KM_AES_256 0x14
50 #define CPACF_KM_PAES_128 0x1a
51 #define CPACF_KM_PAES_192 0x1b
52 #define CPACF_KM_PAES_256 0x1c
53 #define CPACF_KM_XTS_128 0x32
54 #define CPACF_KM_XTS_256 0x34
55 #define CPACF_KM_PXTS_128 0x3a
56 #define CPACF_KM_PXTS_256 0x3c
57 #define CPACF_KM_XTS_128_FULL 0x52
58 #define CPACF_KM_XTS_256_FULL 0x54
59
60 /*
61 * Function codes for the KMC (CIPHER MESSAGE WITH CHAINING)
62 * instruction
63 */
64 #define CPACF_KMC_QUERY 0x00
65 #define CPACF_KMC_DEA 0x01
66 #define CPACF_KMC_TDEA_128 0x02
67 #define CPACF_KMC_TDEA_192 0x03
68 #define CPACF_KMC_AES_128 0x12
69 #define CPACF_KMC_AES_192 0x13
70 #define CPACF_KMC_AES_256 0x14
71 #define CPACF_KMC_PAES_128 0x1a
72 #define CPACF_KMC_PAES_192 0x1b
73 #define CPACF_KMC_PAES_256 0x1c
74 #define CPACF_KMC_PRNG 0x43
75
76 /*
77 * Function codes for the KMCTR (CIPHER MESSAGE WITH COUNTER)
78 * instruction
79 */
80 #define CPACF_KMCTR_QUERY 0x00
81 #define CPACF_KMCTR_DEA 0x01
82 #define CPACF_KMCTR_TDEA_128 0x02
83 #define CPACF_KMCTR_TDEA_192 0x03
84 #define CPACF_KMCTR_AES_128 0x12
85 #define CPACF_KMCTR_AES_192 0x13
86 #define CPACF_KMCTR_AES_256 0x14
87 #define CPACF_KMCTR_PAES_128 0x1a
88 #define CPACF_KMCTR_PAES_192 0x1b
89 #define CPACF_KMCTR_PAES_256 0x1c
90
91 /*
92 * Function codes for the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST)
93 * instruction
94 */
95 #define CPACF_KIMD_QUERY 0x00
96 #define CPACF_KIMD_SHA_1 0x01
97 #define CPACF_KIMD_SHA_256 0x02
98 #define CPACF_KIMD_SHA_512 0x03
99 #define CPACF_KIMD_SHA3_224 0x20
100 #define CPACF_KIMD_SHA3_256 0x21
101 #define CPACF_KIMD_SHA3_384 0x22
102 #define CPACF_KIMD_SHA3_512 0x23
103 #define CPACF_KIMD_GHASH 0x41
104
105 /*
106 * Function codes for the KLMD (COMPUTE LAST MESSAGE DIGEST)
107 * instruction
108 */
109 #define CPACF_KLMD_QUERY 0x00
110 #define CPACF_KLMD_SHA_1 0x01
111 #define CPACF_KLMD_SHA_256 0x02
112 #define CPACF_KLMD_SHA_512 0x03
113 #define CPACF_KLMD_SHA3_224 0x20
114 #define CPACF_KLMD_SHA3_256 0x21
115 #define CPACF_KLMD_SHA3_384 0x22
116 #define CPACF_KLMD_SHA3_512 0x23
117
118 /*
119 * function codes for the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE)
120 * instruction
121 */
122 #define CPACF_KMAC_QUERY 0x00
123 #define CPACF_KMAC_DEA 0x01
124 #define CPACF_KMAC_TDEA_128 0x02
125 #define CPACF_KMAC_TDEA_192 0x03
126 #define CPACF_KMAC_HMAC_SHA_224 0x70
127 #define CPACF_KMAC_HMAC_SHA_256 0x71
128 #define CPACF_KMAC_HMAC_SHA_384 0x72
129 #define CPACF_KMAC_HMAC_SHA_512 0x73
130
131 /*
132 * Function codes for the PCKMO (PERFORM CRYPTOGRAPHIC KEY MANAGEMENT)
133 * instruction
134 */
135 #define CPACF_PCKMO_QUERY 0x00
136 #define CPACF_PCKMO_ENC_DES_KEY 0x01
137 #define CPACF_PCKMO_ENC_TDES_128_KEY 0x02
138 #define CPACF_PCKMO_ENC_TDES_192_KEY 0x03
139 #define CPACF_PCKMO_ENC_AES_128_KEY 0x12
140 #define CPACF_PCKMO_ENC_AES_192_KEY 0x13
141 #define CPACF_PCKMO_ENC_AES_256_KEY 0x14
142 #define CPACF_PCKMO_ENC_AES_XTS_128_DOUBLE_KEY 0x15
143 #define CPACF_PCKMO_ENC_AES_XTS_256_DOUBLE_KEY 0x16
144 #define CPACF_PCKMO_ENC_ECC_P256_KEY 0x20
145 #define CPACF_PCKMO_ENC_ECC_P384_KEY 0x21
146 #define CPACF_PCKMO_ENC_ECC_P521_KEY 0x22
147 #define CPACF_PCKMO_ENC_ECC_ED25519_KEY 0x28
148 #define CPACF_PCKMO_ENC_ECC_ED448_KEY 0x29
149 #define CPACF_PCKMO_ENC_HMAC_512_KEY 0x76
150 #define CPACF_PCKMO_ENC_HMAC_1024_KEY 0x7a
151
152 /*
153 * Function codes for the PRNO (PERFORM RANDOM NUMBER OPERATION)
154 * instruction
155 */
156 #define CPACF_PRNO_QUERY 0x00
157 #define CPACF_PRNO_SHA512_DRNG_GEN 0x03
158 #define CPACF_PRNO_SHA512_DRNG_SEED 0x83
159 #define CPACF_PRNO_TRNG_Q_R2C_RATIO 0x70
160 #define CPACF_PRNO_TRNG 0x72
161
162 /*
163 * Function codes for the KMA (CIPHER MESSAGE WITH AUTHENTICATION)
164 * instruction
165 */
166 #define CPACF_KMA_QUERY 0x00
167 #define CPACF_KMA_GCM_AES_128 0x12
168 #define CPACF_KMA_GCM_AES_192 0x13
169 #define CPACF_KMA_GCM_AES_256 0x14
170
171 /*
172 * Flags for the KMA (CIPHER MESSAGE WITH AUTHENTICATION) instruction
173 */
174 #define CPACF_KMA_LPC 0x100 /* Last-Plaintext/Ciphertext */
175 #define CPACF_KMA_LAAD 0x200 /* Last-AAD */
176 #define CPACF_KMA_HS 0x400 /* Hash-subkey Supplied */
177
178 /*
179 * Flags for the KIMD/KLMD (COMPUTE INTERMEDIATE/LAST MESSAGE DIGEST)
180 * instructions
181 */
182 #define CPACF_KIMD_NIP 0x8000
183 #define CPACF_KLMD_DUFOP 0x4000
184 #define CPACF_KLMD_NIP 0x8000
185
186 /*
187 * Function codes for KDSA (COMPUTE DIGITAL SIGNATURE AUTHENTICATION)
188 * instruction
189 */
190 #define CPACF_KDSA_QUERY 0x00
191 #define CPACF_KDSA_ECDSA_VERIFY_P256 0x01
192 #define CPACF_KDSA_ECDSA_VERIFY_P384 0x02
193 #define CPACF_KDSA_ECDSA_VERIFY_P521 0x03
194 #define CPACF_KDSA_ECDSA_SIGN_P256 0x09
195 #define CPACF_KDSA_ECDSA_SIGN_P384 0x0a
196 #define CPACF_KDSA_ECDSA_SIGN_P521 0x0b
197 #define CPACF_KDSA_ENC_ECDSA_SIGN_P256 0x11
198 #define CPACF_KDSA_ENC_ECDSA_SIGN_P384 0x12
199 #define CPACF_KDSA_ENC_ECDSA_SIGN_P521 0x13
200 #define CPACF_KDSA_EDDSA_VERIFY_ED25519 0x20
201 #define CPACF_KDSA_EDDSA_VERIFY_ED448 0x24
202 #define CPACF_KDSA_EDDSA_SIGN_ED25519 0x28
203 #define CPACF_KDSA_EDDSA_SIGN_ED448 0x2c
204 #define CPACF_KDSA_ENC_EDDSA_SIGN_ED25519 0x30
205 #define CPACF_KDSA_ENC_EDDSA_SIGN_ED448 0x34
206
207 #define CPACF_FC_QUERY 0x00
208 #define CPACF_FC_QUERY_AUTH_INFO 0x7F
209
210 typedef struct { unsigned char bytes[16]; } cpacf_mask_t;
211 typedef struct { unsigned char bytes[256]; } cpacf_qai_t;
212
213 /*
214 * Prototype for a not existing function to produce a link
215 * error if __cpacf_query() or __cpacf_check_opcode() is used
216 * with an invalid compile time const opcode.
217 */
218 void __cpacf_bad_opcode(void);
219
__cpacf_query_rre(u32 opc,u8 r1,u8 r2,u8 * pb,u8 fc)220 static __always_inline void __cpacf_query_rre(u32 opc, u8 r1, u8 r2,
221 u8 *pb, u8 fc)
222 {
223 asm volatile(
224 " la %%r1,%[pb]\n"
225 " lghi %%r0,%[fc]\n"
226 " .insn rre,%[opc] << 16,%[r1],%[r2]\n"
227 : [pb] "=R" (*pb)
228 : [opc] "i" (opc), [fc] "i" (fc),
229 [r1] "i" (r1), [r2] "i" (r2)
230 : "cc", "memory", "r0", "r1");
231 }
232
__cpacf_query_rrf(u32 opc,u8 r1,u8 r2,u8 r3,u8 m4,u8 * pb,u8 fc)233 static __always_inline void __cpacf_query_rrf(u32 opc, u8 r1, u8 r2, u8 r3,
234 u8 m4, u8 *pb, u8 fc)
235 {
236 asm volatile(
237 " la %%r1,%[pb]\n"
238 " lghi %%r0,%[fc]\n"
239 " .insn rrf,%[opc] << 16,%[r1],%[r2],%[r3],%[m4]\n"
240 : [pb] "=R" (*pb)
241 : [opc] "i" (opc), [fc] "i" (fc), [r1] "i" (r1),
242 [r2] "i" (r2), [r3] "i" (r3), [m4] "i" (m4)
243 : "cc", "memory", "r0", "r1");
244 }
245
__cpacf_query_insn(unsigned int opcode,void * pb,u8 fc)246 static __always_inline void __cpacf_query_insn(unsigned int opcode, void *pb,
247 u8 fc)
248 {
249 switch (opcode) {
250 case CPACF_KDSA:
251 __cpacf_query_rre(CPACF_KDSA, 0, 2, pb, fc);
252 break;
253 case CPACF_KIMD:
254 __cpacf_query_rre(CPACF_KIMD, 0, 2, pb, fc);
255 break;
256 case CPACF_KLMD:
257 __cpacf_query_rre(CPACF_KLMD, 0, 2, pb, fc);
258 break;
259 case CPACF_KM:
260 __cpacf_query_rre(CPACF_KM, 2, 4, pb, fc);
261 break;
262 case CPACF_KMA:
263 __cpacf_query_rrf(CPACF_KMA, 2, 4, 6, 0, pb, fc);
264 break;
265 case CPACF_KMAC:
266 __cpacf_query_rre(CPACF_KMAC, 0, 2, pb, fc);
267 break;
268 case CPACF_KMC:
269 __cpacf_query_rre(CPACF_KMC, 2, 4, pb, fc);
270 break;
271 case CPACF_KMCTR:
272 __cpacf_query_rrf(CPACF_KMCTR, 2, 4, 6, 0, pb, fc);
273 break;
274 case CPACF_KMF:
275 __cpacf_query_rre(CPACF_KMF, 2, 4, pb, fc);
276 break;
277 case CPACF_KMO:
278 __cpacf_query_rre(CPACF_KMO, 2, 4, pb, fc);
279 break;
280 case CPACF_PCC:
281 __cpacf_query_rre(CPACF_PCC, 0, 0, pb, fc);
282 break;
283 case CPACF_PCKMO:
284 __cpacf_query_rre(CPACF_PCKMO, 0, 0, pb, fc);
285 break;
286 case CPACF_PRNO:
287 __cpacf_query_rre(CPACF_PRNO, 2, 4, pb, fc);
288 break;
289 default:
290 __cpacf_bad_opcode();
291 }
292 }
293
__cpacf_query(unsigned int opcode,cpacf_mask_t * mask)294 static __always_inline void __cpacf_query(unsigned int opcode,
295 cpacf_mask_t *mask)
296 {
297 __cpacf_query_insn(opcode, mask, CPACF_FC_QUERY);
298 }
299
__cpacf_check_opcode(unsigned int opcode)300 static __always_inline int __cpacf_check_opcode(unsigned int opcode)
301 {
302 switch (opcode) {
303 case CPACF_KMAC:
304 case CPACF_KM:
305 case CPACF_KMC:
306 case CPACF_KIMD:
307 case CPACF_KLMD:
308 return test_facility(17); /* check for MSA */
309 case CPACF_PCKMO:
310 return test_facility(76); /* check for MSA3 */
311 case CPACF_KMF:
312 case CPACF_KMO:
313 case CPACF_PCC:
314 case CPACF_KMCTR:
315 return test_facility(77); /* check for MSA4 */
316 case CPACF_PRNO:
317 return test_facility(57); /* check for MSA5 */
318 case CPACF_KMA:
319 return test_facility(146); /* check for MSA8 */
320 case CPACF_KDSA:
321 return test_facility(155); /* check for MSA9 */
322 default:
323 __cpacf_bad_opcode();
324 return 0;
325 }
326 }
327
328 /**
329 * cpacf_query() - Query the function code mask for this CPACF opcode
330 * @opcode: the opcode of the crypto instruction
331 * @mask: ptr to struct cpacf_mask_t
332 *
333 * Executes the query function for the given crypto instruction @opcode
334 * and checks if @func is available
335 *
336 * On success 1 is returned and the mask is filled with the function
337 * code mask for this CPACF opcode, otherwise 0 is returned.
338 */
cpacf_query(unsigned int opcode,cpacf_mask_t * mask)339 static __always_inline int cpacf_query(unsigned int opcode, cpacf_mask_t *mask)
340 {
341 if (__cpacf_check_opcode(opcode)) {
342 __cpacf_query(opcode, mask);
343 return 1;
344 }
345 memset(mask, 0, sizeof(*mask));
346 return 0;
347 }
348
cpacf_test_func(cpacf_mask_t * mask,unsigned int func)349 static inline int cpacf_test_func(cpacf_mask_t *mask, unsigned int func)
350 {
351 return (mask->bytes[func >> 3] & (0x80 >> (func & 7))) != 0;
352 }
353
cpacf_query_func(unsigned int opcode,unsigned int func)354 static __always_inline int cpacf_query_func(unsigned int opcode,
355 unsigned int func)
356 {
357 cpacf_mask_t mask;
358
359 if (cpacf_query(opcode, &mask))
360 return cpacf_test_func(&mask, func);
361 return 0;
362 }
363
__cpacf_qai(unsigned int opcode,cpacf_qai_t * qai)364 static __always_inline void __cpacf_qai(unsigned int opcode, cpacf_qai_t *qai)
365 {
366 __cpacf_query_insn(opcode, qai, CPACF_FC_QUERY_AUTH_INFO);
367 }
368
369 /**
370 * cpacf_qai() - Get the query authentication information for a CPACF opcode
371 * @opcode: the opcode of the crypto instruction
372 * @mask: ptr to struct cpacf_qai_t
373 *
374 * Executes the query authentication information function for the given crypto
375 * instruction @opcode and checks if @func is available
376 *
377 * On success 1 is returned and the mask is filled with the query authentication
378 * information for this CPACF opcode, otherwise 0 is returned.
379 */
cpacf_qai(unsigned int opcode,cpacf_qai_t * qai)380 static __always_inline int cpacf_qai(unsigned int opcode, cpacf_qai_t *qai)
381 {
382 if (cpacf_query_func(opcode, CPACF_FC_QUERY_AUTH_INFO)) {
383 __cpacf_qai(opcode, qai);
384 return 1;
385 }
386 memset(qai, 0, sizeof(*qai));
387 return 0;
388 }
389
390 /**
391 * cpacf_km() - executes the KM (CIPHER MESSAGE) instruction
392 * @func: the function code passed to KM; see CPACF_KM_xxx defines
393 * @param: address of parameter block; see POP for details on each func
394 * @dest: address of destination memory area
395 * @src: address of source memory area
396 * @src_len: length of src operand in bytes
397 *
398 * Returns 0 for the query func, number of processed bytes for
399 * encryption/decryption funcs
400 */
cpacf_km(unsigned long func,void * param,u8 * dest,const u8 * src,long src_len)401 static inline int cpacf_km(unsigned long func, void *param,
402 u8 *dest, const u8 *src, long src_len)
403 {
404 union register_pair d, s;
405
406 d.even = (unsigned long)dest;
407 s.even = (unsigned long)src;
408 s.odd = (unsigned long)src_len;
409 asm volatile(
410 " lgr 0,%[fc]\n"
411 " lgr 1,%[pba]\n"
412 "0: .insn rre,%[opc] << 16,%[dst],%[src]\n"
413 " brc 1,0b\n" /* handle partial completion */
414 : [src] "+&d" (s.pair), [dst] "+&d" (d.pair)
415 : [fc] "d" (func), [pba] "d" ((unsigned long)param),
416 [opc] "i" (CPACF_KM)
417 : "cc", "memory", "0", "1");
418
419 return src_len - s.odd;
420 }
421
422 /**
423 * cpacf_kmc() - executes the KMC (CIPHER MESSAGE WITH CHAINING) instruction
424 * @func: the function code passed to KM; see CPACF_KMC_xxx defines
425 * @param: address of parameter block; see POP for details on each func
426 * @dest: address of destination memory area
427 * @src: address of source memory area
428 * @src_len: length of src operand in bytes
429 *
430 * Returns 0 for the query func, number of processed bytes for
431 * encryption/decryption funcs
432 */
cpacf_kmc(unsigned long func,void * param,u8 * dest,const u8 * src,long src_len)433 static inline int cpacf_kmc(unsigned long func, void *param,
434 u8 *dest, const u8 *src, long src_len)
435 {
436 union register_pair d, s;
437
438 d.even = (unsigned long)dest;
439 s.even = (unsigned long)src;
440 s.odd = (unsigned long)src_len;
441 asm volatile(
442 " lgr 0,%[fc]\n"
443 " lgr 1,%[pba]\n"
444 "0: .insn rre,%[opc] << 16,%[dst],%[src]\n"
445 " brc 1,0b\n" /* handle partial completion */
446 : [src] "+&d" (s.pair), [dst] "+&d" (d.pair)
447 : [fc] "d" (func), [pba] "d" ((unsigned long)param),
448 [opc] "i" (CPACF_KMC)
449 : "cc", "memory", "0", "1");
450
451 return src_len - s.odd;
452 }
453
454 /**
455 * cpacf_kimd() - executes the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST)
456 * instruction
457 * @func: the function code passed to KM; see CPACF_KIMD_xxx defines
458 * @param: address of parameter block; see POP for details on each func
459 * @src: address of source memory area
460 * @src_len: length of src operand in bytes
461 */
cpacf_kimd(unsigned long func,void * param,const u8 * src,long src_len)462 static inline void cpacf_kimd(unsigned long func, void *param,
463 const u8 *src, long src_len)
464 {
465 union register_pair s;
466
467 s.even = (unsigned long)src;
468 s.odd = (unsigned long)src_len;
469 asm volatile(
470 " lgr 0,%[fc]\n"
471 " lgr 1,%[pba]\n"
472 "0: .insn rrf,%[opc] << 16,0,%[src],8,0\n"
473 " brc 1,0b\n" /* handle partial completion */
474 : [src] "+&d" (s.pair)
475 : [fc] "d" (func), [pba] "d" ((unsigned long)(param)),
476 [opc] "i" (CPACF_KIMD)
477 : "cc", "memory", "0", "1");
478 }
479
480 /**
481 * cpacf_klmd() - executes the KLMD (COMPUTE LAST MESSAGE DIGEST) instruction
482 * @func: the function code passed to KM; see CPACF_KLMD_xxx defines
483 * @param: address of parameter block; see POP for details on each func
484 * @src: address of source memory area
485 * @src_len: length of src operand in bytes
486 */
cpacf_klmd(unsigned long func,void * param,const u8 * src,long src_len)487 static inline void cpacf_klmd(unsigned long func, void *param,
488 const u8 *src, long src_len)
489 {
490 union register_pair s;
491
492 s.even = (unsigned long)src;
493 s.odd = (unsigned long)src_len;
494 asm volatile(
495 " lgr 0,%[fc]\n"
496 " lgr 1,%[pba]\n"
497 "0: .insn rrf,%[opc] << 16,0,%[src],8,0\n"
498 " brc 1,0b\n" /* handle partial completion */
499 : [src] "+&d" (s.pair)
500 : [fc] "d" (func), [pba] "d" ((unsigned long)param),
501 [opc] "i" (CPACF_KLMD)
502 : "cc", "memory", "0", "1");
503 }
504
505 /**
506 * _cpacf_kmac() - executes the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE)
507 * instruction and updates flags in gr0
508 * @gr0: pointer to gr0 (fc and flags) passed to KMAC; see CPACF_KMAC_xxx defines
509 * @param: address of parameter block; see POP for details on each func
510 * @src: address of source memory area
511 * @src_len: length of src operand in bytes
512 *
513 * Returns 0 for the query func, number of processed bytes for digest funcs
514 */
_cpacf_kmac(unsigned long * gr0,void * param,const u8 * src,long src_len)515 static inline int _cpacf_kmac(unsigned long *gr0, void *param,
516 const u8 *src, long src_len)
517 {
518 union register_pair s;
519
520 s.even = (unsigned long)src;
521 s.odd = (unsigned long)src_len;
522 asm volatile(
523 " lgr 0,%[r0]\n"
524 " lgr 1,%[pba]\n"
525 "0: .insn rre,%[opc] << 16,0,%[src]\n"
526 " brc 1,0b\n" /* handle partial completion */
527 " lgr %[r0],0\n"
528 : [r0] "+d" (*gr0), [src] "+&d" (s.pair)
529 : [pba] "d" ((unsigned long)param),
530 [opc] "i" (CPACF_KMAC)
531 : "cc", "memory", "0", "1");
532
533 return src_len - s.odd;
534 }
535
536 /**
537 * cpacf_kmac() - executes the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE)
538 * instruction
539 * @func: function code passed to KMAC; see CPACF_KMAC_xxx defines
540 * @param: address of parameter block; see POP for details on each func
541 * @src: address of source memory area
542 * @src_len: length of src operand in bytes
543 *
544 * Returns 0 for the query func, number of processed bytes for digest funcs
545 */
cpacf_kmac(unsigned long func,void * param,const u8 * src,long src_len)546 static inline int cpacf_kmac(unsigned long func, void *param,
547 const u8 *src, long src_len)
548 {
549 return _cpacf_kmac(&func, param, src, src_len);
550 }
551
552 /**
553 * cpacf_kmctr() - executes the KMCTR (CIPHER MESSAGE WITH COUNTER) instruction
554 * @func: the function code passed to KMCTR; see CPACF_KMCTR_xxx defines
555 * @param: address of parameter block; see POP for details on each func
556 * @dest: address of destination memory area
557 * @src: address of source memory area
558 * @src_len: length of src operand in bytes
559 * @counter: address of counter value
560 *
561 * Returns 0 for the query func, number of processed bytes for
562 * encryption/decryption funcs
563 */
cpacf_kmctr(unsigned long func,void * param,u8 * dest,const u8 * src,long src_len,u8 * counter)564 static inline int cpacf_kmctr(unsigned long func, void *param, u8 *dest,
565 const u8 *src, long src_len, u8 *counter)
566 {
567 union register_pair d, s, c;
568
569 d.even = (unsigned long)dest;
570 s.even = (unsigned long)src;
571 s.odd = (unsigned long)src_len;
572 c.even = (unsigned long)counter;
573 asm volatile(
574 " lgr 0,%[fc]\n"
575 " lgr 1,%[pba]\n"
576 "0: .insn rrf,%[opc] << 16,%[dst],%[src],%[ctr],0\n"
577 " brc 1,0b\n" /* handle partial completion */
578 : [src] "+&d" (s.pair), [dst] "+&d" (d.pair),
579 [ctr] "+&d" (c.pair)
580 : [fc] "d" (func), [pba] "d" ((unsigned long)param),
581 [opc] "i" (CPACF_KMCTR)
582 : "cc", "memory", "0", "1");
583
584 return src_len - s.odd;
585 }
586
587 /**
588 * cpacf_prno() - executes the PRNO (PERFORM RANDOM NUMBER OPERATION)
589 * instruction
590 * @func: the function code passed to PRNO; see CPACF_PRNO_xxx defines
591 * @param: address of parameter block; see POP for details on each func
592 * @dest: address of destination memory area
593 * @dest_len: size of destination memory area in bytes
594 * @seed: address of seed data
595 * @seed_len: size of seed data in bytes
596 */
cpacf_prno(unsigned long func,void * param,u8 * dest,unsigned long dest_len,const u8 * seed,unsigned long seed_len)597 static inline void cpacf_prno(unsigned long func, void *param,
598 u8 *dest, unsigned long dest_len,
599 const u8 *seed, unsigned long seed_len)
600 {
601 union register_pair d, s;
602
603 d.even = (unsigned long)dest;
604 d.odd = (unsigned long)dest_len;
605 s.even = (unsigned long)seed;
606 s.odd = (unsigned long)seed_len;
607 asm volatile (
608 " lgr 0,%[fc]\n"
609 " lgr 1,%[pba]\n"
610 "0: .insn rre,%[opc] << 16,%[dst],%[seed]\n"
611 " brc 1,0b\n" /* handle partial completion */
612 : [dst] "+&d" (d.pair)
613 : [fc] "d" (func), [pba] "d" ((unsigned long)param),
614 [seed] "d" (s.pair), [opc] "i" (CPACF_PRNO)
615 : "cc", "memory", "0", "1");
616 }
617
618 /**
619 * cpacf_trng() - executes the TRNG subfunction of the PRNO instruction
620 * @ucbuf: buffer for unconditioned data
621 * @ucbuf_len: amount of unconditioned data to fetch in bytes
622 * @cbuf: buffer for conditioned data
623 * @cbuf_len: amount of conditioned data to fetch in bytes
624 */
cpacf_trng(u8 * ucbuf,unsigned long ucbuf_len,u8 * cbuf,unsigned long cbuf_len)625 static inline void cpacf_trng(u8 *ucbuf, unsigned long ucbuf_len,
626 u8 *cbuf, unsigned long cbuf_len)
627 {
628 union register_pair u, c;
629
630 u.even = (unsigned long)ucbuf;
631 u.odd = (unsigned long)ucbuf_len;
632 c.even = (unsigned long)cbuf;
633 c.odd = (unsigned long)cbuf_len;
634 asm volatile (
635 " lghi 0,%[fc]\n"
636 "0: .insn rre,%[opc] << 16,%[ucbuf],%[cbuf]\n"
637 " brc 1,0b\n" /* handle partial completion */
638 : [ucbuf] "+&d" (u.pair), [cbuf] "+&d" (c.pair)
639 : [fc] "K" (CPACF_PRNO_TRNG), [opc] "i" (CPACF_PRNO)
640 : "cc", "memory", "0");
641 kmsan_unpoison_memory(ucbuf, ucbuf_len);
642 kmsan_unpoison_memory(cbuf, cbuf_len);
643 }
644
645 /**
646 * cpacf_pcc() - executes the PCC (PERFORM CRYPTOGRAPHIC COMPUTATION)
647 * instruction
648 * @func: the function code passed to PCC; see CPACF_KM_xxx defines
649 * @param: address of parameter block; see POP for details on each func
650 */
cpacf_pcc(unsigned long func,void * param)651 static inline void cpacf_pcc(unsigned long func, void *param)
652 {
653 asm volatile(
654 " lgr 0,%[fc]\n"
655 " lgr 1,%[pba]\n"
656 "0: .insn rre,%[opc] << 16,0,0\n" /* PCC opcode */
657 " brc 1,0b\n" /* handle partial completion */
658 :
659 : [fc] "d" (func), [pba] "d" ((unsigned long)param),
660 [opc] "i" (CPACF_PCC)
661 : "cc", "memory", "0", "1");
662 }
663
664 /**
665 * cpacf_pckmo() - executes the PCKMO (PERFORM CRYPTOGRAPHIC KEY
666 * MANAGEMENT) instruction
667 * @func: the function code passed to PCKMO; see CPACF_PCKMO_xxx defines
668 * @param: address of parameter block; see POP for details on each func
669 *
670 * Returns 0.
671 */
cpacf_pckmo(long func,void * param)672 static inline void cpacf_pckmo(long func, void *param)
673 {
674 asm volatile(
675 " lgr 0,%[fc]\n"
676 " lgr 1,%[pba]\n"
677 " .insn rre,%[opc] << 16,0,0\n" /* PCKMO opcode */
678 :
679 : [fc] "d" (func), [pba] "d" ((unsigned long)param),
680 [opc] "i" (CPACF_PCKMO)
681 : "cc", "memory", "0", "1");
682 }
683
684 /**
685 * cpacf_kma() - executes the KMA (CIPHER MESSAGE WITH AUTHENTICATION)
686 * instruction
687 * @func: the function code passed to KMA; see CPACF_KMA_xxx defines
688 * @param: address of parameter block; see POP for details on each func
689 * @dest: address of destination memory area
690 * @src: address of source memory area
691 * @src_len: length of src operand in bytes
692 * @aad: address of additional authenticated data memory area
693 * @aad_len: length of aad operand in bytes
694 */
cpacf_kma(unsigned long func,void * param,u8 * dest,const u8 * src,unsigned long src_len,const u8 * aad,unsigned long aad_len)695 static inline void cpacf_kma(unsigned long func, void *param, u8 *dest,
696 const u8 *src, unsigned long src_len,
697 const u8 *aad, unsigned long aad_len)
698 {
699 union register_pair d, s, a;
700
701 d.even = (unsigned long)dest;
702 s.even = (unsigned long)src;
703 s.odd = (unsigned long)src_len;
704 a.even = (unsigned long)aad;
705 a.odd = (unsigned long)aad_len;
706 asm volatile(
707 " lgr 0,%[fc]\n"
708 " lgr 1,%[pba]\n"
709 "0: .insn rrf,%[opc] << 16,%[dst],%[src],%[aad],0\n"
710 " brc 1,0b\n" /* handle partial completion */
711 : [dst] "+&d" (d.pair), [src] "+&d" (s.pair),
712 [aad] "+&d" (a.pair)
713 : [fc] "d" (func), [pba] "d" ((unsigned long)param),
714 [opc] "i" (CPACF_KMA)
715 : "cc", "memory", "0", "1");
716 }
717
718 #endif /* _ASM_S390_CPACF_H */
719