]> git.saurik.com Git - apple/security.git/blob - AppleCSP/AES/rijndaelGladman.h
Security-54.1.tar.gz
[apple/security.git] / AppleCSP / AES / rijndaelGladman.h
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 /*
20 * rijndaelGladman.h - constants and macros for Gladman AES/Rijndael implementation.
21 * Based on std_defs.h written by Dr. Brian Gladman.
22 */
23
24
25
26 /* 1. Standard types for AES cryptography source code */
27
28 typedef unsigned char u1byte; /* an 8 bit unsigned character type */
29 typedef unsigned short u2byte; /* a 16 bit unsigned integer type */
30 typedef unsigned long u4byte; /* a 32 bit unsigned integer type */
31
32 typedef signed char s1byte; /* an 8 bit signed character type */
33 typedef signed short s2byte; /* a 16 bit signed integer type */
34 typedef signed long s4byte; /* a 32 bit signed integer type */
35
36 /* 2. Standard interface for AES cryptographic routines */
37
38 /* These are all based on 32 bit unsigned values and will therefore */
39 /* require endian conversions for big-endian architectures */
40
41 #ifdef __cplusplus
42 extern "C"
43 {
44 #endif
45
46 /*
47 * Lookup tables, dynamically allocated (by client) and generated (by
48 * gen_tabs())
49 */
50 #define LARGE_TABLES
51
52 #define POW_TAB_SIZE 256
53 #define LOG_TAB_SIZE 256
54 #define SBX_TAB_SIZE 256
55 #define ISB_TAB_SIZE 256
56 #define RCO_TAB_SIZE 10
57 #define FT_TAB_SIZE_MS 4
58 #define FT_TAB_SIZE_LS 256
59 #define IT_TAB_SIZE_MS 4
60 #define IT_TAB_SIZE_LS 256
61 extern u1byte *pow_tab; /* [POW_TAB_SIZE] */
62 extern u1byte *log_tab; /* [LOG_TAB_SIZE] */;
63 extern u1byte *sbx_tab; /* [SBX_TAB_SIZE] */
64 extern u1byte *isb_tab; /* [ISB_TAB_SIZE] */
65 extern u4byte *rco_tab; /* [RCO_TAB_SIZE] */
66 extern u4byte (*ft_tab)[FT_TAB_SIZE_LS];
67 extern u4byte (*it_tab)[IT_TAB_SIZE_LS];
68
69 #ifdef LARGE_TABLES
70 #define FL_TAB_SIZE_MS 4
71 #define FL_TAB_SIZE_LS 256
72 #define IL_TAB_SIZE_MS 4
73 #define IL_TAB_SIZE_LS 256
74 extern u4byte (*fl_tab)[FL_TAB_SIZE_LS];
75 extern u4byte (*il_tab)[IL_TAB_SIZE_LS];
76 #endif
77
78 typedef struct {
79 u4byte k_len;
80 u4byte e_key[64];
81 u4byte d_key[64];
82 } GAesKey;
83
84 void gen_tabs(void); // one-time-only table generate
85 u4byte *set_key(const u4byte in_key[], const u4byte key_len, GAesKey *aesKey);
86 void rEncrypt(const u4byte in_blk[4], u4byte out_blk[4], const GAesKey *aesKey);
87 void rDecrypt(const u4byte in_blk[4], u4byte out_blk[4], const GAesKey *aesKey);
88
89 #ifdef __cplusplus
90 };
91 #endif
92
93 /* 3. Basic macros for speeding up generic operations */
94
95 /* Circular rotate of 32 bit values */
96
97 #ifdef _MSC_VER
98
99 # include <stdlib.h>
100 # pragma intrinsic(_lrotr,_lrotl)
101 # define rotr(x,n) _lrotr(x,n)
102 # define rotl(x,n) _lrotl(x,n)
103
104 #else
105
106 #define rotr(x,n) (((x) >> ((int)(n))) | ((x) << (32 - (int)(n))))
107 #define rotl(x,n) (((x) << ((int)(n))) | ((x) >> (32 - (int)(n))))
108
109 #endif
110
111 /* Invert byte order in a 32 bit variable */
112
113 #define bswap(x) ((rotl(x, 8) & 0x00ff00ff) | (rotr(x, 8) & 0xff00ff00))
114
115 /* Extract byte from a 32 bit quantity (little endian notation) */
116
117 #define byte(x,n) ((u1byte)((x) >> (8 * n)))
118
119 /* For inverting byte order in input/output 32 bit words if needed */
120 #ifdef __ppc__
121 #define BYTE_SWAP
122 #endif
123
124 #ifdef BLOCK_SWAP
125 #define BYTE_SWAP
126 #define WORD_SWAP
127 #endif
128
129 #ifdef BYTE_SWAP
130 #define io_swap(x) bswap(x)
131 #else
132 #define io_swap(x) (x)
133 #endif
134
135 /* For inverting the byte order of input/output blocks if needed */
136
137 #ifdef WORD_SWAP
138
139 #define get_block(x) \
140 ((u4byte*)(x))[0] = io_swap(in_blk[3]); \
141 ((u4byte*)(x))[1] = io_swap(in_blk[2]); \
142 ((u4byte*)(x))[2] = io_swap(in_blk[1]); \
143 ((u4byte*)(x))[3] = io_swap(in_blk[0])
144
145 #define put_block(x) \
146 out_blk[3] = io_swap(((u4byte*)(x))[0]); \
147 out_blk[2] = io_swap(((u4byte*)(x))[1]); \
148 out_blk[1] = io_swap(((u4byte*)(x))[2]); \
149 out_blk[0] = io_swap(((u4byte*)(x))[3])
150
151 #define get_key(x,len) \
152 ((u4byte*)(x))[4] = ((u4byte*)(x))[5] = \
153 ((u4byte*)(x))[6] = ((u4byte*)(x))[7] = 0; \
154 switch((((len) + 63) / 64)) { \
155 case 2: \
156 ((u4byte*)(x))[0] = io_swap(in_key[3]); \
157 ((u4byte*)(x))[1] = io_swap(in_key[2]); \
158 ((u4byte*)(x))[2] = io_swap(in_key[1]); \
159 ((u4byte*)(x))[3] = io_swap(in_key[0]); \
160 break; \
161 case 3: \
162 ((u4byte*)(x))[0] = io_swap(in_key[5]); \
163 ((u4byte*)(x))[1] = io_swap(in_key[4]); \
164 ((u4byte*)(x))[2] = io_swap(in_key[3]); \
165 ((u4byte*)(x))[3] = io_swap(in_key[2]); \
166 ((u4byte*)(x))[4] = io_swap(in_key[1]); \
167 ((u4byte*)(x))[5] = io_swap(in_key[0]); \
168 break; \
169 case 4: \
170 ((u4byte*)(x))[0] = io_swap(in_key[7]); \
171 ((u4byte*)(x))[1] = io_swap(in_key[6]); \
172 ((u4byte*)(x))[2] = io_swap(in_key[5]); \
173 ((u4byte*)(x))[3] = io_swap(in_key[4]); \
174 ((u4byte*)(x))[4] = io_swap(in_key[3]); \
175 ((u4byte*)(x))[5] = io_swap(in_key[2]); \
176 ((u4byte*)(x))[6] = io_swap(in_key[1]); \
177 ((u4byte*)(x))[7] = io_swap(in_key[0]); \
178 }
179
180 #else
181
182 #define get_block(x) \
183 ((u4byte*)(x))[0] = io_swap(in_blk[0]); \
184 ((u4byte*)(x))[1] = io_swap(in_blk[1]); \
185 ((u4byte*)(x))[2] = io_swap(in_blk[2]); \
186 ((u4byte*)(x))[3] = io_swap(in_blk[3])
187
188 #define put_block(x) \
189 out_blk[0] = io_swap(((u4byte*)(x))[0]); \
190 out_blk[1] = io_swap(((u4byte*)(x))[1]); \
191 out_blk[2] = io_swap(((u4byte*)(x))[2]); \
192 out_blk[3] = io_swap(((u4byte*)(x))[3])
193
194 #define get_key(x,len) \
195 ((u4byte*)(x))[4] = ((u4byte*)(x))[5] = \
196 ((u4byte*)(x))[6] = ((u4byte*)(x))[7] = 0; \
197 switch((((len) + 63) / 64)) { \
198 case 4: \
199 ((u4byte*)(x))[6] = io_swap(in_key[6]); \
200 ((u4byte*)(x))[7] = io_swap(in_key[7]); \
201 case 3: \
202 ((u4byte*)(x))[4] = io_swap(in_key[4]); \
203 ((u4byte*)(x))[5] = io_swap(in_key[5]); \
204 case 2: \
205 ((u4byte*)(x))[0] = io_swap(in_key[0]); \
206 ((u4byte*)(x))[1] = io_swap(in_key[1]); \
207 ((u4byte*)(x))[2] = io_swap(in_key[2]); \
208 ((u4byte*)(x))[3] = io_swap(in_key[3]); \
209 }
210
211 #endif