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)
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)
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
;
35 crypto_xor(iv
, src
, bsize
);
36 crypto_cipher_encrypt_one(tfm
, dst
, iv
);
37 crypto_xor_cpy(iv
, dst
, src
, bsize
);
41 } while ((nbytes
-= bsize
) >= bsize
);
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
];
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
);
63 } while ((nbytes
-= bsize
) >= bsize
);
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
;
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
,
83 nbytes
= crypto_pcbc_encrypt_segment(req
, &walk
,
85 err
= skcipher_walk_done(&walk
, nbytes
);
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
;
102 crypto_cipher_decrypt_one(tfm
, dst
, src
);
103 crypto_xor(dst
, iv
, bsize
);
104 crypto_xor_cpy(iv
, dst
, src
, bsize
);
108 } while ((nbytes
-= bsize
) >= bsize
);
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
));
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
);
130 } while ((nbytes
-= bsize
) >= bsize
);
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
;
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
,
150 nbytes
= crypto_pcbc_decrypt_segment(req
, &walk
,
152 err
= skcipher_walk_done(&walk
, nbytes
);
158 static int crypto_pcbc_create(struct crypto_template
*tmpl
, struct rtattr
**tb
)
160 struct skcipher_instance
*inst
;
161 struct crypto_alg
*alg
;
164 inst
= skcipher_alloc_instance_simple(tmpl
, tb
, &alg
);
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
);
178 static struct crypto_template crypto_pcbc_tmpl
= {
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");