Overview / Security & product

cryptography

Checksums, digests, authenticated encryption, key derivation, key agreement, signatures and obfuscation, all implemented inside the library.

Always compiled: part of the core build.Include #include <ethereal/cpp/cryptography/crypto_inc.hpp>C layer

crypto_inc.hpp

#include <ethereal/cpp/cryptography/crypto_inc.hpp>

aead.hpp

#include <ethereal/cpp/cryptography/aead.hpp>

Authenticated encryption (ChaCha20-Poly1305) and key derivation (HMAC-SHA-256, HKDF).

Encryption alone protects nothing useful. An attacker who cannot read a message can still change its bytes, and in a stream cipher, which is what networks use, flipping a bit of the cipher text flips the same bit of the plain text. Without authentication the receiver accepts the changed message as legitimate.

So the interface here is encrypt and authenticate. ICipher::Decrypt returns false when the tag does not match, and in that case it returns no plain text at all. There is no way to ask for decryption without the check.

What this library implements: CChaCha20Poly1305, from RFC 8439, checked against the test vectors of the RFC itself. ChaCha20 is additions, XOR and rotations over a fixed state, with no lookup table and no branch that depends on the secret, so it is free of the timing attacks that affect AES in software.

What it does not implement: key agreement. No RSA, no elliptic curve key exchange, no Diffie-Hellman. That part has subtle failure modes that no test catches, and the right answer is an audited library. The key arrives ready at this layer.

A test vector proves that for those inputs the output is the one the standard requires, which catches rotation, byte order, padding and reduction errors. It does not prove constant time under your compiler and options, resistance to fault attacks, or that the key did not leak some other way. For a product where that matters, replace this implementation with an audited one. ICipher exists so that the swap takes about twenty lines.

function

ConstantTimeEquals

Ethereal::Crypto#
bool ConstantTimeEquals( std::span< const std::uint8_t > A, std::span< const std::uint8_t > B ) noexcept;

Compares two blocks without leaking where they differ.

A memcmp stops at the first different byte, and the time it takes tells how many bytes matched. When comparing an authentication tag, that lets an attacker find it one byte at a time: try 256 values for the first byte, see which one took longer, keep it, and move on. That is 16 x 256 attempts instead of 2^128.

This function always reads every byte and accumulates the differences with OR.

A
First block.
B
Second block.
true when both have the same size and the same bytes.
function

SecureZero

Ethereal::Crypto#
void SecureZero( std::span< std::uint8_t > Buffer ) noexcept;

Wipes a block of memory in a way the compiler cannot remove.

A memset over memory that nobody reads again is dead code, and the optimizer is allowed to remove it, leaving the key on the stack. This is known and documented compiler behavior, not a compiler defect.

Buffer
The memory to wipe.
function

HmacSha256

Ethereal::Crypto#
std::array< std::uint8_t, 32 > HmacSha256( std::span< const std::uint8_t > Key, std::span< const std::uint8_t > Message );

HMAC-SHA-256, from RFC 2104.

A keyed message authenticator. It is here because key derivation needs it, and because a hash of the key concatenated with the message is not an HMAC. A raw SHA-256 allows extending the message without knowing the key, and whoever trusts that construction as an HMAC inherits the weakness.

Key
The key, of any size.
Message
The message.
The 32 byte tag.
function

HkdfExtract

Ethereal::Crypto#
std::array< std::uint8_t, 32 > HkdfExtract( std::span< const std::uint8_t > Salt, std::span< const std::uint8_t > Secret );

HKDF-Extract, from RFC 5869.

Concentrates a secret of uneven quality, such as the output of a key agreement, into a uniform 32 byte key.

Salt
The salt. It is the HMAC key.
Secret
The input secret.
The pseudorandom key (PRK).
function

HkdfExpand

Ethereal::Crypto#
bool HkdfExpand( std::span< const std::uint8_t > Prk, std::span< const std::uint8_t > Info, std::span< std::uint8_t > OutKey );

HKDF-Expand, from RFC 5869.

Stretches a uniform key into as many keys as needed, each bound to a different label.

This is what makes two keys derived from the same secret independent. Deriving by concatenation with a label looks the same and is not: without the HKDF construction there is no guarantee that knowing one key does not help find the other.

Prk
The pseudorandom key from HkdfExtract.
Info
The label. Different labels give independent keys.
OutKey
Where to write. Up to 8160 bytes.
false when OutKey is larger than the construction allows.
class

ICipher

Ethereal::Crypto#

An authenticated cipher with associated data.

class ICipher

This is the swap point. Whoever wants an audited implementation, such as libsodium, BearSSL or mbedTLS, writes a class derived from this one and installs it. Nothing else in the product changes.

destructor

~ICipher

#
virtual ~ICipher() = default;

Virtual destructor.

function

KeySize

#
virtual std::size_t KeySize() const noexcept = 0;

Key size, in bytes.

function

NonceSize

#
virtual std::size_t NonceSize() const noexcept = 0;

Nonce size, in bytes.

function

TagSize

#
virtual std::size_t TagSize() const noexcept = 0;

Authentication tag size, in bytes.

function

SetKey

#
virtual bool SetKey( std::span< const std::uint8_t > Key ) = 0;

Installs the key.

Key
The key.
false when the size is not KeySize, or when the library is not licensed (the built-in ciphers).
function

Encrypt

#
virtual bool Encrypt( std::span< const std::uint8_t > Nonce, std::span< const std::uint8_t > AssociatedData, std::span< const std::uint8_t > PlainText, std::vector< std::uint8_t > & OutCipherText ) = 0;

Encrypts and authenticates.

Nonce
Single use value. Reusing it with the same key destroys the security completely. See the warning in Ethereal::Network::CSecureTransform.
AssociatedData
Authenticated but not encrypted. This is where data that must be readable in clear and still cannot be changed goes: a routing header, a connection identifier.
PlainText
The data to encrypt.
OutCipherText
Receives the cipher text followed by the tag, that is PlainText.size() + TagSize bytes.
false when there is no key or the nonce has the wrong size.
function

Decrypt

#
virtual bool Decrypt( std::span< const std::uint8_t > Nonce, std::span< const std::uint8_t > AssociatedData, std::span< const std::uint8_t > CipherText, std::vector< std::uint8_t > & OutPlainText ) = 0;

Checks the tag and decrypts.

The tag is checked before any plain text is returned. Returning the decrypted data and only then reporting a mismatch is the classic way to turn an authenticated cipher into one that authenticates nothing: the caller has already used the data.

Nonce
Single use value, the one used to encrypt.
AssociatedData
The associated data, the same used to encrypt.
CipherText
Cipher text followed by the tag.
OutPlainText
Receives the plain text.
false when the tag does not match. OutPlainText is left empty.
function

Name

#
virtual const char * Name() const noexcept = 0;

Name of the implementation, for logs and diagnostics.

class

CChaCha20Poly1305

Ethereal::Crypto#
class CChaCha20Poly1305 final : public ICipher
const

KEY_SIZE

#
static constexpr std::size_t KEY_SIZE = 32;

Key size, in bytes.

const

NONCE_SIZE

#
static constexpr std::size_t NONCE_SIZE = 12;

Nonce size, in bytes.

const

TAG_SIZE

#
static constexpr std::size_t TAG_SIZE = 16;

Tag size, in bytes.

constructor

CChaCha20Poly1305

#
CChaCha20Poly1305() noexcept;

Constructor. No key installed.

function

KeySize

#
std::size_t KeySize() const noexcept override

Key size, in bytes.

function

NonceSize

#
std::size_t NonceSize() const noexcept override

Nonce size, in bytes.

function

TagSize

#
std::size_t TagSize() const noexcept override

Tag size, in bytes.

function

Name

#
const char * Name() const noexcept override

Name of the implementation.

function

SetKey

#
bool SetKey( std::span< const std::uint8_t > Key ) override;

Installs the key. See ICipher::SetKey.

function

Encrypt

#
bool Encrypt( std::span< const std::uint8_t > Nonce, std::span< const std::uint8_t > AssociatedData, std::span< const std::uint8_t > PlainText, std::vector< std::uint8_t > & OutCipherText ) override;

Encrypts and authenticates. See ICipher::Encrypt.

function

Decrypt

#
bool Decrypt( std::span< const std::uint8_t > Nonce, std::span< const std::uint8_t > AssociatedData, std::span< const std::uint8_t > CipherText, std::vector< std::uint8_t > & OutPlainText ) override;

Checks the tag and decrypts. See ICipher::Decrypt.

function

ChaCha20Xor

#
static void ChaCha20Xor( std::span< const std::uint8_t > Key, std::span< const std::uint8_t > Nonce, const std::uint32_t uCounter, std::span< std::uint8_t > Data ) noexcept;

The ChaCha20 key stream, applied with XOR over Data.

Key
32 byte key.
Nonce
12 byte nonce.
uCounter
Initial block counter. The RFC uses 0 to derive the tag key and 1 for the content.
Data
The data, changed in place.
function

Poly1305

#
static void Poly1305( std::span< const std::uint8_t > OneTimeKey, std::span< const std::uint8_t > Message, std::span< std::uint8_t > OutTag ) noexcept;

The Poly1305 authenticator.

OneTimeKey
32 bytes, and truly single use: reusing it with different messages reveals the inner key and allows forgery.
Message
The authenticated message.
OutTag
Receives the 16 byte tag.

aes.hpp

#include <ethereal/cpp/cryptography/aes.hpp>

AES block encryption (FIPS 197) and AES-GCM (NIST SP 800-38D).

Two paths with the same bytes, chosen at run time when the key is installed:

  • Constant time scalar code. AES is bitsliced (four blocks per pass over eight 64 bit words, the S-box as a Boolean circuit) and GHASH multiplies with integer products masked so that no carry crosses between bits. No table is indexed by a secret and no branch depends on one. The technique is the one of BearSSL aes_ct64 and ghash_ctmul64.
  • AES-NI and PCLMULQDQ, when Ethereal::Cpu::AesClmulActive says so. Hardware AES has no table either.

