Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file aes_i.h |
| 2 | * AES (Rijndael) cipher. */ |
Harald Welte | 781bd5d | 2011-12-06 22:23:52 +0100 | [diff] [blame] | 3 | /* |
Harald Welte | 781bd5d | 2011-12-06 22:23:52 +0100 | [diff] [blame] | 4 | * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * Alternatively, this software may be distributed under the terms of BSD |
| 11 | * license. |
| 12 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 13 | * SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause |
| 14 | * |
Harald Welte | 781bd5d | 2011-12-06 22:23:52 +0100 | [diff] [blame] | 15 | * See README and COPYING for more details. |
| 16 | */ |
| 17 | |
Sylvain Munaut | 12ba778 | 2014-06-16 10:13:40 +0200 | [diff] [blame] | 18 | #pragma once |
Harald Welte | 781bd5d | 2011-12-06 22:23:52 +0100 | [diff] [blame] | 19 | |
| 20 | #include "aes.h" |
| 21 | |
| 22 | /* #define FULL_UNROLL */ |
| 23 | #define AES_SMALL_TABLES |
| 24 | |
| 25 | extern const u32 Te0[256]; |
| 26 | extern const u32 Te1[256]; |
| 27 | extern const u32 Te2[256]; |
| 28 | extern const u32 Te3[256]; |
| 29 | extern const u32 Te4[256]; |
| 30 | extern const u32 Td0[256]; |
| 31 | extern const u32 Td1[256]; |
| 32 | extern const u32 Td2[256]; |
| 33 | extern const u32 Td3[256]; |
| 34 | extern const u32 Td4[256]; |
| 35 | extern const u32 rcon[10]; |
| 36 | extern const u8 Td4s[256]; |
| 37 | extern const u8 rcons[10]; |
| 38 | |
| 39 | #ifndef AES_SMALL_TABLES |
| 40 | |
| 41 | #define RCON(i) rcon[(i)] |
| 42 | |
| 43 | #define TE0(i) Te0[((i) >> 24) & 0xff] |
| 44 | #define TE1(i) Te1[((i) >> 16) & 0xff] |
| 45 | #define TE2(i) Te2[((i) >> 8) & 0xff] |
| 46 | #define TE3(i) Te3[(i) & 0xff] |
| 47 | #define TE41(i) (Te4[((i) >> 24) & 0xff] & 0xff000000) |
| 48 | #define TE42(i) (Te4[((i) >> 16) & 0xff] & 0x00ff0000) |
| 49 | #define TE43(i) (Te4[((i) >> 8) & 0xff] & 0x0000ff00) |
| 50 | #define TE44(i) (Te4[(i) & 0xff] & 0x000000ff) |
| 51 | #define TE421(i) (Te4[((i) >> 16) & 0xff] & 0xff000000) |
| 52 | #define TE432(i) (Te4[((i) >> 8) & 0xff] & 0x00ff0000) |
| 53 | #define TE443(i) (Te4[(i) & 0xff] & 0x0000ff00) |
| 54 | #define TE414(i) (Te4[((i) >> 24) & 0xff] & 0x000000ff) |
| 55 | #define TE4(i) (Te4[(i)] & 0x000000ff) |
| 56 | |
| 57 | #define TD0(i) Td0[((i) >> 24) & 0xff] |
| 58 | #define TD1(i) Td1[((i) >> 16) & 0xff] |
| 59 | #define TD2(i) Td2[((i) >> 8) & 0xff] |
| 60 | #define TD3(i) Td3[(i) & 0xff] |
| 61 | #define TD41(i) (Td4[((i) >> 24) & 0xff] & 0xff000000) |
| 62 | #define TD42(i) (Td4[((i) >> 16) & 0xff] & 0x00ff0000) |
| 63 | #define TD43(i) (Td4[((i) >> 8) & 0xff] & 0x0000ff00) |
| 64 | #define TD44(i) (Td4[(i) & 0xff] & 0x000000ff) |
| 65 | #define TD0_(i) Td0[(i) & 0xff] |
| 66 | #define TD1_(i) Td1[(i) & 0xff] |
| 67 | #define TD2_(i) Td2[(i) & 0xff] |
| 68 | #define TD3_(i) Td3[(i) & 0xff] |
| 69 | |
| 70 | #else /* AES_SMALL_TABLES */ |
| 71 | |
Holger Hans Peter Freyther | aa16d63 | 2016-04-23 17:55:39 +0200 | [diff] [blame] | 72 | #define RCON(i) ((u32)rcons[(i)] << 24) |
Harald Welte | 781bd5d | 2011-12-06 22:23:52 +0100 | [diff] [blame] | 73 | |
| 74 | static inline u32 rotr(u32 val, int bits) |
| 75 | { |
| 76 | return (val >> bits) | (val << (32 - bits)); |
| 77 | } |
| 78 | |
| 79 | #define TE0(i) Te0[((i) >> 24) & 0xff] |
| 80 | #define TE1(i) rotr(Te0[((i) >> 16) & 0xff], 8) |
| 81 | #define TE2(i) rotr(Te0[((i) >> 8) & 0xff], 16) |
| 82 | #define TE3(i) rotr(Te0[(i) & 0xff], 24) |
| 83 | #define TE41(i) ((Te0[((i) >> 24) & 0xff] << 8) & 0xff000000) |
| 84 | #define TE42(i) (Te0[((i) >> 16) & 0xff] & 0x00ff0000) |
| 85 | #define TE43(i) (Te0[((i) >> 8) & 0xff] & 0x0000ff00) |
| 86 | #define TE44(i) ((Te0[(i) & 0xff] >> 8) & 0x000000ff) |
| 87 | #define TE421(i) ((Te0[((i) >> 16) & 0xff] << 8) & 0xff000000) |
| 88 | #define TE432(i) (Te0[((i) >> 8) & 0xff] & 0x00ff0000) |
| 89 | #define TE443(i) (Te0[(i) & 0xff] & 0x0000ff00) |
| 90 | #define TE414(i) ((Te0[((i) >> 24) & 0xff] >> 8) & 0x000000ff) |
| 91 | #define TE4(i) ((Te0[(i)] >> 8) & 0x000000ff) |
| 92 | |
| 93 | #define TD0(i) Td0[((i) >> 24) & 0xff] |
| 94 | #define TD1(i) rotr(Td0[((i) >> 16) & 0xff], 8) |
| 95 | #define TD2(i) rotr(Td0[((i) >> 8) & 0xff], 16) |
| 96 | #define TD3(i) rotr(Td0[(i) & 0xff], 24) |
| 97 | #define TD41(i) (Td4s[((i) >> 24) & 0xff] << 24) |
| 98 | #define TD42(i) (Td4s[((i) >> 16) & 0xff] << 16) |
| 99 | #define TD43(i) (Td4s[((i) >> 8) & 0xff] << 8) |
| 100 | #define TD44(i) (Td4s[(i) & 0xff]) |
| 101 | #define TD0_(i) Td0[(i) & 0xff] |
| 102 | #define TD1_(i) rotr(Td0[(i) & 0xff], 8) |
| 103 | #define TD2_(i) rotr(Td0[(i) & 0xff], 16) |
| 104 | #define TD3_(i) rotr(Td0[(i) & 0xff], 24) |
| 105 | |
| 106 | #endif /* AES_SMALL_TABLES */ |
| 107 | |
| 108 | #ifdef _MSC_VER |
| 109 | #define SWAP(x) (_lrotl(x, 8) & 0x00ff00ff | _lrotr(x, 8) & 0xff00ff00) |
| 110 | #define GETU32(p) SWAP(*((u32 *)(p))) |
| 111 | #define PUTU32(ct, st) { *((u32 *)(ct)) = SWAP((st)); } |
| 112 | #else |
| 113 | #define GETU32(pt) (((u32)(pt)[0] << 24) ^ ((u32)(pt)[1] << 16) ^ \ |
| 114 | ((u32)(pt)[2] << 8) ^ ((u32)(pt)[3])) |
| 115 | #define PUTU32(ct, st) { \ |
| 116 | (ct)[0] = (u8)((st) >> 24); (ct)[1] = (u8)((st) >> 16); \ |
| 117 | (ct)[2] = (u8)((st) >> 8); (ct)[3] = (u8)(st); } |
| 118 | #endif |
| 119 | |
| 120 | #define AES_PRIV_SIZE (4 * 44) |
| 121 | |
| 122 | void rijndaelKeySetupEnc(u32 rk[/*44*/], const u8 cipherKey[]); |