123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- #include "sgx_tcrypto.h"
- #include "ippcp.h"
- #include "stdlib.h"
- #ifndef SAFE_FREE
- #define SAFE_FREE(ptr) {if (NULL != (ptr)) {free(ptr); (ptr)=NULL;}}
- #endif
- sgx_status_t sgx_sha256_init(sgx_sha_state_handle_t* p_sha_handle)
- {
- IppStatus ipp_ret = ippStsNoErr;
- IppsHashState* p_temp_state = NULL;
- if (p_sha_handle == NULL)
- return SGX_ERROR_INVALID_PARAMETER;
- int ctx_size = 0;
- ipp_ret = ippsHashGetSize(&ctx_size);
- if (ipp_ret != ippStsNoErr)
- return SGX_ERROR_UNEXPECTED;
- p_temp_state = (IppsHashState*)(malloc(ctx_size));
- if (p_temp_state == NULL)
- return SGX_ERROR_OUT_OF_MEMORY;
- ipp_ret = ippsHashInit(p_temp_state, IPP_ALG_HASH_SHA256);
- if (ipp_ret != ippStsNoErr)
- {
- SAFE_FREE(p_temp_state);
- *p_sha_handle = NULL;
- switch (ipp_ret)
- {
- case ippStsNullPtrErr:
- case ippStsLengthErr: return SGX_ERROR_INVALID_PARAMETER;
- default: return SGX_ERROR_UNEXPECTED;
- }
- }
- *p_sha_handle = p_temp_state;
- return SGX_SUCCESS;
- }
- 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;
- }
- IppStatus ipp_ret = ippStsNoErr;
- ipp_ret = ippsHashUpdate(p_src, src_len, (IppsHashState*)sha_handle);
- switch (ipp_ret)
- {
- case ippStsNoErr: return SGX_SUCCESS;
- case ippStsNullPtrErr:
- case ippStsLengthErr: return SGX_ERROR_INVALID_PARAMETER;
- default: return SGX_ERROR_UNEXPECTED;
- }
- }
- 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;
- }
- IppStatus ipp_ret = ippStsNoErr;
- ipp_ret = ippsHashGetTag((Ipp8u*)p_hash, SGX_SHA256_HASH_SIZE, (IppsHashState*)sha_handle);
- switch (ipp_ret)
- {
- case ippStsNoErr: return SGX_SUCCESS;
- case ippStsNullPtrErr:
- case ippStsLengthErr: return SGX_ERROR_INVALID_PARAMETER;
- default: return SGX_ERROR_UNEXPECTED;
- }
- }
- sgx_status_t sgx_sha256_close(sgx_sha_state_handle_t sha_handle)
- {
- if (sha_handle == NULL)
- {
- return SGX_ERROR_INVALID_PARAMETER;
- }
- SAFE_FREE(sha_handle);
- return SGX_SUCCESS;
- }
|