libosmogsm
0.11.0-dirty
Osmocom GSM library
|
00001 00003 /* 00004 * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi> 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License version 2 as 00008 * published by the Free Software Foundation. 00009 * 00010 * Alternatively, this software may be distributed under the terms of BSD 00011 * license. 00012 * 00013 * SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause 00014 * 00015 * See README and COPYING for more details. 00016 */ 00017 00018 #pragma once 00019 00020 #include "aes.h" 00021 00022 /* #define FULL_UNROLL */ 00023 #define AES_SMALL_TABLES 00024 00025 extern const u32 Te0[256]; 00026 extern const u32 Te1[256]; 00027 extern const u32 Te2[256]; 00028 extern const u32 Te3[256]; 00029 extern const u32 Te4[256]; 00030 extern const u32 Td0[256]; 00031 extern const u32 Td1[256]; 00032 extern const u32 Td2[256]; 00033 extern const u32 Td3[256]; 00034 extern const u32 Td4[256]; 00035 extern const u32 rcon[10]; 00036 extern const u8 Td4s[256]; 00037 extern const u8 rcons[10]; 00038 00039 #ifndef AES_SMALL_TABLES 00040 00041 #define RCON(i) rcon[(i)] 00042 00043 #define TE0(i) Te0[((i) >> 24) & 0xff] 00044 #define TE1(i) Te1[((i) >> 16) & 0xff] 00045 #define TE2(i) Te2[((i) >> 8) & 0xff] 00046 #define TE3(i) Te3[(i) & 0xff] 00047 #define TE41(i) (Te4[((i) >> 24) & 0xff] & 0xff000000) 00048 #define TE42(i) (Te4[((i) >> 16) & 0xff] & 0x00ff0000) 00049 #define TE43(i) (Te4[((i) >> 8) & 0xff] & 0x0000ff00) 00050 #define TE44(i) (Te4[(i) & 0xff] & 0x000000ff) 00051 #define TE421(i) (Te4[((i) >> 16) & 0xff] & 0xff000000) 00052 #define TE432(i) (Te4[((i) >> 8) & 0xff] & 0x00ff0000) 00053 #define TE443(i) (Te4[(i) & 0xff] & 0x0000ff00) 00054 #define TE414(i) (Te4[((i) >> 24) & 0xff] & 0x000000ff) 00055 #define TE4(i) (Te4[(i)] & 0x000000ff) 00056 00057 #define TD0(i) Td0[((i) >> 24) & 0xff] 00058 #define TD1(i) Td1[((i) >> 16) & 0xff] 00059 #define TD2(i) Td2[((i) >> 8) & 0xff] 00060 #define TD3(i) Td3[(i) & 0xff] 00061 #define TD41(i) (Td4[((i) >> 24) & 0xff] & 0xff000000) 00062 #define TD42(i) (Td4[((i) >> 16) & 0xff] & 0x00ff0000) 00063 #define TD43(i) (Td4[((i) >> 8) & 0xff] & 0x0000ff00) 00064 #define TD44(i) (Td4[(i) & 0xff] & 0x000000ff) 00065 #define TD0_(i) Td0[(i) & 0xff] 00066 #define TD1_(i) Td1[(i) & 0xff] 00067 #define TD2_(i) Td2[(i) & 0xff] 00068 #define TD3_(i) Td3[(i) & 0xff] 00069 00070 #else /* AES_SMALL_TABLES */ 00071 00072 #define RCON(i) ((u32)rcons[(i)] << 24) 00073 00074 static inline u32 rotr(u32 val, int bits) 00075 { 00076 return (val >> bits) | (val << (32 - bits)); 00077 } 00078 00079 #define TE0(i) Te0[((i) >> 24) & 0xff] 00080 #define TE1(i) rotr(Te0[((i) >> 16) & 0xff], 8) 00081 #define TE2(i) rotr(Te0[((i) >> 8) & 0xff], 16) 00082 #define TE3(i) rotr(Te0[(i) & 0xff], 24) 00083 #define TE41(i) ((Te0[((i) >> 24) & 0xff] << 8) & 0xff000000) 00084 #define TE42(i) (Te0[((i) >> 16) & 0xff] & 0x00ff0000) 00085 #define TE43(i) (Te0[((i) >> 8) & 0xff] & 0x0000ff00) 00086 #define TE44(i) ((Te0[(i) & 0xff] >> 8) & 0x000000ff) 00087 #define TE421(i) ((Te0[((i) >> 16) & 0xff] << 8) & 0xff000000) 00088 #define TE432(i) (Te0[((i) >> 8) & 0xff] & 0x00ff0000) 00089 #define TE443(i) (Te0[(i) & 0xff] & 0x0000ff00) 00090 #define TE414(i) ((Te0[((i) >> 24) & 0xff] >> 8) & 0x000000ff) 00091 #define TE4(i) ((Te0[(i)] >> 8) & 0x000000ff) 00092 00093 #define TD0(i) Td0[((i) >> 24) & 0xff] 00094 #define TD1(i) rotr(Td0[((i) >> 16) & 0xff], 8) 00095 #define TD2(i) rotr(Td0[((i) >> 8) & 0xff], 16) 00096 #define TD3(i) rotr(Td0[(i) & 0xff], 24) 00097 #define TD41(i) (Td4s[((i) >> 24) & 0xff] << 24) 00098 #define TD42(i) (Td4s[((i) >> 16) & 0xff] << 16) 00099 #define TD43(i) (Td4s[((i) >> 8) & 0xff] << 8) 00100 #define TD44(i) (Td4s[(i) & 0xff]) 00101 #define TD0_(i) Td0[(i) & 0xff] 00102 #define TD1_(i) rotr(Td0[(i) & 0xff], 8) 00103 #define TD2_(i) rotr(Td0[(i) & 0xff], 16) 00104 #define TD3_(i) rotr(Td0[(i) & 0xff], 24) 00105 00106 #endif /* AES_SMALL_TABLES */ 00107 00108 #ifdef _MSC_VER 00109 #define SWAP(x) (_lrotl(x, 8) & 0x00ff00ff | _lrotr(x, 8) & 0xff00ff00) 00110 #define GETU32(p) SWAP(*((u32 *)(p))) 00111 #define PUTU32(ct, st) { *((u32 *)(ct)) = SWAP((st)); } 00112 #else 00113 #define GETU32(pt) (((u32)(pt)[0] << 24) ^ ((u32)(pt)[1] << 16) ^ \ 00114 ((u32)(pt)[2] << 8) ^ ((u32)(pt)[3])) 00115 #define PUTU32(ct, st) { \ 00116 (ct)[0] = (u8)((st) >> 24); (ct)[1] = (u8)((st) >> 16); \ 00117 (ct)[2] = (u8)((st) >> 8); (ct)[3] = (u8)(st); } 00118 #endif 00119 00120 #define AES_PRIV_SIZE (4 * 44) 00121 00122 void rijndaelKeySetupEnc(u32 rk[/*44*/], const u8 cipherKey[]);