262 lines
8.1 KiB
C
Raw Normal View History

2004-01-25 17:40:34 +00:00
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
*
* LibTomCrypt is a library that provides various cryptographic
* algorithms in a highly modular and flexible manner.
*
* The library is free for all purposes without any express
2004-05-12 20:42:16 +00:00
* guarantee it works.
2004-01-25 17:40:34 +00:00
*/
2004-12-30 23:55:53 +00:00
/**
@file xtea.c
2007-07-20 17:48:02 +00:00
Implementation of LTC_XTEA, Tom St Denis
2004-12-30 23:55:53 +00:00
*/
#include "tomcrypt.h"
2003-03-03 00:59:24 +00:00
2007-07-20 17:48:02 +00:00
#ifdef LTC_XTEA
2003-03-03 00:59:24 +00:00
2004-12-30 23:55:53 +00:00
const struct ltc_cipher_descriptor xtea_desc =
2003-03-03 00:59:24 +00:00
{
"xtea",
1,
16, 16, 8, 32,
&xtea_setup,
&xtea_ecb_encrypt,
&xtea_ecb_decrypt,
&xtea_test,
2005-04-17 11:37:13 +00:00
&xtea_done,
&xtea_keysize,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
2003-03-03 00:59:24 +00:00
};
int xtea_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
{
2014-01-20 12:05:58 +01:00
ulong32 x, sum, K[4];
2012-07-02 12:05:48 +02:00
2004-12-30 23:55:53 +00:00
LTC_ARGCHK(key != NULL);
LTC_ARGCHK(skey != NULL);
2003-03-03 00:59:24 +00:00
/* check arguments */
if (keylen != 16) {
return CRYPT_INVALID_KEYSIZE;
}
if (num_rounds != 0 && num_rounds != 32) {
return CRYPT_INVALID_ROUNDS;
}
/* load key */
LOAD32H(K[0], key+0);
LOAD32H(K[1], key+4);
LOAD32H(K[2], key+8);
LOAD32H(K[3], key+12);
2012-07-02 12:05:48 +02:00
2003-03-03 01:01:00 +00:00
for (x = sum = 0; x < 32; x++) {
skey->xtea.A[x] = (sum + K[sum&3]) & 0xFFFFFFFFUL;
sum = (sum + 0x9E3779B9UL) & 0xFFFFFFFFUL;
skey->xtea.B[x] = (sum + K[(sum>>11)&3]) & 0xFFFFFFFFUL;
}
2012-07-02 12:05:48 +02:00
2004-12-30 23:55:53 +00:00
#ifdef LTC_CLEAN_STACK
2003-03-03 01:01:00 +00:00
zeromem(&K, sizeof(K));
2012-07-02 12:05:48 +02:00
#endif
2003-03-03 00:59:24 +00:00
return CRYPT_OK;
}
2004-12-30 23:55:53 +00:00
/**
2007-07-20 17:48:02 +00:00
Encrypts a block of text with LTC_XTEA
2004-12-30 23:55:53 +00:00
@param pt The input plaintext (8 bytes)
@param ct The output ciphertext (8 bytes)
@param skey The key as scheduled
2005-11-18 05:15:37 +00:00
@return CRYPT_OK if successful
2004-12-30 23:55:53 +00:00
*/
2005-11-18 05:15:37 +00:00
int xtea_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
2003-03-03 00:59:24 +00:00
{
2014-01-20 12:05:58 +01:00
ulong32 y, z;
2003-03-03 00:59:24 +00:00
int r;
2004-12-30 23:55:53 +00:00
LTC_ARGCHK(pt != NULL);
LTC_ARGCHK(ct != NULL);
LTC_ARGCHK(skey != NULL);
2003-03-03 00:59:24 +00:00
LOAD32H(y, &pt[0]);
LOAD32H(z, &pt[4]);
2003-03-03 01:01:00 +00:00
for (r = 0; r < 32; r += 4) {
2004-12-30 23:55:53 +00:00
y = (y + ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r])) & 0xFFFFFFFFUL;
z = (z + ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r])) & 0xFFFFFFFFUL;
2003-03-03 01:01:00 +00:00
2004-12-30 23:55:53 +00:00
y = (y + ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r+1])) & 0xFFFFFFFFUL;
z = (z + ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r+1])) & 0xFFFFFFFFUL;
2003-03-03 01:01:00 +00:00
2004-12-30 23:55:53 +00:00
y = (y + ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r+2])) & 0xFFFFFFFFUL;
z = (z + ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r+2])) & 0xFFFFFFFFUL;
2003-03-03 01:01:00 +00:00
2004-12-30 23:55:53 +00:00
y = (y + ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r+3])) & 0xFFFFFFFFUL;
z = (z + ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r+3])) & 0xFFFFFFFFUL;
2003-03-03 00:59:24 +00:00
}
STORE32H(y, &ct[0]);
STORE32H(z, &ct[4]);
2005-11-18 05:15:37 +00:00
return CRYPT_OK;
2003-03-03 00:59:24 +00:00
}
2004-12-30 23:55:53 +00:00
/**
2007-07-20 17:48:02 +00:00
Decrypts a block of text with LTC_XTEA
2004-12-30 23:55:53 +00:00
@param ct The input ciphertext (8 bytes)
@param pt The output plaintext (8 bytes)
2012-07-02 12:05:48 +02:00
@param skey The key as scheduled
2005-11-18 05:15:37 +00:00
@return CRYPT_OK if successful
2004-12-30 23:55:53 +00:00
*/
2005-11-18 05:15:37 +00:00
int xtea_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
2003-03-03 00:59:24 +00:00
{
2014-01-20 12:05:58 +01:00
ulong32 y, z;
2003-03-03 00:59:24 +00:00
int r;
2004-12-30 23:55:53 +00:00
LTC_ARGCHK(pt != NULL);
LTC_ARGCHK(ct != NULL);
LTC_ARGCHK(skey != NULL);
2003-03-03 00:59:24 +00:00
LOAD32H(y, &ct[0]);
LOAD32H(z, &ct[4]);
2003-03-03 01:01:00 +00:00
for (r = 31; r >= 0; r -= 4) {
2004-12-30 23:55:53 +00:00
z = (z - ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r])) & 0xFFFFFFFFUL;
y = (y - ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r])) & 0xFFFFFFFFUL;
2003-03-03 01:01:00 +00:00
2004-12-30 23:55:53 +00:00
z = (z - ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r-1])) & 0xFFFFFFFFUL;
y = (y - ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r-1])) & 0xFFFFFFFFUL;
2003-03-03 01:01:00 +00:00
2004-12-30 23:55:53 +00:00
z = (z - ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r-2])) & 0xFFFFFFFFUL;
y = (y - ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r-2])) & 0xFFFFFFFFUL;
2003-03-03 01:01:00 +00:00
2004-12-30 23:55:53 +00:00
z = (z - ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r-3])) & 0xFFFFFFFFUL;
y = (y - ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r-3])) & 0xFFFFFFFFUL;
2003-03-03 00:59:24 +00:00
}
STORE32H(y, &pt[0]);
STORE32H(z, &pt[4]);
2005-11-18 05:15:37 +00:00
return CRYPT_OK;
2003-03-03 00:59:24 +00:00
}
2004-12-30 23:55:53 +00:00
/**
2007-07-20 17:48:02 +00:00
Performs a self-test of the LTC_XTEA block cipher
2004-12-30 23:55:53 +00:00
@return CRYPT_OK if functional, CRYPT_NOP if self-test has been disabled
*/
2003-03-03 00:59:24 +00:00
int xtea_test(void)
{
2003-06-01 18:55:11 +00:00
#ifndef LTC_TEST
return CRYPT_NOP;
2012-07-02 12:05:48 +02:00
#else
2012-07-02 15:10:55 +02:00
static const struct {
unsigned char key[16], pt[8], ct[8];
} tests[] = {
{
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0xde, 0xe9, 0xd4, 0xd8, 0xf7, 0x13, 0x1e, 0xd9 }
}, {
{ 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02,
0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04 },
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0xa5, 0x97, 0xab, 0x41, 0x76, 0x01, 0x4d, 0x72 }
}, {
{ 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04,
0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x06 },
{ 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02 },
{ 0xb1, 0xfd, 0x5d, 0xa9, 0xcc, 0x6d, 0xc9, 0xdc }
}, {
{ 0x78, 0x69, 0x5a, 0x4b, 0x3c, 0x2d, 0x1e, 0x0f,
0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87 },
{ 0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87 },
{ 0x70, 0x4b, 0x31, 0x34, 0x47, 0x44, 0xdf, 0xab }
}, {
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
{ 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
{ 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 }
}, {
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
{ 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
{ 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 }
}, {
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
{ 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f },
{ 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }
}, {
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
{ 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 }
}, {
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
{ 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d }
}, {
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 },
{ 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }
}
};
2003-03-03 00:59:24 +00:00
unsigned char tmp[2][8];
symmetric_key skey;
2012-07-02 15:10:55 +02:00
int i, err, y;
for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
zeromem(&skey, sizeof(skey));
if ((err = xtea_setup(tests[i].key, 16, 0, &skey)) != CRYPT_OK) {
return err;
}
xtea_ecb_encrypt(tests[i].pt, tmp[0], &skey);
xtea_ecb_decrypt(tmp[0], tmp[1], &skey);
if (compare_testvector(tmp[0], 8, tests[i].ct, 8, "XTEA Encrypt", i) != 0 ||
compare_testvector(tmp[1], 8, tests[i].pt, 8, "XTEA Decrypt", i) != 0) {
2012-07-02 15:10:55 +02:00
return CRYPT_FAIL_TESTVECTOR;
}
2003-03-03 00:59:24 +00:00
2003-12-24 18:59:57 +00:00
/* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */
for (y = 0; y < 8; y++) tmp[0][y] = 0;
for (y = 0; y < 1000; y++) xtea_ecb_encrypt(tmp[0], tmp[0], &skey);
for (y = 0; y < 1000; y++) xtea_ecb_decrypt(tmp[0], tmp[0], &skey);
for (y = 0; y < 8; y++) if (tmp[0][y] != 0) return CRYPT_FAIL_TESTVECTOR;
2012-07-02 15:10:55 +02:00
} /* for */
2003-12-24 18:59:57 +00:00
2003-03-03 00:59:24 +00:00
return CRYPT_OK;
2003-06-01 18:55:11 +00:00
#endif
2003-03-03 00:59:24 +00:00
}
2012-07-02 12:05:48 +02:00
/** Terminate the context
2005-04-17 11:37:13 +00:00
@param skey The scheduled key
*/
void xtea_done(symmetric_key *skey)
{
2014-08-24 18:24:15 +02:00
LTC_UNUSED_PARAM(skey);
2005-04-17 11:37:13 +00:00
}
2004-12-30 23:55:53 +00:00
/**
Gets suitable key size
@param keysize [in/out] The length of the recommended key (in bytes). This function will store the suitable size back in this variable.
@return CRYPT_OK if the input key size is acceptable.
*/
int xtea_keysize(int *keysize)
2003-03-03 00:59:24 +00:00
{
2004-12-30 23:55:53 +00:00
LTC_ARGCHK(keysize != NULL);
if (*keysize < 16) {
2012-07-02 12:05:48 +02:00
return CRYPT_INVALID_KEYSIZE;
2003-03-03 00:59:24 +00:00
}
2004-12-30 23:55:53 +00:00
*keysize = 16;
2003-03-03 00:59:24 +00:00
return CRYPT_OK;
}
#endif
2005-06-09 00:08:13 +00:00
2017-06-19 13:43:49 +02:00
/* ref: $Format:%D$ */
/* git commit: $Format:%H$ */
/* commit time: $Format:%ai$ */