Pyrite
Pyrite provides hardware-bound and cloud-gated binary execution sealing and
real-time anti-tamper verification for Crystal applications.
Core Architecture
Applications protected by Pyrite are incomplete at rest. Their production
configuration and credentials are encrypted inside a sealed envelope (bootstrap.enc)
bound to a physical TPM 2.0 chip or Cloud KMS workload identity.
At boot, Pyrite.bootstrap! unseals the envelope, streams the running /proc/self/exe
binary in 16KB chunks to compute its live SHA-256 digest, compares it in constant-time
against the authorized digest inside the envelope, and deserializes the configuration into
a strongly typed JSON::Serializable struct.
Basic Usage
require "pyrite"
struct AppConfig
include JSON::Serializable
getter database_url : String
getter session_secret : String
getter api_token : String
end
# Single-call bootstrap with automatic trust anchor detection:
config = Pyrite.bootstrap!(AppConfig)
puts "Verified binary execution. DB: #{config.database_url}"
Constants
Current library version
Class methods
Automatically detects the ambient hardware or cloud trust anchor on the current machine.
Detection precedence:
- Explicit Environment Overrides:
$KMS_KEY_RESOURCE-> Google Cloud KMS$AWS_KMS_KEY_IDor$AWS_CONTAINER_CREDENTIALS_RELATIVE_URIor$AWS_EXECUTION_ENV-> AWS KMS$CREDENTIALS_DIRECTORY-> systemd credentials
- Local Hardware TPM 2.0:
/dev/tpmrm0or/dev/tpm0->SystemdCredsifsystemd-credsbinary is available, elseTPM2Direct
- DMI / Virtualization Identity:
- Google Compute Engine / Cloud Run ->
GCPKMS - Amazon EC2 ->
AWSKMS
- Google Compute Engine / Cloud Run ->
- Container & Metadata Server Discovery:
- Container environments with active GCP metadata server ->
GCPKMS
- Container environments with active GCP metadata server ->
Raises Pyrite::HardwareAuthError if no supported trust anchor is detected.
Single-call bootstrap helper for consuming applications.
Unseals the target envelope using the resolved provider (or auto-detects the
ambient hardware/cloud trust anchor), verifies the running binary's SHA-256
self-integrity against the authorized digest in constant-time, and returns
strongly-typed application configuration deserialized into schema.
Parameters
schema(Class): A struct or class includingJSON::Serializablerepresenting the application configuration.envelope_path(String): Path to the sealed envelope file. Defaults to$PYRITE_ENVELOPEor"bootstrap.enc".provider(Provider?): Explicit provider instance to use. Ifnil,auto_detect_provideris invoked.
Errors
Pyrite::TamperError: Raised if the live binary SHA-256 does not match the authorized hash inside the envelope.Pyrite::HardwareAuthError: Raised if hardware TPM or Cloud KMS rejects authorization/identity.Pyrite::MissingEnvelopeError: Raised ifenvelope_pathdoes not exist on disk.Pyrite::ConfigError: Raised if decrypted JSON cannot be parsed intoschema.
Example
struct ServerConfig
include JSON::Serializable
getter port : Int32
getter secret_key : String
end
config = Pyrite.bootstrap!(ServerConfig, envelope_path: "config/prod.enc")
Validates connectivity and response headers from the Google Cloud Metadata Server (169.254.169.254).
Returns true if reachable and responding with header Metadata-Flavor: Google.
Core execution gate and real-time self-integrity verification.
Unwraps the sealed envelope via provider, computes the live SHA-256 digest
of /proc/self/exe, and performs constant-time comparison against the
authorized_sha256 payload field.
Returns the raw JSON::Any payload if integrity is verified.
Errors
Pyrite::TamperError: Raised immediately if the running binary digest does not match the authorized hash.Pyrite::HardwareAuthError: Raised if the provider unwrap fails.
Example
provider = Pyrite::Providers::SystemdCreds.new
payload = Pyrite.unlock!(provider, "bootstrap.enc")
puts "Authorized binary digest: #{payload["authorized_sha256"]}"