Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

crypto: lrw - split gf128mul table initialization from setkey

Split gf128mul initialization from setkey so that it can be used outside
lrw-module.

Signed-off-by: Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

authored by

Jussi Kivilinna and committed by
Herbert Xu
171c0204 4660720d

+40 -21
+40 -21
crypto/lrw.c
··· 29 29 30 30 #define LRW_BLOCK_SIZE 16 31 31 32 - struct priv { 33 - struct crypto_cipher *child; 32 + struct lrw_table_ctx { 34 33 /* optimizes multiplying a random (non incrementing, as at the 35 34 * start of a new sector) value with key2, we could also have 36 35 * used 4k optimization tables or no optimization at all. In the ··· 44 45 be128 mulinc[128]; 45 46 }; 46 47 48 + struct priv { 49 + struct crypto_cipher *child; 50 + struct lrw_table_ctx table; 51 + }; 52 + 47 53 static inline void setbit128_bbe(void *b, int bit) 48 54 { 49 55 __set_bit(bit ^ (0x80 - ··· 60 56 ), b); 61 57 } 62 58 63 - static int setkey(struct crypto_tfm *parent, const u8 *key, 64 - unsigned int keylen) 59 + static int lrw_init_table(struct lrw_table_ctx *ctx, const u8 *tweak) 65 60 { 66 - struct priv *ctx = crypto_tfm_ctx(parent); 67 - struct crypto_cipher *child = ctx->child; 68 - int err, i; 69 61 be128 tmp = { 0 }; 70 - int bsize = LRW_BLOCK_SIZE; 71 - 72 - crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK); 73 - crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) & 74 - CRYPTO_TFM_REQ_MASK); 75 - if ((err = crypto_cipher_setkey(child, key, keylen - bsize))) 76 - return err; 77 - crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) & 78 - CRYPTO_TFM_RES_MASK); 62 + int i; 79 63 80 64 if (ctx->table) 81 65 gf128mul_free_64k(ctx->table); 82 66 83 67 /* initialize multiplication table for Key2 */ 84 - ctx->table = gf128mul_init_64k_bbe((be128 *)(key + keylen - bsize)); 68 + ctx->table = gf128mul_init_64k_bbe((be128 *)tweak); 85 69 if (!ctx->table) 86 70 return -ENOMEM; 87 71 ··· 81 89 } 82 90 83 91 return 0; 92 + } 93 + 94 + static void lrw_free_table(struct lrw_table_ctx *ctx) 95 + { 96 + if (ctx->table) 97 + gf128mul_free_64k(ctx->table); 98 + } 99 + 100 + static int setkey(struct crypto_tfm *parent, const u8 *key, 101 + unsigned int keylen) 102 + { 103 + struct priv *ctx = crypto_tfm_ctx(parent); 104 + struct crypto_cipher *child = ctx->child; 105 + int err, bsize = LRW_BLOCK_SIZE; 106 + const u8 *tweak = key + keylen - bsize; 107 + 108 + crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK); 109 + crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) & 110 + CRYPTO_TFM_REQ_MASK); 111 + err = crypto_cipher_setkey(child, key, keylen - bsize); 112 + if (err) 113 + return err; 114 + crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) & 115 + CRYPTO_TFM_RES_MASK); 116 + 117 + return lrw_init_table(&ctx->table, tweak); 84 118 } 85 119 86 120 struct sinfo { ··· 175 157 s.t = *iv; 176 158 177 159 /* T <- I*Key2 */ 178 - gf128mul_64k_bbe(&s.t, ctx->table); 160 + gf128mul_64k_bbe(&s.t, ctx->table.table); 179 161 180 162 goto first; 181 163 ··· 183 165 do { 184 166 /* T <- I*Key2, using the optimization 185 167 * discussed in the specification */ 186 - be128_xor(&s.t, &s.t, &ctx->mulinc[get_index128(iv)]); 168 + be128_xor(&s.t, &s.t, 169 + &ctx->table.mulinc[get_index128(iv)]); 187 170 inc(iv); 188 171 189 172 first: ··· 252 233 static void exit_tfm(struct crypto_tfm *tfm) 253 234 { 254 235 struct priv *ctx = crypto_tfm_ctx(tfm); 255 - if (ctx->table) 256 - gf128mul_free_64k(ctx->table); 236 + 237 + lrw_free_table(&ctx->table); 257 238 crypto_free_cipher(ctx->child); 258 239 } 259 240