crypto.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * Slitheen - a decoy routing system for censorship resistance
  3. * Copyright (C) 2017 Cecylia Bocovich (cbocovic@uwaterloo.ca)
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, version 3.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * Additional permission under GNU GPL version 3 section 7
  18. *
  19. * If you modify this Program, or any covered work, by linking or combining
  20. * it with the OpenSSL library (or a modified version of that library),
  21. * containing parts covered by the terms of [name of library's license],
  22. * the licensors of this Program grant you additional permission to convey
  23. * the resulting work. {Corresponding Source for a non-source form of such
  24. * a combination shall include the source code for the parts of the OpenSSL
  25. * library used as well as that of the covered work.}
  26. */
  27. #include <openssl/evp.h>
  28. #include <openssl/dh.h>
  29. #include <openssl/bn.h>
  30. #include <openssl/err.h>
  31. #include <openssl/rand.h>
  32. #include <openssl/ssl.h>
  33. #include <netinet/in.h>
  34. #include "crypto.h"
  35. #include "socks5proxy.h"
  36. #include "tagging.h"
  37. #include "ptwist.h"
  38. static super_data *super;
  39. /* PRF using sha384, as defined in RFC 5246 */
  40. int PRF(uint8_t *secret, int32_t secret_len,
  41. uint8_t *seed1, int32_t seed1_len,
  42. uint8_t *seed2, int32_t seed2_len,
  43. uint8_t *seed3, int32_t seed3_len,
  44. uint8_t *seed4, int32_t seed4_len,
  45. uint8_t *output, int32_t output_len){
  46. EVP_MD_CTX ctx, ctx_tmp, ctx_init;
  47. EVP_PKEY *mac_key;
  48. const EVP_MD *md = EVP_sha256();
  49. uint8_t A[EVP_MAX_MD_SIZE];
  50. size_t len, A_len;
  51. int chunk = EVP_MD_size(md);
  52. int remaining = output_len;
  53. uint8_t *out = output;
  54. EVP_MD_CTX_init(&ctx);
  55. EVP_MD_CTX_init(&ctx_tmp);
  56. EVP_MD_CTX_init(&ctx_init);
  57. EVP_MD_CTX_set_flags(&ctx_init, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
  58. mac_key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, secret, secret_len);
  59. /* Calculate first A value */
  60. EVP_DigestSignInit(&ctx_init, NULL, md, NULL, mac_key);
  61. EVP_MD_CTX_copy_ex(&ctx, &ctx_init);
  62. if(seed1 != NULL && seed1_len > 0){
  63. EVP_DigestSignUpdate(&ctx, seed1, seed1_len);
  64. }
  65. if(seed2 != NULL && seed2_len > 0){
  66. EVP_DigestSignUpdate(&ctx, seed2, seed2_len);
  67. }
  68. if(seed3 != NULL && seed3_len > 0){
  69. EVP_DigestSignUpdate(&ctx, seed3, seed3_len);
  70. }
  71. if(seed4 != NULL && seed4_len > 0){
  72. EVP_DigestSignUpdate(&ctx, seed4, seed4_len);
  73. }
  74. EVP_DigestSignFinal(&ctx, A, &A_len);
  75. //iterate until desired length is achieved
  76. while(remaining > 0){
  77. /* Now compute SHA384(secret, A+seed) */
  78. EVP_MD_CTX_copy_ex(&ctx, &ctx_init);
  79. EVP_DigestSignUpdate(&ctx, A, A_len);
  80. EVP_MD_CTX_copy_ex(&ctx_tmp, &ctx);
  81. if(seed1 != NULL && seed1_len > 0){
  82. EVP_DigestSignUpdate(&ctx, seed1, seed1_len);
  83. }
  84. if(seed2 != NULL && seed2_len > 0){
  85. EVP_DigestSignUpdate(&ctx, seed2, seed2_len);
  86. }
  87. if(seed3 != NULL && seed3_len > 0){
  88. EVP_DigestSignUpdate(&ctx, seed3, seed3_len);
  89. }
  90. if(seed4 != NULL && seed4_len > 0){
  91. EVP_DigestSignUpdate(&ctx, seed4, seed4_len);
  92. }
  93. if(remaining > chunk){
  94. EVP_DigestSignFinal(&ctx, out, &len);
  95. out += len;
  96. remaining -= len;
  97. /* Next A value */
  98. EVP_DigestSignFinal(&ctx_tmp, A, &A_len);
  99. } else {
  100. EVP_DigestSignFinal(&ctx, A, &A_len);
  101. memcpy(out, A, remaining);
  102. remaining -= remaining;
  103. }
  104. }
  105. EVP_PKEY_free(mac_key);
  106. EVP_MD_CTX_cleanup(&ctx);
  107. EVP_MD_CTX_cleanup(&ctx_tmp);
  108. EVP_MD_CTX_cleanup(&ctx_init);
  109. return 1;
  110. }
  111. /*
  112. * Generate the keys for the super encryption layer, based on the slitheen ID
  113. */
  114. int generate_super_keys(uint8_t *secret){
  115. super = calloc(1, sizeof(super_data));
  116. EVP_MD_CTX *mac_ctx;
  117. const EVP_MD *md = EVP_sha256();
  118. /* Generate Keys */
  119. uint8_t *hdr_key, *bdy_key;
  120. uint8_t *mac_secret;
  121. EVP_PKEY *mac_key;
  122. int32_t mac_len, key_len;
  123. key_len = EVP_CIPHER_key_length(EVP_aes_256_cbc());
  124. mac_len = EVP_MD_size(md);
  125. int32_t total_len = 2*key_len + mac_len;
  126. uint8_t *key_block = calloc(1, total_len);
  127. PRF(secret, SLITHEEN_SUPER_SECRET_SIZE,
  128. (uint8_t *) SLITHEEN_SUPER_CONST, SLITHEEN_SUPER_CONST_SIZE,
  129. NULL, 0,
  130. NULL, 0,
  131. NULL, 0,
  132. key_block, total_len);
  133. #ifdef DEBUG
  134. int i;
  135. printf("secret: \n");
  136. for(i=0; i< SLITHEEN_SUPER_SECRET_SIZE; i++){
  137. printf("%02x ", secret[i]);
  138. }
  139. printf("\n");
  140. printf("keyblock: \n");
  141. for(i=0; i< total_len; i++){
  142. printf("%02x ", key_block[i]);
  143. }
  144. printf("\n");
  145. #endif
  146. hdr_key = key_block;
  147. bdy_key = key_block + key_len;
  148. mac_secret = key_block + 2*key_len;
  149. /* Initialize MAC Context */
  150. mac_ctx = EVP_MD_CTX_create();
  151. EVP_DigestInit_ex(mac_ctx, md, NULL);
  152. mac_key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, mac_secret, mac_len);
  153. EVP_DigestSignInit(mac_ctx, NULL, md, NULL, mac_key);
  154. super->header_key = malloc(key_len);
  155. super->body_key = malloc(key_len);
  156. memcpy(super->header_key, hdr_key, key_len);
  157. memcpy(super->body_key, bdy_key, key_len);
  158. super->body_mac_ctx = mac_ctx;
  159. //Free everything
  160. free(key_block);
  161. EVP_PKEY_free(mac_key);
  162. return 0;
  163. }
  164. int peek_header(uint8_t *data){
  165. EVP_CIPHER_CTX *hdr_ctx = NULL;
  166. int32_t out_len;
  167. uint8_t *p = data;
  168. int retval = 1;
  169. //decrypt header
  170. #ifdef DEBUG
  171. int i;
  172. printf("Encrypted header:\n");
  173. for(i=0; i< SLITHEEN_HEADER_LEN; i++){
  174. printf("%02x ", p[i]);
  175. }
  176. printf("\n");
  177. #endif
  178. hdr_ctx = EVP_CIPHER_CTX_new();
  179. EVP_CipherInit_ex(hdr_ctx, EVP_aes_256_ecb(), NULL, super->header_key, NULL, 0);
  180. if(!EVP_CipherUpdate(hdr_ctx, p, &out_len, p, SLITHEEN_HEADER_LEN)){
  181. printf("Decryption failed!");
  182. retval = 0;
  183. goto end;
  184. }
  185. struct slitheen_hdr *sl_hdr = (struct slitheen_hdr *) p;
  186. if(!sl_hdr->len){//there are no data to be decrypted
  187. retval = 1;
  188. goto end;
  189. }
  190. #ifdef DEBUG_PARSE
  191. printf("Decrypted header (%d bytes):\n", SLITHEEN_HEADER_LEN);
  192. for(i=0; i< SLITHEEN_HEADER_LEN; i++){
  193. printf("%02x ", p[i]);
  194. }
  195. printf("\n");
  196. fflush(stdout);
  197. #endif
  198. retval = 1;
  199. end:
  200. if(hdr_ctx != NULL){
  201. EVP_CIPHER_CTX_cleanup(hdr_ctx);
  202. OPENSSL_free(hdr_ctx);
  203. }
  204. return retval;
  205. }
  206. int super_decrypt(uint8_t *data){
  207. EVP_CIPHER_CTX *bdy_ctx = NULL;
  208. EVP_CIPHER_CTX *hdr_ctx = NULL;
  209. uint8_t *p = data;
  210. int32_t out_len, len;
  211. uint8_t output[EVP_MAX_MD_SIZE];
  212. size_t mac_len;
  213. int i, retval = 1;
  214. //decrypt header
  215. #ifdef DEBUG
  216. printf("Encrypted header:\n");
  217. for(i=0; i< SLITHEEN_HEADER_LEN; i++){
  218. printf("%02x ", p[i]);
  219. }
  220. printf("\n");
  221. #endif
  222. hdr_ctx = EVP_CIPHER_CTX_new();
  223. EVP_CipherInit_ex(hdr_ctx, EVP_aes_256_ecb(), NULL, super->header_key, NULL, 0);
  224. if(!EVP_CipherUpdate(hdr_ctx, p, &out_len, p, SLITHEEN_HEADER_LEN)){
  225. printf("Decryption failed!");
  226. retval = 0;
  227. goto end;
  228. }
  229. struct slitheen_hdr *sl_hdr = (struct slitheen_hdr *) p;
  230. len = htons(sl_hdr->len);
  231. if(!sl_hdr->len){//there are no data to be decrypted
  232. retval = 1;
  233. goto end;
  234. }
  235. if(len %16){ //add padding to len
  236. len += 16 - len%16;
  237. }
  238. //#ifdef DEBUG_PARSE
  239. printf("Decrypted header (%d bytes):\n", SLITHEEN_HEADER_LEN);
  240. for(i=0; i< SLITHEEN_HEADER_LEN; i++){
  241. printf("%02x ", p[i]);
  242. }
  243. printf("\n");
  244. fflush(stdout);
  245. //#endif
  246. p += SLITHEEN_HEADER_LEN;
  247. //initialize body cipher context with IV
  248. bdy_ctx = EVP_CIPHER_CTX_new();
  249. EVP_CipherInit_ex(bdy_ctx, EVP_aes_256_cbc(), NULL, super->body_key, p, 0);
  250. p+=16;
  251. //compute mac
  252. EVP_MD_CTX mac_ctx;
  253. EVP_MD_CTX_init(&mac_ctx);
  254. EVP_MD_CTX_copy_ex(&mac_ctx, super->body_mac_ctx);
  255. EVP_DigestSignUpdate(&mac_ctx, p, len);
  256. EVP_DigestSignFinal(&mac_ctx, output, &mac_len);
  257. EVP_MD_CTX_cleanup(&mac_ctx);
  258. #ifdef DEBUG_PARSE
  259. printf("Received mac:\n");
  260. for(i=0; i< 16; i++){
  261. printf("%02x ", p[len+i]);
  262. }
  263. printf("\n");
  264. fflush(stdout);
  265. #endif
  266. #ifdef DEBUG_PARSE
  267. printf("Computed mac:\n");
  268. for(i=0; i< 16; i++){
  269. printf("%02x ", output[i]);
  270. }
  271. printf("\n");
  272. fflush(stdout);
  273. #endif
  274. if(memcmp(p+len, output, 16)){
  275. printf("MAC verification failed\n");
  276. retval = 0;
  277. goto end;
  278. }
  279. //decrypt body
  280. #ifdef DEBUG_PARSE
  281. printf("Encrypted data (%d bytes):\n", len);
  282. for(i=0; i< len; i++){
  283. printf("%02x ", p[i]);
  284. }
  285. printf("\n");
  286. #endif
  287. if(!EVP_CipherUpdate(bdy_ctx, p, &out_len, p, len)){
  288. printf("Decryption failed!");
  289. retval = 0;
  290. goto end;
  291. }
  292. #ifdef DEBUG_PARSE
  293. printf("Decrypted data (%d bytes):\n", out_len);
  294. for(i=0; i< out_len; i++){
  295. printf("%02x ", p[i]);
  296. }
  297. printf("\n");
  298. fflush(stdout);
  299. #endif
  300. p += out_len;
  301. retval = 1;
  302. end:
  303. if(hdr_ctx != NULL){
  304. EVP_CIPHER_CTX_cleanup(hdr_ctx);
  305. OPENSSL_free(hdr_ctx);
  306. }
  307. if(bdy_ctx != NULL){
  308. EVP_CIPHER_CTX_cleanup(bdy_ctx);
  309. OPENSSL_free(bdy_ctx);
  310. }
  311. return retval;
  312. }