2828
2929#include <crypto/b128ops.h>
3030#include <crypto/gf128mul.h>
31- #include <crypto/lrw.h>
3231
3332#define LRW_BUFFER_SIZE 128u
3433
34+ #define LRW_BLOCK_SIZE 16
35+
3536struct priv {
3637 struct crypto_skcipher * child ;
37- struct lrw_table_ctx table ;
38+
39+ /*
40+ * optimizes multiplying a random (non incrementing, as at the
41+ * start of a new sector) value with key2, we could also have
42+ * used 4k optimization tables or no optimization at all. In the
43+ * latter case we would have to store key2 here
44+ */
45+ struct gf128mul_64k * table ;
46+
47+ /*
48+ * stores:
49+ * key2*{ 0,0,...0,0,0,0,1 }, key2*{ 0,0,...0,0,0,1,1 },
50+ * key2*{ 0,0,...0,0,1,1,1 }, key2*{ 0,0,...0,1,1,1,1 }
51+ * key2*{ 0,0,...1,1,1,1,1 }, etc
52+ * needed for optimized multiplication of incrementing values
53+ * with key2
54+ */
55+ be128 mulinc [128 ];
3856};
3957
4058struct rctx {
@@ -65,11 +83,25 @@ static inline void setbit128_bbe(void *b, int bit)
6583 ), b );
6684}
6785
68- int lrw_init_table (struct lrw_table_ctx * ctx , const u8 * tweak )
86+ static int setkey (struct crypto_skcipher * parent , const u8 * key ,
87+ unsigned int keylen )
6988{
89+ struct priv * ctx = crypto_skcipher_ctx (parent );
90+ struct crypto_skcipher * child = ctx -> child ;
91+ int err , bsize = LRW_BLOCK_SIZE ;
92+ const u8 * tweak = key + keylen - bsize ;
7093 be128 tmp = { 0 };
7194 int i ;
7295
96+ crypto_skcipher_clear_flags (child , CRYPTO_TFM_REQ_MASK );
97+ crypto_skcipher_set_flags (child , crypto_skcipher_get_flags (parent ) &
98+ CRYPTO_TFM_REQ_MASK );
99+ err = crypto_skcipher_setkey (child , key , keylen - bsize );
100+ crypto_skcipher_set_flags (parent , crypto_skcipher_get_flags (child ) &
101+ CRYPTO_TFM_RES_MASK );
102+ if (err )
103+ return err ;
104+
73105 if (ctx -> table )
74106 gf128mul_free_64k (ctx -> table );
75107
@@ -87,34 +119,6 @@ int lrw_init_table(struct lrw_table_ctx *ctx, const u8 *tweak)
87119
88120 return 0 ;
89121}
90- EXPORT_SYMBOL_GPL (lrw_init_table );
91-
92- void lrw_free_table (struct lrw_table_ctx * ctx )
93- {
94- if (ctx -> table )
95- gf128mul_free_64k (ctx -> table );
96- }
97- EXPORT_SYMBOL_GPL (lrw_free_table );
98-
99- static int setkey (struct crypto_skcipher * parent , const u8 * key ,
100- unsigned int keylen )
101- {
102- struct priv * ctx = crypto_skcipher_ctx (parent );
103- struct crypto_skcipher * child = ctx -> child ;
104- int err , bsize = LRW_BLOCK_SIZE ;
105- const u8 * tweak = key + keylen - bsize ;
106-
107- crypto_skcipher_clear_flags (child , CRYPTO_TFM_REQ_MASK );
108- crypto_skcipher_set_flags (child , crypto_skcipher_get_flags (parent ) &
109- CRYPTO_TFM_REQ_MASK );
110- err = crypto_skcipher_setkey (child , key , keylen - bsize );
111- crypto_skcipher_set_flags (parent , crypto_skcipher_get_flags (child ) &
112- CRYPTO_TFM_RES_MASK );
113- if (err )
114- return err ;
115-
116- return lrw_init_table (& ctx -> table , tweak );
117- }
118122
119123static inline void inc (be128 * iv )
120124{
@@ -238,7 +242,7 @@ static int pre_crypt(struct skcipher_request *req)
238242 /* T <- I*Key2, using the optimization
239243 * discussed in the specification */
240244 be128_xor (& rctx -> t , & rctx -> t ,
241- & ctx -> table . mulinc [get_index128 (iv )]);
245+ & ctx -> mulinc [get_index128 (iv )]);
242246 inc (iv );
243247 } while ((avail -= bs ) >= bs );
244248
@@ -301,7 +305,7 @@ static int init_crypt(struct skcipher_request *req, crypto_completion_t done)
301305 memcpy (& rctx -> t , req -> iv , sizeof (rctx -> t ));
302306
303307 /* T <- I*Key2 */
304- gf128mul_64k_bbe (& rctx -> t , ctx -> table . table );
308+ gf128mul_64k_bbe (& rctx -> t , ctx -> table );
305309
306310 return 0 ;
307311}
@@ -416,85 +420,6 @@ static int decrypt(struct skcipher_request *req)
416420 return do_decrypt (req , init_crypt (req , decrypt_done ));
417421}
418422
419- int lrw_crypt (struct blkcipher_desc * desc , struct scatterlist * sdst ,
420- struct scatterlist * ssrc , unsigned int nbytes ,
421- struct lrw_crypt_req * req )
422- {
423- const unsigned int bsize = LRW_BLOCK_SIZE ;
424- const unsigned int max_blks = req -> tbuflen / bsize ;
425- struct lrw_table_ctx * ctx = req -> table_ctx ;
426- struct blkcipher_walk walk ;
427- unsigned int nblocks ;
428- be128 * iv , * src , * dst , * t ;
429- be128 * t_buf = req -> tbuf ;
430- int err , i ;
431-
432- BUG_ON (max_blks < 1 );
433-
434- blkcipher_walk_init (& walk , sdst , ssrc , nbytes );
435-
436- err = blkcipher_walk_virt (desc , & walk );
437- nbytes = walk .nbytes ;
438- if (!nbytes )
439- return err ;
440-
441- nblocks = min (walk .nbytes / bsize , max_blks );
442- src = (be128 * )walk .src .virt .addr ;
443- dst = (be128 * )walk .dst .virt .addr ;
444-
445- /* calculate first value of T */
446- iv = (be128 * )walk .iv ;
447- t_buf [0 ] = * iv ;
448-
449- /* T <- I*Key2 */
450- gf128mul_64k_bbe (& t_buf [0 ], ctx -> table );
451-
452- i = 0 ;
453- goto first ;
454-
455- for (;;) {
456- do {
457- for (i = 0 ; i < nblocks ; i ++ ) {
458- /* T <- I*Key2, using the optimization
459- * discussed in the specification */
460- be128_xor (& t_buf [i ], t ,
461- & ctx -> mulinc [get_index128 (iv )]);
462- inc (iv );
463- first :
464- t = & t_buf [i ];
465-
466- /* PP <- T xor P */
467- be128_xor (dst + i , t , src + i );
468- }
469-
470- /* CC <- E(Key2,PP) */
471- req -> crypt_fn (req -> crypt_ctx , (u8 * )dst ,
472- nblocks * bsize );
473-
474- /* C <- T xor CC */
475- for (i = 0 ; i < nblocks ; i ++ )
476- be128_xor (dst + i , dst + i , & t_buf [i ]);
477-
478- src += nblocks ;
479- dst += nblocks ;
480- nbytes -= nblocks * bsize ;
481- nblocks = min (nbytes / bsize , max_blks );
482- } while (nblocks > 0 );
483-
484- err = blkcipher_walk_done (desc , & walk , nbytes );
485- nbytes = walk .nbytes ;
486- if (!nbytes )
487- break ;
488-
489- nblocks = min (nbytes / bsize , max_blks );
490- src = (be128 * )walk .src .virt .addr ;
491- dst = (be128 * )walk .dst .virt .addr ;
492- }
493-
494- return err ;
495- }
496- EXPORT_SYMBOL_GPL (lrw_crypt );
497-
498423static int init_tfm (struct crypto_skcipher * tfm )
499424{
500425 struct skcipher_instance * inst = skcipher_alg_instance (tfm );
@@ -518,7 +443,8 @@ static void exit_tfm(struct crypto_skcipher *tfm)
518443{
519444 struct priv * ctx = crypto_skcipher_ctx (tfm );
520445
521- lrw_free_table (& ctx -> table );
446+ if (ctx -> table )
447+ gf128mul_free_64k (ctx -> table );
522448 crypto_free_skcipher (ctx -> child );
523449}
524450
0 commit comments