relic-toolkit / relic

Code
Other
458 stars 179 forks source link

Map a SHA256 hash to bn_t element #210

Closed AyeshaJunejo closed 2 years ago

AyeshaJunejo commented 3 years ago

I want to map an SHA-256 hash to an element in bn_t. I am using the following code to generate the hash of a string.

static int calc_and_compare_hash(const char *str, const unsigned char *expected)
{
    static unsigned char hash[SHA256_DIGEST_LENGTH];
    sha256_context_t sha256;

    sha256_init(&sha256);
    sha256_update(&sha256, (uint8_t*)str, strlen(str));
    sha256_final(&sha256,hash);
    return (memcmp(expected, hash, SHA256_DIGEST_LENGTH) == 0);
}
//
static void test_hashes_sha256_hash_sequence_01(void)
{
    static const char *teststring = "Testing The Hash";
    printf("output of hash %d\n", calc_and_compare_hash(teststring, h01));
}

The next step is to map this hash to a bn_t element and multiply it with the elliptic curve generator to get a new point on the curve using

ec_mul_gen(q, d);

I would appreciate it if someone can help me sort this out?

dfaranha commented 3 years ago

Hi,

Are you doing this to hash to the curve? Why not hash to a point directly (and not leak discrete logs in the process)?

AyeshaJunejo commented 3 years ago

Hi, thanks for your comment. Actually, I want to hash a unique ID with SHA256 and then map it to bn_t and subsequently add a random number to it. Once I get the secret number I want to multiply it with the generator on the curve to generate the public/private key pair. My aim is basically to utilize the unique ID in key pair generation.

dfaranha commented 3 years ago

That sounds better! You can then hash to a byte vector and use bn_read_bin() to convert that to a bn_t.

An example can be found here: https://github.com/relic-toolkit/relic/blob/main/src/cp/relic_cp_vbnn.c#L106

AyeshaJunejo commented 3 years ago

Thanks. I will check out this example.

AyeshaJunejo commented 3 years ago

I have written the code to generate the secret number by modifying the example given in code

I am getting the following errors. I have included

include "relic_md.h" and also tried to run with uint8_t hash[32] but getting the same errors.

  uint8_t hash[RLC_MD_LEN];
  uint8_t hash[32]
  //int len, result = RLC_OK;
  uint8_t *buf = NULL;
  bn_t n, r, sk;
  uint8_t id = 123;
  int id_len = 8;

  /* zero variables */
  bn_null(n);
  bn_null(r);
  bn_null(sk);

    /* initialize variables */
    bn_new(n);
    bn_new(r);
    bn_new(sk);
    /* get order of ECC group */
    ec_curve_get_ord(n);
    /* extract user key from id */
    bn_rand_mod(r, n);

    /* calculate s part of the user key */
        buf = RLC_ALLOCA(uint8_t, id_len);
        if (buf == NULL) {
            printf("no memory \n");
        }
        memcpy(buf, id, id_len);
    //
    md_map(hash, buf, id_len);
    bn_read_bin(sk, hash, RLC_MD_LEN);
    bn_mod(sk, sk, n);
    bn_add(sk, sk, r);
    bn_mod(sk, sk, n);
    printf("new secret number \n");
    bn_print(sk);
    //
    /* free variables */
    bn_free(n);
    bn_free(r);
    RLC_FREE(buf);
cp /home/emdSys/RIOT/tests/pkg_relic_ecdh/bin/nrf52840dk/pkg-build/relic/lib/librelic_s.a /home/emdSys/RIOT/tests/pkg_relic_ecdh/bin/nrf52840dk/relic.a /home/emdSys/RIOT/tests/pkg_relic_ecdh/main.c: In function 'main': /home/emdSys/RIOT/tests/pkg_relic_ecdh/main.c:195:16: error: 'RLC_MD_LEN' undeclared (first use in this function); did you mean 'MD_LEN'? 195 uint8_t hash[RLC_MD_LEN]; ^~~~~~ MD_LEN /home/emdSys/RIOT/tests/pkg_relic_ecdh/main.c:195:16: note: each undeclared identifier is reported only once for each function it appears in /home/emdSys/RIOT/tests/pkg_relic_ecdh/main.c:196:19: error: expected ';' before 'uint8_t' 196 uint8_t hash[32] ^ ; 197 //int len, result = RLC_OK; 198 uint8_t *buf = NULL; ~~~
/home/emdSys/RIOT/tests/pkg_relic_ecdh/main.c:268:9: error: implicit declaration of function 'RLC_ALLOCA' [-Werror=implicit-function-declaration] 268
buf = RLC_ALLOCA(uint8_t, id_len); ^~~~~~ /home/emdSys/RIOT/tests/pkg_relic_ecdh/main.c:268:20: error: expected expression before 'uint8_t' 268 buf = RLC_ALLOCA(uint8_t, id_len); ^~~ /home/emdSys/RIOT/tests/pkg_relic_ecdh/main.c:272:15: error: passing argument 2 of 'memcpy' makes pointer from integer without a cast [-Werror=int-conversion] 272 memcpy(buf, id, id_len); ^~
uint8_t {aka unsigned char}

In file included from /home/emdSys/RIOT/tests/pkg_relic_ecdh/sha2xx_common.h:53, from /home/emdSys/RIOT/tests/pkg_relic_ecdh/sha256.h:54, from /home/emdSys/RIOT/tests/pkg_relic_ecdh/main.c:26: /opt/gcc-arm-none-eabi-10-2020-q4-major/arm-none-eabi/include/string.h:31:35: note: expected 'const void restrict' but argument is of type 'uint8_t' {aka 'unsigned char'} 31 | void memcpy (void __restrict, const void __restrict, size_t); | ^ /home/emdSys/RIOT/tests/pkg_relic_ecdh/main.c:285:3: error: implicit declaration of function 'RLC_FREE' [-Werror=implicit-function-declaration] 285 | RLC_FREE(buf); | ^~~~ /home/emdSys/RIOT/tests/pkg_relic_ecdh/main.c:245:17: error: unused variable 's' [-Werror=unused-variable] 245 | const char* s = "IDBRD23450"; | ^ /home/emdSys/RIOT/tests/pkg_relic_ecdh/main.c:195:11: error: unused variable 'hash' [-Werror=unused-variable] 195 | uint8_t hash[RLC_MD_LEN]; | ^~~~ cc1: all warnings being treated as errors /home/emdSys/RIOT/Makefile.base:107: recipe for target '/home/emdSys/RIOT/tests/pkg_relic_ecdh/bin/nrf52840dk/application_pkg_relic_ecdh/main.o' failed make[1]: [/home/emdSys/RIOT/tests/pkg_relic_ecdh/bin/nrf52840dk/application_pkg_relic_ecdh/main.o] Error 1 /home/emdSys/RIOT/tests/pkg_relic_ecdh/../../Makefile.include:643: recipe for target 'application_pkg_relic_ecdh.module' failed make: [application_pkg_relic_ecdh.module] Error 2

dfaranha commented 3 years ago

It seems you are running an older version of RELIC under RIOT.

AyeshaJunejo commented 3 years ago

Yes, I intend to use RELIC with riot but did not know it is old. I have recently downloaded RELIC from GitHub and installed it as well. How can I make riot use the newer version of RELIC?

dfaranha commented 3 years ago

That unfortunately goes beyond my expertise, but you can try the Discussion Group to see if someone there can help.

AyeshaJunejo commented 3 years ago

okay, thanks for your help :+1: