github.com/amberframework/beryl

Security for Amber V2. Cryptography written in Crystal, with no C library underneath.

0 stars
2 dependents
License: MIT

Nothing has been indexed for 0.2.3 yet. The tag is recorded, its shard.yml has not been read, so the manifest and dependency list below are empty because they are unknown rather than because they are absent.

Installation

# Add this to your shard.yml
dependencies:
  beryl:
    github: amberframework/beryl
    version: ~> 0.2.3

Then run:

shards install

shard.yml

No shard.yml has been indexed for 0.2.3. You can read it on the repository.

Dependencies

Unknown: the shard.yml for this version has not been read yet.

README

This README is the one indexed from the repository at its latest ref, not from the tag for this version.

Install

dependencies:
  beryl:
    github: amberframework/beryl
    version: 0.2.5
require "beryl"

Beryl::Crypto.digest("contents of a file")                             # SHA-256
tag = Beryl::Crypto.authenticate("message", key: secret_key)           # HMAC-SHA256
Beryl::Crypto.verify_authentication("message", tag, key: secret_key)   # => true
Beryl::Crypto.derive_key(shared_secret, info: "cookies", bytesize: 32) # HKDF-SHA256
sealed = Beryl::Crypto.encrypt("message", key: key_of_32_bytes)         # XChaCha20-Poly1305
Beryl::Crypto.decrypt(sealed, key: key_of_32_bytes)                    # => Bytes, or nil if changed
signing_key = Beryl::Crypto.generate_signing_key                         # Ed25519
signature = Beryl::Crypto.sign("message", signing_key: signing_key)
Beryl::Crypto.verify_signature("message", signature, verification_key: signing_key.verification_key) # => true

chain = Beryl::Certificates.read_pem_chain(File.read("chain.pem"))
Beryl::Certificates.verify_server_chain(chain, hostname: "example.com") # => the verified path, or raises

The calls on Beryl::Crypto always use the algorithm Beryl recommends today. Beryl::Crypto::V1 names each algorithm exactly, and Beryl::Crypto::Compatibility holds the old ones that existing formats still need.

[!NOTE] Beryl is opt-in today. It becomes a default in Amber once its own gates pass, and every pull request must pass them to merge. 0.2.0 has the hashes, MACs, AEADs, public-key algorithms and certificate validation below, and TLS 1.3 as a client and a server (docs/tls.md, CHANGELOG.md).

What's inside

AreaAlgorithmsStandardsGuide
Hashes and MACsSHA-224, SHA-256, SHA-384, SHA-512, SHA-512/256, HMAC, HKDF; for existing formats MD5, SHA-1 and PBKDF2FIPS 180-4, RFC 2104, RFC 5869, RFC 8018hashes-and-macs.md
Authenticated encryptionXChaCha20-Poly1305, ChaCha20-Poly1305, AES-GCM; for existing formats AES-CBCdraft-irtf-cfrg-xchacha-03, RFC 8439, FIPS 197, SP 800-38D, SP 800-38Aciphers-and-aeads.md
CurvesX25519 key agreement, Ed25519 signatures, P-256 ECDSA (RFC 6979 nonces) and ECDH, P-384 ECDSA verificationRFC 7748, RFC 8032, FIPS 186-5, SP 800-186, SEC 1public-key.md
RSAPKCS #1 v1.5 and PSS signatures, OAEP encryption, 2,048 to 4,096-bit private keysRFC 8017public-key.md
CertificatesX.509 certificates, CRLs and private key files; chain verification against the pinned Mozilla roots or the system bundle; host names checkedRFC 5280, RFC 9525, RFC 7468certificates.md
TLSTLS 1.3 client and server (Beryl::TLS), shaped like OpenSSL::SSL: session tickets, HelloRetryRequest, ALPN, SNI, KeyUpdate, record size limit; TLS 1.3 onlyRFC 8446 (9846), RFC 8448, RFC 8449, RFC 7301, RFC 6066, RFC 8701tls.md

Processor instructions, and Data Independent Timing

ProcessorHashesAES-GCM and AES-CBCCurves and RSA
Apple SiliconARMv8 SHA1, SHA2 and SHA512 instructionsaese/aesd, pmull and eor3P-256 and P-384 field arithmetic and RSA's Montgomery multiplication in AArch64 assembly
Linux on AArch64the same, each found at run timethe same, found at run timethe same
Linux on x86-64SHA-NI for SHA-1, SHA-224 and SHA-256AES-NI and pclmulqdqP-256 field multiplication and squaring in x86-64 assembly (mulq/adcq), fiat-crypto's field arithmetic for the rest, RSA in Crystal
Anything elseportable Crystalportable Crystalportable Crystal

On Arm, every operation on a secret (HMAC, HKDF, PBKDF2, the AEADs, AES key setup, every private-key operation) runs with the processor's Data Independent Timing mode on. Without it, Apple M4 ran an X25519 multiplication measurably faster for a scalar with few set bits, though the compiled code has no branch on the scalar; with it on, every timing case is clean (public-key.md). x86-64 gives programs no such mode. AMD Zen 3 and Zen 4 CI runners ran P-256 multiplication by the scalar 1 measurably faster, so the curve multiplications give every window fresh operands: odd digits for the P-256 and P-384 generator, randomized starting points for X25519 and edwards25519, a blinded ECDSA nonce inversion (public-key.md).

Speed

Hashes, MACs and AEADs, against OpenSSL 4.1.0 on an Apple M1 Max (median of ten or more rounds of 2 s per arm). All 42 hash and HMAC rows and all 32 AEAD rows are at least as fast, with the whole paired 95% interval on that side:

Algorithm64 B1 KiB16 KiB1 MiB
SHA-2562.59× as fast1.2× as fast1.002× as fast
SHA-5122.03× as fast1.12× as fast1.014× as fast
HMAC-SHA-2561.18× as fast1.015× as fast1.001× as fast
ChaCha20-Poly1305, seal1.23× as fast1.044× as fast1.13× as fast1.15× as fast
ChaCha20-Poly1305, open1.21× as fast1.035× as fast1.053× as fast1.076× as fast
XChaCha20-Poly1305, seal2.01× as fast1.31× as fast1.16× as fast1.16× as fast
XChaCha20-Poly1305, open1.93× as fast1.3× as fast1.098× as fast1.063× as fast
AES-128-GCM, seal1.49× as fast1.078× as fast1.063× as fast1.048× as fast
AES-128-GCM, open1.27× as fast1.049× as fast1.054× as fast1.053× as fast
AES-256-GCM, seal1.45× as fast1.078× as fast1.087× as fast1.08× as fast
AES-256-GCM, open1.24× as fast1.04× as fast1.068× as fast1.07× as fast

The hashes were measured at 64 B, 1 KiB and 1 MiB. AES-CBC, which does not authenticate, ranges from 4.92× as fast (AES-128 decryption at 64 B) to 1.073× slower (AES-128 decryption at 1 MiB).

Apple M4 with all ten cores reserved, ten rounds of 2 s of processor time per arm, against OpenSSL as the bench prints it (4.0.1). Milestone F3b: curves at commit e7a0765, RSA and certificates at commit 05d1481.

OperationBeryl against OpenSSL
X25519 key generation1.21× as fast
X25519 shared secret1.034× as fast
Ed25519 key generation1.23× as fast
Ed25519 sign1.15× as fast
Ed25519 verify1.34× as fast
P-256 key generation1.49× as fast
P-256 ECDSA sign1.28× as fast
P-256 ECDSA verify1.3× as fast
P-256 ECDH shared secret1.056× as fast
P-384 ECDSA verify1.5× as fast
RSA-2048 PKCS #1 v1.5 sign1.11× as fast
RSA-2048 PKCS #1 v1.5 verify1.17× as fast
RSA-2048 OAEP decrypt1.12× as fast
RSA-3072 PKCS #1 v1.5 sign1.092× as fast
RSA-3072 PKCS #1 v1.5 verify1.2× as fast
RSA-4096 PKCS #1 v1.5 sign1.087× as fast
RSA-4096 PKCS #1 v1.5 verify1.21× as fast
Read 256 certificates, keys left until used6.21× as fast
Read 256 certificates, public keys decoded5.27× as fast
Verify a chain with an ECDSA leaf1.34× as fast
Verify a chain with an RSA leaf1.17× as fast

Every row, with operations per second and its paired 95% interval, and how to rerun it: hashes-and-macs.md, ciphers-and-aeads.md, public-key.md and certificates.md.

How it's verified

flowchart LR
    pr(["Pull request"]) --> vectors["Published vectors<br/>NIST CAVP, RFCs, Wycheproof,<br/>x509-limbo"]
    pr --> diff["Differential against OpenSSL<br/>ciphers, curves, RSA,<br/>certificates, random inputs"]
    pr --> ct["Constant time, three ways<br/>dudect timing, Valgrind taint,<br/>LLVM IR checker"]
    pr --> fiat["fiat-crypto equality<br/>every field function and<br/>its assembly"]
    pr --> link["Linkage probe<br/>no libssl, no libcrypto"]
    pr --> fuzz["DER fuzzer<br/>1,000,000 mutated inputs"]
    vectors --> merge(["Merge"])
    diff --> merge
    ct --> merge
    fiat --> merge
    link --> merge
    fuzz --> merge
    classDef step fill:#FFFDF9,stroke:#D9AD7E,color:#211914
    classDef brass fill:#FFC777,stroke:#A93D08,color:#211914
    classDef gate fill:#F47A16,stroke:#A93D08,color:#211914
    class vectors,diff,fiat,link,fuzz step
    class pr,merge brass
    class ct gate

