Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion .github/workflows/library.yml
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,11 @@ jobs:
needs: plan
if: needs.plan.outputs.has-own-lanes == 'true'
runs-on: ${{ matrix.os }}
timeout-minutes: 60
# crypto's constant-time lane is followed by dudect's judge, whose reruns of a case under
# wasmtime may last up to 1800 s each: 120 minutes there, 60 elsewhere. Several wasip2 cases each
# needing two reruns could exceed 120 minutes, which cancels the job (rare; the evidence is
# still in the log).
timeout-minutes: ${{ (matrix.library == 'crypto' && matrix.lane == 'constant-time') && 120 || 60 }}
strategy:
fail-fast: false
matrix: ${{ fromJSON(needs.plan.outputs.own-lanes) }}
Expand Down Expand Up @@ -409,12 +413,34 @@ jobs:
job: own-${{ matrix.id || matrix.name }}

# It registers its toolset in .local/user-config.jam, prints its b2 command and runs it.
# crypto's constant-time lane on wasip2 runs dudect_sodium and dudect_secp256k1 under
# wasmtime for minutes, near or past the launcher's default bound of 600 s, so it gets 1800
# (run.sh rejects an empty value, so the fallback is the default).
- name: Own lane
id: lane
env:
LANE: ${{ toJSON(matrix) }}
WEBCPP_WASIP2_TEST_TIMEOUT: ${{ (matrix.library == 'crypto' && matrix.lane == 'constant-time') && '1800' || '600' }}
run: python3 tools/ci/matrix.py own-lane "$LANE"

# crypto's constant-time lane: every dudect output printed, a warning for every |t| of 10
# or more, and on a runner GitHub hosts a case over 10 measured again in two fresh
# processes, failing when two of the three exceed 10 (libs/crypto's change api-gaps, D7).
# The launcher's bound is the lane's, so a run of the judge's own lasts at most that, and
# the judge's subprocess timeout (2400 s) lies beyond it.
- name: Judge dudect
if: ${{ !cancelled() && matrix.library == 'crypto' && matrix.lane == 'constant-time' }}
env:
WEBCPP_WASIP2_TEST_TIMEOUT: '1800'
TARGET: ${{ matrix.platform }}
RERUN: ${{ runner.environment == 'github-hosted' && '--rerun' || '' }}
shell: bash
run: |
# RERUN is empty off the hosted runners, so it is left unquoted to vanish.
# shellcheck disable=SC2086
python3 libs/crypto/test/constant_time/dudect_judge.py --bin bin \
--target "$TARGET" $RERUN --launcher tools/boost/asio-wasip2/run.sh

