mirror of
https://github.com/fumiama/simple-dict.git
synced 2026-06-27 15:30:23 +08:00
优化tea
This commit is contained in:
57
crypto.c
57
crypto.c
@@ -47,48 +47,47 @@ void reset_seq(int index) {
|
|||||||
|
|
||||||
char* raw_encrypt(const char* buf, off_t* len, int index, const char pwd[64]) {
|
char* raw_encrypt(const char* buf, off_t* len, int index, const char pwd[64]) {
|
||||||
TEADAT tin = {*len, (uint8_t*)buf};
|
TEADAT tin = {*len, (uint8_t*)buf};
|
||||||
|
TEADAT tout;
|
||||||
TEA tea[4];
|
TEA tea[4];
|
||||||
|
|
||||||
((uint64_t*)tea)[0] = ((uint64_t*)pwd)[0];
|
((uint64_t*)tea)[0] = ((uint64_t*)pwd)[0];
|
||||||
((uint64_t*)tea)[1] = ((uint64_t*)pwd)[1];
|
((uint64_t*)tea)[1] = ((uint64_t*)pwd)[1];
|
||||||
((uint8_t*)tea)[15] = seqs[index]++;
|
((uint8_t*)tea)[15] = seqs[index]++;
|
||||||
TEADAT* tout = tea_encrypt_native_endian(tea, sumtable, &tin);
|
tea_encrypt_native_endian(tea, sumtable, &tin, &tout);
|
||||||
|
|
||||||
*len = tout->len;
|
*len = tout.len;
|
||||||
char* encbuf = (char*)malloc(*len);
|
char* encbuf = (char*)malloc(*len);
|
||||||
memcpy(encbuf, tout->data, *len);
|
memcpy(encbuf, tout.data, *len);
|
||||||
free(tout->ptr);
|
free(tout.ptr);
|
||||||
free(tout);
|
|
||||||
|
|
||||||
return encbuf;
|
return encbuf;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* raw_decrypt(const char* buf, off_t* len, int index, const char pwd[64]) {
|
char* raw_decrypt(const char* buf, off_t* len, int index, const char pwd[64]) {
|
||||||
TEADAT tin = {*len, (uint8_t*)buf};
|
TEADAT tin = {*len, (uint8_t*)buf};
|
||||||
|
TEADAT tout;
|
||||||
TEA tea[4];
|
TEA tea[4];
|
||||||
|
|
||||||
((uint64_t*)tea)[0] = ((uint64_t*)pwd)[0];
|
((uint64_t*)tea)[0] = ((uint64_t*)pwd)[0];
|
||||||
((uint64_t*)tea)[1] = ((uint64_t*)pwd)[1];
|
((uint64_t*)tea)[1] = ((uint64_t*)pwd)[1];
|
||||||
((uint8_t*)tea)[15] = seqs[index];
|
((uint8_t*)tea)[15] = seqs[index];
|
||||||
TEADAT* tout = tea_decrypt_native_endian(tea, sumtable, &tin);
|
if(!tea_decrypt_native_endian(tea, sumtable, &tin, &tout)) return NULL;
|
||||||
if(!tout) return NULL;
|
else if(tout.len <= 0) {
|
||||||
else if(tout->len <= 0) {
|
free(tout.ptr);
|
||||||
free(tout->ptr);
|
|
||||||
free(tout);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
} else seqs[index]++;
|
} else seqs[index]++;
|
||||||
|
|
||||||
*len = tout->len;
|
*len = tout.len;
|
||||||
char* decbuf = (char*)malloc(*len);
|
char* decbuf = (char*)malloc(*len);
|
||||||
memcpy(decbuf, tout->data, *len);
|
memcpy(decbuf, tout.data, *len);
|
||||||
free(tout->ptr);
|
free(tout.ptr);
|
||||||
free(tout);
|
|
||||||
|
|
||||||
return decbuf;
|
return decbuf;
|
||||||
}
|
}
|
||||||
|
|
||||||
void cmdpacket_encrypt(CMDPACKET* p, int index, const char pwd[64]) {
|
void cmdpacket_encrypt(CMDPACKET* p, int index, const char pwd[64]) {
|
||||||
TEADAT tin = {p->datalen, p->data};
|
TEADAT tin = {p->datalen, p->data};
|
||||||
|
TEADAT tout;
|
||||||
TEA tea[4];
|
TEA tea[4];
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
printf("encrypt len: %d, data: ", p->datalen);
|
printf("encrypt len: %d, data: ", p->datalen);
|
||||||
@@ -106,7 +105,7 @@ void cmdpacket_encrypt(CMDPACKET* p, int index, const char pwd[64]) {
|
|||||||
putchar('\n');
|
putchar('\n');
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
TEADAT* tout = tea_encrypt_native_endian(tea, sumtable, &tin);
|
tea_encrypt_native_endian(tea, sumtable, &tin, &tout);
|
||||||
|
|
||||||
md5(p->data, p->datalen, p->md5);
|
md5(p->data, p->datalen, p->md5);
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
@@ -115,21 +114,21 @@ void cmdpacket_encrypt(CMDPACKET* p, int index, const char pwd[64]) {
|
|||||||
putchar('\n');
|
putchar('\n');
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
p->datalen = tout->len;
|
p->datalen = tout.len;
|
||||||
memcpy(p->data, tout->data, p->datalen);
|
memcpy(p->data, tout.data, p->datalen);
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
printf("encrypted data len: %d, data: ", p->datalen);
|
printf("encrypted data len: %d, data: ", p->datalen);
|
||||||
for(int i = 0; i < p->datalen; i++) printf("%02x", p->data[i]);
|
for(int i = 0; i < p->datalen; i++) printf("%02x", p->data[i]);
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
#endif
|
#endif
|
||||||
free(tout->ptr);
|
free(tout.ptr);
|
||||||
free(tout);
|
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int cmdpacket_decrypt(CMDPACKET* p, int index, const char pwd[64]) {
|
int cmdpacket_decrypt(CMDPACKET* p, int index, const char pwd[64]) {
|
||||||
TEADAT tin = {p->datalen, p->data};
|
TEADAT tin = {p->datalen, p->data};
|
||||||
|
TEADAT tout;
|
||||||
TEA tea[4];
|
TEA tea[4];
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
printf("decrypt len: %d, data: ", p->datalen);
|
printf("decrypt len: %d, data: ", p->datalen);
|
||||||
@@ -147,15 +146,13 @@ int cmdpacket_decrypt(CMDPACKET* p, int index, const char pwd[64]) {
|
|||||||
putchar('\n');
|
putchar('\n');
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
TEADAT* tout = tea_decrypt_native_endian(tea, sumtable, &tin);
|
if(!tea_decrypt_native_endian(tea, sumtable, &tin, &tout)) return 0;
|
||||||
if(!tout) return 0;
|
if(tout.len <= 0) {
|
||||||
if(tout->len <= 0) {
|
free(tout.ptr);
|
||||||
free(tout->ptr);
|
|
||||||
free(tout);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
uint8_t datamd5[16];
|
uint8_t datamd5[16];
|
||||||
md5(tout->data, tout->len, datamd5);
|
md5(tout.data, tout.len, datamd5);
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
printf("decrypt md5: ");
|
printf("decrypt md5: ");
|
||||||
for(int i = 0; i < 16; i++) printf("%02x", datamd5[i]);
|
for(int i = 0; i < 16; i++) printf("%02x", datamd5[i]);
|
||||||
@@ -166,13 +163,11 @@ int cmdpacket_decrypt(CMDPACKET* p, int index, const char pwd[64]) {
|
|||||||
#endif
|
#endif
|
||||||
if(is_md5_equal((uint8_t*)datamd5, p->md5)) {
|
if(is_md5_equal((uint8_t*)datamd5, p->md5)) {
|
||||||
seqs[index]++;
|
seqs[index]++;
|
||||||
p->datalen = tout->len;
|
p->datalen = tout.len;
|
||||||
memcpy(p->data, tout->data, p->datalen);
|
memcpy(p->data, tout.data, p->datalen);
|
||||||
free(tout->ptr);
|
free(tout.ptr);
|
||||||
free(tout);
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
free(tout->ptr);
|
free(tout.ptr);
|
||||||
free(tout);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user