ToLive

캡스톤디자인2

No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
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;
}
\ No newline at end of file
This diff is collapsed. Click to expand it.
static TEE_Result test_encrypt_ta(TEE_Param params[4])
{
char *in = (char *)params[TEST_STRING].memref.buffer;
int in_len = params[TEST_STRING].memref.size;
char encrypted[512];
int encrypted_len;
encrypt_using_private_key(in, in_len, encrypted, &encrypted_len);
memcpy(params[TEST_EN_DECRYPTED].memref.buffer, encrypted, encrypted_len);
params[TEST_EN_DECRYPTED].memref.size = encrypted_len;
params[TEST_RC_POS].value.a = ALL_OK;
return TEE_SUCCESS;
}
bool encrypt_using_private_key(char *in, int in_len, char *out, int *out_len)
{
TEE_Result ret = TEE_SUCCESS; // return code
TEE_ObjectHandle key = (TEE_ObjectHandle)NULL;
TEE_Attribute rsa_attrs[3];
void *to_encrypt = NULL;
uint32_t cipher_len = 256;
void *cipher = NULL;
rsa_attrs[0].attributeID = TEE_ATTR_RSA_MODULUS;
rsa_attrs[0].content.ref.buffer = CA_modulus;
rsa_attrs[0].content.ref.length = SIZE_OF_VEC(CA_modulus);
rsa_attrs[1].attributeID = TEE_ATTR_RSA_PUBLIC_EXPONENT;
rsa_attrs[1].content.ref.buffer = CA_public_key;
rsa_attrs[1].content.ref.length = SIZE_OF_VEC(CA_public_key);
rsa_attrs[2].attributeID = TEE_ATTR_RSA_PRIVATE_EXPONENT;
rsa_attrs[2].content.ref.buffer = CA_private_key;
rsa_attrs[2].content.ref.length = SIZE_OF_VEC(CA_private_key);
ret = TEE_AllocateTransientObject(TEE_TYPE_RSA_KEYPAIR, 1024, &key);
ret = TEE_PopulateTransientObject(key, (TEE_Attribute *)&rsa_attrs, 3);
to_encrypt = TEE_Malloc(in_len, 0);
cipher = TEE_Malloc(cipher_len, 0);
TEE_MemMove(to_encrypt, in, in_len - 1);
if (!perform_rsa_operation(key, TEE_MODE_ENCRYPT, to_encrypt, in_len, cipher, &cipher_len))
{
DMSG("Encrypt failed : 0x%x", ret);
return TEE_ERROR_BAD_PARAMETERS;
}
memcpy(out, cipher, cipher_len);
*out_len = cipher_len;
out[cipher_len] = '\0';
DMSG("Return: %s", out);
DMSG("Return lenght: %i", *out_len);
return TRUE;
}
#define SIZE_OF_VEC(vec) (sizeof(vec) - 1)
uint8_t CA_modulus[] =
"\xbe\x5c\xe7\x5f\xef\xd6\x8b\x23\xaf\x9f\xa5\x44\xfc\xa4\x9a"
"\x94\x0a\xc8\x67\x57\x30\x6d\x20\x4b\xa0\xee\xd6\x5f\x07\x9b"
"\x4a\x98\x5d\xcf\x9a\xce\xae\xaa\xa9\x9b\xeb\xdf\xdc\xde\xb9"
"\xfc\x3f\x54\xb2\x93\x7d\xe2\x9e\x29\x52\x57\xd4\x3d\xbc\x4c"
"\x89\xa7\xe9\xc5";
uint8_t CA_public_key[] =
"\x01\x00\x01";
uint8_t CA_private_key[] =
"\x48\x30\x89\x19\xcb\xa5\x2b\xac\xc3\xcc\x21\xeb\x90\x77\x87"
"\x9b\x3e\x9f\x92\xf8\xf0\x87\x61\xa8\xec\x85\xc6\x4b\xd1\x61"
"\xa5\x9e\x8b\xc7\xa1\x5a\x72\xf0\x04\xc8\x04\x5d\x5e\x52\x18"
"\x5c\xd4\x68\x82\x21\x17\xdd\xa1\xcc\x42\x87\xe5\x84\xe1\x58"
"\x20\xc2\x03\x7d";
B perform_rsa_operation(TEE_ObjectHandle key, TEE_OperationMode mode,
void *in_chunk, uint32_t in_chunk_len,
void *out_chunk, uint32_t *out_chunk_len)
{
TEE_ObjectInfo info;
TEE_OperationHandle handle = (TEE_OperationHandle)NULL;
TEE_Result ret = TEE_SUCCESS;
TEE_GetObjectInfo(key, &info);
ret = TEE_AllocateOperation(&handle, TEE_ALG_RSAES_PKCS1_V1_5, mode, info.maxObjectSize);
ret = TEE_SetOperationKey(handle, key);
if (mode == TEE_MODE_ENCRYPT)
{
DMSG("Encrypting values\n");
ret = TEE_AsymmetricEncrypt(handle, (TEE_Attribute *)NULL, 0, in_chunk, in_chunk_len, out_chunk, out_chunk_len);
}
if (mode == TEE_MODE_DECRYPT)
{
DMSG("Decrypting values\n");
ret = TEE_AsymmetricDecrypt(handle, (TEE_Attribute *)NULL, 0, in_chunk, in_chunk_len, out_chunk, out_chunk_len);
}
TEE_FreeOperation(handle);
return TRUE;
}
\ No newline at end of file