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: |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [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 | |
Sebastian Siewior | 89e12654 | 2007-10-17 23:18:57 +0800 | [diff] [blame] | 20 | #include <crypto/aes.h> |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 21 | #include <crypto/algapi.h> |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame^] | 22 | #include <linux/err.h> |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 23 | #include <linux/module.h> |
| 24 | #include <linux/init.h> |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 25 | #include "crypt_s390.h" |
| 26 | |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 27 | #define AES_KEYLEN_128 1 |
| 28 | #define AES_KEYLEN_192 2 |
| 29 | #define AES_KEYLEN_256 4 |
| 30 | |
| 31 | static char keylen_flag = 0; |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 32 | |
| 33 | struct s390_aes_ctx { |
| 34 | u8 iv[AES_BLOCK_SIZE]; |
| 35 | u8 key[AES_MAX_KEY_SIZE]; |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 36 | long enc; |
| 37 | long dec; |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 38 | int key_len; |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame^] | 39 | union { |
| 40 | struct crypto_blkcipher *blk; |
| 41 | struct crypto_cipher *cip; |
| 42 | } fallback; |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 43 | }; |
| 44 | |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame^] | 45 | /* |
| 46 | * Check if the key_len is supported by the HW. |
| 47 | * Returns 0 if it is, a positive number if it is not and software fallback is |
| 48 | * required or a negative number in case the key size is not valid |
| 49 | */ |
| 50 | static int need_fallback(unsigned int key_len) |
| 51 | { |
| 52 | switch (key_len) { |
| 53 | case 16: |
| 54 | if (!(keylen_flag & AES_KEYLEN_128)) |
| 55 | return 1; |
| 56 | break; |
| 57 | case 24: |
| 58 | if (!(keylen_flag & AES_KEYLEN_192)) |
| 59 | return 1; |
| 60 | break; |
| 61 | case 32: |
| 62 | if (!(keylen_flag & AES_KEYLEN_256)) |
| 63 | return 1; |
| 64 | break; |
| 65 | default: |
| 66 | return -1; |
| 67 | break; |
| 68 | } |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | static int setkey_fallback_cip(struct crypto_tfm *tfm, const u8 *in_key, |
| 73 | unsigned int key_len) |
| 74 | { |
| 75 | struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm); |
| 76 | int ret; |
| 77 | |
| 78 | sctx->fallback.blk->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK; |
| 79 | sctx->fallback.blk->base.crt_flags |= (tfm->crt_flags & |
| 80 | CRYPTO_TFM_REQ_MASK); |
| 81 | |
| 82 | ret = crypto_cipher_setkey(sctx->fallback.cip, in_key, key_len); |
| 83 | if (ret) { |
| 84 | tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK; |
| 85 | tfm->crt_flags |= (sctx->fallback.blk->base.crt_flags & |
| 86 | CRYPTO_TFM_RES_MASK); |
| 87 | } |
| 88 | return ret; |
| 89 | } |
| 90 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 91 | 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] | 92 | unsigned int key_len) |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 93 | { |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 94 | struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm); |
Herbert Xu | 560c06a | 2006-08-13 14:16:39 +1000 | [diff] [blame] | 95 | u32 *flags = &tfm->crt_flags; |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame^] | 96 | int ret; |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 97 | |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame^] | 98 | ret = need_fallback(key_len); |
| 99 | if (ret < 0) { |
| 100 | *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN; |
| 101 | return -EINVAL; |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | sctx->key_len = key_len; |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame^] | 105 | if (!ret) { |
| 106 | memcpy(sctx->key, in_key, key_len); |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | return setkey_fallback_cip(tfm, in_key, key_len); |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 113 | 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] | 114 | { |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 115 | const struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm); |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 116 | |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame^] | 117 | if (unlikely(need_fallback(sctx->key_len))) { |
| 118 | crypto_cipher_encrypt_one(sctx->fallback.cip, out, in); |
| 119 | return; |
| 120 | } |
| 121 | |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 122 | switch (sctx->key_len) { |
| 123 | case 16: |
| 124 | crypt_s390_km(KM_AES_128_ENCRYPT, &sctx->key, out, in, |
| 125 | AES_BLOCK_SIZE); |
| 126 | break; |
| 127 | case 24: |
| 128 | crypt_s390_km(KM_AES_192_ENCRYPT, &sctx->key, out, in, |
| 129 | AES_BLOCK_SIZE); |
| 130 | break; |
| 131 | case 32: |
| 132 | crypt_s390_km(KM_AES_256_ENCRYPT, &sctx->key, out, in, |
| 133 | AES_BLOCK_SIZE); |
| 134 | break; |
| 135 | } |
| 136 | } |
| 137 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 138 | 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] | 139 | { |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 140 | const struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm); |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 141 | |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame^] | 142 | if (unlikely(need_fallback(sctx->key_len))) { |
| 143 | crypto_cipher_decrypt_one(sctx->fallback.cip, out, in); |
| 144 | return; |
| 145 | } |
| 146 | |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 147 | switch (sctx->key_len) { |
| 148 | case 16: |
| 149 | crypt_s390_km(KM_AES_128_DECRYPT, &sctx->key, out, in, |
| 150 | AES_BLOCK_SIZE); |
| 151 | break; |
| 152 | case 24: |
| 153 | crypt_s390_km(KM_AES_192_DECRYPT, &sctx->key, out, in, |
| 154 | AES_BLOCK_SIZE); |
| 155 | break; |
| 156 | case 32: |
| 157 | crypt_s390_km(KM_AES_256_DECRYPT, &sctx->key, out, in, |
| 158 | AES_BLOCK_SIZE); |
| 159 | break; |
| 160 | } |
| 161 | } |
| 162 | |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame^] | 163 | static int fallback_init_cip(struct crypto_tfm *tfm) |
| 164 | { |
| 165 | const char *name = tfm->__crt_alg->cra_name; |
| 166 | struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm); |
| 167 | |
| 168 | sctx->fallback.cip = crypto_alloc_cipher(name, 0, |
| 169 | CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK); |
| 170 | |
| 171 | if (IS_ERR(sctx->fallback.cip)) { |
| 172 | printk(KERN_ERR "Error allocating fallback algo %s\n", name); |
| 173 | return PTR_ERR(sctx->fallback.blk); |
| 174 | } |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | static void fallback_exit_cip(struct crypto_tfm *tfm) |
| 180 | { |
| 181 | struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm); |
| 182 | |
| 183 | crypto_free_cipher(sctx->fallback.cip); |
| 184 | sctx->fallback.cip = NULL; |
| 185 | } |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 186 | |
| 187 | static struct crypto_alg aes_alg = { |
| 188 | .cra_name = "aes", |
Herbert Xu | 65b75c3 | 2006-08-21 21:18:50 +1000 | [diff] [blame] | 189 | .cra_driver_name = "aes-s390", |
| 190 | .cra_priority = CRYPT_S390_PRIORITY, |
Jan Glauber | f67d136 | 2007-05-04 18:47:47 +0200 | [diff] [blame] | 191 | .cra_flags = CRYPTO_ALG_TYPE_CIPHER | |
| 192 | CRYPTO_ALG_NEED_FALLBACK, |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 193 | .cra_blocksize = AES_BLOCK_SIZE, |
| 194 | .cra_ctxsize = sizeof(struct s390_aes_ctx), |
| 195 | .cra_module = THIS_MODULE, |
| 196 | .cra_list = LIST_HEAD_INIT(aes_alg.cra_list), |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame^] | 197 | .cra_init = fallback_init_cip, |
| 198 | .cra_exit = fallback_exit_cip, |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 199 | .cra_u = { |
| 200 | .cipher = { |
| 201 | .cia_min_keysize = AES_MIN_KEY_SIZE, |
| 202 | .cia_max_keysize = AES_MAX_KEY_SIZE, |
| 203 | .cia_setkey = aes_set_key, |
| 204 | .cia_encrypt = aes_encrypt, |
| 205 | .cia_decrypt = aes_decrypt, |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | }; |
| 209 | |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame^] | 210 | static int setkey_fallback_blk(struct crypto_tfm *tfm, const u8 *key, |
| 211 | unsigned int len) |
| 212 | { |
| 213 | struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm); |
| 214 | unsigned int ret; |
| 215 | |
| 216 | sctx->fallback.blk->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK; |
| 217 | sctx->fallback.blk->base.crt_flags |= (tfm->crt_flags & |
| 218 | CRYPTO_TFM_REQ_MASK); |
| 219 | |
| 220 | ret = crypto_blkcipher_setkey(sctx->fallback.blk, key, len); |
| 221 | if (ret) { |
| 222 | tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK; |
| 223 | tfm->crt_flags |= (sctx->fallback.blk->base.crt_flags & |
| 224 | CRYPTO_TFM_RES_MASK); |
| 225 | } |
| 226 | return ret; |
| 227 | } |
| 228 | |
| 229 | static int fallback_blk_dec(struct blkcipher_desc *desc, |
| 230 | struct scatterlist *dst, struct scatterlist *src, |
| 231 | unsigned int nbytes) |
| 232 | { |
| 233 | unsigned int ret; |
| 234 | struct crypto_blkcipher *tfm; |
| 235 | struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm); |
| 236 | |
| 237 | memcpy(crypto_blkcipher_crt(sctx->fallback.blk)->iv, desc->info, |
| 238 | AES_BLOCK_SIZE); |
| 239 | |
| 240 | tfm = desc->tfm; |
| 241 | desc->tfm = sctx->fallback.blk; |
| 242 | |
| 243 | ret = crypto_blkcipher_decrypt(desc, dst, src, nbytes); |
| 244 | |
| 245 | desc->tfm = tfm; |
| 246 | return ret; |
| 247 | } |
| 248 | |
| 249 | static int fallback_blk_enc(struct blkcipher_desc *desc, |
| 250 | struct scatterlist *dst, struct scatterlist *src, |
| 251 | unsigned int nbytes) |
| 252 | { |
| 253 | unsigned int ret; |
| 254 | struct crypto_blkcipher *tfm; |
| 255 | struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm); |
| 256 | |
| 257 | memcpy(crypto_blkcipher_crt(sctx->fallback.blk)->iv, desc->info, |
| 258 | AES_BLOCK_SIZE); |
| 259 | |
| 260 | tfm = desc->tfm; |
| 261 | desc->tfm = sctx->fallback.blk; |
| 262 | |
| 263 | ret = crypto_blkcipher_encrypt(desc, dst, src, nbytes); |
| 264 | |
| 265 | desc->tfm = tfm; |
| 266 | return ret; |
| 267 | } |
| 268 | |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 269 | static int ecb_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key, |
| 270 | unsigned int key_len) |
| 271 | { |
| 272 | struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm); |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame^] | 273 | int ret; |
| 274 | |
| 275 | ret = need_fallback(key_len); |
| 276 | if (ret > 0) { |
| 277 | sctx->key_len = key_len; |
| 278 | return setkey_fallback_blk(tfm, in_key, key_len); |
| 279 | } |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 280 | |
| 281 | switch (key_len) { |
| 282 | case 16: |
| 283 | sctx->enc = KM_AES_128_ENCRYPT; |
| 284 | sctx->dec = KM_AES_128_DECRYPT; |
| 285 | break; |
| 286 | case 24: |
| 287 | sctx->enc = KM_AES_192_ENCRYPT; |
| 288 | sctx->dec = KM_AES_192_DECRYPT; |
| 289 | break; |
| 290 | case 32: |
| 291 | sctx->enc = KM_AES_256_ENCRYPT; |
| 292 | sctx->dec = KM_AES_256_DECRYPT; |
| 293 | break; |
| 294 | } |
| 295 | |
| 296 | return aes_set_key(tfm, in_key, key_len); |
| 297 | } |
| 298 | |
| 299 | static int ecb_aes_crypt(struct blkcipher_desc *desc, long func, void *param, |
| 300 | struct blkcipher_walk *walk) |
| 301 | { |
| 302 | int ret = blkcipher_walk_virt(desc, walk); |
| 303 | unsigned int nbytes; |
| 304 | |
| 305 | while ((nbytes = walk->nbytes)) { |
| 306 | /* only use complete blocks */ |
| 307 | unsigned int n = nbytes & ~(AES_BLOCK_SIZE - 1); |
| 308 | u8 *out = walk->dst.virt.addr; |
| 309 | u8 *in = walk->src.virt.addr; |
| 310 | |
| 311 | ret = crypt_s390_km(func, param, out, in, n); |
| 312 | BUG_ON((ret < 0) || (ret != n)); |
| 313 | |
| 314 | nbytes &= AES_BLOCK_SIZE - 1; |
| 315 | ret = blkcipher_walk_done(desc, walk, nbytes); |
| 316 | } |
| 317 | |
| 318 | return ret; |
| 319 | } |
| 320 | |
| 321 | static int ecb_aes_encrypt(struct blkcipher_desc *desc, |
| 322 | struct scatterlist *dst, struct scatterlist *src, |
| 323 | unsigned int nbytes) |
| 324 | { |
| 325 | struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm); |
| 326 | struct blkcipher_walk walk; |
| 327 | |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame^] | 328 | if (unlikely(need_fallback(sctx->key_len))) |
| 329 | return fallback_blk_enc(desc, dst, src, nbytes); |
| 330 | |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 331 | blkcipher_walk_init(&walk, dst, src, nbytes); |
| 332 | return ecb_aes_crypt(desc, sctx->enc, sctx->key, &walk); |
| 333 | } |
| 334 | |
| 335 | static int ecb_aes_decrypt(struct blkcipher_desc *desc, |
| 336 | struct scatterlist *dst, struct scatterlist *src, |
| 337 | unsigned int nbytes) |
| 338 | { |
| 339 | struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm); |
| 340 | struct blkcipher_walk walk; |
| 341 | |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame^] | 342 | if (unlikely(need_fallback(sctx->key_len))) |
| 343 | return fallback_blk_dec(desc, dst, src, nbytes); |
| 344 | |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 345 | blkcipher_walk_init(&walk, dst, src, nbytes); |
| 346 | return ecb_aes_crypt(desc, sctx->dec, sctx->key, &walk); |
| 347 | } |
| 348 | |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame^] | 349 | static int fallback_init_blk(struct crypto_tfm *tfm) |
| 350 | { |
| 351 | const char *name = tfm->__crt_alg->cra_name; |
| 352 | struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm); |
| 353 | |
| 354 | sctx->fallback.blk = crypto_alloc_blkcipher(name, 0, |
| 355 | CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK); |
| 356 | |
| 357 | if (IS_ERR(sctx->fallback.blk)) { |
| 358 | printk(KERN_ERR "Error allocating fallback algo %s\n", name); |
| 359 | return PTR_ERR(sctx->fallback.blk); |
| 360 | } |
| 361 | |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | static void fallback_exit_blk(struct crypto_tfm *tfm) |
| 366 | { |
| 367 | struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm); |
| 368 | |
| 369 | crypto_free_blkcipher(sctx->fallback.blk); |
| 370 | sctx->fallback.blk = NULL; |
| 371 | } |
| 372 | |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 373 | static struct crypto_alg ecb_aes_alg = { |
| 374 | .cra_name = "ecb(aes)", |
| 375 | .cra_driver_name = "ecb-aes-s390", |
| 376 | .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY, |
Jan Glauber | f67d136 | 2007-05-04 18:47:47 +0200 | [diff] [blame] | 377 | .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER | |
| 378 | CRYPTO_ALG_NEED_FALLBACK, |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 379 | .cra_blocksize = AES_BLOCK_SIZE, |
| 380 | .cra_ctxsize = sizeof(struct s390_aes_ctx), |
| 381 | .cra_type = &crypto_blkcipher_type, |
| 382 | .cra_module = THIS_MODULE, |
| 383 | .cra_list = LIST_HEAD_INIT(ecb_aes_alg.cra_list), |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame^] | 384 | .cra_init = fallback_init_blk, |
| 385 | .cra_exit = fallback_exit_blk, |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 386 | .cra_u = { |
| 387 | .blkcipher = { |
| 388 | .min_keysize = AES_MIN_KEY_SIZE, |
| 389 | .max_keysize = AES_MAX_KEY_SIZE, |
| 390 | .setkey = ecb_aes_set_key, |
| 391 | .encrypt = ecb_aes_encrypt, |
| 392 | .decrypt = ecb_aes_decrypt, |
| 393 | } |
| 394 | } |
| 395 | }; |
| 396 | |
| 397 | static int cbc_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key, |
| 398 | unsigned int key_len) |
| 399 | { |
| 400 | struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm); |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame^] | 401 | int ret; |
| 402 | |
| 403 | ret = need_fallback(key_len); |
| 404 | if (ret > 0) { |
| 405 | sctx->key_len = key_len; |
| 406 | return setkey_fallback_blk(tfm, in_key, key_len); |
| 407 | } |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 408 | |
| 409 | switch (key_len) { |
| 410 | case 16: |
| 411 | sctx->enc = KMC_AES_128_ENCRYPT; |
| 412 | sctx->dec = KMC_AES_128_DECRYPT; |
| 413 | break; |
| 414 | case 24: |
| 415 | sctx->enc = KMC_AES_192_ENCRYPT; |
| 416 | sctx->dec = KMC_AES_192_DECRYPT; |
| 417 | break; |
| 418 | case 32: |
| 419 | sctx->enc = KMC_AES_256_ENCRYPT; |
| 420 | sctx->dec = KMC_AES_256_DECRYPT; |
| 421 | break; |
| 422 | } |
| 423 | |
| 424 | return aes_set_key(tfm, in_key, key_len); |
| 425 | } |
| 426 | |
| 427 | static int cbc_aes_crypt(struct blkcipher_desc *desc, long func, void *param, |
| 428 | struct blkcipher_walk *walk) |
| 429 | { |
| 430 | int ret = blkcipher_walk_virt(desc, walk); |
| 431 | unsigned int nbytes = walk->nbytes; |
| 432 | |
| 433 | if (!nbytes) |
| 434 | goto out; |
| 435 | |
| 436 | memcpy(param, walk->iv, AES_BLOCK_SIZE); |
| 437 | do { |
| 438 | /* only use complete blocks */ |
| 439 | unsigned int n = nbytes & ~(AES_BLOCK_SIZE - 1); |
| 440 | u8 *out = walk->dst.virt.addr; |
| 441 | u8 *in = walk->src.virt.addr; |
| 442 | |
| 443 | ret = crypt_s390_kmc(func, param, out, in, n); |
| 444 | BUG_ON((ret < 0) || (ret != n)); |
| 445 | |
| 446 | nbytes &= AES_BLOCK_SIZE - 1; |
| 447 | ret = blkcipher_walk_done(desc, walk, nbytes); |
| 448 | } while ((nbytes = walk->nbytes)); |
| 449 | memcpy(walk->iv, param, AES_BLOCK_SIZE); |
| 450 | |
| 451 | out: |
| 452 | return ret; |
| 453 | } |
| 454 | |
| 455 | static int cbc_aes_encrypt(struct blkcipher_desc *desc, |
| 456 | struct scatterlist *dst, struct scatterlist *src, |
| 457 | unsigned int nbytes) |
| 458 | { |
| 459 | struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm); |
| 460 | struct blkcipher_walk walk; |
| 461 | |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame^] | 462 | if (unlikely(need_fallback(sctx->key_len))) |
| 463 | return fallback_blk_enc(desc, dst, src, nbytes); |
| 464 | |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 465 | blkcipher_walk_init(&walk, dst, src, nbytes); |
| 466 | return cbc_aes_crypt(desc, sctx->enc, sctx->iv, &walk); |
| 467 | } |
| 468 | |
| 469 | static int cbc_aes_decrypt(struct blkcipher_desc *desc, |
| 470 | struct scatterlist *dst, struct scatterlist *src, |
| 471 | unsigned int nbytes) |
| 472 | { |
| 473 | struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm); |
| 474 | struct blkcipher_walk walk; |
| 475 | |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame^] | 476 | if (unlikely(need_fallback(sctx->key_len))) |
| 477 | return fallback_blk_dec(desc, dst, src, nbytes); |
| 478 | |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 479 | blkcipher_walk_init(&walk, dst, src, nbytes); |
| 480 | return cbc_aes_crypt(desc, sctx->dec, sctx->iv, &walk); |
| 481 | } |
| 482 | |
| 483 | static struct crypto_alg cbc_aes_alg = { |
| 484 | .cra_name = "cbc(aes)", |
| 485 | .cra_driver_name = "cbc-aes-s390", |
| 486 | .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY, |
Jan Glauber | f67d136 | 2007-05-04 18:47:47 +0200 | [diff] [blame] | 487 | .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER | |
| 488 | CRYPTO_ALG_NEED_FALLBACK, |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 489 | .cra_blocksize = AES_BLOCK_SIZE, |
| 490 | .cra_ctxsize = sizeof(struct s390_aes_ctx), |
| 491 | .cra_type = &crypto_blkcipher_type, |
| 492 | .cra_module = THIS_MODULE, |
| 493 | .cra_list = LIST_HEAD_INIT(cbc_aes_alg.cra_list), |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame^] | 494 | .cra_init = fallback_init_blk, |
| 495 | .cra_exit = fallback_exit_blk, |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 496 | .cra_u = { |
| 497 | .blkcipher = { |
| 498 | .min_keysize = AES_MIN_KEY_SIZE, |
| 499 | .max_keysize = AES_MAX_KEY_SIZE, |
| 500 | .ivsize = AES_BLOCK_SIZE, |
| 501 | .setkey = cbc_aes_set_key, |
| 502 | .encrypt = cbc_aes_encrypt, |
| 503 | .decrypt = cbc_aes_decrypt, |
| 504 | } |
| 505 | } |
| 506 | }; |
| 507 | |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 508 | static int __init aes_init(void) |
| 509 | { |
| 510 | int ret; |
| 511 | |
| 512 | if (crypt_s390_func_available(KM_AES_128_ENCRYPT)) |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 513 | keylen_flag |= AES_KEYLEN_128; |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 514 | if (crypt_s390_func_available(KM_AES_192_ENCRYPT)) |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 515 | keylen_flag |= AES_KEYLEN_192; |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 516 | if (crypt_s390_func_available(KM_AES_256_ENCRYPT)) |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 517 | keylen_flag |= AES_KEYLEN_256; |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 518 | |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 519 | if (!keylen_flag) |
| 520 | return -EOPNOTSUPP; |
| 521 | |
| 522 | /* z9 109 and z9 BC/EC only support 128 bit key length */ |
Sebastian Siewior | b0c3e75 | 2007-12-01 12:47:37 +1100 | [diff] [blame^] | 523 | if (keylen_flag == AES_KEYLEN_128) |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 524 | printk(KERN_INFO |
| 525 | "aes_s390: hardware acceleration only available for" |
| 526 | "128 bit keys\n"); |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 527 | |
| 528 | ret = crypto_register_alg(&aes_alg); |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 529 | if (ret) |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 530 | goto aes_err; |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 531 | |
| 532 | ret = crypto_register_alg(&ecb_aes_alg); |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 533 | if (ret) |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 534 | goto ecb_aes_err; |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 535 | |
| 536 | ret = crypto_register_alg(&cbc_aes_alg); |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 537 | if (ret) |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 538 | goto cbc_aes_err; |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 539 | |
| 540 | out: |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 541 | return ret; |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 542 | |
| 543 | cbc_aes_err: |
| 544 | crypto_unregister_alg(&ecb_aes_alg); |
| 545 | ecb_aes_err: |
| 546 | crypto_unregister_alg(&aes_alg); |
| 547 | aes_err: |
| 548 | goto out; |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | static void __exit aes_fini(void) |
| 552 | { |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 553 | crypto_unregister_alg(&cbc_aes_alg); |
| 554 | crypto_unregister_alg(&ecb_aes_alg); |
Jan Glauber | bf754ae | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 555 | crypto_unregister_alg(&aes_alg); |
| 556 | } |
| 557 | |
| 558 | module_init(aes_init); |
| 559 | module_exit(aes_fini); |
| 560 | |
| 561 | MODULE_ALIAS("aes"); |
| 562 | |
| 563 | MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm"); |
| 564 | MODULE_LICENSE("GPL"); |