1
0
mirror of https://github.com/fumiama/simple-crypto.git synced 2026-06-10 21:50:24 +08:00

优化md5

This commit is contained in:
源文雨
2022-04-18 11:42:01 +08:00
parent 01e254f8e2
commit e46a95a06d
3 changed files with 50 additions and 110 deletions

84
tea.c
View File

@@ -29,49 +29,7 @@ const static uint32_t qqsumtable[0x10] = {
};
TEADAT* tea_encrypt_qq(const TEA t[4], const TEADAT* src) {
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);
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];
#else
uint64_t block = __builtin_bswap64(((uint64_t*)dstdat)[i]);
#endif
holder = block ^ iv1;
iv1 = holder;
uint32_t v1 = holder;
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]);
}
iv1 = ((uint64_t)v0<<32) | (uint64_t)v1;
iv1 = iv1 ^ iv2;
iv2 = holder;
#ifdef WORDS_BIGENDIAN
((uint64_t*)dstdat)[i] = iv1;
#else
((uint64_t*)dstdat)[i] = __builtin_bswap64(iv1);
#endif
}
TEADAT* dst = (TEADAT*)malloc(sizeof(TEADAT));
dst->len = dstlen;
dst->data = dstdat;
dst->ptr = dstdat;
return dst;
return tea_encrypt(t, qqsumtable, src);
}
TEADAT* tea_encrypt(const TEA t[4], const uint32_t sumtable[0x10], const TEADAT* src) {
@@ -159,45 +117,7 @@ TEADAT* tea_encrypt_native_endian(const TEA t[4], const uint32_t sumtable[0x10],
}
TEADAT* tea_decrypt_qq(const TEA t[4], const TEADAT* src) {
if (src->len < 16 || (src->len)%8 != 0) {
return NULL;
}
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]);
}
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;
}
TEADAT* dst = (TEADAT*)malloc(sizeof(TEADAT));
int start = (dstdat[0]&7)+3;
dst->len = src->len-7-start;
dst->data = dstdat+start;
dst->ptr = dstdat;
return dst;
return tea_decrypt(t, qqsumtable, src);
}
TEADAT* tea_decrypt(const TEA t[4], const uint32_t sumtable[0x10], const TEADAT* src) {