Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Cryptographic API. |
| 3 | * |
| 4 | * s390 implementation of the AES Cipher Algorithm. |
| 5 | * |
| 6 | * s390 Version: |
Heiko Carstens | a53c8fa | 2012-07-20 11:15:04 +0200 | [diff] [blame] | 7 | * Copyright IBM Corp. 2005, 2007 |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 8 | * Author(s): Jan Glauber (jang@de.ibm.com) |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame] | 9 | * Sebastian Siewior (sebastian@breakpoint.cc> SW-Fallback |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 10 | * |
Sebastian Siewior | f8246af | 2007-10-05 16:52:01 +0800 | [diff] [blame] | 11 | * Derived from "crypto/aes_generic.c" |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify it |
| 14 | * under the terms of the GNU General Public License as published by the Free |
| 15 | * Software Foundation; either version 2 of the License, or (at your option) |
| 16 | * any later version. |
| 17 | * |
| 18 | */ |
| 19 | |
Jan Glauber | 39f0939 | 2008-12-25 13:39:37 +0100 | [diff] [blame] | 20 | #define KMSG_COMPONENT "aes_s390" |
| 21 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
| 22 | |
Sebastian Siewior | 89e1265 | 2007-10-17 23:18:57 +0800 | [diff] [blame] | 23 | #include <crypto/aes.h> |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 24 | #include <crypto/algapi.h> |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame] | 25 | #include <linux/err.h> |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 26 | #include <linux/module.h> |
Hendrik Brueckner | d05377c | 2015-02-19 17:34:07 +0100 | [diff] [blame] | 27 | #include <linux/cpufeature.h> |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 28 | #include <linux/init.h> |
Harald Freudenberger | 0519e9a | 2014-01-16 16:01:11 +0100 | [diff] [blame] | 29 | #include <linux/spinlock.h> |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 30 | #include "crypt_s390.h" |
| 31 | |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 32 | #define AES_KEYLEN_128 1 |
| 33 | #define AES_KEYLEN_192 2 |
| 34 | #define AES_KEYLEN_256 4 |
| 35 | |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 36 | static u8 *ctrblk; |
Harald Freudenberger | 0519e9a | 2014-01-16 16:01:11 +0100 | [diff] [blame] | 37 | static DEFINE_SPINLOCK(ctrblk_lock); |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 38 | static char keylen_flag; |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 39 | |
| 40 | struct s390_aes_ctx { |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 41 | u8 key[AES_MAX_KEY_SIZE]; |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 42 | long enc; |
| 43 | long dec; |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 44 | int key_len; |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame] | 45 | union { |
| 46 | struct crypto_blkcipher *blk; |
| 47 | struct crypto_cipher *cip; |
| 48 | } fallback; |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 49 | }; |
| 50 | |
Gerald Schaefer | 99d9722 | 2011-04-26 16:12:42 +1000 | [diff] [blame] | 51 | struct pcc_param { |
| 52 | u8 key[32]; |
| 53 | u8 tweak[16]; |
| 54 | u8 block[16]; |
| 55 | u8 bit[16]; |
| 56 | u8 xts[16]; |
| 57 | }; |
| 58 | |
| 59 | struct s390_xts_ctx { |
| 60 | u8 key[32]; |
Gerald Schaefer | 9dda276 | 2013-11-19 17:12:47 +0100 | [diff] [blame] | 61 | u8 pcc_key[32]; |
Gerald Schaefer | 99d9722 | 2011-04-26 16:12:42 +1000 | [diff] [blame] | 62 | long enc; |
| 63 | long dec; |
| 64 | int key_len; |
| 65 | struct crypto_blkcipher *fallback; |
| 66 | }; |
| 67 | |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame] | 68 | /* |
| 69 | * Check if the key_len is supported by the HW. |
| 70 | * Returns 0 if it is, a positive number if it is not and software fallback is |
| 71 | * required or a negative number in case the key size is not valid |
| 72 | */ |
| 73 | static int need_fallback(unsigned int key_len) |
| 74 | { |
| 75 | switch (key_len) { |
| 76 | case 16: |
| 77 | if (!(keylen_flag & AES_KEYLEN_128)) |
| 78 | return 1; |
| 79 | break; |
| 80 | case 24: |
| 81 | if (!(keylen_flag & AES_KEYLEN_192)) |
| 82 | return 1; |
| 83 | break; |
| 84 | case 32: |
| 85 | if (!(keylen_flag & AES_KEYLEN_256)) |
| 86 | return 1; |
| 87 | break; |
| 88 | default: |
| 89 | return -1; |
| 90 | break; |
| 91 | } |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | static int setkey_fallback_cip(struct crypto_tfm *tfm, const u8 *in_key, |
| 96 | unsigned int key_len) |
| 97 | { |
| 98 | struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm); |
| 99 | int ret; |
| 100 | |
Roel Kluin | d7ac769 | 2010-01-08 14:18:34 +1100 | [diff] [blame] | 101 | sctx->fallback.cip->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK; |
| 102 | sctx->fallback.cip->base.crt_flags |= (tfm->crt_flags & |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame] | 103 | CRYPTO_TFM_REQ_MASK); |
| 104 | |
| 105 | ret = crypto_cipher_setkey(sctx->fallback.cip, in_key, key_len); |
| 106 | if (ret) { |
| 107 | tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK; |
Roel Kluin | d7ac769 | 2010-01-08 14:18:34 +1100 | [diff] [blame] | 108 | tfm->crt_flags |= (sctx->fallback.cip->base.crt_flags & |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame] | 109 | CRYPTO_TFM_RES_MASK); |
| 110 | } |
| 111 | return ret; |
| 112 | } |
| 113 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 114 | static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key, |
Herbert Xu | 560c06a | 2006-08-13 14:16:39 +1000 | [diff] [blame] | 115 | unsigned int key_len) |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 116 | { |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 117 | struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm); |
Herbert Xu | 560c06a | 2006-08-13 14:16:39 +1000 | [diff] [blame] | 118 | u32 *flags = &tfm->crt_flags; |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame] | 119 | int ret; |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 120 | |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame] | 121 | ret = need_fallback(key_len); |
| 122 | if (ret < 0) { |
| 123 | *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN; |
| 124 | return -EINVAL; |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | sctx->key_len = key_len; |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame] | 128 | if (!ret) { |
| 129 | memcpy(sctx->key, in_key, key_len); |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | return setkey_fallback_cip(tfm, in_key, key_len); |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 136 | static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 137 | { |
Chen Gang | e6a67ad | 2015-01-01 22:56:02 +0800 | [diff] [blame] | 138 | struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm); |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 139 | |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame] | 140 | if (unlikely(need_fallback(sctx->key_len))) { |
| 141 | crypto_cipher_encrypt_one(sctx->fallback.cip, out, in); |
| 142 | return; |
| 143 | } |
| 144 | |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 145 | switch (sctx->key_len) { |
| 146 | case 16: |
| 147 | crypt_s390_km(KM_AES_128_ENCRYPT, &sctx->key, out, in, |
| 148 | AES_BLOCK_SIZE); |
| 149 | break; |
| 150 | case 24: |
| 151 | crypt_s390_km(KM_AES_192_ENCRYPT, &sctx->key, out, in, |
| 152 | AES_BLOCK_SIZE); |
| 153 | break; |
| 154 | case 32: |
| 155 | crypt_s390_km(KM_AES_256_ENCRYPT, &sctx->key, out, in, |
| 156 | AES_BLOCK_SIZE); |
| 157 | break; |
| 158 | } |
| 159 | } |
| 160 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 161 | static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 162 | { |
Chen Gang | e6a67ad | 2015-01-01 22:56:02 +0800 | [diff] [blame] | 163 | struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm); |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 164 | |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame] | 165 | if (unlikely(need_fallback(sctx->key_len))) { |
| 166 | crypto_cipher_decrypt_one(sctx->fallback.cip, out, in); |
| 167 | return; |
| 168 | } |
| 169 | |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 170 | switch (sctx->key_len) { |
| 171 | case 16: |
| 172 | crypt_s390_km(KM_AES_128_DECRYPT, &sctx->key, out, in, |
| 173 | AES_BLOCK_SIZE); |
| 174 | break; |
| 175 | case 24: |
| 176 | crypt_s390_km(KM_AES_192_DECRYPT, &sctx->key, out, in, |
| 177 | AES_BLOCK_SIZE); |
| 178 | break; |
| 179 | case 32: |
| 180 | crypt_s390_km(KM_AES_256_DECRYPT, &sctx->key, out, in, |
| 181 | AES_BLOCK_SIZE); |
| 182 | break; |
| 183 | } |
| 184 | } |
| 185 | |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame] | 186 | static int fallback_init_cip(struct crypto_tfm *tfm) |
| 187 | { |
| 188 | const char *name = tfm->__crt_alg->cra_name; |
| 189 | struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm); |
| 190 | |
| 191 | sctx->fallback.cip = crypto_alloc_cipher(name, 0, |
| 192 | CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK); |
| 193 | |
| 194 | if (IS_ERR(sctx->fallback.cip)) { |
Jan Glauber | 39f0939 | 2008-12-25 13:39:37 +0100 | [diff] [blame] | 195 | pr_err("Allocating AES fallback algorithm %s failed\n", |
| 196 | name); |
Roel Kluin | b59cdcb3 | 2009-12-18 17:43:18 +0100 | [diff] [blame] | 197 | return PTR_ERR(sctx->fallback.cip); |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | static void fallback_exit_cip(struct crypto_tfm *tfm) |
| 204 | { |
| 205 | struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm); |
| 206 | |
| 207 | crypto_free_cipher(sctx->fallback.cip); |
| 208 | sctx->fallback.cip = NULL; |
| 209 | } |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 210 | |
| 211 | static struct crypto_alg aes_alg = { |
| 212 | .cra_name = "aes", |
Herbert Xu | 65b75c3 | 2006-08-21 21:18:50 +1000 | [diff] [blame] | 213 | .cra_driver_name = "aes-s390", |
| 214 | .cra_priority = CRYPT_S390_PRIORITY, |
Jan Glauber | f67d136 | 2007-05-04 18:47:47 +0200 | [diff] [blame] | 215 | .cra_flags = CRYPTO_ALG_TYPE_CIPHER | |
| 216 | CRYPTO_ALG_NEED_FALLBACK, |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 217 | .cra_blocksize = AES_BLOCK_SIZE, |
| 218 | .cra_ctxsize = sizeof(struct s390_aes_ctx), |
| 219 | .cra_module = THIS_MODULE, |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame] | 220 | .cra_init = fallback_init_cip, |
| 221 | .cra_exit = fallback_exit_cip, |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 222 | .cra_u = { |
| 223 | .cipher = { |
| 224 | .cia_min_keysize = AES_MIN_KEY_SIZE, |
| 225 | .cia_max_keysize = AES_MAX_KEY_SIZE, |
| 226 | .cia_setkey = aes_set_key, |
| 227 | .cia_encrypt = aes_encrypt, |
| 228 | .cia_decrypt = aes_decrypt, |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | }; |
| 232 | |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame] | 233 | static int setkey_fallback_blk(struct crypto_tfm *tfm, const u8 *key, |
| 234 | unsigned int len) |
| 235 | { |
| 236 | struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm); |
| 237 | unsigned int ret; |
| 238 | |
| 239 | sctx->fallback.blk->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK; |
| 240 | sctx->fallback.blk->base.crt_flags |= (tfm->crt_flags & |
| 241 | CRYPTO_TFM_REQ_MASK); |
| 242 | |
| 243 | ret = crypto_blkcipher_setkey(sctx->fallback.blk, key, len); |
| 244 | if (ret) { |
| 245 | tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK; |
| 246 | tfm->crt_flags |= (sctx->fallback.blk->base.crt_flags & |
| 247 | CRYPTO_TFM_RES_MASK); |
| 248 | } |
| 249 | return ret; |
| 250 | } |
| 251 | |
| 252 | static int fallback_blk_dec(struct blkcipher_desc *desc, |
| 253 | struct scatterlist *dst, struct scatterlist *src, |
| 254 | unsigned int nbytes) |
| 255 | { |
| 256 | unsigned int ret; |
| 257 | struct crypto_blkcipher *tfm; |
| 258 | struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm); |
| 259 | |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame] | 260 | tfm = desc->tfm; |
| 261 | desc->tfm = sctx->fallback.blk; |
| 262 | |
Sebastian Siewior | 2d74d40 | 2007-12-10 15:49:41 +0800 | [diff] [blame] | 263 | ret = crypto_blkcipher_decrypt_iv(desc, dst, src, nbytes); |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame] | 264 | |
| 265 | desc->tfm = tfm; |
| 266 | return ret; |
| 267 | } |
| 268 | |
| 269 | static int fallback_blk_enc(struct blkcipher_desc *desc, |
| 270 | struct scatterlist *dst, struct scatterlist *src, |
| 271 | unsigned int nbytes) |
| 272 | { |
| 273 | unsigned int ret; |
| 274 | struct crypto_blkcipher *tfm; |
| 275 | struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm); |
| 276 | |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame] | 277 | tfm = desc->tfm; |
| 278 | desc->tfm = sctx->fallback.blk; |
| 279 | |
Sebastian Siewior | 2d74d40 | 2007-12-10 15:49:41 +0800 | [diff] [blame] | 280 | ret = crypto_blkcipher_encrypt_iv(desc, dst, src, nbytes); |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame] | 281 | |
| 282 | desc->tfm = tfm; |
| 283 | return ret; |
| 284 | } |
| 285 | |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 286 | static int ecb_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key, |
| 287 | unsigned int key_len) |
| 288 | { |
| 289 | struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm); |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame] | 290 | int ret; |
| 291 | |
| 292 | ret = need_fallback(key_len); |
| 293 | if (ret > 0) { |
| 294 | sctx->key_len = key_len; |
| 295 | return setkey_fallback_blk(tfm, in_key, key_len); |
| 296 | } |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 297 | |
| 298 | switch (key_len) { |
| 299 | case 16: |
| 300 | sctx->enc = KM_AES_128_ENCRYPT; |
| 301 | sctx->dec = KM_AES_128_DECRYPT; |
| 302 | break; |
| 303 | case 24: |
| 304 | sctx->enc = KM_AES_192_ENCRYPT; |
| 305 | sctx->dec = KM_AES_192_DECRYPT; |
| 306 | break; |
| 307 | case 32: |
| 308 | sctx->enc = KM_AES_256_ENCRYPT; |
| 309 | sctx->dec = KM_AES_256_DECRYPT; |
| 310 | break; |
| 311 | } |
| 312 | |
| 313 | return aes_set_key(tfm, in_key, key_len); |
| 314 | } |
| 315 | |
| 316 | static int ecb_aes_crypt(struct blkcipher_desc *desc, long func, void *param, |
| 317 | struct blkcipher_walk *walk) |
| 318 | { |
| 319 | int ret = blkcipher_walk_virt(desc, walk); |
| 320 | unsigned int nbytes; |
| 321 | |
| 322 | while ((nbytes = walk->nbytes)) { |
| 323 | /* only use complete blocks */ |
| 324 | unsigned int n = nbytes & ~(AES_BLOCK_SIZE - 1); |
| 325 | u8 *out = walk->dst.virt.addr; |
| 326 | u8 *in = walk->src.virt.addr; |
| 327 | |
| 328 | ret = crypt_s390_km(func, param, out, in, n); |
Jan Glauber | 36eb2ca | 2012-10-26 15:06:12 +0200 | [diff] [blame] | 329 | if (ret < 0 || ret != n) |
| 330 | return -EIO; |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 331 | |
| 332 | nbytes &= AES_BLOCK_SIZE - 1; |
| 333 | ret = blkcipher_walk_done(desc, walk, nbytes); |
| 334 | } |
| 335 | |
| 336 | return ret; |
| 337 | } |
| 338 | |
| 339 | static int ecb_aes_encrypt(struct blkcipher_desc *desc, |
| 340 | struct scatterlist *dst, struct scatterlist *src, |
| 341 | unsigned int nbytes) |
| 342 | { |
| 343 | struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm); |
| 344 | struct blkcipher_walk walk; |
| 345 | |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame] | 346 | if (unlikely(need_fallback(sctx->key_len))) |
| 347 | return fallback_blk_enc(desc, dst, src, nbytes); |
| 348 | |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 349 | blkcipher_walk_init(&walk, dst, src, nbytes); |
| 350 | return ecb_aes_crypt(desc, sctx->enc, sctx->key, &walk); |
| 351 | } |
| 352 | |
| 353 | static int ecb_aes_decrypt(struct blkcipher_desc *desc, |
| 354 | struct scatterlist *dst, struct scatterlist *src, |
| 355 | unsigned int nbytes) |
| 356 | { |
| 357 | struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm); |
| 358 | struct blkcipher_walk walk; |
| 359 | |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame] | 360 | if (unlikely(need_fallback(sctx->key_len))) |
| 361 | return fallback_blk_dec(desc, dst, src, nbytes); |
| 362 | |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 363 | blkcipher_walk_init(&walk, dst, src, nbytes); |
| 364 | return ecb_aes_crypt(desc, sctx->dec, sctx->key, &walk); |
| 365 | } |
| 366 | |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame] | 367 | static int fallback_init_blk(struct crypto_tfm *tfm) |
| 368 | { |
| 369 | const char *name = tfm->__crt_alg->cra_name; |
| 370 | struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm); |
| 371 | |
| 372 | sctx->fallback.blk = crypto_alloc_blkcipher(name, 0, |
| 373 | CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK); |
| 374 | |
| 375 | if (IS_ERR(sctx->fallback.blk)) { |
Jan Glauber | 39f0939 | 2008-12-25 13:39:37 +0100 | [diff] [blame] | 376 | pr_err("Allocating AES fallback algorithm %s failed\n", |
| 377 | name); |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame] | 378 | return PTR_ERR(sctx->fallback.blk); |
| 379 | } |
| 380 | |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | static void fallback_exit_blk(struct crypto_tfm *tfm) |
| 385 | { |
| 386 | struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm); |
| 387 | |
| 388 | crypto_free_blkcipher(sctx->fallback.blk); |
| 389 | sctx->fallback.blk = NULL; |
| 390 | } |
| 391 | |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 392 | static struct crypto_alg ecb_aes_alg = { |
| 393 | .cra_name = "ecb(aes)", |
| 394 | .cra_driver_name = "ecb-aes-s390", |
| 395 | .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY, |
Jan Glauber | f67d136 | 2007-05-04 18:47:47 +0200 | [diff] [blame] | 396 | .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER | |
| 397 | CRYPTO_ALG_NEED_FALLBACK, |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 398 | .cra_blocksize = AES_BLOCK_SIZE, |
| 399 | .cra_ctxsize = sizeof(struct s390_aes_ctx), |
| 400 | .cra_type = &crypto_blkcipher_type, |
| 401 | .cra_module = THIS_MODULE, |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame] | 402 | .cra_init = fallback_init_blk, |
| 403 | .cra_exit = fallback_exit_blk, |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 404 | .cra_u = { |
| 405 | .blkcipher = { |
| 406 | .min_keysize = AES_MIN_KEY_SIZE, |
| 407 | .max_keysize = AES_MAX_KEY_SIZE, |
| 408 | .setkey = ecb_aes_set_key, |
| 409 | .encrypt = ecb_aes_encrypt, |
| 410 | .decrypt = ecb_aes_decrypt, |
| 411 | } |
| 412 | } |
| 413 | }; |
| 414 | |
| 415 | static int cbc_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key, |
| 416 | unsigned int key_len) |
| 417 | { |
| 418 | struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm); |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame] | 419 | int ret; |
| 420 | |
| 421 | ret = need_fallback(key_len); |
| 422 | if (ret > 0) { |
| 423 | sctx->key_len = key_len; |
| 424 | return setkey_fallback_blk(tfm, in_key, key_len); |
| 425 | } |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 426 | |
| 427 | switch (key_len) { |
| 428 | case 16: |
| 429 | sctx->enc = KMC_AES_128_ENCRYPT; |
| 430 | sctx->dec = KMC_AES_128_DECRYPT; |
| 431 | break; |
| 432 | case 24: |
| 433 | sctx->enc = KMC_AES_192_ENCRYPT; |
| 434 | sctx->dec = KMC_AES_192_DECRYPT; |
| 435 | break; |
| 436 | case 32: |
| 437 | sctx->enc = KMC_AES_256_ENCRYPT; |
| 438 | sctx->dec = KMC_AES_256_DECRYPT; |
| 439 | break; |
| 440 | } |
| 441 | |
| 442 | return aes_set_key(tfm, in_key, key_len); |
| 443 | } |
| 444 | |
Herbert Xu | f262f0f | 2013-11-05 19:36:27 +0800 | [diff] [blame] | 445 | static int cbc_aes_crypt(struct blkcipher_desc *desc, long func, |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 446 | struct blkcipher_walk *walk) |
| 447 | { |
Herbert Xu | f262f0f | 2013-11-05 19:36:27 +0800 | [diff] [blame] | 448 | struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm); |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 449 | int ret = blkcipher_walk_virt(desc, walk); |
| 450 | unsigned int nbytes = walk->nbytes; |
Herbert Xu | f262f0f | 2013-11-05 19:36:27 +0800 | [diff] [blame] | 451 | struct { |
| 452 | u8 iv[AES_BLOCK_SIZE]; |
| 453 | u8 key[AES_MAX_KEY_SIZE]; |
| 454 | } param; |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 455 | |
| 456 | if (!nbytes) |
| 457 | goto out; |
| 458 | |
Herbert Xu | f262f0f | 2013-11-05 19:36:27 +0800 | [diff] [blame] | 459 | memcpy(param.iv, walk->iv, AES_BLOCK_SIZE); |
| 460 | memcpy(param.key, sctx->key, sctx->key_len); |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 461 | do { |
| 462 | /* only use complete blocks */ |
| 463 | unsigned int n = nbytes & ~(AES_BLOCK_SIZE - 1); |
| 464 | u8 *out = walk->dst.virt.addr; |
| 465 | u8 *in = walk->src.virt.addr; |
| 466 | |
Herbert Xu | f262f0f | 2013-11-05 19:36:27 +0800 | [diff] [blame] | 467 | ret = crypt_s390_kmc(func, ¶m, out, in, n); |
Jan Glauber | 36eb2ca | 2012-10-26 15:06:12 +0200 | [diff] [blame] | 468 | if (ret < 0 || ret != n) |
| 469 | return -EIO; |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 470 | |
| 471 | nbytes &= AES_BLOCK_SIZE - 1; |
| 472 | ret = blkcipher_walk_done(desc, walk, nbytes); |
| 473 | } while ((nbytes = walk->nbytes)); |
Herbert Xu | f262f0f | 2013-11-05 19:36:27 +0800 | [diff] [blame] | 474 | memcpy(walk->iv, param.iv, AES_BLOCK_SIZE); |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 475 | |
| 476 | out: |
| 477 | return ret; |
| 478 | } |
| 479 | |
| 480 | static int cbc_aes_encrypt(struct blkcipher_desc *desc, |
| 481 | struct scatterlist *dst, struct scatterlist *src, |
| 482 | unsigned int nbytes) |
| 483 | { |
| 484 | struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm); |
| 485 | struct blkcipher_walk walk; |
| 486 | |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame] | 487 | if (unlikely(need_fallback(sctx->key_len))) |
| 488 | return fallback_blk_enc(desc, dst, src, nbytes); |
| 489 | |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 490 | blkcipher_walk_init(&walk, dst, src, nbytes); |
Herbert Xu | f262f0f | 2013-11-05 19:36:27 +0800 | [diff] [blame] | 491 | return cbc_aes_crypt(desc, sctx->enc, &walk); |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | static int cbc_aes_decrypt(struct blkcipher_desc *desc, |
| 495 | struct scatterlist *dst, struct scatterlist *src, |
| 496 | unsigned int nbytes) |
| 497 | { |
| 498 | struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm); |
| 499 | struct blkcipher_walk walk; |
| 500 | |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame] | 501 | if (unlikely(need_fallback(sctx->key_len))) |
| 502 | return fallback_blk_dec(desc, dst, src, nbytes); |
| 503 | |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 504 | blkcipher_walk_init(&walk, dst, src, nbytes); |
Herbert Xu | f262f0f | 2013-11-05 19:36:27 +0800 | [diff] [blame] | 505 | return cbc_aes_crypt(desc, sctx->dec, &walk); |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | static struct crypto_alg cbc_aes_alg = { |
| 509 | .cra_name = "cbc(aes)", |
| 510 | .cra_driver_name = "cbc-aes-s390", |
| 511 | .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY, |
Jan Glauber | f67d136 | 2007-05-04 18:47:47 +0200 | [diff] [blame] | 512 | .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER | |
| 513 | CRYPTO_ALG_NEED_FALLBACK, |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 514 | .cra_blocksize = AES_BLOCK_SIZE, |
| 515 | .cra_ctxsize = sizeof(struct s390_aes_ctx), |
| 516 | .cra_type = &crypto_blkcipher_type, |
| 517 | .cra_module = THIS_MODULE, |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame] | 518 | .cra_init = fallback_init_blk, |
| 519 | .cra_exit = fallback_exit_blk, |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 520 | .cra_u = { |
| 521 | .blkcipher = { |
| 522 | .min_keysize = AES_MIN_KEY_SIZE, |
| 523 | .max_keysize = AES_MAX_KEY_SIZE, |
| 524 | .ivsize = AES_BLOCK_SIZE, |
| 525 | .setkey = cbc_aes_set_key, |
| 526 | .encrypt = cbc_aes_encrypt, |
| 527 | .decrypt = cbc_aes_decrypt, |
| 528 | } |
| 529 | } |
| 530 | }; |
| 531 | |
Gerald Schaefer | 99d9722 | 2011-04-26 16:12:42 +1000 | [diff] [blame] | 532 | static int xts_fallback_setkey(struct crypto_tfm *tfm, const u8 *key, |
| 533 | unsigned int len) |
| 534 | { |
| 535 | struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm); |
| 536 | unsigned int ret; |
| 537 | |
| 538 | xts_ctx->fallback->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK; |
| 539 | xts_ctx->fallback->base.crt_flags |= (tfm->crt_flags & |
| 540 | CRYPTO_TFM_REQ_MASK); |
| 541 | |
| 542 | ret = crypto_blkcipher_setkey(xts_ctx->fallback, key, len); |
| 543 | if (ret) { |
| 544 | tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK; |
| 545 | tfm->crt_flags |= (xts_ctx->fallback->base.crt_flags & |
| 546 | CRYPTO_TFM_RES_MASK); |
| 547 | } |
| 548 | return ret; |
| 549 | } |
| 550 | |
| 551 | static int xts_fallback_decrypt(struct blkcipher_desc *desc, |
| 552 | struct scatterlist *dst, struct scatterlist *src, |
| 553 | unsigned int nbytes) |
| 554 | { |
| 555 | struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(desc->tfm); |
| 556 | struct crypto_blkcipher *tfm; |
| 557 | unsigned int ret; |
| 558 | |
| 559 | tfm = desc->tfm; |
| 560 | desc->tfm = xts_ctx->fallback; |
| 561 | |
| 562 | ret = crypto_blkcipher_decrypt_iv(desc, dst, src, nbytes); |
| 563 | |
| 564 | desc->tfm = tfm; |
| 565 | return ret; |
| 566 | } |
| 567 | |
| 568 | static int xts_fallback_encrypt(struct blkcipher_desc *desc, |
| 569 | struct scatterlist *dst, struct scatterlist *src, |
| 570 | unsigned int nbytes) |
| 571 | { |
| 572 | struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(desc->tfm); |
| 573 | struct crypto_blkcipher *tfm; |
| 574 | unsigned int ret; |
| 575 | |
| 576 | tfm = desc->tfm; |
| 577 | desc->tfm = xts_ctx->fallback; |
| 578 | |
| 579 | ret = crypto_blkcipher_encrypt_iv(desc, dst, src, nbytes); |
| 580 | |
| 581 | desc->tfm = tfm; |
| 582 | return ret; |
| 583 | } |
| 584 | |
| 585 | static int xts_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key, |
| 586 | unsigned int key_len) |
| 587 | { |
| 588 | struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm); |
| 589 | u32 *flags = &tfm->crt_flags; |
| 590 | |
| 591 | switch (key_len) { |
| 592 | case 32: |
| 593 | xts_ctx->enc = KM_XTS_128_ENCRYPT; |
| 594 | xts_ctx->dec = KM_XTS_128_DECRYPT; |
| 595 | memcpy(xts_ctx->key + 16, in_key, 16); |
Gerald Schaefer | 9dda276 | 2013-11-19 17:12:47 +0100 | [diff] [blame] | 596 | memcpy(xts_ctx->pcc_key + 16, in_key + 16, 16); |
Gerald Schaefer | 99d9722 | 2011-04-26 16:12:42 +1000 | [diff] [blame] | 597 | break; |
| 598 | case 48: |
| 599 | xts_ctx->enc = 0; |
| 600 | xts_ctx->dec = 0; |
| 601 | xts_fallback_setkey(tfm, in_key, key_len); |
| 602 | break; |
| 603 | case 64: |
| 604 | xts_ctx->enc = KM_XTS_256_ENCRYPT; |
| 605 | xts_ctx->dec = KM_XTS_256_DECRYPT; |
| 606 | memcpy(xts_ctx->key, in_key, 32); |
Gerald Schaefer | 9dda276 | 2013-11-19 17:12:47 +0100 | [diff] [blame] | 607 | memcpy(xts_ctx->pcc_key, in_key + 32, 32); |
Gerald Schaefer | 99d9722 | 2011-04-26 16:12:42 +1000 | [diff] [blame] | 608 | break; |
| 609 | default: |
| 610 | *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN; |
| 611 | return -EINVAL; |
| 612 | } |
| 613 | xts_ctx->key_len = key_len; |
| 614 | return 0; |
| 615 | } |
| 616 | |
| 617 | static int xts_aes_crypt(struct blkcipher_desc *desc, long func, |
| 618 | struct s390_xts_ctx *xts_ctx, |
| 619 | struct blkcipher_walk *walk) |
| 620 | { |
| 621 | unsigned int offset = (xts_ctx->key_len >> 1) & 0x10; |
| 622 | int ret = blkcipher_walk_virt(desc, walk); |
| 623 | unsigned int nbytes = walk->nbytes; |
| 624 | unsigned int n; |
| 625 | u8 *in, *out; |
Gerald Schaefer | 9dda276 | 2013-11-19 17:12:47 +0100 | [diff] [blame] | 626 | struct pcc_param pcc_param; |
| 627 | struct { |
| 628 | u8 key[32]; |
| 629 | u8 init[16]; |
| 630 | } xts_param; |
Gerald Schaefer | 99d9722 | 2011-04-26 16:12:42 +1000 | [diff] [blame] | 631 | |
| 632 | if (!nbytes) |
| 633 | goto out; |
| 634 | |
Gerald Schaefer | 9dda276 | 2013-11-19 17:12:47 +0100 | [diff] [blame] | 635 | memset(pcc_param.block, 0, sizeof(pcc_param.block)); |
| 636 | memset(pcc_param.bit, 0, sizeof(pcc_param.bit)); |
| 637 | memset(pcc_param.xts, 0, sizeof(pcc_param.xts)); |
| 638 | memcpy(pcc_param.tweak, walk->iv, sizeof(pcc_param.tweak)); |
| 639 | memcpy(pcc_param.key, xts_ctx->pcc_key, 32); |
| 640 | ret = crypt_s390_pcc(func, &pcc_param.key[offset]); |
Jan Glauber | 36eb2ca | 2012-10-26 15:06:12 +0200 | [diff] [blame] | 641 | if (ret < 0) |
| 642 | return -EIO; |
Gerald Schaefer | 99d9722 | 2011-04-26 16:12:42 +1000 | [diff] [blame] | 643 | |
Gerald Schaefer | 9dda276 | 2013-11-19 17:12:47 +0100 | [diff] [blame] | 644 | memcpy(xts_param.key, xts_ctx->key, 32); |
| 645 | memcpy(xts_param.init, pcc_param.xts, 16); |
Gerald Schaefer | 99d9722 | 2011-04-26 16:12:42 +1000 | [diff] [blame] | 646 | do { |
| 647 | /* only use complete blocks */ |
| 648 | n = nbytes & ~(AES_BLOCK_SIZE - 1); |
| 649 | out = walk->dst.virt.addr; |
| 650 | in = walk->src.virt.addr; |
| 651 | |
Gerald Schaefer | 9dda276 | 2013-11-19 17:12:47 +0100 | [diff] [blame] | 652 | ret = crypt_s390_km(func, &xts_param.key[offset], out, in, n); |
Jan Glauber | 36eb2ca | 2012-10-26 15:06:12 +0200 | [diff] [blame] | 653 | if (ret < 0 || ret != n) |
| 654 | return -EIO; |
Gerald Schaefer | 99d9722 | 2011-04-26 16:12:42 +1000 | [diff] [blame] | 655 | |
| 656 | nbytes &= AES_BLOCK_SIZE - 1; |
| 657 | ret = blkcipher_walk_done(desc, walk, nbytes); |
| 658 | } while ((nbytes = walk->nbytes)); |
| 659 | out: |
| 660 | return ret; |
| 661 | } |
| 662 | |
| 663 | static int xts_aes_encrypt(struct blkcipher_desc *desc, |
| 664 | struct scatterlist *dst, struct scatterlist *src, |
| 665 | unsigned int nbytes) |
| 666 | { |
| 667 | struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(desc->tfm); |
| 668 | struct blkcipher_walk walk; |
| 669 | |
| 670 | if (unlikely(xts_ctx->key_len == 48)) |
| 671 | return xts_fallback_encrypt(desc, dst, src, nbytes); |
| 672 | |
| 673 | blkcipher_walk_init(&walk, dst, src, nbytes); |
| 674 | return xts_aes_crypt(desc, xts_ctx->enc, xts_ctx, &walk); |
| 675 | } |
| 676 | |
| 677 | static int xts_aes_decrypt(struct blkcipher_desc *desc, |
| 678 | struct scatterlist *dst, struct scatterlist *src, |
| 679 | unsigned int nbytes) |
| 680 | { |
| 681 | struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(desc->tfm); |
| 682 | struct blkcipher_walk walk; |
| 683 | |
| 684 | if (unlikely(xts_ctx->key_len == 48)) |
| 685 | return xts_fallback_decrypt(desc, dst, src, nbytes); |
| 686 | |
| 687 | blkcipher_walk_init(&walk, dst, src, nbytes); |
| 688 | return xts_aes_crypt(desc, xts_ctx->dec, xts_ctx, &walk); |
| 689 | } |
| 690 | |
| 691 | static int xts_fallback_init(struct crypto_tfm *tfm) |
| 692 | { |
| 693 | const char *name = tfm->__crt_alg->cra_name; |
| 694 | struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm); |
| 695 | |
| 696 | xts_ctx->fallback = crypto_alloc_blkcipher(name, 0, |
| 697 | CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK); |
| 698 | |
| 699 | if (IS_ERR(xts_ctx->fallback)) { |
| 700 | pr_err("Allocating XTS fallback algorithm %s failed\n", |
| 701 | name); |
| 702 | return PTR_ERR(xts_ctx->fallback); |
| 703 | } |
| 704 | return 0; |
| 705 | } |
| 706 | |
| 707 | static void xts_fallback_exit(struct crypto_tfm *tfm) |
| 708 | { |
| 709 | struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm); |
| 710 | |
| 711 | crypto_free_blkcipher(xts_ctx->fallback); |
| 712 | xts_ctx->fallback = NULL; |
| 713 | } |
| 714 | |
| 715 | static struct crypto_alg xts_aes_alg = { |
| 716 | .cra_name = "xts(aes)", |
| 717 | .cra_driver_name = "xts-aes-s390", |
| 718 | .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY, |
| 719 | .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER | |
| 720 | CRYPTO_ALG_NEED_FALLBACK, |
| 721 | .cra_blocksize = AES_BLOCK_SIZE, |
| 722 | .cra_ctxsize = sizeof(struct s390_xts_ctx), |
| 723 | .cra_type = &crypto_blkcipher_type, |
| 724 | .cra_module = THIS_MODULE, |
Gerald Schaefer | 99d9722 | 2011-04-26 16:12:42 +1000 | [diff] [blame] | 725 | .cra_init = xts_fallback_init, |
| 726 | .cra_exit = xts_fallback_exit, |
| 727 | .cra_u = { |
| 728 | .blkcipher = { |
| 729 | .min_keysize = 2 * AES_MIN_KEY_SIZE, |
| 730 | .max_keysize = 2 * AES_MAX_KEY_SIZE, |
| 731 | .ivsize = AES_BLOCK_SIZE, |
| 732 | .setkey = xts_aes_set_key, |
| 733 | .encrypt = xts_aes_encrypt, |
| 734 | .decrypt = xts_aes_decrypt, |
| 735 | } |
| 736 | } |
| 737 | }; |
| 738 | |
Ingo Tuchscherer | 4f57ba7 | 2013-10-15 11:24:07 +0200 | [diff] [blame] | 739 | static int xts_aes_alg_reg; |
| 740 | |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 741 | static int ctr_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key, |
| 742 | unsigned int key_len) |
| 743 | { |
| 744 | struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm); |
| 745 | |
| 746 | switch (key_len) { |
| 747 | case 16: |
| 748 | sctx->enc = KMCTR_AES_128_ENCRYPT; |
| 749 | sctx->dec = KMCTR_AES_128_DECRYPT; |
| 750 | break; |
| 751 | case 24: |
| 752 | sctx->enc = KMCTR_AES_192_ENCRYPT; |
| 753 | sctx->dec = KMCTR_AES_192_DECRYPT; |
| 754 | break; |
| 755 | case 32: |
| 756 | sctx->enc = KMCTR_AES_256_ENCRYPT; |
| 757 | sctx->dec = KMCTR_AES_256_DECRYPT; |
| 758 | break; |
| 759 | } |
| 760 | |
| 761 | return aes_set_key(tfm, in_key, key_len); |
| 762 | } |
| 763 | |
Harald Freudenberger | 0519e9a | 2014-01-16 16:01:11 +0100 | [diff] [blame] | 764 | static unsigned int __ctrblk_init(u8 *ctrptr, unsigned int nbytes) |
| 765 | { |
| 766 | unsigned int i, n; |
| 767 | |
| 768 | /* only use complete blocks, max. PAGE_SIZE */ |
| 769 | n = (nbytes > PAGE_SIZE) ? PAGE_SIZE : nbytes & ~(AES_BLOCK_SIZE - 1); |
| 770 | for (i = AES_BLOCK_SIZE; i < n; i += AES_BLOCK_SIZE) { |
| 771 | memcpy(ctrptr + i, ctrptr + i - AES_BLOCK_SIZE, |
| 772 | AES_BLOCK_SIZE); |
| 773 | crypto_inc(ctrptr + i, AES_BLOCK_SIZE); |
| 774 | } |
| 775 | return n; |
| 776 | } |
| 777 | |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 778 | static int ctr_aes_crypt(struct blkcipher_desc *desc, long func, |
| 779 | struct s390_aes_ctx *sctx, struct blkcipher_walk *walk) |
| 780 | { |
| 781 | int ret = blkcipher_walk_virt_block(desc, walk, AES_BLOCK_SIZE); |
Harald Freudenberger | 0519e9a | 2014-01-16 16:01:11 +0100 | [diff] [blame] | 782 | unsigned int n, nbytes; |
| 783 | u8 buf[AES_BLOCK_SIZE], ctrbuf[AES_BLOCK_SIZE]; |
| 784 | u8 *out, *in, *ctrptr = ctrbuf; |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 785 | |
| 786 | if (!walk->nbytes) |
| 787 | return ret; |
| 788 | |
Harald Freudenberger | 0519e9a | 2014-01-16 16:01:11 +0100 | [diff] [blame] | 789 | if (spin_trylock(&ctrblk_lock)) |
| 790 | ctrptr = ctrblk; |
| 791 | |
| 792 | memcpy(ctrptr, walk->iv, AES_BLOCK_SIZE); |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 793 | while ((nbytes = walk->nbytes) >= AES_BLOCK_SIZE) { |
| 794 | out = walk->dst.virt.addr; |
| 795 | in = walk->src.virt.addr; |
| 796 | while (nbytes >= AES_BLOCK_SIZE) { |
Harald Freudenberger | 0519e9a | 2014-01-16 16:01:11 +0100 | [diff] [blame] | 797 | if (ctrptr == ctrblk) |
| 798 | n = __ctrblk_init(ctrptr, nbytes); |
| 799 | else |
| 800 | n = AES_BLOCK_SIZE; |
| 801 | ret = crypt_s390_kmctr(func, sctx->key, out, in, |
| 802 | n, ctrptr); |
| 803 | if (ret < 0 || ret != n) { |
| 804 | if (ctrptr == ctrblk) |
| 805 | spin_unlock(&ctrblk_lock); |
Jan Glauber | 36eb2ca | 2012-10-26 15:06:12 +0200 | [diff] [blame] | 806 | return -EIO; |
Harald Freudenberger | 0519e9a | 2014-01-16 16:01:11 +0100 | [diff] [blame] | 807 | } |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 808 | if (n > AES_BLOCK_SIZE) |
Harald Freudenberger | 0519e9a | 2014-01-16 16:01:11 +0100 | [diff] [blame] | 809 | memcpy(ctrptr, ctrptr + n - AES_BLOCK_SIZE, |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 810 | AES_BLOCK_SIZE); |
Harald Freudenberger | 0519e9a | 2014-01-16 16:01:11 +0100 | [diff] [blame] | 811 | crypto_inc(ctrptr, AES_BLOCK_SIZE); |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 812 | out += n; |
| 813 | in += n; |
| 814 | nbytes -= n; |
| 815 | } |
| 816 | ret = blkcipher_walk_done(desc, walk, nbytes); |
| 817 | } |
Harald Freudenberger | 0519e9a | 2014-01-16 16:01:11 +0100 | [diff] [blame] | 818 | if (ctrptr == ctrblk) { |
| 819 | if (nbytes) |
| 820 | memcpy(ctrbuf, ctrptr, AES_BLOCK_SIZE); |
| 821 | else |
| 822 | memcpy(walk->iv, ctrptr, AES_BLOCK_SIZE); |
| 823 | spin_unlock(&ctrblk_lock); |
Harald Freudenberger | 3901c11 | 2014-05-07 16:51:29 +0200 | [diff] [blame] | 824 | } else { |
| 825 | if (!nbytes) |
| 826 | memcpy(walk->iv, ctrptr, AES_BLOCK_SIZE); |
Harald Freudenberger | 0519e9a | 2014-01-16 16:01:11 +0100 | [diff] [blame] | 827 | } |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 828 | /* |
| 829 | * final block may be < AES_BLOCK_SIZE, copy only nbytes |
| 830 | */ |
| 831 | if (nbytes) { |
| 832 | out = walk->dst.virt.addr; |
| 833 | in = walk->src.virt.addr; |
| 834 | ret = crypt_s390_kmctr(func, sctx->key, buf, in, |
Harald Freudenberger | 0519e9a | 2014-01-16 16:01:11 +0100 | [diff] [blame] | 835 | AES_BLOCK_SIZE, ctrbuf); |
Jan Glauber | 36eb2ca | 2012-10-26 15:06:12 +0200 | [diff] [blame] | 836 | if (ret < 0 || ret != AES_BLOCK_SIZE) |
| 837 | return -EIO; |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 838 | memcpy(out, buf, nbytes); |
Harald Freudenberger | 0519e9a | 2014-01-16 16:01:11 +0100 | [diff] [blame] | 839 | crypto_inc(ctrbuf, AES_BLOCK_SIZE); |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 840 | ret = blkcipher_walk_done(desc, walk, 0); |
Harald Freudenberger | 0519e9a | 2014-01-16 16:01:11 +0100 | [diff] [blame] | 841 | memcpy(walk->iv, ctrbuf, AES_BLOCK_SIZE); |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 842 | } |
Harald Freudenberger | 0519e9a | 2014-01-16 16:01:11 +0100 | [diff] [blame] | 843 | |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 844 | return ret; |
| 845 | } |
| 846 | |
| 847 | static int ctr_aes_encrypt(struct blkcipher_desc *desc, |
| 848 | struct scatterlist *dst, struct scatterlist *src, |
| 849 | unsigned int nbytes) |
| 850 | { |
| 851 | struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm); |
| 852 | struct blkcipher_walk walk; |
| 853 | |
| 854 | blkcipher_walk_init(&walk, dst, src, nbytes); |
| 855 | return ctr_aes_crypt(desc, sctx->enc, sctx, &walk); |
| 856 | } |
| 857 | |
| 858 | static int ctr_aes_decrypt(struct blkcipher_desc *desc, |
| 859 | struct scatterlist *dst, struct scatterlist *src, |
| 860 | unsigned int nbytes) |
| 861 | { |
| 862 | struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm); |
| 863 | struct blkcipher_walk walk; |
| 864 | |
| 865 | blkcipher_walk_init(&walk, dst, src, nbytes); |
| 866 | return ctr_aes_crypt(desc, sctx->dec, sctx, &walk); |
| 867 | } |
| 868 | |
| 869 | static struct crypto_alg ctr_aes_alg = { |
| 870 | .cra_name = "ctr(aes)", |
| 871 | .cra_driver_name = "ctr-aes-s390", |
| 872 | .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY, |
| 873 | .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, |
| 874 | .cra_blocksize = 1, |
| 875 | .cra_ctxsize = sizeof(struct s390_aes_ctx), |
| 876 | .cra_type = &crypto_blkcipher_type, |
| 877 | .cra_module = THIS_MODULE, |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 878 | .cra_u = { |
| 879 | .blkcipher = { |
| 880 | .min_keysize = AES_MIN_KEY_SIZE, |
| 881 | .max_keysize = AES_MAX_KEY_SIZE, |
| 882 | .ivsize = AES_BLOCK_SIZE, |
| 883 | .setkey = ctr_aes_set_key, |
| 884 | .encrypt = ctr_aes_encrypt, |
| 885 | .decrypt = ctr_aes_decrypt, |
| 886 | } |
| 887 | } |
| 888 | }; |
| 889 | |
Ingo Tuchscherer | 4f57ba7 | 2013-10-15 11:24:07 +0200 | [diff] [blame] | 890 | static int ctr_aes_alg_reg; |
| 891 | |
Heiko Carstens | 9f7819c | 2008-04-17 07:46:17 +0200 | [diff] [blame] | 892 | static int __init aes_s390_init(void) |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 893 | { |
| 894 | int ret; |
| 895 | |
Jan Glauber | 1822bc9 | 2011-04-19 21:29:14 +0200 | [diff] [blame] | 896 | if (crypt_s390_func_available(KM_AES_128_ENCRYPT, CRYPT_S390_MSA)) |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 897 | keylen_flag |= AES_KEYLEN_128; |
Jan Glauber | 1822bc9 | 2011-04-19 21:29:14 +0200 | [diff] [blame] | 898 | if (crypt_s390_func_available(KM_AES_192_ENCRYPT, CRYPT_S390_MSA)) |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 899 | keylen_flag |= AES_KEYLEN_192; |
Jan Glauber | 1822bc9 | 2011-04-19 21:29:14 +0200 | [diff] [blame] | 900 | if (crypt_s390_func_available(KM_AES_256_ENCRYPT, CRYPT_S390_MSA)) |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 901 | keylen_flag |= AES_KEYLEN_256; |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 902 | |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 903 | if (!keylen_flag) |
| 904 | return -EOPNOTSUPP; |
| 905 | |
| 906 | /* z9 109 and z9 BC/EC only support 128 bit key length */ |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame] | 907 | if (keylen_flag == AES_KEYLEN_128) |
Jan Glauber | 39f0939 | 2008-12-25 13:39:37 +0100 | [diff] [blame] | 908 | pr_info("AES hardware acceleration is only available for" |
| 909 | " 128-bit keys\n"); |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 910 | |
| 911 | ret = crypto_register_alg(&aes_alg); |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 912 | if (ret) |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 913 | goto aes_err; |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 914 | |
| 915 | ret = crypto_register_alg(&ecb_aes_alg); |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 916 | if (ret) |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 917 | goto ecb_aes_err; |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 918 | |
| 919 | ret = crypto_register_alg(&cbc_aes_alg); |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 920 | if (ret) |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 921 | goto cbc_aes_err; |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 922 | |
Gerald Schaefer | 99d9722 | 2011-04-26 16:12:42 +1000 | [diff] [blame] | 923 | if (crypt_s390_func_available(KM_XTS_128_ENCRYPT, |
| 924 | CRYPT_S390_MSA | CRYPT_S390_MSA4) && |
| 925 | crypt_s390_func_available(KM_XTS_256_ENCRYPT, |
| 926 | CRYPT_S390_MSA | CRYPT_S390_MSA4)) { |
| 927 | ret = crypto_register_alg(&xts_aes_alg); |
| 928 | if (ret) |
| 929 | goto xts_aes_err; |
Ingo Tuchscherer | 4f57ba7 | 2013-10-15 11:24:07 +0200 | [diff] [blame] | 930 | xts_aes_alg_reg = 1; |
Gerald Schaefer | 99d9722 | 2011-04-26 16:12:42 +1000 | [diff] [blame] | 931 | } |
| 932 | |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 933 | if (crypt_s390_func_available(KMCTR_AES_128_ENCRYPT, |
| 934 | CRYPT_S390_MSA | CRYPT_S390_MSA4) && |
| 935 | crypt_s390_func_available(KMCTR_AES_192_ENCRYPT, |
| 936 | CRYPT_S390_MSA | CRYPT_S390_MSA4) && |
| 937 | crypt_s390_func_available(KMCTR_AES_256_ENCRYPT, |
| 938 | CRYPT_S390_MSA | CRYPT_S390_MSA4)) { |
| 939 | ctrblk = (u8 *) __get_free_page(GFP_KERNEL); |
| 940 | if (!ctrblk) { |
| 941 | ret = -ENOMEM; |
| 942 | goto ctr_aes_err; |
| 943 | } |
| 944 | ret = crypto_register_alg(&ctr_aes_alg); |
| 945 | if (ret) { |
| 946 | free_page((unsigned long) ctrblk); |
| 947 | goto ctr_aes_err; |
| 948 | } |
Ingo Tuchscherer | 4f57ba7 | 2013-10-15 11:24:07 +0200 | [diff] [blame] | 949 | ctr_aes_alg_reg = 1; |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 950 | } |
| 951 | |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 952 | out: |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 953 | return ret; |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 954 | |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 955 | ctr_aes_err: |
| 956 | crypto_unregister_alg(&xts_aes_alg); |
Gerald Schaefer | 99d9722 | 2011-04-26 16:12:42 +1000 | [diff] [blame] | 957 | xts_aes_err: |
| 958 | crypto_unregister_alg(&cbc_aes_alg); |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 959 | cbc_aes_err: |
| 960 | crypto_unregister_alg(&ecb_aes_alg); |
| 961 | ecb_aes_err: |
| 962 | crypto_unregister_alg(&aes_alg); |
| 963 | aes_err: |
| 964 | goto out; |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 965 | } |
| 966 | |
Heiko Carstens | 9f7819c | 2008-04-17 07:46:17 +0200 | [diff] [blame] | 967 | static void __exit aes_s390_fini(void) |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 968 | { |
Ingo Tuchscherer | 4f57ba7 | 2013-10-15 11:24:07 +0200 | [diff] [blame] | 969 | if (ctr_aes_alg_reg) { |
| 970 | crypto_unregister_alg(&ctr_aes_alg); |
| 971 | free_page((unsigned long) ctrblk); |
| 972 | } |
| 973 | if (xts_aes_alg_reg) |
| 974 | crypto_unregister_alg(&xts_aes_alg); |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 975 | crypto_unregister_alg(&cbc_aes_alg); |
| 976 | crypto_unregister_alg(&ecb_aes_alg); |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 977 | crypto_unregister_alg(&aes_alg); |
| 978 | } |
| 979 | |
Hendrik Brueckner | d05377c | 2015-02-19 17:34:07 +0100 | [diff] [blame] | 980 | module_cpu_feature_match(MSA, aes_s390_init); |
Heiko Carstens | 9f7819c | 2008-04-17 07:46:17 +0200 | [diff] [blame] | 981 | module_exit(aes_s390_fini); |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 982 | |
Kees Cook | 5d26a10 | 2014-11-20 17:05:53 -0800 | [diff] [blame] | 983 | MODULE_ALIAS_CRYPTO("aes-all"); |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 984 | |
| 985 | MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm"); |
| 986 | MODULE_LICENSE("GPL"); |