diff options
| author | Sebastian Siewior <sebastian@breakpoint.cc> | 2007-11-08 08:20:30 -0500 |
|---|---|---|
| committer | Herbert Xu <herbert@gondor.apana.org.au> | 2008-01-10 16:16:09 -0500 |
| commit | 96e82e4551d38e0863b366a7b61185bc4a9946cc (patch) | |
| tree | 514e38d847cb09c55230ceb3088329ed4175c55c | |
| parent | be5fb270125729b7bca7879967f1dfadff0d9841 (diff) | |
[CRYPTO] aes-generic: Make key generation exportable
This patch exports four tables and the set_key() routine. This ressources
can be shared by other AES implementations (aes-x86_64 for instance).
The decryption key has been turned around (deckey[0] is the first piece
of the key instead of deckey[keylen+20]). The encrypt/decrypt functions
are looking now identical (except they are using different tables and
key).
Signed-off-by: Sebastian Siewior <sebastian@breakpoint.cc>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
| -rw-r--r-- | crypto/aes_generic.c | 249 | ||||
| -rw-r--r-- | include/crypto/aes.h | 16 |
2 files changed, 136 insertions, 129 deletions
diff --git a/crypto/aes_generic.c b/crypto/aes_generic.c index df8df4d346d2..cf30af74480f 100644 --- a/crypto/aes_generic.c +++ b/crypto/aes_generic.c | |||
| @@ -47,11 +47,6 @@ | |||
| 47 | * --------------------------------------------------------------------------- | 47 | * --------------------------------------------------------------------------- |
| 48 | */ | 48 | */ |
| 49 | 49 | ||
| 50 | /* Some changes from the Gladman version: | ||
| 51 | s/RIJNDAEL(e_key)/E_KEY/g | ||
| 52 | s/RIJNDAEL(d_key)/D_KEY/g | ||
| 53 | */ | ||
| 54 | |||
| 55 | #include <crypto/aes.h> | 50 | #include <crypto/aes.h> |
| 56 | #include <linux/module.h> | 51 | #include <linux/module.h> |
| 57 | #include <linux/init.h> | 52 | #include <linux/init.h> |
| @@ -60,32 +55,26 @@ | |||
| 60 | #include <linux/crypto.h> | 55 | #include <linux/crypto.h> |
| 61 | #include <asm/byteorder.h> | 56 | #include <asm/byteorder.h> |
| 62 | 57 | ||
| 63 | /* | ||
| 64 | * #define byte(x, nr) ((unsigned char)((x) >> (nr*8))) | ||
| 65 | */ | ||
| 66 | static inline u8 byte(const u32 x, const unsigned n) | 58 | static inline u8 byte(const u32 x, const unsigned n) |
| 67 | { | 59 | { |
| 68 | return x >> (n << 3); | 60 | return x >> (n << 3); |
| 69 | } | 61 | } |
| 70 | 62 | ||
| 71 | struct aes_ctx { | ||
| 72 | int key_length; | ||
| 73 | u32 buf[120]; | ||
| 74 | }; | ||
| 75 | |||
| 76 | #define E_KEY (&ctx->buf[0]) | ||
| 77 | #define D_KEY (&ctx->buf[60]) | ||
| 78 | |||
| 79 | static u8 pow_tab[256] __initdata; | 63 | static u8 pow_tab[256] __initdata; |
| 80 | static u8 log_tab[256] __initdata; | 64 | static u8 log_tab[256] __initdata; |
| 81 | static u8 sbx_tab[256] __initdata; | 65 | static u8 sbx_tab[256] __initdata; |
| 82 | static u8 isb_tab[256] __initdata; | 66 | static u8 isb_tab[256] __initdata; |
| 83 | static u32 rco_tab[10]; | 67 | static u32 rco_tab[10]; |
| 84 | static u32 ft_tab[4][256]; | ||
| 85 | static u32 it_tab[4][256]; | ||
| 86 | 68 | ||
| 87 | static u32 fl_tab[4][256]; | 69 | u32 crypto_ft_tab[4][256]; |
| 88 | static u32 il_tab[4][256]; | 70 | u32 crypto_fl_tab[4][256]; |
| 71 | u32 crypto_it_tab[4][256]; | ||
| 72 | u32 crypto_il_tab[4][256]; | ||
| 73 | |||
| 74 | EXPORT_SYMBOL_GPL(crypto_ft_tab); | ||
| 75 | EXPORT_SYMBOL_GPL(crypto_fl_tab); | ||
| 76 | EXPORT_SYMBOL_GPL(crypto_it_tab); | ||
| 77 | EXPORT_SYMBOL_GPL(crypto_il_tab); | ||
| 89 | 78 | ||
| 90 | static inline u8 __init f_mult(u8 a, u8 b) | 79 | static inline u8 __init f_mult(u8 a, u8 b) |
| 91 | { | 80 | { |
| @@ -134,37 +123,37 @@ static void __init gen_tabs(void) | |||
| 134 | p = sbx_tab[i]; | 123 | p = sbx_tab[i]; |
| 135 | 124 | ||
| 136 | t = p; | 125 | t = p; |
| 137 | fl_tab[0][i] = t; | 126 | crypto_fl_tab[0][i] = t; |
| 138 | fl_tab[1][i] = rol32(t, 8); | 127 | crypto_fl_tab[1][i] = rol32(t, 8); |
| 139 | fl_tab[2][i] = rol32(t, 16); | 128 | crypto_fl_tab[2][i] = rol32(t, 16); |
| 140 | fl_tab[3][i] = rol32(t, 24); | 129 | crypto_fl_tab[3][i] = rol32(t, 24); |
| 141 | 130 | ||
| 142 | t = ((u32) ff_mult(2, p)) | | 131 | t = ((u32) ff_mult(2, p)) | |
| 143 | ((u32) p << 8) | | 132 | ((u32) p << 8) | |
| 144 | ((u32) p << 16) | ((u32) ff_mult(3, p) << 24); | 133 | ((u32) p << 16) | ((u32) ff_mult(3, p) << 24); |
| 145 | 134 | ||
| 146 | ft_tab[0][i] = t; | 135 | crypto_ft_tab[0][i] = t; |
| 147 | ft_tab[1][i] = rol32(t, 8); | 136 | crypto_ft_tab[1][i] = rol32(t, 8); |
| 148 | ft_tab[2][i] = rol32(t, 16); | 137 | crypto_ft_tab[2][i] = rol32(t, 16); |
| 149 | ft_tab[3][i] = rol32(t, 24); | 138 | crypto_ft_tab[3][i] = rol32(t, 24); |
| 150 | 139 | ||
| 151 | p = isb_tab[i]; | 140 | p = isb_tab[i]; |
| 152 | 141 | ||
| 153 | t = p; | 142 | t = p; |
| 154 | il_tab[0][i] = t; | 143 | crypto_il_tab[0][i] = t; |
| 155 | il_tab[1][i] = rol32(t, 8); | 144 | crypto_il_tab[1][i] = rol32(t, 8); |
| 156 | il_tab[2][i] = rol32(t, 16); | 145 | crypto_il_tab[2][i] = rol32(t, 16); |
| 157 | il_tab[3][i] = rol32(t, 24); | 146 | crypto_il_tab[3][i] = rol32(t, 24); |
| 158 | 147 | ||
| 159 | t = ((u32) ff_mult(14, p)) | | 148 | t = ((u32) ff_mult(14, p)) | |
| 160 | ((u32) ff_mult(9, p) << 8) | | 149 | ((u32) ff_mult(9, p) << 8) | |
| 161 | ((u32) ff_mult(13, p) << 16) | | 150 | ((u32) ff_mult(13, p) << 16) | |
| 162 | ((u32) ff_mult(11, p) << 24); | 151 | ((u32) ff_mult(11, p) << 24); |
| 163 | 152 | ||
| 164 | it_tab[0][i] = t; | 153 | crypto_it_tab[0][i] = t; |
| 165 | it_tab[1][i] = rol32(t, 8); | 154 | crypto_it_tab[1][i] = rol32(t, 8); |
| 166 | it_tab[2][i] = rol32(t, 16); | 155 | crypto_it_tab[2][i] = rol32(t, 16); |
| 167 | it_tab[3][i] = rol32(t, 24); | 156 | crypto_it_tab[3][i] = rol32(t, 24); |
| 168 | } | 157 | } |
| 169 | } | 158 | } |
| 170 | 159 | ||
| @@ -184,69 +173,69 @@ static void __init gen_tabs(void) | |||
| 184 | } while (0) | 173 | } while (0) |
| 185 | 174 | ||
| 186 | #define ls_box(x) \ | 175 | #define ls_box(x) \ |
| 187 | fl_tab[0][byte(x, 0)] ^ \ | 176 | crypto_fl_tab[0][byte(x, 0)] ^ \ |
| 188 | fl_tab[1][byte(x, 1)] ^ \ | 177 | crypto_fl_tab[1][byte(x, 1)] ^ \ |
| 189 | fl_tab[2][byte(x, 2)] ^ \ | 178 | crypto_fl_tab[2][byte(x, 2)] ^ \ |
| 190 | fl_tab[3][byte(x, 3)] | 179 | crypto_fl_tab[3][byte(x, 3)] |
| 191 | 180 | ||
| 192 | #define loop4(i) do { \ | 181 | #define loop4(i) do { \ |
| 193 | t = ror32(t, 8); \ | 182 | t = ror32(t, 8); \ |
| 194 | t = ls_box(t) ^ rco_tab[i]; \ | 183 | t = ls_box(t) ^ rco_tab[i]; \ |
| 195 | t ^= E_KEY[4 * i]; \ | 184 | t ^= ctx->key_enc[4 * i]; \ |
| 196 | E_KEY[4 * i + 4] = t; \ | 185 | ctx->key_enc[4 * i + 4] = t; \ |
| 197 | t ^= E_KEY[4 * i + 1]; \ | 186 | t ^= ctx->key_enc[4 * i + 1]; \ |
| 198 | E_KEY[4 * i + 5] = t; \ | 187 | ctx->key_enc[4 * i + 5] = t; \ |
| 199 | t ^= E_KEY[4 * i + 2]; \ | 188 | t ^= ctx->key_enc[4 * i + 2]; \ |
| 200 | E_KEY[4 * i + 6] = t; \ | 189 | ctx->key_enc[4 * i + 6] = t; \ |
| 201 | t ^= E_KEY[4 * i + 3]; \ | 190 | t ^= ctx->key_enc[4 * i + 3]; \ |
| 202 | E_KEY[4 * i + 7] = t; \ | 191 | ctx->key_enc[4 * i + 7] = t; \ |
| 203 | } while (0) | 192 | } while (0) |
| 204 | 193 | ||
| 205 | #define loop6(i) do { \ | 194 | #define loop6(i) do { \ |
| 206 | t = ror32(t, 8); \ | 195 | t = ror32(t, 8); \ |
| 207 | t = ls_box(t) ^ rco_tab[i]; \ | 196 | t = ls_box(t) ^ rco_tab[i]; \ |
| 208 | t ^= E_KEY[6 * i]; \ | 197 | t ^= ctx->key_enc[6 * i]; \ |
| 209 | E_KEY[6 * i + 6] = t; \ | 198 | ctx->key_enc[6 * i + 6] = t; \ |
| 210 | t ^= E_KEY[6 * i + 1]; \ | 199 | t ^= ctx->key_enc[6 * i + 1]; \ |
| 211 | E_KEY[6 * i + 7] = t; \ | 200 | ctx->key_enc[6 * i + 7] = t; \ |
| 212 | t ^= E_KEY[6 * i + 2]; \ | 201 | t ^= ctx->key_enc[6 * i + 2]; \ |
| 213 | E_KEY[6 * i + 8] = t; \ | 202 | ctx->key_enc[6 * i + 8] = t; \ |
| 214 | t ^= E_KEY[6 * i + 3]; \ | 203 | t ^= ctx->key_enc[6 * i + 3]; \ |
| 215 | E_KEY[6 * i + 9] = t; \ | 204 | ctx->key_enc[6 * i + 9] = t; \ |
| 216 | t ^= E_KEY[6 * i + 4]; \ | 205 | t ^= ctx->key_enc[6 * i + 4]; \ |
| 217 | E_KEY[6 * i + 10] = t; \ | 206 | ctx->key_enc[6 * i + 10] = t; \ |
| 218 | t ^= E_KEY[6 * i + 5]; \ | 207 | t ^= ctx->key_enc[6 * i + 5]; \ |
| 219 | E_KEY[6 * i + 11] = t; \ | 208 | ctx->key_enc[6 * i + 11] = t; \ |
| 220 | } while (0) | 209 | } while (0) |
| 221 | 210 | ||
| 222 | #define loop8(i) do { \ | 211 | #define loop8(i) do { \ |
| 223 | t = ror32(t, 8); \ | 212 | t = ror32(t, 8); \ |
| 224 | t = ls_box(t) ^ rco_tab[i]; \ | 213 | t = ls_box(t) ^ rco_tab[i]; \ |