- name: Save the third-party builds
if: ${{ !cancelled() && !startsWith(matrix.lane, 'openssl-upstream') }}
uses: ./tools/ci/actions/third-party
Expand Down
1 change: 1 addition & 0 deletions doc/releases/0.1.0.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ None.
* Improved lint preparation by separating OpenSSL headers from archive builds and upstream tests.
* Changed the CI's lint to twelve shards, which a slow runner no longer takes past its time limit.
* Added the user's Jamfiles of webcpp::crypto, tested as fixtures of a test of the tools.
* Added a judge to crypto's constant-time lane: dudect results warn at 10, and hosted runners fail on two of three.
* Added same-as: to crypto's user Jamfiles test, which keeps copied programs identical.
* Added not-contains: to crypto's user Jamfiles test, which shows the canary is a library's only link error.
* Added tests of a missing /third_party// name, of the BIPs' pin and of Emscripten without a crypto backend.
Expand Down
2 changes: 1 addition & 1 deletion libs/crypto
Submodule crypto updated 77 files
+11 −0 build.jam
+1 −1 doc/Jamfile
+65 −15 doc/aead.adoc
+13 −6 doc/backends.adoc
+11 −5 doc/block.adoc
+2 −2 doc/changelog.adoc
+4 −2 doc/choosing_backend.adoc
+1 −1 doc/curves.adoc
+7 −5 doc/errors.adoc
+3 −6 doc/generated/backends-aes_block.adoc
+1 −1 doc/generated/backends-keypair.adoc
+10 −10 doc/generated/constant-time.adoc
+5 −0 doc/generated/default-aliases.adoc
+16 −4 doc/generated/defaults.adoc
+16 −0 doc/generated/jamfiles.adoc
+1 −1 doc/generated/matrix.adoc
+30 −0 doc/generated/messages.adoc
+3 −3 doc/generated/namespace-ciphers.adoc
+3 −3 doc/generated/namespace-curves.adoc
+6 −3 doc/jamfile.adoc
+21 −12 doc/llms.txt
+11 −9 doc/memory.adoc
+18 −14 doc/secp256k1.adoc
+19 −11 doc/tables/crypto.json
+7 −9 doc/tables/generate.py
+79 −3 doc/tables/generate_test.py
+4 −2 doc/testing.adoc
+4 −0 example/Jamfile
+155 −0 example/opened_in_two_passes.cpp
+4 −0 example/opened_in_two_passes.expected
+3 −1 include/webcpp/crypto.hpp
+2 −0 include/webcpp/crypto/backends/ctaes.hpp
+2 −0 include/webcpp/crypto/backends/secp256k1.hpp
+63 −14 include/webcpp/crypto/ciphers/aes.hpp
+34 −0 include/webcpp/crypto/ciphers/aes_block.hpp
+244 −18 include/webcpp/crypto/ciphers/gcm.hpp
+8 −0 include/webcpp/crypto/config.hpp
+34 −1 include/webcpp/crypto/curves/secp256k1.hpp
+11 −6 include/webcpp/crypto/curves/secp256k1/tweak.hpp
+8 −4 include/webcpp/crypto/detail/capabilities.hpp
+24 −3 include/webcpp/crypto/detail/defaults.hpp
+8 −4 include/webcpp/crypto/error.hpp
+22 −6 test/Jamfile
+7 −0 test/allocation_bearssl_test.cpp
+7 −0 test/allocation_openssl_test.cpp
+14 −4 test/constant_time/Jamfile
+435 −18 test/constant_time/dudect.hpp
+255 −0 test/constant_time/dudect_harness_test.cpp
+144 −0 test/constant_time/dudect_judge.py
+65 −27 test/constant_time/dudect_secp256k1_test.cpp
+100 −41 test/constant_time/dudect_sodium_test.cpp
+106 −61 test/constant_time/dudect_test.cpp
+76 −0 test/default_aes_block_test.cpp
+17 −14 test/default_backend_test.cpp
+8 −2 test/default_bearssl_test.cpp
+3 −1 test/default_both_test.cpp
+4 −3 test/default_both_wasm_test.cpp
+11 −8 test/default_selection_test.cpp
+4 −2 test/gcm_bearssl_test.cpp
+169 −20 test/gcm_mock_test.cpp
+2 −1 test/gcm_openssl_test.cpp
+2 −0 test/messages/Jamfile
+12 −0 test/messages/aes_block_default_not_linked/Jamfile
+23 −0 test/messages/aes_block_default_not_linked/aes_block_default_not_linked.cpp
+12 −0 test/messages/aes_block_default_not_linked_wasm/Jamfile
+23 −0 test/messages/aes_block_default_not_linked_wasm/aes_block_default_not_linked_wasm.cpp
+1 −0 test/msvc/Jamfile
+11 −0 test/msvc/aes_block_default_not_linked/Jamfile
+20 −0 test/msvc/aes_block_default_not_linked/app.cpp
+4 −1 test/secp256k1_allocation_test.cpp
+152 −0 test/secp256k1_generate_test.cpp
+2 −2 test/secp256k1_silent_payments_test.cpp
+18 −0 test/secp256k1_test.cpp
+12 −7 test/secp256k1_tweak_test.cpp
+443 −2 test/support/gcm_checks.hpp
+121 −24 test/support/timing_coverage.hpp
+9 −2 test/timing_coverage_secp256k1_test.cpp
180 changes: 180 additions & 0 deletions tools/test/crypto_dudect_judge_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
#!/usr/bin/env python3
# Copyright (c) 2026 WebCpp.org
#
# Distributed under the Boost Software License, Version 1.0. (See
# accompanying file LICENSE_1_0.txt or copy at
# https://www.boost.org/LICENSE_1_0.txt)

