-
Notifications
You must be signed in to change notification settings - Fork 4
/
enclave.c
99 lines (85 loc) · 2.08 KB
/
enclave.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "sgx_trts.h"
#include <stdarg.h>
#include <stdio.h> /* vsnprintf */
#include "enclave.h"
#include "enclave_t.h"
#include <string.h>
#include <ctype.h>
#include <sgx_tseal.h>
#include <sgx_utils.h>
#include <sgx_tcrypto.h>
#include <stdlib.h>
#include "sgx_tprotected_fs.h"
/*
* printf:
* Invokes OCALL to display the enclave buffer to the terminal.
*/
void printf(const char *fmt, ...)
{
char buf[BUFSIZ] = {'\0'};
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, BUFSIZ, fmt, ap);
va_end(ap);
ocall_print_string(buf);
}
void ecall_enclaveString(char *s, size_t len)
{
const char *secret = "Hello Enclave!";
if (len > strlen(secret))
{
memcpy(s, secret, strlen(secret) + 1);
}
else
{
memcpy(s, "false", strlen("false") + 1);
}
}
SGX_FILE* ecall_file_open(const char* filename, const char* mode)
{
SGX_FILE* a;
a = sgx_fopen_auto_key(filename, mode);
return a;
}
uint64_t ecall_file_get_file_size(SGX_FILE * fp)
{
uint64_t file_size = 0;
sgx_fseek(fp, 0, SEEK_END);
file_size = sgx_ftell(fp);
return file_size;
}
size_t ecall_file_write(SGX_FILE* fp, char data[100])
{
size_t sizeofWrite;
size_t len = strlen(data);
sizeofWrite = sgx_fwrite(data, sizeof(char), len, fp);
/*for (int i = 0; i < 5; i++)
{
char buffer[] = { 'x' , 'c' };
sizeofWrite += sgx_fwrite(buffer, sizeof(char), sizeof(buffer), fp);
}*/
return sizeofWrite;
}
size_t ecall_file_read(SGX_FILE* fp, char* readData, uint64_t size)
{
char *data;
uint64_t startN = 1;
sgx_fseek(fp, 0, SEEK_END);
uint64_t finalN = sgx_ftell(fp);
sgx_fseek(fp, 0, SEEK_SET);
printf("file size%d\n", finalN);
data = (char*)malloc(sizeof(char)*finalN);
memset(data, 0, sizeof(char)*finalN);
size_t sizeofRead = sgx_fread(data, startN, finalN, fp);
int len = strlen(data);
memcpy(readData, data, sizeofRead);
memset(readData+sizeofRead, '\0', 1);
printf("%s\n", readData);
return sizeofRead;
}
int32_t ecall_file_close(SGX_FILE* fp)
{
int32_t a;
a = sgx_fclose(fp);
return a;
}