Skip to content

Commit 1cde0d8

Browse files
committed
Add tests for crypto_core_ristretto255_from_string_ro
Using test vectors from RFC 9497. Suggested by @wmcelderry, thanks! Fixes #1515
1 parent babd0c3 commit 1cde0d8

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ test/default/core_ed25519
111111
test/default/core_ed25519_h2c
112112
test/default/core_keccak1600
113113
test/default/core_ristretto255
114+
test/default/core_ristretto255_h2c
114115
test/default/core1
115116
test/default/core2
116117
test/default/core3

test/default/Makefile.am

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ EXTRA_DIST = \
3131
core_ed25519.exp \
3232
core_ed25519_h2c.exp \
3333
core_ristretto255.exp \
34+
core_ristretto255_h2c.exp \
3435
core1.exp \
3536
core2.exp \
3637
core3.exp \
@@ -126,6 +127,7 @@ DISTCLEANFILES = \
126127
core_ed25519.res \
127128
core_ed25519_h2c.res \
128129
core_ristretto255.res \
130+
core_ristretto255_h2c.res \
129131
core1.res \
130132
core2.res \
131133
core3.res \
@@ -379,6 +381,9 @@ core_ed25519_h2c_LDADD = $(TESTS_LDADD)
379381
core_ristretto255_SOURCE = cmptest.h core_ristretto255.c
380382
core_ristretto255_LDADD = $(TESTS_LDADD)
381383

384+
core_ristretto255_h2c_SOURCE = cmptest.h core_ristretto255_h2c.c
385+
core_ristretto255_h2c_LDADD = $(TESTS_LDADD)
386+
382387
core1_SOURCE = cmptest.h core1.c
383388
core1_LDADD = $(TESTS_LDADD)
384389

