プログラミングの作業に何の価値も見出せなくなってしまったd金魚による日記 このページをアンテナに追加 RSSフィード

 iTunes Music Store(Japan) なかのひと あわせて読みたいブログパーツ
|

0001 | 00 |
2004 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2005 | 01 | 02 | 03 | 04 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2006 | 00 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2007 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 |
2008 | 01 | 02 | 03 | 05 | 07 | 08 | 09 | 10 | 11 | 12 |
2009 | 01 | 02 | 03 |
2010 | 03 | 04 | 06 | 07 | 09 | 10 | 11 |
2011 | 01 | 02 | 10 |
2012 | 04 |
2013 | 01 | 05 | 06 | 07 | 08 | 10 |
2014 | 02 | 03 | 05 | 09 |
2015 | 04 |
2016 | 09 | 11 | 12 |
はてな一覧
アンテナに追加
私のアンテナ
私のダイアリー
私のアーカイブ
私のアイデア
私のブックマーク
私のグループ
私のキーワード
ニュース系、今まで続いているシリーズモノの読み物
dKingyo Utility Toolkit Projectのリリース情報
やっぱり暗号化は大人の味(笑)
プログラムのパッキング方法を調べよ
ココが厳しいよMinGW
ライブラリアン通信
ゲームプログラミングどうしよう
CRCについて
ビット演算練習
d金魚の今更Ajax
Windows Tips
VC6 Tips
Win32 WTL Tips
Ruby for C++ User
Ruby Tips
今日のRubyで嵌った事
正規表現PIECE
書きかけ
続く・・・

私のダイアリーの人気記事
新しくブックマークされた記事


あまり、役に立たなそうな個人的に調べた情報や妄想に耽った事、今 勉強している事ヒソヒソと公開していたりします。 | 登録してくれている方々 | d金魚にメール | 当サイトは640x480の画面解像度に対応しています。
日記へのリンク、アンリンクはフリーですが、selfタグのついている部分のコンテンツの引用はご遠慮願います。ご協力よろしくお願いします。


 | 

2006-04-04 Camellia 実装記 part3

[][]やっとCamelliaのC言語の実装が完成・・・ やっとCamelliaのC言語の実装が完成・・・を含むブックマーク やっとCamelliaのC言語の実装が完成・・・のブックマークコメント

