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