2 * seqiv: Sequence Number IV Generator
4 * This generator generates an IV based on a sequence number by xoring it
5 * with a salt. This algorithm is mainly useful for CTR and similar modes.
7 * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
16 #include <crypto/internal/geniv.h>
17 #include <crypto/scatterwalk.h>
18 #include <crypto/skcipher.h>
19 #include <linux/err.h>
20 #include <linux/init.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include <linux/string.h>
26 static void seqiv_free(struct crypto_instance
*inst
);
28 static void seqiv_aead_encrypt_complete2(struct aead_request
*req
, int err
)
30 struct aead_request
*subreq
= aead_request_ctx(req
);
31 struct crypto_aead
*geniv
;
33 if (err
== -EINPROGRESS
)
39 geniv
= crypto_aead_reqtfm(req
);
40 memcpy(req
->iv
, subreq
->iv
, crypto_aead_ivsize(geniv
));
46 static void seqiv_aead_encrypt_complete(struct crypto_async_request
*base
,
49 struct aead_request
*req
= base
->data
;
51 seqiv_aead_encrypt_complete2(req
, err
);
52 aead_request_complete(req
, err
);
55 static int seqiv_aead_encrypt(struct aead_request
*req
)
57 struct crypto_aead
*geniv
= crypto_aead_reqtfm(req
);
58 struct aead_geniv_ctx
*ctx
= crypto_aead_ctx(geniv
);
59 struct aead_request
*subreq
= aead_request_ctx(req
);
60 crypto_completion_t
compl;
63 unsigned int ivsize
= 8;
66 if (req
->cryptlen
< ivsize
)
69 aead_request_set_tfm(subreq
, ctx
->child
);
71 compl = req
->base
.complete
;
72 data
= req
->base
.data
;
75 if (req
->src
!= req
->dst
) {
76 SYNC_SKCIPHER_REQUEST_ON_STACK(nreq
, ctx
->sknull
);
78 skcipher_request_set_sync_tfm(nreq
, ctx
->sknull
);
79 skcipher_request_set_callback(nreq
, req
->base
.flags
,
81 skcipher_request_set_crypt(nreq
, req
->src
, req
->dst
,
82 req
->assoclen
+ req
->cryptlen
,
85 err
= crypto_skcipher_encrypt(nreq
);
90 if (unlikely(!IS_ALIGNED((unsigned long)info
,
91 crypto_aead_alignmask(geniv
) + 1))) {
92 info
= kmemdup(req
->iv
, ivsize
, req
->base
.flags
&
93 CRYPTO_TFM_REQ_MAY_SLEEP
? GFP_KERNEL
:
98 compl = seqiv_aead_encrypt_complete
;
102 aead_request_set_callback(subreq
, req
->base
.flags
, compl, data
);
103 aead_request_set_crypt(subreq
, req
->dst
, req
->dst
,
104 req
->cryptlen
- ivsize
, info
);
105 aead_request_set_ad(subreq
, req
->assoclen
+ ivsize
);
107 crypto_xor(info
, ctx
->salt
, ivsize
);
108 scatterwalk_map_and_copy(info
, req
->dst
, req
->assoclen
, ivsize
, 1);
110 err
= crypto_aead_encrypt(subreq
);
111 if (unlikely(info
!= req
->iv
))
112 seqiv_aead_encrypt_complete2(req
, err
);
116 static int seqiv_aead_decrypt(struct aead_request
*req
)
118 struct crypto_aead
*geniv
= crypto_aead_reqtfm(req
);
119 struct aead_geniv_ctx
*ctx
= crypto_aead_ctx(geniv
);
120 struct aead_request
*subreq
= aead_request_ctx(req
);
121 crypto_completion_t
compl;
123 unsigned int ivsize
= 8;
125 if (req
->cryptlen
< ivsize
+ crypto_aead_authsize(geniv
))
128 aead_request_set_tfm(subreq
, ctx
->child
);
130 compl = req
->base
.complete
;
131 data
= req
->base
.data
;
133 aead_request_set_callback(subreq
, req
->base
.flags
, compl, data
);
134 aead_request_set_crypt(subreq
, req
->src
, req
->dst
,
135 req
->cryptlen
- ivsize
, req
->iv
);
136 aead_request_set_ad(subreq
, req
->assoclen
+ ivsize
);
138 scatterwalk_map_and_copy(req
->iv
, req
->src
, req
->assoclen
, ivsize
, 0);
140 return crypto_aead_decrypt(subreq
);
143 static int seqiv_aead_create(struct crypto_template
*tmpl
, struct rtattr
**tb
)
145 struct aead_instance
*inst
;
148 inst
= aead_geniv_alloc(tmpl
, tb
, 0, 0);
151 return PTR_ERR(inst
);
154 if (inst
->alg
.ivsize
!= sizeof(u64
))
157 inst
->alg
.encrypt
= seqiv_aead_encrypt
;
158 inst
->alg
.decrypt
= seqiv_aead_decrypt
;
160 inst
->alg
.init
= aead_init_geniv
;
161 inst
->alg
.exit
= aead_exit_geniv
;
163 inst
->alg
.base
.cra_ctxsize
= sizeof(struct aead_geniv_ctx
);
164 inst
->alg
.base
.cra_ctxsize
+= inst
->alg
.ivsize
;
166 err
= aead_register_instance(tmpl
, inst
);
174 aead_geniv_free(inst
);
178 static int seqiv_create(struct crypto_template
*tmpl
, struct rtattr
**tb
)
180 struct crypto_attr_type
*algt
;
182 algt
= crypto_get_attr_type(tb
);
184 return PTR_ERR(algt
);
186 if ((algt
->type
^ CRYPTO_ALG_TYPE_AEAD
) & CRYPTO_ALG_TYPE_MASK
)
189 return seqiv_aead_create(tmpl
, tb
);
192 static void seqiv_free(struct crypto_instance
*inst
)
194 aead_geniv_free(aead_instance(inst
));
197 static struct crypto_template seqiv_tmpl
= {
199 .create
= seqiv_create
,
201 .module
= THIS_MODULE
,
204 static int __init
seqiv_module_init(void)
206 return crypto_register_template(&seqiv_tmpl
);
209 static void __exit
seqiv_module_exit(void)
211 crypto_unregister_template(&seqiv_tmpl
);
214 module_init(seqiv_module_init
);
215 module_exit(seqiv_module_exit
);
217 MODULE_LICENSE("GPL");
218 MODULE_DESCRIPTION("Sequence Number IV Generator");
219 MODULE_ALIAS_CRYPTO("seqiv");