2929
3030#define LRW_BLOCK_SIZE 16
3131
32- struct priv {
33- struct crypto_cipher * child ;
32+ struct lrw_table_ctx {
3433 /* optimizes multiplying a random (non incrementing, as at the
3534 * start of a new sector) value with key2, we could also have
3635 * used 4k optimization tables or no optimization at all. In the
@@ -45,6 +44,11 @@ struct priv {
4544 be128 mulinc [128 ];
4645};
4746
47+ struct priv {
48+ struct crypto_cipher * child ;
49+ struct lrw_table_ctx table ;
50+ };
51+
4852static inline void setbit128_bbe (void * b , int bit )
4953{
5054 __set_bit (bit ^ (0x80 -
@@ -56,28 +60,16 @@ static inline void setbit128_bbe(void *b, int bit)
5660 ), b );
5761}
5862
59- static int setkey (struct crypto_tfm * parent , const u8 * key ,
60- unsigned int keylen )
63+ static int lrw_init_table (struct lrw_table_ctx * ctx , const u8 * tweak )
6164{
62- struct priv * ctx = crypto_tfm_ctx (parent );
63- struct crypto_cipher * child = ctx -> child ;
64- int err , i ;
6565 be128 tmp = { 0 };
66- int bsize = LRW_BLOCK_SIZE ;
67-
68- crypto_cipher_clear_flags (child , CRYPTO_TFM_REQ_MASK );
69- crypto_cipher_set_flags (child , crypto_tfm_get_flags (parent ) &
70- CRYPTO_TFM_REQ_MASK );
71- if ((err = crypto_cipher_setkey (child , key , keylen - bsize )))
72- return err ;
73- crypto_tfm_set_flags (parent , crypto_cipher_get_flags (child ) &
74- CRYPTO_TFM_RES_MASK );
66+ int i ;
7567
7668 if (ctx -> table )
7769 gf128mul_free_64k (ctx -> table );
7870
7971 /* initialize multiplication table for Key2 */
80- ctx -> table = gf128mul_init_64k_bbe ((be128 * )( key + keylen - bsize ) );
72+ ctx -> table = gf128mul_init_64k_bbe ((be128 * )tweak );
8173 if (!ctx -> table )
8274 return - ENOMEM ;
8375
@@ -91,6 +83,32 @@ static int setkey(struct crypto_tfm *parent, const u8 *key,
9183 return 0 ;
9284}
9385
86+ static void lrw_free_table (struct lrw_table_ctx * ctx )
87+ {
88+ if (ctx -> table )
89+ gf128mul_free_64k (ctx -> table );
90+ }
91+
92+ static int setkey (struct crypto_tfm * parent , const u8 * key ,
93+ unsigned int keylen )
94+ {
95+ struct priv * ctx = crypto_tfm_ctx (parent );
96+ struct crypto_cipher * child = ctx -> child ;
97+ int err , bsize = LRW_BLOCK_SIZE ;
98+ const u8 * tweak = key + keylen - bsize ;
99+
100+ crypto_cipher_clear_flags (child , CRYPTO_TFM_REQ_MASK );
101+ crypto_cipher_set_flags (child , crypto_tfm_get_flags (parent ) &
102+ CRYPTO_TFM_REQ_MASK );
103+ err = crypto_cipher_setkey (child , key , keylen - bsize );
104+ if (err )
105+ return err ;
106+ crypto_tfm_set_flags (parent , crypto_cipher_get_flags (child ) &
107+ CRYPTO_TFM_RES_MASK );
108+
109+ return lrw_init_table (& ctx -> table , tweak );
110+ }
111+
94112struct sinfo {
95113 be128 t ;
96114 struct crypto_tfm * tfm ;
@@ -157,15 +175,16 @@ static int crypt(struct blkcipher_desc *d,
157175 s .t = * iv ;
158176
159177 /* T <- I*Key2 */
160- gf128mul_64k_bbe (& s .t , ctx -> table );
178+ gf128mul_64k_bbe (& s .t , ctx -> table . table );
161179
162180 goto first ;
163181
164182 for (;;) {
165183 do {
166184 /* T <- I*Key2, using the optimization
167185 * discussed in the specification */
168- be128_xor (& s .t , & s .t , & ctx -> mulinc [get_index128 (iv )]);
186+ be128_xor (& s .t , & s .t ,
187+ & ctx -> table .mulinc [get_index128 (iv )]);
169188 inc (iv );
170189
171190first :
@@ -233,8 +252,8 @@ static int init_tfm(struct crypto_tfm *tfm)
233252static void exit_tfm (struct crypto_tfm * tfm )
234253{
235254 struct priv * ctx = crypto_tfm_ctx (tfm );
236- if ( ctx -> table )
237- gf128mul_free_64k ( ctx -> table );
255+
256+ lrw_free_table ( & ctx -> table );
238257 crypto_free_cipher (ctx -> child );
239258}
240259
0 commit comments