2 * Shared glue code for 128bit block ciphers
4 * Copyright © 2012-2013 Jussi Kivilinna <jussi.kivilinna@iki.fi>
6 * CBC & ECB parts based on code (crypto/cbc.c,ecb.c) by:
7 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
8 * CTR part based on code (crypto/ctr.c) by:
9 * (C) Copyright IBM Corp. 2007 - Joy Latten <latten@us.ibm.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28 #include <linux/module.h>
29 #include <crypto/b128ops.h>
30 #include <crypto/lrw.h>
31 #include <crypto/xts.h>
32 #include <asm/crypto/glue_helper.h>
33 #include <crypto/scatterwalk.h>
35 static int __glue_ecb_crypt_128bit(const struct common_glue_ctx *gctx,
36 struct blkcipher_desc *desc,
37 struct blkcipher_walk *walk)
39 void *ctx = crypto_blkcipher_ctx(desc->tfm);
40 const unsigned int bsize = 128 / 8;
41 unsigned int nbytes, i, func_bytes;
45 err = blkcipher_walk_virt(desc, walk);
47 while ((nbytes = walk->nbytes)) {
48 u8 *wsrc = walk->src.virt.addr;
49 u8 *wdst = walk->dst.virt.addr;
51 fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
54 for (i = 0; i < gctx->num_funcs; i++) {
55 func_bytes = bsize * gctx->funcs[i].num_blocks;
57 /* Process multi-block batch */
58 if (nbytes >= func_bytes) {
60 gctx->funcs[i].fn_u.ecb(ctx, wdst,
66 } while (nbytes >= func_bytes);
74 glue_fpu_end(fpu_enabled);
75 err = blkcipher_walk_done(desc, walk, nbytes);
81 int glue_ecb_crypt_128bit(const struct common_glue_ctx *gctx,
82 struct blkcipher_desc *desc, struct scatterlist *dst,
83 struct scatterlist *src, unsigned int nbytes)
85 struct blkcipher_walk walk;
87 blkcipher_walk_init(&walk, dst, src, nbytes);
88 return __glue_ecb_crypt_128bit(gctx, desc, &walk);
90 EXPORT_SYMBOL_GPL(glue_ecb_crypt_128bit);
92 static unsigned int __glue_cbc_encrypt_128bit(const common_glue_func_t fn,
93 struct blkcipher_desc *desc,
94 struct blkcipher_walk *walk)
96 void *ctx = crypto_blkcipher_ctx(desc->tfm);
97 const unsigned int bsize = 128 / 8;
98 unsigned int nbytes = walk->nbytes;
99 u128 *src = (u128 *)walk->src.virt.addr;
100 u128 *dst = (u128 *)walk->dst.virt.addr;
101 u128 *iv = (u128 *)walk->iv;
104 u128_xor(dst, src, iv);
105 fn(ctx, (u8 *)dst, (u8 *)dst);
111 } while (nbytes >= bsize);
113 *(u128 *)walk->iv = *iv;
117 int glue_cbc_encrypt_128bit(const common_glue_func_t fn,
118 struct blkcipher_desc *desc,
119 struct scatterlist *dst,
120 struct scatterlist *src, unsigned int nbytes)
122 struct blkcipher_walk walk;
125 blkcipher_walk_init(&walk, dst, src, nbytes);
126 err = blkcipher_walk_virt(desc, &walk);
128 while ((nbytes = walk.nbytes)) {
129 nbytes = __glue_cbc_encrypt_128bit(fn, desc, &walk);
130 err = blkcipher_walk_done(desc, &walk, nbytes);
135 EXPORT_SYMBOL_GPL(glue_cbc_encrypt_128bit);
138 __glue_cbc_decrypt_128bit(const struct common_glue_ctx *gctx,
139 struct blkcipher_desc *desc,
140 struct blkcipher_walk *walk)
142 void *ctx = crypto_blkcipher_ctx(desc->tfm);
143 const unsigned int bsize = 128 / 8;
144 unsigned int nbytes = walk->nbytes;
145 u128 *src = (u128 *)walk->src.virt.addr;
146 u128 *dst = (u128 *)walk->dst.virt.addr;
148 unsigned int num_blocks, func_bytes;
151 /* Start of the last block. */
152 src += nbytes / bsize - 1;
153 dst += nbytes / bsize - 1;
157 for (i = 0; i < gctx->num_funcs; i++) {
158 num_blocks = gctx->funcs[i].num_blocks;
159 func_bytes = bsize * num_blocks;
161 /* Process multi-block batch */
162 if (nbytes >= func_bytes) {
164 nbytes -= func_bytes - bsize;
165 src -= num_blocks - 1;
166 dst -= num_blocks - 1;
168 gctx->funcs[i].fn_u.cbc(ctx, dst, src);
174 u128_xor(dst, dst, src - 1);
177 } while (nbytes >= func_bytes);
185 u128_xor(dst, dst, (u128 *)walk->iv);
186 *(u128 *)walk->iv = last_iv;
191 int glue_cbc_decrypt_128bit(const struct common_glue_ctx *gctx,
192 struct blkcipher_desc *desc,
193 struct scatterlist *dst,
194 struct scatterlist *src, unsigned int nbytes)
196 const unsigned int bsize = 128 / 8;
198 struct blkcipher_walk walk;
201 blkcipher_walk_init(&walk, dst, src, nbytes);
202 err = blkcipher_walk_virt(desc, &walk);
204 while ((nbytes = walk.nbytes)) {
205 fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
206 desc, false, nbytes);
207 nbytes = __glue_cbc_decrypt_128bit(gctx, desc, &walk);
208 glue_fpu_end(fpu_enabled);
209 err = blkcipher_walk_done(desc, &walk, nbytes);
214 EXPORT_SYMBOL_GPL(glue_cbc_decrypt_128bit);
216 static void glue_ctr_crypt_final_128bit(const common_glue_ctr_func_t fn_ctr,
217 struct blkcipher_desc *desc,
218 struct blkcipher_walk *walk)
220 void *ctx = crypto_blkcipher_ctx(desc->tfm);
221 u8 *src = (u8 *)walk->src.virt.addr;
222 u8 *dst = (u8 *)walk->dst.virt.addr;
223 unsigned int nbytes = walk->nbytes;
227 be128_to_le128(&ctrblk, (be128 *)walk->iv);
229 memcpy(&tmp, src, nbytes);
230 fn_ctr(ctx, &tmp, &tmp, &ctrblk);
231 memcpy(dst, &tmp, nbytes);
233 le128_to_be128((be128 *)walk->iv, &ctrblk);
236 static unsigned int __glue_ctr_crypt_128bit(const struct common_glue_ctx *gctx,
237 struct blkcipher_desc *desc,
238 struct blkcipher_walk *walk)
240 const unsigned int bsize = 128 / 8;
241 void *ctx = crypto_blkcipher_ctx(desc->tfm);
242 unsigned int nbytes = walk->nbytes;
243 u128 *src = (u128 *)walk->src.virt.addr;
244 u128 *dst = (u128 *)walk->dst.virt.addr;
246 unsigned int num_blocks, func_bytes;
249 be128_to_le128(&ctrblk, (be128 *)walk->iv);
251 /* Process multi-block batch */
252 for (i = 0; i < gctx->num_funcs; i++) {
253 num_blocks = gctx->funcs[i].num_blocks;
254 func_bytes = bsize * num_blocks;
256 if (nbytes >= func_bytes) {
258 gctx->funcs[i].fn_u.ctr(ctx, dst, src, &ctrblk);
262 nbytes -= func_bytes;
263 } while (nbytes >= func_bytes);
271 le128_to_be128((be128 *)walk->iv, &ctrblk);
275 int glue_ctr_crypt_128bit(const struct common_glue_ctx *gctx,
276 struct blkcipher_desc *desc, struct scatterlist *dst,
277 struct scatterlist *src, unsigned int nbytes)
279 const unsigned int bsize = 128 / 8;
281 struct blkcipher_walk walk;
284 blkcipher_walk_init(&walk, dst, src, nbytes);
285 err = blkcipher_walk_virt_block(desc, &walk, bsize);
287 while ((nbytes = walk.nbytes) >= bsize) {
288 fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
289 desc, false, nbytes);
290 nbytes = __glue_ctr_crypt_128bit(gctx, desc, &walk);
291 glue_fpu_end(fpu_enabled);
292 err = blkcipher_walk_done(desc, &walk, nbytes);
296 glue_ctr_crypt_final_128bit(
297 gctx->funcs[gctx->num_funcs - 1].fn_u.ctr, desc, &walk);
298 err = blkcipher_walk_done(desc, &walk, 0);
303 EXPORT_SYMBOL_GPL(glue_ctr_crypt_128bit);
305 static unsigned int __glue_xts_crypt_128bit(const struct common_glue_ctx *gctx,
307 struct blkcipher_desc *desc,
308 struct blkcipher_walk *walk)
310 const unsigned int bsize = 128 / 8;
311 unsigned int nbytes = walk->nbytes;
312 u128 *src = (u128 *)walk->src.virt.addr;
313 u128 *dst = (u128 *)walk->dst.virt.addr;
314 unsigned int num_blocks, func_bytes;
317 /* Process multi-block batch */
318 for (i = 0; i < gctx->num_funcs; i++) {
319 num_blocks = gctx->funcs[i].num_blocks;
320 func_bytes = bsize * num_blocks;
322 if (nbytes >= func_bytes) {
324 gctx->funcs[i].fn_u.xts(ctx, dst, src,
329 nbytes -= func_bytes;
330 } while (nbytes >= func_bytes);
341 /* for implementations implementing faster XTS IV generator */
342 int glue_xts_crypt_128bit(const struct common_glue_ctx *gctx,
343 struct blkcipher_desc *desc, struct scatterlist *dst,
344 struct scatterlist *src, unsigned int nbytes,
345 void (*tweak_fn)(void *ctx, u8 *dst, const u8 *src),
346 void *tweak_ctx, void *crypt_ctx)
348 const unsigned int bsize = 128 / 8;
350 struct blkcipher_walk walk;
353 blkcipher_walk_init(&walk, dst, src, nbytes);
355 err = blkcipher_walk_virt(desc, &walk);
356 nbytes = walk.nbytes;
360 /* set minimum length to bsize, for tweak_fn */
361 fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
363 nbytes < bsize ? bsize : nbytes);
364 /* calculate first value of T */
365 tweak_fn(tweak_ctx, walk.iv, walk.iv);
366 glue_fpu_end(fpu_enabled);
369 fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
370 desc, false, nbytes);
371 nbytes = __glue_xts_crypt_128bit(gctx, crypt_ctx, desc, &walk);
373 glue_fpu_end(fpu_enabled);
374 err = blkcipher_walk_done(desc, &walk, nbytes);
375 nbytes = walk.nbytes;
379 EXPORT_SYMBOL_GPL(glue_xts_crypt_128bit);
381 void glue_xts_crypt_128bit_one(void *ctx, u128 *dst, const u128 *src, le128 *iv,
382 common_glue_func_t fn)
386 /* generate next IV */
387 le128_gf128mul_x_ble(iv, &ivblk);
390 u128_xor(dst, src, (u128 *)&ivblk);
392 /* PP <- D(Key2,CC) */
393 fn(ctx, (u8 *)dst, (u8 *)dst);
396 u128_xor(dst, dst, (u128 *)&ivblk);
398 EXPORT_SYMBOL_GPL(glue_xts_crypt_128bit_one);
400 MODULE_LICENSE("GPL");