123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- #include "sgx_tcrypto.h"
- #include "ippcp.h"
- #include "stdlib.h"
- #include "string.h"
- sgx_status_t sgx_aes_ctr_encrypt(const sgx_aes_ctr_128bit_key_t *p_key, const uint8_t *p_src,
- const uint32_t src_len, uint8_t *p_ctr, const uint32_t ctr_inc_bits,
- uint8_t *p_dst)
- {
- IppStatus error_code = ippStsNoErr;
- IppsAESSpec* ptr_ctx = NULL;
- int ctx_size = 0;
- if ((p_key == NULL) || (p_src == NULL) || (p_ctr == NULL) || (p_dst == NULL))
- {
- return SGX_ERROR_INVALID_PARAMETER;
- }
-
- error_code = ippsAESGetSize(&ctx_size);
- if (error_code != ippStsNoErr)
- {
- return SGX_ERROR_UNEXPECTED;
- }
- ptr_ctx = (IppsAESSpec*)malloc(ctx_size);
- if (ptr_ctx == NULL)
- {
- return SGX_ERROR_OUT_OF_MEMORY;
- }
-
- error_code = ippsAESInit((const Ipp8u*)p_key, SGX_AESCTR_KEY_SIZE, ptr_ctx, ctx_size);
- if (error_code != ippStsNoErr)
- {
-
- memset_s(ptr_ctx, ctx_size, 0, ctx_size);
- free(ptr_ctx);
- switch (error_code)
- {
- case ippStsMemAllocErr: return SGX_ERROR_OUT_OF_MEMORY;
- case ippStsNullPtrErr:
- case ippStsLengthErr: return SGX_ERROR_INVALID_PARAMETER;
- default: return SGX_ERROR_UNEXPECTED;
- }
- }
- error_code = ippsAESEncryptCTR(p_src, p_dst, src_len, ptr_ctx, p_ctr, ctr_inc_bits);
- if (error_code != ippStsNoErr)
- {
-
- memset_s(ptr_ctx, ctx_size, 0, ctx_size);
- free(ptr_ctx);
- switch (error_code)
- {
- case ippStsCTRSizeErr:
- case ippStsNullPtrErr:
- case ippStsLengthErr: return SGX_ERROR_INVALID_PARAMETER;
- default: return SGX_ERROR_UNEXPECTED;
- }
- }
-
- memset_s(ptr_ctx, ctx_size, 0, ctx_size);
- free(ptr_ctx);
- return SGX_SUCCESS;
- }
- sgx_status_t sgx_aes_ctr_decrypt(const sgx_aes_ctr_128bit_key_t *p_key, const uint8_t *p_src,
- const uint32_t src_len, uint8_t *p_ctr, const uint32_t ctr_inc_bits,
- uint8_t *p_dst)
- {
- IppStatus error_code = ippStsNoErr;
- IppsAESSpec* ptr_ctx = NULL;
- int ctx_size = 0;
- if ((p_key == NULL) || (p_src == NULL) || (p_ctr == NULL) || (p_dst == NULL))
- {
- return SGX_ERROR_INVALID_PARAMETER;
- }
-
- error_code = ippsAESGetSize(&ctx_size);
- if (error_code != ippStsNoErr)
- {
- return SGX_ERROR_UNEXPECTED;
- }
- ptr_ctx = (IppsAESSpec*)malloc(ctx_size);
- if (ptr_ctx == NULL)
- {
- return SGX_ERROR_OUT_OF_MEMORY;
- }
-
- error_code = ippsAESInit((const Ipp8u*)p_key, SGX_AESCTR_KEY_SIZE, ptr_ctx, ctx_size);
- if (error_code != ippStsNoErr)
- {
-
- memset_s(ptr_ctx, ctx_size, 0, ctx_size);
- free(ptr_ctx);
- switch (error_code)
- {
- case ippStsMemAllocErr: return SGX_ERROR_OUT_OF_MEMORY;
- case ippStsNullPtrErr:
- case ippStsLengthErr: return SGX_ERROR_INVALID_PARAMETER;
- default: return SGX_ERROR_UNEXPECTED;
- }
- }
- error_code = ippsAESDecryptCTR(p_src, p_dst, src_len, ptr_ctx, p_ctr, ctr_inc_bits);
- if (error_code != ippStsNoErr)
- {
-
- memset_s(ptr_ctx, ctx_size, 0, ctx_size);
- free(ptr_ctx);
- switch (error_code)
- {
- case ippStsCTRSizeErr:
- case ippStsNullPtrErr:
- case ippStsLengthErr: return SGX_ERROR_INVALID_PARAMETER;
- default: return SGX_ERROR_UNEXPECTED;
- }
- }
-
- memset_s(ptr_ctx, ctx_size, 0, ctx_size);
- free(ptr_ctx);
- return SGX_SUCCESS;
- }
|