Coverage Report

Created: 2026-01-21 08:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/node/test/fuzzers/fuzz_sign_verify.cc
Line
Count
Source
1
// Copyright 2025 Google LLC
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//      http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include <cstdint>
16
#include <string>
17
#include "fuzzer/FuzzedDataProvider.h"
18
#include "fuzz_common.h"
19
#include "fuzz_js_format.h"
20
21
24.7k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
22
24.7k
  FuzzedDataProvider fdp(data, size);
23
24
  // Use all remaining bytes as the message string
25
24.7k
  const std::string message = fdp.ConsumeRemainingBytesAsString();
26
27
  // JS template to fuzz crypto signing/verifying using Node.js
28
24.7k
  static constexpr std::string_view kTemplate = R"(
29
24.7k
const crypto = require('crypto');
30
24.7k
31
24.7k
// Generate an RSA key pair (per fuzz run)
32
24.7k
const { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', {
33
24.7k
  modulusLength: 2048
34
24.7k
});
35
24.7k
36
24.7k
// The fuzzed message
37
24.7k
const message = {0};
38
24.7k
39
24.7k
// --- SIGNING ---
40
24.7k
try {
41
24.7k
  const signer = crypto.createSign('SHA256');
42
24.7k
  signer.update(message);
43
24.7k
  signer.end();
44
24.7k
  const signature = signer.sign(privateKey, 'base64');
45
24.7k
46
24.7k
  // --- VERIFYING ---
47
24.7k
  const verifier = crypto.createVerify('SHA256');
48
24.7k
  verifier.update(message);
49
24.7k
  verifier.end();
50
24.7k
  const isValid = verifier.verify(publicKey, signature, 'base64');
51
24.7k
} catch (e) {
52
24.7k
  // Catch errors to prevent harness crashes
53
24.7k
}
54
24.7k
)";
55
56
  // Inject the fuzzed message into the JS template
57
24.7k
  const std::string js = FormatJs(kTemplate, ToSingleQuotedJsLiteral(message));
58
59
  // Execute the JavaScript under a Node isolate
60
24.7k
  fuzz::IsolateScope iso;
61
24.7k
  if (!iso.ok()) return 0;
62
24.7k
  fuzz::RunEnvString(iso.isolate(), js.c_str());
63
64
24.7k
  return 0;
65
24.7k
}