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 */ 23 #define CPACF_PCKMO 0xb928 /* MSA3 */ 24 #define CPACF_KMF 0xb92a /* MSA4 */ 25 #define CPACF_KMO 0xb92b /* MSA4 */ 26 #define CPACF_PCC 0xb92c /* MSA4 */ 27 #define CPACF_KMCTR 0xb92d /* MSA4 */ 28 #define CPACF_PPNO 0xb93c /* MSA5 */ 29 30 /* 31 * Decryption modifier bit 32 */ 33 #define CPACF_DECRYPT 0x80 34 35 /* 36 * Function codes for the KM (CIPHER MESSAGE) instruction 37 */ 38 #define CPACF_KM_QUERY 0x00 39 #define CPACF_KM_DEA 0x01 40 #define CPACF_KM_TDEA_128 0x02 41 #define CPACF_KM_TDEA_192 0x03 42 #define CPACF_KM_AES_128 0x12 43 #define CPACF_KM_AES_192 0x13 44 #define CPACF_KM_AES_256 0x14 45 #define CPACF_KM_XTS_128 0x32 46 #define CPACF_KM_XTS_256 0x34 47 48 /* 49 * Function codes for the KMC (CIPHER MESSAGE WITH CHAINING) 50 * instruction 51 */ 52 #define CPACF_KMC_QUERY 0x00 53 #define CPACF_KMC_DEA 0x01 54 #define CPACF_KMC_TDEA_128 0x02 55 #define CPACF_KMC_TDEA_192 0x03 56 #define CPACF_KMC_AES_128 0x12 57 #define CPACF_KMC_AES_192 0x13 58 #define CPACF_KMC_AES_256 0x14 59 #define CPACF_KMC_PRNG 0x43 60 61 /* 62 * Function codes for the KMCTR (CIPHER MESSAGE WITH COUNTER) 63 * instruction 64 */ 65 #define CPACF_KMCTR_QUERY 0x00 66 #define CPACF_KMCTR_DEA 0x01 67 #define CPACF_KMCTR_TDEA_128 0x02 68 #define CPACF_KMCTR_TDEA_192 0x03 69 #define CPACF_KMCTR_AES_128 0x12 70 #define CPACF_KMCTR_AES_192 0x13 71 #define CPACF_KMCTR_AES_256 0x14 72 73 /* 74 * Function codes for the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST) 75 * instruction 76 */ 77 #define CPACF_KIMD_QUERY 0x00 78 #define CPACF_KIMD_SHA_1 0x01 79 #define CPACF_KIMD_SHA_256 0x02 80 #define CPACF_KIMD_SHA_512 0x03 81 #define CPACF_KIMD_GHASH 0x41 82 83 /* 84 * Function codes for the KLMD (COMPUTE LAST MESSAGE DIGEST) 85 * instruction 86 */ 87 #define CPACF_KLMD_QUERY 0x00 88 #define CPACF_KLMD_SHA_1 0x01 89 #define CPACF_KLMD_SHA_256 0x02 90 #define CPACF_KLMD_SHA_512 0x03 91 92 /* 93 * function codes for the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE) 94 * instruction 95 */ 96 #define CPACF_KMAC_QUERY 0x00 97 #define CPACF_KMAC_DEA 0x01 98 #define CPACF_KMAC_TDEA_128 0x02 99 #define CPACF_KMAC_TDEA_192 0x03 100 101 /* 102 * Function codes for the PPNO (PERFORM PSEUDORANDOM NUMBER OPERATION) 103 * instruction 104 */ 105 #define CPACF_PPNO_QUERY 0x00 106 #define CPACF_PPNO_SHA512_DRNG_GEN 0x03 107 #define CPACF_PPNO_SHA512_DRNG_SEED 0x83 108 109 /** 110 * cpacf_query() - check if a specific CPACF function is available 111 * @opcode: the opcode of the crypto instruction 112 * @func: the function code to test for 113 * 114 * Executes the query function for the given crypto instruction @opcode 115 * and checks if @func is available 116 * 117 * Returns 1 if @func is available for @opcode, 0 otherwise 118 */ 119 static inline void __cpacf_query(unsigned int opcode, unsigned char *status) 120 { 121 typedef struct { unsigned char _[16]; } status_type; 122 register unsigned long r0 asm("0") = 0; /* query function */ 123 register unsigned long r1 asm("1") = (unsigned long) status; 124 125 asm volatile( 126 " spm 0\n" /* pckmo doesn't change the cc */ 127 /* Parameter registers are ignored, but may not be 0 */ 128 "0: .insn rrf,%[opc] << 16,2,2,2,0\n" 129 " brc 1,0b\n" /* handle partial completion */ 130 : "=m" (*(status_type *) status) 131 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (opcode) 132 : "cc"); 133 } 134 135 static inline int cpacf_query(unsigned int opcode, unsigned int func) 136 { 137 unsigned char status[16]; 138 139 switch (opcode) { 140 case CPACF_KMAC: 141 case CPACF_KM: 142 case CPACF_KMC: 143 case CPACF_KIMD: 144 case CPACF_KLMD: 145 if (!test_facility(17)) /* check for MSA */ 146 return 0; 147 break; 148 case CPACF_PCKMO: 149 if (!test_facility(76)) /* check for MSA3 */ 150 return 0; 151 break; 152 case CPACF_KMF: 153 case CPACF_KMO: 154 case CPACF_PCC: 155 case CPACF_KMCTR: 156 if (!test_facility(77)) /* check for MSA4 */ 157 return 0; 158 break; 159 case CPACF_PPNO: 160 if (!test_facility(57)) /* check for MSA5 */ 161 return 0; 162 break; 163 default: 164 BUG(); 165 } 166 __cpacf_query(opcode, status); 167 return (status[func >> 3] & (0x80 >> (func & 7))) != 0; 168 } 169 170 /** 171 * cpacf_km() - executes the KM (CIPHER MESSAGE) instruction 172 * @func: the function code passed to KM; see CPACF_KM_xxx defines 173 * @param: address of parameter block; see POP for details on each func 174 * @dest: address of destination memory area 175 * @src: address of source memory area 176 * @src_len: length of src operand in bytes 177 * 178 * Returns 0 for the query func, number of processed bytes for 179 * encryption/decryption funcs 180 */ 181 static inline int cpacf_km(unsigned long func, void *param, 182 u8 *dest, const u8 *src, long src_len) 183 { 184 register unsigned long r0 asm("0") = (unsigned long) func; 185 register unsigned long r1 asm("1") = (unsigned long) param; 186 register unsigned long r2 asm("2") = (unsigned long) src; 187 register unsigned long r3 asm("3") = (unsigned long) src_len; 188 register unsigned long r4 asm("4") = (unsigned long) dest; 189 190 asm volatile( 191 "0: .insn rre,%[opc] << 16,%[dst],%[src]\n" 192 " brc 1,0b\n" /* handle partial completion */ 193 : [src] "+a" (r2), [len] "+d" (r3), [dst] "+a" (r4) 194 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KM) 195 : "cc", "memory"); 196 197 return src_len - r3; 198 } 199 200 /** 201 * cpacf_kmc() - executes the KMC (CIPHER MESSAGE WITH CHAINING) instruction 202 * @func: the function code passed to KM; see CPACF_KMC_xxx defines 203 * @param: address of parameter block; see POP for details on each func 204 * @dest: address of destination memory area 205 * @src: address of source memory area 206 * @src_len: length of src operand in bytes 207 * 208 * Returns 0 for the query func, number of processed bytes for 209 * encryption/decryption funcs 210 */ 211 static inline int cpacf_kmc(unsigned long func, void *param, 212 u8 *dest, const u8 *src, long src_len) 213 { 214 register unsigned long r0 asm("0") = (unsigned long) func; 215 register unsigned long r1 asm("1") = (unsigned long) param; 216 register unsigned long r2 asm("2") = (unsigned long) src; 217 register unsigned long r3 asm("3") = (unsigned long) src_len; 218 register unsigned long r4 asm("4") = (unsigned long) dest; 219 220 asm volatile( 221 "0: .insn rre,%[opc] << 16,%[dst],%[src]\n" 222 " brc 1,0b\n" /* handle partial completion */ 223 : [src] "+a" (r2), [len] "+d" (r3), [dst] "+a" (r4) 224 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KMC) 225 : "cc", "memory"); 226 227 return src_len - r3; 228 } 229 230 /** 231 * cpacf_kimd() - executes the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST) 232 * instruction 233 * @func: the function code passed to KM; see CPACF_KIMD_xxx defines 234 * @param: address of parameter block; see POP for details on each func 235 * @src: address of source memory area 236 * @src_len: length of src operand in bytes 237 */ 238 static inline void cpacf_kimd(unsigned long func, void *param, 239 const u8 *src, long src_len) 240 { 241 register unsigned long r0 asm("0") = (unsigned long) func; 242 register unsigned long r1 asm("1") = (unsigned long) param; 243 register unsigned long r2 asm("2") = (unsigned long) src; 244 register unsigned long r3 asm("3") = (unsigned long) src_len; 245 246 asm volatile( 247 "0: .insn rre,%[opc] << 16,0,%[src]\n" 248 " brc 1,0b\n" /* handle partial completion */ 249 : [src] "+a" (r2), [len] "+d" (r3) 250 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KIMD) 251 : "cc", "memory"); 252 } 253 254 /** 255 * cpacf_klmd() - executes the KLMD (COMPUTE LAST MESSAGE DIGEST) instruction 256 * @func: the function code passed to KM; see CPACF_KLMD_xxx defines 257 * @param: address of parameter block; see POP for details on each func 258 * @src: address of source memory area 259 * @src_len: length of src operand in bytes 260 */ 261 static inline void cpacf_klmd(unsigned long func, void *param, 262 const u8 *src, long src_len) 263 { 264 register unsigned long r0 asm("0") = (unsigned long) func; 265 register unsigned long r1 asm("1") = (unsigned long) param; 266 register unsigned long r2 asm("2") = (unsigned long) src; 267 register unsigned long r3 asm("3") = (unsigned long) src_len; 268 269 asm volatile( 270 "0: .insn rre,%[opc] << 16,0,%[src]\n" 271 " brc 1,0b\n" /* handle partial completion */ 272 : [src] "+a" (r2), [len] "+d" (r3) 273 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KLMD) 274 : "cc", "memory"); 275 } 276 277 /** 278 * cpacf_kmac() - executes the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE) 279 * instruction 280 * @func: the function code passed to KM; see CPACF_KMAC_xxx defines 281 * @param: address of parameter block; see POP for details on each func 282 * @src: address of source memory area 283 * @src_len: length of src operand in bytes 284 * 285 * Returns 0 for the query func, number of processed bytes for digest funcs 286 */ 287 static inline int cpacf_kmac(unsigned long func, void *param, 288 const u8 *src, long src_len) 289 { 290 register unsigned long r0 asm("0") = (unsigned long) func; 291 register unsigned long r1 asm("1") = (unsigned long) param; 292 register unsigned long r2 asm("2") = (unsigned long) src; 293 register unsigned long r3 asm("3") = (unsigned long) src_len; 294 295 asm volatile( 296 "0: .insn rre,%[opc] << 16,0,%[src]\n" 297 " brc 1,0b\n" /* handle partial completion */ 298 : [src] "+a" (r2), [len] "+d" (r3) 299 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KMAC) 300 : "cc", "memory"); 301 302 return src_len - r3; 303 } 304 305 /** 306 * cpacf_kmctr() - executes the KMCTR (CIPHER MESSAGE WITH COUNTER) instruction 307 * @func: the function code passed to KMCTR; see CPACF_KMCTR_xxx defines 308 * @param: address of parameter block; see POP for details on each func 309 * @dest: address of destination memory area 310 * @src: address of source memory area 311 * @src_len: length of src operand in bytes 312 * @counter: address of counter value 313 * 314 * Returns 0 for the query func, number of processed bytes for 315 * encryption/decryption funcs 316 */ 317 static inline int cpacf_kmctr(unsigned long func, void *param, u8 *dest, 318 const u8 *src, long src_len, u8 *counter) 319 { 320 register unsigned long r0 asm("0") = (unsigned long) func; 321 register unsigned long r1 asm("1") = (unsigned long) param; 322 register unsigned long r2 asm("2") = (unsigned long) src; 323 register unsigned long r3 asm("3") = (unsigned long) src_len; 324 register unsigned long r4 asm("4") = (unsigned long) dest; 325 register unsigned long r6 asm("6") = (unsigned long) counter; 326 327 asm volatile( 328 "0: .insn rrf,%[opc] << 16,%[dst],%[src],%[ctr],0\n" 329 " brc 1,0b\n" /* handle partial completion */ 330 : [src] "+a" (r2), [len] "+d" (r3), 331 [dst] "+a" (r4), [ctr] "+a" (r6) 332 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KMCTR) 333 : "cc", "memory"); 334 335 return src_len - r3; 336 } 337 338 /** 339 * cpacf_ppno() - executes the PPNO (PERFORM PSEUDORANDOM NUMBER OPERATION) 340 * instruction 341 * @func: the function code passed to PPNO; see CPACF_PPNO_xxx defines 342 * @param: address of parameter block; see POP for details on each func 343 * @dest: address of destination memory area 344 * @dest_len: size of destination memory area in bytes 345 * @seed: address of seed data 346 * @seed_len: size of seed data in bytes 347 */ 348 static inline void cpacf_ppno(unsigned long func, void *param, 349 u8 *dest, long dest_len, 350 const u8 *seed, long seed_len) 351 { 352 register unsigned long r0 asm("0") = (unsigned long) func; 353 register unsigned long r1 asm("1") = (unsigned long) param; 354 register unsigned long r2 asm("2") = (unsigned long) dest; 355 register unsigned long r3 asm("3") = (unsigned long) dest_len; 356 register unsigned long r4 asm("4") = (unsigned long) seed; 357 register unsigned long r5 asm("5") = (unsigned long) seed_len; 358 359 asm volatile ( 360 "0: .insn rre,%[opc] << 16,%[dst],%[seed]\n" 361 " brc 1,0b\n" /* handle partial completion */ 362 : [dst] "+a" (r2), [dlen] "+d" (r3) 363 : [fc] "d" (r0), [pba] "a" (r1), 364 [seed] "a" (r4), [slen] "d" (r5), [opc] "i" (CPACF_PPNO) 365 : "cc", "memory"); 366 } 367 368 /** 369 * cpacf_pcc() - executes the PCC (PERFORM CRYPTOGRAPHIC COMPUTATION) 370 * instruction 371 * @func: the function code passed to PCC; see CPACF_KM_xxx defines 372 * @param: address of parameter block; see POP for details on each func 373 */ 374 static inline void cpacf_pcc(unsigned long func, void *param) 375 { 376 register unsigned long r0 asm("0") = (unsigned long) func; 377 register unsigned long r1 asm("1") = (unsigned long) param; 378 379 asm volatile( 380 "0: .insn rre,%[opc] << 16,0,0\n" /* PCC opcode */ 381 " brc 1,0b\n" /* handle partial completion */ 382 : 383 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_PCC) 384 : "cc", "memory"); 385 } 386 387 #endif /* _ASM_S390_CPACF_H */ 388