]> git.saurik.com Git - apple/security.git/blob - SecurityTests/cspxutils/aesVect/std_defs.h
Security-57031.1.35.tar.gz
[apple/security.git] / SecurityTests / cspxutils / aesVect / std_defs.h
1
2
3
4 /* 1. Standard types for AES cryptography source code */
5
6 typedef unsigned char u1byte; /* an 8 bit unsigned character type */
7 typedef unsigned short u2byte; /* a 16 bit unsigned integer type */
8 typedef unsigned long u4byte; /* a 32 bit unsigned integer type */
9
10 typedef signed char s1byte; /* an 8 bit signed character type */
11 typedef signed short s2byte; /* a 16 bit signed integer type */
12 typedef signed long s4byte; /* a 32 bit signed integer type */
13
14 /* 2. Standard interface for AES cryptographic routines */
15
16 /* These are all based on 32 bit unsigned values and will therefore */
17 /* require endian conversions for big-endian architectures */
18
19 #ifdef __cplusplus
20 extern "C"
21 {
22 #endif
23
24 char **cipher_name(void);
25 u4byte *set_key(const u4byte in_key[], const u4byte key_len);
26 void rEncrypt(const u4byte in_blk[4], u4byte out_blk[4]);
27 void rDecrypt(const u4byte in_blk[4], u4byte out_blk[4]);
28
29 #ifdef __cplusplus
30 };
31 #endif
32
33 /* 3. Basic macros for speeding up generic operations */
34
35 /* Circular rotate of 32 bit values */
36
37 #ifdef _MSC_VER
38
39 # include <stdlib.h>
40 # pragma intrinsic(_lrotr,_lrotl)
41 # define rotr(x,n) _lrotr(x,n)
42 # define rotl(x,n) _lrotl(x,n)
43
44 #else
45
46 #define rotr(x,n) (((x) >> ((int)(n))) | ((x) << (32 - (int)(n))))
47 #define rotl(x,n) (((x) << ((int)(n))) | ((x) >> (32 - (int)(n))))
48
49 #endif
50
51 /* Invert byte order in a 32 bit variable */
52
53 #define bswap(x) ((rotl(x, 8) & 0x00ff00ff) | (rotr(x, 8) & 0xff00ff00))
54
55 /* Extract byte from a 32 bit quantity (little endian notation) */
56
57 #define byte(x,n) ((u1byte)((x) >> (8 * n)))
58
59 /* For inverting byte order in input/output 32 bit words if needed */
60 #ifdef __ppc__
61 #define BYTE_SWAP
62 #endif
63
64 #ifdef BLOCK_SWAP
65 #define BYTE_SWAP
66 #define WORD_SWAP
67 #endif
68
69 #ifdef BYTE_SWAP
70 #define io_swap(x) bswap(x)
71 #else
72 #define io_swap(x) (x)
73 #endif
74
75 /* For inverting the byte order of input/output blocks if needed */
76
77 #ifdef WORD_SWAP
78
79 #define get_block(x) \
80 ((u4byte*)(x))[0] = io_swap(in_blk[3]); \
81 ((u4byte*)(x))[1] = io_swap(in_blk[2]); \
82 ((u4byte*)(x))[2] = io_swap(in_blk[1]); \
83 ((u4byte*)(x))[3] = io_swap(in_blk[0])
84
85 #define put_block(x) \
86 out_blk[3] = io_swap(((u4byte*)(x))[0]); \
87 out_blk[2] = io_swap(((u4byte*)(x))[1]); \
88 out_blk[1] = io_swap(((u4byte*)(x))[2]); \
89 out_blk[0] = io_swap(((u4byte*)(x))[3])
90
91 #define get_key(x,len) \
92 ((u4byte*)(x))[4] = ((u4byte*)(x))[5] = \
93 ((u4byte*)(x))[6] = ((u4byte*)(x))[7] = 0; \
94 switch((((len) + 63) / 64)) { \
95 case 2: \
96 ((u4byte*)(x))[0] = io_swap(in_key[3]); \
97 ((u4byte*)(x))[1] = io_swap(in_key[2]); \
98 ((u4byte*)(x))[2] = io_swap(in_key[1]); \
99 ((u4byte*)(x))[3] = io_swap(in_key[0]); \
100 break; \
101 case 3: \
102 ((u4byte*)(x))[0] = io_swap(in_key[5]); \
103 ((u4byte*)(x))[1] = io_swap(in_key[4]); \
104 ((u4byte*)(x))[2] = io_swap(in_key[3]); \
105 ((u4byte*)(x))[3] = io_swap(in_key[2]); \
106 ((u4byte*)(x))[4] = io_swap(in_key[1]); \
107 ((u4byte*)(x))[5] = io_swap(in_key[0]); \
108 break; \
109 case 4: \
110 ((u4byte*)(x))[0] = io_swap(in_key[7]); \
111 ((u4byte*)(x))[1] = io_swap(in_key[6]); \
112 ((u4byte*)(x))[2] = io_swap(in_key[5]); \
113 ((u4byte*)(x))[3] = io_swap(in_key[4]); \
114 ((u4byte*)(x))[4] = io_swap(in_key[3]); \
115 ((u4byte*)(x))[5] = io_swap(in_key[2]); \
116 ((u4byte*)(x))[6] = io_swap(in_key[1]); \
117 ((u4byte*)(x))[7] = io_swap(in_key[0]); \
118 }
119
120 #else
121
122 #define get_block(x) \
123 ((u4byte*)(x))[0] = io_swap(in_blk[0]); \
124 ((u4byte*)(x))[1] = io_swap(in_blk[1]); \
125 ((u4byte*)(x))[2] = io_swap(in_blk[2]); \
126 ((u4byte*)(x))[3] = io_swap(in_blk[3])
127
128 #define put_block(x) \
129 out_blk[0] = io_swap(((u4byte*)(x))[0]); \
130 out_blk[1] = io_swap(((u4byte*)(x))[1]); \
131 out_blk[2] = io_swap(((u4byte*)(x))[2]); \
132 out_blk[3] = io_swap(((u4byte*)(x))[3])
133
134 #define get_key(x,len) \
135 ((u4byte*)(x))[4] = ((u4byte*)(x))[5] = \
136 ((u4byte*)(x))[6] = ((u4byte*)(x))[7] = 0; \
137 switch((((len) + 63) / 64)) { \
138 case 4: \
139 ((u4byte*)(x))[6] = io_swap(in_key[6]); \
140 ((u4byte*)(x))[7] = io_swap(in_key[7]); \
141 case 3: \
142 ((u4byte*)(x))[4] = io_swap(in_key[4]); \
143 ((u4byte*)(x))[5] = io_swap(in_key[5]); \
144 case 2: \
145 ((u4byte*)(x))[0] = io_swap(in_key[0]); \
146 ((u4byte*)(x))[1] = io_swap(in_key[1]); \
147 ((u4byte*)(x))[2] = io_swap(in_key[2]); \
148 ((u4byte*)(x))[3] = io_swap(in_key[3]); \
149 }
150
151 #endif