tSeal_internal.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (C) 2011-2016 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include "sgx_tseal.h"
  32. #include "tSeal_internal.h"
  33. #include "sgx_utils.h"
  34. #include <string.h>
  35. // sgx_seal_data_iv
  36. //
  37. // Parameters:
  38. // additional_MACtext_length - [IN] length of the plaintext data stream in bytes
  39. // p_additional_MACtext - [IN] pointer to the plaintext data stream to be GCM protected
  40. // text2encrypt_length - [IN] length of the data stream to encrypt in bytes
  41. // p_text2encrypt - [IN] pointer to data stream to encrypt
  42. // p_payload_iv - [IN] Pointer to Initialization Vector to be used during AES GCM encryption
  43. // p_key_request - [IN] Pointer to the key request structure to be utilized to obtain the SEAL key
  44. // p_sealed_data - [OUT] pointer to the sealed data structure containing protected data
  45. //
  46. // Return Value:
  47. // sgx_status_t - SGX Error code
  48. sgx_status_t sgx_seal_data_iv(const uint32_t additional_MACtext_length,
  49. const uint8_t *p_additional_MACtext, const uint32_t text2encrypt_length,
  50. const uint8_t *p_text2encrypt, const uint8_t *p_payload_iv,
  51. const sgx_key_request_t* p_key_request, sgx_sealed_data_t *p_sealed_data)
  52. {
  53. sgx_status_t err = SGX_ERROR_UNEXPECTED;
  54. // Parameter checking performed in sgx_seal_data
  55. // Get the seal key
  56. sgx_key_128bit_t seal_key;
  57. memset(&seal_key, 0, sizeof(sgx_key_128bit_t));
  58. err = sgx_get_key(p_key_request, &seal_key);
  59. if (err != SGX_SUCCESS)
  60. {
  61. // Clear temp state
  62. memset_s(&seal_key, sizeof(sgx_key_128bit_t), 0, sizeof(sgx_key_128bit_t));
  63. if (err != SGX_ERROR_OUT_OF_MEMORY)
  64. err = SGX_ERROR_UNEXPECTED;
  65. return err;
  66. }
  67. err = sgx_rijndael128GCM_encrypt(&seal_key, p_text2encrypt, text2encrypt_length,
  68. reinterpret_cast<uint8_t *>(&(p_sealed_data->aes_data.payload)), p_payload_iv,
  69. SGX_SEAL_IV_SIZE, p_additional_MACtext, additional_MACtext_length,
  70. &(p_sealed_data->aes_data.payload_tag));
  71. if (err == SGX_SUCCESS)
  72. {
  73. // Copy additional MAC text
  74. uint8_t* p_aad = NULL;
  75. if (additional_MACtext_length > 0)
  76. {
  77. p_aad = &(p_sealed_data->aes_data.payload[text2encrypt_length]);
  78. memcpy(p_aad, p_additional_MACtext, additional_MACtext_length);
  79. }
  80. // populate the plain_text_offset, payload_size in the data_blob
  81. p_sealed_data->plain_text_offset = text2encrypt_length;
  82. p_sealed_data->aes_data.payload_size = additional_MACtext_length + text2encrypt_length;
  83. }
  84. // Clear temp state
  85. memset_s(&seal_key, sizeof(sgx_key_128bit_t), 0, sizeof(sgx_key_128bit_t));
  86. return err;
  87. }
  88. // sgx_unseal_data_helper
  89. //
  90. // Parameters:
  91. // p_sealed_data - [IN] pointer to the sealed data structure containing protected data
  92. // p_additional_MACtext - [OUT] pointer to the plaintext data stream which was GCM protected
  93. // additional_MACtext_length - [IN] length of the plaintext data stream in bytes
  94. // p_decrypted_text - [OUT] pointer to decrypted data stream
  95. // decrypted_text_length - [IN] length of the decrypted data stream to encrypt in bytes
  96. //
  97. // Return Value:
  98. // sgx_status_t - SGX Error code
  99. sgx_status_t sgx_unseal_data_helper(const sgx_sealed_data_t *p_sealed_data, uint8_t *p_additional_MACtext,
  100. uint32_t additional_MACtext_length, uint8_t *p_decrypted_text, uint32_t decrypted_text_length)
  101. {
  102. sgx_status_t err = SGX_ERROR_UNEXPECTED;
  103. sgx_key_128bit_t seal_key;
  104. memset(&seal_key, 0, sizeof(sgx_key_128bit_t));
  105. uint8_t payload_iv[SGX_SEAL_IV_SIZE];
  106. memset(&payload_iv, 0, SGX_SEAL_IV_SIZE);
  107. if (decrypted_text_length > 0)
  108. memset(p_decrypted_text, 0, decrypted_text_length);
  109. if (additional_MACtext_length > 0)
  110. memset(p_additional_MACtext, 0, additional_MACtext_length);
  111. // Get the seal key
  112. err = sgx_get_key(&p_sealed_data->key_request, &seal_key);
  113. if (err != SGX_SUCCESS)
  114. {
  115. // Clear temp state
  116. memset_s(&seal_key, sizeof(sgx_key_128bit_t), 0, sizeof(sgx_key_128bit_t));
  117. // Provide only error codes that the calling code could act on
  118. if ((err == SGX_ERROR_INVALID_CPUSVN) || (err == SGX_ERROR_INVALID_ISVSVN) || (err == SGX_ERROR_OUT_OF_MEMORY))
  119. return err;
  120. // Return error indicating the blob is corrupted
  121. return SGX_ERROR_MAC_MISMATCH;
  122. }
  123. err = sgx_rijndael128GCM_decrypt(&seal_key, const_cast<uint8_t *>(p_sealed_data->aes_data.payload),
  124. decrypted_text_length, p_decrypted_text, &payload_iv[0], SGX_SEAL_IV_SIZE,
  125. const_cast<uint8_t *>(&(p_sealed_data->aes_data.payload[decrypted_text_length])), additional_MACtext_length,
  126. const_cast<sgx_aes_gcm_128bit_tag_t *>(&p_sealed_data->aes_data.payload_tag));
  127. if (err != SGX_SUCCESS)
  128. {
  129. // Clear temp state
  130. memset_s(&seal_key, sizeof(sgx_key_128bit_t), 0, sizeof(sgx_key_128bit_t));
  131. return err;
  132. }
  133. if (additional_MACtext_length > 0)
  134. {
  135. memcpy(p_additional_MACtext, &(p_sealed_data->aes_data.payload[decrypted_text_length]), additional_MACtext_length);
  136. }
  137. // Clear temp state
  138. memset_s(&seal_key, sizeof(sgx_key_128bit_t), 0, sizeof(sgx_key_128bit_t));
  139. return SGX_SUCCESS;
  140. }