Advertisement
Guest User

Untitled

a guest
Apr 5th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <sys/socket.h>
  5. #include <pthread.h>
  6. #include <netinet/in.h>
  7. #include <unistd.h>
  8. #include <string.h>
  9.  
  10. #include <openssl/bn.h>
  11. #include <time.h>
  12. #include <math.h>
  13. #include <memory.h>
  14. #include <string.h>
  15. #include <openssl/sha.h>
  16. #include <openssl/rand.h>
  17.  
  18. struct fileReadArgs{
  19. int childNumber;
  20. int sockfd;
  21. //char* filename;
  22. };
  23. void* receiveMessage(void *fd){
  24. struct fileReadArgs *p = (struct fileReadArgs*)fd;
  25. char client_response[256];
  26. int sockfd = p->sockfd;
  27. recv(sockfd, &client_response, sizeof(client_response), 0);
  28. printf("Ther client send this data: %s\n", client_response);
  29. //printf("Sending message to client");
  30. //send(sockfd, server_message, sizeof(server_message), 0);
  31.  
  32. //Retrieve s and v
  33.  
  34. }
  35.  
  36. void* sendMessage(void *fd){
  37. struct fileReadArgs *p = (struct fileReadArgs*)fd;
  38. char server_message[256] = "MUHAHAHAHHA!";
  39. int sockfd = p->sockfd;
  40. printf("Sending message to client: %s", server_message);
  41. send(sockfd, server_message, sizeof(server_message), 0);
  42. }
  43.  
  44. /*
  45.  
  46. void fileRead(void *fd){
  47. int BUFFER_SIZE = 64;
  48. struct fileReadArgs p = (struct fileReadArgs)fd;
  49. int sockfd = p->sockfd;
  50. int childNumber = p->childNumber;
  51. char* filename = p->filename;
  52. FILE *f = fopen(filename, "rb");
  53. if(f == NULL){
  54. printf("Error opening file");
  55. return;
  56. }
  57. fseek(f, 0, SEEK_END);
  58. int fileSize = ftell(f);
  59. fseek(f, 0, SEEK_SET);
  60.  
  61. char size[32];
  62. sprintf(size, "%d", fileSize);
  63. write(sockfd, size, 32);
  64.  
  65. char *name = strrchr(filename, '/');
  66. if(name == NULL)
  67. name = filename;
  68. else
  69. name++;
  70.  
  71. char fileNameSize = strlen(name)+1;
  72. write(sockfd, &fileNameSize, 1);
  73.  
  74. write(sockfd, name, strlen(name)+1);
  75.  
  76. int n = 0; //total bytes read/written
  77. char buffer[BUFFER_SIZE];
  78. while(!feof(f)){
  79. int read = fread(buffer, 1, BUFFER_SIZE, f);
  80. if(read < 0)
  81. printf("Error reading from file");
  82.  
  83. write(sockfd, buffer, read);
  84. //printf("%s",buffer);
  85.  
  86. n+=read;
  87. //loader(n, fileSize, LOADER_LENGTH, 0);
  88. }
  89.  
  90. fclose(f);
  91. }*/
  92.  
  93.  
  94. int serverFileTransfer(int port){
  95. //char server_message[256] = "MUHAHAHAHHA!";
  96.  
  97. int sockfd = socket(AF_INET, SOCK_STREAM, 0);
  98. if(sockfd<0)
  99. printf("Error opening socket");
  100.  
  101. struct sockaddr_in serv_addr, cli_addr;
  102. memset((char*) &serv_addr, 0, sizeof(serv_addr));
  103.  
  104. serv_addr.sin_family = AF_INET;
  105. serv_addr.sin_port = htons(port);
  106. serv_addr.sin_addr.s_addr = INADDR_ANY;
  107.  
  108.  
  109. if(bind(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
  110. printf("Error Binding");
  111.  
  112. listen(sockfd, 5);
  113.  
  114. socklen_t cilen = sizeof(cli_addr);
  115.  
  116. pthread_t newThread;
  117. pthread_attr_t attr;
  118. pthread_attr_init(&attr);
  119. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  120.  
  121. int noOfChilds = 0;
  122.  
  123. while(1){
  124. int newsockfd = accept(sockfd, (struct sockaddr*)&cli_addr, &cilen);
  125. noOfChilds++;
  126.  
  127. struct fileReadArgs args;
  128. args.sockfd = newsockfd;
  129. args.childNumber = noOfChilds;
  130. //args.filename = "123.txt";
  131.  
  132. if(newsockfd < 0)
  133. printf("Error accepting");
  134.  
  135. //send(newsockfd, server_message, sizeof(server_message), 0);
  136. //pthread_create(&newThread, &attr, &fileRead, &args);
  137.  
  138. pthread_create(&newThread, &attr, &receiveMessage, &args);
  139.  
  140. }
  141.  
  142. pthread_attr_destroy(&attr);
  143.  
  144. return 0;
  145. }
  146.  
  147. //1. Find a user list and password
  148. //User - Salt - Password Verifer(salt + password)[hardcode]
  149. struct userlist
  150. {
  151. char username[30];
  152. char salt[32];//int RAND_bytes(salt,32);
  153. char password[30];
  154. //verifier
  155. } user;
  156.  
  157. //Hardcode user list, salt, v
  158. //2. Compute x, x= H(s || w)
  159. char* compute_x(char* salt, char* password){
  160. //Concat Salt and password
  161. const char * concat_x = (char *) malloc(1 + strlen(salt)+ strlen(password) );
  162. strcpy(concat_x, salt);
  163. strcat(concat_x, password);
  164. printf("%s\n", concat_x);
  165.  
  166. unsigned char digest[SHA256_DIGEST_LENGTH];
  167.  
  168. SHA256_CTX ctx;
  169. SHA256_Init(&ctx);
  170. SHA256_Update(&ctx, concat_x, strlen(concat_x));
  171. SHA256_Final(digest, &ctx);
  172.  
  173. static char mdString[SHA256_DIGEST_LENGTH*2+1];
  174. for (int i = 0; i < SHA256_DIGEST_LENGTH; i++)
  175. sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);
  176.  
  177. printf("x = H(s || w) : %s\n", mdString);
  178. return mdString;
  179. }
  180.  
  181. //3. compute verifier
  182. //g^x mod N
  183. char* compute_v(char* x, char* N, char* g){
  184. /*
  185. printf("x in compute_v: %ld\n",x);
  186. printf("FAKE N: %d\n",N);
  187. printf("g: %d\n", g);
  188. int result = g^x % N;
  189. printf("result = %d", result);
  190. */
  191.  
  192. //printf("x = %s\n", x);
  193. //printf("N = %s\n", N);
  194. //printf("g = %s\n", g);
  195. BIGNUM *tmp1 = BN_new();
  196.  
  197. BIGNUM *X = BN_new();
  198. BN_hex2bn(&X, x);
  199.  
  200. BIGNUM *PrimeN = BN_new();
  201. BN_hex2bn(&PrimeN, N);
  202.  
  203. BIGNUM *G = BN_new();
  204. BN_hex2bn(&G, g);
  205.  
  206. BN_CTX *ctx = BN_CTX_new();
  207. BN_mod_exp(tmp1, G, X, PrimeN, ctx);
  208.  
  209. char * v = BN_bn2hex(tmp1);
  210. printf("v: %s\n",v);
  211. return v;
  212. }
  213.  
  214.  
  215. //get b
  216. char* random_b(){
  217. BIGNUM *BN_b = BN_new();
  218. BN_rand(BN_b, 256, -1, 0);
  219. char * b = BN_bn2hex(BN_b);
  220. printf("b: %s\n",b);
  221. return b;
  222. }
  223.  
  224. //5. get key, k = H (N || G)
  225. char* get_small_k(char* N, char* g){
  226. //Concat N and G
  227. const char * concat_x = (char *) malloc(1 + strlen(N)+ strlen(g));
  228. strcpy(concat_x, N);
  229. strcat(concat_x, g);
  230. printf("%s\n", concat_x);
  231.  
  232. unsigned char digest[SHA256_DIGEST_LENGTH];
  233.  
  234. SHA256_CTX ctx;
  235. SHA256_Init(&ctx);
  236. SHA256_Update(&ctx, concat_x, strlen(concat_x));
  237. SHA256_Final(digest, &ctx);
  238.  
  239. static char mdString[SHA256_DIGEST_LENGTH*2+1];
  240. for (int i = 0; i < SHA256_DIGEST_LENGTH; i++)
  241. sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);
  242.  
  243. printf("k = H (N || G) : %s\n", mdString);
  244. return mdString;
  245. }
  246.  
  247. //6. Get B = kv + g^b
  248. char* get_B(char* k,char* v,char* g,char* b, char* N){
  249. BN_CTX *ctx = BN_CTX_new();
  250. BIGNUM *tmp1 = BN_new();
  251. BIGNUM *tmp2 = BN_new();
  252. BIGNUM *B_v = BN_new();
  253.  
  254. BIGNUM *K = BN_new();
  255. BN_hex2bn(&K, k);
  256.  
  257. BIGNUM *V = BN_new();
  258. BN_hex2bn(&V, v);
  259.  
  260. BIGNUM *G = BN_new();
  261. BN_hex2bn(&G, g);
  262.  
  263. BIGNUM *B = BN_new();
  264. BN_hex2bn(&B, b);
  265.  
  266. BIGNUM *PrimeN = BN_new();
  267. BN_hex2bn(&PrimeN, N);
  268.  
  269. BN_mul(tmp1, K, V, ctx);
  270. BN_mod_exp(tmp2, G, B, PrimeN, ctx);
  271. BN_mod_add(B_v, tmp1, tmp2, PrimeN, ctx);
  272.  
  273. char * B_value = BN_bn2hex(B_v);
  274. printf("B = kv + g^b : %s\n",B_value);
  275. return B_value;
  276. }
  277.  
  278. char* concat_ngs(char* N, char* g, char* s){
  279. //Concat N and G
  280. const char * concat_x = (char *) malloc(1 + strlen(N)+ strlen(g));
  281. strcpy(concat_x, N);
  282. strcat(concat_x, g);
  283. const char * concat_x2 = (char *) malloc(1 + strlen(concat_x)+ strlen(s));
  284. strcpy(concat_x2, concat_x);
  285. strcat(concat_x2, s);
  286. //printf("CONCAT2: %s\n", concat_x2);
  287. return concat_x2;
  288. }
  289.  
  290. //Printing BIG NUM VALUE
  291. char* toPrintBIGNUM(char* str){ //HEX to BIGNUM
  292. BIGNUM *p = BN_new();
  293. BN_hex2bn(&p, str);
  294. //For printing purposes
  295. char * number_str = BN_bn2hex(p);
  296. printf("HEX TO BIGNUM: %s\n", number_str);
  297. return number_str;
  298. }
  299.  
  300.  
  301. int main(int argc, char *argv[]){
  302.  
  303. //Create userlist
  304. struct userlist user1;
  305. strcpy( user1.username, "yihjin");
  306. strcpy( user1.salt, "yes");
  307. strcpy( user1.password, "supersecret");
  308. struct userlist user2;
  309. strcpy( user2.username, "weijian");
  310. strcpy( user2.salt, "no");
  311. strcpy( user2.password, "nosecret");
  312. struct userlist user3;
  313. strcpy( user3.username, "nicholas");
  314. strcpy( user3.salt, "nah");
  315. strcpy( user3.password, "secretpass");
  316.  
  317. /*
  318. //print user1 info
  319. printf("----------------------------------\n");
  320. printf( "username : %s\n", user1.username);
  321. printf( "salt : %s\n", user1.salt);
  322. printf( "password : %s\n", user1.password);
  323.  
  324. //print user2 info
  325. printf( "username : %s\n", user2.username);
  326. printf( "salt : %s\n", user2.salt);
  327. printf( "password : %s\n", user2.password);
  328.  
  329. //print user3 info
  330. printf( "username : %s\n", user3.username);
  331. printf( "salt : %s\n", user3.salt);
  332. printf( "password : %s\n", user3.password);
  333. printf("----------------------------------\n");
  334. */
  335. //int N = 12345678; //give a small value fuck this shit
  336. //int g = 2;
  337.  
  338.  
  339.  
  340. char N[] = "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA051015728E5A8AACAA68FFFFFFFFFFFFFFFF";
  341. toPrintBIGNUM(N);
  342. char g[] = "2";
  343.  
  344. concat_ngs(N, g, user1.salt);
  345.  
  346.  
  347. char* x;
  348. x = compute_x(user1.salt, user1.password);
  349. //printf("x: %s\n",x);
  350.  
  351. char* v;
  352. v = compute_v(x,N,g);
  353.  
  354. char* b;
  355. b = random_b();
  356.  
  357. char* k;
  358. k = get_small_k(N,g);
  359. //printf("k: %s\n",k);
  360.  
  361. char* B_value;
  362. B_value= get_B( k, v, g, b, N);
  363.  
  364.  
  365.  
  366. //char g[] = "2";
  367. //toBIGNUM(g);
  368.  
  369. //static long x;
  370. //x = compute_x(user1.salt, user1.password);
  371. //printf("----new_x=%s\n", x);
  372. //compute_v(x, N, g);
  373. //rand_string();
  374.  
  375.  
  376. //serverFileTransfer(8080);
  377.  
  378.  
  379. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement