Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 1 | /* |
| 2 | * CTS: Cipher Text Stealing mode |
| 3 | * |
| 4 | * COPYRIGHT (c) 2008 |
| 5 | * The Regents of the University of Michigan |
| 6 | * ALL RIGHTS RESERVED |
| 7 | * |
| 8 | * Permission is granted to use, copy, create derivative works |
| 9 | * and redistribute this software and such derivative works |
| 10 | * for any purpose, so long as the name of The University of |
| 11 | * Michigan is not used in any advertising or publicity |
| 12 | * pertaining to the use of distribution of this software |
| 13 | * without specific, written prior authorization. If the |
| 14 | * above copyright notice or any other identification of the |
| 15 | * University of Michigan is included in any copy of any |
| 16 | * portion of this software, then the disclaimer below must |
| 17 | * also be included. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION |
| 20 | * FROM THE UNIVERSITY OF MICHIGAN AS TO ITS FITNESS FOR ANY |
| 21 | * PURPOSE, AND WITHOUT WARRANTY BY THE UNIVERSITY OF |
| 22 | * MICHIGAN OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING |
| 23 | * WITHOUT LIMITATION THE IMPLIED WARRANTIES OF |
| 24 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE |
| 25 | * REGENTS OF THE UNIVERSITY OF MICHIGAN SHALL NOT BE LIABLE |
| 26 | * FOR ANY DAMAGES, INCLUDING SPECIAL, INDIRECT, INCIDENTAL, OR |
| 27 | * CONSEQUENTIAL DAMAGES, WITH RESPECT TO ANY CLAIM ARISING |
| 28 | * OUT OF OR IN CONNECTION WITH THE USE OF THE SOFTWARE, EVEN |
| 29 | * IF IT HAS BEEN OR IS HEREAFTER ADVISED OF THE POSSIBILITY OF |
| 30 | * SUCH DAMAGES. |
| 31 | */ |
| 32 | |
| 33 | /* Derived from various: |
| 34 | * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au> |
| 35 | */ |
| 36 | |
| 37 | /* |
| 38 | * This is the Cipher Text Stealing mode as described by |
| 39 | * Section 8 of rfc2040 and referenced by rfc3962. |
| 40 | * rfc3962 includes errata information in its Appendix A. |
| 41 | */ |
| 42 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 43 | #include <crypto/internal/skcipher.h> |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 44 | #include <linux/err.h> |
| 45 | #include <linux/init.h> |
| 46 | #include <linux/kernel.h> |
| 47 | #include <linux/log2.h> |
| 48 | #include <linux/module.h> |
| 49 | #include <linux/scatterlist.h> |
| 50 | #include <crypto/scatterwalk.h> |
| 51 | #include <linux/slab.h> |
Gideon Israel Dsouza | d8c34b9 | 2016-12-31 21:26:23 +0530 | [diff] [blame] | 52 | #include <linux/compiler.h> |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 53 | |
| 54 | struct crypto_cts_ctx { |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 55 | struct crypto_skcipher *child; |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 56 | }; |
| 57 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 58 | struct crypto_cts_reqctx { |
| 59 | struct scatterlist sg[2]; |
| 60 | unsigned offset; |
| 61 | struct skcipher_request subreq; |
| 62 | }; |
| 63 | |
| 64 | static inline u8 *crypto_cts_reqctx_space(struct skcipher_request *req) |
| 65 | { |
| 66 | struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req); |
| 67 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 68 | struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 69 | struct crypto_skcipher *child = ctx->child; |
| 70 | |
| 71 | return PTR_ALIGN((u8 *)(rctx + 1) + crypto_skcipher_reqsize(child), |
| 72 | crypto_skcipher_alignmask(tfm) + 1); |
| 73 | } |
| 74 | |
| 75 | static int crypto_cts_setkey(struct crypto_skcipher *parent, const u8 *key, |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 76 | unsigned int keylen) |
| 77 | { |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 78 | struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(parent); |
| 79 | struct crypto_skcipher *child = ctx->child; |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 80 | int err; |
| 81 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 82 | crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK); |
| 83 | crypto_skcipher_set_flags(child, crypto_skcipher_get_flags(parent) & |
| 84 | CRYPTO_TFM_REQ_MASK); |
| 85 | err = crypto_skcipher_setkey(child, key, keylen); |
| 86 | crypto_skcipher_set_flags(parent, crypto_skcipher_get_flags(child) & |
| 87 | CRYPTO_TFM_RES_MASK); |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 88 | return err; |
| 89 | } |
| 90 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 91 | static void cts_cbc_crypt_done(struct crypto_async_request *areq, int err) |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 92 | { |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 93 | struct skcipher_request *req = areq->data; |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 94 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 95 | if (err == -EINPROGRESS) |
| 96 | return; |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 97 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 98 | skcipher_request_complete(req, err); |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 99 | } |
| 100 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 101 | static int cts_cbc_encrypt(struct skcipher_request *req) |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 102 | { |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 103 | struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req); |
| 104 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 105 | struct skcipher_request *subreq = &rctx->subreq; |
| 106 | int bsize = crypto_skcipher_blocksize(tfm); |
Gideon Israel Dsouza | d8c34b9 | 2016-12-31 21:26:23 +0530 | [diff] [blame] | 107 | u8 d[bsize * 2] __aligned(__alignof__(u32)); |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 108 | struct scatterlist *sg; |
| 109 | unsigned int offset; |
| 110 | int lastn; |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 111 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 112 | offset = rctx->offset; |
| 113 | lastn = req->cryptlen - offset; |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 114 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 115 | sg = scatterwalk_ffwd(rctx->sg, req->dst, offset - bsize); |
| 116 | scatterwalk_map_and_copy(d + bsize, sg, 0, bsize, 0); |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 117 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 118 | memset(d, 0, bsize); |
| 119 | scatterwalk_map_and_copy(d, req->src, offset, lastn, 0); |
| 120 | |
| 121 | scatterwalk_map_and_copy(d, sg, 0, bsize + lastn, 1); |
| 122 | memzero_explicit(d, sizeof(d)); |
| 123 | |
| 124 | skcipher_request_set_callback(subreq, req->base.flags & |
| 125 | CRYPTO_TFM_REQ_MAY_BACKLOG, |
| 126 | cts_cbc_crypt_done, req); |
| 127 | skcipher_request_set_crypt(subreq, sg, sg, bsize, req->iv); |
| 128 | return crypto_skcipher_encrypt(subreq); |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 129 | } |
| 130 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 131 | static void crypto_cts_encrypt_done(struct crypto_async_request *areq, int err) |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 132 | { |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 133 | struct skcipher_request *req = areq->data; |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 134 | |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 135 | if (err) |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 136 | goto out; |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 137 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 138 | err = cts_cbc_encrypt(req); |
| 139 | if (err == -EINPROGRESS || |
| 140 | (err == -EBUSY && req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) |
| 141 | return; |
Daniel Borkmann | 7185ad2 | 2014-09-07 23:23:38 +0200 | [diff] [blame] | 142 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 143 | out: |
| 144 | skcipher_request_complete(req, err); |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 145 | } |
| 146 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 147 | static int crypto_cts_encrypt(struct skcipher_request *req) |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 148 | { |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 149 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 150 | struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req); |
| 151 | struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 152 | struct skcipher_request *subreq = &rctx->subreq; |
| 153 | int bsize = crypto_skcipher_blocksize(tfm); |
| 154 | unsigned int nbytes = req->cryptlen; |
| 155 | int cbc_blocks = (nbytes + bsize - 1) / bsize - 1; |
| 156 | unsigned int offset; |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 157 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 158 | skcipher_request_set_tfm(subreq, ctx->child); |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 159 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 160 | if (cbc_blocks <= 0) { |
| 161 | skcipher_request_set_callback(subreq, req->base.flags, |
| 162 | req->base.complete, |
| 163 | req->base.data); |
| 164 | skcipher_request_set_crypt(subreq, req->src, req->dst, nbytes, |
| 165 | req->iv); |
| 166 | return crypto_skcipher_encrypt(subreq); |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 167 | } |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 168 | |
| 169 | offset = cbc_blocks * bsize; |
| 170 | rctx->offset = offset; |
| 171 | |
| 172 | skcipher_request_set_callback(subreq, req->base.flags, |
| 173 | crypto_cts_encrypt_done, req); |
| 174 | skcipher_request_set_crypt(subreq, req->src, req->dst, |
| 175 | offset, req->iv); |
| 176 | |
| 177 | return crypto_skcipher_encrypt(subreq) ?: |
| 178 | cts_cbc_encrypt(req); |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 179 | } |
| 180 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 181 | static int cts_cbc_decrypt(struct skcipher_request *req) |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 182 | { |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 183 | struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req); |
| 184 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 185 | struct skcipher_request *subreq = &rctx->subreq; |
| 186 | int bsize = crypto_skcipher_blocksize(tfm); |
Gideon Israel Dsouza | d8c34b9 | 2016-12-31 21:26:23 +0530 | [diff] [blame] | 187 | u8 d[bsize * 2] __aligned(__alignof__(u32)); |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 188 | struct scatterlist *sg; |
| 189 | unsigned int offset; |
| 190 | u8 *space; |
| 191 | int lastn; |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 192 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 193 | offset = rctx->offset; |
| 194 | lastn = req->cryptlen - offset; |
| 195 | |
| 196 | sg = scatterwalk_ffwd(rctx->sg, req->dst, offset - bsize); |
| 197 | |
| 198 | /* 1. Decrypt Cn-1 (s) to create Dn */ |
| 199 | scatterwalk_map_and_copy(d + bsize, sg, 0, bsize, 0); |
| 200 | space = crypto_cts_reqctx_space(req); |
| 201 | crypto_xor(d + bsize, space, bsize); |
| 202 | /* 2. Pad Cn with zeros at the end to create C of length BB */ |
| 203 | memset(d, 0, bsize); |
| 204 | scatterwalk_map_and_copy(d, req->src, offset, lastn, 0); |
| 205 | /* 3. Exclusive-or Dn with C to create Xn */ |
| 206 | /* 4. Select the first Ln bytes of Xn to create Pn */ |
| 207 | crypto_xor(d + bsize, d, lastn); |
| 208 | |
| 209 | /* 5. Append the tail (BB - Ln) bytes of Xn to Cn to create En */ |
| 210 | memcpy(d + lastn, d + bsize + lastn, bsize - lastn); |
| 211 | /* 6. Decrypt En to create Pn-1 */ |
| 212 | |
| 213 | scatterwalk_map_and_copy(d, sg, 0, bsize + lastn, 1); |
| 214 | memzero_explicit(d, sizeof(d)); |
| 215 | |
| 216 | skcipher_request_set_callback(subreq, req->base.flags & |
| 217 | CRYPTO_TFM_REQ_MAY_BACKLOG, |
| 218 | cts_cbc_crypt_done, req); |
| 219 | |
| 220 | skcipher_request_set_crypt(subreq, sg, sg, bsize, space); |
| 221 | return crypto_skcipher_decrypt(subreq); |
| 222 | } |
| 223 | |
| 224 | static void crypto_cts_decrypt_done(struct crypto_async_request *areq, int err) |
| 225 | { |
| 226 | struct skcipher_request *req = areq->data; |
| 227 | |
| 228 | if (err) |
| 229 | goto out; |
| 230 | |
| 231 | err = cts_cbc_decrypt(req); |
| 232 | if (err == -EINPROGRESS || |
| 233 | (err == -EBUSY && req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) |
| 234 | return; |
| 235 | |
| 236 | out: |
| 237 | skcipher_request_complete(req, err); |
| 238 | } |
| 239 | |
| 240 | static int crypto_cts_decrypt(struct skcipher_request *req) |
| 241 | { |
| 242 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 243 | struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req); |
| 244 | struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 245 | struct skcipher_request *subreq = &rctx->subreq; |
| 246 | int bsize = crypto_skcipher_blocksize(tfm); |
| 247 | unsigned int nbytes = req->cryptlen; |
| 248 | int cbc_blocks = (nbytes + bsize - 1) / bsize - 1; |
| 249 | unsigned int offset; |
| 250 | u8 *space; |
| 251 | |
| 252 | skcipher_request_set_tfm(subreq, ctx->child); |
| 253 | |
| 254 | if (cbc_blocks <= 0) { |
| 255 | skcipher_request_set_callback(subreq, req->base.flags, |
| 256 | req->base.complete, |
| 257 | req->base.data); |
| 258 | skcipher_request_set_crypt(subreq, req->src, req->dst, nbytes, |
| 259 | req->iv); |
| 260 | return crypto_skcipher_decrypt(subreq); |
| 261 | } |
| 262 | |
| 263 | skcipher_request_set_callback(subreq, req->base.flags, |
| 264 | crypto_cts_decrypt_done, req); |
| 265 | |
| 266 | space = crypto_cts_reqctx_space(req); |
| 267 | |
| 268 | offset = cbc_blocks * bsize; |
| 269 | rctx->offset = offset; |
| 270 | |
| 271 | if (cbc_blocks <= 1) |
| 272 | memcpy(space, req->iv, bsize); |
| 273 | else |
| 274 | scatterwalk_map_and_copy(space, req->src, offset - 2 * bsize, |
| 275 | bsize, 0); |
| 276 | |
| 277 | skcipher_request_set_crypt(subreq, req->src, req->dst, |
| 278 | offset, req->iv); |
| 279 | |
| 280 | return crypto_skcipher_decrypt(subreq) ?: |
| 281 | cts_cbc_decrypt(req); |
| 282 | } |
| 283 | |
| 284 | static int crypto_cts_init_tfm(struct crypto_skcipher *tfm) |
| 285 | { |
| 286 | struct skcipher_instance *inst = skcipher_alg_instance(tfm); |
| 287 | struct crypto_skcipher_spawn *spawn = skcipher_instance_ctx(inst); |
| 288 | struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 289 | struct crypto_skcipher *cipher; |
| 290 | unsigned reqsize; |
| 291 | unsigned bsize; |
| 292 | unsigned align; |
| 293 | |
Eric Biggers | 60425a8 | 2016-10-28 09:52:19 -0700 | [diff] [blame] | 294 | cipher = crypto_spawn_skcipher(spawn); |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 295 | if (IS_ERR(cipher)) |
| 296 | return PTR_ERR(cipher); |
| 297 | |
| 298 | ctx->child = cipher; |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 299 | |
| 300 | align = crypto_skcipher_alignmask(tfm); |
| 301 | bsize = crypto_skcipher_blocksize(cipher); |
| 302 | reqsize = ALIGN(sizeof(struct crypto_cts_reqctx) + |
| 303 | crypto_skcipher_reqsize(cipher), |
| 304 | crypto_tfm_ctx_alignment()) + |
| 305 | (align & ~(crypto_tfm_ctx_alignment() - 1)) + bsize; |
| 306 | |
| 307 | crypto_skcipher_set_reqsize(tfm, reqsize); |
| 308 | |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 309 | return 0; |
| 310 | } |
| 311 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 312 | static void crypto_cts_exit_tfm(struct crypto_skcipher *tfm) |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 313 | { |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 314 | struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 315 | |
| 316 | crypto_free_skcipher(ctx->child); |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 317 | } |
| 318 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 319 | static void crypto_cts_free(struct skcipher_instance *inst) |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 320 | { |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 321 | crypto_drop_skcipher(skcipher_instance_ctx(inst)); |
| 322 | kfree(inst); |
| 323 | } |
| 324 | |
| 325 | static int crypto_cts_create(struct crypto_template *tmpl, struct rtattr **tb) |
| 326 | { |
| 327 | struct crypto_skcipher_spawn *spawn; |
| 328 | struct skcipher_instance *inst; |
| 329 | struct crypto_attr_type *algt; |
| 330 | struct skcipher_alg *alg; |
| 331 | const char *cipher_name; |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 332 | int err; |
| 333 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 334 | algt = crypto_get_attr_type(tb); |
| 335 | if (IS_ERR(algt)) |
| 336 | return PTR_ERR(algt); |
| 337 | |
| 338 | if ((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask) |
| 339 | return -EINVAL; |
| 340 | |
| 341 | cipher_name = crypto_attr_alg_name(tb[1]); |
| 342 | if (IS_ERR(cipher_name)) |
| 343 | return PTR_ERR(cipher_name); |
| 344 | |
| 345 | inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL); |
| 346 | if (!inst) |
| 347 | return -ENOMEM; |
| 348 | |
| 349 | spawn = skcipher_instance_ctx(inst); |
| 350 | |
| 351 | crypto_set_skcipher_spawn(spawn, skcipher_crypto_instance(inst)); |
Eric Biggers | a35528e | 2016-10-28 09:51:13 -0700 | [diff] [blame] | 352 | err = crypto_grab_skcipher(spawn, cipher_name, 0, |
| 353 | crypto_requires_sync(algt->type, |
| 354 | algt->mask)); |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 355 | if (err) |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 356 | goto err_free_inst; |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 357 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 358 | alg = crypto_spawn_skcipher_alg(spawn); |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 359 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 360 | err = -EINVAL; |
| 361 | if (crypto_skcipher_alg_ivsize(alg) != alg->base.cra_blocksize) |
| 362 | goto err_drop_spawn; |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 363 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 364 | if (strncmp(alg->base.cra_name, "cbc(", 4)) |
| 365 | goto err_drop_spawn; |
Herbert Xu | 988dc01 | 2015-01-16 19:38:17 +1100 | [diff] [blame] | 366 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 367 | err = crypto_inst_setname(skcipher_crypto_instance(inst), "cts", |
| 368 | &alg->base); |
| 369 | if (err) |
| 370 | goto err_drop_spawn; |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 371 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 372 | inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC; |
| 373 | inst->alg.base.cra_priority = alg->base.cra_priority; |
| 374 | inst->alg.base.cra_blocksize = alg->base.cra_blocksize; |
| 375 | inst->alg.base.cra_alignmask = alg->base.cra_alignmask; |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 376 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 377 | inst->alg.ivsize = alg->base.cra_blocksize; |
| 378 | inst->alg.chunksize = crypto_skcipher_alg_chunksize(alg); |
| 379 | inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg); |
| 380 | inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg); |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 381 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 382 | inst->alg.base.cra_ctxsize = sizeof(struct crypto_cts_ctx); |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 383 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 384 | inst->alg.init = crypto_cts_init_tfm; |
| 385 | inst->alg.exit = crypto_cts_exit_tfm; |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 386 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 387 | inst->alg.setkey = crypto_cts_setkey; |
| 388 | inst->alg.encrypt = crypto_cts_encrypt; |
| 389 | inst->alg.decrypt = crypto_cts_decrypt; |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 390 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 391 | inst->free = crypto_cts_free; |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 392 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 393 | err = skcipher_register_instance(tmpl, inst); |
| 394 | if (err) |
| 395 | goto err_drop_spawn; |
| 396 | |
| 397 | out: |
| 398 | return err; |
| 399 | |
| 400 | err_drop_spawn: |
| 401 | crypto_drop_skcipher(spawn); |
| 402 | err_free_inst: |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 403 | kfree(inst); |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 404 | goto out; |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | static struct crypto_template crypto_cts_tmpl = { |
| 408 | .name = "cts", |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 409 | .create = crypto_cts_create, |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 410 | .module = THIS_MODULE, |
| 411 | }; |
| 412 | |
| 413 | static int __init crypto_cts_module_init(void) |
| 414 | { |
| 415 | return crypto_register_template(&crypto_cts_tmpl); |
| 416 | } |
| 417 | |
| 418 | static void __exit crypto_cts_module_exit(void) |
| 419 | { |
| 420 | crypto_unregister_template(&crypto_cts_tmpl); |
| 421 | } |
| 422 | |
| 423 | module_init(crypto_cts_module_init); |
| 424 | module_exit(crypto_cts_module_exit); |
| 425 | |
| 426 | MODULE_LICENSE("Dual BSD/GPL"); |
| 427 | MODULE_DESCRIPTION("CTS-CBC CipherText Stealing for CBC"); |
Kees Cook | 4943ba1 | 2014-11-24 16:32:38 -0800 | [diff] [blame] | 428 | MODULE_ALIAS_CRYPTO("cts"); |