1
0
mirror of https://github.com/fumiama/simple-crypto.git synced 2026-06-12 15:10:23 +08:00

update api

This commit is contained in:
源文雨
2022-10-14 20:32:52 +08:00
parent 95f76aa730
commit efa7cfab66
3 changed files with 213 additions and 146 deletions

View File

@@ -12,8 +12,7 @@ endif()
add_compile_options(-std=c99) add_compile_options(-std=c99)
message(STATUS "optional:-std=c99") message(STATUS "optional:-std=c99")
set(TEST 0) if($ENV{BUILDTEST})
if(TEST)
add_definitions(-DTEST_SIMPLE_CRYPTO) add_definitions(-DTEST_SIMPLE_CRYPTO)
add_executable(smd5 md5.c) add_executable(smd5 md5.c)
add_executable(stea tea.c) add_executable(stea tea.c)

View File

@@ -13,20 +13,52 @@ uint8_t* md5(const uint8_t *data, size_t data_len, uint8_t digest[16]);
// ---------------TEA area--------------- // ---------------TEA area---------------
typedef uint32_t TEA; // TEA is the password of the tea algorithm
struct TEADAT { struct TEA {
int64_t len; uint32_t t[4];
uint8_t* data;
void* ptr; // free() must use this val
}; };
typedef struct TEADAT TEADAT; typedef struct TEA TEA;
int64_t tea_encrypt_qq(const TEA t[4], const TEADAT* src, TEADAT* out); // tea_encrypt_len is exactly the input buffer length
int64_t tea_encrypt(const TEA t[4], const uint32_t sumtable[0x10], const TEADAT* src, TEADAT* out); inline int64_t tea_encrypt_len(int64_t inlen) {
int64_t tea_encrypt_native_endian(const TEA t[4], const uint32_t sumtable[0x10], const TEADAT* src, TEADAT* out); int64_t fill = 10 - (inlen+1)%8;
int64_t tea_decrypt_qq(const TEA t[4], const TEADAT* src, TEADAT* out); return fill+inlen+7;
int64_t tea_decrypt(const TEA t[4], const uint32_t sumtable[0x10], const TEADAT* src, TEADAT* out); }
int64_t tea_decrypt_native_endian(const TEA t[4], const uint32_t sumtable[0x10], const TEADAT* src, TEADAT* out);
// tea_decrypt_len's buf0 is the first byte of the buffer passed into decode
// if you want to allocate decode buffer, just use the encoded data length
inline int64_t tea_decrypt_len(int64_t inlen, uint8_t buf0) {
int start = (buf0&7)+3;
return inlen-7-start;
}
// tea_encrypt_qq use qq sumtable, read from src, write to dst, return write count
// please allocate no less than tea_encrypt_len(len(src)) bytes for dst
int64_t tea_encrypt_qq(const TEA t, const uint8_t* src, int64_t srclen, uint8_t* dst);
// tea_encrypt use custom sumtable, read from src, write to dst, return write count
// please allocate no less than tea_encrypt_len(len(src)) bytes for dst
int64_t tea_encrypt(const TEA t, const uint32_t sumtable[0x10], const uint8_t* src, int64_t srclen, uint8_t* dst);
// tea_encrypt_qq_native_endian is the same as tea_encrypt_qq on BE machine
int64_t tea_encrypt_qq_native_endian(const TEA t, const uint8_t* src, int64_t srclen, uint8_t* dst);
// tea_encrypt_native_endian is the same as tea_encrypt on BE machine
int64_t tea_encrypt_native_endian(const TEA t, const uint32_t sumtable[0x10], const uint8_t* src, int64_t srclen, uint8_t* dst);
// tea_decrypt_qq use qq sumtable, read from src, write to dst, return start pointer of decrypted data
// length can be calculated by tea_decrypt_len or dst_buf_last-dst
uint8_t* tea_decrypt_qq(const TEA t, const uint8_t* src, int64_t srclen, uint8_t* dst);
// tea_decrypt use custom sumtable, read from src, write to dst, return start pointer of decrypted data
// length can be calculated by tea_decrypt_len or dst_buf_last-dst
uint8_t* tea_decrypt(const TEA t, const uint32_t sumtable[0x10], const uint8_t* src, int64_t srclen, uint8_t* dst);
// tea_decrypt_qq_native_endian is the same as tea_decrypt_qq on BE machine
uint8_t* tea_decrypt_qq_native_endian(const TEA t, const uint8_t* src, int64_t srclen, uint8_t* dst);
// tea_decrypt_native_endian is the same as tea_decrypt on BE machine
uint8_t* tea_decrypt_native_endian(const TEA t, const uint32_t sumtable[0x10], const uint8_t* src, int64_t srclen, uint8_t* dst);
// ---------------TEA area--------------- // ---------------TEA area---------------

