46 #include <linux/kernel.h>
48 #include <linux/module.h>
51 #include <linux/slab.h>
65 crypto_blkcipher_set_flags(child, crypto_tfm_get_flags(parent) &
67 err = crypto_blkcipher_setkey(child, key, keylen);
68 crypto_tfm_set_flags(parent, crypto_blkcipher_get_flags(child) &
80 int bsize = crypto_blkcipher_blocksize(desc->
tfm);
81 u8 tmp[bsize], tmp2[bsize];
84 int lastn = nbytes - bsize;
86 u8 s[bsize * 2],
d[bsize * 2];
100 lcldesc.tfm = ctx->
child;
102 lcldesc.flags = desc->
flags;
104 sg_set_buf(&sgsrc[0], s, bsize);
105 sg_set_buf(&sgdst[0], tmp, bsize);
106 err = crypto_blkcipher_encrypt_iv(&lcldesc, sgdst, sgsrc, bsize);
108 memcpy(d + bsize, tmp, lastn);
112 sg_set_buf(&sgsrc[0], s + bsize, bsize);
113 sg_set_buf(&sgdst[0], tmp2, bsize);
114 err = crypto_blkcipher_encrypt_iv(&lcldesc, sgdst, sgsrc, bsize);
130 int bsize = crypto_blkcipher_blocksize(desc->
tfm);
131 int tot_blocks = (nbytes + bsize - 1) / bsize;
132 int cbc_blocks = tot_blocks > 2 ? tot_blocks - 2 : 0;
136 lcldesc.tfm = ctx->
child;
137 lcldesc.info = desc->
info;
138 lcldesc.flags = desc->
flags;
140 if (tot_blocks == 1) {
141 err = crypto_blkcipher_encrypt_iv(&lcldesc, dst, src, bsize);
142 }
else if (nbytes <= bsize * 2) {
143 err = cts_cbc_encrypt(ctx, desc, dst, src, 0, nbytes);
146 err = crypto_blkcipher_encrypt_iv(&lcldesc, dst, src,
150 err = cts_cbc_encrypt(ctx, desc, dst, src,
152 nbytes - (cbc_blocks * bsize));
166 int bsize = crypto_blkcipher_blocksize(desc->
tfm);
170 int lastn = nbytes - bsize;
172 u8 s[bsize * 2], d[bsize * 2];
183 lcldesc.tfm = ctx->
child;
185 lcldesc.flags = desc->
flags;
188 memset(iv, 0,
sizeof(iv));
189 sg_set_buf(&sgsrc[0], s, bsize);
190 sg_set_buf(&sgdst[0], tmp, bsize);
191 err = crypto_blkcipher_decrypt_iv(&lcldesc, sgdst, sgsrc, bsize);
195 memset(iv, 0,
sizeof(iv));
196 memcpy(iv, s + bsize, lastn);
200 memcpy(d + bsize, tmp, lastn);
203 memcpy(s + bsize + lastn, tmp + lastn, bsize - lastn);
205 memset(iv, 0,
sizeof(iv));
206 sg_set_buf(&sgsrc[0], s + bsize, bsize);
207 sg_set_buf(&sgdst[0], d, bsize);
208 err = crypto_blkcipher_decrypt_iv(&lcldesc, sgdst, sgsrc, bsize);
224 int bsize = crypto_blkcipher_blocksize(desc->
tfm);
225 int tot_blocks = (nbytes + bsize - 1) / bsize;
226 int cbc_blocks = tot_blocks > 2 ? tot_blocks - 2 : 0;
230 lcldesc.tfm = ctx->
child;
231 lcldesc.info = desc->
info;
232 lcldesc.flags = desc->
flags;
234 if (tot_blocks == 1) {
235 err = crypto_blkcipher_decrypt_iv(&lcldesc, dst, src, bsize);
236 }
else if (nbytes <= bsize * 2) {
237 err = cts_cbc_decrypt(ctx, desc, dst, src, 0, nbytes);
240 err = crypto_blkcipher_decrypt_iv(&lcldesc, dst, src,
244 err = cts_cbc_decrypt(ctx, desc, dst, src,
246 nbytes - (cbc_blocks * bsize));
259 cipher = crypto_spawn_blkcipher(spawn);
261 return PTR_ERR(cipher);
267 static void crypto_cts_exit_tfm(
struct crypto_tfm *tfm)
270 crypto_free_blkcipher(ctx->
child);
304 inst->
alg.cra_alignmask |= __alignof__(
u32) - 1;
307 inst->
alg.cra_blkcipher.min_keysize = alg->cra_blkcipher.min_keysize;
308 inst->
alg.cra_blkcipher.max_keysize = alg->cra_blkcipher.max_keysize;
310 inst->
alg.cra_blkcipher.geniv =
"seqiv";
314 inst->
alg.cra_init = crypto_cts_init_tfm;
315 inst->
alg.cra_exit = crypto_cts_exit_tfm;
317 inst->
alg.cra_blkcipher.setkey = crypto_cts_setkey;
318 inst->
alg.cra_blkcipher.encrypt = crypto_cts_encrypt;
319 inst->
alg.cra_blkcipher.decrypt = crypto_cts_decrypt;
334 .alloc = crypto_cts_alloc,
335 .free = crypto_cts_free,
339 static int __init crypto_cts_module_init(
void)
344 static void __exit crypto_cts_module_exit(
void)