Question

I have two uint8_t pointers in my program and I want to compare the values of those. I don't have good idea to deal with unsigned ints. Here is my code

static void bacast_signed_message()
{
  uint8_t *M = malloc(MAX_M_LEN*sizeof(uint8_t));//uint8_t M[MAX_M_LEN];//uint8_t *M;
  int M_len = MAX_M_LEN;;

  uint8_t *C = malloc((2*KEYDIGITS*NN_DIGIT_LEN + 1 + MAX_M_LEN + HMAC_LEN)*sizeof(uint8_t));   
  int C_len; 
  //uint8_t C[2*KEYDIGITS*NN_DIGIT_LEN + 1 + MAX_M_LEN + HMAC_LEN]; 

  uint8_t *dM = malloc(MAX_M_LEN*sizeof(uint8_t));  //uint8_t dM[MAX_M_LEN];
  int dM_len = MAX_M_LEN;

  random_data(M, MAX_M_LEN);

  printf("C before encrypt %p\n",*C);
  printf("M before encrypt %p\n",*M);
  printf("dM before encrypt %p\n",*dM);

  C_len = encrypt(C, (2*KEYDIGITS*NN_DIGIT_LEN + 1 + M_len + HMAC_LEN), M, M_len, &pbkey_alice);
  //encrypt(uint8_t *C, int C_len, uint8_t *M, int M_len, Point *PublicKey);

  printf("C after encrypt %p\n",*C);
  printf("M after encrypt %p\n",*M);
  printf("dM after encrypt %p\n",*dM);

  dM_len = decrypt(dM, dM_len, C, C_len, prKey_alice);   
  //decrypt(uint8_t *M, int M_len, uint8_t *C, int C_len, NN_DIGIT *d);

  printf("C after decrypt %p\n",*C);
  printf("M after decrypt %p\n",*M);
  printf("dM after decrypt %p\n",*dM);

  printf("C_len = %i , M_len = %i\n",C_len,M_len);

  if (dM == M){printf("Works\n");}
  else{printf("Not Works\n");}
}

and this is the out put I got

C before encrypt 0x40
M before encrypt 0x28
dM before encrypt 0x70
C after encrypt 0x4
M after encrypt 0x28
dM after encrypt 0x70
C after decrypt 0x4
M after decrypt 0x28
dM after decrypt 0x28
C_len = 102 , M_len = 41
Not Works

And if I changed private_key(no change of public key) then It looks like this

C before encrypt 0x40
M before encrypt 0x28
dM before encrypt 0x70
C after encrypt 0x4
M after encrypt 0x28
dM after encrypt 0x70
C after decrypt 0x4
M after decrypt 0x28
dM after decrypt 0x70
C_len = 102 , M_len = 41
Not Works

That mean dM has not changed no. But I am not sure about this. If I use %u instead of %p it will give me totally different answers(it says if we use %u it'll convert to decimal). Please give me some advice, and tell me whats wrong with this.

Thanks in advance

Was it helpful?

Solution

Possible ways to understand your question

Your code if (dM == M) currently compares pointers. So you're currently checking whether or not the two pointers indicate the same chunk of memory.

Your question title suggests that you want to compare single numbers. After all, an uint8_t is just a single number in the range 0 - 255. If you have pointers to such numbers, you'd have to dereference them to get at the actual numbers, like if (*dM == *M).

But your code suggests that you actually want to compare memory regions. Dereferencing a pointer to an array is the same as accessing the first element of the array (i.e. the same as if (dM[0] == M[0])), which probably is not what you want.

Comparing memory regions

You can use memcmp to compare the content of memory regions:

if (dM_len == M_len && memcmp(dM, M, dM_len) == 0)

This means “if the lengths are the same, and the content of the two memory regions is the same as well”. If the result of memcmp is nonzero, its sign provides information as to which value is considered greater.

Printing

Note that the %p in your printf calls is probably wrong: %p is for printing pointers, but as you're dereferencing them in the function call, you're actually printing values. So %u is much more suitable. If you want to print the content of e.g. M, you'll have to use a loop like this:

for (i = 0; i != M_len; ++i) {
  if (i%32 == 0 && i != 0) printf("\n"); /* wrap lines */
  printf("%02x", M[i]);
}
printf("\n");

This will print a hexadecimal representation of your data, with 32 bytes (= 64 hexadecimal digits) per line. The %02x format instructs printf to allways use two digits for every byte, padding with zero as required.

OTHER TIPS

There are specific macros for printing the "stdint.h" types. For instance:

#include <inttypes.h>

...
uint8_t u8 = 5;
printf("%"PRIu8, u8); // unsigned 
printf("%"PRIx8, u8); // hexadecimal

Hence you should change e.g:

printf("C after decrypt %p\n",*C); 

to

printf("C after decrypt %"PRIu8"\n",*C);

Note that %p is for printing pointers (addresses), and not the-value-pointed-to. And since you de-reference C you probably are not getting the results you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top