"""Checks libs/crypto/test/constant_time/dudect_judge.py, the CI's judge of dudect's outputs, with
scratch outputs and fake programs: it prints each output in a group and warns for every case whose
|t| is 10 or more, the planted program and the harness's test excepted; with --rerun it measures
each judged case over 10 again in two fresh processes and fails exactly when two of the three
values are 10 or more, naming them; it never reruns a case that is not judged; it refuses an
output with no case line; and on wasip2 it runs the program through the launcher. Run with the
names of some cases to run only those."""

from __future__ import annotations

import shlex
import subprocess
import sys
import tempfile
from collections.abc import Callable
from pathlib import Path

ROOT = Path(__file__).resolve().parents[2]
JUDGE = ROOT / 'libs/crypto/test/constant_time/dudect_judge.py'

FAKE = '''#!/usr/bin/env python3
import sys
from pathlib import Path
answers = {answers!r}
counter = Path(sys.argv[0] + '.count')
done = int(counter.read_text()) if counter.exists() else 0
counter.write_text(str(done + 1))
assert sys.argv[1:] == ['--case', {case!r}], sys.argv
print('dudect case={case} t=' + answers[done] + ' threshold=10 judged=yes')
'''

LAUNCHER = '''#!/usr/bin/env python3
import sys
from pathlib import Path
Path({log!r}).write_text(' '.join(sys.argv[1:]))
print('dudect case={case} t=1.00 threshold=10 judged=yes')
'''


def run_judge(*args: str) -> subprocess.CompletedProcess[str]:
return subprocess.run([sys.executable, str(JUDGE), *args], capture_output=True, text=True,
check=False, timeout=120)


def write_output(directory: Path, name: str, text: str, suffix: str = '') -> Path:
"""bin/<build directory>/<name>.output holding text and the lane's exit line."""
where = directory / f'{name}.test/clang/release{suffix}'
where.mkdir(parents=True, exist_ok=True)
(where / f'{name}.output').write_text(text + '\nEXIT STATUS: 0\n')
return where


def write_script(path: Path, text: str) -> None:
path.write_text(text)
path.chmod(0o755)


def write_fake_program(path: Path, answers: list[str], case: str) -> None:
write_script(path, FAKE.format(answers=answers, case=case))


BEARSSL = ('dudect case=chacha_open t=69.80 threshold=10 judged=yes\n'
'dudect evidence case=chacha_open crop=1 limit_ns=9.00 class0_n=5 class0_mean_ns=3.00 '
'class1_n=6 class1_mean_ns=4.00\n'
'dudect case=equal t=3.10 threshold=10 judged=yes\n')


def expect(done: subprocess.CompletedProcess[str], status: int) -> None:
if done.returncode != status:
raise AssertionError(f'exit {done.returncode}, not {status}\n{done.stdout}{done.stderr}')


def judge_prints_and_warns(scratch: Path) -> None:
write_output(scratch, 'dudect_bearssl', BEARSSL)
done = run_judge('--bin', str(scratch), '--target', 'native')
expect(done, 0)
for text in ('::group::dudect_bearssl (native)\n', BEARSSL, '::endgroup::\n'):
assert text in done.stdout, done.stdout
warnings = [line for line in done.stdout.splitlines() if line.startswith('::warning')]
assert len(warnings) == 1, done.stdout
assert warnings[0].startswith('::warning title=dudect dudect_bearssl chacha_open (native)::'
'dudect case=chacha_open t=69.80 '), warnings
assert 'dudect evidence case=chacha_open' in warnings[0], warnings


def judge_two_of_three_pass(scratch: Path) -> None:
directory = write_output(scratch, 'dudect_bearssl', BEARSSL)
write_fake_program(directory / 'dudect_bearssl', ['3.10', '2.70'], 'chacha_open')
done = run_judge('--bin', str(scratch), '--target', 'native', '--rerun')
expect(done, 0)
assert '69.80, 3.10, 2.70' in done.stdout, done.stdout


def judge_two_of_three_fail(scratch: Path) -> None:
directory = write_output(scratch, 'dudect_bearssl',
'dudect case=equal t=14.00 threshold=10 judged=yes\n')
write_fake_program(directory / 'dudect_bearssl', answers=['12.50', '3.00'], case='equal')
done = run_judge('--bin', str(scratch), '--target', 'native', '--rerun')
assert done.returncode == 1, done
assert ('dudect: equal (dudect_bearssl, native) exceeded 10 in 2 of 3 runs: '
'14.00, 12.50, 3.00') in done.stderr, done.stderr


def judge_unjudged_case(scratch: Path) -> None:
directory = write_output(scratch, 'dudect_sodium',
'dudect case=x25519_agree t=30.00 threshold=10 judged=no\n')
# No program: a rerun would fail to start it.
done = run_judge('--bin', str(scratch), '--target', 'native', '--rerun')
expect(done, 0)
assert done.stdout.count('::warning') == 1, done.stdout
assert not list(directory.glob('*.count')), 'the case was run again'


def judge_skips_planted(scratch: Path) -> None:
write_output(scratch, 'dudect_planted',
'dudect case=x25519_agree t=66.70 threshold=10 judged=yes\n')
write_output(scratch, 'dudect_harness',
'dudect case=x t=10.00 threshold=10 judged=yes\nplanted leak found\n')
done = run_judge('--bin', str(scratch), '--target', 'native', '--rerun')
expect(done, 0)
assert 't=66.70' in done.stdout and '::warning' not in done.stdout, done.stdout
assert '::group::dudect_planted (native)' in done.stdout, done.stdout


def judge_output_without_case_lines(scratch: Path) -> None:
write_output(scratch, 'dudect_ctaes', '')
done = run_judge('--bin', str(scratch), '--target', 'native')
expect(done, 1)
assert 'dudect_ctaes.output' in done.stderr, done.stderr


def judge_wasip2_launcher(scratch: Path) -> None:
directory = write_output(scratch, 'dudect_bearssl', BEARSSL, suffix='/wasi')
log = scratch / 'launcher.log'
launcher = scratch / 'launch.py'
write_script(launcher, LAUNCHER.format(log=str(log), case='chacha_open'))
done = run_judge('--bin', str(scratch), '--target', 'wasip2', '--rerun', '--launcher',
shlex.join([sys.executable, str(launcher)]))
expect(done, 0)
wanted = f'{directory / "dudect_bearssl.wasm"} --case chacha_open'
assert log.read_text() == wanted, log.read_text()
assert '69.80, 1.00, 1.00' in done.stdout, done.stdout


CASES: list[Callable[[Path], None]] = [
judge_prints_and_warns,
judge_two_of_three_pass,
judge_two_of_three_fail,
judge_unjudged_case,
judge_skips_planted,
judge_output_without_case_lines,
judge_wasip2_launcher,
]


def main(argv: list[str]) -> int:
unknown = set(argv) - {case.__name__ for case in CASES}
if unknown:
print(f'crypto_dudect_judge_test: no case named {", ".join(sorted(unknown))}',
file=sys.stderr)
return 2
for case in CASES:
if argv and case.__name__ not in argv:
continue
with tempfile.TemporaryDirectory(prefix='webcpp crypto dudect judge ') as scratch:
case(Path(scratch))
print(f'{case.__name__}: ok')
print('crypto_dudect_judge_test: ok')
return 0


if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))
9 changes: 9 additions & 0 deletions tools/test/fixtures/crypto_jamfiles/aes_block_default/Jamfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright (c) 2026 WebCpp.org
#
# Distributed under the Boost Software License, Version 1.0. (See
# accompanying file LICENSE_1_0.txt or copy at
# https://www.boost.org/LICENSE_1_0.txt)

