1
0
mirror of https://github.com/fumiama/simple-dict.git synced 2026-06-12 06:00:31 +08:00
This commit is contained in:
fumiama
2021-12-12 14:43:54 +08:00
parent 5abe175e90
commit 53f46ac1d3
4 changed files with 185 additions and 89 deletions

View File

@@ -3,6 +3,8 @@
#include <time.h>
#include "crypto.h"
//#define DEBUG
// TEA encoding sumtable
static const uint32_t sumtable[0x10] = {
0x9e3579b9,
@@ -69,8 +71,10 @@ char* raw_decrypt(const char* buf, off_t* len, int index, const char pwd[64]) {
((uint64_t*)tea)[0] = ((uint64_t*)pwd)[0];
((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(!tout) return NULL;
else seqs[index]++;
*len = tout->len;
char* decbuf = (char*)malloc(*len);
@@ -84,18 +88,40 @@ char* raw_decrypt(const char* buf, off_t* len, int index, const char pwd[64]) {
void cmdpacket_encrypt(CMDPACKET* p, int index, const char pwd[64]) {
TEADAT tin = {p->datalen, p->data};
TEA tea[4];
#ifdef DEBUG
printf("encrypt len: %d, data: ", p->datalen);
for(int i = 0; i < p->datalen; i++) printf("%02x", p->data[i]);
putchar('\n');
#endif
((uint64_t*)tea)[0] = ((uint64_t*)pwd)[0];
((uint64_t*)tea)[1] = ((uint64_t*)pwd)[1];
((uint8_t*)tea)[15] = seqs[index]++;
#ifdef DEBUG
printf("encrypt tea: ");
for(int i = 0; i < 16; i++) printf("%02x", ((uint8_t*)tea)[i]);
putchar('\n');
#endif
TEADAT* tout = tea_encrypt_native_endian(tea, sumtable, &tin);
uint8_t* datamd5 = md5(p->data, p->datalen);
#ifdef DEBUG
printf("encrypt md5: ");
for(int i = 0; i < 16; i++) printf("%02x", datamd5[i]);
putchar('\n');
#endif
memcpy(p->md5, datamd5, 16);
free(datamd5);
p->datalen = tout->len;
memcpy(p->data, tout->data, p->datalen);
#ifdef DEBUG
printf("encrypted data len: %d, data: ", p->datalen);
for(int i = 0; i < p->datalen; i++) printf("%02x", p->data[i]);
putchar('\n');
#endif
free(tout->ptr);
free(tout);
@@ -105,13 +131,34 @@ void cmdpacket_encrypt(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};
TEA tea[4];
#ifdef DEBUG
printf("decrypt len: %d, data: ", p->datalen);
for(int i = 0; i < p->datalen; i++) printf("%02x", p->data[i]);
putchar('\n');
#endif
((uint64_t*)tea)[0] = ((uint64_t*)pwd)[0];
((uint64_t*)tea)[1] = ((uint64_t*)pwd)[1];
((uint8_t*)tea)[15] = seqs[index];
#ifdef DEBUG
printf("decrypt tea: ");
for(int i = 0; i < 16; i++) printf("%02x", ((uint8_t*)tea)[i]);
putchar('\n');
#endif
TEADAT* tout = tea_decrypt_native_endian(tea, sumtable, &tin);
if(!tout) return 0;
uint8_t* datamd5 = md5(tout->data, tout->len);
#ifdef DEBUG
printf("decrypt md5: ");
for(int i = 0; i < 16; i++) printf("%02x", datamd5[i]);
putchar('\n');
printf("decrypted data len: %d, data: ", tout->len);
for(int i = 0; i < tout->len; i++) printf("%02x", tout->data[i]);
putchar('\n');
#endif
if(is_md5_equal(datamd5, p->md5)) {
seqs[index]++;
p->datalen = tout->len;