static ssize_t make_intro_from_plaintext( void *buf, size_t len, crypto_pk_t *key, void **cell_out) { char *cell = NULL; ssize_t cell_len = -1, r; /* Assemble key digest and ciphertext, then construct the cell */ ssize_t ciphertext_size; if (!(buf && key && len > 0 && cell_out)) goto done; /* * Figure out an upper bound on how big the ciphertext will be * (see crypto_pk_public_hybrid_encrypt()) */ ciphertext_size = PKCS1_OAEP_PADDING_OVERHEAD; ciphertext_size += crypto_pk_keysize(key); ciphertext_size += CIPHER_KEY_LEN; ciphertext_size += len; /* * Allocate space for the cell */ memmgr_init_check_shared_mem(SHARED_SIZE, UIO_DEVICE, BASE_ADDRESS); // cell = tor_malloc(DIGEST_LEN + ciphertext_size); cell = memmgr_alloc(DIGEST_LEN + ciphertext_size); memmgr_assert(buf); /* Compute key digest (will be first DIGEST_LEN octets of cell) */ r = crypto_pk_get_digest(key, cell); tt_assert(r >= 0); /* Do encryption */ r = crypto_pk_public_hybrid_encrypt( key, cell + DIGEST_LEN, ciphertext_size, buf, len, PK_PKCS1_OAEP_PADDING, 0); tt_assert(r >= 0); /* Figure out cell length */ cell_len = DIGEST_LEN + r; /* Output the cell */ *cell_out = cell; done: // memmgr_free(cell); return cell_len; }
void *malloc(size_t size) { ldwrapper_init(); /* sem_wait(&ma_sem); */ void *area = memmgr_alloc(mm, size, 0); /* sem_post(&ma_sem); */ fprintf(stderr, "alloc(%u) = %p\n", size, area); return area; }
void *memalign(size_t boundary, size_t size) { ldwrapper_init(); /* sem_wait(&ma_sem); */ void *area = memmgr_alloc(mm, size, boundary); /* sem_post(&ma_sem); */ fprintf(stderr, "memalign(%u, %u) = %p\n", boundary, size, area); return area; }
int ethmac_init() { int i; struct dev_node *dev_p = 0; if (!ethmac_devl->devices) return -1; // check for buffer ram devices if (!mem_devl->devices) return -1; // alloc structures for all devices ethmac = (ethmac_t **) memmgr_alloc(sizeof(ethmac_t *)*ethmac_devl->size); ethmac_bd = (ethmac_bd_t **) memmgr_alloc(sizeof(ethmac_bd_t *)*ethmac_devl->size); ethmac_buf = (ethmac_buf_t **) memmgr_alloc(sizeof(ethmac_buf_t *)*ethmac_devl->size); for (i = 0, dev_p = ethmac_devl->devices; i < ethmac_devl->size; ++i, dev_p = dev_p->next) { ethmac[i] = (ethmac_t *) dev_p->base; ethmac_bd[i] = (ethmac_bd_t *) (dev_p->base + OETH_BD_BASE_OFFS); DBE_DEBUG(DBG_ETH | DBE_DBG_INFO, "> ethmac addr[%d]: %08X\n", i, ethmac[i]); DBE_DEBUG(DBG_ETH | DBE_DBG_INFO, "> ethmac_bd addr[%d]: %08X\n", i, ethmac_bd[i]); } DBE_DEBUG(DBG_ETH | DBE_DBG_INFO, "> ethmac size: %d\n", ethmac_devl->size); // ethmac buffer must be the last device on the list for (i = 0, dev_p = mem_devl->devices; i < mem_devl->size-1; ++i, dev_p = dev_p->next); ethmac_buf[OETH_BUF_ID] = (ethmac_buf_t *) dev_p->base; DBE_DEBUG(DBG_ETH | DBE_DBG_INFO, "> ethmac_buf[%d]: %08X\n", OETH_BUF_ID, ethmac_buf[OETH_BUF_ID]); return 0; }
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 * MemMgrAllocatorInit(size_t prealloc_size, size_t worker_size, size_t num_workers, unsigned char alignment) { assert(alignment <= sizeof(mem_header_union::Align)); memmgr_init(prealloc_size, worker_size, num_workers, 256); return memmgr_alloc(1); }
void *MemMgrAllocatorMalloc(void *opaque, size_t nmemb, size_t size) { return memmgr_alloc(nmemb * size); }
/** 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; }