Files

8.7 KiB
Raw Permalink Blame History

Crypto implementation plan

Status: planning only; all tasks PLANNED. Source baseline f1f9ba375b22abf1adcc7c550ec21ac20895c0a4, inspected 2026-11-24. Applies requirements R-004, R-006, R-012, R-014–R-016 and architecture C-02, C-03, C-07, C-13.

Existing primitive surface

Crypto.hpp declares Void::Crypto::Hasher, Digest, SHA-1/256/512 helpers, file hashing and verifyEd25519 with fixed 32-byte public keys and 64-byte signatures. Crypto.cpp uses OpenSSL EVP contexts with RAII. Incremental hashers are move-only; use after move/finalize is an error. File hashing streams a 64 KiB buffer. Ed25519 verification uses exact message bytes: malformed sizes are errors, well-formed invalid signatures return false. module.cmake declares Functional and OpenSSL::Crypto.

Tests cover known SHA vectors, streamed/file equivalence, rejected update after finalize and an RFC 8032 Ed25519 vector with modified message rejection. They do not establish provider-failure handling, signing, key management, asynchronous file cancellation or broad malformed-key coverage. Graph metadata matched all four linked source paths with no recorded issue; source was also read directly.

Responsibility and backend decision

Retain OpenSSL as the initial implementation of hashes and signatures. Native preference means Void-owned lifetime/error/stream interfaces and no public EVP types; it does not authorize original cryptographic algorithms. TLS certificate and connection policy belong to Net, authentication/session/authorization workflows to Security, signed document selection and exact-byte verification sequencing to Update. Crypto must not depend on those consumers, Storage, or Runtime.

Keep existing verification formats intact. NEW HMAC-SHA-256 and HKDF-SHA-256 are staged primitive additions through EVP, with separate known-answer and misuse tests; new signing is Ed25519 through an owning private-key handle. SHA-1 stays available only for explicit legacy interoperability; new security protocols select SHA-256 or stronger. No silent fallback if an algorithm/provider is unavailable. Provider configuration is injected per context; do not mutate process-wide OpenSSL state from a library constructor.

NEW API shapes, algorithms and ownership

// NEW illustrative Void::Crypto shapes; Result is C-02 shorthand.
class PrivateKey; // move-only opaque backend state, explicit import/generate
struct Ed25519PublicKey { std::array<std::byte, 32> bytes; };
struct Ed25519Signature { std::array<std::byte, 64> bytes; };
Result<Ed25519Signature> sign_ed25519(ByteSpan exact_message, const PrivateKey&);
Result<Digest> hmac_sha256(ByteSpan secret_key, ByteSpan message);
Result<void> hkdf_sha256(ByteSpan secret_input, ByteSpan salt,
                         ByteSpan context, MutableByteSpan secret_output);
// Optional IO adapter, not a new executor or OperationOptions definition:
Task<Result<Digest>> hash_stream(HashAlgorithm, IOByteReader&, OperationOptions);

Byte-span and reader names above denote proposed owner-provided shapes, not existing aliases. Memory owns resource integration. Callers may supply Security's secure buffer through spans; Crypto does not import the authentication module or allocate a general-purpose secret result type. HKDF fills the caller-owned destination, wipes it on failure and rejects overlapping input/output. Primitive APIs borrow inputs until the synchronous call returns; async adapters retain owned buffers through IO completion. PrivateKey has no implicit copy/export/string conversion. Import uses an explicitly named encoding and validates algorithm/size; public export and secret export are separate capabilities. Secret export requires explicit caller intent and a secure owning result.

Hasher state is Ready → Updating → Finalized or Failed. Backend update/finalize failure poisons the context until destroyed; retrying a partially effective update is invalid. Hash-stream reads at most 64 KiB, updates, checks stop/deadline between chunks, finalizes only at clean EOF and never treats a short failed read as success. Descriptor close or concurrent path replacement has the IO-defined result; hashing a path does not promise a snapshot of a concurrently modified file. Update's downloader must pin its own artifact.

Signature operations do not prehash/reencode Ed25519 messages implicitly. Binary/text decoding is outside verification; Data's StrictJson rules and exact signed payload bytes remain unchanged under C-07. A successful primitive verification is not authorization, freshness, trust-root selection or replay protection.

NEW defaults: stream chunk 64 KiB, optional file adapter 8 concurrent operations and 32 waiting, 8 MiB adapter buffer budget; oversized input is processed incrementally where the algorithm supports it. One-shot signing/verification initially limits messages to 16 MiB and directs larger formats to an explicitly specified signed-manifest design. The NEW HKDF API caps output at 4 KiB and also enforces the backend primitive's limit; invalid sizes fail before mutation. Error context contains operation/provider codes, never keys, passwords or plaintext. Output buffers are wiped on failed secret-producing operations.

Failure matrix and evidence

Fault Required behavior Acceptance scenario
Bad key/signature size Typed invalid input, no backend use Every length near expected boundary
Wrong message/key/signature Successful false verification Altered bytes and independent vectors
Algorithm unavailable / provider error Unsupported/backend error; no downgrade Inject provider failures per operation
Hash context fails midway Failed terminal state, cannot reuse Update/finalize/move sequences
File read error / cancellation No digest success; one terminal result Partial read then error, stop at EOF race
Allocation/admission failure Resource error before secret output publication Budget exhaustion with no retained secret
Async shutdown Stop admission, stop/join readers, destroy contexts last Caller buffers cannot escape completion

Task DAG

ID Phase / dependencies Scope and acceptance Recommended role
M-CRYPTO-01 P0; baseline Preserve existing vectors and exact-byte false-vs-error behavior; record consumers before API migration sol_implementer
M-CRYPTO-02 P1; M-CRYPTO-01, Functional/Memory contracts Typed size wrappers, poisoned-state/error handling and provider isolation; malformed/state tests pass astra_implementer
M-CRYPTO-03 P2; M-CRYPTO-02 Owning Ed25519 private key/signing, HMAC/HKDF with caller-owned output; independent known-answer vectors and no secret diagnostics astra_implementer
M-CRYPTO-04 P2; M-CRYPTO-02, IO/Async gates Optional hash-stream/file adapter, bounded cancellation and workload accounting; no mandatory runtime dependency sol_implementer
M-CRYPTO-05 P3; M-CRYPTO-03, M-CRYPTO-04 Signed-update integration fixtures, component consumers and resource profiles; exact signed bytes unchanged astra_implementer
M-CRYPTO-06 P4; M-CRYPTO-05, distributed consumers Snapshot/transport integrity recipes distinguish checksums, authenticated channels and trusted signatures sol_implementer

Verification and migration

CURRENT checks are the linked Crypto tests in VoidTests; no build/test was rerun for this documentation task. PROPOSED tests add backend fault injection, independent vector corpora, secret lifetime tests, file mutation/short-read cases, fuzzed imports and sanitizer coverage. Backend-version testing follows BUILD-AND-DISTRIBUTION; the OpenSSL 3.5 client guide informs Net TLS policy, not a claim about the currently linked version or FIPS certification.

Measure small-message setup separately from large streaming throughput, with p50/p95/p99, allocations, RSS/CPU and build cost under PERFORMANCE. No native replacement is justified by speculative speedups. VALIDATION defines future runner prerequisites; do not invent a presently available signing target.

Major-release evolution adds typed overloads and consistent naming with adapters for existing camelCase entry points. Preserve SHA digest bytes, file semantics and Ed25519 verification outcome distinction. Never reinterpret old signatures with a new framing scheme. Rollback retains readable key/algorithm formats and the previous backend adapter; unsupported new algorithms fail explicitly. See MIGRATION.