mirror of
https://github.com/fumiama/simple-dict.git
synced 2026-06-30 08:50:25 +08:00
inline crypto
This commit is contained in:
@@ -17,8 +17,8 @@ link_directories("/usr/local/lib")
|
|||||||
add_compile_options(-std=gnu99)
|
add_compile_options(-std=gnu99)
|
||||||
message(STATUS "optional:-std=gnu99")
|
message(STATUS "optional:-std=gnu99")
|
||||||
|
|
||||||
add_executable(simple-dict-server server.c crypto.c)
|
add_executable(simple-dict-server server.c)
|
||||||
add_executable(simple-dict-client client.c crypto.c)
|
add_executable(simple-dict-client client.c)
|
||||||
add_executable(cfgwriter cfgwriter.c)
|
add_executable(cfgwriter cfgwriter.c)
|
||||||
#add_executable(migrate migrate.c)
|
#add_executable(migrate migrate.c)
|
||||||
#add_executable(migratenew migratenew.c)
|
#add_executable(migratenew migratenew.c)
|
||||||
|
|||||||
173
crypto.c
173
crypto.c
@@ -1,173 +0,0 @@
|
|||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <time.h>
|
|
||||||
#include "crypto.h"
|
|
||||||
|
|
||||||
// TEA encoding sumtable
|
|
||||||
static const uint32_t sumtable[0x10] = {
|
|
||||||
0x9e3579b9,
|
|
||||||
0x3c6ef172,
|
|
||||||
0xd2a66d2b,
|
|
||||||
0x78dd36e4,
|
|
||||||
0x17e5609d,
|
|
||||||
0xb54fda56,
|
|
||||||
0x5384560f,
|
|
||||||
0xf1bb77c8,
|
|
||||||
0x8ff24781,
|
|
||||||
0x2e4ac13a,
|
|
||||||
0xcc653af3,
|
|
||||||
0x6a9964ac,
|
|
||||||
0x08d12965,
|
|
||||||
0xa708081e,
|
|
||||||
0x451221d7,
|
|
||||||
0xe37793d0,
|
|
||||||
};
|
|
||||||
|
|
||||||
static uint8_t seqs[THREADCNT]; // 消息序号
|
|
||||||
|
|
||||||
static inline int is_md5_equal(uint8_t* digest, uint8_t* digest2) {
|
|
||||||
#ifdef CPUBIT64
|
|
||||||
return (digest[0] == digest2[0]) &&
|
|
||||||
(digest[1] == digest2[1]);
|
|
||||||
#else
|
|
||||||
return (digest[0] == digest2[0]) &&
|
|
||||||
(digest[1] == digest2[1]) &&
|
|
||||||
(digest[2] == digest2[2]) &&
|
|
||||||
(digest[3] == digest2[3]);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
void init_crypto() {
|
|
||||||
srand(time(NULL));
|
|
||||||
}
|
|
||||||
|
|
||||||
void reset_seq(int index) {
|
|
||||||
seqs[index] = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
char* raw_encrypt(const char* buf, off_t* len, int index, const char pwd[64]) {
|
|
||||||
TEADAT tin = {*len, (uint8_t*)buf};
|
|
||||||
TEADAT tout;
|
|
||||||
TEA tea[4];
|
|
||||||
|
|
||||||
((uint64_t*)tea)[0] = ((uint64_t*)pwd)[0];
|
|
||||||
((uint64_t*)tea)[1] = ((uint64_t*)pwd)[1];
|
|
||||||
((uint8_t*)tea)[15] = seqs[index]++;
|
|
||||||
tea_encrypt_native_endian(tea, sumtable, &tin, &tout);
|
|
||||||
|
|
||||||
*len = tout.len;
|
|
||||||
char* encbuf = (char*)malloc(*len);
|
|
||||||
memcpy(encbuf, tout.data, *len);
|
|
||||||
free(tout.ptr);
|
|
||||||
|
|
||||||
return encbuf;
|
|
||||||
}
|
|
||||||
|
|
||||||
char* raw_decrypt(const char* buf, off_t* len, int index, const char pwd[64]) {
|
|
||||||
TEADAT tin = {*len, (uint8_t*)buf};
|
|
||||||
TEADAT tout;
|
|
||||||
TEA tea[4];
|
|
||||||
|
|
||||||
((uint64_t*)tea)[0] = ((uint64_t*)pwd)[0];
|
|
||||||
((uint64_t*)tea)[1] = ((uint64_t*)pwd)[1];
|
|
||||||
((uint8_t*)tea)[15] = seqs[index];
|
|
||||||
if(!tea_decrypt_native_endian(tea, sumtable, &tin, &tout)) return NULL;
|
|
||||||
else if(tout.len <= 0) {
|
|
||||||
free(tout.ptr);
|
|
||||||
return NULL;
|
|
||||||
} else seqs[index]++;
|
|
||||||
|
|
||||||
*len = tout.len;
|
|
||||||
char* decbuf = (char*)malloc(*len);
|
|
||||||
memcpy(decbuf, tout.data, *len);
|
|
||||||
free(tout.ptr);
|
|
||||||
|
|
||||||
return decbuf;
|
|
||||||
}
|
|
||||||
|
|
||||||
void cmdpacket_encrypt(CMDPACKET* p, int index, const char pwd[64]) {
|
|
||||||
TEADAT tin = {p->datalen, p->data};
|
|
||||||
TEADAT tout;
|
|
||||||
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
|
|
||||||
|
|
||||||
tea_encrypt_native_endian(tea, sumtable, &tin, &tout);
|
|
||||||
|
|
||||||
md5(p->data, p->datalen, p->md5);
|
|
||||||
#ifdef DEBUG
|
|
||||||
printf("encrypt md5: ");
|
|
||||||
for(int i = 0; i < 16; i++) printf("%02x", p->md5[i]);
|
|
||||||
putchar('\n');
|
|
||||||
#endif
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
int cmdpacket_decrypt(CMDPACKET* p, int index, const char pwd[64]) {
|
|
||||||
TEADAT tin = {p->datalen, p->data};
|
|
||||||
TEADAT tout;
|
|
||||||
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
|
|
||||||
|
|
||||||
if(!tea_decrypt_native_endian(tea, sumtable, &tin, &tout)) return 0;
|
|
||||||
if(tout.len <= 0) {
|
|
||||||
free(tout.ptr);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
uint8_t datamd5[16];
|
|
||||||
md5(tout.data, tout.len, datamd5);
|
|
||||||
#ifdef DEBUG
|
|
||||||
printf("decrypt md5: ");
|
|
||||||
for(int i = 0; i < 16; i++) printf("%02x", datamd5[i]);
|
|
||||||
putchar('\n');
|
|
||||||
printf("decrypted data len: %u, data: ", (unsigned int)tout->len);
|
|
||||||
for(int i = 0; i < tout->len; i++) printf("%02x", tout->data[i]);
|
|
||||||
putchar('\n');
|
|
||||||
#endif
|
|
||||||
if(is_md5_equal((uint8_t*)datamd5, p->md5)) {
|
|
||||||
seqs[index]++;
|
|
||||||
p->datalen = tout.len;
|
|
||||||
memcpy(p->data, tout.data, p->datalen);
|
|
||||||
free(tout.ptr);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
free(tout.ptr);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
180
crypto.h
180
crypto.h
@@ -6,11 +6,179 @@
|
|||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include "server.h"
|
#include "server.h"
|
||||||
|
|
||||||
void init_crypto();
|
#include <stdlib.h>
|
||||||
void reset_seq(int index);
|
#include <string.h>
|
||||||
char* raw_encrypt(const char* buf, off_t* len, int index, const char pwd[64]);
|
#include <time.h>
|
||||||
char* raw_decrypt(const char* buf, off_t* len, int index, const char pwd[64]);
|
#include "crypto.h"
|
||||||
void cmdpacket_encrypt(CMDPACKET* p, int index, const char pwd[64]);
|
|
||||||
int cmdpacket_decrypt(CMDPACKET* p, int index, const char pwd[64]);
|
// TEA encoding sumtable
|
||||||
|
static const uint32_t sumtable[0x10] = {
|
||||||
|
0x9e3579b9,
|
||||||
|
0x3c6ef172,
|
||||||
|
0xd2a66d2b,
|
||||||
|
0x78dd36e4,
|
||||||
|
0x17e5609d,
|
||||||
|
0xb54fda56,
|
||||||
|
0x5384560f,
|
||||||
|
0xf1bb77c8,
|
||||||
|
0x8ff24781,
|
||||||
|
0x2e4ac13a,
|
||||||
|
0xcc653af3,
|
||||||
|
0x6a9964ac,
|
||||||
|
0x08d12965,
|
||||||
|
0xa708081e,
|
||||||
|
0x451221d7,
|
||||||
|
0xe37793d0,
|
||||||
|
};
|
||||||
|
|
||||||
|
static uint8_t seqs[THREADCNT]; // 消息序号
|
||||||
|
|
||||||
|
static inline int is_md5_equal(uint8_t* digest, uint8_t* digest2) {
|
||||||
|
#ifdef CPUBIT64
|
||||||
|
return (digest[0] == digest2[0]) &&
|
||||||
|
(digest[1] == digest2[1]);
|
||||||
|
#else
|
||||||
|
return (digest[0] == digest2[0]) &&
|
||||||
|
(digest[1] == digest2[1]) &&
|
||||||
|
(digest[2] == digest2[2]) &&
|
||||||
|
(digest[3] == digest2[3]);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static void init_crypto() {
|
||||||
|
srand(time(NULL));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void reset_seq(int index) {
|
||||||
|
seqs[index] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static char* raw_encrypt(const char* buf, off_t* len, int index, const char pwd[64]) {
|
||||||
|
TEADAT tin = {*len, (uint8_t*)buf};
|
||||||
|
TEADAT tout;
|
||||||
|
TEA tea[4];
|
||||||
|
|
||||||
|
((uint64_t*)tea)[0] = ((uint64_t*)pwd)[0];
|
||||||
|
((uint64_t*)tea)[1] = ((uint64_t*)pwd)[1];
|
||||||
|
((uint8_t*)tea)[15] = seqs[index]++;
|
||||||
|
tea_encrypt_native_endian(tea, sumtable, &tin, &tout);
|
||||||
|
|
||||||
|
*len = tout.len;
|
||||||
|
char* encbuf = (char*)malloc(*len);
|
||||||
|
memcpy(encbuf, tout.data, *len);
|
||||||
|
free(tout.ptr);
|
||||||
|
|
||||||
|
return encbuf;
|
||||||
|
}
|
||||||
|
|
||||||
|
static char* raw_decrypt(const char* buf, off_t* len, int index, const char pwd[64]) {
|
||||||
|
TEADAT tin = {*len, (uint8_t*)buf};
|
||||||
|
TEADAT tout;
|
||||||
|
TEA tea[4];
|
||||||
|
|
||||||
|
((uint64_t*)tea)[0] = ((uint64_t*)pwd)[0];
|
||||||
|
((uint64_t*)tea)[1] = ((uint64_t*)pwd)[1];
|
||||||
|
((uint8_t*)tea)[15] = seqs[index];
|
||||||
|
if(!tea_decrypt_native_endian(tea, sumtable, &tin, &tout)) return NULL;
|
||||||
|
else if(tout.len <= 0) {
|
||||||
|
free(tout.ptr);
|
||||||
|
return NULL;
|
||||||
|
} else seqs[index]++;
|
||||||
|
|
||||||
|
*len = tout.len;
|
||||||
|
char* decbuf = (char*)malloc(*len);
|
||||||
|
memcpy(decbuf, tout.data, *len);
|
||||||
|
free(tout.ptr);
|
||||||
|
|
||||||
|
return decbuf;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void cmdpacket_encrypt(CMDPACKET* p, int index, const char pwd[64]) {
|
||||||
|
TEADAT tin = {p->datalen, p->data};
|
||||||
|
TEADAT tout;
|
||||||
|
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
|
||||||
|
|
||||||
|
tea_encrypt_native_endian(tea, sumtable, &tin, &tout);
|
||||||
|
|
||||||
|
md5(p->data, p->datalen, p->md5);
|
||||||
|
#ifdef DEBUG
|
||||||
|
printf("encrypt md5: ");
|
||||||
|
for(int i = 0; i < 16; i++) printf("%02x", p->md5[i]);
|
||||||
|
putchar('\n');
|
||||||
|
#endif
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cmdpacket_decrypt(CMDPACKET* p, int index, const char pwd[64]) {
|
||||||
|
TEADAT tin = {p->datalen, p->data};
|
||||||
|
TEADAT tout;
|
||||||
|
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
|
||||||
|
|
||||||
|
if(!tea_decrypt_native_endian(tea, sumtable, &tin, &tout)) return 0;
|
||||||
|
if(tout.len <= 0) {
|
||||||
|
free(tout.ptr);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
uint8_t datamd5[16];
|
||||||
|
md5(tout.data, tout.len, datamd5);
|
||||||
|
#ifdef DEBUG
|
||||||
|
printf("decrypt md5: ");
|
||||||
|
for(int i = 0; i < 16; i++) printf("%02x", datamd5[i]);
|
||||||
|
putchar('\n');
|
||||||
|
printf("decrypted data len: %u, data: ", (unsigned int)tout->len);
|
||||||
|
for(int i = 0; i < tout->len; i++) printf("%02x", tout->data[i]);
|
||||||
|
putchar('\n');
|
||||||
|
#endif
|
||||||
|
if(is_md5_equal((uint8_t*)datamd5, p->md5)) {
|
||||||
|
seqs[index]++;
|
||||||
|
p->datalen = tout.len;
|
||||||
|
memcpy(p->data, tout.data, p->datalen);
|
||||||
|
free(tout.ptr);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
free(tout.ptr);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#endif /* _CRYPTO_H_ */
|
#endif /* _CRYPTO_H_ */
|
||||||
|
|||||||
2
dict.h
2
dict.h
@@ -131,7 +131,7 @@ static void close_dict(uint8_t lock_type, uint32_t index, pthread_rwlock_t* mu)
|
|||||||
puts("Close dict");
|
puts("Close dict");
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline int is_md5_equal(uint8_t* digest) {
|
static inline int is_dict_md5_equal(uint8_t* digest) {
|
||||||
#ifdef CPUBIT64
|
#ifdef CPUBIT64
|
||||||
uint64_t* digest2 = (uint64_t*)digest;
|
uint64_t* digest2 = (uint64_t*)digest;
|
||||||
return (digest2[0] == _dict_md5_2[0]) &&
|
return (digest2[0] == _dict_md5_2[0]) &&
|
||||||
|
|||||||
2
server.c
2
server.c
@@ -385,7 +385,7 @@ static int s4_del(THREADTIMER *timer) {
|
|||||||
static int s5_md5(THREADTIMER *timer) {
|
static int s5_md5(THREADTIMER *timer) {
|
||||||
//timer->status = 0;
|
//timer->status = 0;
|
||||||
fill_md5(&mu);
|
fill_md5(&mu);
|
||||||
if(is_md5_equal((uint8_t*)timer->dat)) return send_data(timer->accept_fd, timer->index, ACKNULL, "null", 4);
|
if(is_dict_md5_equal((uint8_t*)timer->dat)) return send_data(timer->accept_fd, timer->index, ACKNULL, "null", 4);
|
||||||
else return send_data(timer->accept_fd, timer->index, ACKNEQU, "nequ", 4);
|
else return send_data(timer->accept_fd, timer->index, ACKNEQU, "nequ", 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user