296
tea.c
View File

@@ -28,22 +28,22 @@ const static uint32_t qqsumtable[0x10] = {
0xe3779b90, 0xe3779b90,
}; };
int64_t tea_encrypt_qq(const TEA t[4], const TEADAT* src, TEADAT* out) { // tea_encrypt_qq use qq sumtable, read from src, write to dst, return write count
int64_t lens = src->len; // please allocate no less than tea_encrypt_len(len(src)) bytes for dst
int64_t fill = 10 - (lens+1)%8; int64_t tea_encrypt_qq(const TEA t, const uint8_t* src, int64_t srclen, uint8_t* dst) {
int64_t dstlen = fill+lens+7; int64_t fill = 10 - (srclen+1)%8;
uint8_t* dstdat = (uint8_t*)malloc(dstlen); int64_t dstlen = fill+srclen+7;
((uint32_t*)dstdat)[0] = rand(); ((uint32_t*)dst)[0] = rand();
((uint32_t*)dstdat)[1] = rand(); ((uint32_t*)dst)[1] = rand();
((uint32_t*)dstdat)[2] = rand(); ((uint32_t*)dst)[2] = rand();
dstdat[0] = (fill-3)|0xF8; // 存储pad长度 dst[0] = (fill-3)|0xF8; // 存储pad长度
memcpy(dstdat+fill, src->data, lens); memcpy(dst+fill, src, srclen);
uint64_t iv1 = 0, iv2 = 0, holder; uint64_t iv1 = 0, iv2 = 0, holder;
for(int64_t i = 0; i < dstlen/8; i++) { for(int64_t i = 0; i < dstlen/8; i++) {
#ifdef WORDS_BIGENDIAN #ifdef WORDS_BIGENDIAN
uint64_t block = ((uint64_t*)dstdat)[i]; uint64_t block = ((uint64_t*)dst)[i];
#else #else
uint64_t block = __builtin_bswap64(((uint64_t*)dstdat)[i]); uint64_t block = __builtin_bswap64(((uint64_t*)dst)[i]);
#endif #endif
holder = block ^ iv1; holder = block ^ iv1;
iv1 = holder; iv1 = holder;
@@ -51,40 +51,37 @@ int64_t tea_encrypt_qq(const TEA t[4], const TEADAT* src, TEADAT* out) {
iv1 >>= 32; iv1 >>= 32;
uint32_t v0 = iv1; uint32_t v0 = iv1;
for (int i = 0; i < 0x10; i++) { for (int i = 0; i < 0x10; i++) {
v0 += (v1 + qqsumtable[i]) ^ ((v1 << 4) + t[0]) ^ ((v1 >> 5) + t[1]); v0 += (v1 + qqsumtable[i]) ^ ((v1 << 4) + t.t[0]) ^ ((v1 >> 5) + t.t[1]);
v1 += (v0 + qqsumtable[i]) ^ ((v0 << 4) + t[2]) ^ ((v0 >> 5) + t[3]); v1 += (v0 + qqsumtable[i]) ^ ((v0 << 4) + t.t[2]) ^ ((v0 >> 5) + t.t[3]);
} }
iv1 = ((uint64_t)v0<<32) | (uint64_t)v1; iv1 = ((uint64_t)v0<<32) | (uint64_t)v1;
iv1 = iv1 ^ iv2; iv1 = iv1 ^ iv2;
iv2 = holder; iv2 = holder;
#ifdef WORDS_BIGENDIAN #ifdef WORDS_BIGENDIAN
((uint64_t*)dstdat)[i] = iv1; ((uint64_t*)dst)[i] = iv1;
#else #else
((uint64_t*)dstdat)[i] = __builtin_bswap64(iv1); ((uint64_t*)dst)[i] = __builtin_bswap64(iv1);
#endif #endif
} }
out->len = dstlen;
out->data = dstdat;
out->ptr = dstdat;
return dstlen; return dstlen;
} }
int64_t tea_encrypt(const TEA t[4], const uint32_t sumtable[0x10], const TEADAT* src, TEADAT* out) { // tea_encrypt use custom sumtable, read from src, write to dst, return write count
int64_t lens = src->len; // please allocate no less than tea_encrypt_len(len(src)) bytes for dst
int64_t fill = 10 - (lens+1)%8; int64_t tea_encrypt(const TEA t, const uint32_t sumtable[0x10], const uint8_t* src, int64_t srclen, uint8_t* dst) {
int64_t dstlen = fill+lens+7; int64_t fill = 10 - (srclen+1)%8;
uint8_t* dstdat = (uint8_t*)malloc(dstlen); int64_t dstlen = fill+srclen+7;
((uint32_t*)dstdat)[0] = rand(); ((uint32_t*)dst)[0] = rand();
((uint32_t*)dstdat)[1] = rand(); ((uint32_t*)dst)[1] = rand();
((uint32_t*)dstdat)[2] = rand(); ((uint32_t*)dst)[2] = rand();
dstdat[0] = (fill-3)|0xF8; // 存储pad长度 dst[0] = (fill-3)|0xF8; // 存储pad长度
memcpy(dstdat+fill, src->data, lens); memcpy(dst+fill, src, srclen);
uint64_t iv1 = 0, iv2 = 0, holder; uint64_t iv1 = 0, iv2 = 0, holder;
for(int64_t i = 0; i < dstlen/8; i++) { for(int64_t i = 0; i < dstlen/8; i++) {
#ifdef WORDS_BIGENDIAN #ifdef WORDS_BIGENDIAN
uint64_t block = ((uint64_t*)dstdat)[i]; uint64_t block = ((uint64_t*)dst)[i];
#else #else
uint64_t block = __builtin_bswap64(((uint64_t*)dstdat)[i]); uint64_t block = __builtin_bswap64(((uint64_t*)dst)[i]);
#endif #endif
holder = block ^ iv1; holder = block ^ iv1;
iv1 = holder; iv1 = holder;
@@ -92,153 +89,190 @@ int64_t tea_encrypt(const TEA t[4], const uint32_t sumtable[0x10], const TEADAT*
iv1 >>= 32; iv1 >>= 32;
uint32_t v0 = iv1; uint32_t v0 = iv1;
for (int i = 0; i < 0x10; i++) { for (int i = 0; i < 0x10; i++) {
v0 += (v1 + sumtable[i]) ^ ((v1 << 4) + t[0]) ^ ((v1 >> 5) + t[1]); v0 += (v1 + sumtable[i]) ^ ((v1 << 4) + t.t[0]) ^ ((v1 >> 5) + t.t[1]);
v1 += (v0 + sumtable[i]) ^ ((v0 << 4) + t[2]) ^ ((v0 >> 5) + t[3]); v1 += (v0 + sumtable[i]) ^ ((v0 << 4) + t.t[2]) ^ ((v0 >> 5) + t.t[3]);
} }
iv1 = ((uint64_t)v0<<32) | (uint64_t)v1; iv1 = ((uint64_t)v0<<32) | (uint64_t)v1;
iv1 = iv1 ^ iv2; iv1 = iv1 ^ iv2;
iv2 = holder; iv2 = holder;
#ifdef WORDS_BIGENDIAN #ifdef WORDS_BIGENDIAN
((uint64_t*)dstdat)[i] = iv1; ((uint64_t*)dst)[i] = iv1;
#else #else
((uint64_t*)dstdat)[i] = __builtin_bswap64(iv1); ((uint64_t*)dst)[i] = __builtin_bswap64(iv1);
#endif #endif
} }
out->len = dstlen;
out->data = dstdat;
out->ptr = dstdat;
return dstlen; return dstlen;
} }
int64_t tea_encrypt_native_endian(const TEA t[4], const uint32_t sumtable[0x10], const TEADAT* src, TEADAT* out) { // tea_encrypt_qq_native_endian is the same as tea_encrypt_qq on BE machine
int64_t lens = src->len; int64_t tea_encrypt_qq_native_endian(const TEA t, const uint8_t* src, int64_t srclen, uint8_t* dst) {
int64_t fill = 10 - (lens+1)%8; int64_t fill = 10 - (srclen+1)%8;
int64_t dstlen = fill+lens+7; int64_t dstlen = fill+srclen+7;
uint8_t* dstdat = (uint8_t*)malloc(dstlen); ((uint32_t*)dst)[0] = rand();
((uint32_t*)dstdat)[0] = rand(); ((uint32_t*)dst)[1] = rand();
((uint32_t*)dstdat)[1] = rand(); ((uint32_t*)dst)[2] = rand();
((uint32_t*)dstdat)[2] = rand(); dst[0] = (fill-3)|0xF8; // 存储pad长度
dstdat[0] = (fill-3)|0xF8; // 存储pad长度 memcpy(dst+fill, src, srclen);
memcpy(dstdat+fill, src->data, lens);
uint64_t iv1 = 0, iv2 = 0, holder; uint64_t iv1 = 0, iv2 = 0, holder;
for(int64_t i = 0; i < dstlen/8; i++) { for(int64_t i = 0; i < dstlen/8; i++) {
uint64_t block = ((uint64_t*)dstdat)[i]; uint64_t block = ((uint64_t*)dst)[i];
holder = block ^ iv1; holder = block ^ iv1;
iv1 = holder; iv1 = holder;
uint32_t v1 = holder; uint32_t v1 = holder;
iv1 >>= 32; iv1 >>= 32;
uint32_t v0 = iv1; uint32_t v0 = iv1;
for (int i = 0; i < 0x10; i++) { for (int i = 0; i < 0x10; i++) {
v0 += (v1 + sumtable[i]) ^ ((v1 << 4) + t[0]) ^ ((v1 >> 5) + t[1]); v0 += (v1 + qqsumtable[i]) ^ ((v1 << 4) + t.t[0]) ^ ((v1 >> 5) + t.t[1]);
v1 += (v0 + sumtable[i]) ^ ((v0 << 4) + t[2]) ^ ((v0 >> 5) + t[3]); v1 += (v0 + qqsumtable[i]) ^ ((v0 << 4) + t.t[2]) ^ ((v0 >> 5) + t.t[3]);
} }
iv1 = ((uint64_t)v0<<32) | (uint64_t)v1; iv1 = ((uint64_t)v0<<32) | (uint64_t)v1;
iv1 = iv1 ^ iv2; iv1 = iv1 ^ iv2;
iv2 = holder; iv2 = holder;
((uint64_t*)dstdat)[i] = iv1; ((uint64_t*)dst)[i] = iv1;
} }
out->len = dstlen;
out->data = dstdat;
out->ptr = dstdat;
return dstlen; return dstlen;
} }
int64_t tea_decrypt_qq(const TEA t[4], const TEADAT* src, TEADAT* out) { // tea_encrypt_native_endian is the same as tea_encrypt on BE machine
if (src->len < 16 || (src->len)%8 != 0) return 0; int64_t tea_encrypt_native_endian(const TEA t, const uint32_t sumtable[0x10], const uint8_t* src, int64_t srclen, uint8_t* dst) {
uint8_t* dstdat = (uint8_t*)malloc(src->len); int64_t fill = 10 - (srclen+1)%8;
uint64_t iv1, iv2 = 0, holder = 0; int64_t dstlen = fill+srclen+7;
for(int64_t i = 0; i < src->len/8; i++) { ((uint32_t*)dst)[0] = rand();
#ifdef WORDS_BIGENDIAN ((uint32_t*)dst)[1] = rand();
iv1 = ((uint64_t*)(src->data))[i]; ((uint32_t*)dst)[2] = rand();
#else dst[0] = (fill-3)|0xF8; // 存储pad长度
iv1 = __builtin_bswap64(((uint64_t*)(src->data))[i]); memcpy(dst+fill, src, srclen);
#endif uint64_t iv1 = 0, iv2 = 0, holder;
iv2 ^= iv1; for(int64_t i = 0; i < dstlen/8; i++) {
uint32_t v1 = iv2; uint64_t block = ((uint64_t*)dst)[i];
iv2 >>= 32; holder = block ^ iv1;
uint32_t v0 = iv2; iv1 = holder;
for (int i = 0x0f; i >= 0; i--) { uint32_t v1 = holder;
v1 -= (v0 + qqsumtable[i]) ^ ((v0 << 4) + t[2]) ^ ((v0 >> 5) + t[3]); iv1 >>= 32;
v0 -= (v1 + qqsumtable[i]) ^ ((v1 << 4) + t[0]) ^ ((v1 >> 5) + t[1]); uint32_t v0 = iv1;
for (int i = 0; i < 0x10; i++) {
v0 += (v1 + sumtable[i]) ^ ((v1 << 4) + t.t[0]) ^ ((v1 >> 5) + t.t[1]);
v1 += (v0 + sumtable[i]) ^ ((v0 << 4) + t.t[2]) ^ ((v0 >> 5) + t.t[3]);
} }
iv2 = ((uint64_t)v0<<32) | (uint64_t)v1; iv1 = ((uint64_t)v0<<32) | (uint64_t)v1;
#ifdef WORDS_BIGENDIAN iv1 = iv1 ^ iv2;
((uint64_t*)dstdat)[i] = iv2^holder; iv2 = holder;
#else ((uint64_t*)dst)[i] = iv1;
((uint64_t*)dstdat)[i] = __builtin_bswap64(iv2^holder);
#endif
holder = iv1;
} }
int start = (dstdat[0]&7)+3; return dstlen;
out->len = src->len-7-start;
out->data = dstdat+start;
out->ptr = dstdat;
return out->len;
} }
int64_t tea_decrypt(const TEA t[4], const uint32_t sumtable[0x10], const TEADAT* src, TEADAT* out) { // tea_decrypt_qq use qq sumtable, read from src, write to dst, return start pointer of decrypted data
if (src->len < 16 || (src->len)%8 != 0) return 0; // length can be calculated by tea_decrypt_len or dst_buf_last-dst
uint8_t* dstdat = (uint8_t*)malloc(src->len); uint8_t* tea_decrypt_qq(const TEA t, const uint8_t* src, int64_t srclen, uint8_t* dst) {
if (srclen < 16 || srclen%8 != 0) return 0;
uint64_t iv1, iv2 = 0, holder = 0; uint64_t iv1, iv2 = 0, holder = 0;
for(int64_t i = 0; i < src->len/8; i++) { for(int64_t i = 0; i < srclen/8; i++) {
#ifdef WORDS_BIGENDIAN #ifdef WORDS_BIGENDIAN
iv1 = ((uint64_t*)(src->data))[i]; iv1 = ((uint64_t*)(src))[i];
#else #else
iv1 = __builtin_bswap64(((uint64_t*)(src->data))[i]); iv1 = __builtin_bswap64(((uint64_t*)(src))[i]);
#endif #endif
iv2 ^= iv1; iv2 ^= iv1;
uint32_t v1 = iv2; uint32_t v1 = iv2;
iv2 >>= 32; iv2 >>= 32;
uint32_t v0 = iv2; uint32_t v0 = iv2;
for (int i = 0x0f; i >= 0; i--) { for (int i = 0x0f; i >= 0; i--) {
v1 -= (v0 + sumtable[i]) ^ ((v0 << 4) + t[2]) ^ ((v0 >> 5) + t[3]); v1 -= (v0 + qqsumtable[i]) ^ ((v0 << 4) + t.t[2]) ^ ((v0 >> 5) + t.t[3]);
v0 -= (v1 + sumtable[i]) ^ ((v1 << 4) + t[0]) ^ ((v1 >> 5) + t[1]); v0 -= (v1 + qqsumtable[i]) ^ ((v1 << 4) + t.t[0]) ^ ((v1 >> 5) + t.t[1]);
} }
iv2 = ((uint64_t)v0<<32) | (uint64_t)v1; iv2 = ((uint64_t)v0<<32) | (uint64_t)v1;
#ifdef WORDS_BIGENDIAN #ifdef WORDS_BIGENDIAN
((uint64_t*)dstdat)[i] = iv2^holder; ((uint64_t*)dst)[i] = iv2^holder;
#else #else
((uint64_t*)dstdat)[i] = __builtin_bswap64(iv2^holder); ((uint64_t*)dst)[i] = __builtin_bswap64(iv2^holder);
#endif #endif
holder = iv1; holder = iv1;
} }
int start = (dstdat[0]&7)+3; int start = (dst[0]&7)+3;
out->len = src->len-7-start; return dst+start;
out->data = dstdat+start;
out->ptr = dstdat;
return out->len;
} }
int64_t tea_decrypt_native_endian(const TEA t[4], const uint32_t sumtable[0x10], const TEADAT* src, TEADAT* out) { // tea_decrypt use custom sumtable, read from src, write to dst, return start pointer of decrypted data
if (src->len < 16 || (src->len)%8 != 0) return 0; // length can be calculated by tea_decrypt_len or dst_buf_last-dst
uint8_t* dstdat = (uint8_t*)malloc(src->len); uint8_t* tea_decrypt(const TEA t, const uint32_t sumtable[0x10], const uint8_t* src, int64_t srclen, uint8_t* dst) {
if (srclen < 16 || srclen%8 != 0) return 0;
uint64_t iv1, iv2 = 0, holder = 0; uint64_t iv1, iv2 = 0, holder = 0;
for(int64_t i = 0; i < src->len/8; i++) { for(int64_t i = 0; i < srclen/8; i++) {
iv1 = ((uint64_t*)(src->data))[i]; #ifdef WORDS_BIGENDIAN
iv1 = ((uint64_t*)(src))[i];
#else
iv1 = __builtin_bswap64(((uint64_t*)(src))[i]);
#endif
iv2 ^= iv1; iv2 ^= iv1;
uint32_t v1 = iv2; uint32_t v1 = iv2;
iv2 >>= 32; iv2 >>= 32;
uint32_t v0 = iv2; uint32_t v0 = iv2;
for (int i = 0x0f; i >= 0; i--) { for (int i = 0x0f; i >= 0; i--) {
v1 -= (v0 + sumtable[i]) ^ ((v0 << 4) + t[2]) ^ ((v0 >> 5) + t[3]); v1 -= (v0 + sumtable[i]) ^ ((v0 << 4) + t.t[2]) ^ ((v0 >> 5) + t.t[3]);
v0 -= (v1 + sumtable[i]) ^ ((v1 << 4) + t[0]) ^ ((v1 >> 5) + t[1]); v0 -= (v1 + sumtable[i]) ^ ((v1 << 4) + t.t[0]) ^ ((v1 >> 5) + t.t[1]);
} }
iv2 = ((uint64_t)v0<<32) | (uint64_t)v1; iv2 = ((uint64_t)v0<<32) | (uint64_t)v1;
((uint64_t*)dstdat)[i] = iv2^holder; #ifdef WORDS_BIGENDIAN
((uint64_t*)dst)[i] = iv2^holder;
#else
((uint64_t*)dst)[i] = __builtin_bswap64(iv2^holder);
#endif
holder = iv1; holder = iv1;
} }
int start = (dstdat[0]&7)+3; int start = (dst[0]&7)+3;
out->len = src->len-7-start; return dst+start;
out->data = dstdat+start;
out->ptr = dstdat;
return out->len;
} }
// tea_decrypt_qq_native_endian is the same as tea_decrypt_qq on BE machine
uint8_t* tea_decrypt_qq_native_endian(const TEA t, const uint8_t* src, int64_t srclen, uint8_t* dst) {
if (srclen < 16 || srclen%8 != 0) return 0;
uint64_t iv1, iv2 = 0, holder = 0;
for(int64_t i = 0; i < srclen/8; i++) {
iv1 = ((uint64_t*)(src))[i];
iv2 ^= iv1;
uint32_t v1 = iv2;
iv2 >>= 32;
uint32_t v0 = iv2;
for (int i = 0x0f; i >= 0; i--) {
v1 -= (v0 + qqsumtable[i]) ^ ((v0 << 4) + t.t[2]) ^ ((v0 >> 5) + t.t[3]);
v0 -= (v1 + qqsumtable[i]) ^ ((v1 << 4) + t.t[0]) ^ ((v1 >> 5) + t.t[1]);
}
iv2 = ((uint64_t)v0<<32) | (uint64_t)v1;
((uint64_t*)dst)[i] = iv2^holder;
holder = iv1;
}
int start = (dst[0]&7)+3;
return dst+start;
}
// tea_decrypt_native_endian is the same as tea_decrypt on BE machine
uint8_t* tea_decrypt_native_endian(const TEA t, const uint32_t sumtable[0x10], const uint8_t* src, int64_t srclen, uint8_t* dst) {
if (srclen < 16 || srclen%8 != 0) return 0;
uint64_t iv1, iv2 = 0, holder = 0;
for(int64_t i = 0; i < srclen/8; i++) {
iv1 = ((uint64_t*)(src))[i];
iv2 ^= iv1;
uint32_t v1 = iv2;
iv2 >>= 32;
uint32_t v0 = iv2;
for (int i = 0x0f; i >= 0; i--) {
v1 -= (v0 + sumtable[i]) ^ ((v0 << 4) + t.t[2]) ^ ((v0 >> 5) + t.t[3]);
v0 -= (v1 + sumtable[i]) ^ ((v1 << 4) + t.t[0]) ^ ((v1 >> 5) + t.t[1]);
}
iv2 = ((uint64_t)v0<<32) | (uint64_t)v1;
((uint64_t*)dst)[i] = iv2^holder;
holder = iv1;
}
int start = (dst[0]&7)+3;
return dst+start;
}
#ifdef TEST_SIMPLE_CRYPTO #ifdef TEST_SIMPLE_CRYPTO
int main(int argc, char **argv) { int main(int argc, char **argv) {
TEADAT* td = (TEADAT*)malloc(sizeof(TEADAT)); TEA t;
TEA* t = (TEA*)"32107654BA98FEDC"; memcpy(t.t, "32107654BA98FEDC", 4*8);
TEADAT out;
if (argc != 3) { if (argc != 3) {
printf("usage: %s -[e|d] 'string'\n", argv[0]); printf("usage: %s -[e|d] 'string'\n", argv[0]);
@@ -246,37 +280,39 @@ int main(int argc, char **argv) {
} }
switch(argv[1][1]) { switch(argv[1][1]) {
case 'e': case 'e':
td->data = (uint8_t*)(argv[2]); uint8_t* data = (uint8_t*)(argv[2]);
td->len = strlen(argv[2]); int64_t datalen = strlen(argv[2]);
tea_encrypt_qq(t, td, &out); int64_t outlen = tea_encrypt_len(datalen);
uint8_t* out = (uint8_t*)malloc(outlen);
tea_encrypt_qq(t, data, datalen, out);
// display result // display result
for (int i = 0; i < out.len; i++) printf("%02x", ((uint8_t*)(out.data))[i]); for (int i = 0; i < outlen; i++) printf("%02x", ((uint8_t*)(out))[i]);
putchar('\n'); putchar('\n');
free(out.ptr); free(out);
break; break;
case 'd': case 'd':
td->len = strlen(argv[2])/2; int64_t datalen = strlen(argv[2])/2;
// printf("decode input len: %lld\n", td->len); // printf("decode input len: %lld\n", td->len);
td->data = malloc(td->len); uint8_t* data = malloc(datalen);
int i = td->len; int i = datalen;
while (i--) { while (i--) {
int x; int x;
sscanf(argv[2]+i*2, "%02x", &x); sscanf(argv[2]+i*2, "%02x", &x);
td->data[i] = x; data[i] = x;
argv[2][i*2] = 0; argv[2][i*2] = 0;
} }
if (tea_decrypt_qq(t, td, &out)) { uint8_t* out = (uint8_t*)malloc(datalen);
out.data[out.len] = 0; uint8_t* outdat = tea_decrypt_qq(t, data, datalen, out);
if (outdat) {
// printf("decode output len: %lld\n", tdd->len); // printf("decode output len: %lld\n", tdd->len);
for (int i = 0; i < out.len; i++) putchar(out.data[i]); for (int i = 0; i < tea_decrypt_len(datalen, out[0]); i++) putchar(outdat[i]);
putchar('\n'); putchar('\n');
free(out.ptr); free(out);
} else puts("decode error!"); } else puts("decode error!");
free(td->data); free(data);
break; break;
default: break; default: break;
} }
free(td);
return 0; return 0;
} }
#endif #endif