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