libnacl crypto library - how to recover public key

676 Views Asked by At

I use the nacl cryptographic library and need to recover the public key from the secret key. Is this possible?

This is my somewhat naive attempt:

#include <nacl/crypto_scalarmult_curve25519.h>
#include <nacl/crypto_sign.h>

//...

int main(int argc, char **argv)
{
    UCHAR pk[crypto_sign_PUBLICKEYBYTES];
    UCHAR sk[crypto_sign_SECRETKEYBYTES];
    char pkhexbuf[2*crypto_sign_PUBLICKEYBYTES+1];
    char skhexbuf[2*crypto_sign_SECRETKEYBYTES+1];

    if( crypto_sign_keypair( pk, sk ) != 0) {
        printf( "Failed to generate keys." );
        return 1;
    }

    printf( "public key: %s\n", bytes_to_hex( pkhexbuf, pk, sizeof(pk) ) );
    printf( "secret key: %s\n", bytes_to_hex( skhexbuf, sk, sizeof(sk) ) );

    //reset the public key
    memset(pk, '\0', sizeof(pk));

    //recover public key
    if( crypto_scalarmult_curve25519_base(pk, sk) != 0 ) {
        printf( "Failed to derive public key from secret key." );
        return 1;
    }

    printf( "public key: %s\n", bytes_to_hex( pkhexbuf, pk, sizeof(pk) ) );
    return 0;
}

The public keys don't match. Any ideas how this is supposed to be done?

0

There are 0 best solutions below