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