2020
2121struct crypto_cbc_ctx {
2222 struct crypto_cipher * child ;
23- void (* xor )(u8 * dst , const u8 * src , unsigned int bs );
2423};
2524
2625static int crypto_cbc_setkey (struct crypto_tfm * parent , const u8 * key ,
@@ -41,9 +40,7 @@ static int crypto_cbc_setkey(struct crypto_tfm *parent, const u8 *key,
4140
4241static int crypto_cbc_encrypt_segment (struct blkcipher_desc * desc ,
4342 struct blkcipher_walk * walk ,
44- struct crypto_cipher * tfm ,
45- void (* xor )(u8 * , const u8 * ,
46- unsigned int ))
43+ struct crypto_cipher * tfm )
4744{
4845 void (* fn )(struct crypto_tfm * , u8 * , const u8 * ) =
4946 crypto_cipher_alg (tfm )-> cia_encrypt ;
@@ -54,7 +51,7 @@ static int crypto_cbc_encrypt_segment(struct blkcipher_desc *desc,
5451 u8 * iv = walk -> iv ;
5552
5653 do {
57- xor (iv , src , bsize );
54+ crypto_xor (iv , src , bsize );
5855 fn (crypto_cipher_tfm (tfm ), dst , iv );
5956 memcpy (iv , dst , bsize );
6057
@@ -67,9 +64,7 @@ static int crypto_cbc_encrypt_segment(struct blkcipher_desc *desc,
6764
6865static int crypto_cbc_encrypt_inplace (struct blkcipher_desc * desc ,
6966 struct blkcipher_walk * walk ,
70- struct crypto_cipher * tfm ,
71- void (* xor )(u8 * , const u8 * ,
72- unsigned int ))
67+ struct crypto_cipher * tfm )
7368{
7469 void (* fn )(struct crypto_tfm * , u8 * , const u8 * ) =
7570 crypto_cipher_alg (tfm )-> cia_encrypt ;
@@ -79,7 +74,7 @@ static int crypto_cbc_encrypt_inplace(struct blkcipher_desc *desc,
7974 u8 * iv = walk -> iv ;
8075
8176 do {
82- xor (src , iv , bsize );
77+ crypto_xor (src , iv , bsize );
8378 fn (crypto_cipher_tfm (tfm ), src , src );
8479 iv = src ;
8580
@@ -99,19 +94,16 @@ static int crypto_cbc_encrypt(struct blkcipher_desc *desc,
9994 struct crypto_blkcipher * tfm = desc -> tfm ;
10095 struct crypto_cbc_ctx * ctx = crypto_blkcipher_ctx (tfm );
10196 struct crypto_cipher * child = ctx -> child ;
102- void (* xor )(u8 * , const u8 * , unsigned int bs ) = ctx -> xor ;
10397 int err ;
10498
10599 blkcipher_walk_init (& walk , dst , src , nbytes );
106100 err = blkcipher_walk_virt (desc , & walk );
107101
108102 while ((nbytes = walk .nbytes )) {
109103 if (walk .src .virt .addr == walk .dst .virt .addr )
110- nbytes = crypto_cbc_encrypt_inplace (desc , & walk , child ,
111- xor );
104+ nbytes = crypto_cbc_encrypt_inplace (desc , & walk , child );
112105 else
113- nbytes = crypto_cbc_encrypt_segment (desc , & walk , child ,
114- xor );
106+ nbytes = crypto_cbc_encrypt_segment (desc , & walk , child );
115107 err = blkcipher_walk_done (desc , & walk , nbytes );
116108 }
117109
@@ -120,9 +112,7 @@ static int crypto_cbc_encrypt(struct blkcipher_desc *desc,
120112
121113static int crypto_cbc_decrypt_segment (struct blkcipher_desc * desc ,
122114 struct blkcipher_walk * walk ,
123- struct crypto_cipher * tfm ,
124- void (* xor )(u8 * , const u8 * ,
125- unsigned int ))
115+ struct crypto_cipher * tfm )
126116{
127117 void (* fn )(struct crypto_tfm * , u8 * , const u8 * ) =
128118 crypto_cipher_alg (tfm )-> cia_decrypt ;
@@ -134,7 +124,7 @@ static int crypto_cbc_decrypt_segment(struct blkcipher_desc *desc,
134124
135125 do {
136126 fn (crypto_cipher_tfm (tfm ), dst , src );
137- xor (dst , iv , bsize );
127+ crypto_xor (dst , iv , bsize );
138128 iv = src ;
139129
140130 src += bsize ;
@@ -148,9 +138,7 @@ static int crypto_cbc_decrypt_segment(struct blkcipher_desc *desc,
148138
149139static int crypto_cbc_decrypt_inplace (struct blkcipher_desc * desc ,
150140 struct blkcipher_walk * walk ,
151- struct crypto_cipher * tfm ,
152- void (* xor )(u8 * , const u8 * ,
153- unsigned int ))
141+ struct crypto_cipher * tfm )
154142{
155143 void (* fn )(struct crypto_tfm * , u8 * , const u8 * ) =
156144 crypto_cipher_alg (tfm )-> cia_decrypt ;
@@ -171,11 +159,11 @@ static int crypto_cbc_decrypt_inplace(struct blkcipher_desc *desc,
171159 fn (crypto_cipher_tfm (tfm ), src , src );
172160 if ((nbytes -= bsize ) < bsize )
173161 break ;
174- xor (src , src - bsize , bsize );
162+ crypto_xor (src , src - bsize , bsize );
175163 src -= bsize ;
176164 }
177165
178- xor (src , first_iv , bsize );
166+ crypto_xor (src , first_iv , bsize );
179167
180168 return nbytes ;
181169}
@@ -188,79 +176,29 @@ static int crypto_cbc_decrypt(struct blkcipher_desc *desc,
188176 struct crypto_blkcipher * tfm = desc -> tfm ;
189177 struct crypto_cbc_ctx * ctx = crypto_blkcipher_ctx (tfm );
190178 struct crypto_cipher * child = ctx -> child ;
191- void (* xor )(u8 * , const u8 * , unsigned int bs ) = ctx -> xor ;
192179 int err ;
193180
194181 blkcipher_walk_init (& walk , dst , src , nbytes );
195182 err = blkcipher_walk_virt (desc , & walk );
196183
197184 while ((nbytes = walk .nbytes )) {
198185 if (walk .src .virt .addr == walk .dst .virt .addr )
199- nbytes = crypto_cbc_decrypt_inplace (desc , & walk , child ,
200- xor );
186+ nbytes = crypto_cbc_decrypt_inplace (desc , & walk , child );
201187 else
202- nbytes = crypto_cbc_decrypt_segment (desc , & walk , child ,
203- xor );
188+ nbytes = crypto_cbc_decrypt_segment (desc , & walk , child );
204189 err = blkcipher_walk_done (desc , & walk , nbytes );
205190 }
206191
207192 return err ;
208193}
209194
210- static void xor_byte (u8 * a , const u8 * b , unsigned int bs )
211- {
212- do {
213- * a ++ ^= * b ++ ;
214- } while (-- bs );
215- }
216-
217- static void xor_quad (u8 * dst , const u8 * src , unsigned int bs )
218- {
219- u32 * a = (u32 * )dst ;
220- u32 * b = (u32 * )src ;
221-
222- do {
223- * a ++ ^= * b ++ ;
224- } while ((bs -= 4 ));
225- }
226-
227- static void xor_64 (u8 * a , const u8 * b , unsigned int bs )
228- {
229- ((u32 * )a )[0 ] ^= ((u32 * )b )[0 ];
230- ((u32 * )a )[1 ] ^= ((u32 * )b )[1 ];
231- }
232-
233- static void xor_128 (u8 * a , const u8 * b , unsigned int bs )
234- {
235- ((u32 * )a )[0 ] ^= ((u32 * )b )[0 ];
236- ((u32 * )a )[1 ] ^= ((u32 * )b )[1 ];
237- ((u32 * )a )[2 ] ^= ((u32 * )b )[2 ];
238- ((u32 * )a )[3 ] ^= ((u32 * )b )[3 ];
239- }
240-
241195static int crypto_cbc_init_tfm (struct crypto_tfm * tfm )
242196{
243197 struct crypto_instance * inst = (void * )tfm -> __crt_alg ;
244198 struct crypto_spawn * spawn = crypto_instance_ctx (inst );
245199 struct crypto_cbc_ctx * ctx = crypto_tfm_ctx (tfm );
246200 struct crypto_cipher * cipher ;
247201
248- switch (crypto_tfm_alg_blocksize (tfm )) {
249- case 8 :
250- ctx -> xor = xor_64 ;
251- break ;
252-
253- case 16 :
254- ctx -> xor = xor_128 ;
255- break ;
256-
257- default :
258- if (crypto_tfm_alg_blocksize (tfm ) % 4 )
259- ctx -> xor = xor_byte ;
260- else
261- ctx -> xor = xor_quad ;
262- }
263-
264202 cipher = crypto_spawn_cipher (spawn );
265203 if (IS_ERR (cipher ))
266204 return PTR_ERR (cipher );
@@ -300,8 +238,9 @@ static struct crypto_instance *crypto_cbc_alloc(struct rtattr **tb)
300238 inst -> alg .cra_alignmask = alg -> cra_alignmask ;
301239 inst -> alg .cra_type = & crypto_blkcipher_type ;
302240
303- if (!(alg -> cra_blocksize % 4 ))
304- inst -> alg .cra_alignmask |= 3 ;
241+ /* We access the data as u32s when xoring. */
242+ inst -> alg .cra_alignmask |= __alignof__(u32 ) - 1 ;
243+
305244 inst -> alg .cra_blkcipher .ivsize = alg -> cra_blocksize ;
306245 inst -> alg .cra_blkcipher .min_keysize = alg -> cra_cipher .cia_min_keysize ;
307246 inst -> alg .cra_blkcipher .max_keysize = alg -> cra_cipher .cia_max_keysize ;
0 commit comments