TA_code.c 3.46 KB
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;
}