]>
git.saurik.com Git - apple/security.git/blob - AppleCSP/AES/rijndaelGladman.h
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
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
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.
20 * rijndaelGladman.h - constants and macros for Gladman AES/Rijndael implementation.
21 * Based on std_defs.h written by Dr. Brian Gladman.
26 /* 1. Standard types for AES cryptography source code */
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 */
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 */
36 /* 2. Standard interface for AES cryptographic routines */
38 /* These are all based on 32 bit unsigned values and will therefore */
39 /* require endian conversions for big-endian architectures */
47 * Lookup tables, dynamically allocated (by client) and generated (by
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
];
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
];
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
);
93 /* 3. Basic macros for speeding up generic operations */
95 /* Circular rotate of 32 bit values */
100 # pragma intrinsic(_lrotr,_lrotl)
101 # define rotr(x,n) _lrotr(x,n)
102 # define rotl(x,n) _lrotl(x,n)
106 #define rotr(x,n) (((x) >> ((int)(n))) | ((x) << (32 - (int)(n))))
107 #define rotl(x,n) (((x) << ((int)(n))) | ((x) >> (32 - (int)(n))))
111 /* Invert byte order in a 32 bit variable */
113 #define bswap(x) ((rotl(x, 8) & 0x00ff00ff) | (rotr(x, 8) & 0xff00ff00))
115 /* Extract byte from a 32 bit quantity (little endian notation) */
117 #define byte(x,n) ((u1byte)((x) >> (8 * n)))
119 /* For inverting byte order in input/output 32 bit words if needed */
130 #define io_swap(x) bswap(x)
132 #define io_swap(x) (x)
135 /* For inverting the byte order of input/output blocks if needed */
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])
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])
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)) { \
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]); \
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]); \
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]); \
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])
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])
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)) { \
199 ((u4byte*)(x))[6] = io_swap(in_key[6]); \
200 ((u4byte*)(x))[7] = io_swap(in_key[7]); \
202 ((u4byte*)(x))[4] = io_swap(in_key[4]); \
203 ((u4byte*)(x))[5] = io_swap(in_key[5]); \
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]); \