encrypt.cpp 16 KB

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