前回の記事:(http://d.hatena.ne.jp/studiokingyo/20050928

問答無用NYSLだ。持ってけドロボー

注意:

/*!
@file dkcCamellia.c
@brief 政府が推奨している国産暗号Camellia
@note
The original code and all modifications are in the public domain.<br>
This is the original introductory comment:<br>
<PRE>
// camellia.cpp - by Kevin Springle, 2003
// This code is hereby placed in the public domain.
</PRE>
This code is hereby placed in NYSL(public domain)
 */

#include "dkcCamellia.h"


static const uint8 s1[256]={
  0x70,0x82,0x2c,0xec,0xb3,0x27,0xc0,0xe5,0xe4,0x85,0x57,0x35,0xea,0x0c,0xae,0x41,
  0x23,0xef,0x6b,0x93,0x45,0x19,0xa5,0x21,0xed,0x0e,0x4f,0x4e,0x1d,0x65,0x92,0xbd,
  0x86,0xb8,0xaf,0x8f,0x7c,0xeb,0x1f,0xce,0x3e,0x30,0xdc,0x5f,0x5e,0xc5,0x0b,0x1a,
  0xa6,0xe1,0x39,0xca,0xd5,0x47,0x5d,0x3d,0xd9,0x01,0x5a,0xd6,0x51,0x56,0x6c,0x4d,
  0x8b,0x0d,0x9a,0x66,0xfb,0xcc,0xb0,0x2d,0x74,0x12,0x2b,0x20,0xf0,0xb1,0x84,0x99,
  0xdf,0x4c,0xcb,0xc2,0x34,0x7e,0x76,0x05,0x6d,0xb7,0xa9,0x31,0xd1,0x17,0x04,0xd7,
  0x14,0x58,0x3a,0x61,0xde,0x1b,0x11,0x1c,0x32,0x0f,0x9c,0x16,0x53,0x18,0xf2,0x22,
  0xfe,0x44,0xcf,0xb2,0xc3,0xb5,0x7a,0x91,0x24,0x08,0xe8,0xa8,0x60,0xfc,0x69,0x50,
  0xaa,0xd0,0xa0,0x7d,0xa1,0x89,0x62,0x97,0x54,0x5b,0x1e,0x95,0xe0,0xff,0x64,0xd2,
  0x10,0xc4,0x00,0x48,0xa3,0xf7,0x75,0xdb,0x8a,0x03,0xe6,0xda,0x09,0x3f,0xdd,0x94,
  0x87,0x5c,0x83,0x02,0xcd,0x4a,0x90,0x33,0x73,0x67,0xf6,0xf3,0x9d,0x7f,0xbf,0xe2,
  0x52,0x9b,0xd8,0x26,0xc8,0x37,0xc6,0x3b,0x81,0x96,0x6f,0x4b,0x13,0xbe,0x63,0x2e,
  0xe9,0x79,0xa7,0x8c,0x9f,0x6e,0xbc,0x8e,0x29,0xf5,0xf9,0xb6,0x2f,0xfd,0xb4,0x59,
  0x78,0x98,0x06,0x6a,0xe7,0x46,0x71,0xba,0xd4,0x25,0xab,0x42,0x88,0xa2,0x8d,0xfa,
  0x72,0x07,0xb9,0x55,0xf8,0xee,0xac,0x0a,0x36,0x49,0x2a,0x68,0x3c,0x38,0xf1,0xa4,
  0x40,0x28,0xd3,0x7b,0xbb,0xc9,0x43,0xc1,0x15,0xe3,0xad,0xf4,0x77,0xc7,0x80,0x9e
  };
static const uint8 s2[256]={
  0xe0,0x05,0x58,0xd9,0x67,0x4e,0x81,0xcb,0xc9,0x0b,0xae,0x6a,0xd5,0x18,0x5d,0x82,
  0x46,0xdf,0xd6,0x27,0x8a,0x32,0x4b,0x42,0xdb,0x1c,0x9e,0x9c,0x3a,0xca,0x25,0x7b,
  0x0d,0x71,0x5f,0x1f,0xf8,0xd7,0x3e,0x9d,0x7c,0x60,0xb9,0xbe,0xbc,0x8b,0x16,0x34,
  0x4d,0xc3,0x72,0x95,0xab,0x8e,0xba,0x7a,0xb3,0x02,0xb4,0xad,0xa2,0xac,0xd8,0x9a,
  0x17,0x1a,0x35,0xcc,0xf7,0x99,0x61,0x5a,0xe8,0x24,0x56,0x40,0xe1,0x63,0x09,0x33,
  0xbf,0x98,0x97,0x85,0x68,0xfc,0xec,0x0a,0xda,0x6f,0x53,0x62,0xa3,0x2e,0x08,0xaf,
  0x28,0xb0,0x74,0xc2,0xbd,0x36,0x22,0x38,0x64,0x1e,0x39,0x2c,0xa6,0x30,0xe5,0x44,
  0xfd,0x88,0x9f,0x65,0x87,0x6b,0xf4,0x23,0x48,0x10,0xd1,0x51,0xc0,0xf9,0xd2,0xa0,
  0x55,0xa1,0x41,0xfa,0x43,0x13,0xc4,0x2f,0xa8,0xb6,0x3c,0x2b,0xc1,0xff,0xc8,0xa5,
  0x20,0x89,0x00,0x90,0x47,0xef,0xea,0xb7,0x15,0x06,0xcd,0xb5,0x12,0x7e,0xbb,0x29,
  0x0f,0xb8,0x07,0x04,0x9b,0x94,0x21,0x66,0xe6,0xce,0xed,0xe7,0x3b,0xfe,0x7f,0xc5,
  0xa4,0x37,0xb1,0x4c,0x91,0x6e,0x8d,0x76,0x03,0x2d,0xde,0x96,0x26,0x7d,0xc6,0x5c,
  0xd3,0xf2,0x4f,0x19,0x3f,0xdc,0x79,0x1d,0x52,0xeb,0xf3,0x6d,0x5e,0xfb,0x69,0xb2,
  0xf0,0x31,0x0c,0xd4,0xcf,0x8c,0xe2,0x75,0xa9,0x4a,0x57,0x84,0x11,0x45,0x1b,0xf5,
  0xe4,0x0e,0x73,0xaa,0xf1,0xdd,0x59,0x14,0x6c,0x92,0x54,0xd0,0x78,0x70,0xe3,0x49,
  0x80,0x50,0xa7,0xf6,0x77,0x93,0x86,0x83,0x2a,0xc7,0x5b,0xe9,0xee,0x8f,0x01,0x3d
  };
static const uint8 s3[256]={
  0x38,0x41,0x16,0x76,0xd9,0x93,0x60,0xf2,0x72,0xc2,0xab,0x9a,0x75,0x06,0x57,0xa0,
  0x91,0xf7,0xb5,0xc9,0xa2,0x8c,0xd2,0x90,0xf6,0x07,0xa7,0x27,0x8e,0xb2,0x49,0xde,
  0x43,0x5c,0xd7,0xc7,0x3e,0xf5,0x8f,0x67,0x1f,0x18,0x6e,0xaf,0x2f,0xe2,0x85,0x0d,
  0x53,0xf0,0x9c,0x65,0xea,0xa3,0xae,0x9e,0xec,0x80,0x2d,0x6b,0xa8,0x2b,0x36,0xa6,
  0xc5,0x86,0x4d,0x33,0xfd,0x66,0x58,0x96,0x3a,0x09,0x95,0x10,0x78,0xd8,0x42,0xcc,
  0xef,0x26,0xe5,0x61,0x1a,0x3f,0x3b,0x82,0xb6,0xdb,0xd4,0x98,0xe8,0x8b,0x02,0xeb,
  0x0a,0x2c,0x1d,0xb0,0x6f,0x8d,0x88,0x0e,0x19,0x87,0x4e,0x0b,0xa9,0x0c,0x79,0x11,
  0x7f,0x22,0xe7,0x59,0xe1,0xda,0x3d,0xc8,0x12,0x04,0x74,0x54,0x30,0x7e,0xb4,0x28,
  0x55,0x68,0x50,0xbe,0xd0,0xc4,0x31,0xcb,0x2a,0xad,0x0f,0xca,0x70,0xff,0x32,0x69,
  0x08,0x62,0x00,0x24,0xd1,0xfb,0xba,0xed,0x45,0x81,0x73,0x6d,0x84,0x9f,0xee,0x4a,
  0xc3,0x2e,0xc1,0x01,0xe6,0x25,0x48,0x99,0xb9,0xb3,0x7b,0xf9,0xce,0xbf,0xdf,0x71,
  0x29,0xcd,0x6c,0x13,0x64,0x9b,0x63,0x9d,0xc0,0x4b,0xb7,0xa5,0x89,0x5f,0xb1,0x17,
  0xf4,0xbc,0xd3,0x46,0xcf,0x37,0x5e,0x47,0x94,0xfa,0xfc,0x5b,0x97,0xfe,0x5a,0xac,
  0x3c,0x4c,0x03,0x35,0xf3,0x23,0xb8,0x5d,0x6a,0x92,0xd5,0x21,0x44,0x51,0xc6,0x7d,
  0x39,0x83,0xdc,0xaa,0x7c,0x77,0x56,0x05,0x1b,0xa4,0x15,0x34,0x1e,0x1c,0xf8,0x52,
  0x20,0x14,0xe9,0xbd,0xdd,0xe4,0xa1,0xe0,0x8a,0xf1,0xd6,0x7a,0xbb,0xe3,0x40,0x4f
  };
static const uint8 s4[256]={
  0x70,0x2c,0xb3,0xc0,0xe4,0x57,0xea,0xae,0x23,0x6b,0x45,0xa5,0xed,0x4f,0x1d,0x92,
  0x86,0xaf,0x7c,0x1f,0x3e,0xdc,0x5e,0x0b,0xa6,0x39,0xd5,0x5d,0xd9,0x5a,0x51,0x6c,
  0x8b,0x9a,0xfb,0xb0,0x74,0x2b,0xf0,0x84,0xdf,0xcb,0x34,0x76,0x6d,0xa9,0xd1,0x04,
  0x14,0x3a,0xde,0x11,0x32,0x9c,0x53,0xf2,0xfe,0xcf,0xc3,0x7a,0x24,0xe8,0x60,0x69,
  0xaa,0xa0,0xa1,0x62,0x54,0x1e,0xe0,0x64,0x10,0x00,0xa3,0x75,0x8a,0xe6,0x09,0xdd,
  0x87,0x83,0xcd,0x90,0x73,0xf6,0x9d,0xbf,0x52,0xd8,0xc8,0xc6,0x81,0x6f,0x13,0x63,
  0xe9,0xa7,0x9f,0xbc,0x29,0xf9,0x2f,0xb4,0x78,0x06,0xe7,0x71,0xd4,0xab,0x88,0x8d,
  0x72,0xb9,0xf8,0xac,0x36,0x2a,0x3c,0xf1,0x40,0xd3,0xbb,0x43,0x15,0xad,0x77,0x80,
  0x82,0xec,0x27,0xe5,0x85,0x35,0x0c,0x41,0xef,0x93,0x19,0x21,0x0e,0x4e,0x65,0xbd,
  0xb8,0x8f,0xeb,0xce,0x30,0x5f,0xc5,0x1a,0xe1,0xca,0x47,0x3d,0x01,0xd6,0x56,0x4d,
  0x0d,0x66,0xcc,0x2d,0x12,0x20,0xb1,0x99,0x4c,0xc2,0x7e,0x05,0xb7,0x31,0x17,0xd7,
  0x58,0x61,0x1b,0x1c,0x0f,0x16,0x18,0x22,0x44,0xb2,0xb5,0x91,0x08,0xa8,0xfc,0x50,
  0xd0,0x7d,0x89,0x97,0x5b,0x95,0xff,0xd2,0xc4,0x48,0xf7,0xdb,0x03,0xda,0x3f,0x94,
  0x5c,0x02,0x4a,0x33,0x67,0xf3,0x7f,0xe2,0x9b,0x26,0x37,0x3b,0x96,0x4b,0xbe,0x2e,
  0x79,0x8c,0x6e,0x8e,0xf5,0xb6,0xfd,0x59,0x98,0x6a,0x46,0xba,0x25,0x42,0xa2,0xfa,
  0x07,0x55,0xee,0x0a,0x49,0x68,0x38,0xa4,0x28,0x7b,0xc9,0xc1,0xe3,0xf4,0xc7,0x9e
  };



// Define internal Camellia function macros



static DKC_INLINE void free_key(DKC_CAMELLIA *p)
{
	if(p->m_key){
		free(p->m_key);
		p->m_key = NULL;
	}
}
static DKC_INLINE BOOL alloc_key(DKC_CAMELLIA *p,size_t s)
{
	free_key(p);
	p->m_key = (uint8 *)malloc(s);
	if(NULL==p->m_key) return FALSE;
	return TRUE;
}

static DKC_INLINE uint32 rotlFixed32(uint32 x, unsigned int y)
{
	assert(y < sizeof(uint32)*8);
	return (x<<y) | (x>>(sizeof(uint32)*8-y));
}

static DKC_INLINE debug_get_byte(uint32 x,uint32 y){
	uint32 z;
	z = (uint32)(uint8)( (x)>>(8*(y)));
	return z;
}
#define GETBYTE debug_get_byte
static DKC_INLINE uint64 camellia_F(uint64 X)
{
	uint32 t1 = (uint32)(X >> 32);
	uint32 t2 = (uint32)(X & 0xFFFFFFFFL);
	t2=	(s2[GETBYTE(t2, 3)] << 24) |
		(s3[GETBYTE(t2, 2)] << 16) |
		(s4[GETBYTE(t2, 1)] <<	8) |
		(s1[GETBYTE(t2, 0)]);
	t1=	(s1[GETBYTE(t1, 3)] << 24) |
		(s2[GETBYTE(t1, 2)] << 16) |
		(s3[GETBYTE(t1, 1)] <<	8) |
		(s4[GETBYTE(t1, 0)]);
	t1 ^= rotlFixed32(t2, 8);
	t2 ^= rotlFixed32(t1, 16);
	t1 ^= rotlFixed32(t2, 24);
	t2 ^= rotlFixed32(t1, 24);
	return ( (uint64)t2 << 32) | (uint64)t1;
}

#define ROUND2(Xp, K1, K2) {\
	(Xp)[1] ^= camellia_F( (Xp)[0] ^ K1);\
	(Xp)[0] ^= camellia_F( (Xp)[1] ^ K2); \
}

