From: iceman1001 Date: Wed, 9 Dec 2015 13:57:16 +0000 (+0100) Subject: ADD: a TEA crypto algorithm implemention. X-Git-Url: https://git.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/commitdiff_plain/0a886a1d1bbe91af3e9e0a6a190a1af026faa589 ADD: a TEA crypto algorithm implemention. --- diff --git a/client/Makefile b/client/Makefile index 27617a5d..20273dd6 100644 --- a/client/Makefile +++ b/client/Makefile @@ -81,14 +81,14 @@ CORESRCS = uart.c \ CMDSRCS = nonce2key/crapto1.c\ - nonce2key/crypto1.c\ - nonce2key/nonce2key.c\ - loclass/cipher.c \ - loclass/cipherutils.c \ - loclass/des.c \ - loclass/ikeys.c \ - loclass/elite_crack.c\ - loclass/fileutils.c\ + nonce2key/crypto1.c\ + nonce2key/nonce2key.c\ + loclass/cipher.c \ + loclass/cipherutils.c \ + loclass/des.c \ + loclass/ikeys.c \ + loclass/elite_crack.c\ + loclass/fileutils.c\ mifarehost.c\ crc.c \ crc16.c \ @@ -109,7 +109,7 @@ CMDSRCS = nonce2key/crapto1.c\ cmdhficlass.c \ cmdhfmf.c \ cmdhfmfu.c \ -cmdhfmfhard.c \ + cmdhfmfhard.c \ cmdhfmfdes.c \ cmdhftopaz.c \ cmdhw.c \ @@ -140,6 +140,7 @@ cmdhfmfhard.c \ reveng/model.c\ reveng/poly.c\ reveng/getopt.c\ + tea.c\ ZLIBSRCS = deflate.c adler32.c trees.c zutil.c inflate.c inffast.c inftrees.c diff --git a/common/tea.c b/common/tea.c new file mode 100644 index 00000000..d0bea1c0 --- /dev/null +++ b/common/tea.c @@ -0,0 +1,86 @@ +//----------------------------------------------------------------------------- +// This code is licensed to you under the terms of the GNU GPL, version 2 or, +// at your option, any later version. See the LICENSE.txt file for the text of +// the license. +//----------------------------------------------------------------------------- +// Generic TEA crypto code. +// ref: http://143.53.36.235:8080/source.htm#ansi +//----------------------------------------------------------------------------- +#include "tea.h" +#define ROUNDS 32 +#define DELTA 0x9E3779B9 +#define SUM 0xC6EF3720 +#define SWAPENDIAN(x)\ + (x = (x >> 8 & 0xff00ff) | (x & 0xff00ff) << 8, x = x >> 16 | x << 16) + +void tea_encrypt(uint8_t *v, uint8_t *key) { + + uint32_t a=0,b=0,c=0,d=0,y=0,z=0; + uint32_t sum = 0; + uint8_t n = ROUNDS; + + //key + a = bytes_to_num(key, 4); + b = bytes_to_num(key+4, 4); + c = bytes_to_num(key+8, 4); + d = bytes_to_num(key+12, 4); + + //input + y = bytes_to_num(v, 4); + z = bytes_to_num(v+4, 4); + + // SWAPENDIAN(a); + // SWAPENDIAN(b); + // SWAPENDIAN(c); + // SWAPENDIAN(d); + // SWAPENDIAN(y); + // SWAPENDIAN(z); + + while ( n-- > 0 ) { + sum += DELTA; + y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + } + + // SWAPENDIAN(y); + // SWAPENDIAN(z); + + num_to_bytes(y, 4, v); + num_to_bytes(z, 4, v+4); +} + +void tea_decrypt(uint8_t *v, uint8_t *key) { + + uint32_t a=0,b=0,c=0,d=0,y=0,z=0; + uint32_t sum = SUM; + uint8_t n = ROUNDS; + + //key + a = bytes_to_num(key, 4); + b = bytes_to_num(key+4, 4); + c = bytes_to_num(key+8, 4); + d = bytes_to_num(key+12, 4); + + //input + y = bytes_to_num(v, 4); + z = bytes_to_num(v+4, 4); + + // SWAPENDIAN(a); + // SWAPENDIAN(b); + // SWAPENDIAN(c); + // SWAPENDIAN(d); + // SWAPENDIAN(y); + // SWAPENDIAN(z); + + /* sum = delta<<5, in general sum = delta * n */ + while ( n-- > 0 ) { + z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + sum -= DELTA; + } + + // SWAPENDIAN(y); + // SWAPENDIAN(z); + num_to_bytes(y, 4, v); + num_to_bytes(z, 4, v+4); +} diff --git a/common/tea.h b/common/tea.h new file mode 100644 index 00000000..3a12a769 --- /dev/null +++ b/common/tea.h @@ -0,0 +1,18 @@ +//----------------------------------------------------------------------------- +// This code is licensed to you under the terms of the GNU GPL, version 2 or, +// at your option, any later version. See the LICENSE.txt file for the text of +// the license. +//----------------------------------------------------------------------------- +// Generic TEA crypto code. +// ref: http://143.53.36.235:8080/source.htm#ansi +//----------------------------------------------------------------------------- + +#ifndef __TEA_H +#define __TEA_H + +#include "util.h" +#include +#include +void tea_encrypt(uint8_t *v, uint8_t *key); +void tea_decrypt(uint8_t *v, uint8_t *key); +#endif /* __TEA_H */ \ No newline at end of file