Closed AyeshaJunejo closed 2 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)?
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.
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
Thanks. I will check out this example.
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
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); | ^ |
buf = RLC_ALLOCA(uint8_t, id_len); | ^ |
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
It seems you are running an older version of RELIC under RIOT.
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?
That unfortunately goes beyond my expertise, but you can try the Discussion Group to see if someone there can help.
okay, thanks for your help :+1:
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.
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?