Only encryption of the block cipher is offered: GCM uses the forward direction for both encryption and decryption.

class

CAes

Ethereal::Crypto#
class CAes
const

BLOCK_SIZE

#
static constexpr std::size_t BLOCK_SIZE = 16;

Block size, in bytes.

constructor

CAes

#
CAes() noexcept;

Constructor. No key installed.

function

SetKey

#
bool SetKey( std::span< const std::uint8_t > Key ) noexcept;

Expands and installs the key, and chooses the path.

Key
16, 24 or 32 bytes.
false when the size is not 16, 24 or 32, or when the library is not licensed. No key is installed then.
function

EncryptBlock

#
void EncryptBlock( std::span< const std::uint8_t, 16 > In, std::span< std::uint8_t, 16 > Out ) const noexcept;

Encrypts one 16 byte block. In and Out may be the same memory.

In
The plain block.
Out
Receives the cipher block. Zeros when no key is installed.
function

HasKey

#
bool HasKey() const noexcept

true when a key is installed.

function

UsesHardware

#
bool UsesHardware() const noexcept

true when the key runs on AES-NI (diagnostics and tests).

class

CAesGcm

Ethereal::Crypto#

AES in Galois/Counter Mode, with a 96 bit nonce and a 128 bit tag.

class CAesGcm final : public ICipher

The AEAD of TLS 1.3 (TLS_AES_128_GCM_SHA256 and TLS_AES_256_GCM_SHA384). The key size is fixed at construction: 16 (AES-128-GCM), 24 or 32 (AES-256-GCM).

Decryption checks the tag first, over the cipher text, and only then decrypts. On a mismatch no plain text is produced. The tag comparison is constant time.

Limits of SP 800-38D: at most 2^36 - 32 bytes of text per call. The nonce must never repeat under the same key.

Thread safety: Encrypt and Decrypt do not change the object and may run in several threads at once with the same key. SetKey may not run concurrently with anything.
const

NONCE_SIZE

#
static constexpr std::size_t NONCE_SIZE = 12;

Nonce size, in bytes.

const

TAG_SIZE

#
static constexpr std::size_t TAG_SIZE = 16;

Tag size, in bytes.

const

MAX_TEXT

#
static constexpr std::uint64_t MAX_TEXT = ( std::uint64_t( 1 ) << 36 ) - 32u;

Largest text per call, in bytes (SP 800-38D, 5.2.1.1).

constructor

CAesGcm

#
explicit CAesGcm( std::size_t uKeySize = 16 ) noexcept;

Constructor. No key installed.

uKeySize
16, 24 or 32. Any other value makes every SetKey fail.
function

KeySize

#
std::size_t KeySize() const noexcept override

Key size, in bytes, as given to the constructor.

function

NonceSize

#
std::size_t NonceSize() const noexcept override

Nonce size, in bytes.

function

TagSize

#
std::size_t TagSize() const noexcept override

Tag size, in bytes.

function

Name

#
const char * Name() const noexcept override;

Name of the implementation: "AES-128-GCM", "AES-192-GCM" or "AES-256-GCM".

function

SetKey

#
bool SetKey( std::span< const std::uint8_t > Key ) override;

Installs the key. See ICipher::SetKey.

function

Encrypt

#
bool Encrypt( std::span< const std::uint8_t > Nonce, std::span< const std::uint8_t > AssociatedData, std::span< const std::uint8_t > PlainText, std::vector< std::uint8_t > & OutCipherText ) override;

Encrypts and authenticates. See ICipher::Encrypt.

function

Decrypt

#
bool Decrypt( std::span< const std::uint8_t > Nonce, std::span< const std::uint8_t > AssociatedData, std::span< const std::uint8_t > CipherText, std::vector< std::uint8_t > & OutPlainText ) override;

Checks the tag and decrypts. See ICipher::Decrypt.

function

EncryptInPlace

#
bool EncryptInPlace( std::span< const std::uint8_t > Nonce, std::span< const std::uint8_t > AssociatedData, std::span< std::uint8_t > Data, std::span< std::uint8_t > OutTag ) const noexcept;

Encrypts Data in place and writes the tag apart.

Nonce
12 bytes.
AssociatedData
Authenticated, not encrypted.
Data
Plain text in, cipher text out.
OutTag
Receives the 16 byte tag.
false when there is no key, the nonce or tag buffer has the wrong size, or Data is longer than MAX_TEXT. Nothing is written then.
function

DecryptInPlace

#
bool DecryptInPlace( std::span< const std::uint8_t > Nonce, std::span< const std::uint8_t > AssociatedData, std::span< std::uint8_t > Data, std::span< const std::uint8_t > Tag ) const noexcept;

Checks the tag, then decrypts Data in place.

Nonce
12 bytes.
AssociatedData
The associated data used to encrypt.
Data
Cipher text in, plain text out on success.
Tag
The 16 byte tag.
false when the tag does not match or a size is invalid. Data is left holding the cipher text, unchanged, in that case.
function

UsesHardware

#
bool UsesHardware() const noexcept

true when the installed key runs on AES-NI and PCLMULQDQ (diagnostics and tests).

crc.hpp

#include <ethereal/cpp/cryptography/crc.hpp>
struct

CRC32

Ethereal::Crypto#
C++
struct CRC32
{
    /// @brief    Computes the CRC32 (IEEE 802.3) of a binary buffer.
    ///            Processes the data byte by byte with a CRC32 table compatible with the IEEE 802.3 standard.
    ///            The computation uses the initial value 0xFFFFFFFF and the standard final XOR.
    ///
    /// @param Data        Input binary buffer.
    ///
    /// @return            Computed CRC32 value.
    ///
    static std::uint32_t Get( std::span<const std::uint8_t> Data );


    /// @brief    Computes the CRC32 (IEEE 802.3) of a string.
    ///            Views the string as binary data without a copy and computes the CRC32 over those bytes.
    ///
    /// @param Str        Input string.
    ///
    /// @return            Computed CRC32 value.
    ///
    static std::uint32_t Get( const char * Str );


    /// @brief    Computes the CRC32 and writes the value as hexadecimal (lowercase).
    ///            The CRC32 value is written to the output string as 8 lowercase hexadecimal digits, most significant first, like printf( "%08x" ), on any machine.
    ///            Does not allocate a return value.
    ///
    /// @param Str        Input string.
    /// @param StrOut    Output string that receives the CRC in hexadecimal.
    ///
    static void GetAs_String( const char * Str, std::string & StrOut );
};
struct

CRC64

Ethereal::Crypto#
C++
struct CRC64
{
    /// @brief    Computes the CRC64 (ECMA-182) of a binary buffer.
    ///            Processes the data byte by byte with a CRC64 table compatible with the ECMA-182 standard.
    ///            This algorithm uses no initial XOR and no final XOR.
    ///
    /// @param Data        Input binary buffer.
    ///
    /// @return            Computed CRC64 value.
    ///
    static std::uint64_t Get( std::span<const std::uint8_t> Data );


    /// @brief    Computes the CRC64 (ECMA-182) of a string.
    ///            Views the string as binary data without a copy and computes the CRC64 over those bytes.
    ///
    /// @param Str        Input string.
    ///
    /// @return            Computed CRC64 value.
    ///
    static std::uint64_t Get( const char * Str );


    /// @brief    Computes the CRC64 and writes the value as hexadecimal (lowercase).
    ///            The CRC64 value is written to the output string as 16 lowercase hexadecimal digits, most significant first, like printf( "%016llx" ), on any machine.
    ///            Does not allocate a return value.
    ///
    /// @param Str        Input string.
    /// @param StrOut    Output string that receives the CRC in hexadecimal.
    ///
    static void GetAs_String( const char * Str, std::string & StrOut );
};

ecc.hpp

#include <ethereal/cpp/cryptography/ecc.hpp>

NIST prime curves: P-256 (ECDH, ECDSA) and P-384 (ECDSA verification).

What TLS 1.3 and X.509 need from the NIST curves. P-256 does key agreement (secp256r1 key share), signs and verifies. P-384 only verifies, since it appears in certificate chains and nothing here signs with it.

ECDSA takes the digest, not the message. The caller hashes (SHA-256 for P-256 in TLS, SHA-384 for P-384), and the digest is truncated to the bit length of the group order as FIPS 186-4 section 6.4 requires.

Signatures exist in two forms. The raw one is r || s, each a big-endian integer of the scalar size. The DER one is the ECDSA-Sig-Value of RFC 3279 (SEQUENCE of two INTEGER), which is what TLS and X.509 carry. Decoding is strict DER.

struct

P256

Ethereal::Crypto#
C++
struct P256
{
    /// @brief  Scalar (private key, r, s) size, in bytes.
    static constexpr std::size_t SCALAR_SIZE = 32;

    /// @brief  Uncompressed public key size, in bytes (0x04 || X || Y).
    static constexpr std::size_t PUBLIC_KEY_SIZE = 65;

    /// @brief  Raw signature size, in bytes (r || s).
    static constexpr std::size_t SIGNATURE_SIZE = 64;

    /// @brief  Largest DER signature, in bytes.
    static constexpr std::size_t MAX_DER_SIGNATURE_SIZE = 72;


    /// @brief  The 32 byte private key.
    using PrivateKey    = std::array< std::uint8_t, SCALAR_SIZE >;

    /// @brief  The 65 byte uncompressed public key.
    using PublicKey        = std::array< std::uint8_t, PUBLIC_KEY_SIZE >;

    /// @brief  The 32 byte ECDH shared secret (the x coordinate).
    using SharedSecret    = std::array< std::uint8_t, SCALAR_SIZE >;

    /// @brief  The 64 byte raw signature r || s.
    using Signature        = std::array< std::uint8_t, SIGNATURE_SIZE >;


    ///
    /// GenerateKeyPair
    ///
    /// @brief  Draws a private key in [1, n-1] from the system entropy source and
    ///          computes its public key.
    ///
    /// @param OutPrivate    Receives the private key.
    /// @param OutPublic    Receives the public key.
    ///
    /// @return  false when the entropy source failed. Do not use the outputs.
    ///
    ETL_API_NOIGN    static bool GenerateKeyPair( PrivateKey & OutPrivate, PublicKey & OutPublic ) ETL_NOEXCEPT;


