Re: Encryption ??
- From: Alex Strickland <sscc@xxxxxxxxxx>
- Date: Wed, 16 Aug 2006 09:24:05 +0200
To Ross and Klas
Yes, I would be interested in examining it. Always happy to learn from
the blood and sweat of others <g>.
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.
Hope you don't find any mistakes.
Regards
Alex
//
// TEA.c
//
// Clipper wrappers for TEA written by Alex Strickland
//
// sscc
// @
// mweb
// .
// co
// .
// za
//
// Compiled with MS C/C++ v8.00c
//
// Use cl /c /AL /Gs2 /Oalt /Zl matrix.c
//
// **** cl /c /AL /Gs2 /Zl /W3 TEA.C ****
//
// /c - compile only, no link
// /AL - large model
// /Gs2 - remove stack check calls, generates 286 instructions
// /Oalt - assume no aliasing, enable loop optimisations, favor code speed
// *** no optimisations ? ***
// /Zl - omit default library name in .OBJ
//
// /FPi - calls with floating point emulation (not used)
#include "extend.api"
#include "fm.api"
/*
* 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.\n";
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("%s\n", test);
printf("%s\n", dec);
return 0;
}*/
/* end of tea-example.c */
// TEA_ENCODE(string , @encrypt, key)
//CLIPPER TEA_ENCODE()
CLIPPER TE()
{
char *encrypt;
int ret;
int length = _parclen(1);
encrypt = (char *) _xgrab(length);
ret = TEA_encode(_parc(1), encrypt, length, _parc(3), 16);
_storclen(encrypt, length, 2);
_xfree(encrypt);
_retni(ret);
}
// TEA_DECODE(string , @decrypt, key)
//CLIPPER TEA_DECODE()
CLIPPER TD()
{
char *decrypt;
int ret;
int length = _parclen(1);
decrypt = (char *) _xgrab(length);
ret = TEA_decode(_parc(1), decrypt, length, _parc(3), 16);
_storclen(decrypt, length, 2);
_xfree(decrypt);
_retni(ret);
}
- Follow-Ups:
- Re: Encryption ??
- From: bill robertson
- 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
- Encryption ??
- Prev by Date: Re: Encryption ??
- Next by Date: Re: Encryption ??
- Previous by thread: Re: Encryption ??
- Next by thread: Re: Encryption ??
- Index(es):
Relevant Pages
|