void hash_table_insert(char *key, char *value) { unsigned int index = hash_gen(key); // this is bogus but so am I! hash_t *cursor = hash_table[index]; if (cursor == NULL) { hash_table[index] = cursor = calloc(1, sizeof(hash_t)); cursor->key = strdup(key); cursor->value = strdup(value); cursor->next = NULL; return; // first entry no collisions! } // collisisions! while (cursor->next) { cursor = cursor->next; } if (cursor->next == NULL) { cursor->next = calloc(1, sizeof(hash_t)); cursor = cursor->next; cursor->key = strdup(key); cursor->value = strdup(value); cursor->next = NULL; } }
hash_t * hash_table_search(char *key) { unsigned int idx = hash_gen(key); hash_t *tmp = hash_table[idx]; if (!tmp) return NULL; while (tmp) { if (!strcmp(tmp->key, key)) return (hash_t *) tmp; tmp = tmp->next; } }
int main(void) { register_all_ciphers(); register_all_hashes(); register_all_prngs(); #ifdef USE_LTM ltc_mp = ltm_desc; #elif defined(USE_TFM) ltc_mp = tfm_desc; #elif defined(USE_GMP) ltc_mp = gmp_desc; #elif defined(EXT_MATH_LIB) extern ltc_math_descriptor EXT_MATH_LIB; ltc_mp = EXT_MATH_LIB; #else fprintf(stderr, "No MPI provider available\n"); exit(EXIT_FAILURE); #endif printf("Generating hash vectors..."); fflush(stdout); hash_gen(); printf("done\n"); printf("Generating cipher vectors..."); fflush(stdout); cipher_gen(); printf("done\n"); printf("Generating HMAC vectors..."); fflush(stdout); hmac_gen(); printf("done\n"); #ifdef LTC_OMAC printf("Generating OMAC vectors..."); fflush(stdout); omac_gen(); printf("done\n"); #endif #ifdef LTC_PMAC printf("Generating PMAC vectors..."); fflush(stdout); pmac_gen(); printf("done\n"); #endif #ifdef LTC_EAX_MODE printf("Generating EAX vectors..."); fflush(stdout); eax_gen(); printf("done\n"); #endif #ifdef LTC_OCB_MODE printf("Generating OCB vectors..."); fflush(stdout); ocb_gen(); printf("done\n"); #endif #ifdef LTC_OCB3_MODE printf("Generating OCB3 vectors..."); fflush(stdout); ocb3_gen(); printf("done\n"); #endif #ifdef LTC_CCM_MODE printf("Generating CCM vectors..."); fflush(stdout); ccm_gen(); printf("done\n"); #endif #ifdef LTC_GCM_MODE printf("Generating GCM vectors..."); fflush(stdout); gcm_gen(); printf("done\n"); #endif printf("Generating BASE64 vectors..."); fflush(stdout); base64_gen(); printf("done\n"); printf("Generating MATH vectors..."); fflush(stdout); math_gen(); printf("done\n"); printf("Generating ECC vectors..."); fflush(stdout); ecc_gen(); printf("done\n"); #ifdef LTC_LRW_MODE printf("Generating LRW vectors..."); fflush(stdout); lrw_gen(); printf("done\n"); #endif return 0; }
int main(void) { reg_algs(); printf("Generating hash vectors..."); fflush(stdout); hash_gen(); printf("done\n"); printf("Generating cipher vectors..."); fflush(stdout); cipher_gen(); printf("done\n"); printf("Generating HMAC vectors..."); fflush(stdout); hmac_gen(); printf("done\n"); printf("Generating OMAC vectors..."); fflush(stdout); omac_gen(); printf("done\n"); printf("Generating PMAC vectors..."); fflush(stdout); pmac_gen(); printf("done\n"); printf("Generating EAX vectors..."); fflush(stdout); eax_gen(); printf("done\n"); printf("Generating OCB vectors..."); fflush(stdout); ocb_gen(); printf("done\n"); printf("Generating CCM vectors..."); fflush(stdout); ccm_gen(); printf("done\n"); printf("Generating GCM vectors..."); fflush(stdout); gcm_gen(); printf("done\n"); printf("Generating BASE64 vectors..."); fflush(stdout); base64_gen(); printf("done\n"); return 0; }
int send_file(char* file_name, int sk) { FILE* file; FILE* fp; int name_size; // size of the name of the file to be sent int size,k_size; // size of the file to be sent int ret, i; unsigned char* buffer; // pointer to the buffer containing the file char* sym_key; int enckeysize; int remote_random,local_random; char* password; //***** recieve the random value from server *****// ret = recv(sk, &remote_random, sizeof(int), MSG_WAITALL); file = fopen("key","r"); /* Retrieve the size of the key to be sent */ fseek(file,0,SEEK_END); k_size = ftell(file); /* Memory allocation for the key to be sent */ sym_key = malloc(k_size * sizeof (char)); fseek(file, 0, SEEK_SET); /* Read key from file */ ret = fread(sym_key, 1, k_size, file); //***** RSA ENCRYPTION PART BEGIN *****// char* enckey; RSA* rsa = RSA_new(); fp = fopen("pub.pem","r"); PEM_read_RSAPublicKey(fp,&rsa,NULL,NULL); enckeysize =RSA_size(rsa); enckey=malloc(enckeysize * sizeof(char)); RSA_encrypt(sym_key,enckey,k_size,rsa); fclose(fp); //*** RSA ENCRYPTION PART end ***// /* Computation of the length of the filename */ name_size = strlen(file_name); /* Open the file to be sent */ file = fopen(file_name,"r"); if(file == NULL) { printf("\nError opening the file file\n"); return 1; } /* Retrieve the size of the file to be sent */ fseek(file,0,SEEK_END); size = ftell(file); /* Memory allocation for the file to be sent */ buffer = malloc(size * sizeof (char)); fseek(file, 0, SEEK_SET); /* File reading */ ret = fread(buffer, 1, size, file); if(ret < size) { printf("\n Error reading the file \n"); return 1; } fclose(file); /* The length of the file name is sent */ ret = send(sk, &name_size, sizeof(name_size), 0); if(ret != sizeof(name_size)){ printf("\n Error trasmitting the length of the file name\n "); return 1; } /* The file name is sent */ ret = send(sk, file_name, name_size, 0); if(ret < name_size){ printf("\n Error transmitting the file name\n "); return 1; } //****** Generate hash for freshness and origin(password) check *****// time_t t; int password_size; int fresh_size; char* fresh_txt; t = time(NULL); srand ( time(NULL)); local_random = rand(); file = fopen("passofA.txt","r"); fseek(file,0,SEEK_END); password_size = ftell(file); password = malloc(password_size * sizeof (char)); fseek(file, 0, SEEK_SET); ret = fread(password, 1, password_size, file); fclose(file); int pass_hash_len; const EVP_MD *md1; md1 = EVP_get_digestbyname("sha1"); unsigned char pass_md_value[EVP_MD_size(md1)]; pass_hash_len=hash_gen(password,&pass_md_value[0]); fresh_size=sizeof(password)+sizeof(local_random)+sizeof(remote_random); fresh_txt = malloc(fresh_size); int loc_rand_size=sizeof(local_random); int rem_rand_size = sizeof(remote_random); memcpy(fresh_txt,&pass_md_value[0],pass_hash_len); memcpy(&fresh_txt[pass_hash_len],&local_random,loc_rand_size); memcpy(&fresh_txt[pass_hash_len+loc_rand_size],&remote_random,rem_rand_size); const EVP_MD *md; md = EVP_get_digestbyname("sha1"); unsigned char md_value[EVP_MD_size(md)]; int md_len; md_len=hash_gen(fresh_txt,&md_value[0]); printf("\n Freshness Digest is: \n"); for(i = 0; i < md_len; i++) printbyte(md_value[i]); printf("\n"); ///*** SYMMETRIC KEY ENCRYPTION PART BEGIN ***/// char* totbuffer; int nctot; char *plaintext, *ciphertext; int totbufsize = size+md_len; totbuffer = malloc(totbufsize); // message + digest for freshness and password memcpy(totbuffer,buffer,size); memcpy(&totbuffer[size],md_value,md_len); ciphertext = malloc(totbufsize+128); nctot = symmetric_encrypt(totbuffer,ciphertext,totbufsize); // encrypted size //***** SYMMETRIC KEY encryption part END *****/// //***** concatenate enckey and ciphertext *****// char* textnkeynhash; int totsize; totsize=nctot+enckeysize+loc_rand_size; textnkeynhash=malloc(totsize); memcpy(textnkeynhash,ciphertext,nctot); memcpy(&textnkeynhash[nctot],enckey,enckeysize); memcpy(&textnkeynhash[nctot+enckeysize],&local_random,loc_rand_size); /* The file size is sent */ ret = send(sk, &totsize, sizeof(totsize), 0); if(ret != sizeof(size)){ printf("\n Error transmitting the file size\n "); return 1; } /* The file is sent */ ret = send(sk, textnkeynhash, totsize, 0); if(ret < size){ printf("\n Error transmitting the file\n"); return 1; } printf("\n File %s with size %d bytes has been sent\n", file_name, totsize); free(buffer); free(ciphertext); return 0; }
int receive_file(int sk) { int ret; int name_size; // length of the name of the received file char* filename; // name of the received file int size,i; // size of the buffer for the plaintext char* buffer; // plaintext buffer FILE* file; // pointer to the file where the received message will be saved time_t t; t = time(NULL); srand ( time(NULL)); int local_random = rand(); send(sk, &local_random, sizeof(int), 0); printf("\n generated random value is %d",local_random); /* Reception of the length of the file name */ ret = recv(sk, &name_size, sizeof(name_size), MSG_WAITALL); if (ret != sizeof(name_size)) { printf("%d \n Error receiving the length of the file name\n",ret); return 1; } /* Memory allocation */ filename = malloc(sizeof(char) * (name_size + 1)); if(filename == NULL) { printf("\n Error allocating memory\n"); return 1; } /* Reception of the file name */ ret = recv(sk, filename, name_size, MSG_WAITALL); if(ret != name_size){ printf(" \n Error receiving the file name\n"); return 1; } filename[name_size] ='\0'; /* End of string */ /* Reception of the file size */ ret = recv(sk, &size, sizeof(size), MSG_WAITALL); if(ret != sizeof(size)) { printf("\n Error receiving the file size\n"); return 1; } /* Memory allocation */ buffer = malloc(size * sizeof(char)); if(buffer == NULL){ printf("\n Error allocating memory\n"); return 1; } /* Reception of the file */ ret = recv(sk, buffer, size, MSG_WAITALL); if(ret != size) { printf("\n Error receiving the file\n"); return 1; } //***** SEPARATE THE RECIEVED TEXT BEGIN *****// int remote_random; int md_len = 20; int rem_rand_size = sizeof(remote_random); char* recieved_hash; char* ciphertext; char* enc_key; int enckeysize =128; int enctextsize; recieved_hash = malloc(md_len); RSA* rsa = RSA_new(); enctextsize = size - enckeysize - rem_rand_size; ciphertext = malloc(enctextsize); enc_key = malloc(enckeysize); memcpy(ciphertext,buffer,enctextsize); memcpy(enc_key,&buffer[enctextsize],enckeysize); memcpy(&remote_random,&buffer[enctextsize+enckeysize],rem_rand_size); //****** Generate hash for freshness and origin(password) check *****// char* password; int password_size; int loc_rand_size; int fresh_size; char* fresh_txt; file = fopen("passofAhash.txt","r"); fseek(file,0,SEEK_END); password_size = ftell(file); password = malloc(password_size * sizeof (char)); fseek(file, 0, SEEK_SET); /* File reading */ ret = fread(&password[0], 1, password_size, file); fclose(file); loc_rand_size=sizeof(local_random); fresh_size=password_size+loc_rand_size+sizeof(remote_random); fresh_txt = malloc(fresh_size); memcpy(fresh_txt,&password[0],password_size); memcpy(&fresh_txt[password_size],&local_random,loc_rand_size); memcpy(&fresh_txt[password_size+loc_rand_size],&remote_random,rem_rand_size); const EVP_MD *md; md = EVP_get_digestbyname("sha1"); unsigned char md_value[EVP_MD_size(md)]; md_len=hash_gen(fresh_txt,&md_value[0]); printf("\n \n Freshness hash calculated in server is:\n "); for(i = 0; i < md_len; i++) printbyte(md_value[i]); printf("\n"); //***** DECRYPT THE KEY PART BEGIN *****// char* key; int flen; FILE* fp; fp = fopen("priv.pem","r"); OpenSSL_add_all_algorithms(); rsa = PEM_read_RSAPrivateKey(fp,&rsa,NULL,"password"); if(rsa == NULL) printf("\n ERROR reading rsa private key \n"); flen = RSA_size(rsa); key = malloc(flen); ret = RSA_private_decrypt(flen,enc_key,key,rsa,RSA_PKCS1_PADDING); printf("\n Recieved symmetric key is : \n"); for (i = 0; i < 8; i++) printbyte(key[i]); printf("\n"); free(enc_key); fclose(fp); RSA_free(rsa); ///*** DECRYPT the key part END ***/// ///*** DECRYPT THE MESSAGE PART BEGIN ***/// int nctot; char* decryptedtext; char* plaintext; int msg_len; decryptedtext = (char *)malloc(enctextsize+128); //call decryption function nctot = symmetric_decrypt(ciphertext,decryptedtext,key, enctextsize); msg_len = nctot - md_len; //size of plaintext message plaintext = malloc(msg_len); //separate the plain text from the freshness and password hash memcpy(plaintext,decryptedtext,msg_len); memcpy(recieved_hash,&decryptedtext[msg_len],md_len); printf("\n \n Freshness hash recieved from client is: \n"); for(i = 0; i < md_len; i++) printbyte(recieved_hash[i]); printf("\n"); //compare recieved digest with locally calculated digest to check freshness and password ret = strcmp(recieved_hash,&md_value[0]); if(ret==0)printf("\n MESSAGE FRESHNESS AND ORIGIN IS VERIFIED"); else printf("message not fresh with ret value %d",ret); //***** DECRYPT the message part END *****// /* Open the file to save the received message */ file = fopen(filename,"w"); if(file == NULL) { printf("\n File not found\n"); return 1; } /* Write the received message in the local file */ ret = fwrite(plaintext, 1, msg_len, file); if(ret < msg_len) { printf("\n Error writing the file \n"); return 1; } printf("\n Received file %s with size %d bytes\n", filename, size); fclose(file); free(filename); free(buffer); free(ciphertext); free(plaintext); return 0; }
static void _default_handler(watch_entry_t *pentry, int file_fd, const char *dir_name, E_CHANGE_MODE_t change_mode) { static char *move_from_str; static char *move_to_str; static char *last_to; static char *last_from; static int same_level_move = 0; #if 0 SUB_MOVE_FROM,/* in watched directory, something has be moved from */ SUB_MOVE_TO,/* in watched directory, something has be moved to */ SUB_DELETE, /* file or directory which in watched directory has be deleted */ SUB_CREATE, /* file or directory which in watched directoyr has be added */ SELF_MOVE, /* the watched directroy itself be moved */ SELF_DELETE,/* the watched directory itself be deleted */ #endif dbg("file_fd=%d dir_name=%s EVENT=%s\n", file_fd, dir_name, change_mode_2_str(change_mode)); /* process delete */ if( SELF_DELETE == change_mode || SUB_DELETE == change_mode ) return; #if 1 /* sometimes cannot process, because the event didnot have new name * when it's parent is watched, then can process * */ /* process selfmove */ if( SELF_MOVE == change_mode ) { /* 1.check if moved to the same directory, just rename it in table */ if( same_level_move ) { /* in same directory, just rename */ unsigned int *phash; int index, len; path_entry_t *ppath = pentry->normal_entry; #if 0 last_from = strrchr(move_from_str, '/'); last_to = strrchr(dir_name, '/'); #endif last_to ++;/* skip '/' */ len = strlen(last_to); dbg("same level move\n"); /* find path hash */ do{ /* didnot in this path_entry, try next */ if( file_fd <= ppath->max && file_fd > ppath->min ) break; dbg("ppath->min = %d max=%d\n", ppath->min, ppath->max); ppath = ppath->next; }while(ppath != NULL); if( NULL == ppath ) { /* file_fd not in the entry */ dbg("find old path fail. file_fd=%d\n", file_fd); goto SELF_MOVE_OUT; } index = file_fd - ppath->min; phash = &ppath->path_hash[index].hash; dbg("find old path. old hash=%u\n", *phash); /* find where to save new name */ ppath = pentry->normal_entry; do{ if( ppath->end - ppath->start > len + 1 ) { break; } ppath = ppath->next; }while(ppath != NULL); if( NULL == ppath ) { dbg("find where to save\n"); ppath = _get_path_entry(); if( NULL == ppath ) goto SELF_MOVE_OUT; ppath->next = pentry->normal_entry; pentry->normal_entry= ppath; ppath->min = pentry->normal_entry->max; ppath->max = ppath->min + PATH_ENTRY_NUM; } dbg("find new path\n"); /* save new name */ ppath->end -= len+1; strcpy(ppath->end, last_to); /* member in hash, replace with new hash */ dbg("len:%d move_from_str:%s move_to_str:%s\n", len, move_from_str, move_to_str); *phash = hash_gen(last_to, len); dbg("new hash=%u\n", *phash); hash_insert(&pentry->hash_table, *phash, ppath->end); if( move_from_str ) { free(move_from_str); move_from_str = NULL; } SELF_MOVE_OUT: same_level_move = 0; if( move_to_str ) { free(move_to_str); move_to_str = NULL; } if( move_from_str ) { free(move_from_str); move_from_str = NULL; } return; } /* 2.if moved to another directory, need change it's, * the parent will do it, so just return * */ return; } #endif if( SUB_MOVE_FROM == change_mode ) { if( move_from_str ) { free(move_from_str); move_from_str = NULL; } if( dir_name ) move_from_str = strdup(dir_name); return; } /* check if move to the same dir * if in same the watch can work continuly, just replace the name * if didnot in same directory, then it same as SUB_CREATE * */ if( SUB_MOVE_TO == change_mode ) { if( !move_from_str ) { return; } if( move_to_str ) { free(move_to_str); move_to_str = NULL; } if( dir_name ) move_to_str = strdup(dir_name); dbg("%d: move_to_str=%s move_from_str=%s\n", __LINE__, move_to_str, move_from_str); last_from = strrchr(move_from_str, '/'); last_to = strrchr(move_to_str, '/'); dbg("move_from_str=%s move_to_str=%s\n", move_from_str, move_to_str); if( last_to && last_from ) { dbg("last_to=%s last_from=%s\n", last_to, last_from); if( (last_to - move_to_str) != (last_from - move_from_str) ) goto MOVE_TO_CREATE; if( strncmp(move_from_str, move_to_str, last_to - move_to_str) ) goto MOVE_TO_CREATE; same_level_move = 1; return; }/* end of if( !(last_to && last_from)) */ MOVE_TO_CREATE: change_mode = SUB_CREATE; } /* process add */ if( SUB_CREATE == change_mode ) { struct stat fstat; dbg("process add: dir_name=%s\n", dir_name); if( -1 == lstat(dir_name, &fstat) ) { return; } /* process dir, if not dir, just return */ if ( S_ISDIR(fstat.st_mode) ) { /* process add */ _watch_on_dir_recur(pentry, dir_name, file_fd); } return; } }
static int _map_fd_path(watch_entry_t *pentry, int file_fd, const char *fullpath, int parent_fd) { int len, index; path_entry_t *ppath = pentry->normal_entry; dbg("in file_fd=%d parent_fd=%d fullpath=%s\n", file_fd, parent_fd, fullpath); len = strlen(fullpath) + 1; /* find the valid paht_entry */ do{ /* didnot in this path_entry, try next */ if( (file_fd >= ppath->min) && (file_fd <= ppath->max) && (ppath->start + len < ppath->end ) ) { /* can use */ break; } /* this path_entry no memory, try next */ ppath = ppath->next; }while(ppath != NULL); if( NULL == ppath ) { /* get new path_entry */ ppath = _get_path_entry(); if( NULL == ppath ) { /* no memory */ return -1; } /* insert to list */ ppath->next = pentry->normal_entry; pentry->normal_entry = ppath; ppath->min = (file_fd/PATH_ENTRY_NUM) * PATH_ENTRY_NUM; ppath->max = ppath->min + PATH_ENTRY_NUM; dbg("min=%d max = %d\n", ppath->min, ppath->max); } dbg("find path_entry\n"); /* set the data */ index = file_fd - ppath->min; ppath->path_hash[index].fd = file_fd; ppath->path_hash[index].prefix_fd = parent_fd; dbg("index=%d max=%d min=%d\n", index, ppath->max, ppath->min); if(-1 == parent_fd ) { /* if not parrent, this fullpath is it's path */ int len = strlen(fullpath); unsigned int hash = hash_gen(fullpath,len); dbg("find this_path: %s hash=%u\n", fullpath, hash); if( (len > 2) && str_end_with(fullpath, '/') ) len -= 1; ppath->end -= (len+1); strncpy(ppath->end, fullpath, len); ppath->end[len] = 0; if( NULL == hash_insert(&pentry->hash_table, hash, ppath->end) ) { dbg("watch: map fd path failed: hash:%u path:%s\n", hash, ppath->end); return -1; } ppath->path_hash[index].hash = hash; }else{ /* split this_data & prefix_data , get them's hash_data */ char *this_path; unsigned int this_hash; int this_len = 0; len = strlen(fullpath); this_path = strrchr(fullpath, '/'); if( this_path ) { /* the fullpath is start with "/", and no other level */ this_path++; /* end with '/' */ if( ! *this_path ) { this_path = (char *)fullpath; } } if( NULL == this_path ) { /* fullpath is no prefix path */ this_path = (char *)fullpath; } /* save the this path to the memory */ this_len = strlen(this_path) + 1; ppath->end -= this_len; strncpy(ppath->end, this_path, this_len); if( (this_len > 2) && str_end_with(this_path, '/') ) { ppath->end[this_len-2] = 0; } this_hash = hash_gen(this_path, this_len-1); dbg("find this_path: %s hash=%u\n", this_path, this_hash); if( NULL == hash_insert(&pentry->hash_table, this_hash, ppath->end) ) { dbg("watch: map fd path failed: hash:%u path:%s\n", this_hash, ppath->end); return -1; } ppath->path_hash[index].hash = this_hash; } #if 0 /* incarse max */ if( file_fd > ppath->max ) { //ppath->max = file_fd; } #endif /* modify start point */ ppath->start += sizeof(path_hash_t); return 0; }