|
| 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 | +} |
0 commit comments