# DO NOT DELETE THIS LINE -- make depend depends on it.
-camellia.o: ../../include/openssl/opensslconf.h camellia.c camellia.h
-camellia.o: cmll_locl.h
-cmll_cbc.o: ../../include/openssl/camellia.h
+camellia.o: ../../include/openssl/e_os2.h ../../include/openssl/opensslconf.h
+camellia.o: camellia.c camellia.h cmll_locl.h
+cmll_cbc.o: ../../include/openssl/camellia.h ../../include/openssl/e_os2.h
cmll_cbc.o: ../../include/openssl/opensslconf.h cmll_cbc.c cmll_locl.h
cmll_cfb.o: ../../e_os.h ../../include/openssl/camellia.h
cmll_cfb.o: ../../include/openssl/e_os2.h ../../include/openssl/opensslconf.h
cmll_cfb.o: cmll_cfb.c cmll_locl.h
-cmll_ctr.o: ../../include/openssl/camellia.h
+cmll_ctr.o: ../../include/openssl/camellia.h ../../include/openssl/e_os2.h
cmll_ctr.o: ../../include/openssl/opensslconf.h cmll_ctr.c cmll_locl.h
-cmll_ecb.o: ../../include/openssl/camellia.h
+cmll_ecb.o: ../../include/openssl/camellia.h ../../include/openssl/e_os2.h
cmll_ecb.o: ../../include/openssl/opensslconf.h cmll_ecb.c cmll_locl.h
-cmll_misc.o: ../../include/openssl/camellia.h
+cmll_misc.o: ../../include/openssl/camellia.h ../../include/openssl/e_os2.h
cmll_misc.o: ../../include/openssl/opensslconf.h
cmll_misc.o: ../../include/openssl/opensslv.h cmll_locl.h cmll_misc.c
-cmll_ofb.o: ../../include/openssl/camellia.h
+cmll_ofb.o: ../../include/openssl/camellia.h ../../include/openssl/e_os2.h
cmll_ofb.o: ../../include/openssl/opensslconf.h cmll_locl.h cmll_ofb.c
#include "camellia.h"
#include "cmll_locl.h"
-/*
- * must be defined uint32_t
- */
-
/* key constants */
-
#define CAMELLIA_SIGMA1L (0xA09E667FL)
#define CAMELLIA_SIGMA1R (0x3BCC908BL)
#define CAMELLIA_SIGMA2L (0xB67AE858L)
*/
/* e is pointer of subkey */
-#ifdef L_ENDIAN
-
-#define CamelliaSubkeyL(INDEX) (subkey[(INDEX)*2 + 1])
-#define CamelliaSubkeyR(INDEX) (subkey[(INDEX)*2])
-
-#else /* big endian */
-
#define CamelliaSubkeyL(INDEX) (subkey[(INDEX)*2])
#define CamelliaSubkeyR(INDEX) (subkey[(INDEX)*2 + 1])
-#endif /* IS_LITTLE_ENDIAN */
-
/* rotation right shift 1byte */
#define CAMELLIA_RR8(x) (((x) >> 8) + ((x) << 24))
/* rotation left shift 1bit */
* for speed up
*
*/
-#if !defined(_MSC_VER)
-
-#define CAMELLIA_FLS(ll, lr, rl, rr, kll, klr, krl, krr, t0, t1, t2, t3) \
-do \
- { \
- t0 = kll; \
- t2 = krr; \
- t0 &= ll; \
- t2 |= rr; \
- rl ^= t2; \
- lr ^= CAMELLIA_RL1(t0); \
- t3 = krl; \
- t1 = klr; \
- t3 &= rl; \
- t1 |= lr; \
- ll ^= t1; \
- rr ^= CAMELLIA_RL1(t3); \
- } while(0)
-
-#define CAMELLIA_ROUNDSM(xl, xr, kl, kr, yl, yr, il, ir, t0, t1) \
-do \
- { \
- ir = CAMELLIA_SP1110(xr & 0xff); \
- il = CAMELLIA_SP1110((xl>>24) & 0xff); \
- ir ^= CAMELLIA_SP0222((xr>>24) & 0xff); \
- il ^= CAMELLIA_SP0222((xl>>16) & 0xff); \
- ir ^= CAMELLIA_SP3033((xr>>16) & 0xff); \
- il ^= CAMELLIA_SP3033((xl>>8) & 0xff); \
- ir ^= CAMELLIA_SP4404((xr>>8) & 0xff); \
- il ^= CAMELLIA_SP4404(xl & 0xff); \
- il ^= kl; \
- ir ^= il ^ kr; \
- yl ^= ir; \
- yr ^= CAMELLIA_RR8(il) ^ ir; \
- } while(0)
-
-#else /* for MS-VC */
-
#define CAMELLIA_FLS(ll, lr, rl, rr, kll, klr, krl, krr, t0, t1, t2, t3) \
do \
{ \
yl ^= ir; \
yr ^= il; \
} while(0)
-#endif
-static const uint32_t camellia_sp1110[256] =
+static const u32 camellia_sp1110[256] =
{
0x70707000,0x82828200,0x2c2c2c00,0xececec00,
0xb3b3b300,0x27272700,0xc0c0c000,0xe5e5e500,
0x77777700,0xc7c7c700,0x80808000,0x9e9e9e00,
};
-static const uint32_t camellia_sp0222[256] =
+static const u32 camellia_sp0222[256] =
{
0x00e0e0e0,0x00050505,0x00585858,0x00d9d9d9,
0x00676767,0x004e4e4e,0x00818181,0x00cbcbcb,
0x00eeeeee,0x008f8f8f,0x00010101,0x003d3d3d,
};
-static const uint32_t camellia_sp3033[256] =
+static const u32 camellia_sp3033[256] =
{
0x38003838,0x41004141,0x16001616,0x76007676,
0xd900d9d9,0x93009393,0x60006060,0xf200f2f2,
0xbb00bbbb,0xe300e3e3,0x40004040,0x4f004f4f,
};
-static const uint32_t camellia_sp4404[256] =
+static const u32 camellia_sp4404[256] =
{
0x70700070,0x2c2c002c,0xb3b300b3,0xc0c000c0,
0xe4e400e4,0x57570057,0xeaea00ea,0xaeae00ae,
0xe3e300e3,0xf4f400f4,0xc7c700c7,0x9e9e009e,
};
-
/**
* Stuff related to the Camellia key schedule
*/
#define subl(x) subL[(x)]
#define subr(x) subR[(x)]
-void camellia_setup128(const unsigned char *key, uint32_t *subkey)
+void camellia_setup128(const u8 *key, u32 *subkey)
{
- uint32_t kll, klr, krl, krr;
- uint32_t il, ir, t0, t1, w0, w1;
- uint32_t kw4l, kw4r, dw, tl, tr;
- uint32_t subL[26];
- uint32_t subR[26];
+ u32 kll, klr, krl, krr;
+ u32 il, ir, t0, t1, w0, w1;
+ u32 kw4l, kw4r, dw, tl, tr;
+ u32 subL[26];
+ u32 subR[26];
/**
* k == kll || klr || krl || krr (|| is concatination)
return;
}
-void camellia_setup256(const unsigned char *key, uint32_t *subkey)
+void camellia_setup256(const u8 *key, u32 *subkey)
{
- uint32_t kll,klr,krl,krr; /* left half of key */
- uint32_t krll,krlr,krrl,krrr; /* right half of key */
- uint32_t il, ir, t0, t1, w0, w1; /* temporary variables */
- uint32_t kw4l, kw4r, dw, tl, tr;
- uint32_t subL[34];
- uint32_t subR[34];
+ u32 kll,klr,krl,krr; /* left half of key */
+ u32 krll,krlr,krrl,krrr; /* right half of key */
+ u32 il, ir, t0, t1, w0, w1; /* temporary variables */
+ u32 kw4l, kw4r, dw, tl, tr;
+ u32 subL[34];
+ u32 subR[34];
/**
* key = (kll || klr || krl || krr || krll || krlr || krrl || krrr)
return;
}
-void camellia_setup192(const unsigned char *key, uint32_t *subkey)
+void camellia_setup192(const u8 *key, u32 *subkey)
{
- unsigned char kk[32];
- uint32_t krll, krlr, krrl,krrr;
+ u8 kk[32];
+ u32 krll, krlr, krrl,krrr;
memcpy(kk, key, 24);
- memcpy((unsigned char *)&krll, key+16,4);
- memcpy((unsigned char *)&krlr, key+20,4);
+ memcpy((u8 *)&krll, key+16,4);
+ memcpy((u8 *)&krlr, key+20,4);
krrl = ~krll;
krrr = ~krlr;
- memcpy(kk+24, (unsigned char *)&krrl, 4);
- memcpy(kk+28, (unsigned char *)&krrr, 4);
+ memcpy(kk+24, (u8 *)&krrl, 4);
+ memcpy(kk+28, (u8 *)&krrr, 4);
camellia_setup256(kk, subkey);
return;
}
/**
* Stuff related to camellia encryption/decryption
*/
-void camellia_encrypt128(const uint32_t *subkey, uint32_t *io)
+void camellia_encrypt128(const u32 *subkey, u32 *io)
{
- uint32_t il, ir, t0, t1;
+ u32 il, ir, t0, t1;
- SWAP4WORD(io);
/* pre whitening but absorb kw2*/
io[0] ^= CamelliaSubkeyL(0);
io[1] ^= CamelliaSubkeyR(0);
io[1] = io[3];
io[2] = t0;
io[3] = t1;
- SWAP4WORD(io);
-
+
return;
}
-void camellia_decrypt128(const uint32_t *subkey, uint32_t *io)
+void camellia_decrypt128(const u32 *subkey, u32 *io)
{
- uint32_t il,ir,t0,t1; /* temporary valiables */
-
- SWAP4WORD(io);
+ u32 il,ir,t0,t1; /* temporary valiables */
/* pre whitening but absorb kw2*/
io[0] ^= CamelliaSubkeyL(24);
io[1] = io[3];
io[2] = t0;
io[3] = t1;
- SWAP4WORD(io);
return;
}
/**
* stuff for 192 and 256bit encryption/decryption
*/
-void camellia_encrypt256(const uint32_t *subkey, uint32_t *io)
+void camellia_encrypt256(const u32 *subkey, u32 *io)
{
- uint32_t il,ir,t0,t1; /* temporary valiables */
-
- SWAP4WORD(io);
+ u32 il,ir,t0,t1; /* temporary valiables */
/* pre whitening but absorb kw2*/
io[0] ^= CamelliaSubkeyL(0);
io[1] = io[3];
io[2] = t0;
io[3] = t1;
- SWAP4WORD(io);
return;
}
-void camellia_decrypt256(const uint32_t *subkey, uint32_t *io)
+void camellia_decrypt256(const u32 *subkey, u32 *io)
{
- uint32_t il,ir,t0,t1; /* temporary valiables */
+ u32 il,ir,t0,t1; /* temporary valiables */
- SWAP4WORD(io);
/* pre whitening but absorb kw2*/
io[0] ^= CamelliaSubkeyL(32);
io[1] ^= CamelliaSubkeyR(32);
io[1] = io[3];
io[2] = t0;
io[3] = t1;
- SWAP4WORD(io);
return;
}
unsigned long len = length;
unsigned char tmp[CAMELLIA_BLOCK_SIZE];
const unsigned char *iv = ivec;
- uint32_t t32[UNITSIZE];
+ u32 t32[UNITSIZE];
+ const union { long one; char little; } camellia_endian = {1};
assert(in && out && key && ivec);
{
while (len >= CAMELLIA_BLOCK_SIZE)
{
- XOR4WORD2((uint32_t *)out,
- (uint32_t *)in, (uint32_t *)iv);
- key->enc(key->rd_key, (uint32_t *)out);
+ XOR4WORD2((u32 *)out,
+ (u32 *)in, (u32 *)iv);
+ if (camellia_endian.little)
+ SWAP4WORD((u32 *)out);
+ key->enc(key->rd_key, (u32 *)out);
+ if (camellia_endian.little)
+ SWAP4WORD((u32 *)out);
iv = out;
len -= CAMELLIA_BLOCK_SIZE;
in += CAMELLIA_BLOCK_SIZE;
out[n] = in[n] ^ iv[n];
for(n=len; n < CAMELLIA_BLOCK_SIZE; ++n)
out[n] = iv[n];
- key->enc(key->rd_key, (uint32_t *)out);
+ if (camellia_endian.little)
+ SWAP4WORD((u32 *)out);
+ key->enc(key->rd_key, (u32 *)out);
+ if (camellia_endian.little)
+ SWAP4WORD((u32 *)out);
iv = out;
}
memcpy(ivec,iv,CAMELLIA_BLOCK_SIZE);
while (len >= CAMELLIA_BLOCK_SIZE)
{
memcpy(out,in,CAMELLIA_BLOCK_SIZE);
- key->dec(key->rd_key,(uint32_t *)out);
- XOR4WORD((uint32_t *)out, (uint32_t *)iv);
+ if (camellia_endian.little)
+ SWAP4WORD((u32 *)out);
+ key->dec(key->rd_key,(u32 *)out);
+ if (camellia_endian.little)
+ SWAP4WORD((u32 *)out);
+ XOR4WORD((u32 *)out, (u32 *)iv);
iv = in;
len -= CAMELLIA_BLOCK_SIZE;
in += CAMELLIA_BLOCK_SIZE;
if (len)
{
memcpy(tmp, in, CAMELLIA_BLOCK_SIZE);
- key->dec(key->rd_key, (uint32_t *)tmp);
+ if (camellia_endian.little)
+ SWAP4WORD((u32 *)tmp);
+ key->dec(key->rd_key, (u32 *)tmp);
+ if (camellia_endian.little)
+ SWAP4WORD((u32 *)tmp);
for(n=0; n < len; ++n)
out[n] = tmp[n] ^ iv[n];
iv = in;
while (len >= CAMELLIA_BLOCK_SIZE)
{
memcpy(tmp, in, CAMELLIA_BLOCK_SIZE);
- key->dec(key->rd_key, (uint32_t *)out);
- XOR4WORD((uint32_t *)out, (uint32_t *)ivec);
+ if (camellia_endian.little)
+ SWAP4WORD((u32 *)out);
+ key->dec(key->rd_key, (u32 *)out);
+ if (camellia_endian.little)
+ SWAP4WORD((u32 *)out);
+ XOR4WORD((u32 *)out, (u32 *)ivec);
memcpy(ivec, tmp, CAMELLIA_BLOCK_SIZE);
len -= CAMELLIA_BLOCK_SIZE;
in += CAMELLIA_BLOCK_SIZE;
if (len)
{
memcpy(tmp, in, CAMELLIA_BLOCK_SIZE);
- key->dec(key->rd_key,(uint32_t *)out);
+ if (camellia_endian.little)
+ SWAP4WORD((u32 *)out);
+ key->dec(key->rd_key,(u32 *)out);
+ if (camellia_endian.little)
+ SWAP4WORD((u32 *)out);
for(n=0; n < len; ++n)
out[n] ^= ivec[n];
for(n=len; n < CAMELLIA_BLOCK_SIZE; ++n)
for(n=0; n < CAMELLIA_BLOCK_SIZE; ++n)
out[n] = in[n] ^ iv[n];
memcpy(t32, out, CAMELLIA_BLOCK_SIZE);
+ if (camellia_endian.little)
+ SWAP4WORD(t32);
key->enc(key->rd_key, t32);
+ if (camellia_endian.little)
+ SWAP4WORD(t32);
memcpy(out, t32, CAMELLIA_BLOCK_SIZE);
iv = out;
len -= CAMELLIA_BLOCK_SIZE;
out[n] = in[n] ^ iv[n];
for(n=len; n < CAMELLIA_BLOCK_SIZE; ++n)
out[n] = iv[n];
- key->enc(key->rd_key, (uint32_t *)out);
+ if (camellia_endian.little)
+ SWAP4WORD((u32 *)out);
+ key->enc(key->rd_key, (u32 *)out);
+ if (camellia_endian.little)
+ SWAP4WORD((u32 *)out);
iv = out;
}
memcpy(ivec,iv,CAMELLIA_BLOCK_SIZE);
while (len >= CAMELLIA_BLOCK_SIZE)
{
memcpy(t32,in,CAMELLIA_BLOCK_SIZE);
+ if (camellia_endian.little)
+ SWAP4WORD(t32);
key->dec(key->rd_key,t32);
+ if (camellia_endian.little)
+ SWAP4WORD(t32);
memcpy(out,t32,CAMELLIA_BLOCK_SIZE);
for(n=0; n < CAMELLIA_BLOCK_SIZE; ++n)
out[n] ^= iv[n];
{
memcpy(tmp, in, CAMELLIA_BLOCK_SIZE);
memcpy(t32, in, CAMELLIA_BLOCK_SIZE);
+ if (camellia_endian.little)
+ SWAP4WORD(t32);
key->dec(key->rd_key, t32);
+ if (camellia_endian.little)
+ SWAP4WORD(t32);
memcpy(out, t32, CAMELLIA_BLOCK_SIZE);
for(n=0; n < len; ++n)
out[n] = tmp[n] ^ iv[n];
{
memcpy(tmp, in, CAMELLIA_BLOCK_SIZE);
memcpy(t32, in, CAMELLIA_BLOCK_SIZE);
+ if (camellia_endian.little)
+ SWAP4WORD(t32);
key->dec(key->rd_key, t32);
+ if (camellia_endian.little)
+ SWAP4WORD(t32);
memcpy(out, t32, CAMELLIA_BLOCK_SIZE);
for(n=0; n < CAMELLIA_BLOCK_SIZE; ++n)
out[n] ^= ivec[n];
{
memcpy(tmp, in, CAMELLIA_BLOCK_SIZE);
memcpy(t32, in, CAMELLIA_BLOCK_SIZE);
+ if (camellia_endian.little)
+ SWAP4WORD(t32);
key->dec(key->rd_key,t32);
+ if (camellia_endian.little)
+ SWAP4WORD(t32);
memcpy(out, t32, CAMELLIA_BLOCK_SIZE);
for(n=0; n < len; ++n)
out[n] ^= ivec[n];
#include <stdlib.h>
#include <string.h>
-#if defined(_MSC_VER)
-typedef unsigned char uint8_t;
-typedef unsigned int uint32_t;
-typedef unsigned __int64 uint64_t;
-#else
-#include <inttypes.h>
-#endif
+typedef unsigned char u8;
+typedef unsigned int u32;
#ifdef __cplusplus
extern "C" {
#if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64))
# define SWAP(x) ( _lrotl(x, 8) & 0x00ff00ff | _lrotr(x, 8) & 0xff00ff00 )
-# define GETU32(p) SWAP(*((uint32_t *)(p)))
-# define PUTU32(ct, st) { *((uint32_t *)(ct)) = SWAP((st)); }
+# define GETU32(p) SWAP(*((u32 *)(p)))
+# define PUTU32(ct, st) { *((u32 *)(ct)) = SWAP((st)); }
# define CAMELLIA_SWAP4(x) (x = ( _lrotl(x, 8) & 0x00ff00ff | _lrotr(x, 8) & 0xff00ff00) )
-
#else /* not windows */
-# define GETU32(pt) (((uint32_t)(pt)[0] << 24) \
- ^ ((uint32_t)(pt)[1] << 16) \
- ^ ((uint32_t)(pt)[2] << 8) \
- ^ ((uint32_t)(pt)[3]))
-
-# define PUTU32(ct, st) { (ct)[0] = (uint8_t)((st) >> 24); \
- (ct)[1] = (uint8_t)((st) >> 16); \
- (ct)[2] = (uint8_t)((st) >> 8); \
- (ct)[3] = (uint8_t)(st); }
-
-#ifdef L_ENDIAN
-#if (defined (__GNUC__) && !defined(i386))
+# define GETU32(pt) (((u32)(pt)[0] << 24) \
+ ^ ((u32)(pt)[1] << 16) \
+ ^ ((u32)(pt)[2] << 8) \
+ ^ ((u32)(pt)[3]))
+
+# define PUTU32(ct, st) { (ct)[0] = (u8)((st) >> 24); \
+ (ct)[1] = (u8)((st) >> 16); \
+ (ct)[2] = (u8)((st) >> 8); \
+ (ct)[3] = (u8)(st); }
+
+#if (defined (__GNUC__) && (defined(__x86_64__) || defined(__x86_64)))
#define CAMELLIA_SWAP4(x) \
do{\
asm("bswap %1" : "+r" (x));\
}while(0)
-#else /* not gcc */
+#else
#define CAMELLIA_SWAP4(x) \
do{\
- x = ((uint32_t)x << 16) + ((uint32_t)x >> 16);\
- x = (((uint32_t)x & 0xff00ff) << 8) + (((uint32_t)x >> 8) & 0xff00ff);\
+ x = ((u32)x << 16) + ((u32)x >> 16);\
+ x = (((u32)x & 0xff00ff) << 8) + (((u32)x >> 8) & 0xff00ff);\
} while(0)
-#endif /* not gcc */
-#else /* big endian */
-#define CAMELLIA_SWAP4(x)
-#endif /* L_ENDIAN */
+#endif
#endif
#define COPY4WORD(dst, src) \
}while(0)
-void camellia_setup128(const unsigned char *key, uint32_t *subkey);
-void camellia_setup192(const unsigned char *key, uint32_t *subkey);
-void camellia_setup256(const unsigned char *key, uint32_t *subkey);
+void camellia_setup128(const u8 *key, u32 *subkey);
+void camellia_setup192(const u8 *key, u32 *subkey);
+void camellia_setup256(const u8 *key, u32 *subkey);
-void camellia_encrypt128(const uint32_t *subkey, uint32_t *io);
-void camellia_decrypt128(const uint32_t *subkey, uint32_t *io);
-void camellia_encrypt256(const uint32_t *subkey, uint32_t *io);
-void camellia_decrypt256(const uint32_t *subkey, uint32_t *io);
+void camellia_encrypt128(const u32 *subkey, u32 *io);
+void camellia_decrypt128(const u32 *subkey, u32 *io);
+void camellia_encrypt256(const u32 *subkey, u32 *io);
+void camellia_decrypt256(const u32 *subkey, u32 *io);
#ifdef __cplusplus
}
void Camellia_encrypt(const unsigned char *in, unsigned char *out,
const CAMELLIA_KEY *key)
{
- uint32_t tmp[UNITSIZE];
+ u32 tmp[UNITSIZE];
+ const union { long one; char little; } camellia_endian = {1};
memcpy(tmp, in, CAMELLIA_BLOCK_SIZE);
+ if (camellia_endian.little) SWAP4WORD(tmp);
key->enc(key->rd_key, tmp);
+ if (camellia_endian.little) SWAP4WORD(tmp);
memcpy(out, tmp, CAMELLIA_BLOCK_SIZE);
}
void Camellia_decrypt(const unsigned char *in, unsigned char *out,
const CAMELLIA_KEY *key)
{
- uint32_t tmp[UNITSIZE];
+ u32 tmp[UNITSIZE];
+ const union { long one; char little; } camellia_endian = {1};
memcpy(tmp, in, CAMELLIA_BLOCK_SIZE);
+ if (camellia_endian.little) SWAP4WORD(tmp);
key->dec(key->rd_key, tmp);
+ if (camellia_endian.little) SWAP4WORD(tmp);
memcpy(out, tmp, CAMELLIA_BLOCK_SIZE);
}