1 (edited by muyouyuwan 2017-08-01 23:49:18)

Topic: Generated HMAC Digest length is not fixed 256 bits

I implement a generate hmac digest function, but i find the length of digest is not fixed. It changes as the plain text length changes.
Please have a look my code and if there is some incorrect please point it out.

Post's attachments

ViwiFileEncryption.cpp 2.86 kb, 2 downloads since 2017-08-02 

You don't have the permssions to download the attachments of this post.

Share

Re: Generated HMAC Digest length is not fixed 256 bits

Hi muyouyuwan,

You are using a %s to print the "Hex filled array" and a strlen operation to detect the length. Keep in mind all string operations will treat a 0 value hex as a NULL terminator and try to interpret the hex values as ASCII. I suspect you are encountering the following:

EXAMPLE of Hash:

12bad3007839... up to sha256 len
       |
       String operations will see the hex 0 and think it is the end of the string

Here is a simple example program to show this difference:

PROGRAM:
  #include <stdio.h>                                                               
  #include <string.h>                                                              
                                                                                   
  int main(void)                                                                   
  {                                                                                
      int i;                                                                       
      unsigned char x[] = {                                                        
          0x12, 0x34, 0x45, 0x1b, 0xab, 0x89, 0x99,                                
          0x54, 0x78, 0x00, 0x12, 0x34, 0x45, 0x1b                                 
      };                                                                           
                                                                                   
      printf("Hash1: %s, length %d\n", x, (int) strlen((const char*)x));           
                                                                                   
      printf("Hash2: ");                                                           
      for (i = 0; i < (int)sizeof(x); i++)                                         
          printf("%02x", x[i]);                                                    
      printf(", length %d\n", (int)sizeof(x));                                     
                                                                                  
      return 0;                                                                    
  }
OUTPUT:
Hash1: 4E??Tx, length 9
Hash2: 1234451bab89995478001234451b, length 14

Please try the same thing but iterate over the HEX ARRAY using a for loop and printing each value rather than using string operations on hexidecimal values which are very likely to contain 0's.

SIDE NOTE: (Do not use sizeof() on a pointer as it will return the size of the pointer and not the size of the array)

Let me know your results!


Warm Regards,

Kaleb

Re: Generated HMAC Digest length is not fixed 256 bits

Hi Kaleb,
Thanks for your kindly reply. I printed the correct digest.

Best Regards.

Share

Re: Generated HMAC Digest length is not fixed 256 bits

Hi muyouyuwan,

No problem, glad to hear you were able to get out the expected digest(s)!

Let us know if anything else comes up, we are always happy to help in any way we can!


Sincere Regards,

Kaleb