blob: 23c698b220130423e7261298234b345ddc230310 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Joy Latten23e353c2007-10-23 08:50:32 +08002/*
3 * CTR: Counter mode
4 *
5 * (C) Copyright IBM Corp. 2007 - Joy Latten <latten@us.ibm.com>
Joy Latten23e353c2007-10-23 08:50:32 +08006 */
7
8#include <crypto/algapi.h>
Herbert Xu5311f242007-12-17 21:34:32 +08009#include <crypto/ctr.h>
Ard Biesheuvel0eb76ba2020-12-11 13:27:15 +010010#include <crypto/internal/cipher.h>
Jussi Kivilinna69d3150c2012-12-28 12:04:58 +020011#include <crypto/internal/skcipher.h>
Joy Latten23e353c2007-10-23 08:50:32 +080012#include <linux/err.h>
13#include <linux/init.h>
14#include <linux/kernel.h>
15#include <linux/module.h>
Joy Latten23e353c2007-10-23 08:50:32 +080016#include <linux/slab.h>
17
Herbert Xu5311f242007-12-17 21:34:32 +080018struct crypto_rfc3686_ctx {
Herbert Xub2b39c22016-07-12 13:17:37 +080019 struct crypto_skcipher *child;
Herbert Xu5311f242007-12-17 21:34:32 +080020 u8 nonce[CTR_RFC3686_NONCE_SIZE];
Joy Latten23e353c2007-10-23 08:50:32 +080021};
22
Jussi Kivilinna69d3150c2012-12-28 12:04:58 +020023struct crypto_rfc3686_req_ctx {
24 u8 iv[CTR_RFC3686_BLOCK_SIZE];
Herbert Xub2b39c22016-07-12 13:17:37 +080025 struct skcipher_request subreq CRYPTO_MINALIGN_ATTR;
Jussi Kivilinna69d3150c2012-12-28 12:04:58 +020026};
27
Eric Biggers11f14632019-01-03 20:16:17 -080028static void crypto_ctr_crypt_final(struct skcipher_walk *walk,
Herbert Xu5311f242007-12-17 21:34:32 +080029 struct crypto_cipher *tfm)
Herbert Xu0971eb02007-11-30 00:23:53 +110030{
31 unsigned int bsize = crypto_cipher_blocksize(tfm);
Herbert Xu5311f242007-12-17 21:34:32 +080032 unsigned long alignmask = crypto_cipher_alignmask(tfm);
33 u8 *ctrblk = walk->iv;
Salvatore Mesoraca6650c4d2018-04-09 15:54:47 +020034 u8 tmp[MAX_CIPHER_BLOCKSIZE + MAX_CIPHER_ALIGNMASK];
Herbert Xu5311f242007-12-17 21:34:32 +080035 u8 *keystream = PTR_ALIGN(tmp + 0, alignmask + 1);
Herbert Xu0971eb02007-11-30 00:23:53 +110036 u8 *src = walk->src.virt.addr;
37 u8 *dst = walk->dst.virt.addr;
38 unsigned int nbytes = walk->nbytes;
39
40 crypto_cipher_encrypt_one(tfm, keystream, ctrblk);
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +010041 crypto_xor_cpy(dst, keystream, src, nbytes);
Herbert Xu5311f242007-12-17 21:34:32 +080042
43 crypto_inc(ctrblk, bsize);
Herbert Xu0971eb02007-11-30 00:23:53 +110044}
45
Eric Biggers11f14632019-01-03 20:16:17 -080046static int crypto_ctr_crypt_segment(struct skcipher_walk *walk,
Herbert Xu5311f242007-12-17 21:34:32 +080047 struct crypto_cipher *tfm)
Joy Latten23e353c2007-10-23 08:50:32 +080048{
49 void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
50 crypto_cipher_alg(tfm)->cia_encrypt;
51 unsigned int bsize = crypto_cipher_blocksize(tfm);
Herbert Xu5311f242007-12-17 21:34:32 +080052 u8 *ctrblk = walk->iv;
Joy Latten23e353c2007-10-23 08:50:32 +080053 u8 *src = walk->src.virt.addr;
54 u8 *dst = walk->dst.virt.addr;
55 unsigned int nbytes = walk->nbytes;
56
57 do {
58 /* create keystream */
Herbert Xu0971eb02007-11-30 00:23:53 +110059 fn(crypto_cipher_tfm(tfm), dst, ctrblk);
60 crypto_xor(dst, src, bsize);
Joy Latten23e353c2007-10-23 08:50:32 +080061
62 /* increment counter in counterblock */
Herbert Xu5311f242007-12-17 21:34:32 +080063 crypto_inc(ctrblk, bsize);
Joy Latten23e353c2007-10-23 08:50:32 +080064
Joy Latten23e353c2007-10-23 08:50:32 +080065 src += bsize;
66 dst += bsize;
Herbert Xu0971eb02007-11-30 00:23:53 +110067 } while ((nbytes -= bsize) >= bsize);
Joy Latten23e353c2007-10-23 08:50:32 +080068
Herbert Xu0971eb02007-11-30 00:23:53 +110069 return nbytes;
Joy Latten23e353c2007-10-23 08:50:32 +080070}
71
Eric Biggers11f14632019-01-03 20:16:17 -080072static int crypto_ctr_crypt_inplace(struct skcipher_walk *walk,
Herbert Xu5311f242007-12-17 21:34:32 +080073 struct crypto_cipher *tfm)
Joy Latten23e353c2007-10-23 08:50:32 +080074{
75 void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
76 crypto_cipher_alg(tfm)->cia_encrypt;
77 unsigned int bsize = crypto_cipher_blocksize(tfm);
Herbert Xu5311f242007-12-17 21:34:32 +080078 unsigned long alignmask = crypto_cipher_alignmask(tfm);
Joy Latten23e353c2007-10-23 08:50:32 +080079 unsigned int nbytes = walk->nbytes;
Herbert Xu5311f242007-12-17 21:34:32 +080080 u8 *ctrblk = walk->iv;
Joy Latten23e353c2007-10-23 08:50:32 +080081 u8 *src = walk->src.virt.addr;
Salvatore Mesoraca6650c4d2018-04-09 15:54:47 +020082 u8 tmp[MAX_CIPHER_BLOCKSIZE + MAX_CIPHER_ALIGNMASK];
Herbert Xu5311f242007-12-17 21:34:32 +080083 u8 *keystream = PTR_ALIGN(tmp + 0, alignmask + 1);
Joy Latten23e353c2007-10-23 08:50:32 +080084
85 do {
86 /* create keystream */
87 fn(crypto_cipher_tfm(tfm), keystream, ctrblk);
Herbert Xu0971eb02007-11-30 00:23:53 +110088 crypto_xor(src, keystream, bsize);
Joy Latten23e353c2007-10-23 08:50:32 +080089
90 /* increment counter in counterblock */
Herbert Xu5311f242007-12-17 21:34:32 +080091 crypto_inc(ctrblk, bsize);
Joy Latten23e353c2007-10-23 08:50:32 +080092
Joy Latten23e353c2007-10-23 08:50:32 +080093 src += bsize;
Herbert Xu0971eb02007-11-30 00:23:53 +110094 } while ((nbytes -= bsize) >= bsize);
Joy Latten23e353c2007-10-23 08:50:32 +080095
Herbert Xu0971eb02007-11-30 00:23:53 +110096 return nbytes;
Joy Latten23e353c2007-10-23 08:50:32 +080097}
98
Eric Biggers11f14632019-01-03 20:16:17 -080099static int crypto_ctr_crypt(struct skcipher_request *req)
Joy Latten23e353c2007-10-23 08:50:32 +0800100{
Eric Biggers11f14632019-01-03 20:16:17 -0800101 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
102 struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
103 const unsigned int bsize = crypto_cipher_blocksize(cipher);
104 struct skcipher_walk walk;
105 unsigned int nbytes;
Joy Latten23e353c2007-10-23 08:50:32 +0800106 int err;
107
Eric Biggers11f14632019-01-03 20:16:17 -0800108 err = skcipher_walk_virt(&walk, req, false);
Joy Latten23e353c2007-10-23 08:50:32 +0800109
Herbert Xu0971eb02007-11-30 00:23:53 +1100110 while (walk.nbytes >= bsize) {
Joy Latten23e353c2007-10-23 08:50:32 +0800111 if (walk.src.virt.addr == walk.dst.virt.addr)
Eric Biggers11f14632019-01-03 20:16:17 -0800112 nbytes = crypto_ctr_crypt_inplace(&walk, cipher);
Joy Latten23e353c2007-10-23 08:50:32 +0800113 else
Eric Biggers11f14632019-01-03 20:16:17 -0800114 nbytes = crypto_ctr_crypt_segment(&walk, cipher);
Joy Latten23e353c2007-10-23 08:50:32 +0800115
Eric Biggers11f14632019-01-03 20:16:17 -0800116 err = skcipher_walk_done(&walk, nbytes);
Joy Latten23e353c2007-10-23 08:50:32 +0800117 }
Herbert Xu0971eb02007-11-30 00:23:53 +1100118
119 if (walk.nbytes) {
Eric Biggers11f14632019-01-03 20:16:17 -0800120 crypto_ctr_crypt_final(&walk, cipher);
121 err = skcipher_walk_done(&walk, 0);
Herbert Xu0971eb02007-11-30 00:23:53 +1100122 }
123
Joy Latten23e353c2007-10-23 08:50:32 +0800124 return err;
125}
126
Eric Biggers11f14632019-01-03 20:16:17 -0800127static int crypto_ctr_create(struct crypto_template *tmpl, struct rtattr **tb)
Joy Latten23e353c2007-10-23 08:50:32 +0800128{
Eric Biggers11f14632019-01-03 20:16:17 -0800129 struct skcipher_instance *inst;
Joy Latten23e353c2007-10-23 08:50:32 +0800130 struct crypto_alg *alg;
Joy Latten23e353c2007-10-23 08:50:32 +0800131 int err;
132
Herbert Xub3c16bf2019-12-20 13:29:40 +0800133 inst = skcipher_alloc_instance_simple(tmpl, tb);
Eric Biggers11f14632019-01-03 20:16:17 -0800134 if (IS_ERR(inst))
135 return PTR_ERR(inst);
Joy Latten23e353c2007-10-23 08:50:32 +0800136
Herbert Xub3c16bf2019-12-20 13:29:40 +0800137 alg = skcipher_ialg_simple(inst);
138
Herbert Xu5311f242007-12-17 21:34:32 +0800139 /* Block size must be >= 4 bytes. */
Joy Latten23e353c2007-10-23 08:50:32 +0800140 err = -EINVAL;
Herbert Xu5311f242007-12-17 21:34:32 +0800141 if (alg->cra_blocksize < 4)
Eric Biggers11f14632019-01-03 20:16:17 -0800142 goto out_free_inst;
Joy Latten23e353c2007-10-23 08:50:32 +0800143
Herbert Xu3f8214e2007-11-20 20:32:56 +0800144 /* If this is false we'd fail the alignment of crypto_inc. */
Herbert Xu5311f242007-12-17 21:34:32 +0800145 if (alg->cra_blocksize % 4)
Eric Biggers11f14632019-01-03 20:16:17 -0800146 goto out_free_inst;
Herbert Xu3f8214e2007-11-20 20:32:56 +0800147
Eric Biggers11f14632019-01-03 20:16:17 -0800148 /* CTR mode is a stream cipher. */
149 inst->alg.base.cra_blocksize = 1;
Joy Latten23e353c2007-10-23 08:50:32 +0800150
Eric Biggers11f14632019-01-03 20:16:17 -0800151 /*
152 * To simplify the implementation, configure the skcipher walk to only
153 * give a partial block at the very end, never earlier.
154 */
155 inst->alg.chunksize = alg->cra_blocksize;
Joy Latten23e353c2007-10-23 08:50:32 +0800156
Eric Biggers11f14632019-01-03 20:16:17 -0800157 inst->alg.encrypt = crypto_ctr_crypt;
158 inst->alg.decrypt = crypto_ctr_crypt;
Joy Latten23e353c2007-10-23 08:50:32 +0800159
Eric Biggers11f14632019-01-03 20:16:17 -0800160 err = skcipher_register_instance(tmpl, inst);
Herbert Xub3c16bf2019-12-20 13:29:40 +0800161 if (err) {
Eric Biggers11f14632019-01-03 20:16:17 -0800162out_free_inst:
Herbert Xub3c16bf2019-12-20 13:29:40 +0800163 inst->free(inst);
164 }
165
Eric Biggers11f14632019-01-03 20:16:17 -0800166 return err;
Joy Latten23e353c2007-10-23 08:50:32 +0800167}
168
Herbert Xub2b39c22016-07-12 13:17:37 +0800169static int crypto_rfc3686_setkey(struct crypto_skcipher *parent,
Jussi Kivilinna69d3150c2012-12-28 12:04:58 +0200170 const u8 *key, unsigned int keylen)
Herbert Xu5311f242007-12-17 21:34:32 +0800171{
Herbert Xub2b39c22016-07-12 13:17:37 +0800172 struct crypto_rfc3686_ctx *ctx = crypto_skcipher_ctx(parent);
173 struct crypto_skcipher *child = ctx->child;
Herbert Xu5311f242007-12-17 21:34:32 +0800174
175 /* the nonce is stored in bytes at end of key */
176 if (keylen < CTR_RFC3686_NONCE_SIZE)
177 return -EINVAL;
178
179 memcpy(ctx->nonce, key + (keylen - CTR_RFC3686_NONCE_SIZE),
180 CTR_RFC3686_NONCE_SIZE);
181
182 keylen -= CTR_RFC3686_NONCE_SIZE;
183
Herbert Xub2b39c22016-07-12 13:17:37 +0800184 crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
185 crypto_skcipher_set_flags(child, crypto_skcipher_get_flags(parent) &
186 CRYPTO_TFM_REQ_MASK);
Eric Biggersaf5034e2019-12-30 21:19:38 -0600187 return crypto_skcipher_setkey(child, key, keylen);
Herbert Xu5311f242007-12-17 21:34:32 +0800188}
189
Herbert Xub2b39c22016-07-12 13:17:37 +0800190static int crypto_rfc3686_crypt(struct skcipher_request *req)
Herbert Xu5311f242007-12-17 21:34:32 +0800191{
Herbert Xub2b39c22016-07-12 13:17:37 +0800192 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
193 struct crypto_rfc3686_ctx *ctx = crypto_skcipher_ctx(tfm);
194 struct crypto_skcipher *child = ctx->child;
195 unsigned long align = crypto_skcipher_alignmask(tfm);
Jussi Kivilinna69d3150c2012-12-28 12:04:58 +0200196 struct crypto_rfc3686_req_ctx *rctx =
Herbert Xub2b39c22016-07-12 13:17:37 +0800197 (void *)PTR_ALIGN((u8 *)skcipher_request_ctx(req), align + 1);
198 struct skcipher_request *subreq = &rctx->subreq;
Jussi Kivilinna69d3150c2012-12-28 12:04:58 +0200199 u8 *iv = rctx->iv;
Herbert Xu5311f242007-12-17 21:34:32 +0800200
201 /* set up counter block */
202 memcpy(iv, ctx->nonce, CTR_RFC3686_NONCE_SIZE);
Herbert Xub2b39c22016-07-12 13:17:37 +0800203 memcpy(iv + CTR_RFC3686_NONCE_SIZE, req->iv, CTR_RFC3686_IV_SIZE);
Herbert Xu5311f242007-12-17 21:34:32 +0800204
205 /* initialize counter portion of counter block */
206 *(__be32 *)(iv + CTR_RFC3686_NONCE_SIZE + CTR_RFC3686_IV_SIZE) =
207 cpu_to_be32(1);
208
Herbert Xub2b39c22016-07-12 13:17:37 +0800209 skcipher_request_set_tfm(subreq, child);
210 skcipher_request_set_callback(subreq, req->base.flags,
211 req->base.complete, req->base.data);
212 skcipher_request_set_crypt(subreq, req->src, req->dst,
213 req->cryptlen, iv);
Herbert Xu5311f242007-12-17 21:34:32 +0800214
Herbert Xub2b39c22016-07-12 13:17:37 +0800215 return crypto_skcipher_encrypt(subreq);
Herbert Xu5311f242007-12-17 21:34:32 +0800216}
217
Herbert Xub2b39c22016-07-12 13:17:37 +0800218static int crypto_rfc3686_init_tfm(struct crypto_skcipher *tfm)
Herbert Xu5311f242007-12-17 21:34:32 +0800219{
Herbert Xub2b39c22016-07-12 13:17:37 +0800220 struct skcipher_instance *inst = skcipher_alg_instance(tfm);
221 struct crypto_skcipher_spawn *spawn = skcipher_instance_ctx(inst);
222 struct crypto_rfc3686_ctx *ctx = crypto_skcipher_ctx(tfm);
223 struct crypto_skcipher *cipher;
Jussi Kivilinna69d3150c2012-12-28 12:04:58 +0200224 unsigned long align;
Herbert Xub2b39c22016-07-12 13:17:37 +0800225 unsigned int reqsize;
Herbert Xu5311f242007-12-17 21:34:32 +0800226
Eric Biggers60425a82016-10-28 09:52:19 -0700227 cipher = crypto_spawn_skcipher(spawn);
Herbert Xu5311f242007-12-17 21:34:32 +0800228 if (IS_ERR(cipher))
229 return PTR_ERR(cipher);
230
231 ctx->child = cipher;
232
Herbert Xub2b39c22016-07-12 13:17:37 +0800233 align = crypto_skcipher_alignmask(tfm);
Jussi Kivilinna69d3150c2012-12-28 12:04:58 +0200234 align &= ~(crypto_tfm_ctx_alignment() - 1);
Herbert Xub2b39c22016-07-12 13:17:37 +0800235 reqsize = align + sizeof(struct crypto_rfc3686_req_ctx) +
236 crypto_skcipher_reqsize(cipher);
237 crypto_skcipher_set_reqsize(tfm, reqsize);
Jussi Kivilinna69d3150c2012-12-28 12:04:58 +0200238
Herbert Xu5311f242007-12-17 21:34:32 +0800239 return 0;
240}
241
Herbert Xub2b39c22016-07-12 13:17:37 +0800242static void crypto_rfc3686_exit_tfm(struct crypto_skcipher *tfm)
Herbert Xu5311f242007-12-17 21:34:32 +0800243{
Herbert Xub2b39c22016-07-12 13:17:37 +0800244 struct crypto_rfc3686_ctx *ctx = crypto_skcipher_ctx(tfm);
Herbert Xu5311f242007-12-17 21:34:32 +0800245
Herbert Xub2b39c22016-07-12 13:17:37 +0800246 crypto_free_skcipher(ctx->child);
Herbert Xu5311f242007-12-17 21:34:32 +0800247}
248
Herbert Xub2b39c22016-07-12 13:17:37 +0800249static void crypto_rfc3686_free(struct skcipher_instance *inst)
250{
251 struct crypto_skcipher_spawn *spawn = skcipher_instance_ctx(inst);
252
253 crypto_drop_skcipher(spawn);
254 kfree(inst);
255}
256
257static int crypto_rfc3686_create(struct crypto_template *tmpl,
258 struct rtattr **tb)
Herbert Xu5311f242007-12-17 21:34:32 +0800259{
Herbert Xub2b39c22016-07-12 13:17:37 +0800260 struct skcipher_instance *inst;
261 struct skcipher_alg *alg;
Jussi Kivilinna69d3150c2012-12-28 12:04:58 +0200262 struct crypto_skcipher_spawn *spawn;
Marcelo Cerrid2c2a852017-02-27 09:38:26 -0300263 u32 mask;
Herbert Xu5311f242007-12-17 21:34:32 +0800264 int err;
265
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700266 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SKCIPHER, &mask);
267 if (err)
268 return err;
Jussi Kivilinna69d3150c2012-12-28 12:04:58 +0200269
Jussi Kivilinna69d3150c2012-12-28 12:04:58 +0200270 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
271 if (!inst)
Herbert Xub2b39c22016-07-12 13:17:37 +0800272 return -ENOMEM;
Jussi Kivilinna69d3150c2012-12-28 12:04:58 +0200273
Herbert Xub2b39c22016-07-12 13:17:37 +0800274 spawn = skcipher_instance_ctx(inst);
Jussi Kivilinna69d3150c2012-12-28 12:04:58 +0200275
Eric Biggersb9f76dd2020-01-02 19:58:45 -0800276 err = crypto_grab_skcipher(spawn, skcipher_crypto_instance(inst),
Eric Biggersa108dfc2020-02-25 20:59:16 -0800277 crypto_attr_alg_name(tb[1]), 0, mask);
Jussi Kivilinna69d3150c2012-12-28 12:04:58 +0200278 if (err)
279 goto err_free_inst;
280
Herbert Xub2b39c22016-07-12 13:17:37 +0800281 alg = crypto_spawn_skcipher_alg(spawn);
Jussi Kivilinna69d3150c2012-12-28 12:04:58 +0200282
Herbert Xu5311f242007-12-17 21:34:32 +0800283 /* We only support 16-byte blocks. */
284 err = -EINVAL;
Herbert Xub2b39c22016-07-12 13:17:37 +0800285 if (crypto_skcipher_alg_ivsize(alg) != CTR_RFC3686_BLOCK_SIZE)
Eric Biggersa108dfc2020-02-25 20:59:16 -0800286 goto err_free_inst;
Herbert Xu5311f242007-12-17 21:34:32 +0800287
288 /* Not a stream cipher? */
Herbert Xub2b39c22016-07-12 13:17:37 +0800289 if (alg->base.cra_blocksize != 1)
Eric Biggersa108dfc2020-02-25 20:59:16 -0800290 goto err_free_inst;
Herbert Xu5311f242007-12-17 21:34:32 +0800291
Jussi Kivilinna69d3150c2012-12-28 12:04:58 +0200292 err = -ENAMETOOLONG;
Herbert Xub2b39c22016-07-12 13:17:37 +0800293 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
294 "rfc3686(%s)", alg->base.cra_name) >= CRYPTO_MAX_ALG_NAME)
Eric Biggersa108dfc2020-02-25 20:59:16 -0800295 goto err_free_inst;
Herbert Xub2b39c22016-07-12 13:17:37 +0800296 if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
297 "rfc3686(%s)", alg->base.cra_driver_name) >=
298 CRYPTO_MAX_ALG_NAME)
Eric Biggersa108dfc2020-02-25 20:59:16 -0800299 goto err_free_inst;
Herbert Xu5311f242007-12-17 21:34:32 +0800300
Herbert Xub2b39c22016-07-12 13:17:37 +0800301 inst->alg.base.cra_priority = alg->base.cra_priority;
302 inst->alg.base.cra_blocksize = 1;
303 inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
Herbert Xu5311f242007-12-17 21:34:32 +0800304
Herbert Xub2b39c22016-07-12 13:17:37 +0800305 inst->alg.ivsize = CTR_RFC3686_IV_SIZE;
306 inst->alg.chunksize = crypto_skcipher_alg_chunksize(alg);
307 inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg) +
308 CTR_RFC3686_NONCE_SIZE;
309 inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg) +
310 CTR_RFC3686_NONCE_SIZE;
Jussi Kivilinna69d3150c2012-12-28 12:04:58 +0200311
Herbert Xub2b39c22016-07-12 13:17:37 +0800312 inst->alg.setkey = crypto_rfc3686_setkey;
313 inst->alg.encrypt = crypto_rfc3686_crypt;
314 inst->alg.decrypt = crypto_rfc3686_crypt;
Jussi Kivilinna69d3150c2012-12-28 12:04:58 +0200315
Herbert Xub2b39c22016-07-12 13:17:37 +0800316 inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc3686_ctx);
Herbert Xu0a270322007-11-30 21:38:37 +1100317
Herbert Xub2b39c22016-07-12 13:17:37 +0800318 inst->alg.init = crypto_rfc3686_init_tfm;
319 inst->alg.exit = crypto_rfc3686_exit_tfm;
Herbert Xu5311f242007-12-17 21:34:32 +0800320
Herbert Xub2b39c22016-07-12 13:17:37 +0800321 inst->free = crypto_rfc3686_free;
Herbert Xu5311f242007-12-17 21:34:32 +0800322
Herbert Xub2b39c22016-07-12 13:17:37 +0800323 err = skcipher_register_instance(tmpl, inst);
Eric Biggersa108dfc2020-02-25 20:59:16 -0800324 if (err) {
Jussi Kivilinna69d3150c2012-12-28 12:04:58 +0200325err_free_inst:
Eric Biggersa108dfc2020-02-25 20:59:16 -0800326 crypto_rfc3686_free(inst);
327 }
328 return err;
Herbert Xu5311f242007-12-17 21:34:32 +0800329}
330
Xiongfeng Wang9f8ef362019-01-18 13:58:14 +0800331static struct crypto_template crypto_ctr_tmpls[] = {
332 {
333 .name = "ctr",
334 .create = crypto_ctr_create,
335 .module = THIS_MODULE,
336 }, {
337 .name = "rfc3686",
338 .create = crypto_rfc3686_create,
339 .module = THIS_MODULE,
340 },
Herbert Xu5311f242007-12-17 21:34:32 +0800341};
342
Joy Latten23e353c2007-10-23 08:50:32 +0800343static int __init crypto_ctr_module_init(void)
344{
Xiongfeng Wang9f8ef362019-01-18 13:58:14 +0800345 return crypto_register_templates(crypto_ctr_tmpls,
346 ARRAY_SIZE(crypto_ctr_tmpls));
Joy Latten23e353c2007-10-23 08:50:32 +0800347}
348
349static void __exit crypto_ctr_module_exit(void)
350{
Xiongfeng Wang9f8ef362019-01-18 13:58:14 +0800351 crypto_unregister_templates(crypto_ctr_tmpls,
352 ARRAY_SIZE(crypto_ctr_tmpls));
Joy Latten23e353c2007-10-23 08:50:32 +0800353}
354
Eric Biggersc4741b22019-04-11 21:57:42 -0700355subsys_initcall(crypto_ctr_module_init);
Joy Latten23e353c2007-10-23 08:50:32 +0800356module_exit(crypto_ctr_module_exit);
357
358MODULE_LICENSE("GPL");
Eric Biggers11f14632019-01-03 20:16:17 -0800359MODULE_DESCRIPTION("CTR block cipher mode of operation");
Kees Cook5d26a102014-11-20 17:05:53 -0800360MODULE_ALIAS_CRYPTO("rfc3686");
Kees Cook4943ba12014-11-24 16:32:38 -0800361MODULE_ALIAS_CRYPTO("ctr");
Ard Biesheuvel0eb76ba2020-12-11 13:27:15 +0100362MODULE_IMPORT_NS(CRYPTO_INTERNAL);