Re: Encryption ??
- From: bill robertson <arcadia2@xxxxxxxxxxxxx>
- Date: Sat, 26 Aug 2006 23:42:48 GMT
Alex Strickland wrote:
To Ross and KlasAlex,
I had to cut it out of some other code so I hope I have not made any mistakes. I recall that the website that I got this from asked that Arthur O'Dwyer's name remain in the code - but no other attribution required.
The TEA algorithm has a potential buffer overrun problem. I modified the test routine to show the problem. The program encryts all the data if the string is an exact multiple of 8-bytes. For the 1-7 bytes greater than nSize % 8 = 0, it just moves the plain text data into the encrypted string unaltered. In this case, the sizeof test isn't an exact multiple of 8 and you don't see the problem. The original test case.
The buffer over-run occurs for nSize % 8 == 0, since it encrypts the '\0' character at the end of the string. I modified the test string to show problem. Since Clipper doesn't use c-type strings, it probably doesn't make a difference in Clipper. I haven't found my old MSC-compiler so can't check it out.
c-programmers, should be aware and make appropriate modifications to call the routine with a size small enough to preserve the '\0' character.
/* ------------------------------------------------------------
* tea-example.c
*
* This code copyright Arthur O'Dwyer 2004. Freeware
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#if CHAR_BIT != 8
#error CHAR_BIT must be 8!
#elif ULONG_MAX != 0xFFFFFFFFuL
#error 'unsigned long' must be 32 bits!
#endif
/*
* Tiny Encryption Algorithm
*
* D.J. Wheeler & R.M. Needham
*/
#define ITERATIONS 32
static void encode(unsigned long *plain,
unsigned long *key,
unsigned long *enc)
{
unsigned long y = plain[0];
unsigned long z = plain[1];
unsigned long sum = 0;
unsigned long delta = 0x9E3779B9;
int i;
for (i=0; i < ITERATIONS; ++i)
{
sum += delta;
#if 0
/* The code in the original paper is WRONG WRONG WRONG */
y += ((z << 4) + key[0]) ^ ((z >> 5) + key[1]) ^ (z + sum);
z += ((y << 4) + key[2]) ^ ((y >> 5) + key[3]) ^ (y + sum);
#else
y += (z << 4) + (key[0] ^ z) + (sum ^ (z >> 5)) + key[1];
z += (y << 4) + (key[2] ^ y) + (sum ^ (y >> 5)) + key[3];
#endif
}
enc[0] = y;
enc[1] = z;
}
static void decode(unsigned long *enc, unsigned long *key, unsigned long *dec)
{
unsigned long y = enc[0];
unsigned long z = enc[1];
unsigned long delta = 0x9E3779B9;
unsigned long sum = delta * ITERATIONS;
int i;
for (i=0; i < ITERATIONS; ++i)
{
#if 0
/* The code in the original paper is WRONG WRONG WRONG */
z -= ((y << 4) + key[2]) ^ ((y >> 5) + key[3]) ^ (y + sum);
y -= ((z << 4) + key[0]) ^ ((z >> 5) + key[1]) ^ (z + sum);
#else
z -= (y << 4) + (key[2] ^ y) + (sum ^ (y >> 5)) + key[3];
y -= (z << 4) + (key[0] ^ z) + (sum ^ (z >> 5)) + key[1];
#endif
sum -= delta;
}
dec[0] = y;
dec[1] = z;
}
int TEA_encode(void *plain, void *crypt, size_t len,
void *key, size_t keylen)
{
unsigned long *lplain = (unsigned long *) plain;
unsigned long *lcrypt = (unsigned long *) crypt;
int npairs = len/8;
int i;
/* Paranoia: check key size */
if (keylen != 16)
return -1;
for (i=0; i < npairs; ++i)
encode(lplain+2*i, (unsigned long *) key, lcrypt+2*i);
if (len % 8) {
/* Partial block left over; copy it unencrypted */
memcpy(lcrypt+2*i, lplain+2*i, len%8);
}
return 0;
}
int TEA_decode(void *crypt, void *plain, size_t len, void *key, size_t nkey)
{
unsigned long *lcrypt = (unsigned long *) crypt;
unsigned long *lplain = (unsigned long *) plain;
int npairs = len/8;
int i;
/* Paranoia: check key size */
if (nkey != 16)
return -1;
for (i=0; i < npairs; ++i)
decode(lcrypt+2*i, (unsigned long *) key, lplain+2*i);
if (len % 8) {
/* Partial block left over; copy it undecrypted */
memcpy(lplain+2*i, lcrypt+2*i, len%8);
}
return 0;
}
int main(void)
{
char key[] = "1234567890123456";
char test[] = "Having been some days in preparation\n"
"A splendid time is guaranteed for all.1234";
char enc[sizeof test];
char dec[sizeof test];
TEA_encode(test, enc, sizeof test, key, 16);
TEA_decode( enc, dec, sizeof test, key, 16);
printf("\n\n1 %s", test);
printf("\n\n2 %s", enc );
printf("\n\n3 %s", dec );
printf("\n\nLength of Enc: %d\n", strlen( enc ));
printf( "\nLengh of Dec: %d\n", strlen( dec ));
printf("Size of key %d size of test[] %d", sizeof( key ), sizeof( test ) );
return 0;
}
.
- Follow-Ups:
- Re: Encryption ??
- From: Klas Engwall
- Re: Encryption ??
- References:
- Encryption ??
- From: Mel Smith
- Re: Encryption ??
- From: Klas Engwall
- Re: Encryption ??
- From: Klas Engwall
- Re: Encryption ??
- From: Mel Smith
- Re: Encryption ??
- From: Klas Engwall
- Re: Encryption ??
- From: Mel Smith
- Re: Encryption ??
- From: Klas Engwall
- Re: Encryption ??
- From: Mel Smith
- Re: Encryption ??
- From: Klas Engwall
- Re: Encryption ??
- From: Ross McKenzie
- Re: Encryption ??
- From: Klas Engwall
- Re: Encryption ??
- From: Alex Strickland
- Re: Encryption ??
- From: Ross McKenzie
- Re: Encryption ??
- From: Alex Strickland
- Encryption ??
- Prev by Date: Re: Blowfish encryption
- Next by Date: Re: Blowfish encryption
- Previous by thread: Re: Encryption ??
- Next by thread: Re: Encryption ??
- Index(es):
Relevant Pages
|