123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- #include "se_tcrypto_common.h"
- #include <openssl/evp.h>
- #include <openssl/err.h>
- #include "sgx_tcrypto.h"
- #include "stdlib.h"
- sgx_status_t sgx_sha256_init(sgx_sha_state_handle_t* p_sha_handle)
- {
- if (p_sha_handle == NULL) {
- return SGX_ERROR_INVALID_PARAMETER;
- }
- EVP_MD_CTX* evp_ctx = NULL;
- const EVP_MD* sha256_md = NULL;
- sgx_status_t retval = SGX_ERROR_UNEXPECTED;
- CLEAR_OPENSSL_ERROR_QUEUE;
- do {
-
- evp_ctx = EVP_MD_CTX_new();
- if (evp_ctx == NULL) {
- retval = SGX_ERROR_OUT_OF_MEMORY;
- break;
- }
-
- sha256_md = EVP_sha256();
- if (sha256_md == NULL) {
- break;
- }
-
- if (EVP_DigestInit_ex(evp_ctx, sha256_md, NULL) != 1) {
- break;
- }
- *p_sha_handle = evp_ctx;
- retval = SGX_SUCCESS;
- } while(0);
- if (SGX_SUCCESS != retval) {
- GET_LAST_OPENSSL_ERROR;
- if (evp_ctx != NULL) {
- EVP_MD_CTX_free(evp_ctx);
- }
- }
- return retval;
- }
- sgx_status_t sgx_sha256_update(const uint8_t *p_src, uint32_t src_len, sgx_sha_state_handle_t sha_handle)
- {
- if ((p_src == NULL) || (sha_handle == NULL))
- {
- return SGX_ERROR_INVALID_PARAMETER;
- }
- sgx_status_t retval = SGX_ERROR_UNEXPECTED;
- CLEAR_OPENSSL_ERROR_QUEUE;
- do {
-
- if(EVP_DigestUpdate((EVP_MD_CTX*)sha_handle, p_src, src_len) != 1) {
- GET_LAST_OPENSSL_ERROR;
- break;
- }
- retval = SGX_SUCCESS;
- } while (0);
- return retval;
- }
- sgx_status_t sgx_sha256_get_hash(sgx_sha_state_handle_t sha_handle, sgx_sha256_hash_t *p_hash)
- {
- if ((sha_handle == NULL) || (p_hash == NULL))
- {
- return SGX_ERROR_INVALID_PARAMETER;
- }
- sgx_status_t retval = SGX_ERROR_UNEXPECTED;
- unsigned int hash_len = 0;
- CLEAR_OPENSSL_ERROR_QUEUE;
- do {
-
- if (EVP_DigestFinal_ex((EVP_MD_CTX*)sha_handle, (unsigned char *)p_hash, &hash_len) != 1) {
- GET_LAST_OPENSSL_ERROR;
- break;
- }
- if (SGX_SHA256_HASH_SIZE != hash_len) {
- break;
- }
- retval = SGX_SUCCESS;
- } while(0);
- return retval;
- }
- sgx_status_t sgx_sha256_close(sgx_sha_state_handle_t sha_handle)
- {
- if (sha_handle == NULL)
- {
- return SGX_ERROR_INVALID_PARAMETER;
- }
- EVP_MD_CTX_free((EVP_MD_CTX*)sha_handle);
- return SGX_SUCCESS;
- }
|