#define W64LIT dkcmUINT64DEFINE


static DKC_INLINE void camellia_FLlayer(uint64 *x, uint64 K1, uint64 K2)
{
	uint32 Xl = (uint32)(x[0] >> 32);
	uint32 Xr = (uint32)(x[0] & 0xFFFFFFFFL);
	Xr ^= rotlFixed32(Xl & (uint32)(K1 >> 32), 1);
	Xl ^= (Xr | (uint32)(K1 & 0xFFFFFFFFL));
    x[0] = ( (uint64)Xl << 32) | (uint64)Xr;

	Xl = (uint32)(x[1] >> 32);
	Xr = (uint32)(x[1] & 0xFFFFFFFFL);
	Xl ^= (Xr | (uint32)(K2 & 0xFFFFFFFFL));
	Xr ^= rotlFixed32(Xl & (uint32)(K2 >> 32), 1);
    x[1] = ( (uint64)Xl << 32) | (uint64)Xr;
}

static DKC_INLINE void rotl128(uint64 *x, unsigned int bits)
{
	uint64 temp = x[0] >> (64 - bits);
	x[0] = (x[0] << bits) | (x[1] >> (64 - bits));
	x[1] = (x[1] << bits) | temp;
}


//unsigned int m_rounds;
//SecBlock<word64> m_key;
//uint64 m_key[256];


//#define CAMELLIA_NON_REVERSE

#ifdef ORDER_ABCD //big endian
static DKC_INLINE void get_user_key(uint64 *d,const uint8 *s,size_t size){
	memcpy(d,s,size);
}
#define CAMELLIA_UINT64_REVERSE(x) (x)
#else
#define CAMELLIA_UINT64_REVERSE(x) (x = dkcmREVERSE_ENDIAN64(x))

static DKC_INLINE void get_user_key(uint64 *d,const uint8 *s,size_t size){
	size_t i;
	size_t t = size / sizeof(uint64);
	memcpy(d,s,size);
	for(i=0;i<t;i++)
	{
		CAMELLIA_UINT64_REVERSE(d[i]);
	}
}

#endif

#ifdef CAMELLIA_NON_REVERSE
#	undef CAMELLIA_UINT64_REVERSE
#define CAMELLIA_UINT64_REVERSE(x) (x)
#endif

static int camellia_UncheckedSetKey(DKC_CAMELLIA *p, const uint8 *key, unsigned int keylen,BOOL isDecryption)
{
	//AssertValidKeyLength(keylen);
	
	unsigned int kslen;
	uint64 *ks;
	//FixedSizeSecBlock<uint64, 32> keyuint;
	uint64 keyuint[8];
	uint64 *kw;

	


	p->m_rounds = (keylen >= 24) ? 4 : 3;
	kslen = (8 * p->m_rounds + 2);

	kw = keyuint;
	
	if(FALSE==alloc_key(p,kslen * 8))
		return edk_OutOfMemory;

	//m_key.New(8 * kslen);

	ks = (uint64 *)p->m_key;
#define KL	(kw+0)
#define KR	(kw+2)
#define KA	(kw+4)
#define KB	(kw+6)

	if (keylen == 16)
	{
		//GetUserKey(BIG_ENDIAN_ORDER, kw, 2, key, keylen);
		get_user_key(kw,key,keylen);
		//memcpy(kw,key,keylen);
		KA[0] = KL[0];
		KA[1] = KL[1];
	}
	else
	{
		if (keylen == 24)
		{
			//GetUserKey(BIG_ENDIAN_ORDER, kw, 3, key, keylen);
			get_user_key(kw,key,keylen);
			KR[1] = ~KR[0];
		}
		else if(keylen == 32)
		{
			//GetUserKey(BIG_ENDIAN_ORDER, kw, 4, key, keylen);
			get_user_key(kw,key,keylen);
		}else{
			return edk_ArgumentException;
		}
		KA[0] = KL[0] ^ KR[0];
		KA[1] = KL[1] ^ KR[1];
	}

	
	ROUND2(KA, W64LIT(0xA09E667F3BCC908B), W64LIT(0xB67AE8584CAA73B2));
	KA[0] ^= KL[0];
	KA[1] ^= KL[1];
	ROUND2(KA, W64LIT(0xC6EF372FE94F82BE), W64LIT(0x54FF53A5F1D36F1C));

	if (keylen == 16)
	{
		ks[0] = KL[0]; ks[1] = KL[1];
		rotl128(KL, 15);
		ks[4] = KL[0]; ks[5] = KL[1];
		rotl128(KL, 30);
		ks[10] = KL[0]; ks[11] = KL[1];
		rotl128(KL, 15);
		ks[13] = KL[1];
		rotl128(KL, 17);
		ks[16] = KL[0]; ks[17] = KL[1];
		rotl128(KL, 17);
		ks[18] = KL[0]; ks[19] = KL[1];
		rotl128(KL, 17);
		ks[22] = KL[0]; ks[23] = KL[1];

		ks[2] = KA[0]; ks[3] = KA[1];
		rotl128(KA, 15);
		ks[6] = KA[0]; ks[7] = KA[1];
		rotl128(KA, 15);
		ks[8] = KA[0]; ks[9] = KA[1];
		rotl128(KA, 15);
		ks[12] = KA[0];
		rotl128(KA, 15);
		ks[14] = KA[0]; ks[15] = KA[1];
		rotl128(KA, 34);
		ks[20] = KA[0]; ks[21] = KA[1];
		rotl128(KA, 17);
		ks[24] = KA[0]; ks[25] = KA[1];
	}
	else
	{
		KB[0] = KA[0] ^ KR[0];
		KB[1] = KA[1] ^ KR[1];
		ROUND2(KB, W64LIT(0x10E527FADE682D1D), W64LIT(0xB05688C2B3E6C1FD));

		ks[0] = KL[0]; ks[1] = KL[1];
		rotl128(KL, 45);
		ks[12] = KL[0]; ks[13] = KL[1];
		rotl128(KL, 15);
		ks[16] = KL[0]; ks[17] = KL[1];
		rotl128(KL, 17);
		ks[22] = KL[0]; ks[23] = KL[1];
		rotl128(KL, 34);
		ks[30] = KL[0]; ks[31] = KL[1];

		rotl128(KR, 15);
		ks[4] = KR[0]; ks[5] = KR[1];
		rotl128(KR, 15);
		ks[8] = KR[0]; ks[9] = KR[1];
		rotl128(KR, 30);
		ks[18] = KR[0]; ks[19] = KR[1];
		rotl128(KR, 34);
		ks[26] = KR[0]; ks[27] = KR[1];

		rotl128(KA, 15);
		ks[6] = KA[0]; ks[7] = KA[1];
		rotl128(KA, 30);
		ks[14] = KA[0]; ks[15] = KA[1];
		rotl128(KA, 32);
		ks[24] = KA[0]; ks[25] = KA[1];
		rotl128(KA, 17);
		ks[28] = KA[0]; ks[29] = KA[1];

		ks[2] = KB[0]; ks[3] = KB[1];
		rotl128(KB, 30);
		ks[10] = KB[0]; ks[11] = KB[1];
		rotl128(KB, 30);
		ks[20] = KB[0]; ks[21] = KB[1];
		rotl128(KB, 51);
		ks[32] = KB[0]; ks[33] = KB[1];
	}

	if (TRUE == isDecryption)	// reverse key schedule order
	{
		unsigned int i;
		SWAP_TEMP(uint64,ks[0], ks[kslen-2]);
		SWAP_TEMP(uint64,ks[1], ks[kslen-1]);
		for (i=2; i<kslen/2; i++)
		{
			SWAP_TEMP(uint64,ks[i], ks[kslen-1-i]);
		}
	}
	//dkcSaveBinary(ks,kslen * 8,"camellia_key");
	return edk_SUCCEEDED;
}

