📄 c code.txt
字号:
PHT( f, h );
PHT( a, e );
PHT( b, f );
PHT( c, g );
PHT( d, h );
/* Swap the data octets around. If we unrol the loop we can
eliminate this step through register renaming */
t = b; b = e; e = c; c = t; t = d; d = f; f = g; g = t;
key += 16;
}
/* Perform the final mixed xor/byte addition output transformation
using K2r + 1*/
data[ 0 ] = a ^ key[ 0 ];
data[ 1 ] = b + key[ 1 ];
data[ 2 ] = c + key[ 2 ];
data[ 3 ] = d ^ key[ 3 ];
data[ 4 ] = e ^ key[ 4 ];
data[ 5 ] = f + key[ 5 ];
data[ 6 ] = g + key[ 6 ];
data[ 7 ] = h ^ key[ 7 ];
}
/* Decrypt a block of data with SAFER */
void saferDecryptBlock( BYTE *data, BYTE *key )
{
BYTE a, b, c, d, e, f, g, h, t;
int rounds = *key;
/* Since we're now running throught the algorithm backwards, we move to
the end of the key and start from there */
key += SAFER_BLOCKSIZE * ( 1 + 2 * rounds );
/* Perform the initial mixed xor/byte addition input transformation
using K2r+1 */
a = data[ 0 ] ^ key[ -7 ];
b = data[ 1 ] - key[ -6 ];
c = data[ 2 ] - key[ -5 ];
d = data[ 3 ] ^ key[ -4 ];
e = data[ 4 ] ^ key[ -3 ];
f = data[ 5 ] - key[ -2 ];
g = data[ 6 ] - key[ -1 ];
h = data[ 7 ] ^ key[ 0 ];
key -= 8;
while( rounds-- )
{
/* Swap the data octets around. If we unrol the loop we can
eliminate this step through register renaming */
t = e; e = b; b = c; c = t; t = f; f = d; d = g; g = t;
/* Perform the inverse Pseudo-Hadamard Trasform of the round input.
If we were implementing this in assembly language we should
interleave the order of the two operations in the PHT with those
of the following PHT to reduce pipeline stalls, but for the C
version we rely on the compiler to pick this optimization up */
IPHT( a, e );
IPHT( b, f );
IPHT( c, g );
IPHT( d, h );
IPHT( a, c );
IPHT( e, g );
IPHT( b, d );
IPHT( f, h );
IPHT( a, b );
IPHT( c, d );
IPHT( e, f );
IPHT( g, h );
/* Perform the mixed xor/byte addition of the inverse PHT output with
the subkey K2r+2-2i, combined with the second level of the
nonlinear layer, either 45^n mod 257 or log45n, and finally the
mixed xor/byte addition of the round output with K2r+1-2i */
h = LOG( ( h - key[ 0 ] ) & 0xFF ) ^ key[ -8 ];
g = EXP( ( g ^ key[ -1 ] ) & 0xFF ) - key[ -9 ];
f = EXP( ( f ^ key[ -2 ] ) & 0xFF ) - key[ -10 ];
e = LOG( ( e - key[ -3 ] ) & 0xFF ) ^ key[ -11 ];
d = LOG( ( d - key[ -4 ] ) & 0xFF ) ^ key[ -12 ];
c = EXP( ( c ^ key[ -5 ] ) & 0xFF ) - key[ -13 ];
b = EXP( ( b ^ key[ -6 ] ) & 0xFF ) - key[ -14 ];
a = LOG( ( a - key[ -7 ] ) & 0xFF ) ^ key[ -15 ];
key -= 16;
}
data[ 0 ] = a;
data[ 1 ] = b;
data[ 2 ] = c;
data[ 3 ] = d;
data[ 4 ] = e;
data[ 5 ] = f;
data[ 6 ] = g;
data[ 7 ] = h;
}
#ifdef TEST_SAFER
/* CFB-mode en/decryption routines */
void saferEncryptCFB( BYTE *buffer, int noBytes, BYTE *iv, int ivLen, BYTE *key )
{
BYTE currentIV[ SAFER_BLOCKSIZE ];
/* Set up the initial IV */
memset( currentIV, 0, SAFER_BLOCKSIZE );
memcpy( currentIV, iv, ivLen );
while( noBytes )
{
int ivCount = ( noBytes > SAFER_BLOCKSIZE ) ? SAFER_BLOCKSIZE : noBytes;
int i;
/* Encrypt the IV */
saferEncryptBlock( currentIV, key );
/* XOR the buffer contents with the encrypted IV */
for( i = 0; i < ivCount; i++ )
buffer[ i ] ^= currentIV[ i ];
/* Shift the ciphertext into the IV */
memcpy( currentIV, buffer, ivCount );
/* Move on to next block of data */
noBytes -= ivCount;
buffer += ivCount;
}
}
void saferDecryptCFB( BYTE *buffer, int noBytes, BYTE *iv, int ivLen, BYTE *key )
{
BYTE currentIV[ SAFER_BLOCKSIZE ], temp[ SAFER_BLOCKSIZE ];
/* Set up the initial IV */
memset( currentIV, 0, SAFER_BLOCKSIZE );
memcpy( currentIV, iv, ivLen );
while( noBytes )
{
int ivCount = ( noBytes > SAFER_BLOCKSIZE ) ? SAFER_BLOCKSIZE : noBytes;
int i;
/* Encrypt the IV */
saferEncryptBlock( currentIV, key );
/* Save the ciphertext */
memcpy( temp, buffer, ivCount );
/* XOR the buffer contents with the encrypted IV */
for( i = 0; i < ivCount; i++ )
buffer[ i ] ^= currentIV[ i ];
/* Shift the ciphertext into the IV */
memcpy( currentIV, temp, ivCount );
/* Move on to next block of data */
noBytes -= ivCount;
buffer += ivCount;
}
}
/* Test code */
#include <stdio.h>
#include <time.h>
void _saferEncryptBlock( BYTE *data, BYTE *key );
void _saferDecryptBlock( BYTE *data, BYTE *key );
void test8051( void )
{
BYTE key[] = { 0x03,
0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
0x10, 0x26, 0x8B, 0x5B, 0x46, 0xBE, 0xA8,
0xFD, 0xC6, 0x09, 0x81, 0x67, 0xD9, 0xB4,
0x7B, 0x8E, 0x88, 0xB9, 0xC4, 0xAF, 0xC5,
0x20, 0x1A, 0xC7, 0x3B, 0x99, 0x3A, 0x18,
0xAD, 0xE5, 0x35, 0x8C, 0x5B, 0xC9, 0xEA,
0x99, 0x5C, 0x8D, 0xF9, 0x1B, 0xF8, 0xA2,
0x1C, 0x65, 0x61, 0xFB, 0xB6, 0xF3, 0x0C };
BYTE plainText[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88 };
BYTE cipherText[] = { 0xE9, 0xB8, 0xC0, 0xEA, 0xBC, 0x10, 0x99, 0xA1 };
BYTE plainString[] = "This is a test string which is longer than the blocksize...";
BYTE cipherString[] = { 0xC1, 0x5B, 0x63, 0xE9, 0xA8, 0x15, 0x74, 0x19,
0x88, 0x1B, 0xB6, 0xE4, 0xF3, 0x32, 0x93, 0xC2,
0x6F, 0x81, 0x94, 0xA7, 0x69, 0xA6, 0x92, 0x05,
0x54, 0xA1, 0x8E, 0x3B, 0xF2, 0x56, 0x28, 0xFF,
0x12, 0x02, 0xAB, 0x66, 0xC6, 0xC5, 0xF0, 0xEC,
0x65, 0x82, 0xD0, 0x29, 0x92, 0x2B, 0x26, 0x52,
0x96, 0x83, 0xB1, 0x21, 0x53, 0x09, 0x02, 0x67,
0xD9, 0x6B, 0x2E };
BYTE cfbIV[] = "1234";
BYTE data[ SAFER_BLOCKSIZE ];
BYTE cfbData[ 100 ];
int length = sizeof( plainString ) - 1;
memcpy( data, plainText, SAFER_BLOCKSIZE );
saferEncryptBlock( data, key );
if( memcmp( data, cipherText, SAFER_BLOCKSIZE ) )
puts( "Bang" );
saferDecryptBlock( data, key );
memcpy( cfbData, plainString, length );
saferEncryptCFB( cfbData, sizeof( cfbData ) - 1,
cfbIV, sizeof( cfbIV ) - 1, key );
if( memcmp( cfbData, cipherString, length ) )
puts( "Bang" );
saferDecryptCFB( cfbData, sizeof( cfbData ) - 1,
cfbIV, sizeof( cfbIV ) - 1, key );
}
void main( void )
{
BYTE key1[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };
BYTE plainText1[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
BYTE cipherText1[] = { 0x41, 0x4C, 0x54, 0x5A, 0xB6, 0x99, 0x4A, 0xF7 };
BYTE key2[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
BYTE plainText2[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
BYTE cipherText2[] = { 0xFF, 0x78, 0x11, 0xE4, 0xB3, 0xA7, 0x2E, 0x71 };
BYTE key3[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
BYTE plainText3[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
BYTE cipherText3[] = { 0x49, 0xC9, 0x9D, 0x98, 0xA5, 0xBC, 0x59, 0x08 };
BYTE data[ SAFER_BLOCKSIZE ], key[ SAFER_KEYLEN ];
BYTE cfbData[] = "This is a test string which is longer than the blocksize";
BYTE cfbIV[] = "1234";
time_t secondCount;
long i;
saferExpandKey( key, key1, SAFER_SK128_ROUNDS, TRUE );
memcpy( data, plainText1, SAFER_BLOCKSIZE );
saferEncryptBlock( data, key );
memcpy( data, plainText1, SAFER_BLOCKSIZE );
saferEncryptBlock( data, key );
if( memcmp( data, cipherText1, SAFER_BLOCKSIZE ) )
puts( "Bang" );
saferDecryptBlock( data, key );
saferExpandKey( key, key2, SAFER_SK128_ROUNDS, TRUE );
memcpy( data, plainText2, SAFER_BLOCKSIZE );
saferEncryptBlock( data, key );
if( memcmp( data, cipherText2, SAFER_BLOCKSIZE ) )
puts( "Bang" );
saferDecryptBlock( data, key );
saferExpandKey( key, key3, SAFER_SK128_ROUNDS, TRUE );
memcpy( data, plainText3, SAFER_BLOCKSIZE );
saferEncryptBlock( data, key );
if( memcmp( data, cipherText3, SAFER_BLOCKSIZE ) )
puts( "Bang" );
saferDecryptBlock( data, key );
/* CFB mode test */
saferExpandKey( key, key1, SAFER_SK128_ROUNDS, TRUE );
saferEncryptCFB( cfbData, sizeof( cfbData ) - 1,
cfbIV, sizeof( cfbIV ) - 1, key );
saferDecryptCFB( cfbData, sizeof( cfbData ) - 1,
cfbIV, sizeof( cfbIV ) - 1, key );
test8051();
/* Speed test */
puts( "Encrypting 5MB of data" );
secondCount = time( NULL );
for( i = 0; i < 625000L; i++ )
saferEncryptBlock( data, key );
secondCount = time( NULL ) - secondCount;
printf( "Time = %ld seconds, %ld kbytes/second\n", \
secondCount, 8 * 625L / secondCount );
}
#endif /* TEST_SAFER */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -