RA_code.c
3.32 KB
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
100
void main (void) {
char test_in [] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int test_in_len = strlen (test_in);
char test_decrypted [512];
int test_decrypted_len;
TEEC_Result rc;
TEEC_Context ctx;
TEEC_Session sess;
TEEC_Operation op;
TEEC_SharedMemory field_in;
TEEC_SharedMemory field_back;
TEEC_SharedMemory dummy;
TEEC_UUID uuid = FIM_TA_UUID;
uint32_t err_origin;
rc = TEEC_InitializeContext(NULL, &ctx);
rc = TEEC_OpenSession(&ctx, &sess, &uuid, TEEC_LOGIN_PUBLIC, NULL, NULL, &err_origin);
check_rc(rc, "TEEC_OpenSession", &err_origin);
field_in.buffer = NULL;
field_in.size = 256;
field_in.flags = TEEC_MEM_INPUT;
rc = TEEC_AllocateSharedMemory(&ctx, &field_in);
check_rc(rc, "TEEC_AllocateSharedMemory for field_in", NULL);
field_back.buffer = NULL;
field_back.size = 256;
field_back.flags = TEEC_MEM_OUTPUT;
rc = TEEC_AllocateSharedMemory(&ctx, &field_back);
check_rc(rc, "TEEC_AllocateSharedMemory for field_back", NULL);
dummy.buffer = NULL;
dummy.size = 1;
dummy.flags = TEEC_MEM_INPUT;
rc = TEEC_AllocateSharedMemory(&ctx, &dummy);
check_rc(rc, "TEEC_AllocateSharedMemory for dummy parameter", NULL);
/* Clear the TEEC_Operation struct */
memset(&op, 0, sizeof(op));
op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_WHOLE, TEEC_MEMREF_WHOLE,
TEEC_MEMREF_WHOLE, TEEC_VALUE_OUTPUT);
op.params[0].memref.parent = &field_in;
op.params[1].memref.parent = &field_back;
op.params[2].memref.parent = &dummy;
op.params[3].value.a = 0;
memcpy(field_in.buffer, test_in, test_in_len);
field_in.size = test_in_len;
rc = TEEC_InvokeCommand(&sess, TEST_ENCRYPT_IN_TA, &op, &err_origin);
decrypt_using_public_key (CA_public_key_copy, (char *)field_back.buffer, field_back.size, test_decrypted, &test_decrypted_len);
printf ("In string: %s\n", test_in);
printf ("Test in len: %i\n", test_in_len);
printf ("Encryted value: %s\n", (char *) field_back.buffer);
printf ("Encryted len: %i\n", (int) field_back.size);
printf ("Decrypted value: %s\n", test_decrypted);
printf ("Decrypted len: %i\n", test_decrypted_len);
}
char CA_public_key_copy [] =
"-----BEGIN PUBLIC KEY-----\n"
"MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAL5c51/v1osjr5+lRPykmpQKyGdXMG0g\n"
"S6Du1l8Hm0qYXc+azq6qqZvr39zeufw/VLKTfeKeKVJX1D28TImn6cUCAwEAAQ==\n"
"-----END PUBLIC KEY-----\n";
BOOLEAN decrypt_using_public_key (char * public_key, char * in, int in_len, char * out, int * out_len) {
RSA * rsa = createRSA ((unsigned char *) public_key, 1);
*out_len = RSA_public_decrypt (in_len, (unsigned char *)in, (unsigned char *) out, rsa, RSA_PKCS1_PADDING);
if (*out_len == -1)
return FALSE;
else
return TRUE;
}
RSA *createRSA(unsigned char *key, int public) {
RSA *rsa = NULL;
BIO *keybio;
keybio = BIO_new_mem_buf(key, -1);
if (keybio == NULL) {
printf("Failed to create key BIO");
return 0;
}
if (public) {
rsa = PEM_read_bio_RSA_PUBKEY(keybio, &rsa, NULL, NULL);
} else {
rsa = PEM_read_bio_RSAPrivateKey(keybio, &rsa, NULL, NULL);
}
if (rsa == NULL) {
printf("Failed to create RSA");
}
return rsa;
}