Optimize code space and solve the Serpent issue (https://sourceforge.net/p/veracrypt/discussion/technical/thread/fb09633a/#6406) by removing key length parameter from serpent_set_key and twofish_set_key

This commit is contained in:
Mounir IDRASSI 2014-09-27 16:04:07 +02:00
parent 411e8599f3
commit 0178a6d33f
6 changed files with 16 additions and 20 deletions

View File

@ -119,11 +119,11 @@ int CipherInit (int cipher, unsigned char *key, unsigned __int8 *ks)
break; break;
case SERPENT: case SERPENT:
serpent_set_key (key, CipherGetKeySize(SERPENT) * 8, ks); serpent_set_key (key, ks);
break; break;
case TWOFISH: case TWOFISH:
twofish_set_key ((TwofishInstance *)ks, (const u4byte *)key, CipherGetKeySize(TWOFISH) * 8); twofish_set_key ((TwofishInstance *)ks, (const u4byte *)key);
break; break;
default: default:
@ -972,9 +972,9 @@ int EAInit (int ea, unsigned char *key, unsigned __int8 *ks)
return ERR_CIPHER_INIT_FAILURE; return ERR_CIPHER_INIT_FAILURE;
#elif defined (TC_WINDOWS_BOOT_SERPENT) #elif defined (TC_WINDOWS_BOOT_SERPENT)
serpent_set_key (key, 32 * 8, ks); serpent_set_key (key, ks);
#elif defined (TC_WINDOWS_BOOT_TWOFISH) #elif defined (TC_WINDOWS_BOOT_TWOFISH)
twofish_set_key ((TwofishInstance *)ks, (const u4byte *)key, 32 * 8); twofish_set_key ((TwofishInstance *)ks, (const u4byte *)key);
#endif #endif
return ERR_SUCCESS; return ERR_SUCCESS;
} }

View File

