crypto.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. #include <openssl/evp.h>
  2. #include <openssl/dh.h>
  3. #include <openssl/bn.h>
  4. #include <openssl/err.h>
  5. #include <openssl/rand.h>
  6. #include <openssl/ssl.h>
  7. #include <netinet/in.h>
  8. #include "crypto.h"
  9. #include "socks5proxy.h"
  10. #include "tagging.h"
  11. #include "ptwist.h"
  12. static super_data *super;
  13. /* PRF using sha384, as defined in RFC 5246 */
  14. int PRF(uint8_t *secret, int32_t secret_len,
  15. uint8_t *seed1, int32_t seed1_len,
  16. uint8_t *seed2, int32_t seed2_len,
  17. uint8_t *seed3, int32_t seed3_len,
  18. uint8_t *seed4, int32_t seed4_len,
  19. uint8_t *output, int32_t output_len){
  20. EVP_MD_CTX ctx, ctx_tmp, ctx_init;
  21. EVP_PKEY *mac_key;
  22. const EVP_MD *md = EVP_sha256();
  23. uint8_t A[EVP_MAX_MD_SIZE];
  24. size_t len, A_len;
  25. int chunk = EVP_MD_size(md);
  26. int remaining = output_len;
  27. uint8_t *out = output;
  28. EVP_MD_CTX_init(&ctx);
  29. EVP_MD_CTX_init(&ctx_tmp);
  30. EVP_MD_CTX_init(&ctx_init);
  31. EVP_MD_CTX_set_flags(&ctx_init, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
  32. mac_key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, secret, secret_len);
  33. /* Calculate first A value */
  34. EVP_DigestSignInit(&ctx_init, NULL, md, NULL, mac_key);
  35. EVP_MD_CTX_copy_ex(&ctx, &ctx_init);
  36. if(seed1 != NULL && seed1_len > 0){
  37. EVP_DigestSignUpdate(&ctx, seed1, seed1_len);
  38. }
  39. if(seed2 != NULL && seed2_len > 0){
  40. EVP_DigestSignUpdate(&ctx, seed2, seed2_len);
  41. }
  42. if(seed3 != NULL && seed3_len > 0){
  43. EVP_DigestSignUpdate(&ctx, seed3, seed3_len);
  44. }
  45. if(seed4 != NULL && seed4_len > 0){
  46. EVP_DigestSignUpdate(&ctx, seed4, seed4_len);
  47. }
  48. EVP_DigestSignFinal(&ctx, A, &A_len);
  49. //iterate until desired length is achieved
  50. while(remaining > 0){
  51. /* Now compute SHA384(secret, A+seed) */
  52. EVP_MD_CTX_copy_ex(&ctx, &ctx_init);
  53. EVP_DigestSignUpdate(&ctx, A, A_len);
  54. EVP_MD_CTX_copy_ex(&ctx_tmp, &ctx);
  55. if(seed1 != NULL && seed1_len > 0){
  56. EVP_DigestSignUpdate(&ctx, seed1, seed1_len);
  57. }
  58. if(seed2 != NULL && seed2_len > 0){
  59. EVP_DigestSignUpdate(&ctx, seed2, seed2_len);
  60. }
  61. if(seed3 != NULL && seed3_len > 0){
  62. EVP_DigestSignUpdate(&ctx, seed3, seed3_len);
  63. }
  64. if(seed4 != NULL && seed4_len > 0){
  65. EVP_DigestSignUpdate(&ctx, seed4, seed4_len);
  66. }
  67. if(remaining > chunk){
  68. EVP_DigestSignFinal(&ctx, out, &len);
  69. out += len;
  70. remaining -= len;
  71. /* Next A value */
  72. EVP_DigestSignFinal(&ctx_tmp, A, &A_len);
  73. } else {
  74. EVP_DigestSignFinal(&ctx, A, &A_len);
  75. memcpy(out, A, remaining);
  76. remaining -= remaining;
  77. }
  78. }
  79. EVP_PKEY_free(mac_key);
  80. EVP_MD_CTX_cleanup(&ctx);
  81. EVP_MD_CTX_cleanup(&ctx_tmp);
  82. EVP_MD_CTX_cleanup(&ctx_init);
  83. return 1;
  84. }
  85. /*
  86. * Generate the keys for the super encryption layer, based on the slitheen ID
  87. */
  88. int generate_super_keys(uint8_t *secret){
  89. super = calloc(1, sizeof(super_data));
  90. EVP_MD_CTX *mac_ctx;
  91. const EVP_MD *md = EVP_sha256();
  92. /* Generate Keys */
  93. uint8_t *hdr_key, *bdy_key;
  94. uint8_t *mac_secret;
  95. EVP_PKEY *mac_key;
  96. int32_t mac_len, key_len;
  97. key_len = EVP_CIPHER_key_length(EVP_aes_256_cbc());
  98. mac_len = EVP_MD_size(md);
  99. int32_t total_len = 2*key_len + mac_len;
  100. uint8_t *key_block = calloc(1, total_len);
  101. PRF(secret, SLITHEEN_SUPER_SECRET_SIZE,
  102. (uint8_t *) SLITHEEN_SUPER_CONST, SLITHEEN_SUPER_CONST_SIZE,
  103. NULL, 0,
  104. NULL, 0,
  105. NULL, 0,
  106. key_block, total_len);
  107. #ifdef DEBUG
  108. int i;
  109. printf("secret: \n");
  110. for(i=0; i< SLITHEEN_SUPER_SECRET_SIZE; i++){
  111. printf("%02x ", secret[i]);
  112. }
  113. printf("\n");
  114. printf("keyblock: \n");
  115. for(i=0; i< total_len; i++){
  116. printf("%02x ", key_block[i]);
  117. }
  118. printf("\n");
  119. #endif
  120. hdr_key = key_block;
  121. bdy_key = key_block + key_len;
  122. mac_secret = key_block + 2*key_len;
  123. /* Initialize MAC Context */
  124. mac_ctx = EVP_MD_CTX_create();
  125. EVP_DigestInit_ex(mac_ctx, md, NULL);
  126. mac_key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, mac_secret, mac_len);
  127. EVP_DigestSignInit(mac_ctx, NULL, md, NULL, mac_key);
  128. super->header_key = malloc(key_len);
  129. super->body_key = malloc(key_len);
  130. memcpy(super->header_key, hdr_key, key_len);
  131. memcpy(super->body_key, bdy_key, key_len);
  132. super->body_mac_ctx = mac_ctx;
  133. //Free everything
  134. free(key_block);
  135. EVP_PKEY_free(mac_key);
  136. return 0;
  137. }
  138. int peek_header(uint8_t *data){
  139. EVP_CIPHER_CTX *hdr_ctx = NULL;
  140. int32_t out_len;
  141. uint8_t *p = data;
  142. int retval = 1;
  143. //decrypt header
  144. #ifdef DEBUG
  145. int i;
  146. printf("Encrypted header:\n");
  147. for(i=0; i< SLITHEEN_HEADER_LEN; i++){
  148. printf("%02x ", p[i]);
  149. }
  150. printf("\n");
  151. #endif
  152. hdr_ctx = EVP_CIPHER_CTX_new();
  153. EVP_CipherInit_ex(hdr_ctx, EVP_aes_256_ecb(), NULL, super->header_key, NULL, 0);
  154. if(!EVP_CipherUpdate(hdr_ctx, p, &out_len, p, SLITHEEN_HEADER_LEN)){
  155. printf("Decryption failed!");
  156. retval = 0;
  157. goto end;
  158. }
  159. struct slitheen_hdr *sl_hdr = (struct slitheen_hdr *) p;
  160. if(!sl_hdr->len){//there are no data to be decrypted
  161. retval = 1;
  162. goto end;
  163. }
  164. #ifdef DEBUG_PARSE
  165. printf("Decrypted header (%d bytes):\n", SLITHEEN_HEADER_LEN);
  166. for(i=0; i< SLITHEEN_HEADER_LEN; i++){
  167. printf("%02x ", p[i]);
  168. }
  169. printf("\n");
  170. fflush(stdout);
  171. #endif
  172. retval = 1;
  173. end:
  174. if(hdr_ctx != NULL){
  175. EVP_CIPHER_CTX_cleanup(hdr_ctx);
  176. OPENSSL_free(hdr_ctx);
  177. }
  178. return retval;
  179. }
  180. int super_decrypt(uint8_t *data){
  181. EVP_CIPHER_CTX *bdy_ctx = NULL;
  182. EVP_CIPHER_CTX *hdr_ctx = NULL;
  183. uint8_t *p = data;
  184. int32_t out_len, len;
  185. uint8_t output[EVP_MAX_MD_SIZE];
  186. size_t mac_len;
  187. int i, retval = 1;
  188. //decrypt header
  189. #ifdef DEBUG
  190. printf("Encrypted header:\n");
  191. for(i=0; i< SLITHEEN_HEADER_LEN; i++){
  192. printf("%02x ", p[i]);
  193. }
  194. printf("\n");
  195. #endif
  196. hdr_ctx = EVP_CIPHER_CTX_new();
  197. EVP_CipherInit_ex(hdr_ctx, EVP_aes_256_ecb(), NULL, super->header_key, NULL, 0);
  198. if(!EVP_CipherUpdate(hdr_ctx, p, &out_len, p, SLITHEEN_HEADER_LEN)){
  199. printf("Decryption failed!");
  200. retval = 0;
  201. goto end;
  202. }
  203. struct slitheen_hdr *sl_hdr = (struct slitheen_hdr *) p;
  204. len = htons(sl_hdr->len);
  205. if(!sl_hdr->len){//there are no data to be decrypted
  206. retval = 1;
  207. goto end;
  208. }
  209. if(len %16){ //add padding to len
  210. len += 16 - len%16;
  211. }
  212. //#ifdef DEBUG_PARSE
  213. printf("Decrypted header (%d bytes):\n", SLITHEEN_HEADER_LEN);
  214. for(i=0; i< SLITHEEN_HEADER_LEN; i++){
  215. printf("%02x ", p[i]);
  216. }
  217. printf("\n");
  218. fflush(stdout);
  219. //#endif
  220. p += SLITHEEN_HEADER_LEN;
  221. //initialize body cipher context with IV
  222. bdy_ctx = EVP_CIPHER_CTX_new();
  223. EVP_CipherInit_ex(bdy_ctx, EVP_aes_256_cbc(), NULL, super->body_key, p, 0);
  224. p+=16;
  225. //compute mac
  226. EVP_MD_CTX mac_ctx;
  227. EVP_MD_CTX_init(&mac_ctx);
  228. EVP_MD_CTX_copy_ex(&mac_ctx, super->body_mac_ctx);
  229. EVP_DigestSignUpdate(&mac_ctx, p, len);
  230. EVP_DigestSignFinal(&mac_ctx, output, &mac_len);
  231. EVP_MD_CTX_cleanup(&mac_ctx);
  232. #ifdef DEBUG_PARSE
  233. printf("Received mac:\n");
  234. for(i=0; i< 16; i++){
  235. printf("%02x ", p[len+i]);
  236. }
  237. printf("\n");
  238. fflush(stdout);
  239. #endif
  240. #ifdef DEBUG_PARSE
  241. printf("Computed mac:\n");
  242. for(i=0; i< 16; i++){
  243. printf("%02x ", output[i]);
  244. }
  245. printf("\n");
  246. fflush(stdout);
  247. #endif
  248. if(memcmp(p+len, output, 16)){
  249. printf("MAC verification failed\n");
  250. retval = 0;
  251. goto end;
  252. }
  253. //decrypt body
  254. #ifdef DEBUG_PARSE
  255. printf("Encrypted data (%d bytes):\n", len);
  256. for(i=0; i< len; i++){
  257. printf("%02x ", p[i]);
  258. }
  259. printf("\n");
  260. #endif
  261. if(!EVP_CipherUpdate(bdy_ctx, p, &out_len, p, len)){
  262. printf("Decryption failed!");
  263. retval = 0;
  264. goto end;
  265. }
  266. #ifdef DEBUG_PARSE
  267. printf("Decrypted data (%d bytes):\n", out_len);
  268. for(i=0; i< out_len; i++){
  269. printf("%02x ", p[i]);
  270. }
  271. printf("\n");
  272. fflush(stdout);
  273. #endif
  274. p += out_len;
  275. retval = 1;
  276. end:
  277. if(hdr_ctx != NULL){
  278. EVP_CIPHER_CTX_cleanup(hdr_ctx);
  279. OPENSSL_free(hdr_ctx);
  280. }
  281. if(bdy_ctx != NULL){
  282. EVP_CIPHER_CTX_cleanup(bdy_ctx);
  283. OPENSSL_free(bdy_ctx);
  284. }
  285. return retval;
  286. }