RA_code.c 3.32 KB
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;
}