blob: ec8f8b67473a01d0bfe81e2f16290d90d1492a52 [file] [log] [blame]
Joy Latten23e353c2007-10-23 08:50:32 +08001/*
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 Xu5311f242007-12-17 21:34:32 +080014#include <crypto/ctr.h>
Jussi Kivilinna69d31502012-12-28 12:04:58 +020015#include <crypto/internal/skcipher.h>
Joy Latten23e353c2007-10-23 08:50:32 +080016#include <linux/err.h>
17#include <linux/init.h>
18#include <linux/kernel.h>
19#include <linux/module.h>
Joy Latten23e353c2007-10-23 08:50:32 +080020#include <linux/slab.h>
21
Herbert Xu5311f242007-12-17 21:34:32 +080022struct crypto_rfc3686_ctx {
Herbert Xub2b39c22016-07-12 13:17:37 +080023 struct crypto_skcipher *child;
Herbert Xu5311f242007-12-17 21:34:32 +080024 u8 nonce[CTR_RFC3686_NONCE_SIZE];
Joy Latten23e353c2007-10-23 08:50:32 +080025};
26
Jussi Kivilinna69d31502012-12-28 12:04:58 +020027struct crypto_rfc3686_req_ctx {
28 u8 iv[CTR_RFC3686_BLOCK_SIZE];
Herbert Xub2b39c22016-07-12 13:17:37 +080029 struct skcipher_request subreq CRYPTO_MINALIGN_ATTR;
Jussi Kivilinna69d31502012-12-28 12:04:58 +020030};
31
Eric Biggers11f14632019-01-03 20:16:17 -080032static void crypto_ctr_crypt_final(struct skcipher_walk *walk,
Herbert Xu5311f242007-12-17 21:34:32 +080033 struct crypto_cipher *tfm)
Herbert Xu0971eb02007-11-30 00:23:53 +110034{
35 unsigned int bsize = crypto_cipher_blocksize(tfm);
Herbert Xu5311f242007-12-17 21:34:32 +080036 unsigned long alignmask = crypto_cipher_alignmask(tfm);
37 u8 *ctrblk = walk->iv;
Salvatore Mesoraca6650c4d2018-04-09 15:54:47 +020038 u8 tmp[MAX_CIPHER_BLOCKSIZE + MAX_CIPHER_ALIGNMASK];
Herbert Xu5311f242007-12-17 21:34:32 +080039 u8 *keystream = PTR_ALIGN(tmp + 0, alignmask + 1);
Herbert Xu0971eb02007-11-30 00:23:53 +110040 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 Biesheuvel45fe93d2017-07-24 11:28:04 +010045 crypto_xor_cpy(dst, keystream, src, nbytes);
Herbert Xu5311f242007-12-17 21:34:32 +080046
47 crypto_inc(ctrblk, bsize);
Herbert Xu0971eb02007-11-30 00:23:53 +110048}
49
Eric Biggers11f14632019-01-03 20:16:17 -080050static int crypto_ctr_crypt_segment(struct skcipher_walk *walk,
Herbert Xu5311f242007-12-17 21:34:32 +080051 struct crypto_cipher *tfm)
Joy Latten23e353c2007-10-23 08:50:32 +080052{
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 Xu5311f242007-12-17 21:34:32 +080056 u8 *ctrblk = walk->iv;
Joy Latten23e353c2007-10-23 08:50:32 +080057 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 Xu0971eb02007-11-30 00:23:53 +110063 fn(crypto_cipher_tfm(tfm), dst, ctrblk);
64 crypto_xor(dst, src, bsize);
Joy Latten23e353c2007-10-23 08:50:32 +080065
66 /* increment counter in counterblock */
Herbert Xu5311f242007-12-17 21:34:32 +080067 crypto_inc(ctrblk, bsize);
Joy Latten23e353c2007-10-23 08:50:32 +080068
Joy Latten23e353c2007-10-23 08:50:32 +080069 src += bsize;
70 dst += bsize;
Herbert Xu0971eb02007-11-30 00:23:53 +110071 } while ((nbytes -= bsize) >= bsize);
Joy Latten23e353c2007-10-23 08:50:32 +080072
Herbert Xu0971eb02007-11-30 00:23:53 +110073 return nbytes;
Joy Latten23e353c2007-10-23 08:50:32 +080074}
75
Eric Biggers11f14632019-01-03 20:16:17 -080076static int crypto_ctr_crypt_inplace(struct skcipher_walk *walk,
Herbert Xu5311f242007-12-17 21:34:32 +080077 struct crypto_cipher *tfm)
Joy Latten23e353c2007-10-23 08:50:32 +080078{
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 Xu5311f242007-12-17 21:34:32 +080082 unsigned long alignmask = crypto_cipher_alignmask(tfm);
Joy Latten23e353c2007-10-23 08:50:32 +080083 unsigned int nbytes = walk->nbytes;
Herbert Xu5311f242007-12-17 21:34:32 +080084 u8 *ctrblk = walk->iv;
Joy Latten23e353c2007-10-23 08:50:32 +080085 u8 *src = walk->src.virt.addr;
Salvatore Mesoraca6650c4d2018-04-09 15:54:47 +020086 u8 tmp[MAX_CIPHER_BLOCKSIZE + MAX_CIPHER_ALIGNMASK];
Herbert Xu5311f242007-12-17 21:34:32 +080087 u8 *keystream = PTR_ALIGN(tmp + 0, alignmask + 1);
Joy Latten23e353c2007-10-23 08:50:32 +080088
89 do {
90 /* create keystream */
91 fn(crypto_cipher_tfm(tfm), keystream, ctrblk);
Herbert Xu0971eb02007-11-30 00:23:53 +110092 crypto_xor(src, keystream, bsize);
Joy Latten23e353c2007-10-23 08:50:32 +080093
94 /* increment counter in counterblock */
Herbert Xu5311f242007-12-17 21:34:32 +080095 crypto_inc(ctrblk, bsize);
Joy Latten23e353c2007-10-23 08:50:32 +080096
Joy Latten23e353c2007-10-23 08:50:32 +080097 src += bsize;
Herbert Xu0971eb02007-11-30 00:23:53 +110098 } while ((nbytes -= bsize) >= bsize);
Joy Latten23e353c2007-10-23 08:50:32 +080099
Herbert Xu0971eb02007-11-30 00:23:53 +1100100 return nbytes;
Joy Latten23e353c2007-10-23 08:50:32 +0800101}
102
Eric Biggers11f14632019-01-03 20:16:17 -0800103static int crypto_ctr_crypt(struct skcipher_request *req)
Joy Latten23e353c2007-10-23 08:50:32 +0800104{
Eric Biggers11f14632019-01-03 20:16:17 -0800105 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 Latten23e353c2007-10-23 08:50:32 +0800110 int err;
111
Eric Biggers11f14632019-01-03 20:16:17 -0800112 err = skcipher_walk_virt(&walk, req, false);
Joy Latten23e353c2007-10-23 08:50:32 +0800113
Herbert Xu0971eb02007-11-30 00:23:53 +1100114 while (walk.nbytes >= bsize) {
Joy Latten23e353c2007-10-23 08:50:32 +0800115 if (walk.src.virt.addr == walk.dst.virt.addr)
Eric Biggers11f14632019-01-03 20:16:17 -0800116 nbytes = crypto_ctr_crypt_inplace(&walk, cipher);
Joy Latten23e353c2007-10-23 08:50:32 +0800117 else
Eric Biggers11f14632019-01-03 20:16:17 -0800118 nbytes = crypto_ctr_crypt_segment(&walk, cipher);
Joy Latten23e353c2007-10-23 08:50:32 +0800119
Eric Biggers11f14632019-01-03 20:16:17 -0800120 err = skcipher_walk_done(&walk, nbytes);
Joy Latten23e353c2007-10-23 08:50:32 +0800121 }
Herbert Xu0971eb02007-11-30 00:23:53 +1100122
123 if (walk.nbytes) {
Eric Biggers11f14632019-01-03 20:16:17 -0800124 crypto_ctr_crypt_final(&walk, cipher);
125 err = skcipher_walk_done(&walk, 0);
Herbert Xu0971eb02007-11-30 00:23:53 +1100126 }
127
Joy Latten23e353c2007-10-23 08:50:32 +0800128 return err;
129}
130
Eric Biggers11f14632019-01-03 20:16:17 -0800131static int crypto_ctr_create(struct crypto_template *tmpl, struct rtattr **tb)
Joy Latten23e353c2007-10-23 08:50:32 +0800132{
Eric Biggers11f14632019-01-03 20:16:17 -0800133 struct skcipher_instance *inst;
Joy Latten23e353c2007-10-23 08:50:32 +0800134 struct crypto_alg *alg;
Joy Latten23e353c2007-10-23 08:50:32 +0800135 int err;
136
Eric Biggers11f14632019-01-03 20:16:17 -0800137 inst = skcipher_alloc_instance_simple(tmpl, tb, &alg);
138 if (IS_ERR(inst))
139 return PTR_ERR(inst);
Joy Latten23e353c2007-10-23 08:50:32 +0800140
Herbert Xu5311f242007-12-17 21:34:32 +0800141 /* Block size must be >= 4 bytes. */
Joy Latten23e353c2007-10-23 08:50:32 +0800142 err = -EINVAL;
Herbert Xu5311f242007-12-17 21:34:32 +0800143 if (alg->cra_blocksize < 4)
Eric Biggers11f14632019-01-03 20:16:17 -0800144 goto out_free_inst;
Joy Latten23e353c2007-10-23 08:50:32 +0800145
Herbert Xu3f8214e2007-11-20 20:32:56 +0800146 /* If this is false we'd fail the alignment of crypto_inc. */
Herbert Xu5311f242007-12-17 21:34:32 +0800147 if (alg->cra_blocksize % 4)
Eric Biggers11f14632019-01-03 20:16:17 -0800148 goto out_free_inst;
Herbert Xu3f8214e2007-11-20 20:32:56 +0800149
Eric Biggers11f14632019-01-03 20:16:17 -0800150 /* CTR mode is a stream cipher. */
151 inst->alg.base.cra_blocksize = 1;
Joy Latten23e353c2007-10-23 08:50:32 +0800152
Eric Biggers11f14632019-01-03 20:16:17 -0800153 /*
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 Latten23e353c2007-10-23 08:50:32 +0800158
Eric Biggers11f14632019-01-03 20:16:17 -0800159 inst->alg.encrypt = crypto_ctr_crypt;
160 inst->alg.decrypt = crypto_ctr_crypt;
Joy Latten23e353c2007-10-23 08:50:32 +0800161
Eric Biggers11f14632019-01-03 20:16:17 -0800162 err = skcipher_register_instance(tmpl, inst);
163 if (err)
164 goto out_free_inst;
165 goto out_put_alg;
Joy Latten23e353c2007-10-23 08:50:32 +0800166
Eric Biggers11f14632019-01-03 20:16:17 -0800167out_free_inst:
168 inst->free(inst);
Joy Latten23e353c2007-10-23 08:50:32 +0800169out_put_alg:
Eric Biggers11f14632019-01-03 20:16:17 -0800170 crypto_mod_put(alg);
171 return err;
Joy Latten23e353c2007-10-23 08:50:32 +0800172}
173
Herbert Xub2b39c22016-07-12 13:17:37 +0800174static int crypto_rfc3686_setkey(struct crypto_skcipher *parent,
Jussi Kivilinna69d31502012-12-28 12:04:58 +0200175 const u8 *key, unsigned int keylen)
Herbert Xu5311f242007-12-17 21:34:32 +0800176{
Herbert Xub2b39c22016-07-12 13:17:37 +0800177 struct crypto_rfc3686_ctx *ctx = crypto_skcipher_ctx(parent);
178 struct crypto_skcipher *child = ctx->child;
Herbert Xu5311f242007-12-17 21:34:32 +0800179 int err;
180
181 /* the nonce is stored in bytes at end of key */
182 if (keylen < CTR_RFC3686_NONCE_SIZE)
183 return -EINVAL;
184
185 memcpy(ctx->nonce, key + (keylen - CTR_RFC3686_NONCE_SIZE),
186 CTR_RFC3686_NONCE_SIZE);
187
188 keylen -= CTR_RFC3686_NONCE_SIZE;
189
Herbert Xub2b39c22016-07-12 13:17:37 +0800190 crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
191 crypto_skcipher_set_flags(child, crypto_skcipher_get_flags(parent) &
192 CRYPTO_TFM_REQ_MASK);
193 err = crypto_skcipher_setkey(child, key, keylen);
194 crypto_skcipher_set_flags(parent, crypto_skcipher_get_flags(child) &
195 CRYPTO_TFM_RES_MASK);
Herbert Xu5311f242007-12-17 21:34:32 +0800196
197 return err;
198}
199
Herbert Xub2b39c22016-07-12 13:17:37 +0800200static int crypto_rfc3686_crypt(struct skcipher_request *req)
Herbert Xu5311f242007-12-17 21:34:32 +0800201{
Herbert Xub2b39c22016-07-12 13:17:37 +0800202 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
203 struct crypto_rfc3686_ctx *ctx = crypto_skcipher_ctx(tfm);
204 struct crypto_skcipher *child = ctx->child;
205 unsigned long align = crypto_skcipher_alignmask(tfm);
Jussi Kivilinna69d31502012-12-28 12:04:58 +0200206 struct crypto_rfc3686_req_ctx *rctx =
Herbert Xub2b39c22016-07-12 13:17:37 +0800207 (void *)PTR_ALIGN((u8 *)skcipher_request_ctx(req), align + 1);
208 struct skcipher_request *subreq = &rctx->subreq;
Jussi Kivilinna69d31502012-12-28 12:04:58 +0200209 u8 *iv = rctx->iv;
Herbert Xu5311f242007-12-17 21:34:32 +0800210
211 /* set up counter block */
212 memcpy(iv, ctx->nonce, CTR_RFC3686_NONCE_SIZE);
Herbert Xub2b39c22016-07-12 13:17:37 +0800213 memcpy(iv + CTR_RFC3686_NONCE_SIZE, req->iv, CTR_RFC3686_IV_SIZE);
Herbert Xu5311f242007-12-17 21:34:32 +0800214
215 /* initialize counter portion of counter block */
216 *(__be32 *)(iv + CTR_RFC3686_NONCE_SIZE + CTR_RFC3686_IV_SIZE) =
217 cpu_to_be32(1);
218
Herbert Xub2b39c22016-07-12 13:17:37 +0800219 skcipher_request_set_tfm(subreq, child);
220 skcipher_request_set_callback(subreq, req->base.flags,
221 req->base.complete, req->base.data);
222 skcipher_request_set_crypt(subreq, req->src, req->dst,
223 req->cryptlen, iv);
Herbert Xu5311f242007-12-17 21:34:32 +0800224
Herbert Xub2b39c22016-07-12 13:17:37 +0800225 return crypto_skcipher_encrypt(subreq);
Herbert Xu5311f242007-12-17 21:34:32 +0800226}
227
Herbert Xub2b39c22016-07-12 13:17:37 +0800228static int crypto_rfc3686_init_tfm(struct crypto_skcipher *tfm)
Herbert Xu5311f242007-12-17 21:34:32 +0800229{
Herbert Xub2b39c22016-07-12 13:17:37 +0800230 struct skcipher_instance *inst = skcipher_alg_instance(tfm);
231 struct crypto_skcipher_spawn *spawn = skcipher_instance_ctx(inst);
232 struct crypto_rfc3686_ctx *ctx = crypto_skcipher_ctx(tfm);
233 struct crypto_skcipher *cipher;
Jussi Kivilinna69d31502012-12-28 12:04:58 +0200234 unsigned long align;
Herbert Xub2b39c22016-07-12 13:17:37 +0800235 unsigned int reqsize;
Herbert Xu5311f242007-12-17 21:34:32 +0800236
Eric Biggers60425a82016-10-28 09:52:19 -0700237 cipher = crypto_spawn_skcipher(spawn);
Herbert Xu5311f242007-12-17 21:34:32 +0800238 if (IS_ERR(cipher))
239 return PTR_ERR(cipher);
240
241 ctx->child = cipher;
242
Herbert Xub2b39c22016-07-12 13:17:37 +0800243 align = crypto_skcipher_alignmask(tfm);
Jussi Kivilinna69d31502012-12-28 12:04:58 +0200244 align &= ~(crypto_tfm_ctx_alignment() - 1);
Herbert Xub2b39c22016-07-12 13:17:37 +0800245 reqsize = align + sizeof(struct crypto_rfc3686_req_ctx) +
246 crypto_skcipher_reqsize(cipher);
247 crypto_skcipher_set_reqsize(tfm, reqsize);
Jussi Kivilinna69d31502012-12-28 12:04:58 +0200248
Herbert Xu5311f242007-12-17 21:34:32 +0800249 return 0;
250}
251
Herbert Xub2b39c22016-07-12 13:17:37 +0800252static void crypto_rfc3686_exit_tfm(struct crypto_skcipher *tfm)
Herbert Xu5311f242007-12-17 21:34:32 +0800253{
Herbert Xub2b39c22016-07-12 13:17:37 +0800254 struct crypto_rfc3686_ctx *ctx = crypto_skcipher_ctx(tfm);
Herbert Xu5311f242007-12-17 21:34:32 +0800255
Herbert Xub2b39c22016-07-12 13:17:37 +0800256 crypto_free_skcipher(ctx->child);
Herbert Xu5311f242007-12-17 21:34:32 +0800257}
258
Herbert Xub2b39c22016-07-12 13:17:37 +0800259static void crypto_rfc3686_free(struct skcipher_instance *inst)
260{
261 struct crypto_skcipher_spawn *spawn = skcipher_instance_ctx(inst);
262
263 crypto_drop_skcipher(spawn);
264 kfree(inst);
265}
266
267static int crypto_rfc3686_create(struct crypto_template *tmpl,
268 struct rtattr **tb)
Herbert Xu5311f242007-12-17 21:34:32 +0800269{
Jussi Kivilinna69d31502012-12-28 12:04:58 +0200270 struct crypto_attr_type *algt;
Herbert Xub2b39c22016-07-12 13:17:37 +0800271 struct skcipher_instance *inst;
272 struct skcipher_alg *alg;
Jussi Kivilinna69d31502012-12-28 12:04:58 +0200273 struct crypto_skcipher_spawn *spawn;
274 const char *cipher_name;
Marcelo Cerrid2c2a852017-02-27 09:38:26 -0300275 u32 mask;
276
Herbert Xu5311f242007-12-17 21:34:32 +0800277 int err;
278
Jussi Kivilinna69d31502012-12-28 12:04:58 +0200279 algt = crypto_get_attr_type(tb);
Jussi Kivilinna69d31502012-12-28 12:04:58 +0200280 if (IS_ERR(algt))
Herbert Xub2b39c22016-07-12 13:17:37 +0800281 return PTR_ERR(algt);
Herbert Xu5311f242007-12-17 21:34:32 +0800282
Herbert Xub2b39c22016-07-12 13:17:37 +0800283 if ((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask)
284 return -EINVAL;
Jussi Kivilinna69d31502012-12-28 12:04:58 +0200285
286 cipher_name = crypto_attr_alg_name(tb[1]);
Jussi Kivilinna69d31502012-12-28 12:04:58 +0200287 if (IS_ERR(cipher_name))
Herbert Xub2b39c22016-07-12 13:17:37 +0800288 return PTR_ERR(cipher_name);
Herbert Xu5311f242007-12-17 21:34:32 +0800289
Jussi Kivilinna69d31502012-12-28 12:04:58 +0200290 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
291 if (!inst)
Herbert Xub2b39c22016-07-12 13:17:37 +0800292 return -ENOMEM;
Jussi Kivilinna69d31502012-12-28 12:04:58 +0200293
Marcelo Cerrid2c2a852017-02-27 09:38:26 -0300294 mask = crypto_requires_sync(algt->type, algt->mask) |
295 crypto_requires_off(algt->type, algt->mask,
296 CRYPTO_ALG_NEED_FALLBACK);
297
Herbert Xub2b39c22016-07-12 13:17:37 +0800298 spawn = skcipher_instance_ctx(inst);
Jussi Kivilinna69d31502012-12-28 12:04:58 +0200299
Herbert Xub2b39c22016-07-12 13:17:37 +0800300 crypto_set_skcipher_spawn(spawn, skcipher_crypto_instance(inst));
Marcelo Cerrid2c2a852017-02-27 09:38:26 -0300301 err = crypto_grab_skcipher(spawn, cipher_name, 0, mask);
Jussi Kivilinna69d31502012-12-28 12:04:58 +0200302 if (err)
303 goto err_free_inst;
304
Herbert Xub2b39c22016-07-12 13:17:37 +0800305 alg = crypto_spawn_skcipher_alg(spawn);
Jussi Kivilinna69d31502012-12-28 12:04:58 +0200306
Herbert Xu5311f242007-12-17 21:34:32 +0800307 /* We only support 16-byte blocks. */
308 err = -EINVAL;
Herbert Xub2b39c22016-07-12 13:17:37 +0800309 if (crypto_skcipher_alg_ivsize(alg) != CTR_RFC3686_BLOCK_SIZE)
Jussi Kivilinna69d31502012-12-28 12:04:58 +0200310 goto err_drop_spawn;
Herbert Xu5311f242007-12-17 21:34:32 +0800311
312 /* Not a stream cipher? */
Herbert Xub2b39c22016-07-12 13:17:37 +0800313 if (alg->base.cra_blocksize != 1)
Jussi Kivilinna69d31502012-12-28 12:04:58 +0200314 goto err_drop_spawn;
Herbert Xu5311f242007-12-17 21:34:32 +0800315
Jussi Kivilinna69d31502012-12-28 12:04:58 +0200316 err = -ENAMETOOLONG;
Herbert Xub2b39c22016-07-12 13:17:37 +0800317 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
318 "rfc3686(%s)", alg->base.cra_name) >= CRYPTO_MAX_ALG_NAME)
Jussi Kivilinna69d31502012-12-28 12:04:58 +0200319 goto err_drop_spawn;
Herbert Xub2b39c22016-07-12 13:17:37 +0800320 if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
321 "rfc3686(%s)", alg->base.cra_driver_name) >=
322 CRYPTO_MAX_ALG_NAME)
Jussi Kivilinna69d31502012-12-28 12:04:58 +0200323 goto err_drop_spawn;
Herbert Xu5311f242007-12-17 21:34:32 +0800324
Herbert Xub2b39c22016-07-12 13:17:37 +0800325 inst->alg.base.cra_priority = alg->base.cra_priority;
326 inst->alg.base.cra_blocksize = 1;
327 inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
Herbert Xu5311f242007-12-17 21:34:32 +0800328
Herbert Xub2b39c22016-07-12 13:17:37 +0800329 inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
Herbert Xu5311f242007-12-17 21:34:32 +0800330
Herbert Xub2b39c22016-07-12 13:17:37 +0800331 inst->alg.ivsize = CTR_RFC3686_IV_SIZE;
332 inst->alg.chunksize = crypto_skcipher_alg_chunksize(alg);
333 inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg) +
334 CTR_RFC3686_NONCE_SIZE;
335 inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg) +
336 CTR_RFC3686_NONCE_SIZE;
Jussi Kivilinna69d31502012-12-28 12:04:58 +0200337
Herbert Xub2b39c22016-07-12 13:17:37 +0800338 inst->alg.setkey = crypto_rfc3686_setkey;
339 inst->alg.encrypt = crypto_rfc3686_crypt;
340 inst->alg.decrypt = crypto_rfc3686_crypt;
Jussi Kivilinna69d31502012-12-28 12:04:58 +0200341
Herbert Xub2b39c22016-07-12 13:17:37 +0800342 inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc3686_ctx);
Herbert Xu0a270322007-11-30 21:38:37 +1100343
Herbert Xub2b39c22016-07-12 13:17:37 +0800344 inst->alg.init = crypto_rfc3686_init_tfm;
345 inst->alg.exit = crypto_rfc3686_exit_tfm;
Herbert Xu5311f242007-12-17 21:34:32 +0800346
Herbert Xub2b39c22016-07-12 13:17:37 +0800347 inst->free = crypto_rfc3686_free;
Herbert Xu5311f242007-12-17 21:34:32 +0800348
Herbert Xub2b39c22016-07-12 13:17:37 +0800349 err = skcipher_register_instance(tmpl, inst);
350 if (err)
351 goto err_drop_spawn;
352
353out:
354 return err;
Herbert Xu5311f242007-12-17 21:34:32 +0800355
Jussi Kivilinna69d31502012-12-28 12:04:58 +0200356err_drop_spawn:
357 crypto_drop_skcipher(spawn);
358err_free_inst:
359 kfree(inst);
Herbert Xub2b39c22016-07-12 13:17:37 +0800360 goto out;
Herbert Xu5311f242007-12-17 21:34:32 +0800361}
362
Xiongfeng Wang9f8ef362019-01-18 13:58:14 +0800363static struct crypto_template crypto_ctr_tmpls[] = {
364 {
365 .name = "ctr",
366 .create = crypto_ctr_create,
367 .module = THIS_MODULE,
368 }, {
369 .name = "rfc3686",
370 .create = crypto_rfc3686_create,
371 .module = THIS_MODULE,
372 },
Herbert Xu5311f242007-12-17 21:34:32 +0800373};
374
Joy Latten23e353c2007-10-23 08:50:32 +0800375static int __init crypto_ctr_module_init(void)
376{
Xiongfeng Wang9f8ef362019-01-18 13:58:14 +0800377 return crypto_register_templates(crypto_ctr_tmpls,
378 ARRAY_SIZE(crypto_ctr_tmpls));
Joy Latten23e353c2007-10-23 08:50:32 +0800379}
380
381static void __exit crypto_ctr_module_exit(void)
382{
Xiongfeng Wang9f8ef362019-01-18 13:58:14 +0800383 crypto_unregister_templates(crypto_ctr_tmpls,
384 ARRAY_SIZE(crypto_ctr_tmpls));
Joy Latten23e353c2007-10-23 08:50:32 +0800385}
386
387module_init(crypto_ctr_module_init);
388module_exit(crypto_ctr_module_exit);
389
390MODULE_LICENSE("GPL");
Eric Biggers11f14632019-01-03 20:16:17 -0800391MODULE_DESCRIPTION("CTR block cipher mode of operation");
Kees Cook5d26a102014-11-20 17:05:53 -0800392MODULE_ALIAS_CRYPTO("rfc3686");
Kees Cook4943ba12014-11-24 16:32:38 -0800393MODULE_ALIAS_CRYPTO("ctr");