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