1
0
mirror of https://github.com/fumiama/simple-crypto.git synced 2026-06-06 10:20: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

View File

@@ -20,9 +20,9 @@ make install
```c
#include <simplecrypto.h>
```
2. Call functions. Don't forget to `free` the returned digest.
2. Call functions.
```c
uint8_t* md5(const uint8_t *data, size_t data_len);
uint8_t* md5(const uint8_t *data, size_t data_len, uint8_t digest[16]);
TEADAT* tea_encrypt_qq(const TEA t[4], const TEADAT* src);
TEADAT* tea_encrypt(const TEA t[4], const uint32_t sumtable[0x10], const TEADAT* src);

72
md5.c
View File

@@ -49,7 +49,11 @@ static uint32_t to_uint32(const uint8_t *bytes) {
}
uint8_t* md5(const uint8_t *data, size_t data_len, uint8_t digest[16]) {
uint32_t w[16];
#ifdef WORDS_BIGENDIAN
uint32_t w[16];
#else
uint32_t* w;
#endif
// These vars will contain the hash
// Initialize variables - simple count in nibbles:
@@ -85,8 +89,12 @@ uint8_t* md5(const uint8_t *data, size_t data_len, uint8_t digest[16]) {
//for each 512-bit chunk of message:
for(offset=0; offset<new_len; offset += (512/8)) {
// break chunk into sixteen 32-bit words w[j], 0 ≤ j ≤ 15
for (i = 0; i < 16; i++)
w[i] = to_uint32(msg + offset + i*4);
#ifdef WORDS_BIGENDIAN
for (i = 0; i < 16; i++)
w[i] = to_uint32(msg + offset + i*4);
#else
w = (uint32_t*)(msg + offset);
#endif
// Initialize hash value for this chunk:
a = h0;
@@ -95,28 +103,41 @@ uint8_t* md5(const uint8_t *data, size_t data_len, uint8_t digest[16]) {
d = h3;
// Main loop:
for(i = 0; i<64; i++) {
if (i < 16) {
f = (b & c) | ((~b) & d);
g = i;
} else if (i < 32) {
f = (d & b) | ((~d) & c);
g = (5*i + 1) % 16;
} else if (i < 48) {
f = b ^ c ^ d;
g = (3*i + 5) % 16;
} else {
f = c ^ (b | (~d));
g = (7*i) % 16;
}
for(i = 0; i < 16; i++) {
f = (b & c) | ((~b) & d);
g = i;
temp = d;
d = c;
c = b;
b = b + LEFTROTATE((a + f + k[i] + w[g]), r[i]);
b += LEFTROTATE((a + f + k[i] + w[g]), r[i]);
a = temp;
}
for(i = 16; i < 32; i++) {
f = (d & b) | ((~d) & c);
g = (5*i + 1) % 16;
temp = d;
d = c;
c = b;
b += LEFTROTATE((a + f + k[i] + w[g]), r[i]);
a = temp;
}
for(i = 32; i < 48; i++) {
f = b ^ c ^ d;
g = (3*i + 5) % 16;
temp = d;
d = c;
c = b;
b += LEFTROTATE((a + f + k[i] + w[g]), r[i]);
a = temp;
}
for(i = 48; i < 64; i++) {
f = c ^ (b | (~d));
g = (7*i) % 16;
temp = d;
d = c;
c = b;
b += LEFTROTATE((a + f + k[i] + w[g]), r[i]);
a = temp;
}
// Add this chunk's hash to result so far:
@@ -124,7 +145,6 @@ uint8_t* md5(const uint8_t *data, size_t data_len, uint8_t digest[16]) {
h1 += b;
h2 += c;
h3 += d;
}
// cleanup
@@ -139,11 +159,12 @@ uint8_t* md5(const uint8_t *data, size_t data_len, uint8_t digest[16]) {
}
#ifdef TEST_SIMPLE_CRYPTO
#include <stdio.h>
int main(int argc, char **argv) {
char *msg;
size_t len;
int i;
uint8_t* result;
uint8_t result[16];
if (argc < 2) {
printf("usage: %s 'string'\n", argv[0]);
@@ -152,12 +173,11 @@ int main(int argc, char **argv) {
msg = argv[1];
len = strlen(msg);
result = md5((uint8_t*)msg, len);
md5((uint8_t*)msg, len, result);
// display result
for (i = 0; i < 16; i++)
printf("%2.2x", result[i]);
puts("");
free(result);
putchar('\n');
return 0;
}

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) {