@ -630,19 +630,16 @@ static void KXf (const unsigned __int32 *k, unsigned int r, unsigned __int32 *a,
#ifndef TC_MINIMIZE_CODE_SIZE #ifndef TC_MINIMIZE_CODE_SIZE
void serpent_set_key(const unsigned __int8 userKey[], int keylen, unsigned __int8 *ks) void serpent_set_key(const unsigned __int8 userKey[],unsigned __int8 *ks)
{ {
unsigned __int32 a,b,c,d,e; unsigned __int32 a,b,c,d,e;
unsigned __int32 *k = (unsigned __int32 *)ks; unsigned __int32 *k = (unsigned __int32 *)ks;
unsigned __int32 t; unsigned __int32 t;
int i; int i;
for (i = 0; i < keylen / (int)sizeof(__int32); i++) for (i = 0; i < 8; i++)
k[i] = LE32(((unsigned __int32*)userKey)[i]); k[i] = LE32(((unsigned __int32*)userKey)[i]);
if (keylen < 32)
k[keylen/4] |= (unsigned __int32)1 << ((keylen%4)*8);
k += 8; k += 8;
t = k[-1]; t = k[-1];
for (i = 0; i < 132; ++i) for (i = 0; i < 132; ++i)
@ -694,19 +691,16 @@ static void SKf (unsigned __int32 *k, unsigned int r, unsigned __int32 *a, unsig
k[r + 7] = *d; k[r + 7] = *d;
} }
void serpent_set_key(const unsigned __int8 userKey[], int keylen, unsigned __int8 *ks) void serpent_set_key(const unsigned __int8 userKey[], unsigned __int8 *ks)
{ {
unsigned __int32 a,b,c,d,e; unsigned __int32 a,b,c,d,e;
unsigned __int32 *k = (unsigned __int32 *)ks; unsigned __int32 *k = (unsigned __int32 *)ks;
unsigned __int32 t; unsigned __int32 t;
int i; int i;
for (i = 0; i < keylen / (int)sizeof(__int32); i++) for (i = 0; i < 8; i++)
k[i] = LE32(((unsigned __int32*)userKey)[i]); k[i] = LE32(((unsigned __int32*)userKey)[i]);
if (keylen < 32)
k[keylen/4] |= (unsigned __int32)1 << ((keylen%4)*8);
k += 8; k += 8;
t = k[-1]; t = k[-1];
for (i = 0; i < 132; ++i) for (i = 0; i < 132; ++i)

View File

@ -8,7 +8,8 @@ extern "C"
{ {
#endif #endif
void serpent_set_key(const unsigned __int8 userKey[], int keylen, unsigned __int8 *ks); /* userKey is always 32-bytes long */
void serpent_set_key(const unsigned __int8 userKey[], unsigned __int8 *ks);
void serpent_encrypt(const unsigned __int8 *inBlock, unsigned __int8 *outBlock, unsigned __int8 *ks); void serpent_encrypt(const unsigned __int8 *inBlock, unsigned __int8 *outBlock, unsigned __int8 *ks);
void serpent_decrypt(const unsigned __int8 *inBlock, unsigned __int8 *outBlock, unsigned __int8 *ks); void serpent_decrypt(const unsigned __int8 *inBlock, unsigned __int8 *outBlock, unsigned __int8 *ks);

View File

@ -369,7 +369,7 @@ static u4byte mds_rem(u4byte p0, u4byte p1)
/* initialise the key schedule from the user supplied key */ /* initialise the key schedule from the user supplied key */
u4byte *twofish_set_key(TwofishInstance *instance, const u4byte in_key[], const u4byte key_len) u4byte *twofish_set_key(TwofishInstance *instance, const u4byte in_key[])
{ u4byte i, a, b, me_key[4], mo_key[4]; { u4byte i, a, b, me_key[4], mo_key[4];
u4byte *l_key, *s_key; u4byte *l_key, *s_key;
@ -390,7 +390,7 @@ u4byte *twofish_set_key(TwofishInstance *instance, const u4byte in_key[], const
} }
#endif #endif
instance->k_len = key_len / 64; /* 2, 3 or 4 */ instance->k_len = 4;
for(i = 0; i < instance->k_len; ++i) for(i = 0; i < instance->k_len; ++i)
{ {

View File

@ -44,7 +44,8 @@ typedef struct
#define TWOFISH_KS sizeof(TwofishInstance) #define TWOFISH_KS sizeof(TwofishInstance)
u4byte * twofish_set_key(TwofishInstance *instance, const u4byte in_key[], const u4byte key_len); /* in_key must be 32-bytes long */
u4byte * twofish_set_key(TwofishInstance *instance, const u4byte in_key[]);
void twofish_encrypt(TwofishInstance *instance, const u4byte in_blk[4], u4byte out_blk[]); void twofish_encrypt(TwofishInstance *instance, const u4byte in_blk[4], u4byte out_blk[]);
void twofish_decrypt(TwofishInstance *instance, const u4byte in_blk[4], u4byte out_blk[4]); void twofish_decrypt(TwofishInstance *instance, const u4byte in_blk[4], u4byte out_blk[4]);

View File

@ -211,7 +211,7 @@ namespace VeraCrypt
void CipherSerpent::SetCipherKey (const byte *key) void CipherSerpent::SetCipherKey (const byte *key)
{ {
serpent_set_key (key, static_cast<int> (GetKeySize ()), ScheduledKey); serpent_set_key (key, ScheduledKey);
} }
@ -233,7 +233,7 @@ namespace VeraCrypt
void CipherTwofish::SetCipherKey (const byte *key) void CipherTwofish::SetCipherKey (const byte *key)
{ {
twofish_set_key ((TwofishInstance *) ScheduledKey.Ptr(), (unsigned int *) key, static_cast<int> (GetKeySize ()) * 8); twofish_set_key ((TwofishInstance *) ScheduledKey.Ptr(), (unsigned int *) key);
} }