Question

I'm trying to confirm/get the unsigned int (base 10 / decimal) value of an hex'd mpi value. I'm using the following code:

#include <gcrypt.h>
#include <stdio.h>

static gcry_mpi_t privkey = NULL;

int main(){

    char *parmstr = "48BFDA215C31A9F0B226B3DB11F862450A0F30DA";

    printf("starting program\n");

    gcry_mpi_scan(&privkey, GCRYMPI_FMT_HEX, (char *)parmstr, 0, NULL);

    printf("printing hashed (?) mpi\n");
    gcry_mpi_dump(privkey);
    printf("\n");

    size_t gablen;
    unsigned char *result;
    /* get length */
    gcry_mpi_print (GCRYMPI_FMT_USG, NULL, 0, &gablen, privkey);
    result = gcry_malloc_secure(gablen);
    /* get actual value */
    gcry_mpi_print (GCRYMPI_FMT_USG, result, gablen, NULL, privkey);

    /* print actual value */
    printf("result: %s\n", result);

    printf("finished\n");
}

and i'm getting the following result:

    $ ./foo
    starting program
    printing hashed (?) mpi
     48BFDA215C31A9F0B226B3DB11F862450A0F30DA
    result: H¿Ú!\1©ð²&³ÛøbE
0Ú
    finished

I would the 'result: ' line to print the actual unsigned int (base 10 / decimal) value.

The private key is taking from the Off-The-Record Pidgin plug-in that i'm trying to work with.

EDIT:

can anybody confirm that the actual unsigned int (base 10 / decimal) value should be

415325779662433871844955547383752003988573073626

I could probably update the program to create a new mpi in gcrypt with this value and see if the HEX value is the same as what i already have. I will do this later today.

EDIT 2:

So i'm trying to do the following to print the HEX value of the int value mentioned above. Something is going wrong:

gcry_mpi_t cript_prime;
char buffer[50] = {0};
char number[50] = {0};

cript_prime = gcry_mpi_new(50);

strcpy(number,"415325779662433871844955547383752003988573073626");
gcry_mpi_scan(&cript_prime,GCRYMPI_FMT_USG,number,sizeof(number),NULL);

gcry_mpi_print(GCRYMPI_FMT_USG,buffer,sizeof(buffer),NULL,cript_prime);

printf("The number tested is: %s\n",buffer);

printf("trying to convert to HEX\n");

/* get actual value */
gcry_mpi_print (GCRYMPI_FMT_HEX, buffer, sizeof(buffer), NULL, cript_prime);
/* print actual value */
printf("result: %s\n", buffer);

The output is:

result: 48BFDA215C31A9F0B226B3DB11F862450A0F30DA
The number tested is: 415325779662433871844955547383752003988573073626
trying to convert to HEX
result: 415325779662433871844955547383752003988573073626

EDIT 3:

I updated the post a bit, basically i'm trying to print the base10 decimal value of an hex value that is generated by the gcrypt library. I'm looking for this value to confirm an implementation that i made to read these values. I was looking for a gcrypt function to achieve this. It seems that gcrypt doesn't supports this?

Was it helpful?

Solution 2

Here is a working script, the buffer was too small

#include <gcrypt.h>
#include <stdio.h>

int main(){
    gcry_error_t err;
    gcry_mpi_t cript_prime;
    unsigned char buffer[401] = {0};
    char number[101] = {0};

    cript_prime = gcry_mpi_new(101);

    strcpy(number, "415325779662433871844955547383752003988573073626");
    gcry_mpi_scan(&cript_prime, GCRYMPI_FMT_USG, number, sizeof(number), NULL);

    err = gcry_mpi_print(GCRYMPI_FMT_USG, buffer, sizeof(buffer), NULL, cript_prime);

    if (err != 0){
      printf("error: %d\n", err);
      return -1;
    }

    printf("The number tested is: %s\n",buffer);

    printf("trying to convert to HEX\n");

    /* get actual value */
    err = gcry_mpi_print(GCRYMPI_FMT_HEX, buffer, sizeof(buffer), NULL, cript_prime);

    if (err != 0){
      gcry_err_code_t code = gcry_err_code(err);
      printf("error: %d\n", code);
      return -1;
    }

    /* print actual value */
    printf("result: %s\n", buffer);

    printf("finished\n");

    return 0;
}

I also confirmed my thoughts using this website: http://www.unitconversion.org/numbers/base-10-to-base-16-conversion.html

This will show that hex (base-16) 48BFDA215C31A9F0B226B3DB11F862450A0F30DA actual is 415325779662433762080404206464628666084640806848 (base-10)

OTHER TIPS

result[1] = (gablen >> 24) & 0xff;

This line puts a nul byte into the byte at offset 1 into result. I don't see where you ever initialize the byte at offset zero, but the bytes at offsets 2 and later are past the end of the string you've constructed. The string it's formatting starts at result+5, so that's what you should print here:

gcry_mpi_print (GCRYMPI_FMT_USG, result + 5, gablen, NULL, privkey);
...
printf("result: %s\n", result + 5);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top