1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * CP Assist for Cryptographic Functions (CPACF) 4 * 5 * Copyright IBM Corp. 2003, 2016 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 _ASMS390X_CPACF_H_ 12 #define _ASMS390X_CPACF_H_ 13 14 #include <asm/facility.h> 15 #include <linux/compiler.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 33 /* 34 * En/decryption modifier bits 35 */ 36 #define CPACF_ENCRYPT 0x00 37 #define CPACF_DECRYPT 0x80 38 39 /* 40 * Function codes for the KM (CIPHER MESSAGE) instruction 41 */ 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 56 57 /* 58 * Function codes for the KMC (CIPHER MESSAGE WITH CHAINING) 59 * instruction 60 */ 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 72 73 /* 74 * Function codes for the KMCTR (CIPHER MESSAGE WITH COUNTER) 75 * instruction 76 */ 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 87 88 /* 89 * Function codes for the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST) 90 * instruction 91 */ 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 97 98 /* 99 * Function codes for the KLMD (COMPUTE LAST MESSAGE DIGEST) 100 * instruction 101 */ 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 106 107 /* 108 * function codes for the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE) 109 * instruction 110 */ 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 115 116 /* 117 * Function codes for the PCKMO (PERFORM CRYPTOGRAPHIC KEY MANAGEMENT) 118 * instruction 119 */ 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 127 128 /* 129 * Function codes for the PRNO (PERFORM RANDOM NUMBER OPERATION) 130 * instruction 131 */ 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 137 138 typedef struct { unsigned char bytes[16]; } cpacf_mask_t; 139 140 static __always_inline void __cpacf_query_rre(u32 opc, u8 r1, u8 r2, 141 cpacf_mask_t *mask) 142 { 143 asm volatile( 144 " la %%r1,%[mask]\n" 145 " xgr %%r0,%%r0\n" 146 " .insn rre,%[opc] << 16,%[r1],%[r2]\n" 147 : [mask] "=R" (*mask) 148 : [opc] "i" (opc), 149 [r1] "i" (r1), [r2] "i" (r2) 150 : "cc", "r0", "r1"); 151 } 152 153 static __always_inline void __cpacf_query_rrf(u32 opc, 154 u8 r1, u8 r2, u8 r3, u8 m4, 155 cpacf_mask_t *mask) 156 { 157 asm volatile( 158 " la %%r1,%[mask]\n" 159 " xgr %%r0,%%r0\n" 160 " .insn rrf,%[opc] << 16,%[r1],%[r2],%[r3],%[m4]\n" 161 : [mask] "=R" (*mask) 162 : [opc] "i" (opc), [r1] "i" (r1), [r2] "i" (r2), 163 [r3] "i" (r3), [m4] "i" (m4) 164 : "cc", "r0", "r1"); 165 } 166 167 static __always_inline void __cpacf_query(unsigned int opcode, 168 cpacf_mask_t *mask) 169 { 170 switch (opcode) { 171 case CPACF_KIMD: 172 __cpacf_query_rre(CPACF_KIMD, 0, 2, mask); 173 break; 174 case CPACF_KLMD: 175 __cpacf_query_rre(CPACF_KLMD, 0, 2, mask); 176 break; 177 case CPACF_KM: 178 __cpacf_query_rre(CPACF_KM, 2, 4, mask); 179 break; 180 case CPACF_KMA: 181 __cpacf_query_rrf(CPACF_KMA, 2, 4, 6, 0, mask); 182 break; 183 case CPACF_KMAC: 184 __cpacf_query_rre(CPACF_KMAC, 0, 2, mask); 185 break; 186 case CPACF_KMC: 187 __cpacf_query_rre(CPACF_KMC, 2, 4, mask); 188 break; 189 case CPACF_KMCTR: 190 __cpacf_query_rrf(CPACF_KMCTR, 2, 4, 6, 0, mask); 191 break; 192 case CPACF_KMF: 193 __cpacf_query_rre(CPACF_KMF, 2, 4, mask); 194 break; 195 case CPACF_KMO: 196 __cpacf_query_rre(CPACF_KMO, 2, 4, mask); 197 break; 198 case CPACF_PCC: 199 __cpacf_query_rre(CPACF_PCC, 0, 0, mask); 200 break; 201 case CPACF_PCKMO: 202 __cpacf_query_rre(CPACF_PCKMO, 0, 0, mask); 203 break; 204 case CPACF_PRNO: 205 __cpacf_query_rre(CPACF_PRNO, 2, 4, mask); 206 break; 207 default: 208 asm volatile(".error \"bad opcode\""); 209 } 210 } 211 212 static inline int __cpacf_check_opcode(unsigned int opcode) 213 { 214 switch (opcode) { 215 case CPACF_KMAC: 216 case CPACF_KM: 217 case CPACF_KMC: 218 case CPACF_KIMD: 219 case CPACF_KLMD: 220 return test_facility(17); /* check for MSA */ 221 case CPACF_PCKMO: 222 return test_facility(76); /* check for MSA3 */ 223 case CPACF_KMF: 224 case CPACF_KMO: 225 case CPACF_PCC: 226 case CPACF_KMCTR: 227 return test_facility(77); /* check for MSA4 */ 228 case CPACF_PRNO: 229 return test_facility(57); /* check for MSA5 */ 230 default: 231 return 0; 232 } 233 } 234 235 static __always_inline int cpacf_query(unsigned int opcode, cpacf_mask_t *mask) 236 { 237 if (__cpacf_check_opcode(opcode)) { 238 __cpacf_query(opcode, mask); 239 return 1; 240 } 241 memset(mask, 0, sizeof(*mask)); 242 return 0; 243 } 244 245 static inline int cpacf_test_func(cpacf_mask_t *mask, unsigned int func) 246 { 247 return (mask->bytes[func >> 3] & (0x80 >> (func & 7))) != 0; 248 } 249 250 /** 251 * cpacf_query_func() - check if a specific CPACF function is available 252 * @opcode: the opcode of the crypto instruction 253 * @func: the function code to test for 254 * 255 * Executes the query function for the given crypto instruction @opcode 256 * and checks if @func is available 257 * 258 * Returns 1 if @func is available for @opcode, 0 otherwise 259 */ 260 static inline int cpacf_query_func(unsigned int opcode, unsigned int func) 261 { 262 cpacf_mask_t mask; 263 264 if (cpacf_query(opcode, &mask)) 265 return cpacf_test_func(&mask, func); 266 return 0; 267 } 268 269 /** 270 * cpacf_km() - executes the KM (CIPHER MESSAGE) instruction 271 * @func: the function code passed to KM; see CPACF_KM_xxx defines 272 * @param: address of parameter block; see POP for details on each func 273 * @dest: address of destination memory area 274 * @src: address of source memory area 275 * @src_len: length of src operand in bytes 276 * 277 * Returns 0 for the query func, number of processed bytes for 278 * encryption/decryption funcs 279 */ 280 static inline int cpacf_km(unsigned long func, void *param, 281 u8 *dest, const u8 *src, long src_len) 282 { 283 register unsigned long r0 asm("0") = (unsigned long) func; 284 register unsigned long r1 asm("1") = (unsigned long) param; 285 register unsigned long r2 asm("2") = (unsigned long) src; 286 register unsigned long r3 asm("3") = (unsigned long) src_len; 287 register unsigned long r4 asm("4") = (unsigned long) dest; 288 289 asm volatile( 290 "0: .insn rre,%[opc] << 16,%[dst],%[src]\n" 291 " brc 1,0b\n" /* handle partial completion */ 292 : [src] "+a" (r2), [len] "+d" (r3), [dst] "+a" (r4) 293 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KM) 294 : "cc", "memory"); 295 296 return src_len - r3; 297 } 298 299 /** 300 * cpacf_kmc() - executes the KMC (CIPHER MESSAGE WITH CHAINING) instruction 301 * @func: the function code passed to KM; see CPACF_KMC_xxx defines 302 * @param: address of parameter block; see POP for details on each func 303 * @dest: address of destination memory area 304 * @src: address of source memory area 305 * @src_len: length of src operand in bytes 306 * 307 * Returns 0 for the query func, number of processed bytes for 308 * encryption/decryption funcs 309 */ 310 static inline int cpacf_kmc(unsigned long func, void *param, 311 u8 *dest, const u8 *src, long src_len) 312 { 313 register unsigned long r0 asm("0") = (unsigned long) func; 314 register unsigned long r1 asm("1") = (unsigned long) param; 315 register unsigned long r2 asm("2") = (unsigned long) src; 316 register unsigned long r3 asm("3") = (unsigned long) src_len; 317 register unsigned long r4 asm("4") = (unsigned long) dest; 318 319 asm volatile( 320 "0: .insn rre,%[opc] << 16,%[dst],%[src]\n" 321 " brc 1,0b\n" /* handle partial completion */ 322 : [src] "+a" (r2), [len] "+d" (r3), [dst] "+a" (r4) 323 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KMC) 324 : "cc", "memory"); 325 326 return src_len - r3; 327 } 328 329 /** 330 * cpacf_kimd() - executes the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST) 331 * instruction 332 * @func: the function code passed to KM; see CPACF_KIMD_xxx defines 333 * @param: address of parameter block; see POP for details on each func 334 * @src: address of source memory area 335 * @src_len: length of src operand in bytes 336 */ 337 static inline void cpacf_kimd(unsigned long func, void *param, 338 const u8 *src, long src_len) 339 { 340 register unsigned long r0 asm("0") = (unsigned long) func; 341 register unsigned long r1 asm("1") = (unsigned long) param; 342 register unsigned long r2 asm("2") = (unsigned long) src; 343 register unsigned long r3 asm("3") = (unsigned long) src_len; 344 345 asm volatile( 346 "0: .insn rre,%[opc] << 16,0,%[src]\n" 347 " brc 1,0b\n" /* handle partial completion */ 348 : [src] "+a" (r2), [len] "+d" (r3) 349 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KIMD) 350 : "cc", "memory"); 351 } 352 353 /** 354 * cpacf_klmd() - executes the KLMD (COMPUTE LAST MESSAGE DIGEST) instruction 355 * @func: the function code passed to KM; see CPACF_KLMD_xxx defines 356 * @param: address of parameter block; see POP for details on each func 357 * @src: address of source memory area 358 * @src_len: length of src operand in bytes 359 */ 360 static inline void cpacf_klmd(unsigned long func, void *param, 361 const u8 *src, long src_len) 362 { 363 register unsigned long r0 asm("0") = (unsigned long) func; 364 register unsigned long r1 asm("1") = (unsigned long) param; 365 register unsigned long r2 asm("2") = (unsigned long) src; 366 register unsigned long r3 asm("3") = (unsigned long) src_len; 367 368 asm volatile( 369 "0: .insn rre,%[opc] << 16,0,%[src]\n" 370 " brc 1,0b\n" /* handle partial completion */ 371 : [src] "+a" (r2), [len] "+d" (r3) 372 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KLMD) 373 : "cc", "memory"); 374 } 375 376 /** 377 * cpacf_kmac() - executes the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE) 378 * instruction 379 * @func: the function code passed to KM; see CPACF_KMAC_xxx defines 380 * @param: address of parameter block; see POP for details on each func 381 * @src: address of source memory area 382 * @src_len: length of src operand in bytes 383 * 384 * Returns 0 for the query func, number of processed bytes for digest funcs 385 */ 386 static inline int cpacf_kmac(unsigned long func, void *param, 387 const u8 *src, long src_len) 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) src; 392 register unsigned long r3 asm("3") = (unsigned long) src_len; 393 394 asm volatile( 395 "0: .insn rre,%[opc] << 16,0,%[src]\n" 396 " brc 1,0b\n" /* handle partial completion */ 397 : [src] "+a" (r2), [len] "+d" (r3) 398 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KMAC) 399 : "cc", "memory"); 400 401 return src_len - r3; 402 } 403 404 /** 405 * cpacf_kmctr() - executes the KMCTR (CIPHER MESSAGE WITH COUNTER) instruction 406 * @func: the function code passed to KMCTR; see CPACF_KMCTR_xxx defines 407 * @param: address of parameter block; see POP for details on each func 408 * @dest: address of destination memory area 409 * @src: address of source memory area 410 * @src_len: length of src operand in bytes 411 * @counter: address of counter value 412 * 413 * Returns 0 for the query func, number of processed bytes for 414 * encryption/decryption funcs 415 */ 416 static inline int cpacf_kmctr(unsigned long func, void *param, u8 *dest, 417 const u8 *src, long src_len, u8 *counter) 418 { 419 register unsigned long r0 asm("0") = (unsigned long) func; 420 register unsigned long r1 asm("1") = (unsigned long) param; 421 register unsigned long r2 asm("2") = (unsigned long) src; 422 register unsigned long r3 asm("3") = (unsigned long) src_len; 423 register unsigned long r4 asm("4") = (unsigned long) dest; 424 register unsigned long r6 asm("6") = (unsigned long) counter; 425 426 asm volatile( 427 "0: .insn rrf,%[opc] << 16,%[dst],%[src],%[ctr],0\n" 428 " brc 1,0b\n" /* handle partial completion */ 429 : [src] "+a" (r2), [len] "+d" (r3), 430 [dst] "+a" (r4), [ctr] "+a" (r6) 431 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KMCTR) 432 : "cc", "memory"); 433 434 return src_len - r3; 435 } 436 437 /** 438 * cpacf_prno() - executes the PRNO (PERFORM RANDOM NUMBER OPERATION) 439 * instruction 440 * @func: the function code passed to PRNO; see CPACF_PRNO_xxx defines 441 * @param: address of parameter block; see POP for details on each func 442 * @dest: address of destination memory area 443 * @dest_len: size of destination memory area in bytes 444 * @seed: address of seed data 445 * @seed_len: size of seed data in bytes 446 */ 447 static inline void cpacf_prno(unsigned long func, void *param, 448 u8 *dest, unsigned long dest_len, 449 const u8 *seed, unsigned long seed_len) 450 { 451 register unsigned long r0 asm("0") = (unsigned long) func; 452 register unsigned long r1 asm("1") = (unsigned long) param; 453 register unsigned long r2 asm("2") = (unsigned long) dest; 454 register unsigned long r3 asm("3") = (unsigned long) dest_len; 455 register unsigned long r4 asm("4") = (unsigned long) seed; 456 register unsigned long r5 asm("5") = (unsigned long) seed_len; 457 458 asm volatile ( 459 "0: .insn rre,%[opc] << 16,%[dst],%[seed]\n" 460 " brc 1,0b\n" /* handle partial completion */ 461 : [dst] "+a" (r2), [dlen] "+d" (r3) 462 : [fc] "d" (r0), [pba] "a" (r1), 463 [seed] "a" (r4), [slen] "d" (r5), [opc] "i" (CPACF_PRNO) 464 : "cc", "memory"); 465 } 466 467 /** 468 * cpacf_trng() - executes the TRNG subfunction of the PRNO instruction 469 * @ucbuf: buffer for unconditioned data 470 * @ucbuf_len: amount of unconditioned data to fetch in bytes 471 * @cbuf: buffer for conditioned data 472 * @cbuf_len: amount of conditioned data to fetch in bytes 473 */ 474 static inline void cpacf_trng(u8 *ucbuf, unsigned long ucbuf_len, 475 u8 *cbuf, unsigned long cbuf_len) 476 { 477 register unsigned long r0 asm("0") = (unsigned long) CPACF_PRNO_TRNG; 478 register unsigned long r2 asm("2") = (unsigned long) ucbuf; 479 register unsigned long r3 asm("3") = (unsigned long) ucbuf_len; 480 register unsigned long r4 asm("4") = (unsigned long) cbuf; 481 register unsigned long r5 asm("5") = (unsigned long) cbuf_len; 482 483 asm volatile ( 484 "0: .insn rre,%[opc] << 16,%[ucbuf],%[cbuf]\n" 485 " brc 1,0b\n" /* handle partial completion */ 486 : [ucbuf] "+a" (r2), [ucbuflen] "+d" (r3), 487 [cbuf] "+a" (r4), [cbuflen] "+d" (r5) 488 : [fc] "d" (r0), [opc] "i" (CPACF_PRNO) 489 : "cc", "memory"); 490 } 491 492 /** 493 * cpacf_pcc() - executes the PCC (PERFORM CRYPTOGRAPHIC COMPUTATION) 494 * instruction 495 * @func: the function code passed to PCC; see CPACF_KM_xxx defines 496 * @param: address of parameter block; see POP for details on each func 497 */ 498 static inline void cpacf_pcc(unsigned long func, void *param) 499 { 500 register unsigned long r0 asm("0") = (unsigned long) func; 501 register unsigned long r1 asm("1") = (unsigned long) param; 502 503 asm volatile( 504 "0: .insn rre,%[opc] << 16,0,0\n" /* PCC opcode */ 505 " brc 1,0b\n" /* handle partial completion */ 506 : 507 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_PCC) 508 : "cc", "memory"); 509 } 510 511 /** 512 * cpacf_pckmo() - executes the PCKMO (PERFORM CRYPTOGRAPHIC KEY 513 * MANAGEMENT) instruction 514 * @func: the function code passed to PCKMO; see CPACF_PCKMO_xxx defines 515 * @param: address of parameter block; see POP for details on each func 516 * 517 * Returns 0. 518 */ 519 static inline void cpacf_pckmo(long func, void *param) 520 { 521 register unsigned long r0 asm("0") = (unsigned long) func; 522 register unsigned long r1 asm("1") = (unsigned long) param; 523 524 asm volatile( 525 " .insn rre,%[opc] << 16,0,0\n" /* PCKMO opcode */ 526 : 527 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_PCKMO) 528 : "cc", "memory"); 529 } 530 531 #endif /* _ASMS390X_CPACF_H_ */ 532