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 55 #include <crypto/aes.h> 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 63 /* 64 * #define byte(x, nr) ((unsigned char)((x) >> (nr*8))) 65 */ 66 static inline u8 67 byte(const u32 x, const unsigned n) 68 { 69 return x >> (n << 3); 70 } 71 72 struct aes_ctx { 73 int key_length; 74 u32 buf[120]; 75 }; 76 77 #define E_KEY (&ctx->buf[0]) 78 #define D_KEY (&ctx->buf[60]) 79 80 static u8 pow_tab[256] __initdata; 81 static u8 log_tab[256] __initdata; 82 static u8 sbx_tab[256] __initdata; 83 static u8 isb_tab[256] __initdata; 84 static u32 rco_tab[10]; 85 static u32 ft_tab[4][256]; 86 static u32 it_tab[4][256]; 87 88 static u32 fl_tab[4][256]; 89 static u32 il_tab[4][256]; 90 91 static inline u8 __init 92 f_mult (u8 a, u8 b) 93 { 94 u8 aa = log_tab[a], cc = aa + log_tab[b]; 95 96 return pow_tab[cc + (cc < aa ? 1 : 0)]; 97 } 98 99 #define ff_mult(a,b) (a && b ? f_mult(a, b) : 0) 100 101 #define f_rn(bo, bi, n, k) \ 102 bo[n] = ft_tab[0][byte(bi[n],0)] ^ \ 103 ft_tab[1][byte(bi[(n + 1) & 3],1)] ^ \ 104 ft_tab[2][byte(bi[(n + 2) & 3],2)] ^ \ 105 ft_tab[3][byte(bi[(n + 3) & 3],3)] ^ *(k + n) 106 107 #define i_rn(bo, bi, n, k) \ 108 bo[n] = it_tab[0][byte(bi[n],0)] ^ \ 109 it_tab[1][byte(bi[(n + 3) & 3],1)] ^ \ 110 it_tab[2][byte(bi[(n + 2) & 3],2)] ^ \ 111 it_tab[3][byte(bi[(n + 1) & 3],3)] ^ *(k + n) 112 113 #define ls_box(x) \ 114 ( fl_tab[0][byte(x, 0)] ^ \ 115 fl_tab[1][byte(x, 1)] ^ \ 116 fl_tab[2][byte(x, 2)] ^ \ 117 fl_tab[3][byte(x, 3)] ) 118 119 #define f_rl(bo, bi, n, k) \ 120 bo[n] = fl_tab[0][byte(bi[n],0)] ^ \ 121 fl_tab[1][byte(bi[(n + 1) & 3],1)] ^ \ 122 fl_tab[2][byte(bi[(n + 2) & 3],2)] ^ \ 123 fl_tab[3][byte(bi[(n + 3) & 3],3)] ^ *(k + n) 124 125 #define i_rl(bo, bi, n, k) \ 126 bo[n] = il_tab[0][byte(bi[n],0)] ^ \ 127 il_tab[1][byte(bi[(n + 3) & 3],1)] ^ \ 128 il_tab[2][byte(bi[(n + 2) & 3],2)] ^ \ 129 il_tab[3][byte(bi[(n + 1) & 3],3)] ^ *(k + n) 130 131 static void __init 132 gen_tabs (void) 133 { 134 u32 i, t; 135 u8 p, q; 136 137 /* log and power tables for GF(2**8) finite field with 138 0x011b as modular polynomial - the simplest primitive 139 root is 0x03, used here to generate the tables */ 140 141 for (i = 0, p = 1; i < 256; ++i) { 142 pow_tab[i] = (u8) p; 143 log_tab[p] = (u8) i; 144 145 p ^= (p << 1) ^ (p & 0x80 ? 0x01b : 0); 146 } 147 148 log_tab[1] = 0; 149 150 for (i = 0, p = 1; i < 10; ++i) { 151 rco_tab[i] = p; 152 153 p = (p << 1) ^ (p & 0x80 ? 0x01b : 0); 154 } 155 156 for (i = 0; i < 256; ++i) { 157 p = (i ? pow_tab[255 - log_tab[i]] : 0); 158 q = ((p >> 7) | (p << 1)) ^ ((p >> 6) | (p << 2)); 159 p ^= 0x63 ^ q ^ ((q >> 6) | (q << 2)); 160 sbx_tab[i] = p; 161 isb_tab[p] = (u8) i; 162 } 163 164 for (i = 0; i < 256; ++i) { 165 p = sbx_tab[i]; 166 167 t = p; 168 fl_tab[0][i] = t; 169 fl_tab[1][i] = rol32(t, 8); 170 fl_tab[2][i] = rol32(t, 16); 171 fl_tab[3][i] = rol32(t, 24); 172 173 t = ((u32) ff_mult (2, p)) | 174 ((u32) p << 8) | 175 ((u32) p << 16) | ((u32) ff_mult (3, p) << 24); 176 177 ft_tab[0][i] = t; 178 ft_tab[1][i] = rol32(t, 8); 179 ft_tab[2][i] = rol32(t, 16); 180 ft_tab[3][i] = rol32(t, 24); 181 182 p = isb_tab[i]; 183 184 t = p; 185 il_tab[0][i] = t; 186 il_tab[1][i] = rol32(t, 8); 187 il_tab[2][i] = rol32(t, 16); 188 il_tab[3][i] = rol32(t, 24); 189 190 t = ((u32) ff_mult (14, p)) | 191 ((u32) ff_mult (9, p) << 8) | 192 ((u32) ff_mult (13, p) << 16) | 193 ((u32) ff_mult (11, p) << 24); 194 195 it_tab[0][i] = t; 196 it_tab[1][i] = rol32(t, 8); 197 it_tab[2][i] = rol32(t, 16); 198 it_tab[3][i] = rol32(t, 24); 199 } 200 } 201 202 #define star_x(x) (((x) & 0x7f7f7f7f) << 1) ^ ((((x) & 0x80808080) >> 7) * 0x1b) 203 204 #define imix_col(y,x) \ 205 u = star_x(x); \ 206 v = star_x(u); \ 207 w = star_x(v); \ 208 t = w ^ (x); \ 209 (y) = u ^ v ^ w; \ 210 (y) ^= ror32(u ^ t, 8) ^ \ 211 ror32(v ^ t, 16) ^ \ 212 ror32(t,24) 213 214 /* initialise the key schedule from the user supplied key */ 215 216 #define loop4(i) \ 217 { t = ror32(t, 8); t = ls_box(t) ^ rco_tab[i]; \ 218 t ^= E_KEY[4 * i]; E_KEY[4 * i + 4] = t; \ 219 t ^= E_KEY[4 * i + 1]; E_KEY[4 * i + 5] = t; \ 220 t ^= E_KEY[4 * i + 2]; E_KEY[4 * i + 6] = t; \ 221 t ^= E_KEY[4 * i + 3]; E_KEY[4 * i + 7] = t; \ 222 } 223 224 #define loop6(i) \ 225 { t = ror32(t, 8); t = ls_box(t) ^ rco_tab[i]; \ 226 t ^= E_KEY[6 * i]; E_KEY[6 * i + 6] = t; \ 227 t ^= E_KEY[6 * i + 1]; E_KEY[6 * i + 7] = t; \ 228 t ^= E_KEY[6 * i + 2]; E_KEY[6 * i + 8] = t; \ 229 t ^= E_KEY[6 * i + 3]; E_KEY[6 * i + 9] = t; \ 230 t ^= E_KEY[6 * i + 4]; E_KEY[6 * i + 10] = t; \ 231 t ^= E_KEY[6 * i + 5]; E_KEY[6 * i + 11] = t; \ 232 } 233 234 #define loop8(i) \ 235 { t = ror32(t, 8); ; t = ls_box(t) ^ rco_tab[i]; \ 236 t ^= E_KEY[8 * i]; E_KEY[8 * i + 8] = t; \ 237 t ^= E_KEY[8 * i + 1]; E_KEY[8 * i + 9] = t; \ 238 t ^= E_KEY[8 * i + 2]; E_KEY[8 * i + 10] = t; \ 239 t ^= E_KEY[8 * i + 3]; E_KEY[8 * i + 11] = t; \ 240 t = E_KEY[8 * i + 4] ^ ls_box(t); \ 241 E_KEY[8 * i + 12] = t; \ 242 t ^= E_KEY[8 * i + 5]; E_KEY[8 * i + 13] = t; \ 243 t ^= E_KEY[8 * i + 6]; E_KEY[8 * i + 14] = t; \ 244 t ^= E_KEY[8 * i + 7]; E_KEY[8 * i + 15] = t; \ 245 } 246 247 static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key, 248 unsigned int key_len) 249 { 250 struct aes_ctx *ctx = crypto_tfm_ctx(tfm); 251 const __le32 *key = (const __le32 *)in_key; 252 u32 *flags = &tfm->crt_flags; 253 u32 i, t, u, v, w; 254 255 if (key_len % 8) { 256 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN; 257 return -EINVAL; 258 } 259 260 ctx->key_length = key_len; 261 262 E_KEY[0] = le32_to_cpu(key[0]); 263 E_KEY[1] = le32_to_cpu(key[1]); 264 E_KEY[2] = le32_to_cpu(key[2]); 265 E_KEY[3] = le32_to_cpu(key[3]); 266 267 switch (key_len) { 268 case 16: 269 t = E_KEY[3]; 270 for (i = 0; i < 10; ++i) 271 loop4 (i); 272 break; 273 274 case 24: 275 E_KEY[4] = le32_to_cpu(key[4]); 276 t = E_KEY[5] = le32_to_cpu(key[5]); 277 for (i = 0; i < 8; ++i) 278 loop6 (i); 279 break; 280 281 case 32: 282 E_KEY[4] = le32_to_cpu(key[4]); 283 E_KEY[5] = le32_to_cpu(key[5]); 284 E_KEY[6] = le32_to_cpu(key[6]); 285 t = E_KEY[7] = le32_to_cpu(key[7]); 286 for (i = 0; i < 7; ++i) 287 loop8 (i); 288 break; 289 } 290 291 D_KEY[0] = E_KEY[0]; 292 D_KEY[1] = E_KEY[1]; 293 D_KEY[2] = E_KEY[2]; 294 D_KEY[3] = E_KEY[3]; 295 296 for (i = 4; i < key_len + 24; ++i) { 297 imix_col (D_KEY[i], E_KEY[i]); 298 } 299 300 return 0; 301 } 302 303 /* encrypt a block of text */ 304 305 #define f_nround(bo, bi, k) \ 306 f_rn(bo, bi, 0, k); \ 307 f_rn(bo, bi, 1, k); \ 308 f_rn(bo, bi, 2, k); \ 309 f_rn(bo, bi, 3, k); \ 310 k += 4 311 312 #define f_lround(bo, bi, k) \ 313 f_rl(bo, bi, 0, k); \ 314 f_rl(bo, bi, 1, k); \ 315 f_rl(bo, bi, 2, k); \ 316 f_rl(bo, bi, 3, k) 317 318 static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) 319 { 320 const struct aes_ctx *ctx = crypto_tfm_ctx(tfm); 321 const __le32 *src = (const __le32 *)in; 322 __le32 *dst = (__le32 *)out; 323 u32 b0[4], b1[4]; 324 const u32 *kp = E_KEY + 4; 325 326 b0[0] = le32_to_cpu(src[0]) ^ E_KEY[0]; 327 b0[1] = le32_to_cpu(src[1]) ^ E_KEY[1]; 328 b0[2] = le32_to_cpu(src[2]) ^ E_KEY[2]; 329 b0[3] = le32_to_cpu(src[3]) ^ E_KEY[3]; 330 331 if (ctx->key_length > 24) { 332 f_nround (b1, b0, kp); 333 f_nround (b0, b1, kp); 334 } 335 336 if (ctx->key_length > 16) { 337 f_nround (b1, b0, kp); 338 f_nround (b0, b1, kp); 339 } 340 341 f_nround (b1, b0, kp); 342 f_nround (b0, b1, kp); 343 f_nround (b1, b0, kp); 344 f_nround (b0, b1, kp); 345 f_nround (b1, b0, kp); 346 f_nround (b0, b1, kp); 347 f_nround (b1, b0, kp); 348 f_nround (b0, b1, kp); 349 f_nround (b1, b0, kp); 350 f_lround (b0, b1, kp); 351 352 dst[0] = cpu_to_le32(b0[0]); 353 dst[1] = cpu_to_le32(b0[1]); 354 dst[2] = cpu_to_le32(b0[2]); 355 dst[3] = cpu_to_le32(b0[3]); 356 } 357 358 /* decrypt a block of text */ 359 360 #define i_nround(bo, bi, k) \ 361 i_rn(bo, bi, 0, k); \ 362 i_rn(bo, bi, 1, k); \ 363 i_rn(bo, bi, 2, k); \ 364 i_rn(bo, bi, 3, k); \ 365 k -= 4 366 367 #define i_lround(bo, bi, k) \ 368 i_rl(bo, bi, 0, k); \ 369 i_rl(bo, bi, 1, k); \ 370 i_rl(bo, bi, 2, k); \ 371 i_rl(bo, bi, 3, k) 372 373 static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) 374 { 375 const struct aes_ctx *ctx = crypto_tfm_ctx(tfm); 376 const __le32 *src = (const __le32 *)in; 377 __le32 *dst = (__le32 *)out; 378 u32 b0[4], b1[4]; 379 const int key_len = ctx->key_length; 380 const u32 *kp = D_KEY + key_len + 20; 381 382 b0[0] = le32_to_cpu(src[0]) ^ E_KEY[key_len + 24]; 383 b0[1] = le32_to_cpu(src[1]) ^ E_KEY[key_len + 25]; 384 b0[2] = le32_to_cpu(src[2]) ^ E_KEY[key_len + 26]; 385 b0[3] = le32_to_cpu(src[3]) ^ E_KEY[key_len + 27]; 386 387 if (key_len > 24) { 388 i_nround (b1, b0, kp); 389 i_nround (b0, b1, kp); 390 } 391 392 if (key_len > 16) { 393 i_nround (b1, b0, kp); 394 i_nround (b0, b1, kp); 395 } 396 397 i_nround (b1, b0, kp); 398 i_nround (b0, b1, kp); 399 i_nround (b1, b0, kp); 400 i_nround (b0, b1, kp); 401 i_nround (b1, b0, kp); 402 i_nround (b0, b1, kp); 403 i_nround (b1, b0, kp); 404 i_nround (b0, b1, kp); 405 i_nround (b1, b0, kp); 406 i_lround (b0, b1, kp); 407 408 dst[0] = cpu_to_le32(b0[0]); 409 dst[1] = cpu_to_le32(b0[1]); 410 dst[2] = cpu_to_le32(b0[2]); 411 dst[3] = cpu_to_le32(b0[3]); 412 } 413 414 415 static struct crypto_alg aes_alg = { 416 .cra_name = "aes", 417 .cra_driver_name = "aes-generic", 418 .cra_priority = 100, 419 .cra_flags = CRYPTO_ALG_TYPE_CIPHER, 420 .cra_blocksize = AES_BLOCK_SIZE, 421 .cra_ctxsize = sizeof(struct aes_ctx), 422 .cra_alignmask = 3, 423 .cra_module = THIS_MODULE, 424 .cra_list = LIST_HEAD_INIT(aes_alg.cra_list), 425 .cra_u = { 426 .cipher = { 427 .cia_min_keysize = AES_MIN_KEY_SIZE, 428 .cia_max_keysize = AES_MAX_KEY_SIZE, 429 .cia_setkey = aes_set_key, 430 .cia_encrypt = aes_encrypt, 431 .cia_decrypt = aes_decrypt 432 } 433 } 434 }; 435 436 static int __init aes_init(void) 437 { 438 gen_tabs(); 439 return crypto_register_alg(&aes_alg); 440 } 441 442 static void __exit aes_fini(void) 443 { 444 crypto_unregister_alg(&aes_alg); 445 } 446 447 module_init(aes_init); 448 module_exit(aes_fini); 449 450 MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm"); 451 MODULE_LICENSE("Dual BSD/GPL"); 452 MODULE_ALIAS("aes"); 453