CI runs these on Linux x86-64 and Linux AArch64 for every pull request (ci.yml); the one exception is the hash differential, below.

  • Published vectors and Wycheproof. 4,957 cases for the hashes and MACs, 2,957 for the curves, the NIST CAVP and Wycheproof files for the ciphers and RSA, and 5,947 Wycheproof cases whose keys are read from DER. Every vector file is pinned by sha256 and checked again before it is read.
  • x509-limbo. Of its 9,807 cases, 9,795 run and every one matches its expected result; the 12 skipped are printed with the reason.
  • Differential against OpenSSL. Random inputs through both libraries, with outputs equal byte for byte and altered inputs refused by both: 1,000,000 cases per cipher and for X25519, 200,000 for Ed25519, P-256 and P-384, and 20,000, 5,000 and 2,000 per RSA scheme at 2,048, 3,072 and 4,096 bits. Certificates: 40,960 Certificate Transparency log chains get the same verdict as OpenSSL. The hashes and MACs (1,000,000 cases each) run on request, because they take one to two hours.
  • dudect timing. Each secret operation under a fixed secret against random secrets, with the Welch t statistic required below 4.5: 10,000,000 measurements per class for HMAC and the ciphers and 1,000,000 for the curves on the build Mac, 1,000,000 and 100,000 in CI. Each check is decided by two runs out of three, and CI logs the processor it ran on.
  • Valgrind secret taint. Every key, message and scalar is undefined memory to Memcheck, so any branch or address computed from one is an error. A deliberately leaky comparison must be reported, and CI checks that it is.
  • LLVM IR checker. scripts/check_secret_flow.rb follows secrets through the optimized IR and reports any branch, memory index or division that depends on one. Assembly must use only instructions whose timing does not depend on their operands. Each rule was tested by planting leaks.
  • fiat-crypto. The curve field arithmetic is translated from fiat-crypto's pinned, machine-checked output; every translated function and every one in assembly equals fiat-crypto's C on 100,000,000 inputs (1,000,000 in CI).
  • No OpenSSL. CI builds a probe with -Dwithout_openssl and fails if ldd lists libssl or libcrypto.

[!TIP] These gates, not an outside audit, decide when Beryl becomes a default. Each guide lists its own gates and the command that runs each one.

Beyond cryptography

Beryl owns the security side of an Amber app. The rest is in design; the integration plan lives with the Amber maintainers.

  • Policies: authentication and authorization, written as named, statement-shaped policies and enforced at each step of a request.
  • Supply chain: dependencies, CI actions, images and toolchains pinned and verified by hash.
  • Secrets: kept out of source and delivered to the app by Mo, the secrets broker.
  • Disclosure: a private path to report flaws, and advisories once fixes ship (SECURITY.md).

Questions

Beryl links neither libssl nor libcrypto. Your program still links OpenSSL wherever it or its shards use the OpenSSL parts of Crystal's standard library, such as OpenSSL::SSL::Context for TLS sockets.

P-384 is verification only. RSA key generation and keys of three or more primes are not supported. Certificates with P-521 keys or SHA-1 signatures are not accepted. Each guide lists what it leaves out and why.

Not in the public-key and certificate rows above: since milestone F3b every one is at least as fast as OpenSSL 4.0.1 on the Apple M4, from 1.034× as fast (X25519 key agreement) to 6.21× as fast (reading certificates). Among the symmetric rows, AES-128-CBC decryption at 1 MiB is 1.073× slower. public-key.md and certificates.md have every measured ratio.

Development

minecart install --frozen
/usr/bin/ruby scripts/fetch_vectors.rb --missing   # install the vectors kept out of git (once, after cloning)
crystal-alpha spec                                 # unit specs and every published vector
crystal-alpha spec -Dberyl_portable_hashes         # the same, on the portable hashes alone
crystal-alpha spec -Dberyl_portable_ciphers        # the same, on the portable ciphers alone
crystal-alpha spec --release --tag differential    # against libcrypto, random inputs
crystal-alpha spec --release --tag constant_time   # timing checks of the hashes, ciphers and curves
/usr/bin/ruby scripts/check_secret_flow.rb FILE.ll # compiled-code check (see docs/crypto/hashes-and-macs.md)
/usr/bin/ruby scripts/fetch_vectors.rb             # re-download the vectors and check their pins
/usr/bin/ruby scripts/generate_field_arithmetic.rb --check # the field arithmetic is fiat-crypto pinned output
/usr/bin/ruby scripts/fetch_certificate_corpus.rb  # the CT corpus for the certificate differential (checked by sha256)
/usr/bin/ruby scripts/update_certificate_data.rb PSL_COMMIT # refresh the Mozilla roots and the Public Suffix List

Something off? Open an issue. Questions go to SUPPORT.md, and security problems privately to SECURITY.md.

License

MIT. The README art is built by docs/brand/source/ from the Amberverse character art and the Amber brand fonts.