# tag::jamfile[]
exe app : app.cpp /webcpp/crypto//crypto /webcpp/crypto//aes_block-default ;
# end::jamfile[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright (c) 2026 WebCpp.org
#
# Distributed under the Boost Software License, Version 1.0. (See
# accompanying file LICENSE_1_0.txt or copy at
# https://www.boost.org/LICENSE_1_0.txt)

[all]
targets: native wasip2 wasip3 emscripten
build: succeeds
run: app
# tag::output[]
[native]
aes256 ok backend=openssl
[wasip2]
aes256 ok backend=ctaes
[wasip3]
aes256 ok backend=ctaes
[emscripten]
aes256 ok backend=ctaes
# end::output[]
37 changes: 37 additions & 0 deletions tools/test/fixtures/crypto_jamfiles/aes_block_default/app.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2026 WebCpp.org
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)

// Encrypts the FIPS-197 appendix C.3 block with AES-256 on the block cipher's default backend,
// checks it, decrypts it again, and prints the backend that ran.
#include <webcpp/crypto/ciphers/aes_block.hpp>

#include <array>
#include <cstddef>
#include <cstdint>
#include <cstdio>

int main() {
std::array<std::uint8_t, 32> key{};
for (std::size_t i = 0; i < key.size(); ++i) {
key[i] = static_cast<std::uint8_t>(i);
}
std::array<std::uint8_t, 16> const plain{
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
};
std::array<std::uint8_t, 16> const expected{
0x8e, 0xa2, 0xb7, 0xca, 0x51, 0x67, 0x45, 0xbf,
0xea, 0xfc, 0x49, 0x90, 0x4b, 0x49, 0x60, 0x89,
};
std::array<std::uint8_t, 16> block = plain;
auto aes = webcpp::crypto::ciphers::aes256<>::make(key);
bool const ok =
aes && aes->encrypt(block) && block == expected && aes->decrypt(block) && block == plain;
auto const name = webcpp::crypto::ciphers::aes256<>::backend_type::name;
std::printf("aes256 %s backend=%.*s\n", ok ? "ok" : "FAILED", static_cast<int>(name.size()),
name.data());
return ok ? 0 : 1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright (c) 2026 WebCpp.org
#
# Distributed under the Boost Software License, Version 1.0. (See
# accompanying file LICENSE_1_0.txt or copy at
# https://www.boost.org/LICENSE_1_0.txt)

# tag::jamfile[]
exe app : app.cpp /webcpp/crypto//crypto ;
# end::jamfile[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright (c) 2026 WebCpp.org
#
# Distributed under the Boost Software License, Version 1.0. (See
# accompanying file LICENSE_1_0.txt or copy at
# https://www.boost.org/LICENSE_1_0.txt)

# The compiler's line of the refusal starts with the header's path on the machine, so the
# message is a contains: line. On wasip2 and wasip3, where OpenSSL has the block cipher too, the
# message names it as well (test/messages/aes_block_default_not_linked_wasm); compile-diagnostic
# is clang's and skips Emscripten, so this fixture is the Emscripten one.
[all]
targets: emscripten
build: fails
# tag::messages[]
contains: webcpp::crypto: AES block cipher's default backend on this target is ctaes, which is not linked, and the one backend that has it there. Add /webcpp/crypto//aes_block-default, the default's library on every target, to the program's Jamfile, as in: exe app : app.cpp /webcpp/crypto//crypto /webcpp/crypto//aes_block-default ; (without b2: define WEBCPP_CRYPTO_HAS_CTAES=1 and link ctaes.c with webcpp's src/canary_ctaes.c)
# end::messages[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) 2026 WebCpp.org
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)

// A program that uses the AES block cipher's default backend, which on WebAssembly is ctaes, and
// links none.
#include <webcpp/crypto/ciphers/aes_block.hpp>

#include <array>
#include <cstdint>

int main() {
std::array<std::uint8_t, 32> const key{};
return webcpp::crypto::ciphers::aes256<>::make(key) ? 0 : 1;
}
Loading