@@ -585,6 +590,7 @@ TESTS_TARGETS += \
585590
core_ed25519 \
586591
core_ed25519_h2c \
587592
core_ristretto255 \
593+
core_ristretto255_h2c \
588594
kdf_hkdf \
589595
pwhash_scrypt \
590596
pwhash_scrypt_ll \
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#define TEST_NAME "core_ristretto255_h2c"
2+
#include "cmptest.h"
3+
4+
/*
5+
* Test vectors derived from RFC 9497 (OPRFs Using Prime-Order Groups)
6+
*/
7+
8+
typedef struct TestData_ {
9+
unsigned char mode;
10+
const char *input_hex;
11+
const char expected_hex[65];
12+
} TestData;
13+
14+
static TestData test_data[] = {
15+
/* OPRF mode 0, RFC 9497 A.1.1, Test Vector 1 */
16+
{ 0x00, "00",
17+
"5873db2e5f8f4f544ce3e574c74c487f03bc64a2cf63b7c913908091aab03357" },
18+
/* OPRF mode 0, RFC 9497 A.1.1, Test Vector 2 */
19+
{ 0x00, "5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a",
20+
"743d49d207339ae67aef8f4d0777744e5a604b94df5cbcc13e3dd87e79985a39" },
21+
/* VOPRF mode 1, RFC 9497 A.1.2, Test Vector 1 */
22+
{ 0x01, "00",
23+
"868c9140811d0dc38291c7bbc0bd8f301d0d4e8b15f65e442184a233b8791703" },
24+
/* VOPRF mode 1, RFC 9497 A.1.2, Test Vector 2 */
25+
{ 0x01, "5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a",
26+
"caff66fcdc41da4d87ccc72aaac70c6e267a4b55c3dc9489bb365a70a04f1a52" },
27+
};
28+
29+
#define H2CHASH crypto_core_ristretto255_H2CSHA512
30+
31+
int
32+
main(void)
33+
{
34+
unsigned char *p, *expected, *input;
35+
char *p_hex, *expected_hex;
36+
char *oversized_ctx;
37+
size_t i;
38+
size_t oversized_ctx_len = 500U;
39+
size_t input_len;
40+
41+
/*
42+
* DST = "HashToGroup-OPRFV1-" || mode_byte || "-ristretto255-SHA512"
43+
* The mode byte at offset 19 is patched per test vector.
44+
*/
45+
unsigned char dst[] = {
46+
'H','a','s','h','T','o','G','r','o','u','p','-',
47+
'O','P','R','F','V','1','-', 0x00, '-',
48+
'r','i','s','t','r','e','t','t','o','2','5','5','-',
49+
'S','H','A','5','1','2'
50+
};
51+
const size_t dst_len = sizeof dst;
52+
const size_t mode_offset = 19U;
53+
54+
p = (unsigned char *) sodium_malloc(crypto_core_ristretto255_BYTES);
55+
expected = (unsigned char *) sodium_malloc(crypto_core_ristretto255_BYTES);
56+
input = (unsigned char *) sodium_malloc(256U);
57+
p_hex = (char *) sodium_malloc(crypto_core_ristretto255_BYTES * 2U + 1U);
58+
expected_hex = (char *) sodium_malloc(crypto_core_ristretto255_BYTES * 2U + 1U);
59+
60+
for (i = 0U; i < (sizeof test_data) / (sizeof test_data[0]); i++) {
61+
dst[mode_offset] = test_data[i].mode;
62+
63+
input_len = strlen(test_data[i].input_hex) / 2U;
64+
sodium_hex2bin(input, 256U,
65+
test_data[i].input_hex, strlen(test_data[i].input_hex),
66+
NULL, NULL, NULL);
67+
sodium_hex2bin(expected, crypto_core_ristretto255_BYTES,
68+
test_data[i].expected_hex, 64U, NULL, NULL, NULL);
69+
70+
if (crypto_core_ristretto255_from_string_ro(
71+
p, dst, dst_len, input, input_len, H2CHASH) != 0) {
72+
printf("crypto_core_ristretto255_from_string_ro() failed (test #%u)\n",
73+
(unsigned) i);
74+
continue;
75+
}
76+
if (memcmp(p, expected, crypto_core_ristretto255_BYTES) != 0) {
77+
sodium_bin2hex(expected_hex, crypto_core_ristretto255_BYTES * 2U + 1U,
78+
expected, crypto_core_ristretto255_BYTES);
79+
sodium_bin2hex(p_hex, crypto_core_ristretto255_BYTES * 2U + 1U,
80+
p, crypto_core_ristretto255_BYTES);
81+
printf("Test #%u failed - expected [%s] got [%s]\n",
82+
(unsigned) i, expected_hex, p_hex);
83+
}
84+
}
85+
86+
if (crypto_core_ristretto255_from_string_ro(
87+
p, NULL, 0U, (const unsigned char *) "msg", 3U, H2CHASH) != 0 ||
88+
crypto_core_ristretto255_from_string_ro(
89+
p, (const unsigned char *) "", 0U, guard_page, 0U, H2CHASH) != 0) {
90+
printf("Failed with empty parameters\n");
91+
}
92+
93+
oversized_ctx = (char *) sodium_malloc(oversized_ctx_len);
94+
memset(oversized_ctx, 'X', oversized_ctx_len);
95+
crypto_core_ristretto255_from_string_ro(
96+
p, (const unsigned char *) oversized_ctx, oversized_ctx_len - 1U,
97+
(const unsigned char *) "msg", 3U, H2CHASH);
98+
sodium_bin2hex(p_hex, crypto_core_ristretto255_BYTES * 2U + 1U,
99+
p, crypto_core_ristretto255_BYTES);
100+
printf("RO with oversized context: %s\n", p_hex);
101+
102+
sodium_free(oversized_ctx);
103+
sodium_free(expected_hex);
104+
sodium_free(p_hex);
105+
sodium_free(input);
106+
sodium_free(expected);
107+
sodium_free(p);
108+
109+
printf("OK\n");
110+
111+
return 0;
112+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
RO with oversized context: eedf0384646af8a94b9c8153490ce78f2e79f301800b33f4bfa655bcdc4fb135
2+
OK

0 commit comments

Comments
 (0)