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(unsigned int opcode, cpacf_mask_t *mask) 141 { 142 register unsigned long r0 asm("0") = 0; /* query function */ 143 register unsigned long r1 asm("1") = (unsigned long) mask; 144 145 asm volatile( 146 " spm 0\n" /* pckmo doesn't change the cc */ 147 /* Parameter regs are ignored, but must be nonzero and unique */ 148 "0: .insn rrf,%[opc] << 16,2,4,6,0\n" 149 " brc 1,0b\n" /* handle partial completion */ 150 : "=m" (*mask) 151 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (opcode) 152 : "cc"); 153 } 154 155 static inline int __cpacf_check_opcode(unsigned int opcode) 156 { 157 switch (opcode) { 158 case CPACF_KMAC: 159 case CPACF_KM: 160 case CPACF_KMC: 161 case CPACF_KIMD: 162 case CPACF_KLMD: 163 return test_facility(17); /* check for MSA */ 164 case CPACF_PCKMO: 165 return test_facility(76); /* check for MSA3 */ 166 case CPACF_KMF: 167 case CPACF_KMO: 168 case CPACF_PCC: 169 case CPACF_KMCTR: 170 return test_facility(77); /* check for MSA4 */ 171 case CPACF_PRNO: 172 return test_facility(57); /* check for MSA5 */ 173 default: 174 return 0; 175 } 176 } 177 178 static __always_inline int cpacf_query(unsigned int opcode, cpacf_mask_t *mask) 179 { 180 if (__cpacf_check_opcode(opcode)) { 181 __cpacf_query(opcode, mask); 182 return 1; 183 } 184 memset(mask, 0, sizeof(*mask)); 185 return 0; 186 } 187 188 static inline int cpacf_test_func(cpacf_mask_t *mask, unsigned int func) 189 { 190 return (mask->bytes[func >> 3] & (0x80 >> (func & 7))) != 0; 191 } 192 193 /** 194 * cpacf_query_func() - check if a specific CPACF function is available 195 * @opcode: the opcode of the crypto instruction 196 * @func: the function code to test for 197 * 198 * Executes the query function for the given crypto instruction @opcode 199 * and checks if @func is available 200 * 201 * Returns 1 if @func is available for @opcode, 0 otherwise 202 */ 203 static inline int cpacf_query_func(unsigned int opcode, unsigned int func) 204 { 205 cpacf_mask_t mask; 206 207 if (cpacf_query(opcode, &mask)) 208 return cpacf_test_func(&mask, func); 209 return 0; 210 } 211 212 /** 213 * cpacf_km() - executes the KM (CIPHER MESSAGE) instruction 214 * @func: the function code passed to KM; see CPACF_KM_xxx defines 215 * @param: address of parameter block; see POP for details on each func 216 * @dest: address of destination memory area 217 * @src: address of source memory area 218 * @src_len: length of src operand in bytes 219 * 220 * Returns 0 for the query func, number of processed bytes for 221 * encryption/decryption funcs 222 */ 223 static inline int cpacf_km(unsigned long func, void *param, 224 u8 *dest, const u8 *src, long src_len) 225 { 226 register unsigned long r0 asm("0") = (unsigned long) func; 227 register unsigned long r1 asm("1") = (unsigned long) param; 228 register unsigned long r2 asm("2") = (unsigned long) src; 229 register unsigned long r3 asm("3") = (unsigned long) src_len; 230 register unsigned long r4 asm("4") = (unsigned long) dest; 231 232 asm volatile( 233 "0: .insn rre,%[opc] << 16,%[dst],%[src]\n" 234 " brc 1,0b\n" /* handle partial completion */ 235 : [src] "+a" (r2), [len] "+d" (r3), [dst] "+a" (r4) 236 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KM) 237 : "cc", "memory"); 238 239 return src_len - r3; 240 } 241 242 /** 243 * cpacf_kmc() - executes the KMC (CIPHER MESSAGE WITH CHAINING) instruction 244 * @func: the function code passed to KM; see CPACF_KMC_xxx defines 245 * @param: address of parameter block; see POP for details on each func 246 * @dest: address of destination memory area 247 * @src: address of source memory area 248 * @src_len: length of src operand in bytes 249 * 250 * Returns 0 for the query func, number of processed bytes for 251 * encryption/decryption funcs 252 */ 253 static inline int cpacf_kmc(unsigned long func, void *param, 254 u8 *dest, const u8 *src, long src_len) 255 { 256 register unsigned long r0 asm("0") = (unsigned long) func; 257 register unsigned long r1 asm("1") = (unsigned long) param; 258 register unsigned long r2 asm("2") = (unsigned long) src; 259 register unsigned long r3 asm("3") = (unsigned long) src_len; 260 register unsigned long r4 asm("4") = (unsigned long) dest; 261 262 asm volatile( 263 "0: .insn rre,%[opc] << 16,%[dst],%[src]\n" 264 " brc 1,0b\n" /* handle partial completion */ 265 : [src] "+a" (r2), [len] "+d" (r3), [dst] "+a" (r4) 266 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KMC) 267 : "cc", "memory"); 268 269 return src_len - r3; 270 } 271 272 /** 273 * cpacf_kimd() - executes the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST) 274 * instruction 275 * @func: the function code passed to KM; see CPACF_KIMD_xxx defines 276 * @param: address of parameter block; see POP for details on each func 277 * @src: address of source memory area 278 * @src_len: length of src operand in bytes 279 */ 280 static inline void cpacf_kimd(unsigned long func, void *param, 281 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 288 asm volatile( 289 "0: .insn rre,%[opc] << 16,0,%[src]\n" 290 " brc 1,0b\n" /* handle partial completion */ 291 : [src] "+a" (r2), [len] "+d" (r3) 292 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KIMD) 293 : "cc", "memory"); 294 } 295 296 /** 297 * cpacf_klmd() - executes the KLMD (COMPUTE LAST MESSAGE DIGEST) instruction 298 * @func: the function code passed to KM; see CPACF_KLMD_xxx defines 299 * @param: address of parameter block; see POP for details on each func 300 * @src: address of source memory area 301 * @src_len: length of src operand in bytes 302 */ 303 static inline void cpacf_klmd(unsigned long func, void *param, 304 const u8 *src, long src_len) 305 { 306 register unsigned long r0 asm("0") = (unsigned long) func; 307 register unsigned long r1 asm("1") = (unsigned long) param; 308 register unsigned long r2 asm("2") = (unsigned long) src; 309 register unsigned long r3 asm("3") = (unsigned long) src_len; 310 311 asm volatile( 312 "0: .insn rre,%[opc] << 16,0,%[src]\n" 313 " brc 1,0b\n" /* handle partial completion */ 314 : [src] "+a" (r2), [len] "+d" (r3) 315 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KLMD) 316 : "cc", "memory"); 317 } 318 319 /** 320 * cpacf_kmac() - executes the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE) 321 * instruction 322 * @func: the function code passed to KM; see CPACF_KMAC_xxx defines 323 * @param: address of parameter block; see POP for details on each func 324 * @src: address of source memory area 325 * @src_len: length of src operand in bytes 326 * 327 * Returns 0 for the query func, number of processed bytes for digest funcs 328 */ 329 static inline int cpacf_kmac(unsigned long func, void *param, 330 const u8 *src, long src_len) 331 { 332 register unsigned long r0 asm("0") = (unsigned long) func; 333 register unsigned long r1 asm("1") = (unsigned long) param; 334 register unsigned long r2 asm("2") = (unsigned long) src; 335 register unsigned long r3 asm("3") = (unsigned long) src_len; 336 337 asm volatile( 338 "0: .insn rre,%[opc] << 16,0,%[src]\n" 339 " brc 1,0b\n" /* handle partial completion */ 340 : [src] "+a" (r2), [len] "+d" (r3) 341 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KMAC) 342 : "cc", "memory"); 343 344 return src_len - r3; 345 } 346 347 /** 348 * cpacf_kmctr() - executes the KMCTR (CIPHER MESSAGE WITH COUNTER) instruction 349 * @func: the function code passed to KMCTR; see CPACF_KMCTR_xxx defines 350 * @param: address of parameter block; see POP for details on each func 351 * @dest: address of destination memory area 352 * @src: address of source memory area 353 * @src_len: length of src operand in bytes 354 * @counter: address of counter value 355 * 356 * Returns 0 for the query func, number of processed bytes for 357 * encryption/decryption funcs 358 */ 359 static inline int cpacf_kmctr(unsigned long func, void *param, u8 *dest, 360 const u8 *src, long src_len, u8 *counter) 361 { 362 register unsigned long r0 asm("0") = (unsigned long) func; 363 register unsigned long r1 asm("1") = (unsigned long) param; 364 register unsigned long r2 asm("2") = (unsigned long) src; 365 register unsigned long r3 asm("3") = (unsigned long) src_len; 366 register unsigned long r4 asm("4") = (unsigned long) dest; 367 register unsigned long r6 asm("6") = (unsigned long) counter; 368 369 asm volatile( 370 "0: .insn rrf,%[opc] << 16,%[dst],%[src],%[ctr],0\n" 371 " brc 1,0b\n" /* handle partial completion */ 372 : [src] "+a" (r2), [len] "+d" (r3), 373 [dst] "+a" (r4), [ctr] "+a" (r6) 374 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KMCTR) 375 : "cc", "memory"); 376 377 return src_len - r3; 378 } 379 380 /** 381 * cpacf_prno() - executes the PRNO (PERFORM RANDOM NUMBER OPERATION) 382 * instruction 383 * @func: the function code passed to PRNO; see CPACF_PRNO_xxx defines 384 * @param: address of parameter block; see POP for details on each func 385 * @dest: address of destination memory area 386 * @dest_len: size of destination memory area in bytes 387 * @seed: address of seed data 388 * @seed_len: size of seed data in bytes 389 */ 390 static inline void cpacf_prno(unsigned long func, void *param, 391 u8 *dest, unsigned long dest_len, 392 const u8 *seed, unsigned long seed_len) 393 { 394 register unsigned long r0 asm("0") = (unsigned long) func; 395 register unsigned long r1 asm("1") = (unsigned long) param; 396 register unsigned long r2 asm("2") = (unsigned long) dest; 397 register unsigned long r3 asm("3") = (unsigned long) dest_len; 398 register unsigned long r4 asm("4") = (unsigned long) seed; 399 register unsigned long r5 asm("5") = (unsigned long) seed_len; 400 401 asm volatile ( 402 "0: .insn rre,%[opc] << 16,%[dst],%[seed]\n" 403 " brc 1,0b\n" /* handle partial completion */ 404 : [dst] "+a" (r2), [dlen] "+d" (r3) 405 : [fc] "d" (r0), [pba] "a" (r1), 406 [seed] "a" (r4), [slen] "d" (r5), [opc] "i" (CPACF_PRNO) 407 : "cc", "memory"); 408 } 409 410 /** 411 * cpacf_trng() - executes the TRNG subfunction of the PRNO instruction 412 * @ucbuf: buffer for unconditioned data 413 * @ucbuf_len: amount of unconditioned data to fetch in bytes 414 * @cbuf: buffer for conditioned data 415 * @cbuf_len: amount of conditioned data to fetch in bytes 416 */ 417 static inline void cpacf_trng(u8 *ucbuf, unsigned long ucbuf_len, 418 u8 *cbuf, unsigned long cbuf_len) 419 { 420 register unsigned long r0 asm("0") = (unsigned long) CPACF_PRNO_TRNG; 421 register unsigned long r2 asm("2") = (unsigned long) ucbuf; 422 register unsigned long r3 asm("3") = (unsigned long) ucbuf_len; 423 register unsigned long r4 asm("4") = (unsigned long) cbuf; 424 register unsigned long r5 asm("5") = (unsigned long) cbuf_len; 425 426 asm volatile ( 427 "0: .insn rre,%[opc] << 16,%[ucbuf],%[cbuf]\n" 428 " brc 1,0b\n" /* handle partial completion */ 429 : [ucbuf] "+a" (r2), [ucbuflen] "+d" (r3), 430 [cbuf] "+a" (r4), [cbuflen] "+d" (r5) 431 : [fc] "d" (r0), [opc] "i" (CPACF_PRNO) 432 : "cc", "memory"); 433 } 434 435 /** 436 * cpacf_pcc() - executes the PCC (PERFORM CRYPTOGRAPHIC COMPUTATION) 437 * instruction 438 * @func: the function code passed to PCC; see CPACF_KM_xxx defines 439 * @param: address of parameter block; see POP for details on each func 440 */ 441 static inline void cpacf_pcc(unsigned long func, void *param) 442 { 443 register unsigned long r0 asm("0") = (unsigned long) func; 444 register unsigned long r1 asm("1") = (unsigned long) param; 445 446 asm volatile( 447 "0: .insn rre,%[opc] << 16,0,0\n" /* PCC opcode */ 448 " brc 1,0b\n" /* handle partial completion */ 449 : 450 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_PCC) 451 : "cc", "memory"); 452 } 453 454 /** 455 * cpacf_pckmo() - executes the PCKMO (PERFORM CRYPTOGRAPHIC KEY 456 * MANAGEMENT) instruction 457 * @func: the function code passed to PCKMO; see CPACF_PCKMO_xxx defines 458 * @param: address of parameter block; see POP for details on each func 459 * 460 * Returns 0. 461 */ 462 static inline void cpacf_pckmo(long func, void *param) 463 { 464 register unsigned long r0 asm("0") = (unsigned long) func; 465 register unsigned long r1 asm("1") = (unsigned long) param; 466 467 asm volatile( 468 " .insn rre,%[opc] << 16,0,0\n" /* PCKMO opcode */ 469 : 470 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_PCKMO) 471 : "cc", "memory"); 472 } 473 474 #endif /* _ASMS390X_CPACF_H_ */ 475