2

I have this function which prints the hmac of a string:

char *print_bits(unsigned char *bits, size_t len)
{
  int i;
  char buf[64], *put = buf;
  for(i = 0; i < len; i++){
  put += snprintf(put, 64, "%02x", bits[i]);
}
return put;
}

I want to use the result of printf and it send to my client app:

char buffer[64]
len = strlen(buffer);
    if(len < BUFFERSIZE)
         {
    gen_hmac_sha256(global_eid, (unsigned char *) buffer, len + 1);
    get_hmac_sha256(global_eid, hmac_sha256_out, HMAC_SHA256_LEN);
    printf("App.cpp: hmac sha256 hash: ");
    char *buffer2 = print_bits(hmac_sha256_out, 32);
    printf("\n");
    
    send(new_sock, buffer2, 64, 0);
}

However, send is not sending the result to the client. Thanks in advance for your help.

Shegs01
  • 35
  • 5
  • 1
    `print_bits` exhibits undefined behavior, by way of reaching the closing brace of a non-void function without encountering a `return` statement. – Igor Tandetnik Jul 20 '20 at 04:01
  • I have tried to return a char value, but that didn't work either. Can you show me an example? Thanks – Shegs01 Jul 20 '20 at 04:04
  • It's not clear to me what you are trying to achieve in `print_bits` – Igor Tandetnik Jul 20 '20 at 04:05
  • Use `sprintf` if you want to write characters to a string buffer. `printf` writes to the standard output stream. Since you've marked this as C++, you might instead consider using a `std::ostringstream` along with some I/O manipulators. – paddy Jul 20 '20 at 04:05
  • 1
    `printf` writes to the standard output stream, not your sock. If you want to send data to your sock that has the same layout as a printf result, then you could use the `snprintf` function to createa memory buffer with that layout and then send that to your sock – M.M Jul 20 '20 at 04:07
  • print_bits is a function call that print the digest of a string in SGX. The code is long that is why I only showed the relevant points. – Shegs01 Jul 20 '20 at 04:12
  • Hey M.M, thanks. I am relatively new to c++. I did try snprintf but keep getting errors: connot convert char** to char* – Shegs01 Jul 20 '20 at 04:15
  • Try looking at some sample programs for snprintf – M.M Jul 20 '20 at 04:49
  • @M.M I got it to work using snprintf. Thanks. I am unsure if I can post the answer since I asked the question. Thanks again. – Shegs01 Jul 20 '20 at 06:31
  • @Shegs01 yes it's fine to answer your own question , it may help other people with the same question – M.M Jul 20 '20 at 06:42

2 Answers2

0

In your function print_bits, you are returning address to local variable/array. This will lead to undefined behaviour as you will be pointing to a memory region that you no longer have ownership and some other function/code can modify it.

Since, you are using C++, you can write the same code using std::string.

std::string get_bits_str(unsigned char *bits, size_t len)
{
  std::stringstream stream;
  for(i = 0; i < len; i++){
      stream << std::hex << bits[i] << " ";
  return stream.str();
}

std::string buf = print_bits(hmac_sha256_out, 32);
printf("\n");
    
send(new_sock, buf.c_str(), 64, 0);
abhiarora
  • 9,743
  • 5
  • 32
  • 57
  • thanks, but this was not printing the hmac string. However, I was able to use snprintf to send the hmac string to the client. – Shegs01 Jul 20 '20 at 06:28
  • I got but it's safer to use `std::string`. Any reason you want to stick to `char` array? – abhiarora Jul 20 '20 at 06:31
  • No specific reason, the code is long and intertwined so changing that would probabaly cause problems for me. I am new to c++. – Shegs01 Jul 20 '20 at 06:32
0

Create a buffer to hold the hmac string gotten from the print_bits function using snprintf as shown below:

char buf [64];
char *print_bits(unsigned char *bits, size_t len)
{
   int i;
   
   for(i = 0; i < len; i++){
     snprintf(buf+i*2, 3, "%02x", bits[i]);
    }
    return buf;
}

Then in the main function, use the send() system call as shown below to send from server to the client.

len = strlen(buffer);
if(len < BUFFERSIZE)
 {
     gen_hmac_sha256(global_eid, (unsigned char *) buffer, len + 1);
     get_hmac_sha256(global_eid, hmac_sha256_out, HMAC_SHA256_LEN);
     printf("App.cpp: hmac sha256 hash: ");
     send(new_sock, print_bits(hmac_sha256_out, 32), 64, 0); 
 }

Note that the hmac algorithm itself is executed inside Intel SGX and has not been shown.

Shegs01
  • 35
  • 5