//typedef BlockGetAndPut<uint64, BigEndian> Block;



static DKC_INLINE void camellia_ProcessAndXorBlock(DKC_CAMELLIA *p,const uint8 *inBlock, uint8 *outBlock)
{
	//FixedSizeSecBlock<uint64, 16> mb;
	uint8 mb[16];
	uint64 *m = (uint64 *)mb;
	const uint64 *ks = (const uint64 *)p->m_key;
	unsigned int i;

	//Block::Get(inBlock)(m[0])(m[1]); endian change
#if 0
	for(i=0;i<16;i++)
	{
		mb[15-i] = inBlock[i] ;
	}
#else
	memcpy(m,inBlock,16);
	CAMELLIA_UINT64_REVERSE(m[0]);
	CAMELLIA_UINT64_REVERSE(m[1]);//todo endian check
#endif

	m[0] ^= ks[0];
	m[1] ^= ks[1];
	ks += 2;
	for (i = p->m_rounds; i > 0; --i)
	{
		ROUND2(m, ks[0], ks[1]);
		ROUND2(m, ks[2], ks[3]);
		ROUND2(m, ks[4], ks[5]);
		if (i != 1)
		{
			camellia_FLlayer(m, ks[6], ks[7]);
			ks += 8;
		}
		else
		{
			m[0] ^= ks[7];
			m[1] ^= ks[6];
		}
	}

	//endian change
	//Block::Put(xorBlock, outBlock)(m[1])(m[0]);
	//CAMELLIA_UINT64_REVERSE(m[0]);
	//CAMELLIA_UINT64_REVERSE(m[1]);
	for(i=0;i<16;i++)
	{
		outBlock[i] = mb[15-i];
	}
	//printf("a");
}


