Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Cryptographic API. |
| 3 | * |
| 4 | * Support for s390 cryptographic instructions. |
| 5 | * |
Heiko Carstens | a53c8fa | 2012-07-20 11:15:04 +0200 | [diff] [blame] | 6 | * Copyright IBM Corp. 2003, 2007 |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 7 | * Author(s): Thomas Spatzier |
| 8 | * Jan Glauber (jan.glauber@de.ibm.com) |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify it |
| 11 | * under the terms of the GNU General Public License as published by the Free |
| 12 | * Software Foundation; either version 2 of the License, or (at your option) |
| 13 | * any later version. |
| 14 | * |
| 15 | */ |
| 16 | #ifndef _CRYPTO_ARCH_S390_CRYPT_S390_H |
| 17 | #define _CRYPTO_ARCH_S390_CRYPT_S390_H |
| 18 | |
| 19 | #include <asm/errno.h> |
David Howells | a0616cd | 2012-03-28 18:30:02 +0100 | [diff] [blame] | 20 | #include <asm/facility.h> |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 21 | |
| 22 | #define CRYPT_S390_OP_MASK 0xFF00 |
| 23 | #define CRYPT_S390_FUNC_MASK 0x00FF |
| 24 | |
Herbert Xu | 65b75c3 | 2006-08-21 21:18:50 +1000 | [diff] [blame] | 25 | #define CRYPT_S390_PRIORITY 300 |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 26 | #define CRYPT_S390_COMPOSITE_PRIORITY 400 |
Herbert Xu | 65b75c3 | 2006-08-21 21:18:50 +1000 | [diff] [blame] | 27 | |
Jan Glauber | 1822bc9 | 2011-04-19 21:29:14 +0200 | [diff] [blame] | 28 | #define CRYPT_S390_MSA 0x1 |
| 29 | #define CRYPT_S390_MSA3 0x2 |
| 30 | #define CRYPT_S390_MSA4 0x4 |
| 31 | |
Ralph Wuerthner | bccdbdc | 2007-07-10 11:24:08 +0200 | [diff] [blame] | 32 | /* s390 cryptographic operations */ |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 33 | enum crypt_s390_operations { |
| 34 | CRYPT_S390_KM = 0x0100, |
| 35 | CRYPT_S390_KMC = 0x0200, |
| 36 | CRYPT_S390_KIMD = 0x0300, |
| 37 | CRYPT_S390_KLMD = 0x0400, |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 38 | CRYPT_S390_KMAC = 0x0500, |
| 39 | CRYPT_S390_KMCTR = 0x0600 |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 40 | }; |
| 41 | |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 42 | /* |
| 43 | * function codes for KM (CIPHER MESSAGE) instruction |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 44 | * 0x80 is the decipher modifier bit |
| 45 | */ |
| 46 | enum crypt_s390_km_func { |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 47 | KM_QUERY = CRYPT_S390_KM | 0x0, |
| 48 | KM_DEA_ENCRYPT = CRYPT_S390_KM | 0x1, |
| 49 | KM_DEA_DECRYPT = CRYPT_S390_KM | 0x1 | 0x80, |
| 50 | KM_TDEA_128_ENCRYPT = CRYPT_S390_KM | 0x2, |
| 51 | KM_TDEA_128_DECRYPT = CRYPT_S390_KM | 0x2 | 0x80, |
| 52 | KM_TDEA_192_ENCRYPT = CRYPT_S390_KM | 0x3, |
| 53 | KM_TDEA_192_DECRYPT = CRYPT_S390_KM | 0x3 | 0x80, |
| 54 | KM_AES_128_ENCRYPT = CRYPT_S390_KM | 0x12, |
| 55 | KM_AES_128_DECRYPT = CRYPT_S390_KM | 0x12 | 0x80, |
| 56 | KM_AES_192_ENCRYPT = CRYPT_S390_KM | 0x13, |
| 57 | KM_AES_192_DECRYPT = CRYPT_S390_KM | 0x13 | 0x80, |
| 58 | KM_AES_256_ENCRYPT = CRYPT_S390_KM | 0x14, |
| 59 | KM_AES_256_DECRYPT = CRYPT_S390_KM | 0x14 | 0x80, |
Gerald Schaefer | 99d9722 | 2011-04-26 16:12:42 +1000 | [diff] [blame] | 60 | KM_XTS_128_ENCRYPT = CRYPT_S390_KM | 0x32, |
| 61 | KM_XTS_128_DECRYPT = CRYPT_S390_KM | 0x32 | 0x80, |
| 62 | KM_XTS_256_ENCRYPT = CRYPT_S390_KM | 0x34, |
| 63 | KM_XTS_256_DECRYPT = CRYPT_S390_KM | 0x34 | 0x80, |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 64 | }; |
| 65 | |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 66 | /* |
| 67 | * function codes for KMC (CIPHER MESSAGE WITH CHAINING) |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 68 | * instruction |
| 69 | */ |
| 70 | enum crypt_s390_kmc_func { |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 71 | KMC_QUERY = CRYPT_S390_KMC | 0x0, |
| 72 | KMC_DEA_ENCRYPT = CRYPT_S390_KMC | 0x1, |
| 73 | KMC_DEA_DECRYPT = CRYPT_S390_KMC | 0x1 | 0x80, |
| 74 | KMC_TDEA_128_ENCRYPT = CRYPT_S390_KMC | 0x2, |
| 75 | KMC_TDEA_128_DECRYPT = CRYPT_S390_KMC | 0x2 | 0x80, |
| 76 | KMC_TDEA_192_ENCRYPT = CRYPT_S390_KMC | 0x3, |
| 77 | KMC_TDEA_192_DECRYPT = CRYPT_S390_KMC | 0x3 | 0x80, |
| 78 | KMC_AES_128_ENCRYPT = CRYPT_S390_KMC | 0x12, |
| 79 | KMC_AES_128_DECRYPT = CRYPT_S390_KMC | 0x12 | 0x80, |
| 80 | KMC_AES_192_ENCRYPT = CRYPT_S390_KMC | 0x13, |
| 81 | KMC_AES_192_DECRYPT = CRYPT_S390_KMC | 0x13 | 0x80, |
| 82 | KMC_AES_256_ENCRYPT = CRYPT_S390_KMC | 0x14, |
| 83 | KMC_AES_256_DECRYPT = CRYPT_S390_KMC | 0x14 | 0x80, |
Jan Glauber | 1b27829 | 2007-02-05 21:18:22 +0100 | [diff] [blame] | 84 | KMC_PRNG = CRYPT_S390_KMC | 0x43, |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 85 | }; |
| 86 | |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 87 | /* |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 88 | * function codes for KMCTR (CIPHER MESSAGE WITH COUNTER) |
| 89 | * instruction |
| 90 | */ |
| 91 | enum crypt_s390_kmctr_func { |
| 92 | KMCTR_QUERY = CRYPT_S390_KMCTR | 0x0, |
| 93 | KMCTR_DEA_ENCRYPT = CRYPT_S390_KMCTR | 0x1, |
| 94 | KMCTR_DEA_DECRYPT = CRYPT_S390_KMCTR | 0x1 | 0x80, |
| 95 | KMCTR_TDEA_128_ENCRYPT = CRYPT_S390_KMCTR | 0x2, |
| 96 | KMCTR_TDEA_128_DECRYPT = CRYPT_S390_KMCTR | 0x2 | 0x80, |
| 97 | KMCTR_TDEA_192_ENCRYPT = CRYPT_S390_KMCTR | 0x3, |
| 98 | KMCTR_TDEA_192_DECRYPT = CRYPT_S390_KMCTR | 0x3 | 0x80, |
| 99 | KMCTR_AES_128_ENCRYPT = CRYPT_S390_KMCTR | 0x12, |
| 100 | KMCTR_AES_128_DECRYPT = CRYPT_S390_KMCTR | 0x12 | 0x80, |
| 101 | KMCTR_AES_192_ENCRYPT = CRYPT_S390_KMCTR | 0x13, |
| 102 | KMCTR_AES_192_DECRYPT = CRYPT_S390_KMCTR | 0x13 | 0x80, |
| 103 | KMCTR_AES_256_ENCRYPT = CRYPT_S390_KMCTR | 0x14, |
| 104 | KMCTR_AES_256_DECRYPT = CRYPT_S390_KMCTR | 0x14 | 0x80, |
| 105 | }; |
| 106 | |
| 107 | /* |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 108 | * function codes for KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST) |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 109 | * instruction |
| 110 | */ |
| 111 | enum crypt_s390_kimd_func { |
| 112 | KIMD_QUERY = CRYPT_S390_KIMD | 0, |
| 113 | KIMD_SHA_1 = CRYPT_S390_KIMD | 1, |
Jan Glauber | 0a497c17 | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 114 | KIMD_SHA_256 = CRYPT_S390_KIMD | 2, |
Jan Glauber | 291dc7c | 2008-03-06 19:52:00 +0800 | [diff] [blame] | 115 | KIMD_SHA_512 = CRYPT_S390_KIMD | 3, |
Gerald Schaefer | df1309c | 2011-04-19 21:29:18 +0200 | [diff] [blame] | 116 | KIMD_GHASH = CRYPT_S390_KIMD | 65, |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 117 | }; |
| 118 | |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 119 | /* |
| 120 | * function codes for KLMD (COMPUTE LAST MESSAGE DIGEST) |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 121 | * instruction |
| 122 | */ |
| 123 | enum crypt_s390_klmd_func { |
| 124 | KLMD_QUERY = CRYPT_S390_KLMD | 0, |
| 125 | KLMD_SHA_1 = CRYPT_S390_KLMD | 1, |
Jan Glauber | 0a497c17 | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 126 | KLMD_SHA_256 = CRYPT_S390_KLMD | 2, |
Jan Glauber | 291dc7c | 2008-03-06 19:52:00 +0800 | [diff] [blame] | 127 | KLMD_SHA_512 = CRYPT_S390_KLMD | 3, |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 128 | }; |
| 129 | |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 130 | /* |
| 131 | * function codes for KMAC (COMPUTE MESSAGE AUTHENTICATION CODE) |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 132 | * instruction |
| 133 | */ |
| 134 | enum crypt_s390_kmac_func { |
| 135 | KMAC_QUERY = CRYPT_S390_KMAC | 0, |
| 136 | KMAC_DEA = CRYPT_S390_KMAC | 1, |
| 137 | KMAC_TDEA_128 = CRYPT_S390_KMAC | 2, |
| 138 | KMAC_TDEA_192 = CRYPT_S390_KMAC | 3 |
| 139 | }; |
| 140 | |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 141 | /** |
| 142 | * crypt_s390_km: |
| 143 | * @func: the function code passed to KM; see crypt_s390_km_func |
| 144 | * @param: address of parameter block; see POP for details on each func |
| 145 | * @dest: address of destination memory area |
| 146 | * @src: address of source memory area |
| 147 | * @src_len: length of src operand in bytes |
| 148 | * |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 149 | * Executes the KM (CIPHER MESSAGE) operation of the CPU. |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 150 | * |
| 151 | * Returns -1 for failure, 0 for the query func, number of processed |
| 152 | * bytes for encryption/decryption funcs |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 153 | */ |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 154 | static inline int crypt_s390_km(long func, void *param, |
| 155 | u8 *dest, const u8 *src, long src_len) |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 156 | { |
| 157 | register long __func asm("0") = func & CRYPT_S390_FUNC_MASK; |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 158 | register void *__param asm("1") = param; |
| 159 | register const u8 *__src asm("2") = src; |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 160 | register long __src_len asm("3") = src_len; |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 161 | register u8 *__dest asm("4") = dest; |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 162 | int ret; |
| 163 | |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 164 | asm volatile( |
| 165 | "0: .insn rre,0xb92e0000,%3,%1 \n" /* KM opcode */ |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 166 | "1: brc 1,0b \n" /* handle partial completion */ |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 167 | " la %0,0\n" |
| 168 | "2:\n" |
| 169 | EX_TABLE(0b,2b) EX_TABLE(1b,2b) |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 170 | : "=d" (ret), "+a" (__src), "+d" (__src_len), "+a" (__dest) |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 171 | : "d" (__func), "a" (__param), "0" (-1) : "cc", "memory"); |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 172 | if (ret < 0) |
| 173 | return ret; |
| 174 | return (func & CRYPT_S390_FUNC_MASK) ? src_len - __src_len : __src_len; |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /** |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 178 | * crypt_s390_kmc: |
| 179 | * @func: the function code passed to KM; see crypt_s390_kmc_func |
| 180 | * @param: address of parameter block; see POP for details on each func |
| 181 | * @dest: address of destination memory area |
| 182 | * @src: address of source memory area |
| 183 | * @src_len: length of src operand in bytes |
| 184 | * |
| 185 | * Executes the KMC (CIPHER MESSAGE WITH CHAINING) operation of the CPU. |
| 186 | * |
| 187 | * Returns -1 for failure, 0 for the query func, number of processed |
| 188 | * bytes for encryption/decryption funcs |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 189 | */ |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 190 | static inline int crypt_s390_kmc(long func, void *param, |
| 191 | u8 *dest, const u8 *src, long src_len) |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 192 | { |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 193 | register long __func asm("0") = func & CRYPT_S390_FUNC_MASK; |
| 194 | register void *__param asm("1") = param; |
| 195 | register const u8 *__src asm("2") = src; |
| 196 | register long __src_len asm("3") = src_len; |
| 197 | register u8 *__dest asm("4") = dest; |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 198 | int ret; |
| 199 | |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 200 | asm volatile( |
| 201 | "0: .insn rre,0xb92f0000,%3,%1 \n" /* KMC opcode */ |
| 202 | "1: brc 1,0b \n" /* handle partial completion */ |
| 203 | " la %0,0\n" |
| 204 | "2:\n" |
| 205 | EX_TABLE(0b,2b) EX_TABLE(1b,2b) |
| 206 | : "=d" (ret), "+a" (__src), "+d" (__src_len), "+a" (__dest) |
| 207 | : "d" (__func), "a" (__param), "0" (-1) : "cc", "memory"); |
| 208 | if (ret < 0) |
| 209 | return ret; |
| 210 | return (func & CRYPT_S390_FUNC_MASK) ? src_len - __src_len : __src_len; |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 211 | } |
| 212 | |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 213 | /** |
| 214 | * crypt_s390_kimd: |
| 215 | * @func: the function code passed to KM; see crypt_s390_kimd_func |
| 216 | * @param: address of parameter block; see POP for details on each func |
| 217 | * @src: address of source memory area |
| 218 | * @src_len: length of src operand in bytes |
| 219 | * |
| 220 | * Executes the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST) operation |
| 221 | * of the CPU. |
| 222 | * |
| 223 | * Returns -1 for failure, 0 for the query func, number of processed |
| 224 | * bytes for digest funcs |
| 225 | */ |
| 226 | static inline int crypt_s390_kimd(long func, void *param, |
| 227 | const u8 *src, long src_len) |
| 228 | { |
| 229 | register long __func asm("0") = func & CRYPT_S390_FUNC_MASK; |
| 230 | register void *__param asm("1") = param; |
| 231 | register const u8 *__src asm("2") = src; |
| 232 | register long __src_len asm("3") = src_len; |
| 233 | int ret; |
| 234 | |
| 235 | asm volatile( |
| 236 | "0: .insn rre,0xb93e0000,%1,%1 \n" /* KIMD opcode */ |
| 237 | "1: brc 1,0b \n" /* handle partial completion */ |
| 238 | " la %0,0\n" |
| 239 | "2:\n" |
| 240 | EX_TABLE(0b,2b) EX_TABLE(1b,2b) |
| 241 | : "=d" (ret), "+a" (__src), "+d" (__src_len) |
| 242 | : "d" (__func), "a" (__param), "0" (-1) : "cc", "memory"); |
| 243 | if (ret < 0) |
| 244 | return ret; |
| 245 | return (func & CRYPT_S390_FUNC_MASK) ? src_len - __src_len : __src_len; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * crypt_s390_klmd: |
| 250 | * @func: the function code passed to KM; see crypt_s390_klmd_func |
| 251 | * @param: address of parameter block; see POP for details on each func |
| 252 | * @src: address of source memory area |
| 253 | * @src_len: length of src operand in bytes |
| 254 | * |
| 255 | * Executes the KLMD (COMPUTE LAST MESSAGE DIGEST) operation of the CPU. |
| 256 | * |
| 257 | * Returns -1 for failure, 0 for the query func, number of processed |
| 258 | * bytes for digest funcs |
| 259 | */ |
| 260 | static inline int crypt_s390_klmd(long func, void *param, |
| 261 | const u8 *src, long src_len) |
| 262 | { |
| 263 | register long __func asm("0") = func & CRYPT_S390_FUNC_MASK; |
| 264 | register void *__param asm("1") = param; |
| 265 | register const u8 *__src asm("2") = src; |
| 266 | register long __src_len asm("3") = src_len; |
| 267 | int ret; |
| 268 | |
| 269 | asm volatile( |
| 270 | "0: .insn rre,0xb93f0000,%1,%1 \n" /* KLMD opcode */ |
| 271 | "1: brc 1,0b \n" /* handle partial completion */ |
| 272 | " la %0,0\n" |
| 273 | "2:\n" |
| 274 | EX_TABLE(0b,2b) EX_TABLE(1b,2b) |
| 275 | : "=d" (ret), "+a" (__src), "+d" (__src_len) |
| 276 | : "d" (__func), "a" (__param), "0" (-1) : "cc", "memory"); |
| 277 | if (ret < 0) |
| 278 | return ret; |
| 279 | return (func & CRYPT_S390_FUNC_MASK) ? src_len - __src_len : __src_len; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * crypt_s390_kmac: |
| 284 | * @func: the function code passed to KM; see crypt_s390_klmd_func |
| 285 | * @param: address of parameter block; see POP for details on each func |
| 286 | * @src: address of source memory area |
| 287 | * @src_len: length of src operand in bytes |
| 288 | * |
| 289 | * Executes the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE) operation |
| 290 | * of the CPU. |
| 291 | * |
| 292 | * Returns -1 for failure, 0 for the query func, number of processed |
| 293 | * bytes for digest funcs |
| 294 | */ |
| 295 | static inline int crypt_s390_kmac(long func, void *param, |
| 296 | const u8 *src, long src_len) |
| 297 | { |
| 298 | register long __func asm("0") = func & CRYPT_S390_FUNC_MASK; |
| 299 | register void *__param asm("1") = param; |
| 300 | register const u8 *__src asm("2") = src; |
| 301 | register long __src_len asm("3") = src_len; |
| 302 | int ret; |
| 303 | |
| 304 | asm volatile( |
| 305 | "0: .insn rre,0xb91e0000,%1,%1 \n" /* KLAC opcode */ |
| 306 | "1: brc 1,0b \n" /* handle partial completion */ |
| 307 | " la %0,0\n" |
| 308 | "2:\n" |
| 309 | EX_TABLE(0b,2b) EX_TABLE(1b,2b) |
| 310 | : "=d" (ret), "+a" (__src), "+d" (__src_len) |
| 311 | : "d" (__func), "a" (__param), "0" (-1) : "cc", "memory"); |
| 312 | if (ret < 0) |
| 313 | return ret; |
| 314 | return (func & CRYPT_S390_FUNC_MASK) ? src_len - __src_len : __src_len; |
| 315 | } |
| 316 | |
| 317 | /** |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 318 | * crypt_s390_kmctr: |
| 319 | * @func: the function code passed to KMCTR; see crypt_s390_kmctr_func |
| 320 | * @param: address of parameter block; see POP for details on each func |
| 321 | * @dest: address of destination memory area |
| 322 | * @src: address of source memory area |
| 323 | * @src_len: length of src operand in bytes |
| 324 | * @counter: address of counter value |
| 325 | * |
| 326 | * Executes the KMCTR (CIPHER MESSAGE WITH COUNTER) operation of the CPU. |
| 327 | * |
| 328 | * Returns -1 for failure, 0 for the query func, number of processed |
| 329 | * bytes for encryption/decryption funcs |
| 330 | */ |
| 331 | static inline int crypt_s390_kmctr(long func, void *param, u8 *dest, |
| 332 | const u8 *src, long src_len, u8 *counter) |
| 333 | { |
| 334 | register long __func asm("0") = func & CRYPT_S390_FUNC_MASK; |
| 335 | register void *__param asm("1") = param; |
| 336 | register const u8 *__src asm("2") = src; |
| 337 | register long __src_len asm("3") = src_len; |
| 338 | register u8 *__dest asm("4") = dest; |
| 339 | register u8 *__ctr asm("6") = counter; |
| 340 | int ret = -1; |
| 341 | |
| 342 | asm volatile( |
| 343 | "0: .insn rrf,0xb92d0000,%3,%1,%4,0 \n" /* KMCTR opcode */ |
| 344 | "1: brc 1,0b \n" /* handle partial completion */ |
| 345 | " la %0,0\n" |
| 346 | "2:\n" |
| 347 | EX_TABLE(0b,2b) EX_TABLE(1b,2b) |
| 348 | : "+d" (ret), "+a" (__src), "+d" (__src_len), "+a" (__dest), |
| 349 | "+a" (__ctr) |
| 350 | : "d" (__func), "a" (__param) : "cc", "memory"); |
| 351 | if (ret < 0) |
| 352 | return ret; |
| 353 | return (func & CRYPT_S390_FUNC_MASK) ? src_len - __src_len : __src_len; |
| 354 | } |
| 355 | |
| 356 | /** |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 357 | * crypt_s390_func_available: |
| 358 | * @func: the function code of the specific function; 0 if op in general |
| 359 | * |
| 360 | * Tests if a specific crypto function is implemented on the machine. |
| 361 | * |
| 362 | * Returns 1 if func available; 0 if func or op in general not available |
| 363 | */ |
Jan Glauber | 1822bc9 | 2011-04-19 21:29:14 +0200 | [diff] [blame] | 364 | static inline int crypt_s390_func_available(int func, |
| 365 | unsigned int facility_mask) |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 366 | { |
| 367 | unsigned char status[16]; |
| 368 | int ret; |
| 369 | |
Jan Glauber | 1822bc9 | 2011-04-19 21:29:14 +0200 | [diff] [blame] | 370 | if (facility_mask & CRYPT_S390_MSA && !test_facility(17)) |
| 371 | return 0; |
Jan Glauber | 6ed5438 | 2011-11-14 11:19:07 +0100 | [diff] [blame] | 372 | |
| 373 | if (facility_mask & CRYPT_S390_MSA3 && |
| 374 | (!test_facility(2) || !test_facility(76))) |
Jan Glauber | 1822bc9 | 2011-04-19 21:29:14 +0200 | [diff] [blame] | 375 | return 0; |
Jan Glauber | 6ed5438 | 2011-11-14 11:19:07 +0100 | [diff] [blame] | 376 | if (facility_mask & CRYPT_S390_MSA4 && |
| 377 | (!test_facility(2) || !test_facility(77))) |
Jan Glauber | a72f0db | 2008-07-12 15:42:11 +0800 | [diff] [blame] | 378 | return 0; |
| 379 | |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 380 | switch (func & CRYPT_S390_OP_MASK) { |
| 381 | case CRYPT_S390_KM: |
| 382 | ret = crypt_s390_km(KM_QUERY, &status, NULL, NULL, 0); |
| 383 | break; |
| 384 | case CRYPT_S390_KMC: |
| 385 | ret = crypt_s390_kmc(KMC_QUERY, &status, NULL, NULL, 0); |
| 386 | break; |
| 387 | case CRYPT_S390_KIMD: |
| 388 | ret = crypt_s390_kimd(KIMD_QUERY, &status, NULL, 0); |
| 389 | break; |
| 390 | case CRYPT_S390_KLMD: |
| 391 | ret = crypt_s390_klmd(KLMD_QUERY, &status, NULL, 0); |
| 392 | break; |
| 393 | case CRYPT_S390_KMAC: |
| 394 | ret = crypt_s390_kmac(KMAC_QUERY, &status, NULL, 0); |
| 395 | break; |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 396 | case CRYPT_S390_KMCTR: |
| 397 | ret = crypt_s390_kmctr(KMCTR_QUERY, &status, NULL, NULL, 0, |
| 398 | NULL); |
| 399 | break; |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 400 | default: |
| 401 | return 0; |
| 402 | } |
| 403 | if (ret < 0) |
| 404 | return 0; |
| 405 | func &= CRYPT_S390_FUNC_MASK; |
| 406 | func &= 0x7f; /* mask modifier bit */ |
| 407 | return (status[func >> 3] & (0x80 >> (func & 7))) != 0; |
| 408 | } |
| 409 | |
Gerald Schaefer | 99d9722 | 2011-04-26 16:12:42 +1000 | [diff] [blame] | 410 | /** |
| 411 | * crypt_s390_pcc: |
| 412 | * @func: the function code passed to KM; see crypt_s390_km_func |
| 413 | * @param: address of parameter block; see POP for details on each func |
| 414 | * |
| 415 | * Executes the PCC (PERFORM CRYPTOGRAPHIC COMPUTATION) operation of the CPU. |
| 416 | * |
| 417 | * Returns -1 for failure, 0 for success. |
| 418 | */ |
| 419 | static inline int crypt_s390_pcc(long func, void *param) |
| 420 | { |
| 421 | register long __func asm("0") = func & 0x7f; /* encrypt or decrypt */ |
| 422 | register void *__param asm("1") = param; |
| 423 | int ret = -1; |
| 424 | |
| 425 | asm volatile( |
| 426 | "0: .insn rre,0xb92c0000,0,0 \n" /* PCC opcode */ |
| 427 | "1: brc 1,0b \n" /* handle partial completion */ |
| 428 | " la %0,0\n" |
| 429 | "2:\n" |
| 430 | EX_TABLE(0b,2b) EX_TABLE(1b,2b) |
| 431 | : "+d" (ret) |
| 432 | : "d" (__func), "a" (__param) : "cc", "memory"); |
| 433 | return ret; |
| 434 | } |
| 435 | |
| 436 | |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 437 | #endif /* _CRYPTO_ARCH_S390_CRYPT_S390_H */ |