4 * (C) Copyright IBM Corp. 2007 - Joy Latten <latten@us.ibm.com>
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)
13 #include <crypto/algapi.h>
14 #include <linux/err.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/random.h>
19 #include <linux/scatterlist.h>
20 #include <linux/slab.h>
22 struct ctr_instance_ctx
{
23 struct crypto_spawn alg
;
24 unsigned int noncesize
;
28 struct crypto_ctr_ctx
{
29 struct crypto_cipher
*child
;
33 static inline void __ctr_inc_byte(u8
*a
, unsigned int size
)
38 for (; size
; size
--) {
46 static void ctr_inc_quad(u8
*a
, unsigned int size
)
48 __be32
*b
= (__be32
*)(a
+ size
);
51 for (; size
>= 4; size
-=4) {
52 c
= be32_to_cpu(*--b
) + 1;
58 __ctr_inc_byte(a
, size
);
61 static void xor_byte(u8
*a
, const u8
*b
, unsigned int bs
)
67 static void xor_quad(u8
*dst
, const u8
*src
, unsigned int bs
)
72 for (; bs
>= 4; bs
-= 4)
75 xor_byte((u8
*)a
, (u8
*)b
, bs
);
78 static int crypto_ctr_setkey(struct crypto_tfm
*parent
, const u8
*key
,
81 struct crypto_ctr_ctx
*ctx
= crypto_tfm_ctx(parent
);
82 struct crypto_cipher
*child
= ctx
->child
;
83 struct ctr_instance_ctx
*ictx
=
84 crypto_instance_ctx(crypto_tfm_alg_instance(parent
));
85 unsigned int noncelen
= ictx
->noncesize
;
88 /* the nonce is stored in bytes at end of key */
89 if (keylen
< noncelen
)
92 memcpy(ctx
->nonce
, key
+ (keylen
- noncelen
), noncelen
);
96 crypto_cipher_clear_flags(child
, CRYPTO_TFM_REQ_MASK
);
97 crypto_cipher_set_flags(child
, crypto_tfm_get_flags(parent
) &
99 err
= crypto_cipher_setkey(child
, key
, keylen
);
100 crypto_tfm_set_flags(parent
, crypto_cipher_get_flags(child
) &
101 CRYPTO_TFM_RES_MASK
);
106 static int crypto_ctr_crypt_segment(struct blkcipher_walk
*walk
,
107 struct crypto_cipher
*tfm
, u8
*ctrblk
,
108 unsigned int countersize
)
110 void (*fn
)(struct crypto_tfm
*, u8
*, const u8
*) =
111 crypto_cipher_alg(tfm
)->cia_encrypt
;
112 unsigned int bsize
= crypto_cipher_blocksize(tfm
);
113 unsigned long alignmask
= crypto_cipher_alignmask(tfm
);
114 u8 ks
[bsize
+ alignmask
];
115 u8
*keystream
= (u8
*)ALIGN((unsigned long)ks
, alignmask
+ 1);
116 u8
*src
= walk
->src
.virt
.addr
;
117 u8
*dst
= walk
->dst
.virt
.addr
;
118 unsigned int nbytes
= walk
->nbytes
;
121 /* create keystream */
122 fn(crypto_cipher_tfm(tfm
), keystream
, ctrblk
);
123 xor_quad(keystream
, src
, min(nbytes
, bsize
));
125 /* copy result into dst */
126 memcpy(dst
, keystream
, min(nbytes
, bsize
));
128 /* increment counter in counterblock */
129 ctr_inc_quad(ctrblk
+ (bsize
- countersize
), countersize
);
143 static int crypto_ctr_crypt_inplace(struct blkcipher_walk
*walk
,
144 struct crypto_cipher
*tfm
, u8
*ctrblk
,
145 unsigned int countersize
)
147 void (*fn
)(struct crypto_tfm
*, u8
*, const u8
*) =
148 crypto_cipher_alg(tfm
)->cia_encrypt
;
149 unsigned int bsize
= crypto_cipher_blocksize(tfm
);
150 unsigned long alignmask
= crypto_cipher_alignmask(tfm
);
151 unsigned int nbytes
= walk
->nbytes
;
152 u8
*src
= walk
->src
.virt
.addr
;
153 u8 ks
[bsize
+ alignmask
];
154 u8
*keystream
= (u8
*)ALIGN((unsigned long)ks
, alignmask
+ 1);
157 /* create keystream */
158 fn(crypto_cipher_tfm(tfm
), keystream
, ctrblk
);
159 xor_quad(src
, keystream
, min(nbytes
, bsize
));
161 /* increment counter in counterblock */
162 ctr_inc_quad(ctrblk
+ (bsize
- countersize
), countersize
);
175 static int crypto_ctr_crypt(struct blkcipher_desc
*desc
,
176 struct scatterlist
*dst
, struct scatterlist
*src
,
179 struct blkcipher_walk walk
;
180 struct crypto_blkcipher
*tfm
= desc
->tfm
;
181 struct crypto_ctr_ctx
*ctx
= crypto_blkcipher_ctx(tfm
);
182 struct crypto_cipher
*child
= ctx
->child
;
183 unsigned int bsize
= crypto_cipher_blocksize(child
);
184 struct ctr_instance_ctx
*ictx
=
185 crypto_instance_ctx(crypto_tfm_alg_instance(&tfm
->base
));
186 unsigned long alignmask
= crypto_cipher_alignmask(child
);
187 u8 cblk
[bsize
+ alignmask
];
188 u8
*counterblk
= (u8
*)ALIGN((unsigned long)cblk
, alignmask
+ 1);
189 unsigned int countersize
;
192 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
193 err
= blkcipher_walk_virt_block(desc
, &walk
, bsize
);
195 /* set up counter block */
196 memset(counterblk
, 0 , bsize
);
197 memcpy(counterblk
, ctx
->nonce
, ictx
->noncesize
);
198 memcpy(counterblk
+ ictx
->noncesize
, walk
.iv
, ictx
->ivsize
);
200 /* initialize counter portion of counter block */
201 countersize
= bsize
- ictx
->noncesize
- ictx
->ivsize
;
202 ctr_inc_quad(counterblk
+ (bsize
- countersize
), countersize
);
204 while (walk
.nbytes
) {
205 if (walk
.src
.virt
.addr
== walk
.dst
.virt
.addr
)
206 nbytes
= crypto_ctr_crypt_inplace(&walk
, child
,
210 nbytes
= crypto_ctr_crypt_segment(&walk
, child
,
214 err
= blkcipher_walk_done(desc
, &walk
, nbytes
);
219 static int crypto_ctr_init_tfm(struct crypto_tfm
*tfm
)
221 struct crypto_instance
*inst
= (void *)tfm
->__crt_alg
;
222 struct ctr_instance_ctx
*ictx
= crypto_instance_ctx(inst
);
223 struct crypto_ctr_ctx
*ctx
= crypto_tfm_ctx(tfm
);
224 struct crypto_cipher
*cipher
;
226 ctx
->nonce
= kzalloc(ictx
->noncesize
, GFP_KERNEL
);
230 cipher
= crypto_spawn_cipher(&ictx
->alg
);
232 return PTR_ERR(cipher
);
239 static void crypto_ctr_exit_tfm(struct crypto_tfm
*tfm
)
241 struct crypto_ctr_ctx
*ctx
= crypto_tfm_ctx(tfm
);
244 crypto_free_cipher(ctx
->child
);
247 static struct crypto_instance
*crypto_ctr_alloc(struct rtattr
**tb
)
249 struct crypto_instance
*inst
;
250 struct crypto_alg
*alg
;
251 struct ctr_instance_ctx
*ictx
;
252 unsigned int noncesize
;
256 err
= crypto_check_attr_type(tb
, CRYPTO_ALG_TYPE_BLKCIPHER
);
260 alg
= crypto_attr_alg(tb
[1], CRYPTO_ALG_TYPE_CIPHER
,
261 CRYPTO_ALG_TYPE_MASK
);
263 return ERR_PTR(PTR_ERR(alg
));
265 err
= crypto_attr_u32(tb
[2], &noncesize
);
269 err
= crypto_attr_u32(tb
[3], &ivsize
);
273 /* verify size of nonce + iv + counter */
275 if ((noncesize
+ ivsize
) >= alg
->cra_blocksize
)
278 inst
= kzalloc(sizeof(*inst
) + sizeof(*ictx
), GFP_KERNEL
);
284 if (snprintf(inst
->alg
.cra_name
, CRYPTO_MAX_ALG_NAME
,
285 "ctr(%s,%u,%u)", alg
->cra_name
, noncesize
,
286 ivsize
) >= CRYPTO_MAX_ALG_NAME
) {
290 if (snprintf(inst
->alg
.cra_driver_name
, CRYPTO_MAX_ALG_NAME
,
291 "ctr(%s,%u,%u)", alg
->cra_driver_name
, noncesize
,
292 ivsize
) >= CRYPTO_MAX_ALG_NAME
) {
296 ictx
= crypto_instance_ctx(inst
);
297 ictx
->noncesize
= noncesize
;
298 ictx
->ivsize
= ivsize
;
300 err
= crypto_init_spawn(&ictx
->alg
, alg
, inst
,
301 CRYPTO_ALG_TYPE_MASK
| CRYPTO_ALG_ASYNC
);
306 inst
->alg
.cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
;
307 inst
->alg
.cra_priority
= alg
->cra_priority
;
308 inst
->alg
.cra_blocksize
= 1;
309 inst
->alg
.cra_alignmask
= 3;
310 inst
->alg
.cra_type
= &crypto_blkcipher_type
;
312 inst
->alg
.cra_blkcipher
.ivsize
= ivsize
;
313 inst
->alg
.cra_blkcipher
.min_keysize
= alg
->cra_cipher
.cia_min_keysize
315 inst
->alg
.cra_blkcipher
.max_keysize
= alg
->cra_cipher
.cia_max_keysize
318 inst
->alg
.cra_ctxsize
= sizeof(struct crypto_ctr_ctx
);
320 inst
->alg
.cra_init
= crypto_ctr_init_tfm
;
321 inst
->alg
.cra_exit
= crypto_ctr_exit_tfm
;
323 inst
->alg
.cra_blkcipher
.setkey
= crypto_ctr_setkey
;
324 inst
->alg
.cra_blkcipher
.encrypt
= crypto_ctr_crypt
;
325 inst
->alg
.cra_blkcipher
.decrypt
= crypto_ctr_crypt
;
340 static void crypto_ctr_free(struct crypto_instance
*inst
)
342 struct ctr_instance_ctx
*ictx
= crypto_instance_ctx(inst
);
344 crypto_drop_spawn(&ictx
->alg
);
348 static struct crypto_template crypto_ctr_tmpl
= {
350 .alloc
= crypto_ctr_alloc
,
351 .free
= crypto_ctr_free
,
352 .module
= THIS_MODULE
,
355 static int __init
crypto_ctr_module_init(void)
357 return crypto_register_template(&crypto_ctr_tmpl
);
360 static void __exit
crypto_ctr_module_exit(void)
362 crypto_unregister_template(&crypto_ctr_tmpl
);
365 module_init(crypto_ctr_module_init
);
366 module_exit(crypto_ctr_module_exit
);
368 MODULE_LICENSE("GPL");
369 MODULE_DESCRIPTION("CTR Counter block mode");