73
73
#include <stdlib.h>
74
74
#include <string.h>
77
typedef unsigned char uint8_t;
78
typedef unsigned int uint32_t;
79
typedef unsigned __int64 uint64_t;
76
typedef unsigned char u8;
77
typedef unsigned int u32;
91
83
#if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64))
92
84
# define SWAP(x) ( _lrotl(x, 8) & 0x00ff00ff | _lrotr(x, 8) & 0xff00ff00 )
93
# define GETU32(p) SWAP(*((uint32_t *)(p)))
94
# define PUTU32(ct, st) { *((uint32_t *)(ct)) = SWAP((st)); }
85
# define GETU32(p) SWAP(*((u32 *)(p)))
86
# define PUTU32(ct, st) { *((u32 *)(ct)) = SWAP((st)); }
95
87
# define CAMELLIA_SWAP4(x) (x = ( _lrotl(x, 8) & 0x00ff00ff | _lrotr(x, 8) & 0xff00ff00) )
98
89
#else /* not windows */
99
# define GETU32(pt) (((uint32_t)(pt)[0] << 24) \
100
^ ((uint32_t)(pt)[1] << 16) \
101
^ ((uint32_t)(pt)[2] << 8) \
102
^ ((uint32_t)(pt)[3]))
104
# define PUTU32(ct, st) { (ct)[0] = (uint8_t)((st) >> 24); \
105
(ct)[1] = (uint8_t)((st) >> 16); \
106
(ct)[2] = (uint8_t)((st) >> 8); \
107
(ct)[3] = (uint8_t)(st); }
110
#if (defined (__GNUC__) && !defined(i386))
90
# define GETU32(pt) (((u32)(pt)[0] << 24) \
91
^ ((u32)(pt)[1] << 16) \
92
^ ((u32)(pt)[2] << 8) \
95
# define PUTU32(ct, st) { (ct)[0] = (u8)((st) >> 24); \
96
(ct)[1] = (u8)((st) >> 16); \
97
(ct)[2] = (u8)((st) >> 8); \
100
#if (defined (__GNUC__) && (defined(__x86_64__) || defined(__x86_64)))
111
101
#define CAMELLIA_SWAP4(x) \
113
103
asm("bswap %1" : "+r" (x));\
116
106
#define CAMELLIA_SWAP4(x) \
118
x = ((uint32_t)x << 16) + ((uint32_t)x >> 16);\
119
x = (((uint32_t)x & 0xff00ff) << 8) + (((uint32_t)x >> 8) & 0xff00ff);\
108
x = ((u32)x << 16) + ((u32)x >> 16);\
109
x = (((u32)x & 0xff00ff) << 8) + (((u32)x >> 8) & 0xff00ff);\
122
#else /* big endian */
123
#define CAMELLIA_SWAP4(x)
124
#endif /* L_ENDIAN */
127
114
#define COPY4WORD(dst, src) \
164
void camellia_setup128(const unsigned char *key, uint32_t *subkey);
165
void camellia_setup192(const unsigned char *key, uint32_t *subkey);
166
void camellia_setup256(const unsigned char *key, uint32_t *subkey);
151
void camellia_setup128(const u8 *key, u32 *subkey);
152
void camellia_setup192(const u8 *key, u32 *subkey);
153
void camellia_setup256(const u8 *key, u32 *subkey);
168
void camellia_encrypt128(const uint32_t *subkey, uint32_t *io);
169
void camellia_decrypt128(const uint32_t *subkey, uint32_t *io);
170
void camellia_encrypt256(const uint32_t *subkey, uint32_t *io);
171
void camellia_decrypt256(const uint32_t *subkey, uint32_t *io);
155
void camellia_encrypt128(const u32 *subkey, u32 *io);
156
void camellia_decrypt128(const u32 *subkey, u32 *io);
157
void camellia_encrypt256(const u32 *subkey, u32 *io);
158
void camellia_decrypt256(const u32 *subkey, u32 *io);
173
160
#ifdef __cplusplus