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,127 +47,124 @@ 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);
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);
else {
sgx_status_t status;
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)
{
uint64_t nptr;
sgx_status_t status;
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) ) {
return gmp_realloc_func(ptr, osize, nsize);
}
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
* then something truly terrible has happened. In theory, we could
* free() and try again, but would you trust the OS at this point?
*/
/*
* If the entire range of allocated memory is not outside the enclave
* then something truly terrible has happened. In theory, we could
* 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)
{
mpz_t a, b, c;
void e_mpz_add(mpz_t *c_un, mpz_t *a_un, mpz_t *b_un) {
mpz_t a, b, c;
/*
* Marshal untrusted values into the enclave so we don't accidentally
* leak secrets to untrusted memory.
*
* This is overkill for the trivial example in this function, but
* it's best to develop good coding habits.
*/
/*
* Marshal untrusted values into the enclave so we don't accidentally
* leak secrets to untrusted memory.
*
* This is overkill for the trivial example in this function, but
* it's best to develop good coding habits.
*/
mpz_inits(a, b, c, NULL);
mpz_inits(a, b, c, NULL);
mpz_set(a, *a_un);
mpz_set(b, *b_un);
mpz_set(a, *a_un);
mpz_set(b, *b_un);
mpz_add(c, a, b);
mpz_add(c, a, b);
/* Marshal our result out of the enclave */
/* Marshal our result out of the enclave */
mpz_set(*c_un, c);
mpz_set(*c_un, c);
}
void e_mpz_mul(mpz_t *c_un, mpz_t *a_un, mpz_t *b_un)
{
mpz_t a, b, c;
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. */
/* Marshal untrusted values into the enclave. */
mpz_inits(a, b, c, NULL);
mpz_inits(a, b, c, NULL);
mpz_set(a, *a_un);
mpz_set(b, *b_un);
mpz_set(a, *a_un);
mpz_set(b, *b_un);
mpz_mul(c, a, b);
mpz_mul(c, a, b);
/* Marshal our result out of the enclave. */
/* Marshal our result out of the enclave. */
mpz_set(*c_un, c);
mpz_set(*c_un, c);
}
void e_mpz_div(mpz_t *c_un, mpz_t *a_un, mpz_t *b_un)
{
mpz_t a, b, c;
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 */
/* Marshal untrusted values into the enclave */
mpz_inits(a, b, c, NULL);
mpz_inits(a, b, c, NULL);
mpz_set(a, *a_un);
mpz_set(b, *b_un);
mpz_set(a, *a_un);
mpz_set(b, *b_un);
mpz_div(c, a, b);
mpz_div(c, a, b);
/* Marshal our result out of the enclave */
/* Marshal our result out of the enclave */
mpz_set(*c_un, c);
mpz_set(*c_un, c);
}
void e_mpf_div(mpf_t *c_un, mpf_t *a_un, mpf_t *b_un)
{
mpf_t a, b, c;
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 */
/* Marshal untrusted values into the enclave */
mpf_inits(a, b, c, NULL);
mpf_inits(a, b, c, NULL);
mpf_set(a, *a_un);
mpf_set(b, *b_un);
mpf_set(a, *a_un);
mpf_set(b, *b_un);
mpf_div(c, a, b);
mpf_div(c, a, b);
/* Marshal our result out of the enclave */
/* Marshal our result out of the enclave */
mpf_set(*c_un, c);
mpf_set(*c_un, c);
}
/* Use the Chudnovsky equation to rapidly estimate pi */
......@@ -175,129 +172,122 @@ 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);
import_key(key);
mpf_t pi;
mpf_t pi;
/*
* Perform our operations on a variable that's located in the enclave,
* then marshal the final value out of the enclave.
*/
/*
* Perform our operations on a variable that's located in the enclave,
* then marshal the final value out of the enclave.
*/
mpf_init(pi);
mpf_init(pi);
e_calc_pi(&pi, digits);
e_calc_pi(&pi, digits);
/* Marshal our result to untrusted memory */
/* Marshal our result to untrusted memory */
mpf_set_prec(*pi_un, mpf_get_prec(pi));
mpf_set(*pi_un, pi);
mpf_set_prec(*pi_un, mpf_get_prec(pi));
mpf_set(*pi_un, pi);
}
void e_calc_pi (mpf_t *pi, uint64_t digits)
{
uint64_t k, n;
mp_bitcnt_t precision;
static double bits= log2(10);
mpz_t kf, kf3, threekf, sixkf, z1, z2, c4k, c5_3k;
mpf_t C, sum, div, f2;
void e_calc_pi(mpf_t *pi, uint64_t digits) {
uint64_t k, n;
mp_bitcnt_t precision;
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;
mpf_set_default_prec(precision);
n= (digits/DIGITS_PER_ITERATION)+1;
precision= (digits * bits)+1;
/* Re-initialize the pi variable to use our new precision */
mpf_set_default_prec(precision);
mpf_set_prec(*pi, precision);
/* Re-initialize the pi variable to use our new precision */
char buf[32];
if (sgx_read_rand(buf, 32) != SGX_SUCCESS)
return;
mpf_set_prec(*pi, precision);
uint32_t sealedLen = sgx_calc_sealed_data_size(0, 32);
char buf[32];
if (sgx_read_rand(buf, 32) != SGX_SUCCESS)
return;
uint8_t sealed_data[sealedLen];
if (sgx_seal_data(0, NULL, 32, buf, sealedLen, sealed_data) != SGX_SUCCESS)
return;
uint32_t sealedLen = sgx_calc_sealed_data_size(0, 32);
/*
uint8_t sealed_data[sealedLen];
426880 sqrt(10005) inf (6k)! (13591409+545140134k)
------------------- = SUM ---------------------------
pi k=0 (3k)!(k!)^3(-640320)^3k
if( sgx_seal_data(0, NULL, 32, buf, sealedLen, sealed_data) != SGX_SUCCESS)
return;
C / pi = SUM (6k)! * (c3 + c4*k) / (3k)!(k!)^3(c5)^3k
/*
C / pi = SUM f1 / f2
426880 sqrt(10005) inf (6k)! (13591409+545140134k)
------------------- = SUM ---------------------------
pi k=0 (3k)!(k!)^3(-640320)^3k
pi = C / sum
C / pi = SUM (6k)! * (c3 + c4*k) / (3k)!(k!)^3(c5)^3k
*/
C / pi = SUM f1 / f2
mpz_inits(sixkf, z1, z2, kf, kf3, threekf, c4k, c5_3k, NULL);
mpf_inits(C, sum, div, f2, NULL);
pi = C / sum
/* Calculate 'C' */
*/
mpf_sqrt_ui(C, 10005);
mpf_mul_ui(C, C, 426880);
mpz_inits(sixkf, z1, z2, kf, kf3, threekf, c4k, c5_3k, NULL);
mpf_inits(C, sum, div, f2, NULL);
if (!pi_init) {
/* Constants needed in 'sum'. */
/* Calculate 'C' */
mpz_inits(c3, c4, c5, NULL);
mpf_sqrt_ui(C, 10005);
mpf_mul_ui(C, C, 426880);
mpz_set_ui(c3, 13591409);
mpz_set_ui(c4, 545140134);
mpz_set_si(c5, -640320);
if ( ! pi_init ) {
/* Constants needed in 'sum'. */
pi_init = 1;
}
mpz_inits(c3, c4, c5, NULL);
mpf_set_ui(sum, 0);
mpz_set_ui(c3, 13591409);
mpz_set_ui(c4, 545140134);
mpz_set_si(c5, -640320);
for (k = 0; k < n; ++k) {
/* Numerator */
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);
pi_init= 1;
}
/* Denominator */
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_mul(z2, z2, c5_3k);
/* Divison */
mpf_set_ui(sum, 0);
mpf_set_z(f2, z2);
mpf_div(div, div, f2);
for (k= 0; k< n; ++k) {
/* Numerator */
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);
/* Sum */
/* Denominator */
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_mul(z2, z2, c5_3k);
mpf_add(sum, sum, div);
}
/* Divison */
mpf_div(*pi, C, sum);
mpf_set_z(f2, z2);
mpf_div(div, div, f2);
/* Sum */
mpf_add(sum, sum, div);
}
mpf_div(*pi, C, sum);
mpf_clears(div, sum, f2, NULL);
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