123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- #include "PostLAMessaging.h"
- #include "sgx_trts.h"
- #include "crypto.h"
- #include <unistd.h>
- #include <stdio.h>
- uint32_t PostLAMessaging::aes_gcm_wrapper(int enc, uint8_t* plaintext, uint32_t plaintext_length, uint8_t* ciphertext, uint32_t* ciphertext_length)
- {
- uint32_t actual_plaintext_length=plaintext_length;
- uint8_t tag[16];uint32_t counter, return_status;
- uint8_t iv[12];
- if(enc == 0)
- {
- for(counter=0;counter<16;counter++)
- tag[counter]=plaintext[counter+plaintext_length-16];
- for(counter=0;counter<12;counter++)
- iv[counter]=plaintext[counter+plaintext_length-28];
- actual_plaintext_length-=28;
- }
- else
- {
- return_status=sgx_read_rand(iv, 12);
- if(return_status != 0)
- return return_status;
- }
- return_status = aes_gcm_128(enc, key, iv, plaintext, actual_plaintext_length, ciphertext, ciphertext_length, tag);
- if(enc == 1 && return_status == 0)
- {
- for(counter=0;counter<12;counter++)
- ciphertext[counter + *ciphertext_length] = iv[counter];
- for(counter=0;counter<16;counter++)
- ciphertext[counter + 12 + *ciphertext_length] = tag[counter];
- *ciphertext_length=*ciphertext_length + 28;
- }
- return return_status;
- }
- uint32_t PostLAMessaging::send_secure_msg(uint8_t* input, uint32_t input_size)
- {
- uint8_t* output;
- uint32_t output_size, ret;
- output = (unsigned char*) malloc(input_size + 28);
- ret = aes_gcm_wrapper(1, input, input_size, output, &output_size );
- if(ret != 0)
- return ret;
- size_t post_la_bytes_written = write(fd, output, output_size);
- printf("Wrote the hash and the tag to the decryptor socket.\n Wrote this many bytes: %d\n", post_la_bytes_written); fflush(stdout);
- if(close(fd)!= 0)
- {
- printf("Error in closing the socket connection.\n"); fflush(stdout); return 0xfd;
- }
-
-
- return 0;
- }
- void PostLAMessaging::set_la_symmetric_key(uint8_t* given_key) {
- uint32_t counter;
- for(counter=0; counter<16; counter++)
- {
- key[counter] = given_key[counter];
- }
- }
- void PostLAMessaging::set_fd(int given_fd)
- {
-
- fd = given_fd;
- }
|