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