Crypto++ Documentation

repository·master·Indexed 26 days ago

https://github.com/weidai11/cryptopp

A comprehensive C++ class library providing a wide range of cryptographic schemes, including authenticated encryption, stream and block ciphers, MACs, hash functions, public-key cryptography, key agreement, ECC, and key derivation (KDF). The documentation covers memory ownership rules, compilation instructions for MSVC and Linux/Unix-like systems, thread safety, and a detailed specification for the test data format used to verify cryptographic algorithms.

Tokens
1.9K
Snippets
0
Records
9
Agent score
40%

What's inside Crypto++

  1. Overview of Crypto++ Cryptographic Algorithms

    master

    Crypto++ is a comprehensive C++ class library providing a wide range of cryptographic schemes. Supported algorithms include:

    • Authenticated Encryption: GCM, CCM, EAX, ChaCha20Poly1305, XChaCha20Poly1305.
    • Stream Ciphers: ChaCha (8/12/20, IETF), Panama, Salsa20, Sosemanuk, XSalsa20, XChaCha20.
    • Block Ciphers: AES (Rijndael), RC6, MARS, Twofish, Serpent, CAST-256, ARIA, Blowfish, Camellia, CHAM, HIGHT, IDEA, Kalyna, LEA, SEED, RC5, SHACAL-2, SIMON, Skipjack, SPECK, Simeck, SM4, Threefish, Triple-DES, TEA, XTEA.
    • Block Cipher Modes: ECB, CBC, CBC-CTS, CFB, OFB, CTR, XTS.
    • MACs: BLAKE2s, BLAKE2b, CMAC, CBC-MAC, DMAC, GMAC, HMAC, Poly1305, SipHash, Two-Track-MAC, VMAC.
    • Hash Functions: SHA-1, SHA-2 (224/256/384/512), SHA-3 (224/256/384/512), SHAKE (128/256), SipHash, SM3, Tiger, RIPEMD, WHIRLPOOL, BLAKE2s, BLAKE2b, Keccack.
    • Public-Key Cryptography: RSA, DSA, ElGamal, Nyberg-Rueppel (NR), Rabin-Williams (RW), LUC, ECGDSA, DLIES, ESIGN.
    • Key Agreement: Diffie-Hellman (DH), Unified Diffie-Hellman (DH2), MQV, HMQV, FHMQV, LUCDIF, XTR-DH.
    • Elliptic Curve (ECC): ECDSA, ed25519, ECNR, ECIES, ECDH, ECMQV, x25519.
    • Key Derivation (KDF): PBKDF1, PBKDF2, PBKDF (PKCS #12), HKDF, Scrypt.
  2. Compile Crypto++ on Linux and Unix-like systems

    master

    Use the included makefile with GNU Make and GNU ld.

    Default Build Flags: The makefile uses -DNDEBUG -g2 -O2 for CXXFLAGS by default.

    Production Builds: If using an alternate build system (like Autotools or CMake), ensure you include -DNDEBUG for production/release builds to prevent asserts from leaking sensitive information via error reporting.

    Linker Order for Custom Build Systems: If collecting source files manually, ensure these files are at the head of the list to avoid static initialization issues:

    1. cryptlib.cpp
    2. cpu.cpp
    3. integer.cpp
    4. <other sources>

    They should be linked in this order: cryptlib.o, cpu.o, integer.o, <other objects>.

  3. Thread Safety in Crypto++

    master
    Crypto++ is thread safe at the class level. You can safely use the library in multithreaded applications, but you must provide your own synchronization (e.g., mutexes) if multiple threads access a common Crypto++ object simultaneously.
  4. Compile Crypto++ with MSVC

    master

    To build Crypto++ using Microsoft Visual Studio, open cryptest.sln (compatible with MSVC 2003 - 2015) and build the following projects as needed:

    • cryptest Non-DLL-Import Configuration: Builds the full static library and a full test driver.
    • cryptest DLL-Import Configuration: Builds a static library (containing only algorithms not in the DLL) and a test driver using both the DLL and static library.
    • cryptdll: Builds the DLL.
    • dlltest: Builds a sample application that uses only the DLL.

    Note on DLL usage: To use the Crypto++ DLL, #include "dll.h" before any other Crypto++ headers. Place the DLL in the same directory as your .exe. The dll.h header includes #pragma comment(lib, "cryptopp") to handle the import library automatically.

  5. Understand the Test Data Format structure

    master

    Test data files are ASCII text files composed of independent sections separated by blank lines. Each section contains one or more tests and must include the following mandatory fields:

    • AlgorithmType
    • Name
    • Source
    • Test

    Field Syntax Rules

    • Field Structure: Each field consists of a fieldName: fieldBody.
    • Multi-line Fields: If a field spans multiple lines, every line except the last must end with a backslash (\).
    • Comments: Any text following a hash mark (#) on a line is treated as a comment and ignored.
    • Multiple Tests: A section can contain multiple Test fields. The order is significant; a test uses the most recent occurrence of any given field name appearing before the Test field.
  6. Manage memory ownership in Crypto++

    master

    When using Crypto++ constructors, follow these ownership rules to avoid memory leaks or double-frees:

    1. Pointer Constructors: If a constructor for class A takes a pointer to an object B (excluding primitive types like int or char), class A takes ownership of B and will delete it during its own destruction.
    2. Reference Constructors: If a constructor for class A takes a reference to an object B, the caller retains ownership of B and must ensure B is not destroyed until A no longer requires it.
  7. Reference available Field Types

    master

    The following fields are used to define test parameters. Note that the presence and semantics of fields depend on the AlgorithmType.

    FieldTypeDescription
    AlgorithmTypestringe.g., Signature, AsymmetricCipher, SymmetricCipher, MAC, MessageDigest, or KeyFactory
    NamestringAlgorithm name from SCAN
    TeststringIdentifies the test to run
    SourcestringExplanation of test data origin
    CommentstringAdditional comments
    KeyFormatstringSpecifies key format. If Component, components are listed as name/value pairs. Otherwise uses Key, PublicKey, or PrivateKey
    Keyencoded stringThe key
    PublicKeyencoded stringThe public key
    PrivateKeyencoded stringThe private key
    Modulusencoded stringModulus (used when KeyFormat=Component)
    SubgroupOrderencoded stringSubgroup order (used when KeyFormat=Component)
    SubgroupGeneratorencoded stringSubgroup generator (used when KeyFormat=Component)
    PublicElementencoded stringPublic element (used when KeyFormat=Component)
    PrivateExponentencoded stringPrivate exponent (used when KeyFormat=Component)
    Messageencoded stringMessage to be signed or verified
    Signatureencoded stringSignature to be verified or compared
    BlockSizeencoded stringBlock size for variable block ciphers
    Plaintextencoded stringPlaintext data
    Ciphertextencoded stringCiphertext data
    Headerencoded stringHeader data
    Footerencoded stringFooter data
    Secretencoded stringUsed by some key derivation functions
    DerivedKeyencoded stringDerived key
    DerivedLengthencoded stringDerived length
    Digestencoded stringDigest
    TruncatedSizeintSize of truncated digest in bytes
    SeekintSeek location for random access ciphers
    Seek64unsigned longSeek location for random access ciphers
  8. Reference possible Test types

    master

    The Test field identifies which operation to perform. Common test types include:

    • KeyPairValidAndConsistent: Validates that public and private keys are both valid and consistent with each other.
    • PublicKeyInvalid: Validates that public key validation fails.
    • PrivateKeyInvalid: Validates that private key validation fails.
    • Verify: Validates that signature/digest/MAC verification passes.
    • VerifyTruncated: Validates that truncated digest/MAC verification passes.
    • NotVerify: Validates that signature/digest/MAC verification fails.
    • DeterministicSign: Signs a message using a given seed; the resulting signature must match the provided signature.
    • Encrypt: Validates that plaintext encrypts to the provided ciphertext.
    • DecryptMatch: Validates that ciphertext decrypts back to the provided plaintext.
  9. Use supported Data Types in test fields

    master

    When defining field bodies, you can use the following data types:

    • signed int: Small integers (less than 2^32) in decimal representation.
    • unsigned long: Large integers (less than 2^64) compatible with strtoul or strtoull.
    • string: Human-readable text.
    • encoded string:
      • quoted string: "message" evaluates to message (without quotes or null terminator).
      • hex encoded string: 0x74657374 or 74657374 (e.g., evaluates to test).
      • repeated string: r<count> <value> (e.g., r100 "message" repeats the string 100 times, or r256 0x0011 repeats the hex value 256 times).