encrypt.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. #include "lib/aes-common.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=64;//generate 512bit long keys, use first n chars when needed
  20. const int cipher_key_len=64;
  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. char gro_xor[256+100];//dirty fix for gro
  26. 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"},};
  27. unordered_map<int, const char *> cipher_mode_tostring={{cipher_none,"none"},{cipher_aes128cfb,"aes128cfb"},{cipher_aes128cbc,"aes128cbc"},{cipher_xor,"xor"},};
  28. //TODO aes-gcm
  29. auth_mode_t auth_mode=auth_md5;
  30. cipher_mode_t cipher_mode=cipher_aes128cbc;
  31. int is_hmac_used=0;
  32. int aes128cfb_old=0;
  33. //TODO key negotiation and forward secrecy
  34. int my_init_keys(const char * user_passwd,int is_client)
  35. {
  36. char tmp[1000]="";
  37. int len=strlen(user_passwd);
  38. strcat(tmp,user_passwd);
  39. strcat(tmp,"key1");
  40. md5((uint8_t*)tmp,strlen(tmp),(uint8_t*)normal_key);
  41. if(auth_mode==auth_hmac_sha1)
  42. is_hmac_used=1;
  43. if(is_hmac_used||g_fix_gro||1)
  44. {
  45. unsigned char salt[400]="";
  46. char salt_text[400]="udp2raw_salt1";
  47. md5((uint8_t*)(salt_text),strlen(salt_text),salt); //TODO different salt per session
  48. unsigned char pbkdf2_output1[400]="";
  49. PKCS5_PBKDF2_HMAC_SHA256((uint8_t*)user_passwd,len,salt,16,10000, 32,pbkdf2_output1); //TODO argon2 ?
  50. //unsigned char pbkdf2_output2[400]="";
  51. //PKCS5_PBKDF2_HMAC_SHA256(pbkdf2_output1,32,0,0,1, hmac_key_len*2+cipher_key_len*2,pbkdf2_output2); //stretch it
  52. const char *info_hmac_encrypt="hmac_key server-->client";
  53. const char *info_hmac_decrypt="hmac_key client-->server";
  54. const char *info_cipher_encrypt="cipher_key server-->client";
  55. const char *info_cipher_decrypt="cipher_key client-->server";
  56. if(is_client)
  57. {
  58. const char *tmp;
  59. tmp=info_hmac_encrypt; info_hmac_encrypt=info_hmac_decrypt;info_hmac_decrypt=tmp;
  60. tmp=info_cipher_encrypt; info_cipher_encrypt=info_cipher_decrypt;info_cipher_decrypt=tmp;
  61. }
  62. else
  63. {
  64. //nop
  65. }
  66. assert( hkdf_sha256_expand( pbkdf2_output1,32, (unsigned char *)info_cipher_encrypt,strlen(info_cipher_encrypt), cipher_key_encrypt, cipher_key_len ) ==0);
  67. assert( hkdf_sha256_expand( pbkdf2_output1,32, (unsigned char *)info_cipher_decrypt,strlen(info_cipher_decrypt), cipher_key_decrypt, cipher_key_len ) ==0);
  68. assert( hkdf_sha256_expand( pbkdf2_output1,32, (unsigned char *)info_hmac_encrypt,strlen(info_hmac_encrypt), hmac_key_encrypt, hmac_key_len ) ==0);
  69. assert( hkdf_sha256_expand( pbkdf2_output1,32, (unsigned char *)info_hmac_decrypt,strlen(info_hmac_decrypt), hmac_key_decrypt, hmac_key_len ) ==0);
  70. const char *gro_info="gro";
  71. assert( hkdf_sha256_expand( pbkdf2_output1,32, (unsigned char *)gro_info,strlen(gro_info), (unsigned char *)gro_xor, 256 ) ==0);
  72. }
  73. print_binary_chars(normal_key,16);
  74. print_binary_chars((char *)hmac_key_encrypt,hmac_key_len);
  75. print_binary_chars((char *)hmac_key_decrypt,hmac_key_len);
  76. print_binary_chars((char *)cipher_key_encrypt,cipher_key_len);
  77. print_binary_chars((char *)cipher_key_decrypt,cipher_key_len);
  78. return 0;
  79. }
  80. /*
  81. * this function comes from http://www.hackersdelight.org/hdcodetxt/crc.c.txt
  82. */
  83. unsigned int crc32h(unsigned char *message,int len) {
  84. int i, crc;
  85. unsigned int byte, c;
  86. const unsigned int g0 = 0xEDB88320, g1 = g0>>1,
  87. g2 = g0>>2, g3 = g0>>3, g4 = g0>>4, g5 = g0>>5,
  88. g6 = (g0>>6)^g0, g7 = ((g0>>6)^g0)>>1;
  89. i = 0;
  90. crc = 0xFFFFFFFF;
  91. while (i!=len) { // Get next byte.
  92. byte = message[i];
  93. crc = crc ^ byte;
  94. c = ((crc<<31>>31) & g7) ^ ((crc<<30>>31) & g6) ^
  95. ((crc<<29>>31) & g5) ^ ((crc<<28>>31) & g4) ^
  96. ((crc<<27>>31) & g3) ^ ((crc<<26>>31) & g2) ^
  97. ((crc<<25>>31) & g1) ^ ((crc<<24>>31) & g0);
  98. crc = ((unsigned)crc >> 8) ^ c;
  99. i = i + 1;
  100. }
  101. return ~crc;
  102. }
  103. /*
  104. void sum(const unsigned char *data,int len,unsigned char* res) {
  105. memset(res,0,sizeof(int));
  106. for(int i=0,j=0;i<len;i++,j++)
  107. {
  108. if(j==4) j=0;
  109. res[j]+=data[i];
  110. }
  111. return ;
  112. }*/
  113. void simple_hash(unsigned char *str,int len,unsigned char res[8]) //djb2+ sdbm
  114. {
  115. u32_t hash = 5381;
  116. u32_t hash2 = 0;
  117. int c;
  118. int i=0;
  119. while(c = *str++,i++!=len)
  120. {
  121. // hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
  122. hash = ((hash << 5) + hash)^c; /* (hash * 33) ^ c */
  123. hash2 = c + (hash2 << 6) + (hash2 << 16) - hash2;
  124. }
  125. hash=htonl(hash);
  126. hash2=htonl(hash2);
  127. memcpy(res,&hash,sizeof(hash));
  128. memcpy(res+sizeof(hash),&hash2,sizeof(hash2));
  129. }
  130. int auth_md5_cal(const char *data,char * output,int &len)
  131. {
  132. memcpy(output,data,len);//TODO inefficient code
  133. md5((unsigned char *)output,len,(unsigned char *)(output+len));
  134. len+=16;
  135. return 0;
  136. }
  137. int auth_hmac_sha1_cal(const char *data,char * output,int &len)
  138. {
  139. mylog(log_trace,"auth_hmac_sha1_cal() is called\n");
  140. memcpy(output,data,len);//TODO inefficient code
  141. sha1_hmac(hmac_key_encrypt, 20, (const unsigned char *)data, len,(unsigned char *)(output+len));
  142. //use key len of 20 instead of hmac_key_len, "extra length would not significantly increase the function strength" (rfc2104)
  143. len+=20;
  144. return 0;
  145. }
  146. int auth_hmac_sha1_verify(const char *data,int &len)
  147. {
  148. mylog(log_trace,"auth_hmac_sha1_verify() is called\n");
  149. if(len<20)
  150. {
  151. mylog(log_trace,"auth_hmac_sha1_verify len<20\n");
  152. return -1;
  153. }
  154. char res[20];
  155. sha1_hmac(hmac_key_decrypt, 20, (const unsigned char *)data, len-20,(unsigned char *)(res));
  156. if(memcmp(res,data+len-20,20)!=0)
  157. {
  158. mylog(log_trace,"auth_hmac_sha1 check failed\n");
  159. return -2;
  160. }
  161. len-=20;
  162. return 0;
  163. }
  164. int auth_crc32_cal(const char *data,char * output,int &len)
  165. {
  166. memcpy(output,data,len);//TODO inefficient code
  167. unsigned int ret=crc32h((unsigned char *)output,len);
  168. unsigned int ret_n=htonl(ret);
  169. memcpy(output+len,&ret_n,sizeof(unsigned int));
  170. len+=sizeof(unsigned int);
  171. return 0;
  172. }
  173. int auth_simple_cal(const char *data,char * output,int &len)
  174. {
  175. //char res[4];
  176. memcpy(output,data,len);//TODO inefficient code
  177. simple_hash((unsigned char *)output,len,(unsigned char *)(output+len));
  178. len+=8;
  179. return 0;
  180. }
  181. int auth_simple_verify(const char *data,int &len)
  182. {
  183. if(len<8) return -1;
  184. unsigned char res[8];
  185. len-=8;
  186. simple_hash((unsigned char *)data,len,res);
  187. if(memcmp(res,data+len,8)!=0)
  188. return -1;
  189. return 0;
  190. }
  191. int auth_none_cal(const char *data,char * output,int &len)
  192. {
  193. memcpy(output,data,len);
  194. return 0;
  195. }
  196. int auth_md5_verify(const char *data,int &len)
  197. {
  198. if(len<16)
  199. {
  200. mylog(log_trace,"auth_md5_verify len<16\n");
  201. return -1;
  202. }
  203. char md5_res[16];
  204. md5((unsigned char *)data,len-16,(unsigned char *)md5_res);
  205. if(memcmp(md5_res,data+len-16,16)!=0)
  206. {
  207. mylog(log_trace,"auth_md5_verify md5 check failed\n");
  208. return -2;
  209. }
  210. len-=16;
  211. return 0;
  212. }
  213. int auth_none_verify(const char *data,int &len)
  214. {
  215. return 0;
  216. }
  217. int cipher_xor_encrypt(const char * data, char *output,int &len, char *key) {
  218. int i, j;
  219. for (i = 0, j = 0; i < len; i++, j++) {
  220. if(j==16) j=0;
  221. output[i] = data[i]^key[j];
  222. }
  223. return 0;
  224. }
  225. int cipher_xor_decrypt(const char * data, char *output,int &len, char *key) {
  226. int i, j;
  227. //char tmp[buf_len];
  228. //len=len/16*16+1;
  229. //AES128_CBC_decrypt_buffer((uint8_t *)tmp, (uint8_t *)input, len, (uint8_t *)key, (uint8_t *)iv);
  230. //for(i=0;i<len;i++)
  231. //input[i]=tmp[i];
  232. for (i = 0, j = 0; i < len; i++, j++) {
  233. if(j==16) j=0;
  234. output[i] = data[i]^key[j];
  235. }
  236. return 0;
  237. }
  238. int padding(char *data ,int &data_len,int padding_num)
  239. {
  240. int old_len=data_len;
  241. data_len+=1;
  242. if(data_len%padding_num!=0)
  243. {
  244. data_len= (data_len/padding_num)*padding_num+padding_num;
  245. }
  246. unsigned char * p= (unsigned char *)&data[data_len-1];
  247. *p= (data_len-old_len);
  248. return 0;
  249. }
  250. int de_padding(const char *data ,int &data_len,int padding_num)
  251. {
  252. if(data_len==0) return -1;
  253. if((uint8_t)data[data_len-1] >padding_num) return -1;
  254. data_len-=(uint8_t)data[data_len-1];
  255. if(data_len<0)
  256. {
  257. return -1;
  258. }
  259. return 0;
  260. }
  261. void aes_ecb_encrypt(const char *data,char *output)
  262. {
  263. static int first_time=1;
  264. char *key=(char*)cipher_key_encrypt;
  265. if(aes_key_optimize)
  266. {
  267. if(first_time==0) key=0;
  268. else first_time=0;
  269. }
  270. AES_ECB_encrypt_buffer((uint8_t*)data,(uint8_t*)key,(uint8_t*)output);
  271. }
  272. void aes_ecb_encrypt1(char *data)
  273. {
  274. char buf[16];
  275. memcpy(buf,data,16);
  276. aes_ecb_encrypt(buf,data);
  277. }
  278. void aes_ecb_decrypt(const char *data,char *output)
  279. {
  280. static int first_time=1;
  281. char *key=(char*)cipher_key_decrypt;
  282. if(aes_key_optimize)
  283. {
  284. if(first_time==0) key=0;
  285. else first_time=0;
  286. }
  287. AES_ECB_decrypt_buffer((uint8_t*)data,(uint8_t*)key,(uint8_t*)output);
  288. }
  289. void aes_ecb_decrypt1(char *data)
  290. {
  291. char buf[16];
  292. memcpy(buf,data,16);
  293. aes_ecb_decrypt(buf,data);
  294. }
  295. int cipher_aes128cbc_encrypt(const char *data,char *output,int &len,char * key)
  296. {
  297. static int first_time=1;
  298. char buf[buf_len];
  299. memcpy(buf,data,len);//TODO inefficient code
  300. if(padding(buf,len,16)<0) return -1;
  301. if(aes_key_optimize)
  302. {
  303. if(first_time==0) key=0;
  304. else first_time=0;
  305. }
  306. AES_CBC_encrypt_buffer((unsigned char *)output,(unsigned char *)buf,len,(unsigned char *)key,(unsigned char *)zero_iv);
  307. return 0;
  308. }
  309. int cipher_aes128cfb_encrypt(const char *data,char *output,int &len,char * key)
  310. {
  311. static int first_time=1;
  312. assert(len>=16);
  313. char buf[buf_len];
  314. memcpy(buf,data,len);//TODO inefficient code
  315. if(aes_key_optimize)
  316. {
  317. if(first_time==0) key=0;
  318. else first_time=0;
  319. }
  320. if(!aes128cfb_old)
  321. {
  322. aes_ecb_encrypt(data,buf); //encrypt the first block
  323. }
  324. AES_CFB_encrypt_buffer((unsigned char *)output,(unsigned char *)buf,len,(unsigned char *)key,(unsigned char *)zero_iv);
  325. return 0;
  326. }
  327. int auth_crc32_verify(const char *data,int &len)
  328. {
  329. if(len<int(sizeof(unsigned int)))
  330. {
  331. mylog(log_debug,"auth_crc32_verify len<%d\n",int(sizeof(unsigned int)));
  332. return -1;
  333. }
  334. unsigned int ret=crc32h((unsigned char *)data,len-sizeof(unsigned int));
  335. unsigned int ret_n=htonl(ret);
  336. if(memcmp(data+len-sizeof(unsigned int),&ret_n,sizeof(unsigned int))!=0)
  337. {
  338. mylog(log_debug,"auth_crc32_verify memcmp fail\n");
  339. return -1;
  340. }
  341. len-=sizeof(unsigned int);
  342. return 0;
  343. }
  344. int cipher_none_encrypt(const char *data,char *output,int &len,char * key)
  345. {
  346. memcpy(output,data,len);
  347. return 0;
  348. }
  349. int cipher_aes128cbc_decrypt(const char *data,char *output,int &len,char * key)
  350. {
  351. static int first_time=1;
  352. if(len%16 !=0) {mylog(log_debug,"len%%16!=0\n");return -1;}
  353. if(aes_key_optimize)
  354. {
  355. if(first_time==0) key=0;
  356. else first_time=0;
  357. }
  358. AES_CBC_decrypt_buffer((unsigned char *)output,(unsigned char *)data,len,(unsigned char *)key,(unsigned char *)zero_iv);
  359. if(de_padding(output,len,16)<0) return -1;
  360. return 0;
  361. }
  362. int cipher_aes128cfb_decrypt(const char *data,char *output,int &len,char * key)
  363. {
  364. static int first_time=1;
  365. if(len<16) return -1;
  366. if(aes_key_optimize)
  367. {
  368. if(first_time==0) key=0;
  369. else first_time=0;
  370. }
  371. AES_CFB_decrypt_buffer((unsigned char *)output,(unsigned char *)data,len,(unsigned char *)key,(unsigned char *)zero_iv);
  372. if(!aes128cfb_old)
  373. aes_ecb_decrypt1(output); //decrypt the first block
  374. //if(de_padding(output,len,16)<0) return -1;
  375. return 0;
  376. }
  377. int cipher_none_decrypt(const char *data,char *output,int &len,char * key)
  378. {
  379. memcpy(output,data,len);
  380. return 0;
  381. }
  382. int auth_cal(const char *data,char * output,int &len)
  383. {
  384. mylog(log_trace,"auth:%d\n",auth_mode);
  385. switch(auth_mode)
  386. {
  387. case auth_crc32:return auth_crc32_cal(data, output, len);
  388. case auth_md5:return auth_md5_cal(data, output, len);
  389. case auth_simple:return auth_simple_cal(data, output, len);
  390. case auth_none:return auth_none_cal(data, output, len);
  391. case auth_hmac_sha1:return auth_hmac_sha1_cal(data,output,len);
  392. //default: return auth_md5_cal(data,output,len);//default;
  393. default: assert(0==1);
  394. }
  395. return -1;
  396. }
  397. int auth_verify(const char *data,int &len)
  398. {
  399. mylog(log_trace,"auth:%d\n",auth_mode);
  400. switch(auth_mode)
  401. {
  402. case auth_crc32:return auth_crc32_verify(data, len);
  403. case auth_md5:return auth_md5_verify(data, len);
  404. case auth_simple:return auth_simple_verify(data, len);
  405. case auth_none:return auth_none_verify(data, len);
  406. case auth_hmac_sha1:return auth_hmac_sha1_verify(data,len);
  407. //default: return auth_md5_verify(data,len);//default
  408. default: assert(0==1);
  409. }
  410. return -1;
  411. }
  412. int cipher_encrypt(const char *data,char *output,int &len,char * key)
  413. {
  414. mylog(log_trace,"cipher:%d\n",cipher_mode);
  415. switch(cipher_mode)
  416. {
  417. case cipher_aes128cbc:return cipher_aes128cbc_encrypt(data,output,len, key);
  418. case cipher_aes128cfb:return cipher_aes128cfb_encrypt(data,output,len, key);
  419. case cipher_xor:return cipher_xor_encrypt(data,output,len, key);
  420. case cipher_none:return cipher_none_encrypt(data,output,len, key);
  421. //default:return cipher_aes128cbc_encrypt(data,output,len, key);
  422. default: assert(0==1);
  423. }
  424. return -1;
  425. }
  426. int cipher_decrypt(const char *data,char *output,int &len,char * key)
  427. {
  428. mylog(log_trace,"cipher:%d\n",cipher_mode);
  429. switch(cipher_mode)
  430. {
  431. case cipher_aes128cbc:return cipher_aes128cbc_decrypt(data,output,len, key);
  432. case cipher_aes128cfb:return cipher_aes128cfb_decrypt(data,output,len, key);
  433. case cipher_xor:return cipher_xor_decrypt(data,output,len, key);
  434. case cipher_none:return cipher_none_decrypt(data,output,len, key);
  435. // default: return cipher_aes128cbc_decrypt(data,output,len,key);
  436. default: assert(0==1);
  437. }
  438. return -1;
  439. }
  440. int encrypt_AE(const char *data,char *output,int &len /*,char * key*/)
  441. {
  442. mylog(log_trace,"encrypt_AE is called\n");
  443. char buf[buf_len];
  444. char buf2[buf_len];
  445. memcpy(buf,data,len);
  446. if(cipher_encrypt(buf,buf2,len,(char *)cipher_key_encrypt) !=0) {mylog(log_debug,"cipher_encrypt failed ");return -1;}
  447. if(auth_cal(buf2,output,len)!=0) {mylog(log_debug,"auth_cal failed ");return -1;}
  448. //printf("%d %x %x\n",len,(int)(output[0]),(int)(output[1]));
  449. //print_binary_chars(output,len);
  450. //use encrypt-then-MAC scheme
  451. return 0;
  452. }
  453. int decrypt_AE(const char *data,char *output,int &len /*,char * key*/)
  454. {
  455. mylog(log_trace,"decrypt_AE is called\n");
  456. //printf("%d %x %x\n",len,(int)(data[0]),(int)(data[1]));
  457. //print_binary_chars(data,len);
  458. if(auth_verify(data,len)!=0) {mylog(log_debug,"auth_verify failed\n");return -1;}
  459. if(cipher_decrypt(data,output,len,(char *)cipher_key_decrypt) !=0) {mylog(log_debug,"cipher_decrypt failed \n"); return -1;}
  460. return 0;
  461. }
  462. int my_encrypt(const char *data,char *output,int &len /*,char * key*/)
  463. {
  464. if(len<0) {mylog(log_trace,"len<0");return -1;}
  465. if(len>max_data_len) {mylog(log_warn,"len>max_data_len");return -1;}
  466. if(is_hmac_used)
  467. return encrypt_AE(data,output,len);
  468. char buf[buf_len];
  469. char buf2[buf_len];
  470. memcpy(buf,data,len);
  471. if(auth_cal(buf,buf2,len)!=0) {mylog(log_debug,"auth_cal failed ");return -1;}
  472. if(cipher_encrypt(buf2,output,len,normal_key) !=0) {mylog(log_debug,"cipher_encrypt failed ");return -1;}
  473. return 0;
  474. }
  475. int my_decrypt(const char *data,char *output,int &len /*,char * key*/)
  476. {
  477. if(len<0) return -1;
  478. if(len>max_data_len) {mylog(log_warn,"len>max_data_len");return -1;}
  479. if(is_hmac_used)
  480. return decrypt_AE(data,output,len);
  481. if(cipher_decrypt(data,output,len,normal_key) !=0) {mylog(log_debug,"cipher_decrypt failed \n"); return -1;}
  482. if(auth_verify(output,len)!=0) {mylog(log_debug,"auth_verify failed\n");return -1;}
  483. return 0;
  484. }
  485. int encrypt_AEAD(uint8_t *data,uint8_t *output,int &len,uint8_t * key,uint8_t *header,int hlen)
  486. {
  487. //TODO
  488. return -1;
  489. }
  490. int decrypt_AEAD(uint8_t *data,uint8_t *output,int &len,uint8_t * key,uint8_t *header,int hlen)
  491. {
  492. //TODO
  493. return -1;
  494. }