Seal.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #include <sgx_tseal.h>
  32. #include "Seal_t.h"
  33. #include <memory>
  34. #include <sgx_pcl_guid.h>
  35. /**
  36. * @func provision_key_mock assigns the decryption key
  37. * @param uint8_t* key_ptr: pointer to a key buffer allocated by caller.
  38. * @param uint32_t key_len: key buffer size.
  39. * @return sgx_status_t
  40. * SGX_ERROR_INVALID_PARAMETER if key size is not SGX_AESGCM_KEY_SIZE or if key_ptr is NULL
  41. * SGX_SUCCESS if success
  42. * Notice: Function returns a hardcoded key. Never use in release code!!!
  43. * ISV must replace with secured key provisioning scheme, e.g. using remote attestation & TLS.
  44. */
  45. sgx_status_t provision_key_mock (uint8_t* key_ptr, uint32_t key_len )
  46. {
  47. if ( (NULL == key_ptr) || (SGX_AESGCM_KEY_SIZE != key_len))
  48. {
  49. return SGX_ERROR_INVALID_PARAMETER;
  50. }
  51. const uint8_t key[SGX_AESGCM_KEY_SIZE] =
  52. { 0x22, 0x22, 0x33, 0x33, 0x44, 0x44, 0x55, 0x55, 0x66, 0x66, 0x77, 0x77, 0x88, 0x88, 0x99, 0x99 };
  53. memcpy (key_ptr, key, key_len);
  54. return SGX_SUCCESS;
  55. }
  56. /*
  57. * @func provision_key provisions the key to the ISVs platform
  58. * @param uint8_t* key_ptr is the resulting decryption key
  59. * @param uint32_t key_len is key size in bytes
  60. * @return sgx_status_t, SGX_SUCCESS if function passes
  61. * @warning ISV must replace content of this function with ISVs scheme to provision
  62. * the decryption key to the platform
  63. */
  64. sgx_status_t provision_key( uint8_t* key_ptr, uint32_t key_len )
  65. {
  66. /*
  67. * ISV must replace call to provision_key_mock with an alternative ISV's secured key provisioning scheme, e.g. using remote attestation & TLS.
  68. * For more details, see 'Intel(R) SGX PCL Linux User Guide.pdf', chapter 'Integration with PCL', sub chapter 'Sealing Enclave'.
  69. */
  70. return provision_key_mock(key_ptr, key_len);
  71. }
  72. extern "C"
  73. {
  74. /*
  75. * @func ecall_get_sealed_blob_size returns the PCL sealed blob size
  76. * @return size_t, size of PCL sealed blob size in bytes
  77. */
  78. size_t ecall_get_sealed_blob_size()
  79. {
  80. return (size_t)sgx_calc_sealed_data_size ( SGX_PCL_GUID_SIZE, SGX_AESGCM_KEY_SIZE );
  81. }
  82. /*
  83. * @func ecall_generate_sealed_blob generates the sealed blob
  84. * @param uint8_t* sealed_blob is the resulting sealed blob
  85. * @param uint32_t sealed_blob_size is sealed blob size in bytes
  86. * @return sgx_status_t
  87. * SGX_ERROR_INVALID_PARAMETER if sealed_blob is NULL or if sealed_blob_size does not match PCL sealed blob size
  88. * The respective error in case provision_key or sgx_seal_data fail
  89. * SGX_SUCCESS if function passes
  90. */
  91. sgx_status_t ecall_generate_sealed_blob(uint8_t* sealed_blob, size_t sealed_blob_size)
  92. {
  93. if ((NULL == sealed_blob) || (ecall_get_sealed_blob_size() != sealed_blob_size))
  94. {
  95. return SGX_ERROR_INVALID_PARAMETER;
  96. }
  97. sgx_status_t retstatus = SGX_ERROR_UNEXPECTED;
  98. uint8_t key[SGX_AESGCM_KEY_SIZE] = { 0 };
  99. retstatus = provision_key(key, SGX_AESGCM_KEY_SIZE);
  100. if (retstatus != SGX_SUCCESS )
  101. {
  102. return retstatus;
  103. }
  104. retstatus = sgx_seal_data (
  105. SGX_PCL_GUID_SIZE, // AAD size
  106. g_pcl_guid, // AAD
  107. SGX_AESGCM_KEY_SIZE, // Key len
  108. key, // Key
  109. sealed_blob_size, // Resulting blob size
  110. (sgx_sealed_data_t*)sealed_blob ); // Resulting blob
  111. memset(key, 0,SGX_AESGCM_KEY_SIZE);
  112. return retstatus;
  113. }
  114. }; // extern "C"