Practical Cryptography for Developers Book

repository·master·Indexed 26 days ago

https://github.com/nakov/practical-cryptography-for-developers-book

A code-heavy guide for developers on implementing modern cryptographic primitives and protocols. Covers asymmetric key ciphers (RSA, ECC), hybrid encryption schemes (KEM/DEM), digital signatures (DSA, ECDSA, EdDSA), and key exchange algorithms (DHKE, ECDH). Includes practical implementation examples for SHA-256 and SHA3-256 in Python and JavaScript, as well as recommended libraries for .NET (Bouncy Castle, Nethereum) and Java (JCA, Bouncy Castle, Web3j).

Tokens
49.3K
Snippets
96
Records
239
Agent score
86%

What's inside Practical Cryptography for Developers

  1. Overview of Cryptographic Topics Covered

    master

    This project provides practical demonstrations and code examples for the following cryptographic concepts:

    • Hash Functions: SHA-2, SHA-3, BLAKE2, RIPEMD160, and collision concepts.
    • MAC (Message Authentication Codes): HMAC, CMAK, UMAC, and MAC-based random generators.
    • Key Derivation Functions (KDF): PBKDF2, Scrypt, Bcrypt, Linux crypt(), and Argon2.
    • Password Encryption: Hashing and modern secure KDFs like Argon2.
    • Randomness: Entropy and CSPRNG (Cryptographically Secure Pseudo-Random Number Generators).
    • Key Exchange: Diffie–Hellman (DHKE) and Elliptic Curve Diffie–Hellman (ECDH).
    • Symmetric Encryption: Block and stream ciphers, modes (CBC, CTR, GCM), AES (Rijndael), and Salsa20 / ChaCha20.
    • Authenticated Encryption: AES-256-GCM, ChaCha20-Poly1307, and AES-256-CTR-HMAC.
    • Asymmetric Encryption: RSA (key pairs, encryption, decryption) and ECC (Elliptic Curve Cryptography).
    • Elliptic Curves: secp256k1, P-256, P-521, Curve25519, and Curve448-Goldilocks.
    • Integrated Encryption: ECIES hybrid encryption scheme.
    • Digital Signatures: RSA signatures, ECDSA, and EdDSA.
    • Quantum-Safe Cryptography: SPHINCS+, NewHope, and quantum-safe signatures/key exchange.
    • Other Concepts: Digital certificates, TLS (Transport Layer Security), OTP (One-Time Passwords), and OAuth.
  2. Overview of Digital Signatures

    master

    Digital signatures are cryptographic tools used to sign messages and verify signatures to provide proof of authenticity. They provide three core security properties:

    • Message authentication: Proof that a specific known sender (the secret key owner) created and signed the message.
    • Message integrity: Proof that the message has not been altered after being signed.
    • Non-repudiation: The signer cannot deny having signed the document once the signature is created.

    Note: Digital signatures bind messages to public keys, not directly to digital identities. To bind a public key to a specific identity (person, organization, or website), they must be used in combination with a digital certificate.

  3. Overview of Symmetric Key Ciphers

    master

    Symmetric key ciphers use the same secret key (or a passphrase used to derive a key) for both encryption and decryption. They are categorized into two main types:

    • Block Ciphers: Encrypt data in fixed-size blocks (e.g., AES, Twofish, CAST, RC6).
    • Stream Ciphers: Encrypt data as a continuous sequence of bytes (e.g., ChaCha20).

    Block ciphers can be converted into stream ciphers using block cipher modes of operation (such as CBC or CTR). Symmetric ciphers are considered quantum-resistant provided sufficiently large key lengths are utilized.

  4. Overview of Practical Cryptography for Developers

    master

    This repository contains the source material for the book Practical Cryptography for Developers by Svetlin Nakov. It is a practical guide designed for developers, providing code examples (primarily in Python) to implement core cryptographic concepts.

    Key topics covered include:

    • Hashes: SHA-3, BLAKE2, etc.
    • MAC codes: HMAC, GMAC.
    • Key Derivation Functions (KDF): Scrypt, Argon2.
    • Key Agreement: DHKE, ECDH.
    • Symmetric Ciphers: AES, ChaCha20, AES-GCM, ChaCha20-Poly1305 (AEAD).
    • Asymmetric Ciphers & Public-Key Cryptosystems: RSA, ECC, ECIES.
    • Elliptic Curve Cryptography (ECC): secp256k1, curve25519.
    • Digital Signatures: ECDSA, EdDSA.
    • Secure Random Numbers: PRNG, CSRNG.
    • Quantum-safe cryptography.

    Note: The book is currently a work in progress and is not yet finished.

  5. Overview of Modern Cryptography Concepts

    master

    Modern cryptography involves several core concepts that developers must understand to implement secure systems. Key areas include:

    • Hash Functions: Transform messages into a fixed-length message digest (e.g., SHA-256, SHA-3, RIPEMD, BLAKE2).
    • Message Authentication: Using HMAC (Hashed Message Authentication Code) to prove message authenticity and integrity.
    • Key Derivation: Using functions like Scrypt, Argon2, or PBKDF2 to derive keys from passwords using salts and high computational costs.
    • Symmetric Encryption: Using the same key for encryption and decryption (e.g., AES with CBC/CTR modes, ChaCha20, Twofish).
    • Asymmetric Encryption: Using a public/private key pair (e.g., RSA, ECC/Elliptic Curve Cryptography like secp256k1 or Ed25519).
    • Digital Signatures: Ensuring authenticity and non-repudiation using asymmetric keys (e.g., DSA, ECDSA, EdDSA).
    • Key Exchange: Securely establishing keys between parties (e.g., Diffie-Hellman, ECDH).
    • Randomness: Using CSPRNG (Cryptographically Secure Pseudo-Random Number Generators) to ensure high entropy and unpredictable random numbers.
  6. Overview of Asymmetric Key Ciphers

    master

    Asymmetric key cryptosystems (also known as public-key cryptosystems) use a mathematically linked pair of keys: a public key (used for encryption or signature verification) and a private key (used for decryption or signing).

    Key capabilities provided by these systems include:

    • Key-pair generation: Creating a random private key and its corresponding public key.
    • Encryption/Decryption: Encrypting data with a public key so that only the holder of the private key can decrypt it.
    • Digital Signatures: Signing messages with a private key to provide authentication, integrity, and non-repudiation, which can then be verified by anyone with the public key.
    • Key Exchange: Securely exchanging cryptographic keys between parties over an insecure channel (e.g., DHKE, ECDH).
  7. Understand the Ethereum UTC / JSON Keystore File Format

    master
    The Ethereum UTC / JSON Wallet (Keystore File) is a standard encrypted format used to store private keys or wallet seed words. It is used by major Ethereum tools and libraries including geth, Parity, MyEtherWallet, MetaMask, ethers.js, and Nethereum. The file is a JSON document that specifies the encrypted data, the encryption algorithms used, and their parameters.
  8. Understand the Diffie-Hellman Key Exchange (DHKE) Protocol

    master

    The Diffie-Hellman Key Exchange (DHKE) is an anonymous key agreement protocol that allows two parties to establish a shared secret key over an insecure (public) channel. Once established, this shared secret is typically used as a key for symmetric ciphers like AES.

    Security Properties

    • Resistant to: Sniffing attacks (data interception). An eavesdropper seeing the public exchange cannot efficiently calculate the shared secret.
    • Vulnerable to: Man-in-the-middle (MITM) attacks. An attacker can intercept and alter the communication between the two parties. Authentication is required to prevent this.

    Implementation Variants

    • Classical DHKE: Uses modular exponentiations and the Discrete Logarithm Problem (DLP).
    • ECDH (Elliptic-Curve Diffie-Hellman): Uses elliptic-curve calculations instead of modular exponentiation for improved security and efficiency.
  9. Understand ASIC-Resistant Proof-of-Work Hash Functions

    master

    Proof-of-Work (PoW) mining algorithms utilize a specific class of hash functions designed to be both computationally-intensive and memory-intensive. These are referred to as ASIC-resistant hash functions.

    Key Characteristics

    • Resource Consumption: They are designed to consume significant computational power and large amounts of RAM.
    • Hardware Resistance: They are intentionally difficult to implement efficiently on specialized hardware like FPGAs or ASICs, favoring general-purpose hardware like GPUs (e.g., NVIDIA GTX 1080) or powerful CPUs (e.g., Intel Core i7-8700K) paired with fast RAM (e.g., DDR4).
    • Goal: The primary objective is to minimize mining centralization by making it economically viable for small players (home users) to participate, thereby limiting the dominance of large-scale mining corporations using ASIC miners.
  10. Understand EdDSA and Ed25519/Ed448

    master

    EdDSA (Edwards-curve Digital Signature Algorithm) is a modern, secure digital signature algorithm based on performance-optimized elliptic curves. It is based on the Schnorr signature algorithm and relies on the difficulty of the ECDLP problem.

    Key variants include:

    • Ed25519: Uses the edwards25519 curve (255-bit).
    • Ed448: Uses the edwards448 curve (448-bit).

    EdDSA is generally recommended for modern applications as it is simpler to implement and often faster than ECDSA.

  11. Understand Symmetric Encryption Concepts

    master

    Symmetric encryption uses a single secret key (also known as an encryption key or shared key) to both encrypt and decrypt data.

    In a complete symmetric encryption scheme, the process typically involves several components:

    1. Password-to-key-derivation algorithm: Extracts a binary secret key from a user-friendly password.
    2. Symmetric cipher algorithm: The core encryption algorithm (e.g., AES).
    3. Cipher block mode algorithm: Determines how the cipher handles data blocks (e.g., CBC, CTR).
    4. Message authentication (MAC) algorithm: Provides integrity and authenticity, a combination known as authenticated encryption.
  12. Compare password storage approaches

    master

    When designing a system for user authentication, choose a password storage method based on the required security level.

    ApproachSecurityComments
    Clear-text passwordsExtremely lowNever do this: compromised server will render all passwords leaked
    Simple password hashLowVulnerable to dictionary attacks
    Salted hashed passwordsAverageVulnerable to GPU-based and ASIC-based password cracking
    Secure KDF function (like Argon2)HighRecommended, use strong KDF parameters