    ///
    /// DerivePublicKey
    ///
    /// @brief  Computes the public key d * G.
    ///
    /// @param Private        The private key.
    /// @param OutPublic    Receives the public key.
    ///
    /// @return  false when the private key is not in [1, n-1].
    ///
    ETL_API_NOIGN    static bool DerivePublicKey( const PrivateKey & Private, PublicKey & OutPublic ) ETL_NOEXCEPT;


    ///
    /// IsValidPublicKey
    ///
    /// @brief  Checks a SEC1 uncompressed public key.
    ///
    /// @details  Exactly 65 bytes, first byte 0x04, both coordinates below p, and
    ///            the point on the curve. The curve has prime order and cofactor 1,
    ///            so a point on it is in the group. The point at infinity has no
    ///            65 byte encoding.
    ///
    /// @param Public    The encoded key.
    ///
    /// @return  true when the key is usable.
    ///
    ETL_API_NOIGN    static bool IsValidPublicKey( std::span< const std::uint8_t > Public ) ETL_NOEXCEPT;


    ///
    /// DeriveShared
    ///
    /// @brief  ECDH: the x coordinate of d * Q.
    ///
    /// @details  Rejects an invalid peer key (see @ref IsValidPublicKey) and a
    ///            private key out of [1, n-1]. That is the TLS 1.3 secp256r1 key
    ///            share (RFC 8446 section 7.4.2).
    ///
    /// @param Private        Our private key.
    /// @param PeerPublic    The peer public key, 65 bytes.
    /// @param Out            Receives the shared secret. Zeroed on failure.
    ///
    /// @return  false on an invalid input.
    ///
    ETL_API_NOIGN    static bool DeriveShared( const PrivateKey & Private,
                                              std::span< const std::uint8_t > PeerPublic,
                                              SharedSecret & Out ) ETL_NOEXCEPT;


    ///
    /// Sign
    ///
    /// @brief  ECDSA signature of a digest, with the RFC 6979 nonce.
    ///
    /// @param Private    The private key.
    /// @param Digest    The message digest, computed by the caller. Longer than 32
    ///                    bytes is truncated to the leftmost 256 bits.
    /// @param Out        Receives r || s.
    ///
    /// @return  false when the private key is not in [1, n-1] or the digest is
    ///           empty.
    ///
    ETL_API_NOIGN    static bool Sign( const PrivateKey & Private, std::span< const std::uint8_t > Digest,
                                      Signature & Out ) ETL_NOEXCEPT;


    ///
    /// Verify
    ///
    /// @brief  Verifies a raw ECDSA signature over a digest.
    ///
    /// @details  Rejects r or s out of [1, n-1], an invalid public key and an
    ///            empty digest. Does not tell the reasons apart.
    ///
    /// @param Public    The public key, 65 bytes.
    /// @param Digest    The message digest.
    /// @param Sig        The raw signature r || s.
    ///
    /// @return  true only for a valid signature.
    ///
    ETL_API_NOIGN    static bool Verify( std::span< const std::uint8_t > Public,
                                        std::span< const std::uint8_t > Digest,
                                        const Signature & Sig ) ETL_NOEXCEPT;


    ///
    /// VerifyDer
    ///
    /// @brief  Verifies a DER encoded ECDSA signature (the TLS and X.509 form).
    ///
    /// @param Public    The public key, 65 bytes.
    /// @param Digest    The message digest.
    /// @param DerSig    The ECDSA-Sig-Value.
    ///
    /// @return  true only for a strict DER, valid signature.
    ///
    ETL_API_NOIGN    static bool VerifyDer( std::span< const std::uint8_t > Public,
                                           std::span< const std::uint8_t > Digest,
                                           std::span< const std::uint8_t > DerSig ) ETL_NOEXCEPT;


    ///
    /// SignatureFromDer
    ///
    /// @brief  Decodes a DER ECDSA-Sig-Value into r || s.
    ///
    /// @details  Strict DER: one SEQUENCE with a short form length that covers the
    ///            input exactly, two INTEGER, minimal encoding (no leading zero
    ///            byte unless the next byte has the top bit set), no negative
    ///            value, and r and s in [1, n-1].
    ///
    /// @param Der    The encoding.
    /// @param Out    Receives r || s.
    ///
    /// @return  false for anything else.
    ///
    ETL_API_NOIGN    static bool SignatureFromDer( std::span< const std::uint8_t > Der, Signature & Out ) ETL_NOEXCEPT;


    ///
    /// SignatureToDer
    ///
    /// @brief  Encodes r || s as a DER ECDSA-Sig-Value.
    ///
    /// @param Sig    The raw signature.
    /// @param Out    The output buffer. @ref MAX_DER_SIGNATURE_SIZE always fits.
    ///
    /// @return  The number of bytes written. 0 when the buffer is too small.
    ///
    ETL_API_NOIGN    static std::size_t SignatureToDer( const Signature & Sig, std::span< std::uint8_t > Out ) ETL_NOEXCEPT;


    ///
    /// SelfCheckConstants
    ///
    /// @brief  Checks the transcribed curve constants.
    ///
    /// @details  p must equal 2^256 - 2^224 + 2^192 + 2^96 - 1 (built here from
    ///            the formula), G must be on the curve, and n * G must be the
    ///            point at infinity.
    ///
    /// @return  true when every check passes.
    ///
    ETL_API_NOIGN    static bool SelfCheckConstants() ETL_NOEXCEPT;
};

The NIST P-256 curve (secp256r1, prime256v1): ECDH and ECDSA.

Public keys are SEC1 uncompressed points: 0x04 || X || Y, 65 bytes. Private keys are 32 byte big-endian scalars in [1, n-1].

Constant time where there is a secret. Key generation, public key derivation, ECDH and signing run the same instructions and touch the same addresses for any private key or nonce: a fixed 4 bit window over the whole scalar, complete addition formulas (Renes, Costello and Batina 2016, no special case for doubling or for the point at infinity), a table lookup that reads all 16 entries and keeps one by mask, and inversion by Fermat (a^(n-2), fixed exponent). Verification handles only public data.
Signing is deterministic (RFC 6979, HMAC-SHA256), so no random generator is involved and a weak one cannot leak the key. With a SHA-256 digest the signature matches the RFC 6979 vectors byte for byte. With a digest of another size the nonce still follows RFC 6979 with HMAC-SHA256 and the signature is valid, but it will not match a signer that uses the digest hash for the HMAC.
struct

P384

Ethereal::Crypto#
C++
struct P384
{
    /// @brief  Scalar (r, s) size, in bytes.
    static constexpr std::size_t SCALAR_SIZE = 48;

    /// @brief  Uncompressed public key size, in bytes (0x04 || X || Y).
    static constexpr std::size_t PUBLIC_KEY_SIZE = 97;

    /// @brief  Raw signature size, in bytes (r || s).
    static constexpr std::size_t SIGNATURE_SIZE = 96;

    /// @brief  Largest DER signature, in bytes.
    static constexpr std::size_t MAX_DER_SIGNATURE_SIZE = 104;


    /// @brief  The 97 byte uncompressed public key.
    using PublicKey        = std::array< std::uint8_t, PUBLIC_KEY_SIZE >;

    /// @brief  The 96 byte raw signature r || s.
    using Signature        = std::array< std::uint8_t, SIGNATURE_SIZE >;


    ///
    /// IsValidPublicKey
    ///
    /// @brief  Checks a SEC1 uncompressed public key (97 bytes, 0x04, both
    ///          coordinates below p, point on the curve).
    ///
    /// @param Public    The encoded key.
    ///
    /// @return  true when the key is usable.
    ///
    ETL_API_NOIGN    static bool IsValidPublicKey( std::span< const std::uint8_t > Public ) ETL_NOEXCEPT;


    ///
    /// Verify
    ///
    /// @brief  Verifies a raw ECDSA signature over a digest.
    ///
    /// @param Public    The public key, 97 bytes.
    /// @param Digest    The message digest. Longer than 48 bytes is truncated.
    /// @param Sig        The raw signature r || s.
    ///
    /// @return  true only for a valid signature.
    ///
    ETL_API_NOIGN    static bool Verify( std::span< const std::uint8_t > Public,
                                        std::span< const std::uint8_t > Digest,
                                        const Signature & Sig ) ETL_NOEXCEPT;


    ///
    /// VerifyDer
    ///
    /// @brief  Verifies a DER encoded ECDSA signature.
    ///
    /// @param Public    The public key, 97 bytes.
    /// @param Digest    The message digest.
    /// @param DerSig    The ECDSA-Sig-Value.
    ///
    /// @return  true only for a strict DER, valid signature.
    ///
    ETL_API_NOIGN    static bool VerifyDer( std::span< const std::uint8_t > Public,
                                           std::span< const std::uint8_t > Digest,
                                           std::span< const std::uint8_t > DerSig ) ETL_NOEXCEPT;


    ///
    /// SignatureFromDer
    ///
    /// @brief  Decodes a DER ECDSA-Sig-Value into r || s (same rules as
    ///          @ref P256::SignatureFromDer).
    ///
    /// @param Der    The encoding.
    /// @param Out    Receives r || s.
    ///
    /// @return  false for anything that is not strict DER with r, s in [1, n-1].
    ///
    ETL_API_NOIGN    static bool SignatureFromDer( std::span< const std::uint8_t > Der, Signature & Out ) ETL_NOEXCEPT;


    ///
    /// SignatureToDer
    ///
    /// @brief  Encodes r || s as a DER ECDSA-Sig-Value.
    ///
    /// @param Sig    The raw signature.
    /// @param Out    The output buffer. @ref MAX_DER_SIGNATURE_SIZE always fits.
    ///
    /// @return  The number of bytes written. 0 when the buffer is too small.
    ///
    ETL_API_NOIGN    static std::size_t SignatureToDer( const Signature & Sig, std::span< std::uint8_t > Out ) ETL_NOEXCEPT;


