123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897 |
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <elf.h>
- #include <immintrin.h>
- #include <cpuid.h>
- #include <stdbool.h>
- #include "openssl/evp.h"
- #include "openssl/ossl_typ.h"
- #include "openssl/sha.h"
- #include <sgx_tseal.h>
- #include <sgx_tcrypto.h>
- #include "pcl_common.h"
- #include "encryptip.h"
- #include "sgx_pcl_guid.h"
- int main(int argc, IN char *argv[])
- {
- char* keyfile_name = NULL;
- char* enclave_in_name = NULL;
- char* enclave_out_name = NULL;
- bool debug = false;
- uint8_t* enclave_buf = NULL;
- uint8_t* key_buf = NULL;
- size_t key_size = 0;
- size_t enclave_size = 0;
-
- encip_ret_e ret = parse_args(argc, argv, &enclave_in_name, &enclave_out_name, &keyfile_name, &debug);
- if(ENCIP_ERROR(ret))
- return (int)ret;
-
- ret = read_file(enclave_in_name, &enclave_buf, &enclave_size);
- if(ENCIP_ERROR(ret))
- return (int)ret;
- if(0 == enclave_size)
- {
- ret = ENCIP_ERROR_ENCLAVE_SIZE;
- goto Label_free_enclave_buffer;
- }
-
- ret = read_file(keyfile_name, &key_buf, &key_size);
- if(ENCIP_ERROR(ret))
- goto Label_free_enclave_buffer;
- if(SGX_AESGCM_KEY_SIZE != key_size)
- {
- ret = ENCIP_ERROR_KEY_FILE_SIZE;
- goto Label_free_key_and_enclave_buffers;
- }
-
-
- ret = encrypt_ip(enclave_buf, enclave_size, key_buf, debug);
- if(ENCIP_ERROR(ret))
- goto Label_free_key_and_enclave_buffers;
-
- ret = write_file(enclave_out_name, enclave_buf, enclave_size);
- if(ENCIP_ERROR(ret))
- goto Label_free_key_and_enclave_buffers;
-
- ret = ENCIP_SUCCESS;
- Label_free_key_and_enclave_buffers:
- free(key_buf);
- Label_free_enclave_buffer:
- free(enclave_buf);
- return (int)ret;
- }
- static inline bool can_modify(IN const char* const sec_name, bool debug)
- {
-
- static char const* const non_ip_sec_names[NUM_NON_IP_OR_DEBUG_SEC_NAMES] =
- {
- ".shstrtab",
- ".note.sgxmeta",
- ".bss",
- ".tbss",
- ".dynamic",
- ".dynsym",
- ".dynstr",
- ".rela.dyn",
- PCLTBL_SECTION_NAME,
- PCL_TEXT_SECTION_NAME,
- PCL_DATA_SECTION_NAME,
- PCL_RODATA_SECTION_NAME,
- ".comment",
- ".debug_aranges",
- ".debug_info",
- ".debug_abbrev",
- ".debug_line",
- ".debug_str",
- ".debug_loc",
- ".debug_ranges",
- ".gnu.version_d",
- ".symtab",
- ".strtab",
- };
- if(NULL == sec_name)
- return false;
- uint32_t num_sec_names = debug ? NUM_NON_IP_OR_DEBUG_SEC_NAMES : NUM_NON_IP_SEC_NAMES;
- for(uint32_t secidx = 0; secidx < num_sec_names; secidx++)
- {
- if(!strcmp(non_ip_sec_names[secidx],sec_name))
- return false;
- }
- return true;
- }
- static encip_ret_e parse_elf(const void* const elf_buf_in, size_t elf_size, pcl_data_t* dat)
- {
- if(NULL == elf_buf_in || NULL == dat)
- return ENCIP_ERROR_PARSE_ELF_INVALID_PARAM;
- uint8_t* elf_buf = (uint8_t*)elf_buf_in;
-
- if(sizeof(Elf64_Ehdr) > elf_size)
- return ENCIP_ERROR_PARSE_ELF_INVALID_IMAGE;
- Elf64_Ehdr* elf_hdr = (Elf64_Ehdr*)(elf_buf);
-
- if(ELFMAG0 != elf_hdr->e_ident[EI_MAG0] ||
- ELFMAG1 != elf_hdr->e_ident[EI_MAG1] ||
- ELFMAG2 != elf_hdr->e_ident[EI_MAG2] ||
- ELFMAG3 != elf_hdr->e_ident[EI_MAG3])
- return ENCIP_ERROR_PARSE_ELF_INVALID_IMAGE;
-
- dat->nsections = elf_hdr->e_shnum;
-
- uint16_t shstrndx = elf_hdr->e_shstrndx;
- dat->shstrndx = shstrndx;
- if(dat->nsections <= shstrndx)
- return ENCIP_ERROR_PARSE_ELF_INVALID_IMAGE;
-
- if((elf_hdr->e_shoff >= elf_size) ||
- (elf_hdr->e_shoff + dat->nsections * sizeof(Elf64_Shdr) < elf_hdr->e_shoff) ||
- (elf_hdr->e_shoff + dat->nsections * sizeof(Elf64_Shdr) > elf_size))
- return ENCIP_ERROR_PARSE_ELF_INVALID_IMAGE;
- dat->elf_sec = (Elf64_Shdr*)(elf_buf + elf_hdr->e_shoff);
-
- if((dat->elf_sec[shstrndx].sh_offset >= elf_size) ||
- (dat->elf_sec[shstrndx].sh_offset + dat->elf_sec[shstrndx].sh_size < dat->elf_sec[shstrndx].sh_offset) ||
- (dat->elf_sec[shstrndx].sh_offset + dat->elf_sec[shstrndx].sh_size > elf_size))
- return ENCIP_ERROR_PARSE_ELF_INVALID_IMAGE;
- dat->sections_names = (char*)(elf_buf + dat->elf_sec[shstrndx].sh_offset);
-
- dat->nsegments = elf_hdr->e_phnum;
- if((elf_hdr->e_phoff >= elf_size) ||
- (elf_hdr->e_phoff + dat->nsegments * sizeof(Elf64_Phdr) < elf_hdr->e_phoff) ||
- (elf_hdr->e_phoff + dat->nsegments * sizeof(Elf64_Phdr) > elf_size))
- return ENCIP_ERROR_PARSE_ELF_INVALID_IMAGE;
- dat->phdr = (Elf64_Phdr*)(elf_buf + elf_hdr->e_phoff);
-
- return ENCIP_SUCCESS;
- }
- static encip_ret_e get_pcl_tbl(
- IN const void* const elf_buf_in,
- size_t elf_size,
- IN const pcl_data_t* const dat,
- OUT pcl_table_t** tbl_pp)
- {
- if(NULL == elf_buf_in || NULL == dat || NULL == tbl_pp)
- return ENCIP_ERROR_GETTBL_INVALID_PARAM;
- bool tbl_found = false;
- uint8_t* elf_buf = (uint8_t*)elf_buf_in;
-
- for(uint16_t secidx = 1; secidx < dat->nsections && !tbl_found; secidx++)
- {
- if(dat->elf_sec[secidx].sh_name >= dat->elf_sec[dat->shstrndx].sh_size)
- return ENCIP_ERROR_PARSE_ELF_INVALID_IMAGE;
- char* sec_name = dat->sections_names + dat->elf_sec[secidx].sh_name;
-
- if((uint8_t*)sec_name >= elf_buf + elf_size)
- return ENCIP_ERROR_PARSE_ELF_INVALID_IMAGE;
- if(0 == strcmp(sec_name, PCLTBL_SECTION_NAME))
- {
- *tbl_pp = (pcl_table_t *)(elf_buf + dat->elf_sec[secidx].sh_offset);
- if((uint8_t*)(*tbl_pp) + sizeof(pcl_table_t) >= elf_buf + elf_size)
- return ENCIP_ERROR_PARSE_ELF_INVALID_IMAGE;
-
- if(0 != dat->elf_sec[secidx].sh_offset % PCL_TABLE_ALLIGNMENT)
- return ENCIP_ERROR_TBL_NOT_ALIGNED;
- tbl_found = true;
- }
- }
- if(!tbl_found)
- return ENCIP_ERROR_TBL_NOT_FOUND;
- return ENCIP_SUCCESS;
- }
- bool rdrand_supported()
- {
- int cpu_info[4] = {0, 0, 0, 0};
- __cpuid(cpu_info, 1);
- return (!!(cpu_info[2] & SUPPORT_RDRAND));
- }
- static encip_ret_e init_random_iv(OUT uint8_t* iv)
- {
- if(NULL == iv)
- return ENCIP_ERROR_RANDIV_INVALID_PARAM;
- if(rdrand_supported())
- {
- uint32_t* ivp = (uint32_t*)iv;
-
- for(uint32_t i=0;i < SGX_AESGCM_IV_SIZE / sizeof(uint32_t);i++)
- {
- uint32_t randval = 0;
- int rdrand32ret = _rdrand32_step(&randval);
- if(RDRAND_SUCCESS != rdrand32ret)
- return ENCIP_ERROR_RDRAND_FAILED;
- *ivp = randval;
- ivp++;
- }
- }
- else
- {
- return ENCIP_ERROR_RDRAND_NOT_SUPPORTED;
- }
- return ENCIP_SUCCESS;
- }
- static inline encip_ret_e update_flags(uint16_t secidx, INOUT pcl_data_t* dat)
- {
- if(NULL == dat)
- return ENCIP_ERROR_UPDATEF_INVALID_PAR;
-
- dat->elf_sec[secidx].sh_flags |= SHF_WRITE;
- Elf64_Addr secstart = dat->elf_sec[secidx].sh_addr;
- size_t secsize = dat->elf_sec[secidx].sh_size;
-
- for(uint16_t segidx=0;segidx<dat->nsegments;segidx++)
- {
- Elf64_Addr segstart = dat->phdr[segidx].p_vaddr;
- size_t segsize = dat->phdr[segidx].p_memsz;
- if(((secstart < segstart + segsize) && (secstart >= segstart )) ||
- ((secstart + secsize > segstart ) && (secstart + secsize <= segstart + segsize)))
- {
-
- if(!(dat->phdr[segidx].p_flags & (Elf64_Word)PF_R))
- {
- printf("\n\nError: segment %d ", segidx);
- if(dat->elf_sec[secidx].sh_name < dat->elf_sec[dat->shstrndx].sh_size)
- {
- char* sec_name = dat->sections_names + dat->elf_sec[secidx].sh_name;
-
- printf("overlaps encrypted section \"%s\" and ", sec_name);
- }
- printf("is not readable. Exiting!!!\n\n\n");
- return ENCIP_ERROR_SEGMENT_NOT_READABLE;
- }
-
- dat->phdr[segidx].p_flags |= (Elf64_Word)PF_W;
- }
- }
- return ENCIP_SUCCESS;
- }
- static encip_ret_e encrypt_or_clear_ip_sections(
- IN pcl_data_t* dat,
- IN uint8_t* key,
- INOUT uint8_t* elf_buf,
- size_t elf_size,
- OUT pcl_table_t* tbl,
- OUT uint32_t* num_rvas_out,
- bool debug)
- {
- if(
- NULL == dat ||
- NULL == key ||
- NULL == elf_buf ||
- NULL == tbl ||
- NULL == num_rvas_out)
- return ENCIP_ERROR_ENCSECS_INVALID_PARAM;
- uint32_t num_rvas = 0;
-
- char* sec_name = NULL;
- for(uint16_t secidx = 1; secidx < dat->nsections; secidx++)
- {
- if(dat->elf_sec[secidx].sh_name >= dat->elf_sec[dat->shstrndx].sh_size)
- return ENCIP_ERROR_PARSE_ELF_INVALID_IMAGE;
- sec_name = dat->sections_names + dat->elf_sec[secidx].sh_name;
-
- if((uint8_t*)sec_name > elf_buf + elf_size)
- return ENCIP_ERROR_PARSE_ELF_INVALID_IMAGE;
- if(can_modify(sec_name, debug))
- {
- uint8_t* va = (uint8_t *)(elf_buf + dat->elf_sec[secidx].sh_offset);
- size_t size = dat->elf_sec[secidx].sh_size;
- if((va >= elf_buf + elf_size) ||
- (va + size < va) ||
- (va + size > elf_buf + elf_size))
- return ENCIP_ERROR_PARSE_ELF_INVALID_IMAGE;
-
- if(SHF_ALLOC & dat->elf_sec[secidx].sh_flags)
- {
-
- if(PCL_MAX_NUM_ENCRYPTED_SECTIONS <= num_rvas)
- {
-
- printf("Error: No more empty entries in Intel(R) SGX PCL table\n");
- printf("To fix - redefine PCL_MAX_NUM_ENCRYPTED_SECTIONS in pcl_common.h\n");
- return ENCIP_ERROR_ENCSECS_RVAS_OVERFLOW;
- }
-
- if(PCL_GCM_NUM_BLOCKS(size) > PCL_GCM_MAX_NUM_BLOCKS)
- {
-
- return ENCIP_ERROR_ENCSECS_COUNTER_OVERFLOW;
- }
- uint8_t* iv = (uint8_t*)&(tbl->rvas_sizes_tags_ivs[num_rvas].iv.val);
- encip_ret_e ret = init_random_iv(iv);
- if(ENCIP_ERROR(ret))
- return ret;
- uint8_t* tag = (uint8_t*)&(tbl->rvas_sizes_tags_ivs[num_rvas].tag);
- ret = gcm_encrypt(va, size, NULL, 0, (uint8_t *)key, iv, va, tag);
- if(ENCIP_ERROR(ret))
- {
- printf("Failed to gcm-encrypt section %s\n", sec_name);
- return ret;
- }
-
- tbl->rvas_sizes_tags_ivs[num_rvas].rva = dat->elf_sec[secidx].sh_addr;
- tbl->rvas_sizes_tags_ivs[num_rvas].size = size;
-
- ret = update_flags(secidx, dat);
- if(ENCIP_ERROR(ret))
- return ret;
-
- num_rvas++;
- }
-
- else
- {
- memset(va, 0, size);
- }
- }
- }
- *num_rvas_out = num_rvas;
- return ENCIP_SUCCESS;
- }
- encip_ret_e encrypt_ip(INOUT uint8_t* elf_buf, size_t elf_size, IN uint8_t* key, bool debug)
- {
- if(NULL == elf_buf || NULL == key)
- return ENCIP_ERROR_ENCRYPTIP_INVALID_PARAM;
- encip_ret_e ret = ENCIP_ERROR_FAIL;
- pcl_data_t dat = {
- .elf_sec = 0,
- .shstrndx = 0,
- .sections_names = NULL,
- .phdr = NULL,
- .nsections = 0,
- .nsegments = 0,
- };
- pcl_table_t* tbl = NULL;
- ret = parse_elf(elf_buf, elf_size, &dat);
- if(ENCIP_ERROR(ret))
- return ret;
- ret = get_pcl_tbl(elf_buf, elf_size, &dat, &tbl);
- if(ENCIP_ERROR(ret))
- return ret;
-
- if(PCL_CIPHER == tbl->pcl_state)
- return ENCIP_ERROR_ALREADY_ENCRYPTED;
- if(PCL_PLAIN != tbl->pcl_state)
- return ENCIP_ERROR_IMPROPER_STATE;
-
- uint32_t num_rvas = 0;
- ret = encrypt_or_clear_ip_sections(&dat, key, elf_buf, elf_size, tbl, &num_rvas, debug);
- if(ENCIP_ERROR(ret))
- return ret;
-
- memcpy(tbl->pcl_guid, g_pcl_guid, sizeof(tbl->pcl_guid));
-
- tbl->sealed_blob_size = (size_t)sgx_calc_sealed_data_size(SGX_PCL_GUID_SIZE, SGX_AESGCM_KEY_SIZE);
-
- if(PCL_SEALED_BLOB_SIZE != tbl->sealed_blob_size)
- return ENCIP_ERROR_SEALED_BUF_SIZE;
-
- tbl->num_rvas = num_rvas;
-
- ret = sha256(key, SGX_AESGCM_KEY_SIZE, tbl->decryption_key_hash);
- if(ENCIP_ERROR(ret))
- return ret;
-
- tbl->pcl_state = PCL_CIPHER;
- return ENCIP_SUCCESS;
- }
- void print_usage(IN char* encip_name)
- {
- printf("\n");
- printf("\tUsage: \n");
- printf("\t %s -i <input enclave so file name> -o <output enclave so file name> -k <key file name> [-d]\n",
- encip_name);
- printf("\t -d (optional) prevents the tool from disabling the debug capabilities\n");
- printf("\n");
- }
- encip_ret_e parse_args(
- int argc,
- IN char* argv[],
- OUT char** ifname,
- OUT char** ofname,
- OUT char** kfname,
- OUT bool* debug)
- {
- if(NULL == argv)
- return ENCIP_ERROR_PARSE_INVALID_PARAM;
- char* encip_name = argv[0];
- if((argc != 7 && argc != 8) ||
- NULL == ifname ||
- NULL == ofname ||
- NULL == kfname)
- {
- print_usage(encip_name);
- return ENCIP_ERROR_PARSE_INVALID_PARAM;
- }
- encip_ret_e ret = ENCIP_SUCCESS;
-
- for(int argidx = 1; argidx < argc; argidx++)
- {
- if(!strcmp(argv[argidx],"-d"))
- {
- *debug = true;
- }
- else if(!strcmp(argv[argidx],"-i") && argidx + 1 < argc)
- {
- argidx++;
- *ifname = argv[argidx];
- }
- else if(!strcmp(argv[argidx],"-o") && argidx + 1 < argc)
- {
- argidx++;
- *ofname = argv[argidx]; }
- else if(!strcmp(argv[argidx],"-k") && argidx + 1 < argc)
- {
- argidx++;
- *kfname = argv[argidx];
- }
- else
- {
- ret = ENCIP_ERROR_PARSE_ARGS;
- }
- }
-
- if((ENCIP_SUCCESS != ret) ||
- (NULL == *ifname) ||
- (NULL == *ofname) ||
- (NULL == *kfname))
- {
- print_usage(encip_name);
- ret = ENCIP_ERROR_PARSE_ARGS;
- }
- return ret;
- }
- static encip_ret_e read_file(IN const char* const ifname, OUT uint8_t** buf_pp, OUT size_t* size_out)
- {
- if(NULL == ifname || NULL == buf_pp || NULL == size_out)
- return ENCIP_ERROR_READF_INVALID_PARAM;
- FILE* fin = fopen(ifname, "rb");
- if(NULL == fin)
- return ENCIP_ERROR_READF_OPEN;
- fseek(fin,0,SEEK_END);
- size_t const size = ftell(fin);
- fseek(fin, 0, SEEK_SET);
-
- *buf_pp = (uint8_t*)malloc(size);
- if(NULL == *buf_pp)
- {
- fclose(fin);
- return ENCIP_ERROR_MEM_ALLOC;
- }
- size_t const num_bytes = fread(*buf_pp, 1, size, fin);
- if(num_bytes != size)
- {
- fclose(fin);
- free (*buf_pp);
- return ENCIP_ERROR_READF_READ;
- }
- fclose(fin);
- *size_out = size;
- return ENCIP_SUCCESS;
- }
- static encip_ret_e write_file(IN const char* const ofname, IN uint8_t* buf, size_t size)
- {
- if(NULL == ofname || NULL == buf)
- return ENCIP_ERROR_WRITEF_INVALID_PARAM;
- FILE* fout = fopen(ofname, "wb");
- if(NULL == fout)
- return ENCIP_ERROR_WRITEF_OPEN;
- size_t num_bytes = fwrite(buf, 1, size, fout);
- if(num_bytes != size)
- {
- fclose(fout);
- return ENCIP_ERROR_WRITEF_WRITE;
- }
- fclose(fout);
- return ENCIP_SUCCESS;
- }
- encip_ret_e sha256(IN const void* const buf, size_t buflen, OUT uint8_t* hash)
- {
- encip_ret_e ret = ENCIP_ERROR_FAIL;
- unsigned int digest_len = 0;
-
- if(NULL== buf || NULL== hash)
- return ENCIP_ERROR_SHA_INVALID_PARAM;
- EVP_MD_CTX *mdctx = EVP_MD_CTX_create();
- if(NULL == mdctx)
- return ENCIP_ERROR_SHA_ALLOC;
- if(EVP_SUCCESS != EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL)){
- ret = ENCIP_ERROR_SHA_INIT;
- goto Label_free_context;
- }
- if(EVP_SUCCESS != EVP_DigestUpdate(mdctx, buf, buflen)){
- ret = ENCIP_ERROR_SHA_UPDATE;
- goto Label_free_context;
- }
- if((EVP_SUCCESS != EVP_DigestFinal_ex(mdctx, hash, &digest_len)) ||
- (SGX_SHA256_HASH_SIZE != digest_len)){
- ret = ENCIP_ERROR_SHA_FINAL;
- goto Label_free_context;
- }
- ret = ENCIP_SUCCESS;
- Label_free_context:
- EVP_MD_CTX_destroy(mdctx);
-
- return ret;
- }
- encip_ret_e gcm_encrypt(
- IN unsigned char *plaintext,
- size_t plaintext_len,
- IN unsigned char *aad,
- size_t aad_len,
- IN unsigned char *key,
- IN unsigned char *iv,
- OUT unsigned char *ciphertext,
- OUT unsigned char *tag)
- {
- EVP_CIPHER_CTX *ctx;
- int len;
- encip_ret_e ret = ENCIP_ERROR_GCM_ENCRYPT_INVALID_PARAM;
- if( NULL == plaintext ||
- NULL == key ||
- NULL == iv ||
- NULL == ciphertext ||
- NULL == tag)
- return ENCIP_ERROR_GCM_ENCRYPT_INVALID_PARAM;
-
- if(NULL == (ctx = EVP_CIPHER_CTX_new()))
- return ENCIP_ERROR_ENCRYPT_ALLOC;
-
- if(EVP_SUCCESS != EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL))
- {
- ret = ENCIP_ERROR_ENCRYPT_INIT_EX;
- goto Label_gcm_cleanup;
- }
-
- if(EVP_SUCCESS != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, SGX_AESGCM_IV_SIZE, NULL))
- {
- ret = ENCIP_ERROR_ENCRYPT_IV_LEN;
- goto Label_gcm_cleanup;
- }
-
- if(EVP_SUCCESS != EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv))
- {
- ret = ENCIP_ERROR_ENCRYPT_INIT_KEY;
- goto Label_gcm_cleanup;
- }
-
- if(NULL != aad)
- {
- if(EVP_SUCCESS != EVP_EncryptUpdate(ctx, NULL, &len, aad, (int)aad_len))
- {
- ret = ENCIP_ERROR_ENCRYPT_AAD;
- goto Label_gcm_cleanup;
- }
- }
-
- if(EVP_SUCCESS != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, (int)plaintext_len))
- {
- ret = ENCIP_ERROR_ENCRYPT_UPDATE;
- goto Label_gcm_cleanup;
- }
-
- if(EVP_SUCCESS != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len))
- {
- ret = ENCIP_ERROR_ENCRYPT_FINAL;
- goto Label_gcm_cleanup;
- }
-
- if(EVP_SUCCESS != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, SGX_CMAC_MAC_SIZE, tag))
- {
- ret = ENCIP_ERROR_ENCRYPT_TAG;
- goto Label_gcm_cleanup;
- }
-
- ret = ENCIP_SUCCESS;
-
-
- Label_gcm_cleanup:
- EVP_CIPHER_CTX_free(ctx);
- return ret;
- }
|