blk-mq: split blk_mq_alloc_and_init_hctx into two parts
[linux/fpc-iii.git] / crypto / pcbc.c
blob2fa03fc576fe4af88ba7062238749cf251630d50
1 /*
2 * PCBC: Propagating Cipher Block Chaining mode
4 * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
7 * Derived from cbc.c
8 * - Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
13 * any later version.
17 #include <crypto/algapi.h>
18 #include <crypto/internal/skcipher.h>
19 #include <linux/err.h>
20 #include <linux/init.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
24 static int crypto_pcbc_encrypt_segment(struct skcipher_request *req,
25 struct skcipher_walk *walk,
26 struct crypto_cipher *tfm)
28 int bsize = crypto_cipher_blocksize(tfm);
29 unsigned int nbytes = walk->nbytes;
30 u8 *src = walk->src.virt.addr;
31 u8 *dst = walk->dst.virt.addr;
32 u8 * const iv = walk->iv;
34 do {
35 crypto_xor(iv, src, bsize);
36 crypto_cipher_encrypt_one(tfm, dst, iv);
37 crypto_xor_cpy(iv, dst, src, bsize);
39 src += bsize;
40 dst += bsize;
41 } while ((nbytes -= bsize) >= bsize);
43 return nbytes;
46 static int crypto_pcbc_encrypt_inplace(struct skcipher_request *req,
47 struct skcipher_walk *walk,
48 struct crypto_cipher *tfm)
50 int bsize = crypto_cipher_blocksize(tfm);
51 unsigned int nbytes = walk->nbytes;
52 u8 *src = walk->src.virt.addr;
53 u8 * const iv = walk->iv;
54 u8 tmpbuf[MAX_CIPHER_BLOCKSIZE];
56 do {
57 memcpy(tmpbuf, src, bsize);
58 crypto_xor(iv, src, bsize);
59 crypto_cipher_encrypt_one(tfm, src, iv);
60 crypto_xor_cpy(iv, tmpbuf, src, bsize);
62 src += bsize;
63 } while ((nbytes -= bsize) >= bsize);
65 return nbytes;
68 static int crypto_pcbc_encrypt(struct skcipher_request *req)
70 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
71 struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
72 struct skcipher_walk walk;
73 unsigned int nbytes;
74 int err;
76 err = skcipher_walk_virt(&walk, req, false);
78 while ((nbytes = walk.nbytes)) {
79 if (walk.src.virt.addr == walk.dst.virt.addr)
80 nbytes = crypto_pcbc_encrypt_inplace(req, &walk,
81 cipher);
82 else
83 nbytes = crypto_pcbc_encrypt_segment(req, &walk,
84 cipher);
85 err = skcipher_walk_done(&walk, nbytes);
88 return err;
91 static int crypto_pcbc_decrypt_segment(struct skcipher_request *req,
92 struct skcipher_walk *walk,
93 struct crypto_cipher *tfm)
95 int bsize = crypto_cipher_blocksize(tfm);
96 unsigned int nbytes = walk->nbytes;
97 u8 *src = walk->src.virt.addr;
98 u8 *dst = walk->dst.virt.addr;
99 u8 * const iv = walk->iv;
101 do {
102 crypto_cipher_decrypt_one(tfm, dst, src);
103 crypto_xor(dst, iv, bsize);
104 crypto_xor_cpy(iv, dst, src, bsize);
106 src += bsize;
107 dst += bsize;
108 } while ((nbytes -= bsize) >= bsize);
110 return nbytes;
113 static int crypto_pcbc_decrypt_inplace(struct skcipher_request *req,
114 struct skcipher_walk *walk,
115 struct crypto_cipher *tfm)
117 int bsize = crypto_cipher_blocksize(tfm);
118 unsigned int nbytes = walk->nbytes;
119 u8 *src = walk->src.virt.addr;
120 u8 * const iv = walk->iv;
121 u8 tmpbuf[MAX_CIPHER_BLOCKSIZE] __aligned(__alignof__(u32));
123 do {
124 memcpy(tmpbuf, src, bsize);
125 crypto_cipher_decrypt_one(tfm, src, src);
126 crypto_xor(src, iv, bsize);
127 crypto_xor_cpy(iv, src, tmpbuf, bsize);
129 src += bsize;
130 } while ((nbytes -= bsize) >= bsize);
132 return nbytes;
135 static int crypto_pcbc_decrypt(struct skcipher_request *req)
137 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
138 struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
139 struct skcipher_walk walk;
140 unsigned int nbytes;
141 int err;
143 err = skcipher_walk_virt(&walk, req, false);
145 while ((nbytes = walk.nbytes)) {
146 if (walk.src.virt.addr == walk.dst.virt.addr)
147 nbytes = crypto_pcbc_decrypt_inplace(req, &walk,
148 cipher);
149 else
150 nbytes = crypto_pcbc_decrypt_segment(req, &walk,
151 cipher);
152 err = skcipher_walk_done(&walk, nbytes);
155 return err;
158 static int crypto_pcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
160 struct skcipher_instance *inst;
161 struct crypto_alg *alg;
162 int err;
164 inst = skcipher_alloc_instance_simple(tmpl, tb, &alg);
165 if (IS_ERR(inst))
166 return PTR_ERR(inst);
168 inst->alg.encrypt = crypto_pcbc_encrypt;
169 inst->alg.decrypt = crypto_pcbc_decrypt;
171 err = skcipher_register_instance(tmpl, inst);
172 if (err)
173 inst->free(inst);
174 crypto_mod_put(alg);
175 return err;
178 static struct crypto_template crypto_pcbc_tmpl = {
179 .name = "pcbc",
180 .create = crypto_pcbc_create,
181 .module = THIS_MODULE,
184 static int __init crypto_pcbc_module_init(void)
186 return crypto_register_template(&crypto_pcbc_tmpl);
189 static void __exit crypto_pcbc_module_exit(void)
191 crypto_unregister_template(&crypto_pcbc_tmpl);
194 module_init(crypto_pcbc_module_init);
195 module_exit(crypto_pcbc_module_exit);
197 MODULE_LICENSE("GPL");
198 MODULE_DESCRIPTION("PCBC block cipher mode of operation");
199 MODULE_ALIAS_CRYPTO("pcbc");