    ///
    /// SelfCheckConstants
    ///
    /// @brief  Checks the transcribed curve constants.
    ///
    /// @details  p must equal 2^384 - 2^128 - 2^96 + 2^32 - 1, G must be on the
    ///            curve, and n * G must be the point at infinity.
    ///
    /// @return  true when every check passes.
    ///
    ETL_API_NOIGN    static bool SelfCheckConstants() ETL_NOEXCEPT;
};

The NIST P-384 curve (secp384r1): ECDSA verification only.

Public keys are SEC1 uncompressed points, 97 bytes. Nothing here signs with P-384 or does ECDH with it.

Not constant time, on purpose: verification touches no secret. The public key, the digest and the signature are all public.

hash.hpp

#include <ethereal/cpp/cryptography/hash.hpp>
function

Hash64

Ethereal::Hash#
std::uint64_t Hash64( ETL_CPTR DataPtr, std::size_t DataLength ) noexcept;

Generates a 64-bit hash from a block of memory.

Computes a fast hash with strong dispersion (avalanche), processing the data in 64-bit blocks and then the remaining bytes. The algorithm uses:

  • Large primes to spread the bits evenly.
  • Bitwise rotations to mix the high and low bits.
  • A final avalanche step inspired by MurmurHash, reducing correlation and trivial collisions.

The function is deterministic for the same input + seed (the seed comes from the build and the product license), does not allocate, and fits performance critical paths.

DataPtr
Pointer to the block of data to process.
DataLength
Size of the block in bytes.
64-bit hash of the given data.

hmac.hpp

#include <ethereal/cpp/cryptography/hmac.hpp>

HMAC (RFC 2104) and HKDF (RFC 5869) over SHA-256, SHA-384 and SHA-512.

One shot and incremental HMAC, and HKDF Extract and Expand. Written for TLS 1.3, whose key schedule is HKDF over the hash of the cipher suite (SHA-256 or SHA-384).

The HMAC objects are copyable by value: a copy carries the keyed state, so a tag over a prefix can be taken from a copy while the original keeps absorbing.

The older Ethereal::Crypto::HmacSha256, Ethereal::Crypto::HkdfExtract and Ethereal::Crypto::HkdfExpand in aead.hpp stay as they are. These classes give the same bytes for SHA-256.

class

CHmac

Ethereal::Crypto#

HMAC over one of the incremental SHA digests.

template< class TStream, std::size_t uBlock, std::size_t uDigest > class CHmac

Use the aliases CHmacSha256, CHmacSha384 and CHmacSha512. Only those three are instantiated in the library.

The key is absorbed once, in SetKey: the inner and outer padded blocks are compressed and their states kept. Each tag after that costs the message plus two compressions.

After Final the object is back at the keyed initial state, so it can authenticate another message with the same key.

The destructor wipes the keyed states with SecureZero.

TStream
The incremental digest (CSha256Stream, CSha384Stream or CSha512Stream).
uBlock
The block size of the digest, in bytes.
uDigest
The digest size, in bytes.
Not thread safe. Copy it to use the same key in several threads.
const

DigestSize

#
static constexpr std::size_t DigestSize = uDigest;

Tag size, in bytes.

const

BlockSize

#
static constexpr std::size_t BlockSize = uBlock;

Block size of the digest, in bytes.

constructor

CHmac

#
CHmac() noexcept;

Constructor. The key is empty (a valid HMAC key) until SetKey.

constructor

CHmac

#
explicit CHmac( std::span< const std::uint8_t > Key ) noexcept;

Constructor with the key.

Key
The key, of any size.
constructor

CHmac

#
CHmac( const CHmac & Other ) noexcept = default;

Copy constructor. Copies the keyed state.

operator

operator=

#
CHmac & operator=( const CHmac & Other ) noexcept = default;

Copy assignment. Copies the keyed state.

function

SetKey

#
void SetKey( std::span< const std::uint8_t > Key ) noexcept;

Installs a key and starts an empty message.

A key longer than BlockSize is replaced by its digest, as RFC 2104 requires.

Key
The key, of any size.
function

Update

#
void Update( std::span< const std::uint8_t > Data ) noexcept;

Appends bytes to the message.

Data
The next piece.
function

Final

#
std::array< std::uint8_t, uDigest > Final() noexcept;

Tag of the accumulated message. The object goes back to the keyed initial state.

function

Get

#
static std::array< std::uint8_t, uDigest > Get( std::span< const std::uint8_t > Key, std::span< const std::uint8_t > Message ) noexcept;

One shot HMAC.

Key
The key, of any size.
Message
The message.
The tag.
alias

CHmacSha256

Ethereal::Crypto#
using CHmacSha256 = CHmac< CSha256Stream, 64, 32 >;

HMAC-SHA-256 (RFC 2104, RFC 4231). 32 byte tag, 64 byte block.

alias

CHmacSha384

Ethereal::Crypto#
using CHmacSha384 = CHmac< CSha384Stream, 128, 48 >;

HMAC-SHA-384 (RFC 2104, RFC 4231). 48 byte tag, 128 byte block.

alias

CHmacSha512

Ethereal::Crypto#
using CHmacSha512 = CHmac< CSha512Stream, 128, 64 >;

HMAC-SHA-512 (RFC 2104, RFC 4231). 64 byte tag, 128 byte block.

const

extern template class CHmac< CSha256Stre

Ethereal::Crypto#
extern template class CHmac< CSha256Stream, 64, 32 >;

The three instantiations live in hmac.cpp.

const

extern template class CHmac< CSha384Stre

Ethereal::Crypto#
extern template class CHmac< CSha384Stream, 128, 48 >;
const

extern template class CHmac< CSha512Stre

Ethereal::Crypto#
extern template class CHmac< CSha512Stream, 128, 64 >;
struct

CHkdf

Ethereal::Crypto#
C++
template< class THmac >
struct CHkdf
{
    /// @brief  Size of the pseudorandom key and of each expansion block, in bytes.
    static constexpr std::size_t HashSize = THmac::DigestSize;

    /// @brief  Largest output of @ref Expand: 255 blocks.
    static constexpr std::size_t MaxOutput = 255u * THmac::DigestSize;


    ///
    /// Extract
    ///
    /// @brief  HKDF-Extract: PRK = HMAC( Salt, Ikm ).
    ///
    /// @param Salt        The salt. Empty means a string of HashSize zeros (the same HMAC key).
    /// @param Ikm        The input keying material.
    ///
    /// @return  The pseudorandom key (PRK).
    ///
    static std::array< std::uint8_t, THmac::DigestSize > Extract( std::span< const std::uint8_t > Salt,
                                                                   std::span< const std::uint8_t > Ikm ) noexcept;


    ///
    /// Expand
    ///
    /// @brief  HKDF-Expand: fills @p Out with T(1) | T(2) | ...
    ///
    /// @param Prk        The pseudorandom key. At least HashSize bytes (RFC 5869, 2.3).
    /// @param Info        The context label.
    /// @param Out        Where to write. Up to @ref MaxOutput bytes. May be empty.
    ///
    /// @return  false when @p Prk is shorter than HashSize or @p Out is larger than
    ///          @ref MaxOutput. @p Out is left untouched in that case.
    ///
    ETL_API_NOIGN    static bool Expand( std::span< const std::uint8_t > Prk,
                                        std::span< const std::uint8_t > Info,
                                        std::span< std::uint8_t > Out ) noexcept;
};

HKDF over one HMAC.

Use the aliases CHkdfSha256, CHkdfSha384 and CHkdfSha512. Stateless: two static functions.

Intermediate values (the T(i) blocks and the HMAC states) are wiped with SecureZero before returning.

THmac
The HMAC (CHmacSha256, CHmacSha384 or CHmacSha512).
alias

CHkdfSha256

Ethereal::Crypto#
using CHkdfSha256 = CHkdf< CHmacSha256 >;

HKDF with HMAC-SHA-256.

alias

CHkdfSha384

Ethereal::Crypto#
using CHkdfSha384 = CHkdf< CHmacSha384 >;

HKDF with HMAC-SHA-384.

alias

CHkdfSha512

Ethereal::Crypto#
using CHkdfSha512 = CHkdf< CHmacSha512 >;

HKDF with HMAC-SHA-512.

const

extern template struct CHkdf< CHmacSha25

Ethereal::Crypto#
extern template struct CHkdf< CHmacSha256 >;

The three instantiations live in hmac.cpp.

const

extern template struct CHkdf< CHmacSha38

Ethereal::Crypto#
extern template struct CHkdf< CHmacSha384 >;
const

extern template struct CHkdf< CHmacSha51

Ethereal::Crypto#
extern template struct CHkdf< CHmacSha512 >;

rsa.hpp

#include <ethereal/cpp/cryptography/rsa.hpp>

RSA signature verification: PKCS #1 v1.5 and PSS (RFC 8017).

