123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- #include "stdlib.h"
- #include "string.h"
- #include "sgx_tcrypto.h"
- #include "se_tcrypto_common.h"
- #include "openssl/cmac.h"
- #include "openssl/err.h"
- sgx_status_t sgx_rijndael128_cmac_msg(const sgx_cmac_128bit_key_t *p_key, const uint8_t *p_src,
- uint32_t src_len, sgx_cmac_128bit_tag_t *p_mac)
- {
- void* pState = NULL;
- if ((p_key == NULL) || (p_src == NULL) || (p_mac == NULL)) {
- return SGX_ERROR_INVALID_PARAMETER;
- }
- size_t mactlen;
- sgx_status_t ret = SGX_ERROR_UNEXPECTED;
- CLEAR_OPENSSL_ERROR_QUEUE;
- do {
-
-
- pState = CMAC_CTX_new();
- if (pState == NULL) {
- ret = SGX_ERROR_OUT_OF_MEMORY;
- break;
- }
-
-
- if (!CMAC_Init((CMAC_CTX*)pState, (const void *)p_key, SGX_CMAC_KEY_SIZE, EVP_aes_128_cbc(), NULL)) {
- break;
- }
-
-
- if (!CMAC_Update((CMAC_CTX *)pState, p_src, src_len)) {
- break;
- }
-
-
- if (!CMAC_Final((CMAC_CTX*)pState, (unsigned char*)p_mac, &mactlen)) {
- break;
- }
-
-
- if (mactlen != SGX_CMAC_MAC_SIZE) {
- break;
- }
- ret = SGX_SUCCESS;
- } while (0);
-
-
- if (ret != SGX_SUCCESS) {
- GET_LAST_OPENSSL_ERROR;
- }
-
-
- if (pState) {
- CMAC_CTX_free((CMAC_CTX*)pState);
- }
- return ret;
- }
- sgx_status_t sgx_cmac128_init(const sgx_cmac_128bit_key_t *p_key, sgx_cmac_state_handle_t* p_cmac_handle)
- {
- if ((p_key == NULL) || (p_cmac_handle == NULL)) {
- return SGX_ERROR_INVALID_PARAMETER;
- }
- sgx_status_t ret = SGX_ERROR_UNEXPECTED;
- void* pState = NULL;
- CLEAR_OPENSSL_ERROR_QUEUE;
- do {
-
-
- pState = CMAC_CTX_new();
- if (pState == NULL) {
- ret = SGX_ERROR_OUT_OF_MEMORY;
- break;
- }
-
-
- if (!CMAC_Init((CMAC_CTX*)pState, (const void *)p_key, SGX_CMAC_KEY_SIZE, EVP_aes_128_cbc(), NULL)) {
- CMAC_CTX_free((CMAC_CTX*)pState);
- break;
- }
- *p_cmac_handle = pState;
- ret = SGX_SUCCESS;
- } while (0);
-
-
- if (ret != SGX_SUCCESS) {
- GET_LAST_OPENSSL_ERROR;
- }
- return ret;
- }
- sgx_status_t sgx_cmac128_update(const uint8_t *p_src, uint32_t src_len, sgx_cmac_state_handle_t cmac_handle)
- {
- if ((p_src == NULL) || (cmac_handle == NULL)) {
- return SGX_ERROR_INVALID_PARAMETER;
- }
- CLEAR_OPENSSL_ERROR_QUEUE;
- if (!CMAC_Update((CMAC_CTX *)cmac_handle, p_src, src_len)) {
- GET_LAST_OPENSSL_ERROR;
- return SGX_ERROR_UNEXPECTED;
- }
- return SGX_SUCCESS;
- }
- sgx_status_t sgx_cmac128_final(sgx_cmac_state_handle_t cmac_handle, sgx_cmac_128bit_tag_t *p_hash)
- {
- if ((cmac_handle == NULL) || (p_hash == NULL))
- {
- return SGX_ERROR_INVALID_PARAMETER;
- }
- size_t mactlen;
- CLEAR_OPENSSL_ERROR_QUEUE;
- if (!CMAC_Final((CMAC_CTX*)cmac_handle, (unsigned char*)p_hash, &mactlen)) {
- GET_LAST_OPENSSL_ERROR;
- return SGX_ERROR_UNEXPECTED;
- }
- return SGX_SUCCESS;
- }
- sgx_status_t sgx_cmac128_close(sgx_cmac_state_handle_t cmac_handle)
- {
- if (cmac_handle == NULL) {
- return SGX_ERROR_INVALID_PARAMETER;
- }
- CMAC_CTX* pState = (CMAC_CTX*)cmac_handle;
- CMAC_CTX_free(pState);
- return SGX_SUCCESS;
- }
|