sgx_sha256.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 "se_tcrypto_common.h"
  32. #include <openssl/evp.h>
  33. #include <openssl/err.h>
  34. #include "sgx_tcrypto.h"
  35. #include "stdlib.h"
  36. /* Allocates and initializes sha256 state
  37. * Parameters:
  38. * Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
  39. * Output: sgx_sha_state_handle_t *p_sha_handle - Pointer to the handle of the SHA256 state */
  40. sgx_status_t sgx_sha256_init(sgx_sha_state_handle_t* p_sha_handle)
  41. {
  42. if (p_sha_handle == NULL) {
  43. return SGX_ERROR_INVALID_PARAMETER;
  44. }
  45. EVP_MD_CTX* evp_ctx = NULL;
  46. const EVP_MD* sha256_md = NULL;
  47. sgx_status_t retval = SGX_ERROR_UNEXPECTED;
  48. CLEAR_OPENSSL_ERROR_QUEUE;
  49. do {
  50. /* allocates, initializes and returns a digest context */
  51. evp_ctx = EVP_MD_CTX_new();
  52. if (evp_ctx == NULL) {
  53. retval = SGX_ERROR_OUT_OF_MEMORY;
  54. break;
  55. }
  56. /* return EVP_MD structures for SHA256 digest algorithm */
  57. sha256_md = EVP_sha256();
  58. if (sha256_md == NULL) {
  59. break;
  60. }
  61. /* sets up digest context ctx to use a digest type, if impl is NULL then the default implementation of digest type is used */
  62. if (EVP_DigestInit_ex(evp_ctx, sha256_md, NULL) != 1) {
  63. break;
  64. }
  65. *p_sha_handle = evp_ctx;
  66. retval = SGX_SUCCESS;
  67. } while(0);
  68. if (SGX_SUCCESS != retval) {
  69. GET_LAST_OPENSSL_ERROR;
  70. if (evp_ctx != NULL) {
  71. EVP_MD_CTX_free(evp_ctx);
  72. }
  73. }
  74. return retval;
  75. }
  76. /* Updates sha256 has calculation based on the input message
  77. * Parameters:
  78. * Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.
  79. * Input: sgx_sha_state_handle_t sha_handle - Handle to the SHA256 state
  80. * uint8_t *p_src - Pointer to the input stream to be hashed
  81. * uint32_t src_len - Length of the input stream to be hashed */
  82. sgx_status_t sgx_sha256_update(const uint8_t *p_src, uint32_t src_len, sgx_sha_state_handle_t sha_handle)
  83. {
  84. if ((p_src == NULL) || (sha_handle == NULL))
  85. {
  86. return SGX_ERROR_INVALID_PARAMETER;
  87. }
  88. sgx_status_t retval = SGX_ERROR_UNEXPECTED;
  89. CLEAR_OPENSSL_ERROR_QUEUE;
  90. do {
  91. /* hashes src_len bytes of data at p_src into the digest context sha_handle */
  92. if(EVP_DigestUpdate((EVP_MD_CTX*)sha_handle, p_src, src_len) != 1) {
  93. GET_LAST_OPENSSL_ERROR;
  94. break;
  95. }
  96. retval = SGX_SUCCESS;
  97. } while (0);
  98. return retval;
  99. }
  100. /* Returns Hash calculation
  101. * Parameters:
  102. * Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
  103. * Input: sgx_sha_state_handle_t sha_handle - Handle to the SHA256 state
  104. * Output: sgx_sha256_hash_t *p_hash - Resultant hash from operation */
  105. sgx_status_t sgx_sha256_get_hash(sgx_sha_state_handle_t sha_handle, sgx_sha256_hash_t *p_hash)
  106. {
  107. if ((sha_handle == NULL) || (p_hash == NULL))
  108. {
  109. return SGX_ERROR_INVALID_PARAMETER;
  110. }
  111. sgx_status_t retval = SGX_ERROR_UNEXPECTED;
  112. unsigned int hash_len = 0;
  113. CLEAR_OPENSSL_ERROR_QUEUE;
  114. do {
  115. /* retrieves the digest value from sha_handle and places it in p_hash */
  116. if (EVP_DigestFinal_ex((EVP_MD_CTX*)sha_handle, (unsigned char *)p_hash, &hash_len) != 1) {
  117. GET_LAST_OPENSSL_ERROR;
  118. break;
  119. }
  120. if (SGX_SHA256_HASH_SIZE != hash_len) {
  121. break;
  122. }
  123. retval = SGX_SUCCESS;
  124. } while(0);
  125. return retval;
  126. }
  127. /* Cleans up sha state
  128. * Parameters:
  129. * Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
  130. * Input: sgx_sha_state_handle_t sha_handle - Handle to the SHA256 state */
  131. sgx_status_t sgx_sha256_close(sgx_sha_state_handle_t sha_handle)
  132. {
  133. if (sha_handle == NULL)
  134. {
  135. return SGX_ERROR_INVALID_PARAMETER;
  136. }
  137. EVP_MD_CTX_free((EVP_MD_CTX*)sha_handle);
  138. return SGX_SUCCESS;
  139. }