123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- #include "stdlib.h"
- #include "string.h"
- #include "sgx_tcrypto.h"
- #include "se_tcrypto_common.h"
- #include "openssl/aes.h"
- #include "openssl/evp.h"
- #include "openssl/err.h"
- #define SGXSSL_CTR_BITS 128
- #define SHIFT_BYTE 8
- static void ctr128_inc(unsigned char *counter)
- {
- unsigned int n = 16, c = 1;
- do {
- --n;
- c += counter[n];
- counter[n] = (unsigned char)c;
- c >>= SHIFT_BYTE;
- } while (n);
- }
- 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)
- {
- if ((src_len > INT_MAX) || (p_key == NULL) || (p_src == NULL) || (p_ctr == NULL) || (p_dst == NULL))
- {
- return SGX_ERROR_INVALID_PARAMETER;
- }
-
- sgx_status_t ret = SGX_ERROR_UNEXPECTED;
- int len = 0;
- EVP_CIPHER_CTX* ptr_ctx = NULL;
-
-
-
-
-
- if (ctr_inc_bits != SGXSSL_CTR_BITS)
- {
- return SGX_ERROR_INVALID_PARAMETER;
- }
- CLEAR_OPENSSL_ERROR_QUEUE;
- do {
-
-
- if (!(ptr_ctx = EVP_CIPHER_CTX_new())) {
- ret = SGX_ERROR_OUT_OF_MEMORY;
- break;
- }
-
-
- if (1 != EVP_EncryptInit_ex(ptr_ctx, EVP_aes_128_ctr(), NULL, (unsigned char*)p_key, p_ctr)) {
- break;
- }
-
-
- if (1 != EVP_EncryptUpdate(ptr_ctx, p_dst, &len, p_src, src_len)) {
- break;
- }
-
-
- if (1 != EVP_EncryptFinal_ex(ptr_ctx, p_dst + len, &len)) {
- break;
- }
-
-
- len = src_len;
- while (len >= 0) {
- ctr128_inc(p_ctr);
- len -= 16;
- }
- ret = SGX_SUCCESS;
- } while (0);
- if (ret != SGX_SUCCESS) {
- GET_LAST_OPENSSL_ERROR;
- }
-
-
- if (ptr_ctx) {
- EVP_CIPHER_CTX_free(ptr_ctx);
- }
- return ret;
- }
- 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)
- {
- if ((src_len > INT_MAX) || (p_key == NULL) || (p_src == NULL) || (p_ctr == NULL) || (p_dst == NULL)) {
- return SGX_ERROR_INVALID_PARAMETER;
- }
-
- sgx_status_t ret = SGX_ERROR_UNEXPECTED;
- int len = 0;
- EVP_CIPHER_CTX* ptr_ctx = NULL;
-
-
-
-
-
- if (ctr_inc_bits != SGXSSL_CTR_BITS) {
- return SGX_ERROR_INVALID_PARAMETER;
- }
- CLEAR_OPENSSL_ERROR_QUEUE;
- do {
-
-
- if (!(ptr_ctx = EVP_CIPHER_CTX_new())) {
- ret = SGX_ERROR_OUT_OF_MEMORY;
- break;
- }
-
-
- if (!EVP_DecryptInit_ex(ptr_ctx, EVP_aes_128_ctr(), NULL, (unsigned char*)p_key, p_ctr)) {
- break;
- }
-
-
- if (!EVP_DecryptUpdate(ptr_ctx, p_dst, &len, p_src, src_len)) {
- break;
- }
-
-
-
- if (EVP_DecryptFinal_ex(ptr_ctx, p_dst + len, &len) <= 0) {
- break;
- }
-
-
-
- len = src_len;
- while (len >= 0) {
- ctr128_inc(p_ctr);
- len -= 16;
- }
- ret = SGX_SUCCESS;
- } while (0);
- if (ret != SGX_SUCCESS) {
- GET_LAST_OPENSSL_ERROR;
- }
-
-
- if (ptr_ctx) {
- EVP_CIPHER_CTX_free(ptr_ctx);
- }
- return ret;
- }
|