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 | |
Salvatore Mesoraca | 6650c4d | 2018-04-09 15:54:47 +0200 | [diff] [blame] | 43 | #include <crypto/algapi.h> |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 44 | #include <crypto/internal/skcipher.h> |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 45 | #include <linux/err.h> |
| 46 | #include <linux/init.h> |
| 47 | #include <linux/kernel.h> |
| 48 | #include <linux/log2.h> |
| 49 | #include <linux/module.h> |
| 50 | #include <linux/scatterlist.h> |
| 51 | #include <crypto/scatterwalk.h> |
| 52 | #include <linux/slab.h> |
Gideon Israel Dsouza | d8c34b9 | 2016-12-31 21:26:23 +0530 | [diff] [blame] | 53 | #include <linux/compiler.h> |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 54 | |
| 55 | struct crypto_cts_ctx { |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 56 | struct crypto_skcipher *child; |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 57 | }; |
| 58 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 59 | struct crypto_cts_reqctx { |
| 60 | struct scatterlist sg[2]; |
| 61 | unsigned offset; |
| 62 | struct skcipher_request subreq; |
| 63 | }; |
| 64 | |
| 65 | static inline u8 *crypto_cts_reqctx_space(struct skcipher_request *req) |
| 66 | { |
| 67 | struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req); |
| 68 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 69 | struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 70 | struct crypto_skcipher *child = ctx->child; |
| 71 | |
| 72 | return PTR_ALIGN((u8 *)(rctx + 1) + crypto_skcipher_reqsize(child), |
| 73 | crypto_skcipher_alignmask(tfm) + 1); |
| 74 | } |
| 75 | |
| 76 | static int crypto_cts_setkey(struct crypto_skcipher *parent, const u8 *key, |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 77 | unsigned int keylen) |
| 78 | { |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 79 | struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(parent); |
| 80 | struct crypto_skcipher *child = ctx->child; |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 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); |
Eric Biggers | af5034e | 2019-12-30 21:19:38 -0600 | [diff] [blame] | 85 | return crypto_skcipher_setkey(child, key, keylen); |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 86 | } |
| 87 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 88 | 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] | 89 | { |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 90 | struct skcipher_request *req = areq->data; |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 91 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 92 | if (err == -EINPROGRESS) |
| 93 | return; |
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 | skcipher_request_complete(req, err); |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 96 | } |
| 97 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 98 | static int cts_cbc_encrypt(struct skcipher_request *req) |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 99 | { |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 100 | struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req); |
| 101 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 102 | struct skcipher_request *subreq = &rctx->subreq; |
| 103 | int bsize = crypto_skcipher_blocksize(tfm); |
Salvatore Mesoraca | 6650c4d | 2018-04-09 15:54:47 +0200 | [diff] [blame] | 104 | u8 d[MAX_CIPHER_BLOCKSIZE * 2] __aligned(__alignof__(u32)); |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 105 | struct scatterlist *sg; |
| 106 | unsigned int offset; |
| 107 | int lastn; |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 108 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 109 | offset = rctx->offset; |
| 110 | lastn = req->cryptlen - offset; |
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 | sg = scatterwalk_ffwd(rctx->sg, req->dst, offset - bsize); |
| 113 | scatterwalk_map_and_copy(d + bsize, sg, 0, bsize, 0); |
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 | memset(d, 0, bsize); |
| 116 | scatterwalk_map_and_copy(d, req->src, offset, lastn, 0); |
| 117 | |
| 118 | scatterwalk_map_and_copy(d, sg, 0, bsize + lastn, 1); |
| 119 | memzero_explicit(d, sizeof(d)); |
| 120 | |
| 121 | skcipher_request_set_callback(subreq, req->base.flags & |
| 122 | CRYPTO_TFM_REQ_MAY_BACKLOG, |
| 123 | cts_cbc_crypt_done, req); |
| 124 | skcipher_request_set_crypt(subreq, sg, sg, bsize, req->iv); |
| 125 | return crypto_skcipher_encrypt(subreq); |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 126 | } |
| 127 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 128 | 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] | 129 | { |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 130 | struct skcipher_request *req = areq->data; |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 131 | |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 132 | if (err) |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 133 | goto out; |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 134 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 135 | err = cts_cbc_encrypt(req); |
Gilad Ben-Yossef | 4e5b0ad | 2017-10-18 08:00:36 +0100 | [diff] [blame] | 136 | if (err == -EINPROGRESS || err == -EBUSY) |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 137 | return; |
Daniel Borkmann | 7185ad2 | 2014-09-07 23:23:38 +0200 | [diff] [blame] | 138 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 139 | out: |
| 140 | skcipher_request_complete(req, err); |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 141 | } |
| 142 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 143 | static int crypto_cts_encrypt(struct skcipher_request *req) |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 144 | { |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 145 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 146 | struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req); |
| 147 | struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 148 | struct skcipher_request *subreq = &rctx->subreq; |
| 149 | int bsize = crypto_skcipher_blocksize(tfm); |
| 150 | unsigned int nbytes = req->cryptlen; |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 151 | unsigned int offset; |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 152 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 153 | skcipher_request_set_tfm(subreq, ctx->child); |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 154 | |
Eric Biggers | c31a871 | 2019-03-31 13:04:20 -0700 | [diff] [blame] | 155 | if (nbytes < bsize) |
| 156 | return -EINVAL; |
| 157 | |
| 158 | if (nbytes == bsize) { |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 159 | skcipher_request_set_callback(subreq, req->base.flags, |
| 160 | req->base.complete, |
| 161 | req->base.data); |
| 162 | skcipher_request_set_crypt(subreq, req->src, req->dst, nbytes, |
| 163 | req->iv); |
| 164 | return crypto_skcipher_encrypt(subreq); |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 165 | } |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 166 | |
Eric Biggers | c31a871 | 2019-03-31 13:04:20 -0700 | [diff] [blame] | 167 | offset = rounddown(nbytes - 1, bsize); |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 168 | rctx->offset = offset; |
| 169 | |
| 170 | skcipher_request_set_callback(subreq, req->base.flags, |
| 171 | crypto_cts_encrypt_done, req); |
| 172 | skcipher_request_set_crypt(subreq, req->src, req->dst, |
| 173 | offset, req->iv); |
| 174 | |
| 175 | return crypto_skcipher_encrypt(subreq) ?: |
| 176 | cts_cbc_encrypt(req); |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 177 | } |
| 178 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 179 | static int cts_cbc_decrypt(struct skcipher_request *req) |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 180 | { |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 181 | struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req); |
| 182 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 183 | struct skcipher_request *subreq = &rctx->subreq; |
| 184 | int bsize = crypto_skcipher_blocksize(tfm); |
Salvatore Mesoraca | 6650c4d | 2018-04-09 15:54:47 +0200 | [diff] [blame] | 185 | u8 d[MAX_CIPHER_BLOCKSIZE * 2] __aligned(__alignof__(u32)); |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 186 | struct scatterlist *sg; |
| 187 | unsigned int offset; |
| 188 | u8 *space; |
| 189 | int lastn; |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 190 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 191 | offset = rctx->offset; |
| 192 | lastn = req->cryptlen - offset; |
| 193 | |
| 194 | sg = scatterwalk_ffwd(rctx->sg, req->dst, offset - bsize); |
| 195 | |
| 196 | /* 1. Decrypt Cn-1 (s) to create Dn */ |
| 197 | scatterwalk_map_and_copy(d + bsize, sg, 0, bsize, 0); |
| 198 | space = crypto_cts_reqctx_space(req); |
| 199 | crypto_xor(d + bsize, space, bsize); |
| 200 | /* 2. Pad Cn with zeros at the end to create C of length BB */ |
| 201 | memset(d, 0, bsize); |
| 202 | scatterwalk_map_and_copy(d, req->src, offset, lastn, 0); |
| 203 | /* 3. Exclusive-or Dn with C to create Xn */ |
| 204 | /* 4. Select the first Ln bytes of Xn to create Pn */ |
| 205 | crypto_xor(d + bsize, d, lastn); |
| 206 | |
| 207 | /* 5. Append the tail (BB - Ln) bytes of Xn to Cn to create En */ |
| 208 | memcpy(d + lastn, d + bsize + lastn, bsize - lastn); |
| 209 | /* 6. Decrypt En to create Pn-1 */ |
| 210 | |
| 211 | scatterwalk_map_and_copy(d, sg, 0, bsize + lastn, 1); |
| 212 | memzero_explicit(d, sizeof(d)); |
| 213 | |
| 214 | skcipher_request_set_callback(subreq, req->base.flags & |
| 215 | CRYPTO_TFM_REQ_MAY_BACKLOG, |
| 216 | cts_cbc_crypt_done, req); |
| 217 | |
| 218 | skcipher_request_set_crypt(subreq, sg, sg, bsize, space); |
| 219 | return crypto_skcipher_decrypt(subreq); |
| 220 | } |
| 221 | |
| 222 | static void crypto_cts_decrypt_done(struct crypto_async_request *areq, int err) |
| 223 | { |
| 224 | struct skcipher_request *req = areq->data; |
| 225 | |
| 226 | if (err) |
| 227 | goto out; |
| 228 | |
| 229 | err = cts_cbc_decrypt(req); |
Gilad Ben-Yossef | 4e5b0ad | 2017-10-18 08:00:36 +0100 | [diff] [blame] | 230 | if (err == -EINPROGRESS || err == -EBUSY) |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 231 | return; |
| 232 | |
| 233 | out: |
| 234 | skcipher_request_complete(req, err); |
| 235 | } |
| 236 | |
| 237 | static int crypto_cts_decrypt(struct skcipher_request *req) |
| 238 | { |
| 239 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 240 | struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req); |
| 241 | struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 242 | struct skcipher_request *subreq = &rctx->subreq; |
| 243 | int bsize = crypto_skcipher_blocksize(tfm); |
| 244 | unsigned int nbytes = req->cryptlen; |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 245 | unsigned int offset; |
| 246 | u8 *space; |
| 247 | |
| 248 | skcipher_request_set_tfm(subreq, ctx->child); |
| 249 | |
Eric Biggers | c31a871 | 2019-03-31 13:04:20 -0700 | [diff] [blame] | 250 | if (nbytes < bsize) |
| 251 | return -EINVAL; |
| 252 | |
| 253 | if (nbytes == bsize) { |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 254 | skcipher_request_set_callback(subreq, req->base.flags, |
| 255 | req->base.complete, |
| 256 | req->base.data); |
| 257 | skcipher_request_set_crypt(subreq, req->src, req->dst, nbytes, |
| 258 | req->iv); |
| 259 | return crypto_skcipher_decrypt(subreq); |
| 260 | } |
| 261 | |
| 262 | skcipher_request_set_callback(subreq, req->base.flags, |
| 263 | crypto_cts_decrypt_done, req); |
| 264 | |
| 265 | space = crypto_cts_reqctx_space(req); |
| 266 | |
Eric Biggers | c31a871 | 2019-03-31 13:04:20 -0700 | [diff] [blame] | 267 | offset = rounddown(nbytes - 1, bsize); |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 268 | rctx->offset = offset; |
| 269 | |
Eric Biggers | c31a871 | 2019-03-31 13:04:20 -0700 | [diff] [blame] | 270 | if (offset <= bsize) |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 271 | memcpy(space, req->iv, bsize); |
| 272 | else |
| 273 | scatterwalk_map_and_copy(space, req->src, offset - 2 * bsize, |
| 274 | bsize, 0); |
| 275 | |
| 276 | skcipher_request_set_crypt(subreq, req->src, req->dst, |
| 277 | offset, req->iv); |
| 278 | |
| 279 | return crypto_skcipher_decrypt(subreq) ?: |
| 280 | cts_cbc_decrypt(req); |
| 281 | } |
| 282 | |
| 283 | static int crypto_cts_init_tfm(struct crypto_skcipher *tfm) |
| 284 | { |
| 285 | struct skcipher_instance *inst = skcipher_alg_instance(tfm); |
| 286 | struct crypto_skcipher_spawn *spawn = skcipher_instance_ctx(inst); |
| 287 | struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 288 | struct crypto_skcipher *cipher; |
| 289 | unsigned reqsize; |
| 290 | unsigned bsize; |
| 291 | unsigned align; |
| 292 | |
Eric Biggers | 60425a8 | 2016-10-28 09:52:19 -0700 | [diff] [blame] | 293 | cipher = crypto_spawn_skcipher(spawn); |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 294 | if (IS_ERR(cipher)) |
| 295 | return PTR_ERR(cipher); |
| 296 | |
| 297 | ctx->child = cipher; |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 298 | |
| 299 | align = crypto_skcipher_alignmask(tfm); |
| 300 | bsize = crypto_skcipher_blocksize(cipher); |
| 301 | reqsize = ALIGN(sizeof(struct crypto_cts_reqctx) + |
| 302 | crypto_skcipher_reqsize(cipher), |
| 303 | crypto_tfm_ctx_alignment()) + |
| 304 | (align & ~(crypto_tfm_ctx_alignment() - 1)) + bsize; |
| 305 | |
| 306 | crypto_skcipher_set_reqsize(tfm, reqsize); |
| 307 | |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 308 | return 0; |
| 309 | } |
| 310 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 311 | static void crypto_cts_exit_tfm(struct crypto_skcipher *tfm) |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 312 | { |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 313 | struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 314 | |
| 315 | crypto_free_skcipher(ctx->child); |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 316 | } |
| 317 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 318 | static void crypto_cts_free(struct skcipher_instance *inst) |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 319 | { |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 320 | crypto_drop_skcipher(skcipher_instance_ctx(inst)); |
| 321 | kfree(inst); |
| 322 | } |
| 323 | |
| 324 | static int crypto_cts_create(struct crypto_template *tmpl, struct rtattr **tb) |
| 325 | { |
| 326 | struct crypto_skcipher_spawn *spawn; |
| 327 | struct skcipher_instance *inst; |
| 328 | struct crypto_attr_type *algt; |
| 329 | struct skcipher_alg *alg; |
Eric Biggers | b9f76dd | 2020-01-02 19:58:45 -0800 | [diff] [blame] | 330 | u32 mask; |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 331 | int err; |
| 332 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 333 | algt = crypto_get_attr_type(tb); |
| 334 | if (IS_ERR(algt)) |
| 335 | return PTR_ERR(algt); |
| 336 | |
| 337 | if ((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask) |
| 338 | return -EINVAL; |
| 339 | |
Eric Biggers | b9f76dd | 2020-01-02 19:58:45 -0800 | [diff] [blame] | 340 | mask = crypto_requires_sync(algt->type, algt->mask); |
| 341 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 342 | inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL); |
| 343 | if (!inst) |
| 344 | return -ENOMEM; |
| 345 | |
| 346 | spawn = skcipher_instance_ctx(inst); |
| 347 | |
Eric Biggers | b9f76dd | 2020-01-02 19:58:45 -0800 | [diff] [blame] | 348 | err = crypto_grab_skcipher(spawn, skcipher_crypto_instance(inst), |
Eric Biggers | 3ff2bab | 2020-02-25 20:59:17 -0800 | [diff] [blame] | 349 | crypto_attr_alg_name(tb[1]), 0, mask); |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 350 | if (err) |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 351 | goto err_free_inst; |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 352 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 353 | alg = crypto_spawn_skcipher_alg(spawn); |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 354 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 355 | err = -EINVAL; |
| 356 | if (crypto_skcipher_alg_ivsize(alg) != alg->base.cra_blocksize) |
Eric Biggers | 3ff2bab | 2020-02-25 20:59:17 -0800 | [diff] [blame] | 357 | goto err_free_inst; |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 358 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 359 | if (strncmp(alg->base.cra_name, "cbc(", 4)) |
Eric Biggers | 3ff2bab | 2020-02-25 20:59:17 -0800 | [diff] [blame] | 360 | goto err_free_inst; |
Herbert Xu | 988dc01 | 2015-01-16 19:38:17 +1100 | [diff] [blame] | 361 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 362 | err = crypto_inst_setname(skcipher_crypto_instance(inst), "cts", |
| 363 | &alg->base); |
| 364 | if (err) |
Eric Biggers | 3ff2bab | 2020-02-25 20:59:17 -0800 | [diff] [blame] | 365 | goto err_free_inst; |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 366 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 367 | inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC; |
| 368 | inst->alg.base.cra_priority = alg->base.cra_priority; |
| 369 | inst->alg.base.cra_blocksize = alg->base.cra_blocksize; |
| 370 | inst->alg.base.cra_alignmask = alg->base.cra_alignmask; |
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.ivsize = alg->base.cra_blocksize; |
| 373 | inst->alg.chunksize = crypto_skcipher_alg_chunksize(alg); |
| 374 | inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg); |
| 375 | inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg); |
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.base.cra_ctxsize = sizeof(struct crypto_cts_ctx); |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 378 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 379 | inst->alg.init = crypto_cts_init_tfm; |
| 380 | inst->alg.exit = crypto_cts_exit_tfm; |
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.setkey = crypto_cts_setkey; |
| 383 | inst->alg.encrypt = crypto_cts_encrypt; |
| 384 | inst->alg.decrypt = crypto_cts_decrypt; |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 385 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 386 | inst->free = crypto_cts_free; |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 387 | |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 388 | err = skcipher_register_instance(tmpl, inst); |
Eric Biggers | 3ff2bab | 2020-02-25 20:59:17 -0800 | [diff] [blame] | 389 | if (err) { |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 390 | err_free_inst: |
Eric Biggers | 3ff2bab | 2020-02-25 20:59:17 -0800 | [diff] [blame] | 391 | crypto_cts_free(inst); |
| 392 | } |
| 393 | return err; |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | static struct crypto_template crypto_cts_tmpl = { |
| 397 | .name = "cts", |
Herbert Xu | 0605c41 | 2016-07-12 13:17:48 +0800 | [diff] [blame] | 398 | .create = crypto_cts_create, |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 399 | .module = THIS_MODULE, |
| 400 | }; |
| 401 | |
| 402 | static int __init crypto_cts_module_init(void) |
| 403 | { |
| 404 | return crypto_register_template(&crypto_cts_tmpl); |
| 405 | } |
| 406 | |
| 407 | static void __exit crypto_cts_module_exit(void) |
| 408 | { |
| 409 | crypto_unregister_template(&crypto_cts_tmpl); |
| 410 | } |
| 411 | |
Eric Biggers | c4741b2 | 2019-04-11 21:57:42 -0700 | [diff] [blame] | 412 | subsys_initcall(crypto_cts_module_init); |
Kevin Coffman | 76cb952 | 2008-03-24 21:26:16 +0800 | [diff] [blame] | 413 | module_exit(crypto_cts_module_exit); |
| 414 | |
| 415 | MODULE_LICENSE("Dual BSD/GPL"); |
| 416 | MODULE_DESCRIPTION("CTS-CBC CipherText Stealing for CBC"); |
Kees Cook | 4943ba1 | 2014-11-24 16:32:38 -0800 | [diff] [blame] | 417 | MODULE_ALIAS_CRYPTO("cts"); |