Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Cryptographic API. |
| 3 | * |
| 4 | * AES Cipher Algorithm. |
| 5 | * |
| 6 | * Based on Brian Gladman's code. |
| 7 | * |
| 8 | * Linux developers: |
| 9 | * Alexander Kjeldaas <astor@fast.no> |
| 10 | * Herbert Valerio Riedel <hvr@hvrlab.org> |
| 11 | * Kyle McMartin <kyle@debian.org> |
| 12 | * Adam J. Richter <adam@yggdrasil.com> (conversion to 2.5 API). |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License as published by |
| 16 | * the Free Software Foundation; either version 2 of the License, or |
| 17 | * (at your option) any later version. |
| 18 | * |
| 19 | * --------------------------------------------------------------------------- |
| 20 | * Copyright (c) 2002, Dr Brian Gladman <brg@gladman.me.uk>, Worcester, UK. |
| 21 | * All rights reserved. |
| 22 | * |
| 23 | * LICENSE TERMS |
| 24 | * |
| 25 | * The free distribution and use of this software in both source and binary |
| 26 | * form is allowed (with or without changes) provided that: |
| 27 | * |
| 28 | * 1. distributions of this source code include the above copyright |
| 29 | * notice, this list of conditions and the following disclaimer; |
| 30 | * |
| 31 | * 2. distributions in binary form include the above copyright |
| 32 | * notice, this list of conditions and the following disclaimer |
| 33 | * in the documentation and/or other associated materials; |
| 34 | * |
| 35 | * 3. the copyright holder's name is not used to endorse products |
| 36 | * built using this software without specific written permission. |
| 37 | * |
| 38 | * ALTERNATIVELY, provided that this notice is retained in full, this product |
| 39 | * may be distributed under the terms of the GNU General Public License (GPL), |
| 40 | * in which case the provisions of the GPL apply INSTEAD OF those given above. |
| 41 | * |
| 42 | * DISCLAIMER |
| 43 | * |
| 44 | * This software is provided 'as is' with no explicit or implied warranties |
| 45 | * in respect of its properties, including, but not limited to, correctness |
| 46 | * and/or fitness for purpose. |
| 47 | * --------------------------------------------------------------------------- |
| 48 | */ |
| 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 | |
Sebastian Siewior | 89e1265 | 2007-10-17 23:18:57 +0800 | [diff] [blame] | 55 | #include <crypto/aes.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | #include <linux/module.h> |
| 57 | #include <linux/init.h> |
| 58 | #include <linux/types.h> |
| 59 | #include <linux/errno.h> |
| 60 | #include <linux/crypto.h> |
| 61 | #include <asm/byteorder.h> |
| 62 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | /* |
| 64 | * #define byte(x, nr) ((unsigned char)((x) >> (nr*8))) |
| 65 | */ |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame^] | 66 | static inline u8 byte(const u32 x, const unsigned n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | { |
| 68 | return x >> (n << 3); |
| 69 | } |
| 70 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | struct aes_ctx { |
| 72 | int key_length; |
David McCullough | 55e9dce | 2006-03-15 21:08:51 +1100 | [diff] [blame] | 73 | u32 buf[120]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | }; |
| 75 | |
David McCullough | 55e9dce | 2006-03-15 21:08:51 +1100 | [diff] [blame] | 76 | #define E_KEY (&ctx->buf[0]) |
| 77 | #define D_KEY (&ctx->buf[60]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
| 79 | static u8 pow_tab[256] __initdata; |
| 80 | static u8 log_tab[256] __initdata; |
| 81 | static u8 sbx_tab[256] __initdata; |
| 82 | static u8 isb_tab[256] __initdata; |
| 83 | static u32 rco_tab[10]; |
| 84 | static u32 ft_tab[4][256]; |
| 85 | static u32 it_tab[4][256]; |
| 86 | |
| 87 | static u32 fl_tab[4][256]; |
| 88 | static u32 il_tab[4][256]; |
| 89 | |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame^] | 90 | static inline u8 __init f_mult(u8 a, u8 b) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | { |
| 92 | u8 aa = log_tab[a], cc = aa + log_tab[b]; |
| 93 | |
| 94 | return pow_tab[cc + (cc < aa ? 1 : 0)]; |
| 95 | } |
| 96 | |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame^] | 97 | #define ff_mult(a, b) (a && b ? f_mult(a, b) : 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame^] | 99 | static void __init gen_tabs(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | { |
| 101 | u32 i, t; |
| 102 | u8 p, q; |
| 103 | |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame^] | 104 | /* |
| 105 | * log and power tables for GF(2**8) finite field with |
| 106 | * 0x011b as modular polynomial - the simplest primitive |
| 107 | * root is 0x03, used here to generate the tables |
| 108 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
| 110 | for (i = 0, p = 1; i < 256; ++i) { |
| 111 | pow_tab[i] = (u8) p; |
| 112 | log_tab[p] = (u8) i; |
| 113 | |
| 114 | p ^= (p << 1) ^ (p & 0x80 ? 0x01b : 0); |
| 115 | } |
| 116 | |
| 117 | log_tab[1] = 0; |
| 118 | |
| 119 | for (i = 0, p = 1; i < 10; ++i) { |
| 120 | rco_tab[i] = p; |
| 121 | |
| 122 | p = (p << 1) ^ (p & 0x80 ? 0x01b : 0); |
| 123 | } |
| 124 | |
| 125 | for (i = 0; i < 256; ++i) { |
| 126 | p = (i ? pow_tab[255 - log_tab[i]] : 0); |
| 127 | q = ((p >> 7) | (p << 1)) ^ ((p >> 6) | (p << 2)); |
| 128 | p ^= 0x63 ^ q ^ ((q >> 6) | (q << 2)); |
| 129 | sbx_tab[i] = p; |
| 130 | isb_tab[p] = (u8) i; |
| 131 | } |
| 132 | |
| 133 | for (i = 0; i < 256; ++i) { |
| 134 | p = sbx_tab[i]; |
| 135 | |
| 136 | t = p; |
| 137 | fl_tab[0][i] = t; |
| 138 | fl_tab[1][i] = rol32(t, 8); |
| 139 | fl_tab[2][i] = rol32(t, 16); |
| 140 | fl_tab[3][i] = rol32(t, 24); |
| 141 | |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame^] | 142 | t = ((u32) ff_mult(2, p)) | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | ((u32) p << 8) | |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame^] | 144 | ((u32) p << 16) | ((u32) ff_mult(3, p) << 24); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
| 146 | ft_tab[0][i] = t; |
| 147 | ft_tab[1][i] = rol32(t, 8); |
| 148 | ft_tab[2][i] = rol32(t, 16); |
| 149 | ft_tab[3][i] = rol32(t, 24); |
| 150 | |
| 151 | p = isb_tab[i]; |
| 152 | |
| 153 | t = p; |
| 154 | il_tab[0][i] = t; |
| 155 | il_tab[1][i] = rol32(t, 8); |
| 156 | il_tab[2][i] = rol32(t, 16); |
| 157 | il_tab[3][i] = rol32(t, 24); |
| 158 | |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame^] | 159 | t = ((u32) ff_mult(14, p)) | |
| 160 | ((u32) ff_mult(9, p) << 8) | |
| 161 | ((u32) ff_mult(13, p) << 16) | |
| 162 | ((u32) ff_mult(11, p) << 24); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
| 164 | it_tab[0][i] = t; |
| 165 | it_tab[1][i] = rol32(t, 8); |
| 166 | it_tab[2][i] = rol32(t, 16); |
| 167 | it_tab[3][i] = rol32(t, 24); |
| 168 | } |
| 169 | } |
| 170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | /* initialise the key schedule from the user supplied key */ |
| 172 | |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame^] | 173 | #define star_x(x) (((x) & 0x7f7f7f7f) << 1) ^ ((((x) & 0x80808080) >> 7) * 0x1b) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame^] | 175 | #define imix_col(y,x) do { \ |
| 176 | u = star_x(x); \ |
| 177 | v = star_x(u); \ |
| 178 | w = star_x(v); \ |
| 179 | t = w ^ (x); \ |
| 180 | (y) = u ^ v ^ w; \ |
| 181 | (y) ^= ror32(u ^ t, 8) ^ \ |
| 182 | ror32(v ^ t, 16) ^ \ |
| 183 | ror32(t, 24); \ |
| 184 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame^] | 186 | #define ls_box(x) \ |
| 187 | fl_tab[0][byte(x, 0)] ^ \ |
| 188 | fl_tab[1][byte(x, 1)] ^ \ |
| 189 | fl_tab[2][byte(x, 2)] ^ \ |
| 190 | fl_tab[3][byte(x, 3)] |
| 191 | |
| 192 | #define loop4(i) do { \ |
| 193 | t = ror32(t, 8); \ |
| 194 | t = ls_box(t) ^ rco_tab[i]; \ |
| 195 | t ^= E_KEY[4 * i]; \ |
| 196 | E_KEY[4 * i + 4] = t; \ |
| 197 | t ^= E_KEY[4 * i + 1]; \ |
| 198 | E_KEY[4 * i + 5] = t; \ |
| 199 | t ^= E_KEY[4 * i + 2]; \ |
| 200 | E_KEY[4 * i + 6] = t; \ |
| 201 | t ^= E_KEY[4 * i + 3]; \ |
| 202 | E_KEY[4 * i + 7] = t; \ |
| 203 | } while (0) |
| 204 | |
| 205 | #define loop6(i) do { \ |
| 206 | t = ror32(t, 8); \ |
| 207 | t = ls_box(t) ^ rco_tab[i]; \ |
| 208 | t ^= E_KEY[6 * i]; \ |
| 209 | E_KEY[6 * i + 6] = t; \ |
| 210 | t ^= E_KEY[6 * i + 1]; \ |
| 211 | E_KEY[6 * i + 7] = t; \ |
| 212 | t ^= E_KEY[6 * i + 2]; \ |
| 213 | E_KEY[6 * i + 8] = t; \ |
| 214 | t ^= E_KEY[6 * i + 3]; \ |
| 215 | E_KEY[6 * i + 9] = t; \ |
| 216 | t ^= E_KEY[6 * i + 4]; \ |
| 217 | E_KEY[6 * i + 10] = t; \ |
| 218 | t ^= E_KEY[6 * i + 5]; \ |
| 219 | E_KEY[6 * i + 11] = t; \ |
| 220 | } while (0) |
| 221 | |
| 222 | #define loop8(i) do { \ |
| 223 | t = ror32(t, 8); \ |
| 224 | t = ls_box(t) ^ rco_tab[i]; \ |
| 225 | t ^= E_KEY[8 * i]; \ |
| 226 | E_KEY[8 * i + 8] = t; \ |
| 227 | t ^= E_KEY[8 * i + 1]; \ |
| 228 | E_KEY[8 * i + 9] = t; \ |
| 229 | t ^= E_KEY[8 * i + 2]; \ |
| 230 | E_KEY[8 * i + 10] = t; \ |
| 231 | t ^= E_KEY[8 * i + 3]; \ |
| 232 | E_KEY[8 * i + 11] = t; \ |
| 233 | t = E_KEY[8 * i + 4] ^ ls_box(t); \ |
| 234 | E_KEY[8 * i + 12] = t; \ |
| 235 | t ^= E_KEY[8 * i + 5]; \ |
| 236 | E_KEY[8 * i + 13] = t; \ |
| 237 | t ^= E_KEY[8 * i + 6]; \ |
| 238 | E_KEY[8 * i + 14] = t; \ |
| 239 | t ^= E_KEY[8 * i + 7]; \ |
| 240 | E_KEY[8 * i + 15] = t; \ |
| 241 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 243 | static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key, |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame^] | 244 | unsigned int key_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | { |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 246 | struct aes_ctx *ctx = crypto_tfm_ctx(tfm); |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 247 | const __le32 *key = (const __le32 *)in_key; |
Herbert Xu | 560c06a | 2006-08-13 14:16:39 +1000 | [diff] [blame] | 248 | u32 *flags = &tfm->crt_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | u32 i, t, u, v, w; |
| 250 | |
Herbert Xu | 560c06a | 2006-08-13 14:16:39 +1000 | [diff] [blame] | 251 | if (key_len % 8) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN; |
| 253 | return -EINVAL; |
| 254 | } |
| 255 | |
| 256 | ctx->key_length = key_len; |
| 257 | |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 258 | E_KEY[0] = le32_to_cpu(key[0]); |
| 259 | E_KEY[1] = le32_to_cpu(key[1]); |
| 260 | E_KEY[2] = le32_to_cpu(key[2]); |
| 261 | E_KEY[3] = le32_to_cpu(key[3]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
| 263 | switch (key_len) { |
| 264 | case 16: |
| 265 | t = E_KEY[3]; |
| 266 | for (i = 0; i < 10; ++i) |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame^] | 267 | loop4(i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | break; |
| 269 | |
| 270 | case 24: |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 271 | E_KEY[4] = le32_to_cpu(key[4]); |
| 272 | t = E_KEY[5] = le32_to_cpu(key[5]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | for (i = 0; i < 8; ++i) |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame^] | 274 | loop6(i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | break; |
| 276 | |
| 277 | case 32: |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 278 | E_KEY[4] = le32_to_cpu(key[4]); |
| 279 | E_KEY[5] = le32_to_cpu(key[5]); |
| 280 | E_KEY[6] = le32_to_cpu(key[6]); |
| 281 | t = E_KEY[7] = le32_to_cpu(key[7]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | for (i = 0; i < 7; ++i) |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame^] | 283 | loop8(i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | break; |
| 285 | } |
| 286 | |
| 287 | D_KEY[0] = E_KEY[0]; |
| 288 | D_KEY[1] = E_KEY[1]; |
| 289 | D_KEY[2] = E_KEY[2]; |
| 290 | D_KEY[3] = E_KEY[3]; |
| 291 | |
| 292 | for (i = 4; i < key_len + 24; ++i) { |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame^] | 293 | imix_col(D_KEY[i], E_KEY[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | /* encrypt a block of text */ |
| 300 | |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame^] | 301 | #define f_rn(bo, bi, n, k) do { \ |
| 302 | bo[n] = ft_tab[0][byte(bi[n], 0)] ^ \ |
| 303 | ft_tab[1][byte(bi[(n + 1) & 3], 1)] ^ \ |
| 304 | ft_tab[2][byte(bi[(n + 2) & 3], 2)] ^ \ |
| 305 | ft_tab[3][byte(bi[(n + 3) & 3], 3)] ^ *(k + n); \ |
| 306 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame^] | 308 | #define f_nround(bo, bi, k) do {\ |
| 309 | f_rn(bo, bi, 0, k); \ |
| 310 | f_rn(bo, bi, 1, k); \ |
| 311 | f_rn(bo, bi, 2, k); \ |
| 312 | f_rn(bo, bi, 3, k); \ |
| 313 | k += 4; \ |
| 314 | } while (0) |
| 315 | |
| 316 | #define f_rl(bo, bi, n, k) do { \ |
| 317 | bo[n] = fl_tab[0][byte(bi[n], 0)] ^ \ |
| 318 | fl_tab[1][byte(bi[(n + 1) & 3], 1)] ^ \ |
| 319 | fl_tab[2][byte(bi[(n + 2) & 3], 2)] ^ \ |
| 320 | fl_tab[3][byte(bi[(n + 3) & 3], 3)] ^ *(k + n); \ |
| 321 | } while (0) |
| 322 | |
| 323 | #define f_lround(bo, bi, k) do {\ |
| 324 | f_rl(bo, bi, 0, k); \ |
| 325 | f_rl(bo, bi, 1, k); \ |
| 326 | f_rl(bo, bi, 2, k); \ |
| 327 | f_rl(bo, bi, 3, k); \ |
| 328 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 330 | static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | { |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 332 | const struct aes_ctx *ctx = crypto_tfm_ctx(tfm); |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 333 | const __le32 *src = (const __le32 *)in; |
| 334 | __le32 *dst = (__le32 *)out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | u32 b0[4], b1[4]; |
| 336 | const u32 *kp = E_KEY + 4; |
| 337 | |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 338 | b0[0] = le32_to_cpu(src[0]) ^ E_KEY[0]; |
| 339 | b0[1] = le32_to_cpu(src[1]) ^ E_KEY[1]; |
| 340 | b0[2] = le32_to_cpu(src[2]) ^ E_KEY[2]; |
| 341 | b0[3] = le32_to_cpu(src[3]) ^ E_KEY[3]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | |
| 343 | if (ctx->key_length > 24) { |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame^] | 344 | f_nround(b1, b0, kp); |
| 345 | f_nround(b0, b1, kp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | if (ctx->key_length > 16) { |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame^] | 349 | f_nround(b1, b0, kp); |
| 350 | f_nround(b0, b1, kp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame^] | 353 | f_nround(b1, b0, kp); |
| 354 | f_nround(b0, b1, kp); |
| 355 | f_nround(b1, b0, kp); |
| 356 | f_nround(b0, b1, kp); |
| 357 | f_nround(b1, b0, kp); |
| 358 | f_nround(b0, b1, kp); |
| 359 | f_nround(b1, b0, kp); |
| 360 | f_nround(b0, b1, kp); |
| 361 | f_nround(b1, b0, kp); |
| 362 | f_lround(b0, b1, kp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 364 | dst[0] = cpu_to_le32(b0[0]); |
| 365 | dst[1] = cpu_to_le32(b0[1]); |
| 366 | dst[2] = cpu_to_le32(b0[2]); |
| 367 | dst[3] = cpu_to_le32(b0[3]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | /* decrypt a block of text */ |
| 371 | |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame^] | 372 | #define i_rn(bo, bi, n, k) do { \ |
| 373 | bo[n] = it_tab[0][byte(bi[n], 0)] ^ \ |
| 374 | it_tab[1][byte(bi[(n + 3) & 3], 1)] ^ \ |
| 375 | it_tab[2][byte(bi[(n + 2) & 3], 2)] ^ \ |
| 376 | it_tab[3][byte(bi[(n + 1) & 3], 3)] ^ *(k + n); \ |
| 377 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame^] | 379 | #define i_nround(bo, bi, k) do {\ |
| 380 | i_rn(bo, bi, 0, k); \ |
| 381 | i_rn(bo, bi, 1, k); \ |
| 382 | i_rn(bo, bi, 2, k); \ |
| 383 | i_rn(bo, bi, 3, k); \ |
| 384 | k -= 4; \ |
| 385 | } while (0) |
| 386 | |
| 387 | #define i_rl(bo, bi, n, k) do { \ |
| 388 | bo[n] = il_tab[0][byte(bi[n], 0)] ^ \ |
| 389 | il_tab[1][byte(bi[(n + 3) & 3], 1)] ^ \ |
| 390 | il_tab[2][byte(bi[(n + 2) & 3], 2)] ^ \ |
| 391 | il_tab[3][byte(bi[(n + 1) & 3], 3)] ^ *(k + n); \ |
| 392 | } while (0) |
| 393 | |
| 394 | #define i_lround(bo, bi, k) do {\ |
| 395 | i_rl(bo, bi, 0, k); \ |
| 396 | i_rl(bo, bi, 1, k); \ |
| 397 | i_rl(bo, bi, 2, k); \ |
| 398 | i_rl(bo, bi, 3, k); \ |
| 399 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 401 | static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | { |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 403 | const struct aes_ctx *ctx = crypto_tfm_ctx(tfm); |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 404 | const __le32 *src = (const __le32 *)in; |
| 405 | __le32 *dst = (__le32 *)out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | u32 b0[4], b1[4]; |
| 407 | const int key_len = ctx->key_length; |
| 408 | const u32 *kp = D_KEY + key_len + 20; |
| 409 | |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 410 | b0[0] = le32_to_cpu(src[0]) ^ E_KEY[key_len + 24]; |
| 411 | b0[1] = le32_to_cpu(src[1]) ^ E_KEY[key_len + 25]; |
| 412 | b0[2] = le32_to_cpu(src[2]) ^ E_KEY[key_len + 26]; |
| 413 | b0[3] = le32_to_cpu(src[3]) ^ E_KEY[key_len + 27]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | |
| 415 | if (key_len > 24) { |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame^] | 416 | i_nround(b1, b0, kp); |
| 417 | i_nround(b0, b1, kp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | if (key_len > 16) { |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame^] | 421 | i_nround(b1, b0, kp); |
| 422 | i_nround(b0, b1, kp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame^] | 425 | i_nround(b1, b0, kp); |
| 426 | i_nround(b0, b1, kp); |
| 427 | i_nround(b1, b0, kp); |
| 428 | i_nround(b0, b1, kp); |
| 429 | i_nround(b1, b0, kp); |
| 430 | i_nround(b0, b1, kp); |
| 431 | i_nround(b1, b0, kp); |
| 432 | i_nround(b0, b1, kp); |
| 433 | i_nround(b1, b0, kp); |
| 434 | i_lround(b0, b1, kp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 436 | dst[0] = cpu_to_le32(b0[0]); |
| 437 | dst[1] = cpu_to_le32(b0[1]); |
| 438 | dst[2] = cpu_to_le32(b0[2]); |
| 439 | dst[3] = cpu_to_le32(b0[3]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | static struct crypto_alg aes_alg = { |
| 443 | .cra_name = "aes", |
Herbert Xu | c8a19c9 | 2005-11-05 18:06:26 +1100 | [diff] [blame] | 444 | .cra_driver_name = "aes-generic", |
| 445 | .cra_priority = 100, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | .cra_flags = CRYPTO_ALG_TYPE_CIPHER, |
| 447 | .cra_blocksize = AES_BLOCK_SIZE, |
| 448 | .cra_ctxsize = sizeof(struct aes_ctx), |
Herbert Xu | a429d26 | 2006-01-07 16:38:15 +1100 | [diff] [blame] | 449 | .cra_alignmask = 3, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | .cra_module = THIS_MODULE, |
| 451 | .cra_list = LIST_HEAD_INIT(aes_alg.cra_list), |
| 452 | .cra_u = { |
| 453 | .cipher = { |
| 454 | .cia_min_keysize = AES_MIN_KEY_SIZE, |
| 455 | .cia_max_keysize = AES_MAX_KEY_SIZE, |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame^] | 456 | .cia_setkey = aes_set_key, |
| 457 | .cia_encrypt = aes_encrypt, |
| 458 | .cia_decrypt = aes_decrypt |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | }; |
| 462 | |
| 463 | static int __init aes_init(void) |
| 464 | { |
| 465 | gen_tabs(); |
| 466 | return crypto_register_alg(&aes_alg); |
| 467 | } |
| 468 | |
| 469 | static void __exit aes_fini(void) |
| 470 | { |
| 471 | crypto_unregister_alg(&aes_alg); |
| 472 | } |
| 473 | |
| 474 | module_init(aes_init); |
| 475 | module_exit(aes_fini); |
| 476 | |
| 477 | MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm"); |
| 478 | MODULE_LICENSE("Dual BSD/GPL"); |
Sebastian Siewior | f8246af | 2007-10-05 16:52:01 +0800 | [diff] [blame] | 479 | MODULE_ALIAS("aes"); |