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 | |
Sebastian Siewior | 89e1265 | 2007-10-17 23:18:57 +0800 | [diff] [blame] | 50 | #include <crypto/aes.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #include <linux/module.h> |
| 52 | #include <linux/init.h> |
| 53 | #include <linux/types.h> |
| 54 | #include <linux/errno.h> |
| 55 | #include <linux/crypto.h> |
| 56 | #include <asm/byteorder.h> |
| 57 | |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame] | 58 | static inline u8 byte(const u32 x, const unsigned n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | { |
| 60 | return x >> (n << 3); |
| 61 | } |
| 62 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | static u8 pow_tab[256] __initdata; |
| 64 | static u8 log_tab[256] __initdata; |
| 65 | static u8 sbx_tab[256] __initdata; |
| 66 | static u8 isb_tab[256] __initdata; |
| 67 | static u32 rco_tab[10]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Sebastian Siewior | 96e82e45 | 2007-11-08 21:20:30 +0800 | [diff] [blame] | 69 | u32 crypto_ft_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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame] | 79 | static inline u8 __init f_mult(u8 a, u8 b) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | { |
| 81 | u8 aa = log_tab[a], cc = aa + log_tab[b]; |
| 82 | |
| 83 | return pow_tab[cc + (cc < aa ? 1 : 0)]; |
| 84 | } |
| 85 | |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame] | 86 | #define ff_mult(a, b) (a && b ? f_mult(a, b) : 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame] | 88 | static void __init gen_tabs(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | { |
| 90 | u32 i, t; |
| 91 | u8 p, q; |
| 92 | |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame] | 93 | /* |
| 94 | * log and power tables for GF(2**8) finite field with |
| 95 | * 0x011b as modular polynomial - the simplest primitive |
| 96 | * root is 0x03, used here to generate the tables |
| 97 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
| 99 | for (i = 0, p = 1; i < 256; ++i) { |
| 100 | pow_tab[i] = (u8) p; |
| 101 | log_tab[p] = (u8) i; |
| 102 | |
| 103 | p ^= (p << 1) ^ (p & 0x80 ? 0x01b : 0); |
| 104 | } |
| 105 | |
| 106 | log_tab[1] = 0; |
| 107 | |
| 108 | for (i = 0, p = 1; i < 10; ++i) { |
| 109 | rco_tab[i] = p; |
| 110 | |
| 111 | p = (p << 1) ^ (p & 0x80 ? 0x01b : 0); |
| 112 | } |
| 113 | |
| 114 | for (i = 0; i < 256; ++i) { |
| 115 | p = (i ? pow_tab[255 - log_tab[i]] : 0); |
| 116 | q = ((p >> 7) | (p << 1)) ^ ((p >> 6) | (p << 2)); |
| 117 | p ^= 0x63 ^ q ^ ((q >> 6) | (q << 2)); |
| 118 | sbx_tab[i] = p; |
| 119 | isb_tab[p] = (u8) i; |
| 120 | } |
| 121 | |
| 122 | for (i = 0; i < 256; ++i) { |
| 123 | p = sbx_tab[i]; |
| 124 | |
| 125 | t = p; |
Sebastian Siewior | 96e82e45 | 2007-11-08 21:20:30 +0800 | [diff] [blame] | 126 | crypto_fl_tab[0][i] = t; |
| 127 | crypto_fl_tab[1][i] = rol32(t, 8); |
| 128 | crypto_fl_tab[2][i] = rol32(t, 16); |
| 129 | crypto_fl_tab[3][i] = rol32(t, 24); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame] | 131 | t = ((u32) ff_mult(2, p)) | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | ((u32) p << 8) | |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame] | 133 | ((u32) p << 16) | ((u32) ff_mult(3, p) << 24); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
Sebastian Siewior | 96e82e45 | 2007-11-08 21:20:30 +0800 | [diff] [blame] | 135 | crypto_ft_tab[0][i] = t; |
| 136 | crypto_ft_tab[1][i] = rol32(t, 8); |
| 137 | crypto_ft_tab[2][i] = rol32(t, 16); |
| 138 | crypto_ft_tab[3][i] = rol32(t, 24); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
| 140 | p = isb_tab[i]; |
| 141 | |
| 142 | t = p; |
Sebastian Siewior | 96e82e45 | 2007-11-08 21:20:30 +0800 | [diff] [blame] | 143 | crypto_il_tab[0][i] = t; |
| 144 | crypto_il_tab[1][i] = rol32(t, 8); |
| 145 | crypto_il_tab[2][i] = rol32(t, 16); |
| 146 | crypto_il_tab[3][i] = rol32(t, 24); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame] | 148 | t = ((u32) ff_mult(14, p)) | |
| 149 | ((u32) ff_mult(9, p) << 8) | |
| 150 | ((u32) ff_mult(13, p) << 16) | |
| 151 | ((u32) ff_mult(11, p) << 24); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
Sebastian Siewior | 96e82e45 | 2007-11-08 21:20:30 +0800 | [diff] [blame] | 153 | crypto_it_tab[0][i] = t; |
| 154 | crypto_it_tab[1][i] = rol32(t, 8); |
| 155 | crypto_it_tab[2][i] = rol32(t, 16); |
| 156 | crypto_it_tab[3][i] = rol32(t, 24); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | /* initialise the key schedule from the user supplied key */ |
| 161 | |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame] | 162 | #define star_x(x) (((x) & 0x7f7f7f7f) << 1) ^ ((((x) & 0x80808080) >> 7) * 0x1b) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame] | 164 | #define imix_col(y,x) do { \ |
| 165 | u = star_x(x); \ |
| 166 | v = star_x(u); \ |
| 167 | w = star_x(v); \ |
| 168 | t = w ^ (x); \ |
| 169 | (y) = u ^ v ^ w; \ |
| 170 | (y) ^= ror32(u ^ t, 8) ^ \ |
| 171 | ror32(v ^ t, 16) ^ \ |
| 172 | ror32(t, 24); \ |
| 173 | } while (0) |
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 ls_box(x) \ |
Sebastian Siewior | 96e82e45 | 2007-11-08 21:20:30 +0800 | [diff] [blame] | 176 | crypto_fl_tab[0][byte(x, 0)] ^ \ |
| 177 | crypto_fl_tab[1][byte(x, 1)] ^ \ |
| 178 | crypto_fl_tab[2][byte(x, 2)] ^ \ |
| 179 | crypto_fl_tab[3][byte(x, 3)] |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame] | 180 | |
| 181 | #define loop4(i) do { \ |
| 182 | t = ror32(t, 8); \ |
| 183 | t = ls_box(t) ^ rco_tab[i]; \ |
Sebastian Siewior | 96e82e45 | 2007-11-08 21:20:30 +0800 | [diff] [blame] | 184 | t ^= ctx->key_enc[4 * i]; \ |
| 185 | ctx->key_enc[4 * i + 4] = t; \ |
| 186 | t ^= ctx->key_enc[4 * i + 1]; \ |
| 187 | ctx->key_enc[4 * i + 5] = t; \ |
| 188 | t ^= ctx->key_enc[4 * i + 2]; \ |
| 189 | ctx->key_enc[4 * i + 6] = t; \ |
| 190 | t ^= ctx->key_enc[4 * i + 3]; \ |
| 191 | ctx->key_enc[4 * i + 7] = t; \ |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame] | 192 | } while (0) |
| 193 | |
| 194 | #define loop6(i) do { \ |
| 195 | t = ror32(t, 8); \ |
| 196 | t = ls_box(t) ^ rco_tab[i]; \ |
Sebastian Siewior | 96e82e45 | 2007-11-08 21:20:30 +0800 | [diff] [blame] | 197 | t ^= ctx->key_enc[6 * i]; \ |
| 198 | ctx->key_enc[6 * i + 6] = t; \ |
| 199 | t ^= ctx->key_enc[6 * i + 1]; \ |
| 200 | ctx->key_enc[6 * i + 7] = t; \ |
| 201 | t ^= ctx->key_enc[6 * i + 2]; \ |
| 202 | ctx->key_enc[6 * i + 8] = t; \ |
| 203 | t ^= ctx->key_enc[6 * i + 3]; \ |
| 204 | ctx->key_enc[6 * i + 9] = t; \ |
| 205 | t ^= ctx->key_enc[6 * i + 4]; \ |
| 206 | ctx->key_enc[6 * i + 10] = t; \ |
| 207 | t ^= ctx->key_enc[6 * i + 5]; \ |
| 208 | ctx->key_enc[6 * i + 11] = t; \ |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame] | 209 | } while (0) |
| 210 | |
| 211 | #define loop8(i) do { \ |
| 212 | t = ror32(t, 8); \ |
| 213 | t = ls_box(t) ^ rco_tab[i]; \ |
Sebastian Siewior | 96e82e45 | 2007-11-08 21:20:30 +0800 | [diff] [blame] | 214 | t ^= ctx->key_enc[8 * i]; \ |
| 215 | ctx->key_enc[8 * i + 8] = t; \ |
| 216 | t ^= ctx->key_enc[8 * i + 1]; \ |
| 217 | ctx->key_enc[8 * i + 9] = t; \ |
| 218 | t ^= ctx->key_enc[8 * i + 2]; \ |
| 219 | ctx->key_enc[8 * i + 10] = t; \ |
| 220 | t ^= ctx->key_enc[8 * i + 3]; \ |
| 221 | ctx->key_enc[8 * i + 11] = t; \ |
| 222 | t = ctx->key_enc[8 * i + 4] ^ ls_box(t); \ |
| 223 | ctx->key_enc[8 * i + 12] = t; \ |
| 224 | t ^= ctx->key_enc[8 * i + 5]; \ |
| 225 | ctx->key_enc[8 * i + 13] = t; \ |
| 226 | t ^= ctx->key_enc[8 * i + 6]; \ |
| 227 | ctx->key_enc[8 * i + 14] = t; \ |
| 228 | t ^= ctx->key_enc[8 * i + 7]; \ |
| 229 | ctx->key_enc[8 * i + 15] = t; \ |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame] | 230 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | |
Sebastian Siewior | 5427663 | 2008-04-01 20:58:51 +0800 | [diff] [blame] | 232 | /** |
| 233 | * crypto_aes_expand_key - Expands the AES key as described in FIPS-197 |
| 234 | * @ctx: The location where the computed key will be stored. |
| 235 | * @in_key: The supplied key. |
| 236 | * @key_len: The length of the supplied key. |
| 237 | * |
| 238 | * Returns 0 on success. The function fails only if an invalid key size (or |
| 239 | * pointer) is supplied. |
| 240 | * The expanded key size is 240 bytes (max of 14 rounds with a unique 16 bytes |
| 241 | * key schedule plus a 16 bytes key which is used before the first round). |
| 242 | * The decryption key is prepared for the "Equivalent Inverse Cipher" as |
| 243 | * described in FIPS-197. The first slot (16 bytes) of each key (enc or dec) is |
| 244 | * for the initial combination, the second slot for the first round and so on. |
| 245 | */ |
| 246 | int crypto_aes_expand_key(struct crypto_aes_ctx *ctx, const u8 *in_key, |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame] | 247 | unsigned int key_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | { |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 249 | const __le32 *key = (const __le32 *)in_key; |
Sebastian Siewior | 96e82e45 | 2007-11-08 21:20:30 +0800 | [diff] [blame] | 250 | u32 i, t, u, v, w, j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
Sebastian Siewior | 5427663 | 2008-04-01 20:58:51 +0800 | [diff] [blame] | 252 | if (key_len != AES_KEYSIZE_128 && key_len != AES_KEYSIZE_192 && |
| 253 | key_len != AES_KEYSIZE_256) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
| 256 | ctx->key_length = key_len; |
| 257 | |
Sebastian Siewior | 96e82e45 | 2007-11-08 21:20:30 +0800 | [diff] [blame] | 258 | ctx->key_dec[key_len + 24] = ctx->key_enc[0] = le32_to_cpu(key[0]); |
| 259 | ctx->key_dec[key_len + 25] = ctx->key_enc[1] = le32_to_cpu(key[1]); |
| 260 | ctx->key_dec[key_len + 26] = ctx->key_enc[2] = le32_to_cpu(key[2]); |
| 261 | ctx->key_dec[key_len + 27] = ctx->key_enc[3] = le32_to_cpu(key[3]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
| 263 | switch (key_len) { |
Sebastian Siewior | 5427663 | 2008-04-01 20:58:51 +0800 | [diff] [blame] | 264 | case AES_KEYSIZE_128: |
Sebastian Siewior | 96e82e45 | 2007-11-08 21:20:30 +0800 | [diff] [blame] | 265 | t = ctx->key_enc[3]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Sebastian Siewior | 5427663 | 2008-04-01 20:58:51 +0800 | [diff] [blame] | 270 | case AES_KEYSIZE_192: |
Sebastian Siewior | 96e82e45 | 2007-11-08 21:20:30 +0800 | [diff] [blame] | 271 | ctx->key_enc[4] = le32_to_cpu(key[4]); |
| 272 | t = ctx->key_enc[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 | |
Sebastian Siewior | 5427663 | 2008-04-01 20:58:51 +0800 | [diff] [blame] | 277 | case AES_KEYSIZE_256: |
Sebastian Siewior | 96e82e45 | 2007-11-08 21:20:30 +0800 | [diff] [blame] | 278 | ctx->key_enc[4] = le32_to_cpu(key[4]); |
| 279 | ctx->key_enc[5] = le32_to_cpu(key[5]); |
| 280 | ctx->key_enc[6] = le32_to_cpu(key[6]); |
| 281 | t = ctx->key_enc[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 | |
Sebastian Siewior | 96e82e45 | 2007-11-08 21:20:30 +0800 | [diff] [blame] | 287 | ctx->key_dec[0] = ctx->key_enc[key_len + 24]; |
| 288 | ctx->key_dec[1] = ctx->key_enc[key_len + 25]; |
| 289 | ctx->key_dec[2] = ctx->key_enc[key_len + 26]; |
| 290 | ctx->key_dec[3] = ctx->key_enc[key_len + 27]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
| 292 | for (i = 4; i < key_len + 24; ++i) { |
Sebastian Siewior | 96e82e45 | 2007-11-08 21:20:30 +0800 | [diff] [blame] | 293 | j = key_len + 24 - (i & ~3) + (i & 3); |
| 294 | imix_col(ctx->key_dec[j], ctx->key_enc[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | return 0; |
| 297 | } |
Sebastian Siewior | 5427663 | 2008-04-01 20:58:51 +0800 | [diff] [blame] | 298 | EXPORT_SYMBOL_GPL(crypto_aes_expand_key); |
| 299 | |
| 300 | /** |
| 301 | * crypto_aes_set_key - Set the AES key. |
| 302 | * @tfm: The %crypto_tfm that is used in the context. |
| 303 | * @in_key: The input key. |
| 304 | * @key_len: The size of the key. |
| 305 | * |
| 306 | * Returns 0 on success, on failure the %CRYPTO_TFM_RES_BAD_KEY_LEN flag in tfm |
| 307 | * is set. The function uses crypto_aes_expand_key() to expand the key. |
| 308 | * &crypto_aes_ctx _must_ be the private data embedded in @tfm which is |
| 309 | * retrieved with crypto_tfm_ctx(). |
| 310 | */ |
| 311 | int crypto_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key, |
| 312 | unsigned int key_len) |
| 313 | { |
| 314 | struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm); |
| 315 | u32 *flags = &tfm->crt_flags; |
| 316 | int ret; |
| 317 | |
| 318 | ret = crypto_aes_expand_key(ctx, in_key, key_len); |
| 319 | if (!ret) |
| 320 | return 0; |
| 321 | |
| 322 | *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN; |
| 323 | return -EINVAL; |
| 324 | } |
Sebastian Siewior | 96e82e45 | 2007-11-08 21:20:30 +0800 | [diff] [blame] | 325 | EXPORT_SYMBOL_GPL(crypto_aes_set_key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | |
| 327 | /* encrypt a block of text */ |
| 328 | |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame] | 329 | #define f_rn(bo, bi, n, k) do { \ |
Sebastian Siewior | 96e82e45 | 2007-11-08 21:20:30 +0800 | [diff] [blame] | 330 | bo[n] = crypto_ft_tab[0][byte(bi[n], 0)] ^ \ |
| 331 | crypto_ft_tab[1][byte(bi[(n + 1) & 3], 1)] ^ \ |
| 332 | crypto_ft_tab[2][byte(bi[(n + 2) & 3], 2)] ^ \ |
| 333 | crypto_ft_tab[3][byte(bi[(n + 3) & 3], 3)] ^ *(k + n); \ |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame] | 334 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame] | 336 | #define f_nround(bo, bi, k) do {\ |
| 337 | f_rn(bo, bi, 0, k); \ |
| 338 | f_rn(bo, bi, 1, k); \ |
| 339 | f_rn(bo, bi, 2, k); \ |
| 340 | f_rn(bo, bi, 3, k); \ |
| 341 | k += 4; \ |
| 342 | } while (0) |
| 343 | |
| 344 | #define f_rl(bo, bi, n, k) do { \ |
Sebastian Siewior | 96e82e45 | 2007-11-08 21:20:30 +0800 | [diff] [blame] | 345 | bo[n] = crypto_fl_tab[0][byte(bi[n], 0)] ^ \ |
| 346 | crypto_fl_tab[1][byte(bi[(n + 1) & 3], 1)] ^ \ |
| 347 | crypto_fl_tab[2][byte(bi[(n + 2) & 3], 2)] ^ \ |
| 348 | crypto_fl_tab[3][byte(bi[(n + 3) & 3], 3)] ^ *(k + n); \ |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame] | 349 | } while (0) |
| 350 | |
| 351 | #define f_lround(bo, bi, k) do {\ |
| 352 | f_rl(bo, bi, 0, k); \ |
| 353 | f_rl(bo, bi, 1, k); \ |
| 354 | f_rl(bo, bi, 2, k); \ |
| 355 | f_rl(bo, bi, 3, k); \ |
| 356 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 358 | 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] | 359 | { |
Sebastian Siewior | 96e82e45 | 2007-11-08 21:20:30 +0800 | [diff] [blame] | 360 | const struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm); |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 361 | const __le32 *src = (const __le32 *)in; |
| 362 | __le32 *dst = (__le32 *)out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | u32 b0[4], b1[4]; |
Sebastian Siewior | 96e82e45 | 2007-11-08 21:20:30 +0800 | [diff] [blame] | 364 | const u32 *kp = ctx->key_enc + 4; |
| 365 | const int key_len = ctx->key_length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | |
Sebastian Siewior | 96e82e45 | 2007-11-08 21:20:30 +0800 | [diff] [blame] | 367 | b0[0] = le32_to_cpu(src[0]) ^ ctx->key_enc[0]; |
| 368 | b0[1] = le32_to_cpu(src[1]) ^ ctx->key_enc[1]; |
| 369 | b0[2] = le32_to_cpu(src[2]) ^ ctx->key_enc[2]; |
| 370 | b0[3] = le32_to_cpu(src[3]) ^ ctx->key_enc[3]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | |
Sebastian Siewior | 96e82e45 | 2007-11-08 21:20:30 +0800 | [diff] [blame] | 372 | if (key_len > 24) { |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame] | 373 | f_nround(b1, b0, kp); |
| 374 | f_nround(b0, b1, kp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Sebastian Siewior | 96e82e45 | 2007-11-08 21:20:30 +0800 | [diff] [blame] | 377 | if (key_len > 16) { |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame] | 378 | f_nround(b1, b0, kp); |
| 379 | f_nround(b0, b1, kp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame] | 382 | f_nround(b1, b0, kp); |
| 383 | f_nround(b0, b1, kp); |
| 384 | f_nround(b1, b0, kp); |
| 385 | f_nround(b0, b1, kp); |
| 386 | f_nround(b1, b0, kp); |
| 387 | f_nround(b0, b1, kp); |
| 388 | f_nround(b1, b0, kp); |
| 389 | f_nround(b0, b1, kp); |
| 390 | f_nround(b1, b0, kp); |
| 391 | f_lround(b0, b1, kp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 393 | dst[0] = cpu_to_le32(b0[0]); |
| 394 | dst[1] = cpu_to_le32(b0[1]); |
| 395 | dst[2] = cpu_to_le32(b0[2]); |
| 396 | dst[3] = cpu_to_le32(b0[3]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | /* decrypt a block of text */ |
| 400 | |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame] | 401 | #define i_rn(bo, bi, n, k) do { \ |
Sebastian Siewior | 96e82e45 | 2007-11-08 21:20:30 +0800 | [diff] [blame] | 402 | bo[n] = crypto_it_tab[0][byte(bi[n], 0)] ^ \ |
| 403 | crypto_it_tab[1][byte(bi[(n + 3) & 3], 1)] ^ \ |
| 404 | crypto_it_tab[2][byte(bi[(n + 2) & 3], 2)] ^ \ |
| 405 | crypto_it_tab[3][byte(bi[(n + 1) & 3], 3)] ^ *(k + n); \ |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame] | 406 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame] | 408 | #define i_nround(bo, bi, k) do {\ |
| 409 | i_rn(bo, bi, 0, k); \ |
| 410 | i_rn(bo, bi, 1, k); \ |
| 411 | i_rn(bo, bi, 2, k); \ |
| 412 | i_rn(bo, bi, 3, k); \ |
Sebastian Siewior | 96e82e45 | 2007-11-08 21:20:30 +0800 | [diff] [blame] | 413 | k += 4; \ |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame] | 414 | } while (0) |
| 415 | |
| 416 | #define i_rl(bo, bi, n, k) do { \ |
Sebastian Siewior | 96e82e45 | 2007-11-08 21:20:30 +0800 | [diff] [blame] | 417 | bo[n] = crypto_il_tab[0][byte(bi[n], 0)] ^ \ |
| 418 | crypto_il_tab[1][byte(bi[(n + 3) & 3], 1)] ^ \ |
| 419 | crypto_il_tab[2][byte(bi[(n + 2) & 3], 2)] ^ \ |
| 420 | crypto_il_tab[3][byte(bi[(n + 1) & 3], 3)] ^ *(k + n); \ |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame] | 421 | } while (0) |
| 422 | |
| 423 | #define i_lround(bo, bi, k) do {\ |
| 424 | i_rl(bo, bi, 0, k); \ |
| 425 | i_rl(bo, bi, 1, k); \ |
| 426 | i_rl(bo, bi, 2, k); \ |
| 427 | i_rl(bo, bi, 3, k); \ |
| 428 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 430 | 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] | 431 | { |
Sebastian Siewior | 96e82e45 | 2007-11-08 21:20:30 +0800 | [diff] [blame] | 432 | const struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm); |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 433 | const __le32 *src = (const __le32 *)in; |
| 434 | __le32 *dst = (__le32 *)out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | u32 b0[4], b1[4]; |
| 436 | const int key_len = ctx->key_length; |
Sebastian Siewior | 96e82e45 | 2007-11-08 21:20:30 +0800 | [diff] [blame] | 437 | const u32 *kp = ctx->key_dec + 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | |
Sebastian Siewior | 96e82e45 | 2007-11-08 21:20:30 +0800 | [diff] [blame] | 439 | b0[0] = le32_to_cpu(src[0]) ^ ctx->key_dec[0]; |
| 440 | b0[1] = le32_to_cpu(src[1]) ^ ctx->key_dec[1]; |
| 441 | b0[2] = le32_to_cpu(src[2]) ^ ctx->key_dec[2]; |
| 442 | b0[3] = le32_to_cpu(src[3]) ^ ctx->key_dec[3]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | |
| 444 | if (key_len > 24) { |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame] | 445 | i_nround(b1, b0, kp); |
| 446 | i_nround(b0, b1, kp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | if (key_len > 16) { |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame] | 450 | i_nround(b1, b0, kp); |
| 451 | i_nround(b0, b1, kp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame] | 454 | i_nround(b1, b0, kp); |
| 455 | i_nround(b0, b1, kp); |
| 456 | i_nround(b1, b0, kp); |
| 457 | i_nround(b0, b1, kp); |
| 458 | i_nround(b1, b0, kp); |
| 459 | i_nround(b0, b1, kp); |
| 460 | i_nround(b1, b0, kp); |
| 461 | i_nround(b0, b1, kp); |
| 462 | i_nround(b1, b0, kp); |
| 463 | i_lround(b0, b1, kp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 465 | dst[0] = cpu_to_le32(b0[0]); |
| 466 | dst[1] = cpu_to_le32(b0[1]); |
| 467 | dst[2] = cpu_to_le32(b0[2]); |
| 468 | dst[3] = cpu_to_le32(b0[3]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | } |
| 470 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | static struct crypto_alg aes_alg = { |
| 472 | .cra_name = "aes", |
Herbert Xu | c8a19c9 | 2005-11-05 18:06:26 +1100 | [diff] [blame] | 473 | .cra_driver_name = "aes-generic", |
| 474 | .cra_priority = 100, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | .cra_flags = CRYPTO_ALG_TYPE_CIPHER, |
| 476 | .cra_blocksize = AES_BLOCK_SIZE, |
Sebastian Siewior | 96e82e45 | 2007-11-08 21:20:30 +0800 | [diff] [blame] | 477 | .cra_ctxsize = sizeof(struct crypto_aes_ctx), |
Herbert Xu | a429d26 | 2006-01-07 16:38:15 +1100 | [diff] [blame] | 478 | .cra_alignmask = 3, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | .cra_module = THIS_MODULE, |
| 480 | .cra_list = LIST_HEAD_INIT(aes_alg.cra_list), |
| 481 | .cra_u = { |
| 482 | .cipher = { |
| 483 | .cia_min_keysize = AES_MIN_KEY_SIZE, |
| 484 | .cia_max_keysize = AES_MAX_KEY_SIZE, |
Sebastian Siewior | 96e82e45 | 2007-11-08 21:20:30 +0800 | [diff] [blame] | 485 | .cia_setkey = crypto_aes_set_key, |
Sebastian Siewior | be5fb27 | 2007-11-08 20:39:26 +0800 | [diff] [blame] | 486 | .cia_encrypt = aes_encrypt, |
| 487 | .cia_decrypt = aes_decrypt |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | }; |
| 491 | |
| 492 | static int __init aes_init(void) |
| 493 | { |
| 494 | gen_tabs(); |
| 495 | return crypto_register_alg(&aes_alg); |
| 496 | } |
| 497 | |
| 498 | static void __exit aes_fini(void) |
| 499 | { |
| 500 | crypto_unregister_alg(&aes_alg); |
| 501 | } |
| 502 | |
| 503 | module_init(aes_init); |
| 504 | module_exit(aes_fini); |
| 505 | |
| 506 | MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm"); |
| 507 | MODULE_LICENSE("Dual BSD/GPL"); |
Sebastian Siewior | f8246af | 2007-10-05 16:52:01 +0800 | [diff] [blame] | 508 | MODULE_ALIAS("aes"); |