Added gmp

parent d4d28bdf
......@@ -32,10 +32,10 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "secure_enclave_t.h"
#include <sgx_tgmp.h>
#include <sgx_trts.h>
#include "sgx_tcrypto.h"
#include "sgx_tseal.h"
#include <sgx_tgmp.h>
#include <sgx_trts.h>
#include <math.h>
......@@ -47,39 +47,39 @@ void (*oc_free_func)(void *, size_t);
void *reallocate_function(void *, size_t, size_t);
void free_function(void *, size_t);
void e_calc_pi (mpf_t *pi, uint64_t digits);
void e_calc_pi(mpf_t *pi, uint64_t digits);
void tgmp_init()
{
oc_realloc_func= &reallocate_function;
oc_free_func= &free_function;
void tgmp_init() {
oc_realloc_func = &reallocate_function;
oc_free_func = &free_function;
mp_get_memory_functions(NULL, &gmp_realloc_func, &gmp_free_func);
mp_set_memory_functions(NULL, oc_realloc_func, oc_free_func);
}
void free_function (void *ptr, size_t sz)
{
if ( sgx_is_within_enclave(ptr, sz) ) gmp_free_func(ptr, sz);
void free_function(void *ptr, size_t sz) {
if (sgx_is_within_enclave(ptr, sz))
gmp_free_func(ptr, sz);
else {
sgx_status_t status;
status= oc_free(ptr, sz);
if ( status != SGX_SUCCESS ) abort();
status = oc_free(ptr, sz);
if (status != SGX_SUCCESS)
abort();
}
}
void *reallocate_function (void *ptr, size_t osize, size_t nsize)
{
void *reallocate_function(void *ptr, size_t osize, size_t nsize) {
uint64_t nptr;
sgx_status_t status;
if ( sgx_is_within_enclave(ptr, osize) ) {
if (sgx_is_within_enclave(ptr, osize)) {
return gmp_realloc_func(ptr, osize, nsize);
}
status= oc_realloc(&nptr, ptr, osize, nsize);
if ( status != SGX_SUCCESS ) abort();
status = oc_realloc(&nptr, ptr, osize, nsize);
if (status != SGX_SUCCESS)
abort();
/*
* If the entire range of allocated memory is not outside the enclave
......@@ -87,13 +87,13 @@ void *reallocate_function (void *ptr, size_t osize, size_t nsize)
* free() and try again, but would you trust the OS at this point?
*/
if ( ! sgx_is_outside_enclave((void *) ptr, nsize) ) abort();
if (!sgx_is_outside_enclave((void *)ptr, nsize))
abort();
return (void *) nptr;
return (void *)nptr;
}
void e_mpz_add(mpz_t *c_un, mpz_t *a_un, mpz_t *b_un)
{
void e_mpz_add(mpz_t *c_un, mpz_t *a_un, mpz_t *b_un) {
mpz_t a, b, c;
/*
......@@ -116,8 +116,7 @@ void e_mpz_add(mpz_t *c_un, mpz_t *a_un, mpz_t *b_un)
mpz_set(*c_un, c);
}
void e_mpz_mul(mpz_t *c_un, mpz_t *a_un, mpz_t *b_un)
{
void e_mpz_mul(mpz_t *c_un, mpz_t *a_un, mpz_t *b_un) {
mpz_t a, b, c;
/* Marshal untrusted values into the enclave. */
......@@ -134,8 +133,7 @@ void e_mpz_mul(mpz_t *c_un, mpz_t *a_un, mpz_t *b_un)
mpz_set(*c_un, c);
}
void e_mpz_div(mpz_t *c_un, mpz_t *a_un, mpz_t *b_un)
{
void e_mpz_div(mpz_t *c_un, mpz_t *a_un, mpz_t *b_un) {
mpz_t a, b, c;
/* Marshal untrusted values into the enclave */
......@@ -152,8 +150,7 @@ void e_mpz_div(mpz_t *c_un, mpz_t *a_un, mpz_t *b_un)
mpz_set(*c_un, c);
}
void e_mpf_div(mpf_t *c_un, mpf_t *a_un, mpf_t *b_un)
{
void e_mpf_div(mpf_t *c_un, mpf_t *a_un, mpf_t *b_un) {
mpf_t a, b, c;
/* Marshal untrusted values into the enclave */
......@@ -175,12 +172,12 @@ void e_mpf_div(mpf_t *c_un, mpf_t *a_un, mpf_t *b_un)
#define DIGITS_PER_ITERATION 14.1816 /* Roughly */
mpz_t c3, c4, c5;
int pi_init= 0;
void encrypt_key (mpf_t *pi_un, uint64_t digits, char key[100])
{
int pi_init = 0;
void encrypt_key(mpf_t *pi_un, uint64_t digits, char key[100]) {
if (strnlen(key) == 100)
return;
import_key(key);
......@@ -201,18 +198,15 @@ void encrypt_key (mpf_t *pi_un, uint64_t digits, char key[100])
mpf_set(*pi_un, pi);
}
void e_calc_pi (mpf_t *pi, uint64_t digits)
{
void e_calc_pi(mpf_t *pi, uint64_t digits) {
uint64_t k, n;
mp_bitcnt_t precision;
static double bits= log2(10);
static double bits = log2(10);
mpz_t kf, kf3, threekf, sixkf, z1, z2, c4k, c5_3k;
mpf_t C, sum, div, f2;
n= (digits/DIGITS_PER_ITERATION)+1;
precision= (digits * bits)+1;
n = (digits / DIGITS_PER_ITERATION) + 1;
precision = (digits * bits) + 1;
mpf_set_default_prec(precision);
......@@ -224,15 +218,13 @@ void e_calc_pi (mpf_t *pi, uint64_t digits)
if (sgx_read_rand(buf, 32) != SGX_SUCCESS)
return;
uint32_t sealedLen = sgx_calc_sealed_data_size(0, 32);
uint8_t sealed_data[sealedLen];
if( sgx_seal_data(0, NULL, 32, buf, sealedLen, sealed_data) != SGX_SUCCESS)
if (sgx_seal_data(0, NULL, 32, buf, sealedLen, sealed_data) != SGX_SUCCESS)
return;
/*
426880 sqrt(10005) inf (6k)! (13591409+545140134k)
......@@ -255,7 +247,7 @@ void e_calc_pi (mpf_t *pi, uint64_t digits)
mpf_sqrt_ui(C, 10005);
mpf_mul_ui(C, C, 426880);
if ( ! pi_init ) {
if (!pi_init) {
/* Constants needed in 'sum'. */
mpz_inits(c3, c4, c5, NULL);
......@@ -264,26 +256,25 @@ void e_calc_pi (mpf_t *pi, uint64_t digits)
mpz_set_ui(c4, 545140134);
mpz_set_si(c5, -640320);
pi_init= 1;
pi_init = 1;
}
mpf_set_ui(sum, 0);
for (k= 0; k< n; ++k) {
for (k = 0; k < n; ++k) {
/* Numerator */
mpz_fac_ui(sixkf, 6*k);
mpz_fac_ui(sixkf, 6 * k);
mpz_mul_ui(c4k, c4, k);
mpz_add(c4k, c4k, c3);
mpz_mul(z1, c4k, sixkf);
mpf_set_z(div, z1);
/* Denominator */
mpz_fac_ui(threekf, 3*k);
mpz_fac_ui(threekf, 3 * k);
mpz_fac_ui(kf, k);
mpz_pow_ui(kf3, kf, 3);
mpz_mul(z2, threekf, kf3);
mpz_pow_ui(c5_3k, c5, 3*k);
mpz_pow_ui(c5_3k, c5, 3 * k);
mpz_mul(z2, z2, c5_3k);
/* Divison */
......@@ -300,4 +291,3 @@ void e_calc_pi (mpf_t *pi, uint64_t digits)
mpf_clears(div, sum, f2, NULL);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment