Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 1 | /* |
| 2 | * CTR: Counter mode |
| 3 | * |
| 4 | * (C) Copyright IBM Corp. 2007 - Joy Latten <latten@us.ibm.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License as published by the Free |
| 8 | * Software Foundation; either version 2 of the License, or (at your option) |
| 9 | * any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <crypto/algapi.h> |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 14 | #include <crypto/ctr.h> |
Jussi Kivilinna | 69d3150 | 2012-12-28 12:04:58 +0200 | [diff] [blame] | 15 | #include <crypto/internal/skcipher.h> |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 16 | #include <linux/err.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/module.h> |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 20 | #include <linux/slab.h> |
| 21 | |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 22 | struct crypto_rfc3686_ctx { |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 23 | struct crypto_skcipher *child; |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 24 | u8 nonce[CTR_RFC3686_NONCE_SIZE]; |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 25 | }; |
| 26 | |
Jussi Kivilinna | 69d3150 | 2012-12-28 12:04:58 +0200 | [diff] [blame] | 27 | struct crypto_rfc3686_req_ctx { |
| 28 | u8 iv[CTR_RFC3686_BLOCK_SIZE]; |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 29 | struct skcipher_request subreq CRYPTO_MINALIGN_ATTR; |
Jussi Kivilinna | 69d3150 | 2012-12-28 12:04:58 +0200 | [diff] [blame] | 30 | }; |
| 31 | |
Eric Biggers | 11f1463 | 2019-01-03 20:16:17 -0800 | [diff] [blame^] | 32 | static void crypto_ctr_crypt_final(struct skcipher_walk *walk, |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 33 | struct crypto_cipher *tfm) |
Herbert Xu | 0971eb0 | 2007-11-30 00:23:53 +1100 | [diff] [blame] | 34 | { |
| 35 | unsigned int bsize = crypto_cipher_blocksize(tfm); |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 36 | unsigned long alignmask = crypto_cipher_alignmask(tfm); |
| 37 | u8 *ctrblk = walk->iv; |
Salvatore Mesoraca | 6650c4d | 2018-04-09 15:54:47 +0200 | [diff] [blame] | 38 | u8 tmp[MAX_CIPHER_BLOCKSIZE + MAX_CIPHER_ALIGNMASK]; |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 39 | u8 *keystream = PTR_ALIGN(tmp + 0, alignmask + 1); |
Herbert Xu | 0971eb0 | 2007-11-30 00:23:53 +1100 | [diff] [blame] | 40 | u8 *src = walk->src.virt.addr; |
| 41 | u8 *dst = walk->dst.virt.addr; |
| 42 | unsigned int nbytes = walk->nbytes; |
| 43 | |
| 44 | crypto_cipher_encrypt_one(tfm, keystream, ctrblk); |
Ard Biesheuvel | 45fe93d | 2017-07-24 11:28:04 +0100 | [diff] [blame] | 45 | crypto_xor_cpy(dst, keystream, src, nbytes); |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 46 | |
| 47 | crypto_inc(ctrblk, bsize); |
Herbert Xu | 0971eb0 | 2007-11-30 00:23:53 +1100 | [diff] [blame] | 48 | } |
| 49 | |
Eric Biggers | 11f1463 | 2019-01-03 20:16:17 -0800 | [diff] [blame^] | 50 | static int crypto_ctr_crypt_segment(struct skcipher_walk *walk, |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 51 | struct crypto_cipher *tfm) |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 52 | { |
| 53 | void (*fn)(struct crypto_tfm *, u8 *, const u8 *) = |
| 54 | crypto_cipher_alg(tfm)->cia_encrypt; |
| 55 | unsigned int bsize = crypto_cipher_blocksize(tfm); |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 56 | u8 *ctrblk = walk->iv; |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 57 | u8 *src = walk->src.virt.addr; |
| 58 | u8 *dst = walk->dst.virt.addr; |
| 59 | unsigned int nbytes = walk->nbytes; |
| 60 | |
| 61 | do { |
| 62 | /* create keystream */ |
Herbert Xu | 0971eb0 | 2007-11-30 00:23:53 +1100 | [diff] [blame] | 63 | fn(crypto_cipher_tfm(tfm), dst, ctrblk); |
| 64 | crypto_xor(dst, src, bsize); |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 65 | |
| 66 | /* increment counter in counterblock */ |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 67 | crypto_inc(ctrblk, bsize); |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 68 | |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 69 | src += bsize; |
| 70 | dst += bsize; |
Herbert Xu | 0971eb0 | 2007-11-30 00:23:53 +1100 | [diff] [blame] | 71 | } while ((nbytes -= bsize) >= bsize); |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 72 | |
Herbert Xu | 0971eb0 | 2007-11-30 00:23:53 +1100 | [diff] [blame] | 73 | return nbytes; |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 74 | } |
| 75 | |
Eric Biggers | 11f1463 | 2019-01-03 20:16:17 -0800 | [diff] [blame^] | 76 | static int crypto_ctr_crypt_inplace(struct skcipher_walk *walk, |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 77 | struct crypto_cipher *tfm) |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 78 | { |
| 79 | void (*fn)(struct crypto_tfm *, u8 *, const u8 *) = |
| 80 | crypto_cipher_alg(tfm)->cia_encrypt; |
| 81 | unsigned int bsize = crypto_cipher_blocksize(tfm); |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 82 | unsigned long alignmask = crypto_cipher_alignmask(tfm); |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 83 | unsigned int nbytes = walk->nbytes; |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 84 | u8 *ctrblk = walk->iv; |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 85 | u8 *src = walk->src.virt.addr; |
Salvatore Mesoraca | 6650c4d | 2018-04-09 15:54:47 +0200 | [diff] [blame] | 86 | u8 tmp[MAX_CIPHER_BLOCKSIZE + MAX_CIPHER_ALIGNMASK]; |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 87 | u8 *keystream = PTR_ALIGN(tmp + 0, alignmask + 1); |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 88 | |
| 89 | do { |
| 90 | /* create keystream */ |
| 91 | fn(crypto_cipher_tfm(tfm), keystream, ctrblk); |
Herbert Xu | 0971eb0 | 2007-11-30 00:23:53 +1100 | [diff] [blame] | 92 | crypto_xor(src, keystream, bsize); |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 93 | |
| 94 | /* increment counter in counterblock */ |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 95 | crypto_inc(ctrblk, bsize); |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 96 | |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 97 | src += bsize; |
Herbert Xu | 0971eb0 | 2007-11-30 00:23:53 +1100 | [diff] [blame] | 98 | } while ((nbytes -= bsize) >= bsize); |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 99 | |
Herbert Xu | 0971eb0 | 2007-11-30 00:23:53 +1100 | [diff] [blame] | 100 | return nbytes; |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 101 | } |
| 102 | |
Eric Biggers | 11f1463 | 2019-01-03 20:16:17 -0800 | [diff] [blame^] | 103 | static int crypto_ctr_crypt(struct skcipher_request *req) |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 104 | { |
Eric Biggers | 11f1463 | 2019-01-03 20:16:17 -0800 | [diff] [blame^] | 105 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 106 | struct crypto_cipher *cipher = skcipher_cipher_simple(tfm); |
| 107 | const unsigned int bsize = crypto_cipher_blocksize(cipher); |
| 108 | struct skcipher_walk walk; |
| 109 | unsigned int nbytes; |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 110 | int err; |
| 111 | |
Eric Biggers | 11f1463 | 2019-01-03 20:16:17 -0800 | [diff] [blame^] | 112 | err = skcipher_walk_virt(&walk, req, false); |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 113 | |
Herbert Xu | 0971eb0 | 2007-11-30 00:23:53 +1100 | [diff] [blame] | 114 | while (walk.nbytes >= bsize) { |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 115 | if (walk.src.virt.addr == walk.dst.virt.addr) |
Eric Biggers | 11f1463 | 2019-01-03 20:16:17 -0800 | [diff] [blame^] | 116 | nbytes = crypto_ctr_crypt_inplace(&walk, cipher); |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 117 | else |
Eric Biggers | 11f1463 | 2019-01-03 20:16:17 -0800 | [diff] [blame^] | 118 | nbytes = crypto_ctr_crypt_segment(&walk, cipher); |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 119 | |
Eric Biggers | 11f1463 | 2019-01-03 20:16:17 -0800 | [diff] [blame^] | 120 | err = skcipher_walk_done(&walk, nbytes); |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 121 | } |
Herbert Xu | 0971eb0 | 2007-11-30 00:23:53 +1100 | [diff] [blame] | 122 | |
| 123 | if (walk.nbytes) { |
Eric Biggers | 11f1463 | 2019-01-03 20:16:17 -0800 | [diff] [blame^] | 124 | crypto_ctr_crypt_final(&walk, cipher); |
| 125 | err = skcipher_walk_done(&walk, 0); |
Herbert Xu | 0971eb0 | 2007-11-30 00:23:53 +1100 | [diff] [blame] | 126 | } |
| 127 | |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 128 | return err; |
| 129 | } |
| 130 | |
Eric Biggers | 11f1463 | 2019-01-03 20:16:17 -0800 | [diff] [blame^] | 131 | static int crypto_ctr_create(struct crypto_template *tmpl, struct rtattr **tb) |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 132 | { |
Eric Biggers | 11f1463 | 2019-01-03 20:16:17 -0800 | [diff] [blame^] | 133 | struct skcipher_instance *inst; |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 134 | struct crypto_alg *alg; |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 135 | int err; |
| 136 | |
Eric Biggers | 11f1463 | 2019-01-03 20:16:17 -0800 | [diff] [blame^] | 137 | inst = skcipher_alloc_instance_simple(tmpl, tb, &alg); |
| 138 | if (IS_ERR(inst)) |
| 139 | return PTR_ERR(inst); |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 140 | |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 141 | /* Block size must be >= 4 bytes. */ |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 142 | err = -EINVAL; |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 143 | if (alg->cra_blocksize < 4) |
Eric Biggers | 11f1463 | 2019-01-03 20:16:17 -0800 | [diff] [blame^] | 144 | goto out_free_inst; |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 145 | |
Herbert Xu | 3f8214e | 2007-11-20 20:32:56 +0800 | [diff] [blame] | 146 | /* If this is false we'd fail the alignment of crypto_inc. */ |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 147 | if (alg->cra_blocksize % 4) |
Eric Biggers | 11f1463 | 2019-01-03 20:16:17 -0800 | [diff] [blame^] | 148 | goto out_free_inst; |
Herbert Xu | 3f8214e | 2007-11-20 20:32:56 +0800 | [diff] [blame] | 149 | |
Eric Biggers | 11f1463 | 2019-01-03 20:16:17 -0800 | [diff] [blame^] | 150 | /* CTR mode is a stream cipher. */ |
| 151 | inst->alg.base.cra_blocksize = 1; |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 152 | |
Eric Biggers | 11f1463 | 2019-01-03 20:16:17 -0800 | [diff] [blame^] | 153 | /* |
| 154 | * To simplify the implementation, configure the skcipher walk to only |
| 155 | * give a partial block at the very end, never earlier. |
| 156 | */ |
| 157 | inst->alg.chunksize = alg->cra_blocksize; |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 158 | |
Eric Biggers | 11f1463 | 2019-01-03 20:16:17 -0800 | [diff] [blame^] | 159 | inst->alg.encrypt = crypto_ctr_crypt; |
| 160 | inst->alg.decrypt = crypto_ctr_crypt; |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 161 | |
Eric Biggers | 11f1463 | 2019-01-03 20:16:17 -0800 | [diff] [blame^] | 162 | err = skcipher_register_instance(tmpl, inst); |
| 163 | if (err) |
| 164 | goto out_free_inst; |
| 165 | goto out_put_alg; |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 166 | |
Eric Biggers | 11f1463 | 2019-01-03 20:16:17 -0800 | [diff] [blame^] | 167 | out_free_inst: |
| 168 | inst->free(inst); |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 169 | out_put_alg: |
Eric Biggers | 11f1463 | 2019-01-03 20:16:17 -0800 | [diff] [blame^] | 170 | crypto_mod_put(alg); |
| 171 | return err; |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | static struct crypto_template crypto_ctr_tmpl = { |
| 175 | .name = "ctr", |
Eric Biggers | 11f1463 | 2019-01-03 20:16:17 -0800 | [diff] [blame^] | 176 | .create = crypto_ctr_create, |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 177 | .module = THIS_MODULE, |
| 178 | }; |
| 179 | |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 180 | static int crypto_rfc3686_setkey(struct crypto_skcipher *parent, |
Jussi Kivilinna | 69d3150 | 2012-12-28 12:04:58 +0200 | [diff] [blame] | 181 | const u8 *key, unsigned int keylen) |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 182 | { |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 183 | struct crypto_rfc3686_ctx *ctx = crypto_skcipher_ctx(parent); |
| 184 | struct crypto_skcipher *child = ctx->child; |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 185 | int err; |
| 186 | |
| 187 | /* the nonce is stored in bytes at end of key */ |
| 188 | if (keylen < CTR_RFC3686_NONCE_SIZE) |
| 189 | return -EINVAL; |
| 190 | |
| 191 | memcpy(ctx->nonce, key + (keylen - CTR_RFC3686_NONCE_SIZE), |
| 192 | CTR_RFC3686_NONCE_SIZE); |
| 193 | |
| 194 | keylen -= CTR_RFC3686_NONCE_SIZE; |
| 195 | |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 196 | crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK); |
| 197 | crypto_skcipher_set_flags(child, crypto_skcipher_get_flags(parent) & |
| 198 | CRYPTO_TFM_REQ_MASK); |
| 199 | err = crypto_skcipher_setkey(child, key, keylen); |
| 200 | crypto_skcipher_set_flags(parent, crypto_skcipher_get_flags(child) & |
| 201 | CRYPTO_TFM_RES_MASK); |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 202 | |
| 203 | return err; |
| 204 | } |
| 205 | |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 206 | static int crypto_rfc3686_crypt(struct skcipher_request *req) |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 207 | { |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 208 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 209 | struct crypto_rfc3686_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 210 | struct crypto_skcipher *child = ctx->child; |
| 211 | unsigned long align = crypto_skcipher_alignmask(tfm); |
Jussi Kivilinna | 69d3150 | 2012-12-28 12:04:58 +0200 | [diff] [blame] | 212 | struct crypto_rfc3686_req_ctx *rctx = |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 213 | (void *)PTR_ALIGN((u8 *)skcipher_request_ctx(req), align + 1); |
| 214 | struct skcipher_request *subreq = &rctx->subreq; |
Jussi Kivilinna | 69d3150 | 2012-12-28 12:04:58 +0200 | [diff] [blame] | 215 | u8 *iv = rctx->iv; |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 216 | |
| 217 | /* set up counter block */ |
| 218 | memcpy(iv, ctx->nonce, CTR_RFC3686_NONCE_SIZE); |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 219 | memcpy(iv + CTR_RFC3686_NONCE_SIZE, req->iv, CTR_RFC3686_IV_SIZE); |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 220 | |
| 221 | /* initialize counter portion of counter block */ |
| 222 | *(__be32 *)(iv + CTR_RFC3686_NONCE_SIZE + CTR_RFC3686_IV_SIZE) = |
| 223 | cpu_to_be32(1); |
| 224 | |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 225 | skcipher_request_set_tfm(subreq, child); |
| 226 | skcipher_request_set_callback(subreq, req->base.flags, |
| 227 | req->base.complete, req->base.data); |
| 228 | skcipher_request_set_crypt(subreq, req->src, req->dst, |
| 229 | req->cryptlen, iv); |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 230 | |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 231 | return crypto_skcipher_encrypt(subreq); |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 232 | } |
| 233 | |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 234 | static int crypto_rfc3686_init_tfm(struct crypto_skcipher *tfm) |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 235 | { |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 236 | struct skcipher_instance *inst = skcipher_alg_instance(tfm); |
| 237 | struct crypto_skcipher_spawn *spawn = skcipher_instance_ctx(inst); |
| 238 | struct crypto_rfc3686_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 239 | struct crypto_skcipher *cipher; |
Jussi Kivilinna | 69d3150 | 2012-12-28 12:04:58 +0200 | [diff] [blame] | 240 | unsigned long align; |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 241 | unsigned int reqsize; |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 242 | |
Eric Biggers | 60425a8 | 2016-10-28 09:52:19 -0700 | [diff] [blame] | 243 | cipher = crypto_spawn_skcipher(spawn); |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 244 | if (IS_ERR(cipher)) |
| 245 | return PTR_ERR(cipher); |
| 246 | |
| 247 | ctx->child = cipher; |
| 248 | |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 249 | align = crypto_skcipher_alignmask(tfm); |
Jussi Kivilinna | 69d3150 | 2012-12-28 12:04:58 +0200 | [diff] [blame] | 250 | align &= ~(crypto_tfm_ctx_alignment() - 1); |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 251 | reqsize = align + sizeof(struct crypto_rfc3686_req_ctx) + |
| 252 | crypto_skcipher_reqsize(cipher); |
| 253 | crypto_skcipher_set_reqsize(tfm, reqsize); |
Jussi Kivilinna | 69d3150 | 2012-12-28 12:04:58 +0200 | [diff] [blame] | 254 | |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 255 | return 0; |
| 256 | } |
| 257 | |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 258 | static void crypto_rfc3686_exit_tfm(struct crypto_skcipher *tfm) |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 259 | { |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 260 | struct crypto_rfc3686_ctx *ctx = crypto_skcipher_ctx(tfm); |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 261 | |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 262 | crypto_free_skcipher(ctx->child); |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 263 | } |
| 264 | |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 265 | static void crypto_rfc3686_free(struct skcipher_instance *inst) |
| 266 | { |
| 267 | struct crypto_skcipher_spawn *spawn = skcipher_instance_ctx(inst); |
| 268 | |
| 269 | crypto_drop_skcipher(spawn); |
| 270 | kfree(inst); |
| 271 | } |
| 272 | |
| 273 | static int crypto_rfc3686_create(struct crypto_template *tmpl, |
| 274 | struct rtattr **tb) |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 275 | { |
Jussi Kivilinna | 69d3150 | 2012-12-28 12:04:58 +0200 | [diff] [blame] | 276 | struct crypto_attr_type *algt; |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 277 | struct skcipher_instance *inst; |
| 278 | struct skcipher_alg *alg; |
Jussi Kivilinna | 69d3150 | 2012-12-28 12:04:58 +0200 | [diff] [blame] | 279 | struct crypto_skcipher_spawn *spawn; |
| 280 | const char *cipher_name; |
Marcelo Cerri | d2c2a85 | 2017-02-27 09:38:26 -0300 | [diff] [blame] | 281 | u32 mask; |
| 282 | |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 283 | int err; |
| 284 | |
Jussi Kivilinna | 69d3150 | 2012-12-28 12:04:58 +0200 | [diff] [blame] | 285 | algt = crypto_get_attr_type(tb); |
Jussi Kivilinna | 69d3150 | 2012-12-28 12:04:58 +0200 | [diff] [blame] | 286 | if (IS_ERR(algt)) |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 287 | return PTR_ERR(algt); |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 288 | |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 289 | if ((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask) |
| 290 | return -EINVAL; |
Jussi Kivilinna | 69d3150 | 2012-12-28 12:04:58 +0200 | [diff] [blame] | 291 | |
| 292 | cipher_name = crypto_attr_alg_name(tb[1]); |
Jussi Kivilinna | 69d3150 | 2012-12-28 12:04:58 +0200 | [diff] [blame] | 293 | if (IS_ERR(cipher_name)) |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 294 | return PTR_ERR(cipher_name); |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 295 | |
Jussi Kivilinna | 69d3150 | 2012-12-28 12:04:58 +0200 | [diff] [blame] | 296 | inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL); |
| 297 | if (!inst) |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 298 | return -ENOMEM; |
Jussi Kivilinna | 69d3150 | 2012-12-28 12:04:58 +0200 | [diff] [blame] | 299 | |
Marcelo Cerri | d2c2a85 | 2017-02-27 09:38:26 -0300 | [diff] [blame] | 300 | mask = crypto_requires_sync(algt->type, algt->mask) | |
| 301 | crypto_requires_off(algt->type, algt->mask, |
| 302 | CRYPTO_ALG_NEED_FALLBACK); |
| 303 | |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 304 | spawn = skcipher_instance_ctx(inst); |
Jussi Kivilinna | 69d3150 | 2012-12-28 12:04:58 +0200 | [diff] [blame] | 305 | |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 306 | crypto_set_skcipher_spawn(spawn, skcipher_crypto_instance(inst)); |
Marcelo Cerri | d2c2a85 | 2017-02-27 09:38:26 -0300 | [diff] [blame] | 307 | err = crypto_grab_skcipher(spawn, cipher_name, 0, mask); |
Jussi Kivilinna | 69d3150 | 2012-12-28 12:04:58 +0200 | [diff] [blame] | 308 | if (err) |
| 309 | goto err_free_inst; |
| 310 | |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 311 | alg = crypto_spawn_skcipher_alg(spawn); |
Jussi Kivilinna | 69d3150 | 2012-12-28 12:04:58 +0200 | [diff] [blame] | 312 | |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 313 | /* We only support 16-byte blocks. */ |
| 314 | err = -EINVAL; |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 315 | if (crypto_skcipher_alg_ivsize(alg) != CTR_RFC3686_BLOCK_SIZE) |
Jussi Kivilinna | 69d3150 | 2012-12-28 12:04:58 +0200 | [diff] [blame] | 316 | goto err_drop_spawn; |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 317 | |
| 318 | /* Not a stream cipher? */ |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 319 | if (alg->base.cra_blocksize != 1) |
Jussi Kivilinna | 69d3150 | 2012-12-28 12:04:58 +0200 | [diff] [blame] | 320 | goto err_drop_spawn; |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 321 | |
Jussi Kivilinna | 69d3150 | 2012-12-28 12:04:58 +0200 | [diff] [blame] | 322 | err = -ENAMETOOLONG; |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 323 | if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME, |
| 324 | "rfc3686(%s)", alg->base.cra_name) >= CRYPTO_MAX_ALG_NAME) |
Jussi Kivilinna | 69d3150 | 2012-12-28 12:04:58 +0200 | [diff] [blame] | 325 | goto err_drop_spawn; |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 326 | if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME, |
| 327 | "rfc3686(%s)", alg->base.cra_driver_name) >= |
| 328 | CRYPTO_MAX_ALG_NAME) |
Jussi Kivilinna | 69d3150 | 2012-12-28 12:04:58 +0200 | [diff] [blame] | 329 | goto err_drop_spawn; |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 330 | |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 331 | inst->alg.base.cra_priority = alg->base.cra_priority; |
| 332 | inst->alg.base.cra_blocksize = 1; |
| 333 | inst->alg.base.cra_alignmask = alg->base.cra_alignmask; |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 334 | |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 335 | inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC; |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 336 | |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 337 | inst->alg.ivsize = CTR_RFC3686_IV_SIZE; |
| 338 | inst->alg.chunksize = crypto_skcipher_alg_chunksize(alg); |
| 339 | inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg) + |
| 340 | CTR_RFC3686_NONCE_SIZE; |
| 341 | inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg) + |
| 342 | CTR_RFC3686_NONCE_SIZE; |
Jussi Kivilinna | 69d3150 | 2012-12-28 12:04:58 +0200 | [diff] [blame] | 343 | |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 344 | inst->alg.setkey = crypto_rfc3686_setkey; |
| 345 | inst->alg.encrypt = crypto_rfc3686_crypt; |
| 346 | inst->alg.decrypt = crypto_rfc3686_crypt; |
Jussi Kivilinna | 69d3150 | 2012-12-28 12:04:58 +0200 | [diff] [blame] | 347 | |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 348 | inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc3686_ctx); |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 349 | |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 350 | inst->alg.init = crypto_rfc3686_init_tfm; |
| 351 | inst->alg.exit = crypto_rfc3686_exit_tfm; |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 352 | |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 353 | inst->free = crypto_rfc3686_free; |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 354 | |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 355 | err = skcipher_register_instance(tmpl, inst); |
| 356 | if (err) |
| 357 | goto err_drop_spawn; |
| 358 | |
| 359 | out: |
| 360 | return err; |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 361 | |
Jussi Kivilinna | 69d3150 | 2012-12-28 12:04:58 +0200 | [diff] [blame] | 362 | err_drop_spawn: |
| 363 | crypto_drop_skcipher(spawn); |
| 364 | err_free_inst: |
| 365 | kfree(inst); |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 366 | goto out; |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | static struct crypto_template crypto_rfc3686_tmpl = { |
| 370 | .name = "rfc3686", |
Herbert Xu | b2b39c2 | 2016-07-12 13:17:37 +0800 | [diff] [blame] | 371 | .create = crypto_rfc3686_create, |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 372 | .module = THIS_MODULE, |
| 373 | }; |
| 374 | |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 375 | static int __init crypto_ctr_module_init(void) |
| 376 | { |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 377 | int err; |
| 378 | |
| 379 | err = crypto_register_template(&crypto_ctr_tmpl); |
| 380 | if (err) |
| 381 | goto out; |
| 382 | |
| 383 | err = crypto_register_template(&crypto_rfc3686_tmpl); |
| 384 | if (err) |
| 385 | goto out_drop_ctr; |
| 386 | |
| 387 | out: |
| 388 | return err; |
| 389 | |
| 390 | out_drop_ctr: |
| 391 | crypto_unregister_template(&crypto_ctr_tmpl); |
| 392 | goto out; |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | static void __exit crypto_ctr_module_exit(void) |
| 396 | { |
Herbert Xu | 5311f24 | 2007-12-17 21:34:32 +0800 | [diff] [blame] | 397 | crypto_unregister_template(&crypto_rfc3686_tmpl); |
Joy Latten | 23e353c | 2007-10-23 08:50:32 +0800 | [diff] [blame] | 398 | crypto_unregister_template(&crypto_ctr_tmpl); |
| 399 | } |
| 400 | |
| 401 | module_init(crypto_ctr_module_init); |
| 402 | module_exit(crypto_ctr_module_exit); |
| 403 | |
| 404 | MODULE_LICENSE("GPL"); |
Eric Biggers | 11f1463 | 2019-01-03 20:16:17 -0800 | [diff] [blame^] | 405 | MODULE_DESCRIPTION("CTR block cipher mode of operation"); |
Kees Cook | 5d26a10 | 2014-11-20 17:05:53 -0800 | [diff] [blame] | 406 | MODULE_ALIAS_CRYPTO("rfc3686"); |
Kees Cook | 4943ba1 | 2014-11-24 16:32:38 -0800 | [diff] [blame] | 407 | MODULE_ALIAS_CRYPTO("ctr"); |