Skip to content

Commit f42d13e

Browse files
authored
Various fixes in node, add tests for p256 signature (#1716)
1 parent d54065a commit f42d13e

File tree

12 files changed

+53
-11
lines changed

12 files changed

+53
-11
lines changed

crypto/Ed25519.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ Result<SecureString> Ed25519::compute_shared_secret(const PublicKey &public_key,
237237
BigNum::mod_sub(y2, p, y2, p, context);
238238

239239
BigNum inverse_y_plus_1;
240-
BigNum::mod_inverse(inverse_y_plus_1, y2, p, context);
240+
TRY_STATUS(BigNum::mod_inverse(inverse_y_plus_1, y2, p, context));
241241

242242
BigNum u;
243243
BigNum::mod_mul(u, y, inverse_y_plus_1, p, context);

crypto/ellcurve/p256.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ td::Status p256_check_signature(td::Slice data, td::Slice public_key, td::Slice
5858
SCOPE_EXIT {
5959
EVP_MD_CTX_free(md_ctx);
6060
};
61-
if (EVP_DigestVerifyInit(md_ctx, nullptr, nullptr, nullptr, pkey) <= 0) {
61+
if (EVP_DigestVerifyInit(md_ctx, nullptr, EVP_sha256(), nullptr, pkey) <= 0) {
6262
return td::Status::Error("Can't init DigestVerify");
6363
}
6464
ECDSA_SIG* sig = ECDSA_SIG_new();
@@ -71,7 +71,10 @@ td::Status p256_check_signature(td::Slice data, td::Slice public_key, td::Slice
7171
BIGNUM* r = BN_bin2bn(buf, 33, nullptr);
7272
std::copy(signature.ubegin() + 32, signature.ubegin() + 64, buf + 1);
7373
BIGNUM* s = BN_bin2bn(buf, 33, nullptr);
74+
CHECK(r != nullptr && s != nullptr);
7475
if (ECDSA_SIG_set0(sig, r, s) != 1) {
76+
BN_free(r);
77+
BN_free(s);
7578
return td::Status::Error("Invalid signature");
7679
}
7780
unsigned char* signature_encoded = nullptr;

crypto/test/fift.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,7 @@ TEST(Fift, test_secp256k1) {
175175
TEST(Fift, test_get_extra_balance) {
176176
run_fift("get_extra_balance.fif");
177177
}
178+
179+
TEST(Fift, test_p256) {
180+
run_fift("p256.fif");
181+
}

crypto/test/fift/p256.fif

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"Asm.fif" include
2+
"FiftExt.fif" include
3+
4+
// Test vectors from https://datatracker.ietf.org/doc/html/rfc6979#appendix-A.2.5
5+
6+
x{73616d706c65} // "sample"
7+
x{EFD48B2AACB6A8FD1140DD9CD45E81D69D2C877B56AAF991C34D0EA84EAF3716F7CB1C942D657C41D436C7A1B6E29F65F3E900DBB9AFF4064DC4AB2F843ACDA8}
8+
x{0360FED4BA255A9D31C961EB74C6356D68C049B8923B61FA6CE669622E60F29FB6}
9+
10+
<{ P256_CHKSIGNS }>s 64 runvmx .s // -1 0
11+
drop drop
12+
13+
x{74657374} // "test"
14+
x{F1ABB023518351CD71D881567B1EA663ED3EFCF6C5132B354F28D3B0B7D38367019F4113742A2B14BD25926B49C649155F267E60D3814B4C0CC84250E46F0083}
15+
x{0360FED4BA255A9D31C961EB74C6356D68C049B8923B61FA6CE669622E60F29FB6}
16+
17+
<{ P256_CHKSIGNS }>s 64 runvmx .s // -1 0
18+
drop drop
19+
20+
x{74657374} // "test"
21+
x{83910E8B48BB0C74244EBDF7F07A1C5413D61472BD941EF3920E623FBCCEBEB68DDBEC54CF8CD5874883841D712142A56A8D0F218F5003CB0296B6B509619F2C}
22+
x{0360FED4BA255A9D31C961EB74C6356D68C049B8923B61FA6CE669622E60F29FB6}
23+
24+
<{ P256_CHKSIGNS }>s 64 runvmx .s // 0 0
25+
drop drop

lite-client/lite-client.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,12 @@ void TestNode::got_server_mc_block_id(ton::BlockIdExt blkid, ton::ZeroStateIdExt
422422
}
423423
td::TerminalIO::out() << "latest masterchain block known to server is " << blkid.to_str();
424424
if (created > 0) {
425-
td::TerminalIO::out() << " created at " << created << " (" << now() - created << " seconds ago)\n";
425+
auto time = now();
426+
if (time >= created) {
427+
td::TerminalIO::out() << " created at " << created << " (" << time - created << " seconds ago)\n";
428+
} else {
429+
td::TerminalIO::out() << " created at " << created << " (" << created - time << " seconds in the future)\n";
430+
}
426431
} else {
427432
td::TerminalIO::out() << "\n";
428433
}

storage/MicrochunkTree.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ td::Result<td::Ref<vm::Cell>> MicrochunkTree::get_proof(td::uint64 l, td::uint64
188188
if (!torrent.inited_info()) {
189189
return td::Status::Error("Torrent info is not ready");
190190
}
191-
if (!torrent.get_info().piece_size % MICROCHUNK_SIZE != 0) {
191+
// piece_size must be an exact multiple of MICROCHUNK_SIZE
192+
if ((torrent.get_info().piece_size % MICROCHUNK_SIZE) != 0) {
192193
return td::Status::Error("Invalid piece size in torrent");
193194
}
194195
td::Ref<vm::Cell> root_raw = vm::CellSlice(vm::NoVm(), root_proof_).prefetch_ref();

tdutils/td/utils/BigNum.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,12 @@ void BigNum::mod_mul(BigNum &r, BigNum &a, BigNum &b, const BigNum &m, BigNumCon
291291
LOG_IF(FATAL, result != 1);
292292
}
293293

294-
void BigNum::mod_inverse(BigNum &r, BigNum &a, const BigNum &m, BigNumContext &context) {
294+
td::Status BigNum::mod_inverse(BigNum &r, BigNum &a, const BigNum &m, BigNumContext &context) {
295295
auto result = BN_mod_inverse(r.impl_->big_num, a.impl_->big_num, m.impl_->big_num, context.impl_->big_num_context);
296-
LOG_IF(FATAL, result != r.impl_->big_num);
296+
if (result != r.impl_->big_num) {
297+
return td::Status::Error("Failed to compute modulo inverse");
298+
}
299+
return td::Status::OK();
297300
}
298301

299302
void BigNum::div(BigNum *quotient, BigNum *remainder, const BigNum &dividend, const BigNum &divisor,

tdutils/td/utils/BigNum.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class BigNum {
109109

110110
static void mod_mul(BigNum &r, BigNum &a, BigNum &b, const BigNum &m, BigNumContext &context);
111111

112-
static void mod_inverse(BigNum &r, BigNum &a, const BigNum &m, BigNumContext &context);
112+
static td::Status mod_inverse(BigNum &r, BigNum &a, const BigNum &m, BigNumContext &context);
113113

114114
static void div(BigNum *quotient, BigNum *remainder, const BigNum &dividend, const BigNum &divisor,
115115
BigNumContext &context);

tdutils/td/utils/format.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,12 @@ inline StringBuilder &operator<<(StringBuilder &logger, Time t) {
179179
double value;
180180
};
181181

182-
static constexpr NamedValue durations[] = {{"ns", 1e-9}, {"us", 1e-6}, {"ms", 1e-3}, {"s", 1}};
182+
static constexpr NamedValue durations[] = {{"ns", 1e-9}, {"us", 1e-6}, {"ms", 1e-3},
183+
{"s", 1}, {"h", 3600}, {"d", 86400}};
183184
static constexpr size_t durations_n = sizeof(durations) / sizeof(NamedValue);
184185

185186
size_t i = 0;
186-
while (i + 1 < durations_n && t.seconds_ > 10 * durations[i + 1].value) {
187+
while (i + 1 < durations_n && std::abs(t.seconds_) > 10 * durations[i + 1].value) {
187188
i++;
188189
}
189190
logger << StringBuilder::FixedDouble(t.seconds_ / durations[i].value, 1) << durations[i].name;

test/regression-tests.ans

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Test_Fift_test_hash_ext_default 686fc5680feca5b3bb207768215b27f6872a95128762dee0
2121
Test_Fift_test_hmap_default c269246882039824bb5822e896c3e6e82ef8e1251b6b251f5af8ea9fb8d05067
2222
Test_Fift_test_levels_default 9fba4a7c98aec9000f42846d6e5fd820343ba61d68f9139dd16c88ccda757cf3
2323
Test_Fift_test_namespaces_default e6419619c51332fb5e8bf22043ef415db686c47fe24f03061e5ad831014e7c6c
24+
Test_Fift_test_p256_default e1948ddd3d2686baa9f70fdf376ffcebbc2ec5f20eeb366cd856254e61fbfa31
2425
Test_Fift_test_rist255_default f4d7558f200a656934f986145c19b1dedbe2ad029292a5a975576d6891e25fc4
2526
Test_Fift_test_secp256k1_default 3118450dace6af05fcdbd54a87d9446162ce11ac6ef6dfc57998cf113587d602
2627
Test_Fift_test_sort2_default 9b57d47e6a10e7d1bbb565db35400debf2f963031f434742a702ec76555a5d3a

0 commit comments

Comments
 (0)