int WINAPI dkcCamelliaKeyGenerate(DKC_CAMELLIA *p,const uint8 *key,size_t keysize,BOOL isDecryption)
{
	p->keysize = keysize;
	p->isDecryption = (dkctBOOL)(isDecryption != 0);
	//return camellia_key_generate(p->msbox,key,keysize);
	return camellia_UncheckedSetKey(p,key,keysize,isDecryption);
}

DKC_INLINE DKC_CAMELLIA * WINAPI dkcAllocCamellia(const uint8 *key,size_t keysize,BOOL isDecryption)
{
	DKC_CAMELLIA *p = (DKC_CAMELLIA *)dkcAllocate(sizeof(DKC_CAMELLIA));
	if(NULL==p) return NULL;

	if(DKUTIL_FAILED(dkcCamelliaKeyGenerate(p,key,keysize,isDecryption)))
	{
		dkcFree( (void **)p);
		return NULL;
	}
	return p;
}

DKC_INLINE int WINAPI dkcFreeCamellia(DKC_CAMELLIA **pp)
{
	if(NULL==pp || NULL==*pp){
		return edk_ArgumentException;//ArugmentExcetpion;
	}
	free_key( (*pp));
	return dkcFree( (void **)pp);
}

DKC_INLINE int WINAPI dkcCamelliaEncryptNoDest(DKC_CAMELLIA *p,
	uint8 *dest_and_src,size_t dest_and_srcsize)
{

	int r;
	uint8 *dest = (uint8 *)dkcAllocate(dest_and_srcsize);
	if(NULL==dest) return edk_OutOfMemory;

	r = dkcCamelliaEncrypt(p,dest,dest_and_srcsize,dest_and_src,dest_and_srcsize);
	memcpy(dest_and_src,dest,dest_and_srcsize);
	dkcFree( (void **)&dest);
	return r;
}

DKC_INLINE int WINAPI dkcCamelliaEncrypt(
	DKC_CAMELLIA *p,uint8 *dest,size_t dsize,const uint8 *src,size_t ssize)
{
	if(dsize < ssize || p->isDecryption){
		return edk_ArgumentException;
	}

	camellia_ProcessAndXorBlock(p,src,dest);
	return edk_SUCCEEDED;

}

DKC_INLINE int WINAPI dkcCamelliaDecrypt(
	DKC_CAMELLIA *p,uint8 *dest,size_t dsize,const uint8 *src,size_t ssize)
{
	if(dsize < ssize || !p->isDecryption){
		return edk_ArgumentException;
	}
	camellia_ProcessAndXorBlock(p,src,dest);
	return edk_SUCCEEDED;

}
トラックバック - http://d.hatena.ne.jp/studiokingyo/20060404
 | 
Program | Debug | dKingyo Utility Toolkit | library | D言語 | 御本とか | 備忘録 | テクニック | WayBack | 格言 | 英語 | 他力本願 | news | software |

デースケドガー