prepare_hash_sha256.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (C) 2011-2017 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 "prepare_hash_sha256.h"
  32. #include <cstddef>
  33. PrepareHashSHA256::PrepareHashSHA256()
  34. : m_sgx_status(SGX_SUCCESS), m_pShaState(NULL)
  35. {
  36. m_sgx_status = sgx_sha256_init(&m_pShaState);
  37. }
  38. PrepareHashSHA256::~PrepareHashSHA256(void)
  39. {
  40. if (NULL != m_pShaState)
  41. sgx_sha256_close(m_pShaState);
  42. }
  43. ae_error_t PrepareHashSHA256::Update(const void* pData, size_t numBytes)
  44. {
  45. ae_error_t ae_status = PSE_PR_HASH_CALC_ERROR;
  46. do
  47. {
  48. if (SGX_SUCCESS != m_sgx_status) {
  49. break;
  50. }
  51. if (NULL == pData || numBytes < 1 || NULL == m_pShaState || numBytes > UINT32_MAX)
  52. {
  53. m_sgx_status = SGX_ERROR_INVALID_PARAMETER;
  54. break;
  55. }
  56. m_sgx_status = sgx_sha256_update((const uint8_t*)pData, (uint32_t)numBytes, m_pShaState);
  57. if (SGX_SUCCESS != m_sgx_status) {
  58. break;
  59. }
  60. ae_status = AE_SUCCESS;
  61. } while (0);
  62. if (SGX_ERROR_OUT_OF_MEMORY == m_sgx_status)
  63. {
  64. ae_status = PSE_PR_INSUFFICIENT_MEMORY_ERROR;
  65. }
  66. return ae_status;
  67. }
  68. ae_error_t PrepareHashSHA256::Finalize(SHA256_HASH *pHash)
  69. {
  70. ae_error_t ae_status = PSE_PR_HASH_CALC_ERROR;
  71. do
  72. {
  73. if (SGX_SUCCESS != m_sgx_status)
  74. break;
  75. if (NULL == m_pShaState || NULL == pHash)
  76. {
  77. m_sgx_status = SGX_ERROR_INVALID_PARAMETER;
  78. break;
  79. }
  80. if (sizeof(*pHash) != sizeof(sgx_sha256_hash_t))
  81. {
  82. m_sgx_status = SGX_ERROR_INVALID_PARAMETER;
  83. break;
  84. }
  85. m_sgx_status = sgx_sha256_get_hash(m_pShaState, (sgx_sha256_hash_t*)pHash);
  86. if (SGX_SUCCESS != m_sgx_status) {
  87. break;
  88. }
  89. ae_status = AE_SUCCESS;
  90. } while (0);
  91. if (SGX_ERROR_OUT_OF_MEMORY == m_sgx_status) {
  92. ae_status = PSE_PR_INSUFFICIENT_MEMORY_ERROR;
  93. }
  94. return ae_status;
  95. }