CipherX
CipherX

Reputation: 371

Set OpenSSL parameters with a void function

I am trying to set some parameters (not only the private key, but also EC_POINT and so on) leveraging a void function as following. By the way, I don't understand why the variable my_s is not set properly. Indeed I have the following output:

423360B59A5950C8E18840C7A5B91106E0D6F88F

0

I would to have the same output in the main function that now I'm not able to achieve. Can you explain why? It is a matter of pointers?

#include <openssl/obj_mac.h>
#include <openssl/ec.h>
#include <openssl/rand.h>
#include <openssl/bn.h>
#include <openssl/sha.h>
#include <openssl/evp.h>
#include <openssl/err.h>

void set_keys(BIGNUM *s, BN_CTX *ctx, EC_GROUP *c, const EC_POINT *G, BIGNUM *t);

int main(){

    BN_CTX      *ctx;
    EC_GROUP    *group;
    const EC_POINT *G;
    BIGNUM *n = NULL;

    ctx = BN_CTX_secure_new();
    n   = BN_new();
    group   = EC_GROUP_new_by_curve_name(NID_secp160r1);
    G       = EC_GROUP_get0_generator(group);
    EC_GROUP_get_order(group, n, ctx);
    EC_GROUP_precompute_mult(group, ctx);

    BIGNUM *my_s = BN_new();
    set_keys(my_s, ctx, group, G, n);
    
    putc('\n', stdout);
    BN_print_fp(stdout, my_s);
    putc('\n', stdout);

}

void set_keys(BIGNUM *s, BN_CTX *ctx, EC_GROUP *c, const EC_POINT *G, BIGNUM *t){
    s = BN_new();
    BN_rand_range(s, t);
    BN_print_fp(stdout, s);
    putc('\n', stdout);
}

Upvotes: 0

Views: 69

Answers (1)

dbush
dbush

Reputation: 224997

You're passing the value of my_s to set_keys. So any change you make to s in set_keys isn't reflected in the caller.

You need to pass the address of my_s and change the type of the s parameter to match:

void set_keys(BIGNUM **s, BN_CTX *ctx, EC_GROUP *c, const EC_POINT *G, BIGNUM *t){
    *s = BN_new();
    BN_rand_range(*s, t);
    BN_print_fp(stdout, *s);
    putc('\n', stdout);
}

...

BIGNUM *my_s;
set_keys(&my_s, ctx, group, G, n);

Upvotes: 2

Related Questions