Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * AES (Rijndael) cipher - decrypt |
| 3 | * |
| 4 | * Modifications to public domain implementation: |
| 5 | * - support only 128-bit keys |
| 6 | * - cleanup |
| 7 | * - use C pre-processor to make it easier to change S table access |
| 8 | * - added option (AES_SMALL_TABLES) for reducing code size by about 8 kB at |
| 9 | * cost of reduced throughput (quite small difference on Pentium 4, |
| 10 | * 10-25% when using -O1 or -O2 optimization) |
| 11 | * |
| 12 | * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi> |
| 13 | * |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame^] | 14 | * This software may be distributed under the terms of the BSD license. |
| 15 | * See README for more details. |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include "includes.h" |
| 19 | |
| 20 | #include "common.h" |
| 21 | #include "crypto.h" |
| 22 | #include "aes_i.h" |
| 23 | |
| 24 | /** |
| 25 | * Expand the cipher key into the decryption key schedule. |
| 26 | * |
| 27 | * @return the number of rounds for the given cipher key size. |
| 28 | */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 29 | static void rijndaelKeySetupDec(u32 rk[/*44*/], const u8 cipherKey[]) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 30 | { |
| 31 | int Nr = 10, i, j; |
| 32 | u32 temp; |
| 33 | |
| 34 | /* expand the cipher key: */ |
| 35 | rijndaelKeySetupEnc(rk, cipherKey); |
| 36 | /* invert the order of the round keys: */ |
| 37 | for (i = 0, j = 4*Nr; i < j; i += 4, j -= 4) { |
| 38 | temp = rk[i ]; rk[i ] = rk[j ]; rk[j ] = temp; |
| 39 | temp = rk[i + 1]; rk[i + 1] = rk[j + 1]; rk[j + 1] = temp; |
| 40 | temp = rk[i + 2]; rk[i + 2] = rk[j + 2]; rk[j + 2] = temp; |
| 41 | temp = rk[i + 3]; rk[i + 3] = rk[j + 3]; rk[j + 3] = temp; |
| 42 | } |
| 43 | /* apply the inverse MixColumn transform to all round keys but the |
| 44 | * first and the last: */ |
| 45 | for (i = 1; i < Nr; i++) { |
| 46 | rk += 4; |
| 47 | for (j = 0; j < 4; j++) { |
| 48 | rk[j] = TD0_(TE4((rk[j] >> 24) )) ^ |
| 49 | TD1_(TE4((rk[j] >> 16) & 0xff)) ^ |
| 50 | TD2_(TE4((rk[j] >> 8) & 0xff)) ^ |
| 51 | TD3_(TE4((rk[j] ) & 0xff)); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | void * aes_decrypt_init(const u8 *key, size_t len) |
| 57 | { |
| 58 | u32 *rk; |
| 59 | if (len != 16) |
| 60 | return NULL; |
| 61 | rk = os_malloc(AES_PRIV_SIZE); |
| 62 | if (rk == NULL) |
| 63 | return NULL; |
| 64 | rijndaelKeySetupDec(rk, key); |
| 65 | return rk; |
| 66 | } |
| 67 | |
| 68 | static void rijndaelDecrypt(const u32 rk[/*44*/], const u8 ct[16], u8 pt[16]) |
| 69 | { |
| 70 | u32 s0, s1, s2, s3, t0, t1, t2, t3; |
| 71 | const int Nr = 10; |
| 72 | #ifndef FULL_UNROLL |
| 73 | int r; |
| 74 | #endif /* ?FULL_UNROLL */ |
| 75 | |
| 76 | /* |
| 77 | * map byte array block to cipher state |
| 78 | * and add initial round key: |
| 79 | */ |
| 80 | s0 = GETU32(ct ) ^ rk[0]; |
| 81 | s1 = GETU32(ct + 4) ^ rk[1]; |
| 82 | s2 = GETU32(ct + 8) ^ rk[2]; |
| 83 | s3 = GETU32(ct + 12) ^ rk[3]; |
| 84 | |
| 85 | #define ROUND(i,d,s) \ |
| 86 | d##0 = TD0(s##0) ^ TD1(s##3) ^ TD2(s##2) ^ TD3(s##1) ^ rk[4 * i]; \ |
| 87 | d##1 = TD0(s##1) ^ TD1(s##0) ^ TD2(s##3) ^ TD3(s##2) ^ rk[4 * i + 1]; \ |
| 88 | d##2 = TD0(s##2) ^ TD1(s##1) ^ TD2(s##0) ^ TD3(s##3) ^ rk[4 * i + 2]; \ |
| 89 | d##3 = TD0(s##3) ^ TD1(s##2) ^ TD2(s##1) ^ TD3(s##0) ^ rk[4 * i + 3] |
| 90 | |
| 91 | #ifdef FULL_UNROLL |
| 92 | |
| 93 | ROUND(1,t,s); |
| 94 | ROUND(2,s,t); |
| 95 | ROUND(3,t,s); |
| 96 | ROUND(4,s,t); |
| 97 | ROUND(5,t,s); |
| 98 | ROUND(6,s,t); |
| 99 | ROUND(7,t,s); |
| 100 | ROUND(8,s,t); |
| 101 | ROUND(9,t,s); |
| 102 | |
| 103 | rk += Nr << 2; |
| 104 | |
| 105 | #else /* !FULL_UNROLL */ |
| 106 | |
| 107 | /* Nr - 1 full rounds: */ |
| 108 | r = Nr >> 1; |
| 109 | for (;;) { |
| 110 | ROUND(1,t,s); |
| 111 | rk += 8; |
| 112 | if (--r == 0) |
| 113 | break; |
| 114 | ROUND(0,s,t); |
| 115 | } |
| 116 | |
| 117 | #endif /* ?FULL_UNROLL */ |
| 118 | |
| 119 | #undef ROUND |
| 120 | |
| 121 | /* |
| 122 | * apply last round and |
| 123 | * map cipher state to byte array block: |
| 124 | */ |
| 125 | s0 = TD41(t0) ^ TD42(t3) ^ TD43(t2) ^ TD44(t1) ^ rk[0]; |
| 126 | PUTU32(pt , s0); |
| 127 | s1 = TD41(t1) ^ TD42(t0) ^ TD43(t3) ^ TD44(t2) ^ rk[1]; |
| 128 | PUTU32(pt + 4, s1); |
| 129 | s2 = TD41(t2) ^ TD42(t1) ^ TD43(t0) ^ TD44(t3) ^ rk[2]; |
| 130 | PUTU32(pt + 8, s2); |
| 131 | s3 = TD41(t3) ^ TD42(t2) ^ TD43(t1) ^ TD44(t0) ^ rk[3]; |
| 132 | PUTU32(pt + 12, s3); |
| 133 | } |
| 134 | |
| 135 | void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain) |
| 136 | { |
| 137 | rijndaelDecrypt(ctx, crypt, plain); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | void aes_decrypt_deinit(void *ctx) |
| 142 | { |
| 143 | os_memset(ctx, 0, AES_PRIV_SIZE); |
| 144 | os_free(ctx); |
| 145 | } |