pcl_tSeal_util.cpp 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (C) 2011-2018 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. /* Content from sdk/tseal/tSeal_util.cpp */
  32. #include <stdint.h>
  33. #include <stdlib.h>
  34. #include <sgx_tseal.h>
  35. #ifdef SE_SIM
  36. #include <deriv.h>
  37. #endif // #ifdef SE_SIM
  38. #include <pcl_common.h>
  39. #include <pcl_internal.h>
  40. #include <pcl_unseal_internal.h>
  41. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  42. // pcl_calc_sealed_data_size()
  43. // computes sealedDataSize = payload size + sizeof(sgx_sealed_data_t)
  44. // Parameters:
  45. // [IN] aad_mac_txt_size - optional additional data size
  46. // [IN] txt_encrypt_size - the actual encrypted data (payload)
  47. // Return Value:
  48. // sealedDataSize
  49. uint32_t pcl_calc_sealed_data_size(const uint32_t aad_mac_txt_size, const uint32_t txt_encrypt_size)
  50. {
  51. // code copied from tseal_util.cpp from the tseal library
  52. if (aad_mac_txt_size > UINT32_MAX - txt_encrypt_size)
  53. return UINT32_MAX;
  54. uint32_t payload_size = aad_mac_txt_size + txt_encrypt_size; //Calculate the payload size
  55. if (payload_size > UINT32_MAX - sizeof(sgx_sealed_data_t))
  56. return UINT32_MAX;
  57. return (uint32_t)(sizeof(sgx_sealed_data_t) + payload_size);
  58. }
  59. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  60. // pcl_get_aad_mac_txt_len()
  61. // computes optional additional data size
  62. // Parameters:
  63. // [IN] p_sealed_data - pointer to the sealed blob
  64. // Return Value:
  65. // additional data size
  66. uint32_t pcl_get_aad_mac_txt_len(const sgx_sealed_data_t* p_sealed_data)
  67. {
  68. if (p_sealed_data == NULL)
  69. return UINT32_MAX;
  70. uint32_t data_size = p_sealed_data->aes_data.payload_size - p_sealed_data->plain_text_offset;
  71. if (data_size > p_sealed_data->aes_data.payload_size)
  72. return UINT32_MAX;
  73. return data_size;
  74. }
  75. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  76. // pcl_get_encrypt_txt_len()
  77. // computes encrypted data size
  78. // Parameters:
  79. // [IN] p_sealed_data - pointer to the sealed blob
  80. // Return Value:
  81. // encrypted data size
  82. uint32_t pcl_get_encrypt_txt_len(const sgx_sealed_data_t* p_sealed_data)
  83. {
  84. return ((p_sealed_data == NULL) ? UINT32_MAX : p_sealed_data->plain_text_offset);
  85. }