mirror of
https://github.com/fumiama/simple-crypto.git
synced 2026-06-06 10:20:24 +08:00
update api
This commit is contained in:
@@ -12,8 +12,7 @@ endif()
|
||||
add_compile_options(-std=c99)
|
||||
message(STATUS "optional:-std=c99")
|
||||
|
||||
set(TEST 0)
|
||||
if(TEST)
|
||||
if($ENV{BUILDTEST})
|
||||
add_definitions(-DTEST_SIMPLE_CRYPTO)
|
||||
add_executable(smd5 md5.c)
|
||||
add_executable(stea tea.c)
|
||||
|
||||
@@ -13,21 +13,53 @@ uint8_t* md5(const uint8_t *data, size_t data_len, uint8_t digest[16]);
|
||||
|
||||
// ---------------TEA area---------------
|
||||
|
||||
typedef uint32_t TEA;
|
||||
struct TEADAT {
|
||||
int64_t len;
|
||||
uint8_t* data;
|
||||
void* ptr; // free() must use this val
|
||||
// TEA is the password of the tea algorithm
|
||||
struct TEA {
|
||||
uint32_t t[4];
|
||||
};
|
||||
typedef struct TEADAT TEADAT;
|
||||
typedef struct TEA TEA;
|
||||
|
||||
int64_t tea_encrypt_qq(const TEA t[4], const TEADAT* src, TEADAT* out);
|
||||
int64_t tea_encrypt(const TEA t[4], const uint32_t sumtable[0x10], const TEADAT* src, TEADAT* out);
|
||||
int64_t tea_encrypt_native_endian(const TEA t[4], const uint32_t sumtable[0x10], const TEADAT* src, TEADAT* out);
|
||||
int64_t tea_decrypt_qq(const TEA t[4], const TEADAT* src, TEADAT* out);
|
||||
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_encrypt_len is exactly the input buffer length
|
||||
inline int64_t tea_encrypt_len(int64_t inlen) {
|
||||
int64_t fill = 10 - (inlen+1)%8;
|
||||
return fill+inlen+7;
|
||||
}
|
||||
|
||||
// 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---------------
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
298
tea.c
298
tea.c
@@ -28,22 +28,22 @@ const static uint32_t qqsumtable[0x10] = {
|
||||
0xe3779b90,
|
||||
};
|
||||
|
||||
int64_t tea_encrypt_qq(const TEA t[4], const TEADAT* src, TEADAT* out) {
|
||||
int64_t lens = src->len;
|
||||
int64_t fill = 10 - (lens+1)%8;
|
||||
int64_t dstlen = fill+lens+7;
|
||||
uint8_t* dstdat = (uint8_t*)malloc(dstlen);
|
||||
((uint32_t*)dstdat)[0] = rand();
|
||||
((uint32_t*)dstdat)[1] = rand();
|
||||
((uint32_t*)dstdat)[2] = rand();
|
||||
dstdat[0] = (fill-3)|0xF8; // 存储pad长度
|
||||
memcpy(dstdat+fill, src->data, lens);
|
||||
// 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) {
|
||||
int64_t fill = 10 - (srclen+1)%8;
|
||||
int64_t dstlen = fill+srclen+7;
|
||||
((uint32_t*)dst)[0] = rand();
|
||||
((uint32_t*)dst)[1] = rand();
|
||||
((uint32_t*)dst)[2] = rand();
|
||||
dst[0] = (fill-3)|0xF8; // 存储pad长度
|
||||
memcpy(dst+fill, src, srclen);
|
||||
uint64_t iv1 = 0, iv2 = 0, holder;
|
||||
for(int64_t i = 0; i < dstlen/8; i++) {
|
||||
#ifdef WORDS_BIGENDIAN
|
||||
uint64_t block = ((uint64_t*)dstdat)[i];
|
||||
uint64_t block = ((uint64_t*)dst)[i];
|
||||
#else
|
||||
uint64_t block = __builtin_bswap64(((uint64_t*)dstdat)[i]);
|
||||
uint64_t block = __builtin_bswap64(((uint64_t*)dst)[i]);
|
||||
#endif
|
||||
holder = block ^ iv1;
|
||||
iv1 = holder;
|
||||
@@ -51,40 +51,37 @@ int64_t tea_encrypt_qq(const TEA t[4], const TEADAT* src, TEADAT* out) {
|
||||
iv1 >>= 32;
|
||||
uint32_t v0 = iv1;
|
||||
for (int i = 0; i < 0x10; i++) {
|
||||
v0 += (v1 + qqsumtable[i]) ^ ((v1 << 4) + t[0]) ^ ((v1 >> 5) + t[1]);
|
||||
v1 += (v0 + qqsumtable[i]) ^ ((v0 << 4) + t[2]) ^ ((v0 >> 5) + t[3]);
|
||||
v0 += (v1 + qqsumtable[i]) ^ ((v1 << 4) + t.t[0]) ^ ((v1 >> 5) + t.t[1]);
|
||||
v1 += (v0 + qqsumtable[i]) ^ ((v0 << 4) + t.t[2]) ^ ((v0 >> 5) + t.t[3]);
|
||||
}
|
||||
iv1 = ((uint64_t)v0<<32) | (uint64_t)v1;
|
||||
iv1 = iv1 ^ iv2;
|
||||
iv2 = holder;
|
||||
#ifdef WORDS_BIGENDIAN
|
||||
((uint64_t*)dstdat)[i] = iv1;
|
||||
((uint64_t*)dst)[i] = iv1;
|
||||
#else
|
||||
((uint64_t*)dstdat)[i] = __builtin_bswap64(iv1);
|
||||
((uint64_t*)dst)[i] = __builtin_bswap64(iv1);
|
||||
#endif
|
||||
}
|
||||
out->len = dstlen;
|
||||
out->data = dstdat;
|
||||
out->ptr = dstdat;
|
||||
return dstlen;
|
||||
}
|
||||
|
||||
int64_t tea_encrypt(const TEA t[4], const uint32_t sumtable[0x10], const TEADAT* src, TEADAT* out) {
|
||||
int64_t lens = src->len;
|
||||
int64_t fill = 10 - (lens+1)%8;
|
||||
int64_t dstlen = fill+lens+7;
|
||||
uint8_t* dstdat = (uint8_t*)malloc(dstlen);
|
||||
((uint32_t*)dstdat)[0] = rand();
|
||||
((uint32_t*)dstdat)[1] = rand();
|
||||
((uint32_t*)dstdat)[2] = rand();
|
||||
dstdat[0] = (fill-3)|0xF8; // 存储pad长度
|
||||
memcpy(dstdat+fill, src->data, lens);
|
||||
// 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) {
|
||||
int64_t fill = 10 - (srclen+1)%8;
|
||||
int64_t dstlen = fill+srclen+7;
|
||||
((uint32_t*)dst)[0] = rand();
|
||||
((uint32_t*)dst)[1] = rand();
|
||||
((uint32_t*)dst)[2] = rand();
|
||||
dst[0] = (fill-3)|0xF8; // 存储pad长度
|
||||
memcpy(dst+fill, src, srclen);
|
||||
uint64_t iv1 = 0, iv2 = 0, holder;
|
||||
for(int64_t i = 0; i < dstlen/8; i++) {
|
||||
#ifdef WORDS_BIGENDIAN
|
||||
uint64_t block = ((uint64_t*)dstdat)[i];
|
||||
uint64_t block = ((uint64_t*)dst)[i];
|
||||
#else
|
||||
uint64_t block = __builtin_bswap64(((uint64_t*)dstdat)[i]);
|
||||
uint64_t block = __builtin_bswap64(((uint64_t*)dst)[i]);
|
||||
#endif
|
||||
holder = block ^ iv1;
|
||||
iv1 = holder;
|
||||
@@ -92,153 +89,190 @@ int64_t tea_encrypt(const TEA t[4], const uint32_t sumtable[0x10], const TEADAT*
|
||||
iv1 >>= 32;
|
||||
uint32_t v0 = iv1;
|
||||
for (int i = 0; i < 0x10; i++) {
|
||||
v0 += (v1 + sumtable[i]) ^ ((v1 << 4) + t[0]) ^ ((v1 >> 5) + t[1]);
|
||||
v1 += (v0 + sumtable[i]) ^ ((v0 << 4) + t[2]) ^ ((v0 >> 5) + t[3]);
|
||||
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]);
|
||||
}
|
||||
iv1 = ((uint64_t)v0<<32) | (uint64_t)v1;
|
||||
iv1 = iv1 ^ iv2;
|
||||
iv2 = holder;
|
||||
#ifdef WORDS_BIGENDIAN
|
||||
((uint64_t*)dstdat)[i] = iv1;
|
||||
((uint64_t*)dst)[i] = iv1;
|
||||
#else
|
||||
((uint64_t*)dstdat)[i] = __builtin_bswap64(iv1);
|
||||
((uint64_t*)dst)[i] = __builtin_bswap64(iv1);
|
||||
#endif
|
||||
}
|
||||
out->len = dstlen;
|
||||
out->data = dstdat;
|
||||
out->ptr = dstdat;
|
||||
return dstlen;
|
||||
}
|
||||
|
||||
int64_t tea_encrypt_native_endian(const TEA t[4], const uint32_t sumtable[0x10], const TEADAT* src, TEADAT* out) {
|
||||
int64_t lens = src->len;
|
||||
int64_t fill = 10 - (lens+1)%8;
|
||||
int64_t dstlen = fill+lens+7;
|
||||
uint8_t* dstdat = (uint8_t*)malloc(dstlen);
|
||||
((uint32_t*)dstdat)[0] = rand();
|
||||
((uint32_t*)dstdat)[1] = rand();
|
||||
((uint32_t*)dstdat)[2] = rand();
|
||||
dstdat[0] = (fill-3)|0xF8; // 存储pad长度
|
||||
memcpy(dstdat+fill, src->data, lens);
|
||||
// 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) {
|
||||
int64_t fill = 10 - (srclen+1)%8;
|
||||
int64_t dstlen = fill+srclen+7;
|
||||
((uint32_t*)dst)[0] = rand();
|
||||
((uint32_t*)dst)[1] = rand();
|
||||
((uint32_t*)dst)[2] = rand();
|
||||
dst[0] = (fill-3)|0xF8; // 存储pad长度
|
||||
memcpy(dst+fill, src, srclen);
|
||||
uint64_t iv1 = 0, iv2 = 0, holder;
|
||||
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;
|
||||
iv1 = holder;
|
||||
uint32_t v1 = holder;
|
||||
iv1 >>= 32;
|
||||
uint32_t v0 = iv1;
|
||||
for (int i = 0; i < 0x10; i++) {
|
||||
v0 += (v1 + sumtable[i]) ^ ((v1 << 4) + t[0]) ^ ((v1 >> 5) + t[1]);
|
||||
v1 += (v0 + sumtable[i]) ^ ((v0 << 4) + t[2]) ^ ((v0 >> 5) + t[3]);
|
||||
v0 += (v1 + qqsumtable[i]) ^ ((v1 << 4) + t.t[0]) ^ ((v1 >> 5) + t.t[1]);
|
||||
v1 += (v0 + qqsumtable[i]) ^ ((v0 << 4) + t.t[2]) ^ ((v0 >> 5) + t.t[3]);
|
||||
}
|
||||
iv1 = ((uint64_t)v0<<32) | (uint64_t)v1;
|
||||
iv1 = iv1 ^ iv2;
|
||||
iv2 = holder;
|
||||
((uint64_t*)dstdat)[i] = iv1;
|
||||
((uint64_t*)dst)[i] = iv1;
|
||||
}
|
||||
out->len = dstlen;
|
||||
out->data = dstdat;
|
||||
out->ptr = dstdat;
|
||||
return dstlen;
|
||||
}
|
||||
|
||||
int64_t tea_decrypt_qq(const TEA t[4], const TEADAT* src, TEADAT* out) {
|
||||
if (src->len < 16 || (src->len)%8 != 0) return 0;
|
||||
uint8_t* dstdat = (uint8_t*)malloc(src->len);
|
||||
uint64_t iv1, iv2 = 0, holder = 0;
|
||||
for(int64_t i = 0; i < src->len/8; i++) {
|
||||
#ifdef WORDS_BIGENDIAN
|
||||
iv1 = ((uint64_t*)(src->data))[i];
|
||||
#else
|
||||
iv1 = __builtin_bswap64(((uint64_t*)(src->data))[i]);
|
||||
#endif
|
||||
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[2]) ^ ((v0 >> 5) + t[3]);
|
||||
v0 -= (v1 + qqsumtable[i]) ^ ((v1 << 4) + t[0]) ^ ((v1 >> 5) + t[1]);
|
||||
// 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) {
|
||||
int64_t fill = 10 - (srclen+1)%8;
|
||||
int64_t dstlen = fill+srclen+7;
|
||||
((uint32_t*)dst)[0] = rand();
|
||||
((uint32_t*)dst)[1] = rand();
|
||||
((uint32_t*)dst)[2] = rand();
|
||||
dst[0] = (fill-3)|0xF8; // 存储pad长度
|
||||
memcpy(dst+fill, src, srclen);
|
||||
uint64_t iv1 = 0, iv2 = 0, holder;
|
||||
for(int64_t i = 0; i < dstlen/8; i++) {
|
||||
uint64_t block = ((uint64_t*)dst)[i];
|
||||
holder = block ^ iv1;
|
||||
iv1 = holder;
|
||||
uint32_t v1 = holder;
|
||||
iv1 >>= 32;
|
||||
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;
|
||||
#ifdef WORDS_BIGENDIAN
|
||||
((uint64_t*)dstdat)[i] = iv2^holder;
|
||||
#else
|
||||
((uint64_t*)dstdat)[i] = __builtin_bswap64(iv2^holder);
|
||||
#endif
|
||||
holder = iv1;
|
||||
iv1 = ((uint64_t)v0<<32) | (uint64_t)v1;
|
||||
iv1 = iv1 ^ iv2;
|
||||
iv2 = holder;
|
||||
((uint64_t*)dst)[i] = iv1;
|
||||
}
|
||||
int start = (dstdat[0]&7)+3;
|
||||
out->len = src->len-7-start;
|
||||
out->data = dstdat+start;
|
||||
out->ptr = dstdat;
|
||||
return out->len;
|
||||
return dstlen;
|
||||
}
|
||||
|
||||
int64_t tea_decrypt(const TEA t[4], const uint32_t sumtable[0x10], const TEADAT* src, TEADAT* out) {
|
||||
if (src->len < 16 || (src->len)%8 != 0) return 0;
|
||||
uint8_t* dstdat = (uint8_t*)malloc(src->len);
|
||||
// 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) {
|
||||
if (srclen < 16 || srclen%8 != 0) return 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
|
||||
iv1 = ((uint64_t*)(src->data))[i];
|
||||
iv1 = ((uint64_t*)(src))[i];
|
||||
#else
|
||||
iv1 = __builtin_bswap64(((uint64_t*)(src->data))[i]);
|
||||
iv1 = __builtin_bswap64(((uint64_t*)(src))[i]);
|
||||
#endif
|
||||
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[2]) ^ ((v0 >> 5) + t[3]);
|
||||
v0 -= (v1 + sumtable[i]) ^ ((v1 << 4) + t[0]) ^ ((v1 >> 5) + t[1]);
|
||||
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;
|
||||
#ifdef WORDS_BIGENDIAN
|
||||
((uint64_t*)dstdat)[i] = iv2^holder;
|
||||
((uint64_t*)dst)[i] = iv2^holder;
|
||||
#else
|
||||
((uint64_t*)dstdat)[i] = __builtin_bswap64(iv2^holder);
|
||||
((uint64_t*)dst)[i] = __builtin_bswap64(iv2^holder);
|
||||
#endif
|
||||
holder = iv1;
|
||||
}
|
||||
int start = (dstdat[0]&7)+3;
|
||||
out->len = src->len-7-start;
|
||||
out->data = dstdat+start;
|
||||
out->ptr = dstdat;
|
||||
return out->len;
|
||||
int start = (dst[0]&7)+3;
|
||||
return dst+start;
|
||||
}
|
||||
|
||||
int64_t tea_decrypt_native_endian(const TEA t[4], const uint32_t sumtable[0x10], const TEADAT* src, TEADAT* out) {
|
||||
if (src->len < 16 || (src->len)%8 != 0) return 0;
|
||||
uint8_t* dstdat = (uint8_t*)malloc(src->len);
|
||||
// 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) {
|
||||
if (srclen < 16 || srclen%8 != 0) return 0;
|
||||
uint64_t iv1, iv2 = 0, holder = 0;
|
||||
for(int64_t i = 0; i < src->len/8; i++) {
|
||||
iv1 = ((uint64_t*)(src->data))[i];
|
||||
for(int64_t i = 0; i < srclen/8; i++) {
|
||||
#ifdef WORDS_BIGENDIAN
|
||||
iv1 = ((uint64_t*)(src))[i];
|
||||
#else
|
||||
iv1 = __builtin_bswap64(((uint64_t*)(src))[i]);
|
||||
#endif
|
||||
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[2]) ^ ((v0 >> 5) + t[3]);
|
||||
v0 -= (v1 + sumtable[i]) ^ ((v1 << 4) + t[0]) ^ ((v1 >> 5) + t[1]);
|
||||
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*)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;
|
||||
}
|
||||
int start = (dstdat[0]&7)+3;
|
||||
out->len = src->len-7-start;
|
||||
out->data = dstdat+start;
|
||||
out->ptr = dstdat;
|
||||
return out->len;
|
||||
int start = (dst[0]&7)+3;
|
||||
return dst+start;
|
||||
}
|
||||
|
||||
// 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
|
||||
int main(int argc, char **argv) {
|
||||
TEADAT* td = (TEADAT*)malloc(sizeof(TEADAT));
|
||||
TEA* t = (TEA*)"32107654BA98FEDC";
|
||||
TEADAT out;
|
||||
TEA t;
|
||||
memcpy(t.t, "32107654BA98FEDC", 4*8);
|
||||
|
||||
if (argc != 3) {
|
||||
printf("usage: %s -[e|d] 'string'\n", argv[0]);
|
||||
@@ -246,37 +280,39 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
switch(argv[1][1]) {
|
||||
case 'e':
|
||||
td->data = (uint8_t*)(argv[2]);
|
||||
td->len = strlen(argv[2]);
|
||||
tea_encrypt_qq(t, td, &out);
|
||||
uint8_t* data = (uint8_t*)(argv[2]);
|
||||
int64_t datalen = strlen(argv[2]);
|
||||
int64_t outlen = tea_encrypt_len(datalen);
|
||||
uint8_t* out = (uint8_t*)malloc(outlen);
|
||||
tea_encrypt_qq(t, data, datalen, out);
|
||||
// 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');
|
||||
free(out.ptr);
|
||||
free(out);
|
||||
break;
|
||||
case 'd':
|
||||
td->len = strlen(argv[2])/2;
|
||||
int64_t datalen = strlen(argv[2])/2;
|
||||
// printf("decode input len: %lld\n", td->len);
|
||||
td->data = malloc(td->len);
|
||||
int i = td->len;
|
||||
uint8_t* data = malloc(datalen);
|
||||
int i = datalen;
|
||||
while (i--) {
|
||||
int x;
|
||||
sscanf(argv[2]+i*2, "%02x", &x);
|
||||
td->data[i] = x;
|
||||
data[i] = x;
|
||||
argv[2][i*2] = 0;
|
||||
}
|
||||
if (tea_decrypt_qq(t, td, &out)) {
|
||||
out.data[out.len] = 0;
|
||||
uint8_t* out = (uint8_t*)malloc(datalen);
|
||||
uint8_t* outdat = tea_decrypt_qq(t, data, datalen, out);
|
||||
if (outdat) {
|
||||
// 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');
|
||||
free(out.ptr);
|
||||
free(out);
|
||||
} else puts("decode error!");
|
||||
free(td->data);
|
||||
free(data);
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
free(td);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user