[PATCH] s390: des crypto code speedup

Provide ECB and CBC encrypt / decrypt functions to crypto API to speed up our
hardware accelerated DES implementation. This new functions allow the crypto
API to call ECB / CBC directly with large blocks in difference to the old
functions that were calles with algorithm block size (8 bytes for DES).

This is up to factor 10 faster than our old hardware implementation :)

Signed-off-by: Jan Glauber <jan.glauber@de.ibm.com>
Signed-off-by: Martin Schwidefsky <schwidefsky@de.ibm.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>

authored by Jan Glauber and committed by Linus Torvalds b8dc6038 c1357833

+210 -7
+210 -7
arch/s390/crypto/des_s390.c
··· 57 57 return ret; 58 58 } 59 59 60 - static void des_encrypt(void *ctx, u8 *dst, const u8 *src) 60 + static void des_encrypt(void *ctx, u8 *out, const u8 *in) 61 61 { 62 62 struct crypt_s390_des_ctx *dctx = ctx; 63 63 64 - crypt_s390_km(KM_DEA_ENCRYPT, dctx->key, dst, src, DES_BLOCK_SIZE); 64 + crypt_s390_km(KM_DEA_ENCRYPT, dctx->key, out, in, DES_BLOCK_SIZE); 65 65 } 66 66 67 - static void des_decrypt(void *ctx, u8 *dst, const u8 *src) 67 + static void des_decrypt(void *ctx, u8 *out, const u8 *in) 68 68 { 69 69 struct crypt_s390_des_ctx *dctx = ctx; 70 70 71 - crypt_s390_km(KM_DEA_DECRYPT, dctx->key, dst, src, DES_BLOCK_SIZE); 71 + crypt_s390_km(KM_DEA_DECRYPT, dctx->key, out, in, DES_BLOCK_SIZE); 72 + } 73 + 74 + static unsigned int des_encrypt_ecb(const struct cipher_desc *desc, u8 *out, 75 + const u8 *in, unsigned int nbytes) 76 + { 77 + struct crypt_s390_des_ctx *sctx = crypto_tfm_ctx(desc->tfm); 78 + int ret; 79 + 80 + /* only use complete blocks */ 81 + nbytes &= ~(DES_BLOCK_SIZE - 1); 82 + ret = crypt_s390_km(KM_DEA_ENCRYPT, sctx->key, out, in, nbytes); 83 + BUG_ON((ret < 0) || (ret != nbytes)); 84 + 85 + return nbytes; 86 + } 87 + 88 + static unsigned int des_decrypt_ecb(const struct cipher_desc *desc, u8 *out, 89 + const u8 *in, unsigned int nbytes) 90 + { 91 + struct crypt_s390_des_ctx *sctx = crypto_tfm_ctx(desc->tfm); 92 + int ret; 93 + 94 + /* only use complete blocks */ 95 + nbytes &= ~(DES_BLOCK_SIZE - 1); 96 + ret = crypt_s390_km(KM_DEA_DECRYPT, sctx->key, out, in, nbytes); 97 + BUG_ON((ret < 0) || (ret != nbytes)); 98 + 99 + return nbytes; 100 + } 101 + 102 + static unsigned int des_encrypt_cbc(const struct cipher_desc *desc, u8 *out, 103 + const u8 *in, unsigned int nbytes) 104 + { 105 + struct crypt_s390_des_ctx *sctx = crypto_tfm_ctx(desc->tfm); 106 + int ret; 107 + 108 + /* only use complete blocks */ 109 + nbytes &= ~(DES_BLOCK_SIZE - 1); 110 + 111 + memcpy(sctx->iv, desc->info, DES_BLOCK_SIZE); 112 + ret = crypt_s390_kmc(KMC_DEA_ENCRYPT, &sctx->iv, out, in, nbytes); 113 + BUG_ON((ret < 0) || (ret != nbytes)); 114 + 115 + memcpy(desc->info, sctx->iv, DES_BLOCK_SIZE); 116 + return nbytes; 117 + } 118 + 119 + static unsigned int des_decrypt_cbc(const struct cipher_desc *desc, u8 *out, 120 + const u8 *in, unsigned int nbytes) 121 + { 122 + struct crypt_s390_des_ctx *sctx = crypto_tfm_ctx(desc->tfm); 123 + int ret; 124 + 125 + /* only use complete blocks */ 126 + nbytes &= ~(DES_BLOCK_SIZE - 1); 127 + 128 + memcpy(&sctx->iv, desc->info, DES_BLOCK_SIZE); 129 + ret = crypt_s390_kmc(KMC_DEA_DECRYPT, &sctx->iv, out, in, nbytes); 130 + BUG_ON((ret < 0) || (ret != nbytes)); 131 + 132 + return nbytes; 72 133 } 73 134 74 135 static struct crypto_alg des_alg = { ··· 145 84 .cia_max_keysize = DES_KEY_SIZE, 146 85 .cia_setkey = des_setkey, 147 86 .cia_encrypt = des_encrypt, 148 - .cia_decrypt = des_decrypt 87 + .cia_decrypt = des_decrypt, 88 + .cia_encrypt_ecb = des_encrypt_ecb, 89 + .cia_decrypt_ecb = des_decrypt_ecb, 90 + .cia_encrypt_cbc = des_encrypt_cbc, 91 + .cia_decrypt_cbc = des_decrypt_cbc, 149 92 } 150 93 } 151 94 }; ··· 202 137 DES3_128_BLOCK_SIZE); 203 138 } 204 139 140 + static unsigned int des3_128_encrypt_ecb(const struct cipher_desc *desc, 141 + u8 *out, const u8 *in, 142 + unsigned int nbytes) 143 + { 144 + struct crypt_s390_des3_128_ctx *sctx = crypto_tfm_ctx(desc->tfm); 145 + int ret; 146 + 147 + /* only use complete blocks */ 148 + nbytes &= ~(DES3_128_BLOCK_SIZE - 1); 149 + ret = crypt_s390_km(KM_TDEA_128_ENCRYPT, sctx->key, out, in, nbytes); 150 + BUG_ON((ret < 0) || (ret != nbytes)); 151 + 152 + return nbytes; 153 + } 154 + 155 + static unsigned int des3_128_decrypt_ecb(const struct cipher_desc *desc, 156 + u8 *out, const u8 *in, 157 + unsigned int nbytes) 158 + { 159 + struct crypt_s390_des3_128_ctx *sctx = crypto_tfm_ctx(desc->tfm); 160 + int ret; 161 + 162 + /* only use complete blocks */ 163 + nbytes &= ~(DES3_128_BLOCK_SIZE - 1); 164 + ret = crypt_s390_km(KM_TDEA_128_DECRYPT, sctx->key, out, in, nbytes); 165 + BUG_ON((ret < 0) || (ret != nbytes)); 166 + 167 + return nbytes; 168 + } 169 + 170 + static unsigned int des3_128_encrypt_cbc(const struct cipher_desc *desc, 171 + u8 *out, const u8 *in, 172 + unsigned int nbytes) 173 + { 174 + struct crypt_s390_des3_128_ctx *sctx = crypto_tfm_ctx(desc->tfm); 175 + int ret; 176 + 177 + /* only use complete blocks */ 178 + nbytes &= ~(DES3_128_BLOCK_SIZE - 1); 179 + 180 + memcpy(sctx->iv, desc->info, DES3_128_BLOCK_SIZE); 181 + ret = crypt_s390_kmc(KMC_TDEA_128_ENCRYPT, &sctx->iv, out, in, nbytes); 182 + BUG_ON((ret < 0) || (ret != nbytes)); 183 + 184 + memcpy(desc->info, sctx->iv, DES3_128_BLOCK_SIZE); 185 + return nbytes; 186 + } 187 + 188 + static unsigned int des3_128_decrypt_cbc(const struct cipher_desc *desc, 189 + u8 *out, const u8 *in, 190 + unsigned int nbytes) 191 + { 192 + struct crypt_s390_des3_128_ctx *sctx = crypto_tfm_ctx(desc->tfm); 193 + int ret; 194 + 195 + /* only use complete blocks */ 196 + nbytes &= ~(DES3_128_BLOCK_SIZE - 1); 197 + 198 + memcpy(&sctx->iv, desc->info, DES3_128_BLOCK_SIZE); 199 + ret = crypt_s390_kmc(KMC_TDEA_128_DECRYPT, &sctx->iv, out, in, nbytes); 200 + BUG_ON((ret < 0) || (ret != nbytes)); 201 + 202 + return nbytes; 203 + } 204 + 205 205 static struct crypto_alg des3_128_alg = { 206 206 .cra_name = "des3_ede128", 207 207 .cra_flags = CRYPTO_ALG_TYPE_CIPHER, ··· 280 150 .cia_max_keysize = DES3_128_KEY_SIZE, 281 151 .cia_setkey = des3_128_setkey, 282 152 .cia_encrypt = des3_128_encrypt, 283 - .cia_decrypt = des3_128_decrypt 153 + .cia_decrypt = des3_128_decrypt, 154 + .cia_encrypt_ecb = des3_128_encrypt_ecb, 155 + .cia_decrypt_ecb = des3_128_decrypt_ecb, 156 + .cia_encrypt_cbc = des3_128_encrypt_cbc, 157 + .cia_decrypt_cbc = des3_128_decrypt_cbc, 284 158 } 285 159 } 286 160 }; ··· 341 207 DES3_192_BLOCK_SIZE); 342 208 } 343 209 210 + static unsigned int des3_192_encrypt_ecb(const struct cipher_desc *desc, 211 + u8 *out, const u8 *in, 212 + unsigned int nbytes) 213 + { 214 + struct crypt_s390_des3_192_ctx *sctx = crypto_tfm_ctx(desc->tfm); 215 + int ret; 216 + 217 + /* only use complete blocks */ 218 + nbytes &= ~(DES3_192_BLOCK_SIZE - 1); 219 + ret = crypt_s390_km(KM_TDEA_192_ENCRYPT, sctx->key, out, in, nbytes); 220 + BUG_ON((ret < 0) || (ret != nbytes)); 221 + 222 + return nbytes; 223 + } 224 + 225 + static unsigned int des3_192_decrypt_ecb(const struct cipher_desc *desc, 226 + u8 *out, const u8 *in, 227 + unsigned int nbytes) 228 + { 229 + struct crypt_s390_des3_192_ctx *sctx = crypto_tfm_ctx(desc->tfm); 230 + int ret; 231 + 232 + /* only use complete blocks */ 233 + nbytes &= ~(DES3_192_BLOCK_SIZE - 1); 234 + ret = crypt_s390_km(KM_TDEA_192_DECRYPT, sctx->key, out, in, nbytes); 235 + BUG_ON((ret < 0) || (ret != nbytes)); 236 + 237 + return nbytes; 238 + } 239 + 240 + static unsigned int des3_192_encrypt_cbc(const struct cipher_desc *desc, 241 + u8 *out, const u8 *in, 242 + unsigned int nbytes) 243 + { 244 + struct crypt_s390_des3_192_ctx *sctx = crypto_tfm_ctx(desc->tfm); 245 + int ret; 246 + 247 + /* only use complete blocks */ 248 + nbytes &= ~(DES3_192_BLOCK_SIZE - 1); 249 + 250 + memcpy(sctx->iv, desc->info, DES3_192_BLOCK_SIZE); 251 + ret = crypt_s390_kmc(KMC_TDEA_192_ENCRYPT, &sctx->iv, out, in, nbytes); 252 + BUG_ON((ret < 0) || (ret != nbytes)); 253 + 254 + memcpy(desc->info, sctx->iv, DES3_192_BLOCK_SIZE); 255 + return nbytes; 256 + } 257 + 258 + static unsigned int des3_192_decrypt_cbc(const struct cipher_desc *desc, 259 + u8 *out, const u8 *in, 260 + unsigned int nbytes) 261 + { 262 + struct crypt_s390_des3_192_ctx *sctx = crypto_tfm_ctx(desc->tfm); 263 + int ret; 264 + 265 + /* only use complete blocks */ 266 + nbytes &= ~(DES3_192_BLOCK_SIZE - 1); 267 + 268 + memcpy(&sctx->iv, desc->info, DES3_192_BLOCK_SIZE); 269 + ret = crypt_s390_kmc(KMC_TDEA_192_DECRYPT, &sctx->iv, out, in, nbytes); 270 + BUG_ON((ret < 0) || (ret != nbytes)); 271 + 272 + return nbytes; 273 + } 274 + 344 275 static struct crypto_alg des3_192_alg = { 345 276 .cra_name = "des3_ede", 346 277 .cra_flags = CRYPTO_ALG_TYPE_CIPHER, ··· 419 220 .cia_max_keysize = DES3_192_KEY_SIZE, 420 221 .cia_setkey = des3_192_setkey, 421 222 .cia_encrypt = des3_192_encrypt, 422 - .cia_decrypt = des3_192_decrypt 223 + .cia_decrypt = des3_192_decrypt, 224 + .cia_encrypt_ecb = des3_192_encrypt_ecb, 225 + .cia_decrypt_ecb = des3_192_decrypt_ecb, 226 + .cia_encrypt_cbc = des3_192_encrypt_cbc, 227 + .cia_decrypt_cbc = des3_192_decrypt_cbc, 423 228 } 424 229 } 425 230 };