-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.h
More file actions
194 lines (158 loc) · 5.81 KB
/
config.h
File metadata and controls
194 lines (158 loc) · 5.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#ifndef BLOCK_CIPHER_CONFIG_H
#define BLOCK_CIPHER_CONFIG_H
/* Detecting compiler */
#if defined(__GNUC__)
# define BLKCPHR_ON_GNUC 1
# if defined(__clang__)
# define BLKCPHR_ON_CLANG 1
# else
# define BLKCPHR_ON_GCC 1
# endif
#elif defined(_MSC_VER)
# define BLKCPHR_ON_MSVC 1
#else
# error Unsupported compiler
#endif
/* Detecting endianness */
#if BLKCPHR_ON_GNUC
# if defined(__BYTE_ORDER__)
# if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
# define BLKCPHR_IS_LITTLE 1
# define BLKCPHR_IS_BIG 0
# elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
# define BLKCPHR_IS_LITTLE 0
# define BLKCPHR_IS_BIG 1
# else
# error Unknown endianness
# endif
# else
# error Not defined __BYTE_ORDER__
# endif
#elif BLKCPHR_ON_MSVC
# define BLKCPHR_IS_LITTLE 1
# define BLKCPHR_IS_BIG 0
#else
# error Unsupported compiler
#endif
/* Turning off warning "-Wlong-long" on GNUC */
#if BLKCPHR_ON_GNUC && __STDC_VERSION__ < 199901L
# define BLKCPHR_U64_WARN_BEGIN \
_Pragma("GCC diagnostic push") \
_Pragma("GCC diagnostic ignored \"-Wlong-long\"")
# define BLKCPHR_U64_WARN_END \
_Pragma("GCC diagnostic pop")
#else
# define BLKCPHR_U64_WARN_BEGIN
# define BLKCPHR_U64_WARN_END
#endif
/* Define fixed width integer types */
#if __STDC_VERSION__ >= 199901L
#include <stdint.h>
typedef uint8_t blkcphr_u8_t;
typedef uint16_t blkcphr_u16_t;
typedef uint32_t blkcphr_u32_t;
typedef uint64_t blkcphr_u64_t;
#else /* C89 */
#include <limits.h>
typedef unsigned char blkcphr_u8_t;
typedef unsigned short blkcphr_u16_t;
#if ULONG_MAX == 0xFFFFFFFFul
typedef unsigned long blkcphr_u32_t;
#else
typedef unsigned int blkcphr_u32_t;
#endif
#if BLKCPHR_ON_GNUC
BLKCPHR_U64_WARN_BEGIN
typedef unsigned long long blkcphr_u64_t;
BLKCPHR_U64_WARN_END
#elif BLKCPHR_ON_MSVC
typedef unsigned __int64 blkcphr_u64_t;
#else
# error Unsupported copmiler
#endif
#endif /* __STDC_VERSION__ >= 199901L */
/* Integer byte swap functions */
#if BLKCPHR_ON_GNUC
# define blkcphr_bswap16 __builtin_bswap16
# define blkcphr_bswap32 __builtin_bswap32
# define blkcphr_bswap64 __builtin_bswap64
#elif BLKCPHR_ON_MSVC
# include <stdlib.h>
# define blkcphr_bswap16 _byteswap_ushort
# define blkcphr_bswap32 _byteswap_ulong
# define blkcphr_bswap64 _byteswap_uint64
#else
# error Unsupported compiler
#endif
/* If condition for preprocessor */
#define BLKCPHR_CONCAT_(left, right) left ## right
#define BLKCPHR_CONCAT(left, right) BLKCPHR_CONCAT_(left, right)
#define BLKCPHR_IF_0(stmt)
#define BLKCPHR_IF_1(stmt) stmt
#define BLKCPHR_IF(cond, stmt) BLKCPHR_CONCAT(BLKCPHR_IF_, cond)(stmt)
#define BLKCPHR_IF_LITTLE(stmt) BLKCPHR_IF(BLKCPHR_IS_LITTLE, stmt)
#define BLKCPHR_IF_BIG(stmt) BLKCPHR_IF(BLKCPHR_IS_BIG, stmt)
/* Byte swapping one, pair and blocks */
#define BLKCPHR_BSWAP_B_STMT_1(bits, array) \
(array)[0] = blkcphr_bswap##bits((array)[0]);
#define BLKCPHR_BSWAP_B_STMT_2(bits, array) \
BLKCPHR_BSWAP_B_STMT_1(bits, array) \
(array)[1] = blkcphr_bswap##bits((array)[1]);
#define BLKCPHR_BSWAP_B_STMT_3(bits, array) \
BLKCPHR_BSWAP_B_STMT_2(bits, array) \
(array)[2] = blkcphr_bswap##bits((array)[2]);
#define BLKCPHR_BSWAP_B_STMT_4(bits, array) \
BLKCPHR_BSWAP_B_STMT_3(bits, array) \
(array)[3] = blkcphr_bswap##bits((array)[3]);
#define BLKCPHR_BSWAP_B_STMT_5(bits, array) \
BLKCPHR_BSWAP_B_STMT_4(bits, array) \
(array)[4] = blkcphr_bswap##bits((array)[4]);
#define BLKCPHR_BSWAP_B_STMT_6(bits, array) \
BLKCPHR_BSWAP_B_STMT_5(bits, array) \
(array)[5] = blkcphr_bswap##bits((array)[5]);
#define BLKCPHR_BSWAP_B_STMT_7(bits, array) \
BLKCPHR_BSWAP_B_STMT_6(bits, array) \
(array)[6] = blkcphr_bswap##bits((array)[6]);
#define BLKCPHR_BSWAP_B_STMT_8(bits, array) \
BLKCPHR_BSWAP_B_STMT_7(bits, array) \
(array)[7] = blkcphr_bswap##bits((array)[7]);
#define BLKCPHR_BSWAP_B_ONE(bits, value) do { \
(value) = blkcphr_bswap##bits(value); \
} while (0)
#define BLKCPHR_BSWAP_B_PAIR(bits, L, R) do { \
(L) = blkcphr_bswap##bits(L); \
(R) = blkcphr_bswap##bits(R); \
} while (0)
#define BLKCPHR_BSWAP_BxN(number, bits, array) do { \
BLKCPHR_BSWAP_B_STMT_##number(bits, array) \
} while (0)
#define BLKCPHR_BSWAP_16x4(array) BLKCPHR_BSWAP_BxN(4, 16, array)
#define BLKCPHR_BSWAP_16x8(array) BLKCPHR_BSWAP_BxN(8, 16, array)
#define BLKCPHR_BSWAP_32_ONE(value) BLKCPHR_BSWAP_B_ONE(32, value)
#define BLKCPHR_BSWAP_32_PAIR(L, R) BLKCPHR_BSWAP_B_PAIR(32, L, R)
#define BLKCPHR_BSWAP_32x4(array) BLKCPHR_BSWAP_BxN(4, 32, array)
#define BLKCPHR_BSWAP_32x6(array) BLKCPHR_BSWAP_BxN(6, 32, array)
#define BLKCPHR_BSWAP_32x8(array) BLKCPHR_BSWAP_BxN(8, 32, array)
#define BLKCPHR_BSWAP_64_ONE(value) BLKCPHR_BSWAP_B_ONE(64, value)
#define BLKCPHR_BSWAP_64_PAIR(L, R) BLKCPHR_BSWAP_B_PAIR(64, L, R)
#define BLKCPHR_BSWAP_64x2(array) BLKCPHR_BSWAP_BxN(2, 64, array)
#define BLKCPHR_BSWAP_64x3(array) BLKCPHR_BSWAP_BxN(3, 64, array)
#define BLKCPHR_BSWAP_64x4(array) BLKCPHR_BSWAP_BxN(4, 64, array)
/* Bit rotation functions */
#ifdef BLKCPHR_USE_ROTL32
static blkcphr_u32_t BLKCPHR_USE_ROTL32(blkcphr_u32_t n, blkcphr_u32_t s)
{ s &= 31; return n << s | n >> (-s & 31); }
#endif
#ifdef BLKCPHR_USE_ROTR32
static blkcphr_u32_t BLKCPHR_USE_ROTR32(blkcphr_u32_t n, blkcphr_u32_t s)
{ s &= 31; return n >> s | n << (-s & 31); }
#endif
#ifdef BLKCPHR_USE_ROTL64
static blkcphr_u64_t BLKCPHR_USE_ROTL64(blkcphr_u64_t n, blkcphr_u64_t s)
{ s &= 63; return n << s | n >> (-s & 63); }
#endif
#ifdef BLKCPHR_USE_ROTR64
static blkcphr_u64_t BLKCPHR_USE_ROTR64(blkcphr_u64_t n, blkcphr_u64_t s)
{ s &= 63; return n >> s | n << (-s & 63); }
#endif
#endif /* BLOCK_CIPHER_CONFIG_H */