@@ -31,51 +31,6 @@ struct crypto_ctr_ctx {
3131 u8 * nonce ;
3232};
3333
34- static inline void __ctr_inc_byte (u8 * a , unsigned int size )
35- {
36- u8 * b = (a + size );
37- u8 c ;
38-
39- for (; size ; size -- ) {
40- c = * -- b + 1 ;
41- * b = c ;
42- if (c )
43- break ;
44- }
45- }
46-
47- static void ctr_inc_quad (u8 * a , unsigned int size )
48- {
49- __be32 * b = (__be32 * )(a + size );
50- u32 c ;
51-
52- for (; size >= 4 ; size -= 4 ) {
53- c = be32_to_cpu (* -- b ) + 1 ;
54- * b = cpu_to_be32 (c );
55- if (c )
56- return ;
57- }
58-
59- __ctr_inc_byte (a , size );
60- }
61-
62- static void xor_byte (u8 * a , const u8 * b , unsigned int bs )
63- {
64- for (; bs ; bs -- )
65- * a ++ ^= * b ++ ;
66- }
67-
68- static void xor_quad (u8 * dst , const u8 * src , unsigned int bs )
69- {
70- u32 * a = (u32 * )dst ;
71- u32 * b = (u32 * )src ;
72-
73- for (; bs >= 4 ; bs -= 4 )
74- * a ++ ^= * b ++ ;
75-
76- xor_byte ((u8 * )a , (u8 * )b , bs );
77- }
78-
7934static int crypto_ctr_setkey (struct crypto_tfm * parent , const u8 * key ,
8035 unsigned int keylen )
8136{
@@ -111,7 +66,8 @@ static int crypto_ctr_crypt_segment(struct blkcipher_walk *walk,
11166 void (* fn )(struct crypto_tfm * , u8 * , const u8 * ) =
11267 crypto_cipher_alg (tfm )-> cia_encrypt ;
11368 unsigned int bsize = crypto_cipher_blocksize (tfm );
114- unsigned long alignmask = crypto_cipher_alignmask (tfm );
69+ unsigned long alignmask = crypto_cipher_alignmask (tfm ) |
70+ (__alignof__(u32 ) - 1 );
11571 u8 ks [bsize + alignmask ];
11672 u8 * keystream = (u8 * )ALIGN ((unsigned long )ks , alignmask + 1 );
11773 u8 * src = walk -> src .virt .addr ;
@@ -121,13 +77,13 @@ static int crypto_ctr_crypt_segment(struct blkcipher_walk *walk,
12177 do {
12278 /* create keystream */
12379 fn (crypto_cipher_tfm (tfm ), keystream , ctrblk );
124- xor_quad (keystream , src , min (nbytes , bsize ));
80+ crypto_xor (keystream , src , min (nbytes , bsize ));
12581
12682 /* copy result into dst */
12783 memcpy (dst , keystream , min (nbytes , bsize ));
12884
12985 /* increment counter in counterblock */
130- ctr_inc_quad (ctrblk + ( bsize - countersize ) , countersize );
86+ crypto_inc (ctrblk + bsize - countersize , countersize );
13187
13288 if (nbytes < bsize )
13389 break ;
@@ -148,7 +104,8 @@ static int crypto_ctr_crypt_inplace(struct blkcipher_walk *walk,
148104 void (* fn )(struct crypto_tfm * , u8 * , const u8 * ) =
149105 crypto_cipher_alg (tfm )-> cia_encrypt ;
150106 unsigned int bsize = crypto_cipher_blocksize (tfm );
151- unsigned long alignmask = crypto_cipher_alignmask (tfm );
107+ unsigned long alignmask = crypto_cipher_alignmask (tfm ) |
108+ (__alignof__(u32 ) - 1 );
152109 unsigned int nbytes = walk -> nbytes ;
153110 u8 * src = walk -> src .virt .addr ;
154111 u8 ks [bsize + alignmask ];
@@ -157,10 +114,10 @@ static int crypto_ctr_crypt_inplace(struct blkcipher_walk *walk,
157114 do {
158115 /* create keystream */
159116 fn (crypto_cipher_tfm (tfm ), keystream , ctrblk );
160- xor_quad (src , keystream , min (nbytes , bsize ));
117+ crypto_xor (src , keystream , min (nbytes , bsize ));
161118
162119 /* increment counter in counterblock */
163- ctr_inc_quad (ctrblk + ( bsize - countersize ) , countersize );
120+ crypto_inc (ctrblk + bsize - countersize , countersize );
164121
165122 if (nbytes < bsize )
166123 break ;
@@ -184,7 +141,8 @@ static int crypto_ctr_crypt(struct blkcipher_desc *desc,
184141 unsigned int bsize = crypto_cipher_blocksize (child );
185142 struct ctr_instance_ctx * ictx =
186143 crypto_instance_ctx (crypto_tfm_alg_instance (& tfm -> base ));
187- unsigned long alignmask = crypto_cipher_alignmask (child );
144+ unsigned long alignmask = crypto_cipher_alignmask (child ) |
145+ (__alignof__(u32 ) - 1 );
188146 u8 cblk [bsize + alignmask ];
189147 u8 * counterblk = (u8 * )ALIGN ((unsigned long )cblk , alignmask + 1 );
190148 int err ;
@@ -198,8 +156,7 @@ static int crypto_ctr_crypt(struct blkcipher_desc *desc,
198156 memcpy (counterblk + ictx -> noncesize , walk .iv , ictx -> ivsize );
199157
200158 /* initialize counter portion of counter block */
201- ctr_inc_quad (counterblk + (bsize - ictx -> countersize ),
202- ictx -> countersize );
159+ crypto_inc (counterblk + bsize - ictx -> countersize , ictx -> countersize );
203160
204161 while (walk .nbytes ) {
205162 if (walk .src .virt .addr == walk .dst .virt .addr )
@@ -284,6 +241,10 @@ static struct crypto_instance *crypto_ctr_alloc(struct rtattr **tb)
284241 (countersize > alg -> cra_blocksize ) || (countersize < 4 ))
285242 goto out_put_alg ;
286243
244+ /* If this is false we'd fail the alignment of crypto_inc. */
245+ if ((alg -> cra_blocksize - countersize ) % 4 )
246+ goto out_put_alg ;
247+
287248 inst = kzalloc (sizeof (* inst ) + sizeof (* ictx ), GFP_KERNEL );
288249 err = - ENOMEM ;
289250 if (!inst )
@@ -316,7 +277,7 @@ static struct crypto_instance *crypto_ctr_alloc(struct rtattr **tb)
316277 inst -> alg .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER ;
317278 inst -> alg .cra_priority = alg -> cra_priority ;
318279 inst -> alg .cra_blocksize = 1 ;
319- inst -> alg .cra_alignmask = 3 ;
280+ inst -> alg .cra_alignmask = __alignof__( u32 ) - 1 ;
320281 inst -> alg .cra_type = & crypto_blkcipher_type ;
321282
322283 inst -> alg .cra_blkcipher .ivsize = ivsize ;
0 commit comments