/* Reads all the environment variables from envp and populates * a hashmap for use through out the execution of the command */ void set_env_opts( memmgr **mm, /* memory manager */ job_data **env_attr, char **envp) { int var_num = 0; char *name = NULL; char *value = NULL; int rc = PBSE_NONE; while (envp[var_num] != NULL) { rc = parse_env_line(mm, envp[var_num], &name, &value); if (rc == PBSE_NONE) { hash_add_item(mm, env_attr, name, value, ENV_DATA, SET); memmgr_free(mm, name); name = NULL; memmgr_free(mm, value); value = NULL; } var_num++; } } /* END set_env_opts() */
/* Reads all the environment variables from envp and populates * a hashmap for use through out the execution of the command */ void set_env_opts( memmgr **mm, /* memory manager */ job_data **env_attr, char **envp) { int var_num = 0; char *name = NULL; char *value = NULL; int rc = PBSE_NONE; while (envp[var_num] != NULL) { rc = parse_env_line(mm, envp[var_num], &name, &value); if (rc != PBSE_NONE) { fprintf(stderr, "Malformed environment variable %s. Will not add to job environment\n", envp[var_num]); ; exit(1); } /* strtolower(name); */ hash_add_item(mm, env_attr, name, value, ENV_DATA, SET); memmgr_free(mm, name); name = NULL; memmgr_free(mm, value); value = NULL; var_num++; } } /* END set_env_opts() */
static mem_header_t* get_mem_from_pool(MemMgrState& memmgr, size_t nquantas) { size_t total_req_size; mem_header_t* h; if (nquantas < min_pool_alloc_quantas) nquantas = min_pool_alloc_quantas; total_req_size = nquantas * sizeof(mem_header_t); if (memmgr.pool_free_pos + total_req_size <= memmgr.pool_size) { h = (mem_header_t*) (memmgr.pool + memmgr.pool_free_pos); h->s.size = nquantas; memmgr_free((void*) (h + 1)); memmgr.pool_free_pos += total_req_size; } else { return 0; } return memmgr.freep; }
static mem_header_t* get_mem_from_pool(void *buff, size_t buff_size, ulong nquantas) { buff_header_t *buff_header = MEMMGR_GET_BUFF_HEADER(buff); ulong total_req_size; mem_header_t* h; if (nquantas < MIN_POOL_ALLOC_QUANTAS) nquantas = MIN_POOL_ALLOC_QUANTAS; total_req_size = nquantas * sizeof(mem_header_t); if (pool_free_pos + total_req_size <= POOL_SIZE) { h = (mem_header_t*) (pool + pool_free_pos); h->s.size = nquantas; memmgr_free(buff, buff_size, (void*) (h + 1)); pool_free_pos += total_req_size; } else { return 0; } return freep; }
static mem_header_t* get_mem_from_pool(ulong nquantas) { ulong total_req_size; mem_header_t* h; if (nquantas < MIN_POOL_ALLOC_QUANTAS) nquantas = MIN_POOL_ALLOC_QUANTAS; total_req_size = nquantas * sizeof(mem_header_t); if (pool_free_pos + total_req_size <= pool_size) { h = (mem_header_t*) (pool + pool_free_pos); h->s.size = nquantas; memmgr_free((void*) (h + 1)); pool_free_pos += total_req_size; } else { return 0; } return freep; }
void free(void *ptr) { I(ma_initialized == TRUE); if (ptr != NULL) { /* sem_wait(&ma_sem); */ memmgr_free(mm, ptr); /* sem_post(&ma_sem); */ } fprintf(stderr, "free(%p)\n", ptr); }
void *MemMgrAllocatorRealloc(void * ptr, size_t amount, size_t *ret_size, unsigned int movable, void *opaque) { if (amount == 0) { memmgr_free(ptr); return NULL; } size_t ptr_actual_size = 0; if (ptr) { ptr_actual_size = MemMgrAllocatorMsize(ptr, opaque); if (ptr_actual_size >= amount) { *ret_size = ptr_actual_size; return ptr; } if (!movable) { return NULL; } } void * retval = memmgr_alloc(amount); *ret_size = MemMgrAllocatorMsize(retval, opaque); if (ptr) { memcpy(retval, ptr, std::min(amount, ptr_actual_size)); memmgr_free(ptr); } return retval; }
void MemMgrAllocatorDestroy(void *opaque) { memmgr_free(opaque); memmgr_destroy(); }
void MemMgrAllocatorFree (void *opaque, void *ptr) { memmgr_free(ptr); }
/** Run AES performance benchmarks. */ static void bench_aes(void) { int len, i; char *b1, *b2, *temp; crypto_cipher_t *c; uint64_t start, end; const int bytes_per_iter = (1<<24); reset_perftime(); c = crypto_cipher_new(NULL); memmgr_destroy(); memmgr_init_check_shared_mem(SHARED_SIZE, UIO_DEVICE, BASE_ADDRESS); FPGA_AES *cipher1 = NULL; char default_iv[] = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; cipher1 = fpga_aes_new_short_16((char*)crypto_cipher_get_key(c), default_iv, 2); if(cipher1 == NULL){ printf("\nCould not allocate cipher"); return; } b1 = memmgr_alloc(8192*2+16);//tor_malloc_zero(len); b2 = memmgr_alloc(8192*2+16);//tor_malloc_zero(len); temp = memmgr_alloc(8192*2+16); printf("\nKey: 0x"); for(i=0; i<16; i++){ printf("%02x", cipher1->key[i]); } printf("\n"); // printf("\nBytes per iter: %i", bytes_per_iter); for (len = 2; len <= 8192; len *= 2) { int iters = bytes_per_iter / len; // printf("\nIterations: %i\n", iters); memset(b1, 0, len); memset(b2, 0, len); memset(temp, 0, len); printf("\nb2: 0x"); for(i=0; i<len; i++){ printf("%02x", b2[i]); } start = perftime(); for (i = 0; i < iters; ++i) { // crypto_cipher_encrypt(c, b1, b2, len); // if(i == iters - 1){ // printf(" i: %i ", i); // } Aes_encrypt_memmgr(cipher1, b1, b2, len); } end = perftime(); memset(b2, 0, len); printf("\nFinal iv/iters: 0x%08x", iters); // for(i=1; i<16; i++){ // printf("%02x", cipher1->iv[i]); // } for(i=0; i<iters; ++i){ crypto_cipher_encrypt(c, temp, b2, len); } int incorrect = 0; printf("\nb1: 0x"); for(i=0; i<len; i++){ printf("%02x", b2[i]); } for(i=0; i<len; i++){ if(temp[i] != b1[i]){ incorrect++; printf("\nIncorrect: 0x%02x - 0x%02x fabric", temp[i], b1[i]); } } // tor_free(b1); // tor_free(b2); printf("%d bytes: %.2f nsec per byte\n", len, NANOCOUNT(start, end, iters*len)); printf("Num incorrect: %i\n", incorrect); //printf("start: %lu, end: %lu\n", start, end); } memmgr_free(b1); memmgr_free(b2); memmgr_free(temp); crypto_cipher_free(c); fpga_aes_free(cipher1); }
static void bench_onion_TAP(void) { const int iters = 1<<9; int i; crypto_pk_t *key, *key2; uint64_t start, end; memmgr_destroy(); memmgr_init_check_shared_mem(SHARED_SIZE, UIO_DEVICE, BASE_ADDRESS); // char os[TAP_ONIONSKIN_CHALLENGE_LEN]; char *os = memmgr_alloc(TAP_ONIONSKIN_CHALLENGE_LEN); char or[TAP_ONIONSKIN_REPLY_LEN]; crypto_dh_t *dh_out; key = crypto_pk_new(); key2 = crypto_pk_new(); if (crypto_pk_generate_key_with_bits(key, 1024) < 0) goto done; if (crypto_pk_generate_key_with_bits(key2, 1024) < 0) goto done; reset_perftime(); start = perftime(); for (i = 0; i < iters; ++i) { onion_skin_TAP_create(key, &dh_out, os); crypto_dh_free(dh_out); } end = perftime(); printf("Client-side, part 1: %f usec.\n", NANOCOUNT(start, end, iters)/1e3); onion_skin_TAP_create(key, &dh_out, os); start = perftime(); for (i = 0; i < iters; ++i) { char key_out[CPATH_KEY_MATERIAL_LEN]; onion_skin_TAP_server_handshake(os, key, NULL, or, key_out, sizeof(key_out)); } end = perftime(); printf("Server-side, key guessed right: %f usec\n", NANOCOUNT(start, end, iters)/1e3); start = perftime(); for (i = 0; i < iters; ++i) { char key_out[CPATH_KEY_MATERIAL_LEN]; onion_skin_TAP_server_handshake(os, key2, key, or, key_out, sizeof(key_out)); } end = perftime(); printf("Server-side, key guessed wrong: %f usec.\n", NANOCOUNT(start, end, iters)/1e3); start = perftime(); for (i = 0; i < iters; ++i) { crypto_dh_t *dh; char key_out[CPATH_KEY_MATERIAL_LEN]; int s; dh = crypto_dh_dup(dh_out); s = onion_skin_TAP_client_handshake(dh, or, key_out, sizeof(key_out), NULL); crypto_dh_free(dh); tor_assert(s == 0); } end = perftime(); printf("Client-side, part 2: %f usec.\n", NANOCOUNT(start, end, iters)/1e3); done: crypto_pk_free(key); crypto_pk_free(key2); memmgr_free(os); }
/** Test utility function: checks that the <b>plaintext_len</b>-byte string at * <b>plaintext</b> is at least superficially parseable. */ static void do_parse_test(uint8_t *plaintext, size_t plaintext_len, int phase) { crypto_pk_t *k = NULL; ssize_t r; uint8_t *cell = NULL; size_t cell_len; rend_intro_cell_t *parsed_req = NULL; char *err_msg = NULL; char digest[DIGEST_LEN]; /* Get a key */ k = crypto_pk_new(); tt_assert(k); r = crypto_pk_read_private_key_from_string(k, AUTHORITY_SIGNKEY_1, -1); tt_assert(!r); /* Get digest for future comparison */ r = crypto_pk_get_digest(k, digest); tt_assert(r >= 0); /* Make a cell out of it */ memmgr_init_check_shared_mem(SHARED_SIZE, UIO_DEVICE, BASE_ADDRESS); char* plaintext_buf = memmgr_alloc(plaintext_len); memcpy(plaintext_buf, plaintext, plaintext_len); r = make_intro_from_plaintext( plaintext_buf, plaintext_len, k, (void **)(&cell)); tt_assert(r > 0); tt_assert(cell); cell_len = r; memmgr_free(plaintext_buf); /* Do early parsing */ parsed_req = rend_service_begin_parse_intro(cell, cell_len, 2, &err_msg); tt_assert(parsed_req); tt_assert(!err_msg); tt_mem_op(parsed_req->pk,OP_EQ, digest, DIGEST_LEN); tt_assert(parsed_req->ciphertext); tt_assert(parsed_req->ciphertext_len > 0); if (phase == EARLY_PARSE_ONLY) goto done; printf("\nHere"); /* Do decryption */ r = rend_service_decrypt_intro(parsed_req, k, &err_msg); tt_assert(!r); tt_assert(!err_msg); tt_assert(parsed_req->plaintext); tt_assert(parsed_req->plaintext_len > 0); if (phase == DECRYPT_ONLY) goto done; /* Do late parsing */ r = rend_service_parse_intro_plaintext(parsed_req, &err_msg); tt_assert(!r); tt_assert(!err_msg); tt_assert(parsed_req->parsed); done: // tor_free(cell); memmgr_assert(cell); memmgr_free(cell); crypto_pk_free(k); rend_service_free_intro(parsed_req); tor_free(err_msg); }
// A rudimentary test of the memory manager. // Runs assuming default flags in memmgr.h: // // #define POOL_SIZE 8 * 1024 // #define MIN_POOL_ALLOC_QUANTAS 16 // // And a 32-bit machine (sizeof(unsigned long) == 4) // void test_memmgr() { if (sizeof(void*) != 4) { printf("WARNING: this test was designed for systems with pointer size = 4\n"); } byte *p[30] = {0}; int i; // Each header uses 8 bytes, so this allocates // 3 * (2048 + 8) = 6168 bytes, leaving us // with 8192 - 6168 = 2024 // for (i = 0; i < 3; ++i) { p[i] = memmgr_alloc(2048); assert(p[i]); } // Allocate all the remaining memory // p[4] = memmgr_alloc(2016); assert(p[4]); // Nothing left... // p[5] = memmgr_alloc(1); assert(p[5] == 0); // Release the second block. This frees 2048 + 8 bytes. // memmgr_free(p[1]); p[1] = 0; // Now we can allocate several smaller chunks from the // free list. There, they can be smaller than the // minimal allocation size. // Allocations of 100 require 14 quantas (13 for the // requested space, 1 for the header). So it allocates // 112 bytes. We have 18 allocations to make: // for (i = 10; i < 28; ++i) { p[i] = memmgr_alloc(100); assert(p[i]); } // Not enough for another one... // p[28] = memmgr_alloc(100); assert(p[28] == 0); // Now free everything // for (i = 0; i < 30; ++i) { if (p[i]) memmgr_free(p[i]); } memmgr_print_stats(); }
int main(int argc, char** argv){ int i, j; ERR_load_crypto_strings(); OpenSSL_add_all_algorithms(); OPENSSL_config(NULL); unsigned char data_to_encrypt[] = {0x01, 0x4B, 0xAF, 0x22, 0x78, 0xA6, 0x9D, 0x33, 0x1D, 0x51, 0x80, 0x10, 0x36, 0x43, 0xE9, 0x9A, '\0'}; unsigned char key[] = {0xE8, 0xE9, 0xEA, 0xEB, 0xED, 0xEE, 0xEF, 0xF0, 0xF2, 0xF3, 0xF4, 0xF5, 0xF7, 0xF8, 0xF9, 0xFA, '\0'}; unsigned char key1[] = {0xE9, 0xE9, 0xEA, 0xEB, 0xED, 0xEE, 0xEF, 0xF0, 0xF2, 0xF3, 0xF4, 0xF5, 0xF7, 0xF8, 0xF9, 0xFA, '\0'}; unsigned char key2[] = {0xEA, 0xE9, 0xEA, 0xEB, 0xED, 0xEE, 0xEF, 0xF0, 0xF2, 0xF3, 0xF4, 0xF5, 0xF7, 0xF8, 0xF9, 0xFA, '\0'}; unsigned char iv[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '\0'}; unsigned char data_to_encrypt2[] = {0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, '\0'}; int data_length; if(argc == 2){ data_length = atoi(argv[1]); } else{ data_length = 100; } printf("Data length: %i\n", data_length); unsigned char data_to_encrypt3[16*data_length]; memset(data_to_encrypt3, 0, 16*data_length); unsigned char encrypted_data_openssl[16*data_length*2]; memset(encrypted_data_openssl, 0, 16*data_length); // unsigned char encrypted_data_fabric[16*data_length]; // memset(encrypted_data_fabric, 0, 16*data_length); memmgr_init_shared_short(); printf("\n"); unsigned char *fpga_src = memmgr_alloc(16*data_length); unsigned char *fpga_dest = memmgr_alloc(16*data_length); srand (time(NULL)); int randStart = rand(); for(i=0; i<data_length; i++){ for(j=0; j<16; j++){ data_to_encrypt3[16*i + j] = i*j+j;//+randStart; } } unsigned char* data_pointer = data_to_encrypt3; unsigned char* encrypted_dest = encrypted_data_openssl; // memset(encrypted_dest, 16*data_length, 0); clock_t begin, end; begin = clock(); encrypt(data_pointer, 16*data_length, key, iv, encrypted_dest); encrypt(encrypted_dest, 16*data_length, key1, iv, encrypted_dest); encrypt(encrypted_dest, 16*data_length, key2, iv, encrypted_dest); end = clock(); double ticks = (double)(end - begin); double seconds =(double)(end - begin)/CLOCKS_PER_SEC; printf ("It took %f clicks (%f seconds) in openssl.\n",ticks,seconds); // FPGA_AES *cipher = fpga_aes_new(key, 16, 0x1f410000, "qam", "axi-reset"); data_pointer = data_to_encrypt3; // encrypted_dest = encrypted_data_fabric; begin = clock(); // Aes_encrypt_cbc_memcpy(cipher, iv, encrypted_dest, data_pointer, 16*data_length); triple_encrypt_fpga_ecb(fpga_src, 16*data_length, key, key1, key2, fpga_dest); end = clock(); ticks = (double)(end - begin); seconds =(double)(end - begin)/CLOCKS_PER_SEC; printf ("\nIt took %f clicks (%f seconds) in fabric.\n",ticks,seconds); printf("\nChecking"); ticks = clock(); char bin_buffer[33]; bin_buffer[32] = '\0'; int incorrectCount = 0; int k = 0; for(i=0; i<1000000; i++){ k+=5; } printf("\nk is: %i", k); if(data_length > 5){ for(i=0; i<data_length; i++){ for(j=0; j<16; j++){ char openssl = encrypted_data_openssl[i*16 + j]; int2bin(openssl, bin_buffer, 32); char fabric = fpga_dest[i*16+j]; if(openssl != fabric){ printf("\nChar at index %i is not encrypted correctly. It is %02x in openssl, %02x in fabric", i*16+j, openssl, fabric); incorrectCount++; } } } ticks = clock() - ticks; printf ("\nIt took %f clicks (%f seconds) to check.\n",(float)ticks,((float)ticks)/CLOCKS_PER_SEC); } else{ printf("\nInputs:\n"); for(i=0; i<data_length; i++){ printf(" 0x"); for(j=0; j<16; j++){ printf("%02x", data_to_encrypt3[i*16+j]); } } printf("\nOpenSSL results:\n"); for(i=0; i<data_length; i++){ printf(" 0x"); for(j=0; j<16; j++){ printf("%02x", encrypted_data_openssl[i*16+j]); } } printf("\nFabric results:\n"); for(i=0; i<data_length; i++){ printf(" 0x"); for(j=0; j<16; j++){ printf("%02x", fpga_dest[i*16+j]); } } } printf("\n"); EVP_cleanup(); ERR_free_strings(); memmgr_free(fpga_src); memmgr_free(fpga_dest); return 0; }