What TLS 1.3 and X.509 need from RSA: checking certificate signatures (mostly PKCS #1 v1.5) and the CertificateVerify of a server with an RSA key (PSS, rsa_pss_rsae_*). Nothing here signs, decrypts or encrypts.

The caller hashes. The verification takes the digest, already computed with the hash the signature names.

enum

ERsaHash

Ethereal::Crypto#
C++
enum ERsaHash
{
    /// @brief  SHA-256, 32 byte digest.
    eRsaHash_Sha256    = 0,

    /// @brief  SHA-384, 48 byte digest.
    eRsaHash_Sha384    = 1,

    /// @brief  SHA-512, 64 byte digest.
    eRsaHash_Sha512    = 2
};

The hash a signature was made with.

class

CRsaPublicKey

Ethereal::Crypto#

An RSA public key (n, e) that verifies signatures.

class CRsaPublicKey

The modulus must have 2048 to 8192 bits. Below 2048 bits RSA is no longer accepted by the CA/Browser Forum nor by NIST (SP 800-131A), and a key that small in a TLS chain is an error to report, not a case to support. Above 8192 bits no public CA issues keys, and the bound keeps the working buffers fixed (no allocation).

The exponent must be odd, at least 3 and below n. 65537 is the usual value.

Not constant time, on purpose. Verification uses only public values (the key, the signature, the digest), so the run time leaks nothing. The exponentiation is Montgomery square and multiply over the bits of e, and it branches on those bits.
About 3 KB, with no allocation. Copyable.
const

MIN_MODULUS_BITS

#
static constexpr std::size_t MIN_MODULUS_BITS = 2048;

Smallest accepted modulus, in bits.

const

MAX_MODULUS_BITS

#
static constexpr std::size_t MAX_MODULUS_BITS = 8192;

Largest accepted modulus, in bits.

const

PSS_SALT_ANY

#
static constexpr std::size_t PSS_SALT_ANY = static_cast< std::size_t >( -1 );

Salt size for VerifyPss that accepts any salt length, recovered from the encoding (OpenSSL calls it "auto").

constructor

CRsaPublicKey

#
CRsaPublicKey() noexcept;

Constructor. No key loaded.

function

Load

#
bool Load( std::span< const std::uint8_t > Modulus, std::span< const std::uint8_t > Exponent ) noexcept;

Loads (n, e) from big-endian bytes, as in the X.509 RSAPublicKey. Leading zero bytes are allowed (DER puts one in front of n).

Modulus
n, big-endian.
Exponent
e, big-endian.
false when the modulus is out of 2048 to 8192 bits, n is even, or e is even, below 3 or not below n, or when the library is not licensed. The object is then empty.
function

IsLoaded

#
bool IsLoaded() const noexcept

true after a successful Load.

function

ModulusBits

#
std::size_t ModulusBits() const noexcept

Modulus size in bits (0 when empty).

function

ModulusSize

#
std::size_t ModulusSize() const noexcept

Modulus size in bytes, which is the signature size (0 when empty).

function

VerifyPkcs1v15

#
bool VerifyPkcs1v15( ERsaHash eHash, std::span< const std::uint8_t > Digest, std::span< const std::uint8_t > Signature ) const noexcept;

RSASSA-PKCS1-v1_5 verification (RFC 8017 section 8.2.2).

Builds the whole expected encoding 00 01 FF..FF 00 DigestInfo and compares it with the recovered one. It does not parse the recovered DigestInfo, which is the classic hole (Bleichenbacher 2006 signature forgery with e = 3). The DigestInfo is the DER one with the NULL parameters, the form RFC 8017 prescribes.

eHash
The hash of the signature.
Digest
The digest, exactly the size of that hash.
Signature
The signature, exactly ModulusSize bytes.
true only for a valid signature.
function

VerifyPss

#
bool VerifyPss( ERsaHash eHash, std::span< const std::uint8_t > Digest, std::span< const std::uint8_t > Signature, std::size_t uSaltSize ) const noexcept;

RSASSA-PSS verification (RFC 8017 section 8.1.2), MGF1 with the same hash.

TLS 1.3 uses a salt as long as the digest (RFC 8446 section 4.2.3). Pass PSS_SALT_ANY to accept any salt length.

eHash
The hash of the signature, also used by MGF1.
Digest
The digest, exactly the size of that hash.
Signature
The signature, exactly ModulusSize bytes.
uSaltSize
The expected salt length, or PSS_SALT_ANY.
true only for a valid signature.

sha.hpp

#include <ethereal/cpp/cryptography/sha.hpp>

SHA-256, SHA-384 and SHA-512 digests (FIPS 180-4).

One pass digests over a buffer, the two region SHA-512 that Ed25519 needs, and incremental SHA-256, SHA-384 and SHA-512 for data that arrives in pieces.

struct

SHA256

Ethereal::Crypto#
C++
struct SHA256
{
    /// @brief  Digest size, in bytes.
    static constexpr std::size_t DigestSize = 32;


    ///
    /// Get
    ///
    /// @brief  Computes the SHA-256 of a binary buffer.
    ///
    /// @param Data        Input buffer.
    ///
    /// @return  32 byte digest, in network order (big-endian), as the standard defines.
    ///
    static std::array< std::uint8_t, DigestSize > Get( std::span< const std::uint8_t > Data );


    ///
    /// Get
    ///
    /// @brief  Computes the SHA-256 of a string.
    ///
    /// @details  Views the string as bytes without a copy. The null terminator
    ///            is not part of the digest.
    ///
    /// @param Str        Input string. A null pointer returns the digest of the empty input.
    ///
    /// @return  32 byte digest.
    ///
    static std::array< std::uint8_t, DigestSize > Get( const char * Str );


    ///
    /// GetAs_String
    ///
    /// @brief  Computes the SHA-256 and writes it as lowercase hexadecimal.
    ///
    /// @details  The text is written straight into the output string. No value
    ///            is returned.
    ///
    /// @param Str        Input string.
    /// @param StrOut    Receives the digest in hexadecimal (64 characters).
    ///
    static void GetAs_String( const char * Str, std::string & StrOut );
};

SHA-256 cryptographic digest, as in FIPS 180-4.

One pass, no state between calls and no allocation. The whole digest is computed over the given buffer and returned by value. For data that arrives in pieces, use CSha256Stream.

SHA-256 is for integrity and content fingerprints. It is not a password derivation function. For passwords use an algorithm with a tunable cost (Argon2, scrypt, bcrypt), never a plain SHA.
struct

SHA512

Ethereal::Crypto#
C++
struct SHA512
{
    /// @brief  Digest size, in bytes.
    static constexpr std::size_t DigestSize = 64;


    /// @brief  Compression block size, in bytes.
    ///
    /// @details  Exposed because constructions built on SHA-512 (HMAC, for
    ///            example) need the block size, not the digest size, to build
    ///            their masks.
    ///
    static constexpr std::size_t BlockSize = 128;


    ///
    /// Get
    ///
    /// @brief  Computes the SHA-512 of a binary buffer.
    ///
    /// @param Data        Input buffer.
    ///
    /// @return  64 byte digest, in network order (big-endian), as the standard defines.
    ///
    static std::array< std::uint8_t, DigestSize > Get( std::span< const std::uint8_t > Data );


    ///
    /// Get
    ///
    /// @brief  Computes the SHA-512 of a string.
    ///
    /// @details  The null terminator is not part of the digest.
    ///
    /// @param Str        Input string. A null pointer returns the digest of the empty input.
    ///
    /// @return  64 byte digest.
    ///
    static std::array< std::uint8_t, DigestSize > Get( const char * Str );


    ///
    /// Get
    ///
    /// @brief  Computes the SHA-512 of two regions in sequence, without joining them.
    ///
    /// @details  Ed25519 digests `R || A || M`, where M can be large. Without this
    ///            form the caller would have to allocate a copy just to put 64 bytes
    ///            in front of the message.
    ///
    /// @param First    First region. May be empty.
    /// @param Second    Second region. May be empty.
    ///
    /// @return  64 byte digest of First followed by Second.
    ///
    static std::array< std::uint8_t, DigestSize > Get( std::span< const std::uint8_t > First,
                                                       std::span< const std::uint8_t > Second );


    ///
    /// GetAs_String
    ///
    /// @brief  Computes the SHA-512 and writes it as lowercase hexadecimal.
    ///
    /// @param Str        Input string.
    /// @param StrOut    Receives the digest in hexadecimal (128 characters).
    ///
    static void GetAs_String( const char * Str, std::string & StrOut );
};

SHA-512 cryptographic digest, as in FIPS 180-4.

Same shape as SHA256: one pass, no state between calls and no allocation. The word is 64 bits instead of 32, with 80 rounds instead of 64, a 128 byte block instead of 64 and a 64 byte digest instead of 32.

Added because the Ed25519 signature (Ethereal::Crypto::Ed25519) requires SHA-512 by definition (RFC 8032). Like SHA-256, it is for integrity and fingerprints, and for the same reason not for passwords.
On a 64 bit machine SHA-512 is usually faster than SHA-256 on large inputs, since it processes twice the bytes per block.
struct

SHA384

Ethereal::Crypto#
C++
struct SHA384
{
    /// @brief  Digest size, in bytes.
    static constexpr std::size_t DigestSize = 48;


    /// @brief  Compression block size, in bytes. The same as SHA-512.
    static constexpr std::size_t BlockSize = 128;


    ///
    /// Get
    ///
    /// @brief  Computes the SHA-384 of a binary buffer.
    ///
    /// @param Data        Input buffer.
    ///
    /// @return  48 byte digest, in network order (big-endian).
    ///
    static std::array< std::uint8_t, DigestSize > Get( std::span< const std::uint8_t > Data );
};

SHA-384 cryptographic digest, as in FIPS 180-4 (2026-10-11).

SHA-512 with another initial state, truncated to 48 bytes. Same compression core as SHA512. Added for TLS 1.3, where the TLS_AES_256_GCM_SHA384 suite and the P-384 signatures use it.

class

CSha256Stream

Ethereal::Crypto#

Incremental SHA-256: the bytes arrive in pieces (2026-09-28).

class CSha256Stream

Same digest as SHA256::Get over the concatenated pieces, with the same compression core. Meant for data that does not fit in memory: a download of a multi gigabyte artifact is hashed while it arrives, with 104 bytes of state. No allocation.

After Final the object is back in its initial state and can hash another message without Reset.

constructor

CSha256Stream

#
CSha256Stream() noexcept

Constructor. Starts with the empty message.

function

Reset

#
void Reset() noexcept;

Goes back to the initial state (the empty message).

function

Update

#
void Update( std::span< const std::uint8_t > Data ) noexcept;

Appends bytes to the message.

Data
The next piece.
function

Final

#
std::array< std::uint8_t, SHA256::DigestSize > Final() noexcept;

Digest of the accumulated message. The object goes back to the initial state.

class

CSha512Stream

Ethereal::Crypto#

Incremental SHA-512: the bytes arrive in pieces (2026-10-11).

class CSha512Stream

Same digest as SHA512::Get over the concatenated pieces, with the same compression core. No allocation.

Copyable by value. A copy carries the whole state, so the digest of a prefix can be taken from a copy while the original keeps absorbing (the TLS transcript hash does this).

After Final the object is back in its initial state.

constructor

CSha512Stream

#
CSha512Stream() noexcept

Constructor. Starts with the empty message.

function

Reset

#
void Reset() noexcept;

Goes back to the initial state (the empty message).

function

Update

#
void Update( std::span< const std::uint8_t > Data ) noexcept;

Appends bytes to the message.

Data
The next piece.
function

Final

#
std::array< std::uint8_t, SHA512::DigestSize > Final() noexcept;

Digest of the accumulated message. The object goes back to the initial state.

class

CSha384Stream

Ethereal::Crypto#

Incremental SHA-384: the bytes arrive in pieces (2026-10-11).

class CSha384Stream

Same digest as SHA384::Get over the concatenated pieces. Copyable by value, like CSha512Stream. After Final the object is back in its initial state.

constructor

CSha384Stream

#
CSha384Stream() noexcept

Constructor. Starts with the empty message.

function

Reset

#
void Reset() noexcept;

Goes back to the initial state (the empty message).

function

Update

#
void Update( std::span< const std::uint8_t > Data ) noexcept;

Appends bytes to the message.

Data
The next piece.
function

Final

#
std::array< std::uint8_t, SHA384::DigestSize > Final() noexcept;

Digest of the accumulated message. The object goes back to the initial state.

signature.hpp

#include <ethereal/cpp/cryptography/signature.hpp>

Ed25519 digital signature (RFC 8032).

The other crypto modules here are symmetric: whoever verifies needs the same key as whoever produced. Between two ends that already trust each other (CSecureTransform, for example) that is enough.

For licenses it is not, and that is why this file exists. With a symmetric check, the secret that validates the license has to be inside the library the customer receives. Whoever has the library has the secret, and whoever has the secret can mint licenses. The whole scheme falls to a disassembler and a free afternoon, and it cannot be fixed without reissuing everything.

Ed25519 breaks that symmetry. The private key stays in the generator, which never leaves the issuer's machine. The product ships the public key, which verifies and cannot sign. Whoever disassembles the customer binary finds the public key, and it alone is useless for forging.

What this does not solve: license checks run on the customer machine, and no cryptography stops the customer from skipping the check by patching the binary. What the asymmetric signature prevents is someone becoming a license supplier. Without the private key there is no generator, and the attack changes from "run a keygen" to "patch each customer installation, one by one".

Implemented as in RFC 8032 (pure Ed25519, no context and no prehash).

struct

Ed25519

Ethereal::Crypto#
C++
struct Ed25519
{
    /// @brief  Seed (private key) size, in bytes.
    ///
    /// @details  The RFC calls these 32 bytes the "private key". The signing key
    ///            itself is derived from them by SHA-512 and is never stored.
    ///
    static constexpr std::size_t SEED_SIZE = 32;


    /// @brief  Public key size, in bytes.
    static constexpr std::size_t PUBLIC_KEY_SIZE = 32;


    /// @brief  Signature size, in bytes.
    static constexpr std::size_t SIGNATURE_SIZE = 64;


    /// @brief  The 32 byte seed.
    using Seed        = std::array< std::uint8_t, SEED_SIZE >;

    /// @brief  The 32 byte public key.
    using PublicKey    = std::array< std::uint8_t, PUBLIC_KEY_SIZE >;

    /// @brief  The 64 byte signature.
    using Signature    = std::array< std::uint8_t, SIGNATURE_SIZE >;


    ///
    /// GenerateSeed
    ///
    /// @brief  Draws a new seed from the system entropy source.
    ///
    /// @details  Use this once, to create the product key pair, and keep the seed
    ///            where it never travels with what is distributed.
    ///
    /// @param Out    Receives the 32 bytes of the seed.
    ///
    /// @return  true when the system entropy source answered. false means no seed
    ///           was drawn. Do not use the output.
    ///
    ETL_API_NOIGN    static bool GenerateSeed( Seed & Out ) ETL_NOEXCEPT;


    ///
    /// DerivePublicKey
    ///
    /// @brief  Derives the public key that matches a seed.
    ///
    /// @param Secret    32 byte seed.
    ///
    /// @return  32 byte public key.
    ///
    ETL_API_NOIGN    static PublicKey DerivePublicKey( const Seed & Secret ) ETL_NOEXCEPT;


    ///
    /// Sign
    ///
    /// @brief  Signs a message.
    ///
    /// @param Secret    32 byte seed.
    /// @param Message    The message. May be empty.
    ///
    /// @return  64 byte signature.
    ///
    ETL_API_NOIGN    static Signature Sign( const Seed & Secret,
                                           std::span< const std::uint8_t > Message ) ETL_NOEXCEPT;


    ///
    /// Verify
    ///
    /// @brief  Verifies a signature.
    ///
    /// @details  Does not throw and does not tell the reasons apart: a malformed
    ///            public key, a non canonical signature and a plainly wrong
    ///            signature all return the same false. A license check should
    ///            not know why it failed. The difference would only help someone
    ///            trying to forge.
    ///
    /// @param Public    32 byte public key.
    /// @param Message    The verified message.
    /// @param Sig        64 byte signature.
    ///
    /// @return  true only if the signature is from Public over Message.
    ///
    ETL_API_NOIGN    static bool Verify( const PublicKey & Public,
                                        std::span< const std::uint8_t > Message,
                                        const Signature & Sig ) ETL_NOEXCEPT;


    ///
    /// SelfCheckConstants
    ///
    /// @brief  Checks the internal curve constants against their definitions.
    ///
    /// @details  Makes the central claim verifiable, like @c RawStorage in the
    ///            obfuscation module. A curve constant typed wrong produces an
    ///            implementation that is internally consistent (it signs and
    ///            verifies itself without complaint) and is still not Ed25519.
    ///            Round trip tests do not catch that.
    ///
    ///           Checks that d = -121665/121666, that 2d is twice d, and that the
    ///            sqrt(-1) constant squared is really -1 in the field.
    ///
    /// @return  true when every constant matches its definition.
    ///
    ETL_API_NOIGN    static bool SelfCheckConstants() ETL_NOEXCEPT;
};

Ed25519 digital signature, as in RFC 8032.

Twisted Edwards curve over GF(2^255-19). 64 byte signature, 32 byte public key, 32 byte private key (seed). Deterministic: the same seed and the same message always give the same signature. This is a property of the standard. No random generator is involved in signing, so a weak generator cannot leak the key, which is the classic way ECDSA fails.

Verification here is strict. Besides the group equation, it rejects a signature whose scalar S is not reduced (S >= L) and a point encoding that is not canonical. That removes malleability: given a valid signature, there is no way to produce a second, different one that also passes. For licenses this matters, since a malleable signature would be a distinct serial number for the same license.
On constant time: Sign and DerivePublicKey run in time independent of the key value (the multiplication ladder uses a conditional swap with no branch). Still, the private key exists only on the issuer's machine. The customer product carries only Verify, which touches no secret.
This signs and verifies. It does not do key agreement and must not be reused for it. Ed25519 and X25519 use the same curve but different representations and key domains, and mixing them is a known mistake.

utils.hpp

#include <ethereal/cpp/cryptography/utils.hpp>
function

ToHexLower

Ethereal::Crypto#
void ToHexLower( std::span< const std::uint8_t > Data, std::string & StrHexOut );

Converts a binary buffer to a lowercase hexadecimal string. Converts a block of binary data straight to its hexadecimal form (base 16), with lowercase characters (0-9, a-f). The conversion runs byte by byte, with no intermediate allocation.

Data
Input binary buffer.
StrHexOut
Output string with the lowercase hexadecimal of the buffer.
function

AsBytes

Ethereal::Crypto#
std::span<const std::uint8_t> AsBytes( const std::string_view & Str );

Converts a std::string_view to a span of bytes (uint8_t). Reads the internal memory of the string as a binary buffer of constant bytes. Does not copy the data and does not allocate. The returned span is valid only while the original string exists.

Str
Input string, by reference.
Constant span over the bytes of the string.
function

AsBytes

Ethereal::Crypto#
template< typename T > std::span< const std::uint8_t > AsBytes( const T & v )

Converts any object to a span of bytes (uint8_t). Reinterprets the binary representation of an object in memory as a contiguous buffer of constant bytes. Does not copy and does not allocate.

*** Warning: The type T must be 'trivially copyable' to be used safely. The returned span is valid only while the original object exists.

T
Type of the object.
v
Input object.
Constant span over the bytes of the object.

x25519.hpp

#include <ethereal/cpp/cryptography/x25519.hpp>

X25519 key agreement (RFC 7748).

Diffie-Hellman over Curve25519 with the Montgomery ladder. It is the default key exchange of TLS 1.3. The field arithmetic is the one Ed25519 uses (one code, covered by the vectors and the oracle of both).

struct

X25519

Ethereal::Crypto#
C++
struct X25519
{
    /// @brief  Size of every key and of the shared secret, in bytes.
    static constexpr std::size_t KEY_SIZE = 32;


    /// @brief  The 32 byte private key (scalar, clamped on use).
    using PrivateKey    = std::array< std::uint8_t, KEY_SIZE >;

    /// @brief  The 32 byte public key (u coordinate, little-endian).
    using PublicKey        = std::array< std::uint8_t, KEY_SIZE >;

    /// @brief  The 32 byte shared secret.
    using SharedSecret    = std::array< std::uint8_t, KEY_SIZE >;


    ///
    /// GeneratePrivateKey
    ///
    /// @brief  Draws a private key from the system entropy source.
    ///
    /// @param Out    Receives the 32 bytes.
    ///
    /// @return  false when the entropy source gave all zeros. Do not use the output.
    ///
    ETL_API_NOIGN    static bool GeneratePrivateKey( PrivateKey & Out ) ETL_NOEXCEPT;


    ///
    /// DerivePublicKey
    ///
    /// @brief  Computes the public key, X25519(k, 9).
    ///
    /// @param Private    The private key.
    ///
    /// @return  The 32 byte public key.
    ///
    ETL_API_NOIGN    static PublicKey DerivePublicKey( const PrivateKey & Private ) ETL_NOEXCEPT;


    ///
    /// DeriveShared
    ///
    /// @brief  Computes the shared secret with the peer public key.
    ///
    /// @details  Rejects an all zero result, as TLS 1.3 requires (RFC 8446
    ///            section 7.4.2). It comes from a peer key of small order, and
    ///            accepting it would let the peer force a known secret. The
    ///            check reads every byte and does not branch until the end.
    ///
    /// @param Private    Our private key.
    /// @param Peer        The peer public key.
    /// @param Out        Receives the shared secret. Zeroed on failure.
    ///
    /// @return  false when the result is all zeros.
    ///
    ETL_API_NOIGN    static bool DeriveShared( const PrivateKey & Private, const PublicKey & Peer,
                                              SharedSecret & Out ) ETL_NOEXCEPT;


    ///
    /// ScalarMult
    ///
    /// @brief  The raw X25519 function of RFC 7748 section 5.
    ///
    /// @details  Clamps the scalar, masks the top bit of u, and returns the
    ///            result with no zero check. For test vectors and for callers
    ///            that apply their own policy. Key agreement should use
    ///            @ref DeriveShared.
    ///
    /// @param Scalar    32 byte scalar.
    /// @param U        32 byte u coordinate.
    ///
    /// @return  The 32 byte u coordinate of the result.
    ///
    ETL_API_NOIGN    static std::array< std::uint8_t, KEY_SIZE > ScalarMult(
                        const std::array< std::uint8_t, KEY_SIZE > & Scalar,
                        const std::array< std::uint8_t, KEY_SIZE > & U ) ETL_NOEXCEPT;
};

X25519 key agreement, as in RFC 7748.

32 byte private key, 32 byte public key (the u coordinate), 32 byte shared secret. The scalar is clamped as the RFC requires, and the top bit of a received u is ignored.

Constant time: the ladder takes 255 steps for any scalar and swaps the working points with a mask, never with a branch.
The shared secret is raw. Feed it to a key derivation (HKDF in TLS 1.3), never use it as a key directly.

xor.hpp

#include <ethereal/cpp/cryptography/xor.hpp>
function

GenerateXorKey

Ethereal::XorObjectC#
std::uint64_t GenerateXorKey( void ) noexcept

Draws the next key of the xorshift sequence.

The state is atomic and each step is taken with a compare and swap, so every step goes to one call only. The old plain static was a data race: two threads could read the same state and get the same key. With one thread the sequence is the same as before.

function

RotateKey8

Ethereal::XorObjectC#
std::uint64_t RotateKey8( std::uint64_t k ) noexcept
class

XorValue

Ethereal::XorObjectC#
template< typename T > class XorValue
constructor

XorValue

#
XorValue() noexcept : key_( GenerateXorKey() )

Default constructor. The value is zero (all bytes zero).

constructor

XorValue

#
XorValue( const T & v ) noexcept : key_( GenerateXorKey() )

Constructor that encrypts the given value.

constructor

XorValue

#
XorValue( const XorValue & other ) noexcept : key_( GenerateXorKey() )

Copy constructor. Encrypts the value again with a new key.

operator

operator=

#
XorValue & operator=( const XorValue & other ) noexcept
operator

operator=

#
XorValue & operator=( const T & v ) noexcept
operator

operatorT

#
operator T() const noexcept
function

get

#
[[nodiscard]] T get() const noexcept
function

set

#
void set( const T & v ) noexcept
function

update

#
template< typename F > void update( F && func ) noexcept
function

clear

#
void clear() noexcept
class

XorPtr

Ethereal::XorObjectC#
template<typename T> class XorPtr
constructor

XorPtr

#
XorPtr() noexcept : key_( GenerateXorKey() ) , enc_(0)
constructor

XorPtr

#
explicit XorPtr(T* ptr) noexcept : key_(GenerateXorKey())
constructor

XorPtr

#
XorPtr(const XorPtr& other) noexcept : key_(GenerateXorKey())
operator

operator=

#
XorPtr& operator=(const XorPtr& other) noexcept
operator

operator=

#
XorPtr& operator=(T* ptr) noexcept
function

set

#
void set(T* ptr) noexcept
function

get

#
[[nodiscard]] T* get() const noexcept
operator

operator*

#
T& operator*() const noexcept
operator

operator->

#
T* operator->() const noexcept
operator

operatorbool

#
explicit operator bool() const noexcept
const

ETL_CT_Time

Ethereal::XorStringC#
constexpr auto ETL_CT_Time = __TIME__;
const

ETL_CT_Seed

Ethereal::XorStringC#
constexpr ETL_INT32 ETL_CT_Seed = ( static_cast<ETL_INT32>(ETL_CT_Time[ 7 ]) ) + ( static_cast<ETL_INT32>(ETL_CT_Time[ 6 ]) * 10 ) + ( static_cast<ETL_INT32>(ETL_CT_Time[ 4 ]) * 60 ) + ( static_cast<ETL_INT32>( ETL_CT_Time[ 3 ]) * 600 ) + ( static_cast<ETL_INT32>( ETL_CT_Time[ 1 ]) * 3600 ) + ( static_cast<ETL_INT32>(ETL_CT_Time[ 0 ]) * 36000 );
struct

ETL_CT_Random

Ethereal::XorStringC#
C++
template< ETL_INT32 N >
struct ETL_CT_Random
{
    static constexpr uint32_t a = 16807;
    static constexpr uint32_t m = 2147483647;

    static constexpr uint32_t s  = ETL_CT_Random<N - 1>::value;
    static constexpr uint32_t lo = a * (s & 0xFFFF);
    static constexpr uint32_t hi = a * (s >> 16);
    static constexpr uint32_t lo2 = lo + ((hi & 0x7FFF) << 16);
    static constexpr uint32_t lo3 = lo2 + (hi >> 15);

    static constexpr uint32_t value = (lo3 > m) ? lo3 - m : lo3;
};
struct

ETL_CT_Random

Ethereal::XorStringC#
C++
template<>
struct ETL_CT_Random<0> { static constexpr std::uint32_t value = ETL_CT_Seed; };
struct

ETL_CT_RandomInt

Ethereal::XorStringC#
C++
template< ETL_INT32 N, ETL_INT32 Max >
struct ETL_CT_RandomInt
{
    static constexpr ETL_INT32 value = ETL_CT_Random<N + 1>::value % Max;
};
struct

ETL_CT_RandomChar

Ethereal::XorStringC#
C++
template< ETL_INT32 N >
struct ETL_CT_RandomChar
{
    static constexpr char value = static_cast<char>(1 + ETL_CT_RandomInt<N, 0x7F - 1>::value);
};
class

ETL_XorString

Ethereal::XorStringC#
template< std::size_t N, ETL_INT32 K, typename Char > class ETL_XorString
constructor

ETL_XorString

#
template<std::size_t... I> constexpr ETL_XorString([[maybe_unused]] const Char * str, std::index_sequence<I...>) : key_(ETL_CT_RandomChar<K>::value), encrypted_

'str' is never read for an empty text (no indices), and GCC warns about it with ETHEREAL_USING_XOR on. Hence the attribute.

function

DecryptInto

#
void DecryptInto( Char * pOut ) const

Decrypts into another buffer. The stored bytes are not touched.

Replaces the old 'decrypt()', which had two serious defects. It decrypted in place and never encrypted again, so after the first use the text stayed in clear in memory for good. And it returned a pointer into the object, which the macro created as a temporary, so the pointer was dangling from the start.

Being const, this version can also be called on a 'constexpr' object. That keeps the encrypted bytes in the data section and the clear text out of the binary.

pOut
Destination with room for N + 1 characters.
function

Length

#
static constexpr std::size_t Length()

Length of the text, without the terminator.

class

ETL_XorScoped

Ethereal::XorStringC#

Decrypts on construction and wipes on destruction.

template< std::size_t N, typename Char > class ETL_XorScoped

This fixes the dangling pointer. The clear text lives here, and this object lives until the end of the expression that created it. That is enough for a function call, and not enough to keep the pointer.

Do not keep the pointer. When it must survive, as in a function that returns 'const char*', use ETL_XORSTR_STATIC, which decrypts once into a permanent place.
constructor

ETL_XorScoped

#
template< typename TSource > explicit ETL_XorScoped( const TSource & Source )

Decrypts the source into the internal buffer.

Source
The encrypted string.
function

const

#
operator const Char *() const

The clear text, null terminated.

function

Get

#
const Char * Get() const

The clear text, null terminated.

function

MakeScoped

Ethereal::XorStringC#
template< std::size_t N, ETL_INT32 K, typename Char > ETL_XorScoped< N, Char > MakeScoped( const ETL_XorString< N, K, Char > & Source )

Deduces the parameters and returns the scoped decryptor.

function

ETL_XOR_Printf

Ethereal::XorStringC#
void ETL_XOR_Printf( const char * fmt, ... )

xor_runtime.hpp

#include <ethereal/cpp/cryptography/xor_runtime.hpp>

Run time obfuscation of bytes, text, values and pointers.

This is obfuscation, not cryptography. The key lives in the same process as the data. Whoever can stop the program in a debugger reads both, and no byte arrangement trick changes that.

What it buys:

  • 'strings' on the binary no longer shows the text, so the cheapest reverse engineering path (opening the executable in an editor and reading the messages) stops working.
  • A memory scan for a known pattern does not find the text, because it is not in clear while stored.
  • Two copies of the same text do not look alike in memory (each object has its own salt), so searching for repeated blocks does not help either.
  • The cost for someone just snooping goes from minutes to hours.

What it does not buy:

  • Anything against someone with a debugger and patience.
  • Real confidentiality. For that there is Ethereal::Crypto, with a key that does not live in the process. If the secret matters, it does not belong here.

Compile time and run time are different cases. ETL_XORSTR in xor.hpp encrypts at compile time and only works for literals. It protects the binary on disk. This file covers text that only exists at run time: a license key read from a file, a token received from the network, a path built on the fly. No literal protects that, because there was no literal.

The five rules:

1. The key is not stored next to the data. This was the classic mistake of the previous version of this code: a 'key_' member next to a 'storage_' member. In a memory dump both come out together, and undoing the XOR is one line of script. Here the key does not exist anywhere while the object is idle. It is derived on each use from three things: the process key (drawn once), the object salt and the object address. A memory dump has the salt and the cipher text, and neither is enough.

2. Because of that, the object cannot be relocated with 'memcpy'. The key depends on the address. Copying the bytes of the object elsewhere without the constructor produces garbage on reveal. Copy and move are implemented and redo the math. Treating this as a trivially copyable type does not work.

3. Revealing is scoped, and the object stays encrypted. CXorString::Reveal returns an object that holds the clear text in its own buffer and wipes it when it goes out of scope. The stored data is never in clear, unlike the previous version, whose 'decrypt()' decrypted in place and left it so for good.

4. The length is not hidden. It stays visible, and sometimes it already gives away what was meant to be hidden (a 32 byte field next to a 6 byte one). CXorString::Assign takes a minimum padding for that case.

5. Compare without revealing. CXorString::Equals compares without returning the text to the caller and without leaving the value in clear longer than needed. The comparison is constant time over the common length.

function

ProcessKey

Ethereal::Xor#
ETL_UINT64 ProcessKey() noexcept;

The key of this process, drawn once.

Comes from Ethereal::Random::SecureBytes on the first call. Two runs of the same program obfuscate the same text differently, which prevents comparing memory dumps across runs.

function

NewSalt

Ethereal::Xor#
ETL_UINT64 NewSalt() noexcept;

Draws a new salt for an object.

The salt must be independent of the process key and of the address.

The first version of this module used ProcessKey() @c ^ address as the salt, and the math cancelled out completely:

uSalt ^ ProcessKey() = address DeriveKey = Mix( address ) ^ Mix( address ) = 0

With the key at zero the XOR is the identity and the value stays in clear in memory, which is what the module exists to avoid. Every round trip test passed, because a round trip with a zero key works perfectly. The check that searches for the value bytes inside the object is what caught it.

function

DeriveKey

Ethereal::Xor#
ETL_UINT64 DeriveKey( const ETL_UINT64 uSalt, const void * pAddress ) noexcept;

Derives the key of an object. See rule 1.

Public because whoever writes their own container needs it, and because hiding the formula would protect nothing: it is written here and in the source. What protects is that the process key is not predictable.

uSalt
The object salt.
pAddress
The object address.
The key of the object.
class

CXorBuffer

Ethereal::Xor#

A block of bytes kept obfuscated in memory.

class CXorBuffer final
class

CReveal

#
class CReveal final

The content in clear, while this object lives.

The buffer belongs to this object. The source stays obfuscated the whole time. When it goes out of scope, the clear text is wiped with Ethereal::Crypto::SecureZero.

Do not keep the pointer. It dies with the reveal. That is the difference from the previous design, which decrypted in place and left the text in clear for good.
constructor

CXorBuffer

#
CXorBuffer() noexcept;

Empty buffer, with a new salt.

constructor

CXorBuffer

#
explicit CXorBuffer( std::span< const std::uint8_t > Data );

Stores Data, obfuscated.

Data
The bytes to store.
constructor

CXorBuffer

#
CXorBuffer( const CXorBuffer & Other );

Copy. Encrypted again with its own salt and address.

operator

operator=

#
CXorBuffer & operator=( const CXorBuffer & Other );

Copy assignment. Encrypted again with its own salt and address.

constructor

CXorBuffer

#
CXorBuffer( CXorBuffer && Other ) noexcept;

Move. Redoes the math, since the key depends on the address.

operator

operator=

#
CXorBuffer & operator=( CXorBuffer && Other ) noexcept;

Move assignment. Redoes the math, since the key depends on the address.

function

Assign

#
void Assign( std::span< const std::uint8_t > Data );

Stores Data, obfuscated. The previous content is wiped.

Data
The bytes to store.
function

Clear

#
void Clear() noexcept;

Wipes the content and draws a new salt.

function

Size

#
std::size_t Size() const noexcept;

Number of stored bytes.

function

IsEmpty

#
bool IsEmpty() const noexcept;

true when nothing is stored.

function

Reveal

#
CReveal Reveal() const;

Scoped reveal. See rule 3.

function

Equals

#
bool Equals( std::span< const std::uint8_t > Other ) const;

Compares without returning the content to the caller. See rule 5.

Other
The bytes to compare with.
true when the content equals Other.
function

RawStorage

#
std::span< const std::uint8_t > RawStorage() const noexcept;

The bytes as they are stored.

Makes the central claim of this module verifiable: a test can check that the clear text is not here. An obfuscation library that does not allow measuring whether it obfuscates asks for trust instead of proof.

It weakens nothing. Whoever has the process already has these bytes. What does not come out of here is the key, which is not even stored.

class

CXorString

Ethereal::Xor#

A text kept obfuscated in memory.

class CXorString final
C++
Ethereal::Xor::CXorString Token( sFromNetwork );

/// ... the token stays obfuscated while stored ...

{
    const auto Plain = Token.Reveal();

    Use( Plain.Text() );        /// narrow window, on purpose
}
/// here the clear text is already wiped
class

CReveal

#
class CReveal final

The text in clear, while this object lives. See CXorBuffer::CReveal.

constructor

CXorString

#
CXorString() noexcept;

Empty text, with a new salt.

constructor

CXorString

#
explicit CXorString( std::string_view Text );

Stores Text, obfuscated.

Text
The text to store.
constructor

CXorString

#
CXorString( const CXorString & Other );

Copy. Encrypted again with its own salt and address.

operator

operator=

#
CXorString & operator=( const CXorString & Other );

Copy assignment. Encrypted again with its own salt and address.

constructor

CXorString

#
CXorString( CXorString && Other ) noexcept;

Move. Redoes the math, since the key depends on the address.

operator

operator=

#
CXorString & operator=( CXorString && Other ) noexcept;

Move assignment. Redoes the math, since the key depends on the address.

function

Assign

#
void Assign( std::string_view Text, const std::size_t uPadTo = 0 );

Stores Text, obfuscated.

Text
The text to store.
uPadTo
Minimum padding, in bytes. The stored content is at least this size, filled with random bytes, and Size is still the real size. This serves rule 4: a fixed size field does not give away what is inside.
function

Clear

#
void Clear() noexcept;

Wipes the content and draws a new salt.

function

Size

#
std::size_t Size() const noexcept;

Real length of the text, without the padding.

function

IsEmpty

#
bool IsEmpty() const noexcept;

true when the text is empty.

function

Reveal

#
CReveal Reveal() const;

Scoped reveal. See rule 3.

function

Equals

#
bool Equals( std::string_view Other ) const;

Compares without returning the text. See rule 5.

Other
The text to compare with.
true when the text equals Other.
function

RawStorage

#
std::span< const std::uint8_t > RawStorage() const noexcept;

The bytes as they are stored.

Makes the central claim of this module verifiable: a test can check that the clear text is not here. An obfuscation library that does not allow measuring whether it obfuscates asks for trust instead of proof.

It weakens nothing. Whoever has the process already has these bytes. What does not come out of here is the key, which is not even stored.

function

CopyPlain

#
std::string CopyPlain() const;

A copy in clear, which becomes the caller's problem.

From here on there is a std::string in clear in memory, and nobody wipes it. It exists because it is sometimes really needed, and it is marked so that it is a choice and not an oversight. Prefer Reveal.
class

CXorValue

Ethereal::Xor#
template< typename T > class CXorValue final
constructor

CXorValue

#
CXorValue() noexcept : m_uSalt( NewSalt() )

Stores a value initialized object.

constructor

CXorValue

#
CXorValue( const T & Value ) noexcept : m_uSalt( NewSalt() )

Stores Value, obfuscated.

constructor

CXorValue

#
CXorValue( const CXorValue & Other ) noexcept : m_uSalt( NewSalt() )

Copy. Encrypted again with its own salt and address.

operator

operator=

#
CXorValue & operator=( const CXorValue & Other ) noexcept

Copy assignment. Encrypted again with this object's key.

operator

operator=

#
CXorValue & operator=( const T & Value ) noexcept

Stores Value, obfuscated.

function

Get

#
T Get() const noexcept

The value in clear, as a copy.

function

Set

#
void Set( const T & Value ) noexcept

Stores Value, obfuscated.

operator

operatorT

#
operator T() const noexcept

Implicit conversion to the value in clear.

function

Update

#
template< typename TFn > void Update( TFn && Fn ) noexcept

Reads, lets Fn change the value, and stores it back.

The value is in clear only on the stack, inside the call.

Fn
Callable that takes a T &.
class

CXorPtr

Ethereal::Xor#

A pointer kept obfuscated in memory.

template< typename T > class CXorPtr final

Works against the scanner that searches for the address of a known object to reach another one. Still obfuscation.

constructor

CXorPtr

#
CXorPtr() noexcept : m_uSalt( NewSalt() ) , m_uCipher( 0 )

Null pointer, obfuscated too.

constructor

CXorPtr

#
explicit CXorPtr( T * pPointer ) noexcept : m_uSalt( NewSalt() ) , m_uCipher( 0 )

Stores pPointer, obfuscated.

constructor

CXorPtr

#
CXorPtr( const CXorPtr & Other ) noexcept : m_uSalt( NewSalt() ) , m_uCipher( 0 )

Copy. Encrypted again with its own salt and address.

operator

operator=

#
CXorPtr & operator=( const CXorPtr & Other ) noexcept

Copy assignment. Encrypted again with this object's key.

operator

operator=

#
CXorPtr & operator=( T * pPointer ) noexcept

Stores pPointer, obfuscated.

function

Get

#
T * Get() const noexcept

The pointer in clear.

function

Set

#
void Set( T * pPointer ) noexcept

Stores pPointer, obfuscated.

operator

operator*

#
T & operator*() const noexcept

Dereference.

operator

operator->

#
T * operator->() const noexcept

Member access.

operator

operatorbool

#
explicit operator bool() const noexcept

true when the pointer is not null.