encrypt.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. #include "lib/aes.h"
  2. #include "lib/md5.h"
  3. #include "lib/pbkdf2-sha1.h"
  4. #include "lib/pbkdf2-sha256.h"
  5. #include <string.h>
  6. #include <stdint.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include "encrypt.h"
  10. #include "common.h"
  11. #include "log.h"
  12. //static uint64_t seq=1;
  13. static int8_t zero_iv[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0};//this prog use zero iv,you should make sure first block of data contains a random/nonce data
  14. /****
  15. * security of zero_iv + nonce first data block
  16. * https://crypto.stackexchange.com/questions/5421/using-cbc-with-a-fixed-iv-and-a-random-first-plaintext-block
  17. ****/
  18. char normal_key[16 + 100];//generated from key_string by md5. reserved for compatiblity
  19. const int hmac_key_len=32;
  20. const int cipher_key_len=32;
  21. unsigned char hmac_key_encrypt[hmac_key_len + 100]; //key for hmac
  22. unsigned char hmac_key_decrypt[hmac_key_len + 100]; //key for hmac
  23. unsigned char cipher_key_encrypt[cipher_key_len + 100]; //key for aes etc.
  24. unsigned char cipher_key_decrypt[cipher_key_len + 100]; //key for aes etc.
  25. unordered_map<int, const char *> auth_mode_tostring = {{auth_none, "none"}, {auth_md5, "md5"}, {auth_crc32, "crc32"},{auth_simple,"simple"},{auth_hmac_sha1,"hmac_sha1"},};
  26. unordered_map<int, const char *> cipher_mode_tostring={{cipher_none,"none"},{cipher_aes128cbc,"aes128cbc"},{cipher_xor,"xor"},};
  27. //TODO aes-gcm
  28. auth_mode_t auth_mode=auth_md5;
  29. cipher_mode_t cipher_mode=cipher_aes128cbc;
  30. int is_hmac_used=0;
  31. //TODO key negotiation and forward secrecy
  32. int my_init_keys(const char * user_passwd,int is_client)
  33. {
  34. char tmp[1000]="";
  35. int len=strlen(user_passwd);
  36. strcat(tmp,user_passwd);
  37. strcat(tmp,"key1");
  38. md5((uint8_t*)tmp,strlen(tmp),(uint8_t*)normal_key);
  39. if(auth_mode==auth_hmac_sha1)
  40. is_hmac_used=1;
  41. if(is_hmac_used)
  42. {
  43. unsigned char salt[400]="";
  44. char salt_text[400]="udp2raw_salt1";
  45. md5((uint8_t*)(salt_text),strlen(salt_text),salt); //TODO different salt per session
  46. unsigned char pbkdf2_output1[400]="";
  47. PKCS5_PBKDF2_HMAC_SHA256((uint8_t*)user_passwd,len,salt,16,10000, 32,pbkdf2_output1); //TODO argon2 ?
  48. //unsigned char pbkdf2_output2[400]="";
  49. //PKCS5_PBKDF2_HMAC_SHA256(pbkdf2_output1,32,0,0,1, hmac_key_len*2+cipher_key_len*2,pbkdf2_output2); //stretch it
  50. const char *info_hmac_encrypt="hmac_key server-->client";
  51. const char *info_hmac_decrypt="hmac_key client-->server";
  52. const char *info_cipher_encrypt="cipher_key server-->client";
  53. const char *info_cipher_decrypt="cipher_key client-->server";
  54. if(is_client)
  55. {
  56. const char *tmp;
  57. tmp=info_hmac_encrypt; info_hmac_encrypt=info_hmac_decrypt;info_hmac_decrypt=tmp;
  58. tmp=info_cipher_encrypt; info_cipher_encrypt=info_cipher_decrypt;info_cipher_decrypt=tmp;
  59. }
  60. else
  61. {
  62. //nop
  63. }
  64. assert( hkdf_sha256_expand( pbkdf2_output1,32, (unsigned char *)info_cipher_encrypt,strlen(info_cipher_encrypt), cipher_key_encrypt, cipher_key_len ) ==0);
  65. assert( hkdf_sha256_expand( pbkdf2_output1,32, (unsigned char *)info_cipher_decrypt,strlen(info_cipher_decrypt), cipher_key_decrypt, cipher_key_len ) ==0);
  66. assert( hkdf_sha256_expand( pbkdf2_output1,32, (unsigned char *)info_hmac_encrypt,strlen(info_hmac_encrypt), hmac_key_encrypt, hmac_key_len ) ==0);
  67. assert( hkdf_sha256_expand( pbkdf2_output1,32, (unsigned char *)info_hmac_decrypt,strlen(info_hmac_decrypt), hmac_key_decrypt, hmac_key_len ) ==0);
  68. }
  69. print_binary_chars(normal_key,16);
  70. print_binary_chars((char *)hmac_key_encrypt,32);
  71. print_binary_chars((char *)hmac_key_decrypt,32);
  72. print_binary_chars((char *)cipher_key_encrypt,32);
  73. print_binary_chars((char *)cipher_key_decrypt,32);
  74. return 0;
  75. }
  76. /*
  77. * this function comes from http://www.hackersdelight.org/hdcodetxt/crc.c.txt
  78. */
  79. unsigned int crc32h(unsigned char *message,int len) {
  80. int i, crc;
  81. unsigned int byte, c;
  82. const unsigned int g0 = 0xEDB88320, g1 = g0>>1,
  83. g2 = g0>>2, g3 = g0>>3, g4 = g0>>4, g5 = g0>>5,
  84. g6 = (g0>>6)^g0, g7 = ((g0>>6)^g0)>>1;
  85. i = 0;
  86. crc = 0xFFFFFFFF;
  87. while (i!=len) { // Get next byte.
  88. byte = message[i];
  89. crc = crc ^ byte;
  90. c = ((crc<<31>>31) & g7) ^ ((crc<<30>>31) & g6) ^
  91. ((crc<<29>>31) & g5) ^ ((crc<<28>>31) & g4) ^
  92. ((crc<<27>>31) & g3) ^ ((crc<<26>>31) & g2) ^
  93. ((crc<<25>>31) & g1) ^ ((crc<<24>>31) & g0);
  94. crc = ((unsigned)crc >> 8) ^ c;
  95. i = i + 1;
  96. }
  97. return ~crc;
  98. }
  99. /*
  100. void sum(const unsigned char *data,int len,unsigned char* res) {
  101. memset(res,0,sizeof(int));
  102. for(int i=0,j=0;i<len;i++,j++)
  103. {
  104. if(j==4) j=0;
  105. res[j]+=data[i];
  106. }
  107. return ;
  108. }*/
  109. void simple_hash(unsigned char *str,int len,unsigned char res[8]) //djb2+ sdbm
  110. {
  111. u32_t hash = 5381;
  112. u32_t hash2 = 0;
  113. int c;
  114. int i=0;
  115. while(c = *str++,i++!=len)
  116. {
  117. // hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
  118. hash = ((hash << 5) + hash)^c; /* (hash * 33) ^ c */
  119. hash2 = c + (hash2 << 6) + (hash2 << 16) - hash2;
  120. }
  121. hash=htonl(hash);
  122. hash2=htonl(hash2);
  123. memcpy(res,&hash,sizeof(hash));
  124. memcpy(res+sizeof(hash),&hash2,sizeof(hash2));
  125. }
  126. int auth_md5_cal(const char *data,char * output,int &len)
  127. {
  128. memcpy(output,data,len);//TODO inefficient code
  129. md5((unsigned char *)output,len,(unsigned char *)(output+len));
  130. len+=16;
  131. return 0;
  132. }
  133. int auth_hmac_sha1_cal(const char *data,char * output,int &len)
  134. {
  135. mylog(log_trace,"auth_hmac_sha1_cal() is called\n");
  136. memcpy(output,data,len);//TODO inefficient code
  137. sha1_hmac(hmac_key_encrypt, hmac_key_len, (const unsigned char *)data, len,(unsigned char *)(output+len));
  138. //md5((unsigned char *)output,len,(unsigned char *)(output+len));
  139. len+=20;
  140. return 0;
  141. }
  142. int auth_hmac_sha1_verify(const char *data,int &len)
  143. {
  144. mylog(log_trace,"auth_hmac_sha1_verify() is called\n");
  145. if(len<20)
  146. {
  147. mylog(log_trace,"auth_hmac_sha1_verify len<20\n");
  148. return -1;
  149. }
  150. char res[20];
  151. sha1_hmac(hmac_key_decrypt, hmac_key_len, (const unsigned char *)data, len-20,(unsigned char *)(res));
  152. if(memcmp(res,data+len-20,20)!=0)
  153. {
  154. mylog(log_trace,"auth_hmac_sha1 check failed\n");
  155. return -2;
  156. }
  157. len-=20;
  158. return 0;
  159. }
  160. int auth_crc32_cal(const char *data,char * output,int &len)
  161. {
  162. memcpy(output,data,len);//TODO inefficient code
  163. unsigned int ret=crc32h((unsigned char *)output,len);
  164. unsigned int ret_n=htonl(ret);
  165. memcpy(output+len,&ret_n,sizeof(unsigned int));
  166. len+=sizeof(unsigned int);
  167. return 0;
  168. }
  169. int auth_simple_cal(const char *data,char * output,int &len)
  170. {
  171. //char res[4];
  172. memcpy(output,data,len);//TODO inefficient code
  173. simple_hash((unsigned char *)output,len,(unsigned char *)(output+len));
  174. len+=8;
  175. return 0;
  176. }
  177. int auth_simple_verify(const char *data,int &len)
  178. {
  179. if(len<8) return -1;
  180. unsigned char res[8];
  181. len-=8;
  182. simple_hash((unsigned char *)data,len,res);
  183. if(memcmp(res,data+len,8)!=0)
  184. return -1;
  185. return 0;
  186. }
  187. int auth_none_cal(const char *data,char * output,int &len)
  188. {
  189. memcpy(output,data,len);
  190. return 0;
  191. }
  192. int auth_md5_verify(const char *data,int &len)
  193. {
  194. if(len<16)
  195. {
  196. mylog(log_trace,"auth_md5_verify len<16\n");
  197. return -1;
  198. }
  199. char md5_res[16];
  200. md5((unsigned char *)data,len-16,(unsigned char *)md5_res);
  201. if(memcmp(md5_res,data+len-16,16)!=0)
  202. {
  203. mylog(log_trace,"auth_md5_verify md5 check failed\n");
  204. return -2;
  205. }
  206. len-=16;
  207. return 0;
  208. }
  209. int auth_none_verify(const char *data,int &len)
  210. {
  211. return 0;
  212. }
  213. int cipher_xor_encrypt(const char * data, char *output,int &len, char *key) {
  214. int i, j;
  215. for (i = 0, j = 0; i < len; i++, j++) {
  216. if(j==16) j=0;
  217. output[i] = data[i]^key[j];
  218. }
  219. return 0;
  220. }
  221. int cipher_xor_decrypt(const char * data, char *output,int &len, char *key) {
  222. int i, j;
  223. //char tmp[buf_len];
  224. //len=len/16*16+1;
  225. //AES128_CBC_decrypt_buffer((uint8_t *)tmp, (uint8_t *)input, len, (uint8_t *)key, (uint8_t *)iv);
  226. //for(i=0;i<len;i++)
  227. //input[i]=tmp[i];
  228. for (i = 0, j = 0; i < len; i++, j++) {
  229. if(j==16) j=0;
  230. output[i] = data[i]^key[j];
  231. }
  232. return 0;
  233. }
  234. int padding(char *data ,int &data_len,int padding_num)
  235. {
  236. int old_len=data_len;
  237. data_len+=1;
  238. if(data_len%padding_num!=0)
  239. {
  240. data_len= (data_len/padding_num)*padding_num+padding_num;
  241. }
  242. unsigned char * p= (unsigned char *)&data[data_len-1];
  243. *p= (data_len-old_len);
  244. return 0;
  245. }
  246. int de_padding(const char *data ,int &data_len,int padding_num)
  247. {
  248. if((uint8_t)data[data_len-1] >padding_num) return -1;
  249. data_len-=(uint8_t)data[data_len-1];
  250. if(data_len<0)
  251. {
  252. return -1;
  253. }
  254. return 0;
  255. }
  256. int cipher_aes128cbc_encrypt(const char *data,char *output,int &len,char * key)
  257. {
  258. static int first_time=1;
  259. if(aes_key_optimize)
  260. {
  261. if(first_time==0) key=0;
  262. else first_time=0;
  263. }
  264. char buf[buf_len];
  265. memcpy(buf,data,len);//TODO inefficient code
  266. /*
  267. int ori_len=len;
  268. len+=2;//length
  269. if(len%16!=0)
  270. {
  271. len= (len/16)*16+16;
  272. }
  273. //if(len>max_data_len) return -1;
  274. buf[len-2]= (unsigned char)( (uint16_t(ori_len))>>8);
  275. buf[len-1]=(unsigned char)( ((uint16_t(ori_len))<<8)>>8) ;*/
  276. if(padding(buf,len,16)<0) return -1;
  277. AES_CBC_encrypt_buffer((unsigned char *)output,(unsigned char *)buf,len,(unsigned char *)key,(unsigned char *)zero_iv);
  278. return 0;
  279. }
  280. int auth_crc32_verify(const char *data,int &len)
  281. {
  282. if(len<int(sizeof(unsigned int)))
  283. {
  284. mylog(log_debug,"auth_crc32_verify len<%d\n",int(sizeof(unsigned int)));
  285. return -1;
  286. }
  287. unsigned int ret=crc32h((unsigned char *)data,len-sizeof(unsigned int));
  288. unsigned int ret_n=htonl(ret);
  289. if(memcmp(data+len-sizeof(unsigned int),&ret_n,sizeof(unsigned int))!=0)
  290. {
  291. mylog(log_debug,"auth_crc32_verify memcmp fail\n");
  292. return -1;
  293. }
  294. len-=sizeof(unsigned int);
  295. return 0;
  296. }
  297. int cipher_none_encrypt(const char *data,char *output,int &len,char * key)
  298. {
  299. memcpy(output,data,len);
  300. return 0;
  301. }
  302. int cipher_aes128cbc_decrypt(const char *data,char *output,int &len,char * key)
  303. {
  304. static int first_time=1;
  305. if(aes_key_optimize)
  306. {
  307. if(first_time==0) key=0;
  308. else first_time=0;
  309. }
  310. if(len%16 !=0) {mylog(log_debug,"len%%16!=0\n");return -1;}
  311. //if(len<0) {mylog(log_debug,"len <0\n");return -1;}
  312. AES_CBC_decrypt_buffer((unsigned char *)output,(unsigned char *)data,len,(unsigned char *)key,(unsigned char *)zero_iv);
  313. if(de_padding(output,len,16)<0) return -1;
  314. return 0;
  315. }
  316. int cipher_none_decrypt(const char *data,char *output,int &len,char * key)
  317. {
  318. memcpy(output,data,len);
  319. return 0;
  320. }
  321. int auth_cal(const char *data,char * output,int &len)
  322. {
  323. mylog(log_trace,"auth:%d\n",auth_mode);
  324. switch(auth_mode)
  325. {
  326. case auth_crc32:return auth_crc32_cal(data, output, len);
  327. case auth_md5:return auth_md5_cal(data, output, len);
  328. case auth_simple:return auth_simple_cal(data, output, len);
  329. case auth_none:return auth_none_cal(data, output, len);
  330. case auth_hmac_sha1:return auth_hmac_sha1_cal(data,output,len);
  331. //default: return auth_md5_cal(data,output,len);//default;
  332. default: assert(0==1);
  333. }
  334. return -1;
  335. }
  336. int auth_verify(const char *data,int &len)
  337. {
  338. mylog(log_trace,"auth:%d\n",auth_mode);
  339. switch(auth_mode)
  340. {
  341. case auth_crc32:return auth_crc32_verify(data, len);
  342. case auth_md5:return auth_md5_verify(data, len);
  343. case auth_simple:return auth_simple_verify(data, len);
  344. case auth_none:return auth_none_verify(data, len);
  345. case auth_hmac_sha1:return auth_hmac_sha1_verify(data,len);
  346. //default: return auth_md5_verify(data,len);//default
  347. default: assert(0==1);
  348. }
  349. return -1;
  350. }
  351. int cipher_encrypt(const char *data,char *output,int &len,char * key)
  352. {
  353. mylog(log_trace,"cipher:%d\n",cipher_mode);
  354. switch(cipher_mode)
  355. {
  356. case cipher_aes128cbc:return cipher_aes128cbc_encrypt(data,output,len, key);
  357. case cipher_xor:return cipher_xor_encrypt(data,output,len, key);
  358. case cipher_none:return cipher_none_encrypt(data,output,len, key);
  359. //default:return cipher_aes128cbc_encrypt(data,output,len, key);
  360. default: assert(0==1);
  361. }
  362. return -1;
  363. }
  364. int cipher_decrypt(const char *data,char *output,int &len,char * key)
  365. {
  366. mylog(log_trace,"cipher:%d\n",cipher_mode);
  367. switch(cipher_mode)
  368. {
  369. case cipher_aes128cbc:return cipher_aes128cbc_decrypt(data,output,len, key);
  370. case cipher_xor:return cipher_xor_decrypt(data,output,len, key);
  371. case cipher_none:return cipher_none_decrypt(data,output,len, key);
  372. // default: return cipher_aes128cbc_decrypt(data,output,len,key);
  373. default: assert(0==1);
  374. }
  375. return -1;
  376. }
  377. int encrypt_AE(const char *data,char *output,int &len /*,char * key*/)
  378. {
  379. mylog(log_trace,"encrypt_AE is called\n");
  380. char buf[buf_len];
  381. char buf2[buf_len];
  382. memcpy(buf,data,len);
  383. if(cipher_encrypt(buf,buf2,len,(char *)cipher_key_encrypt) !=0) {mylog(log_debug,"cipher_encrypt failed ");return -1;}
  384. if(auth_cal(buf2,output,len)!=0) {mylog(log_debug,"auth_cal failed ");return -1;}
  385. //printf("%d %x %x\n",len,(int)(output[0]),(int)(output[1]));
  386. //print_binary_chars(output,len);
  387. //use encrypt-then-MAC scheme
  388. return 0;
  389. }
  390. int decrypt_AE(const char *data,char *output,int &len /*,char * key*/)
  391. {
  392. mylog(log_trace,"decrypt_AE is called\n");
  393. //printf("%d %x %x\n",len,(int)(data[0]),(int)(data[1]));
  394. //print_binary_chars(data,len);
  395. if(auth_verify(data,len)!=0) {mylog(log_debug,"auth_verify failed\n");return -1;}
  396. if(cipher_decrypt(data,output,len,(char *)cipher_key_decrypt) !=0) {mylog(log_debug,"cipher_decrypt failed \n"); return -1;}
  397. return 0;
  398. }
  399. int my_encrypt(const char *data,char *output,int &len /*,char * key*/)
  400. {
  401. if(len<0) {mylog(log_trace,"len<0");return -1;}
  402. if(len>max_data_len) {mylog(log_warn,"len>max_data_len");return -1;}
  403. if(is_hmac_used)
  404. return encrypt_AE(data,output,len);
  405. char buf[buf_len];
  406. char buf2[buf_len];
  407. memcpy(buf,data,len);
  408. if(auth_cal(buf,buf2,len)!=0) {mylog(log_debug,"auth_cal failed ");return -1;}
  409. if(cipher_encrypt(buf2,output,len,normal_key) !=0) {mylog(log_debug,"cipher_encrypt failed ");return -1;}
  410. return 0;
  411. }
  412. int my_decrypt(const char *data,char *output,int &len /*,char * key*/)
  413. {
  414. if(len<0) return -1;
  415. if(len>max_data_len) {mylog(log_warn,"len>max_data_len");return -1;}
  416. if(is_hmac_used)
  417. return decrypt_AE(data,output,len);
  418. if(cipher_decrypt(data,output,len,normal_key) !=0) {mylog(log_debug,"cipher_decrypt failed \n"); return -1;}
  419. if(auth_verify(output,len)!=0) {mylog(log_debug,"auth_verify failed\n");return -1;}
  420. return 0;
  421. }
  422. int encrypt_AEAD(uint8_t *data,uint8_t *output,int &len,uint8_t * key,uint8_t *header,int hlen)
  423. {
  424. //TODO
  425. return -1;
  426. }
  427. int decrypt_AEAD(uint8_t *data,uint8_t *output,int &len,uint8_t * key,uint8_t *header,int hlen)
  428. {
  429. //TODO
  430. return -1;
  431. }