2 * echainiv: Encrypted Chain IV Generator
4 * This generator generates an IV based on a sequence number by multiplying
5 * it with a salt and then encrypting it with the same key as used to encrypt
6 * the plain text. This algorithm requires that the block size be equal
7 * to the IV size. It is mainly useful for CBC.
9 * This generator can only be used by algorithms where authentication
10 * is performed after encryption (i.e., authenc).
12 * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the Free
16 * Software Foundation; either version 2 of the License, or (at your option)
21 #include <crypto/internal/geniv.h>
22 #include <crypto/scatterwalk.h>
23 #include <crypto/skcipher.h>
24 #include <linux/err.h>
25 #include <linux/init.h>
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/slab.h>
29 #include <linux/string.h>
31 static int echainiv_encrypt(struct aead_request
*req
)
33 struct crypto_aead
*geniv
= crypto_aead_reqtfm(req
);
34 struct aead_geniv_ctx
*ctx
= crypto_aead_ctx(geniv
);
35 struct aead_request
*subreq
= aead_request_ctx(req
);
39 unsigned int ivsize
= crypto_aead_ivsize(geniv
);
42 if (req
->cryptlen
< ivsize
)
45 aead_request_set_tfm(subreq
, ctx
->child
);
49 if (req
->src
!= req
->dst
) {
50 SKCIPHER_REQUEST_ON_STACK(nreq
, ctx
->sknull
);
52 skcipher_request_set_tfm(nreq
, ctx
->sknull
);
53 skcipher_request_set_callback(nreq
, req
->base
.flags
,
55 skcipher_request_set_crypt(nreq
, req
->src
, req
->dst
,
56 req
->assoclen
+ req
->cryptlen
,
59 err
= crypto_skcipher_encrypt(nreq
);
64 aead_request_set_callback(subreq
, req
->base
.flags
,
65 req
->base
.complete
, req
->base
.data
);
66 aead_request_set_crypt(subreq
, req
->dst
, req
->dst
,
68 aead_request_set_ad(subreq
, req
->assoclen
);
70 memcpy(&nseqno
, info
+ ivsize
- 8, 8);
71 seqno
= be64_to_cpu(nseqno
);
72 memset(info
, 0, ivsize
);
74 scatterwalk_map_and_copy(info
, req
->dst
, req
->assoclen
, ivsize
, 1);
79 memcpy(&a
, ctx
->salt
+ ivsize
- 8, 8);
84 memcpy(info
+ ivsize
- 8, &a
, 8);
85 } while ((ivsize
-= 8));
87 return crypto_aead_encrypt(subreq
);
90 static int echainiv_decrypt(struct aead_request
*req
)
92 struct crypto_aead
*geniv
= crypto_aead_reqtfm(req
);
93 struct aead_geniv_ctx
*ctx
= crypto_aead_ctx(geniv
);
94 struct aead_request
*subreq
= aead_request_ctx(req
);
95 crypto_completion_t
compl;
97 unsigned int ivsize
= crypto_aead_ivsize(geniv
);
99 if (req
->cryptlen
< ivsize
)
102 aead_request_set_tfm(subreq
, ctx
->child
);
104 compl = req
->base
.complete
;
105 data
= req
->base
.data
;
107 aead_request_set_callback(subreq
, req
->base
.flags
, compl, data
);
108 aead_request_set_crypt(subreq
, req
->src
, req
->dst
,
109 req
->cryptlen
- ivsize
, req
->iv
);
110 aead_request_set_ad(subreq
, req
->assoclen
+ ivsize
);
112 scatterwalk_map_and_copy(req
->iv
, req
->src
, req
->assoclen
, ivsize
, 0);
114 return crypto_aead_decrypt(subreq
);
117 static int echainiv_aead_create(struct crypto_template
*tmpl
,
120 struct aead_instance
*inst
;
123 inst
= aead_geniv_alloc(tmpl
, tb
, 0, 0);
126 return PTR_ERR(inst
);
129 if (inst
->alg
.ivsize
& (sizeof(u64
) - 1) || !inst
->alg
.ivsize
)
132 inst
->alg
.encrypt
= echainiv_encrypt
;
133 inst
->alg
.decrypt
= echainiv_decrypt
;
135 inst
->alg
.init
= aead_init_geniv
;
136 inst
->alg
.exit
= aead_exit_geniv
;
138 inst
->alg
.base
.cra_ctxsize
= sizeof(struct aead_geniv_ctx
);
139 inst
->alg
.base
.cra_ctxsize
+= inst
->alg
.ivsize
;
141 inst
->free
= aead_geniv_free
;
143 err
= aead_register_instance(tmpl
, inst
);
151 aead_geniv_free(inst
);
155 static void echainiv_free(struct crypto_instance
*inst
)
157 aead_geniv_free(aead_instance(inst
));
160 static struct crypto_template echainiv_tmpl
= {
162 .create
= echainiv_aead_create
,
163 .free
= echainiv_free
,
164 .module
= THIS_MODULE
,
167 static int __init
echainiv_module_init(void)
169 return crypto_register_template(&echainiv_tmpl
);
172 static void __exit
echainiv_module_exit(void)
174 crypto_unregister_template(&echainiv_tmpl
);
177 module_init(echainiv_module_init
);
178 module_exit(echainiv_module_exit
);
180 MODULE_LICENSE("GPL");
181 MODULE_DESCRIPTION("Encrypted Chain IV Generator");
182 MODULE_ALIAS_CRYPTO("echainiv");