Cryptography in Python Hashes, ECC and ECDSA, Eth Keys Library ECDSA in Python: Generate / Load Keys
Total Page:16
File Type:pdf, Size:1020Kb
Table of Contents Welcome 1.1 Preface 1.2 Cryptography - Overview 1.3 Hash Functions 1.4 Crypto Hashes and Collisions 1.4.1 Hash Functions: Applications 1.4.2 Secure Hash Algorithms 1.4.3 Hash Functions - Examples 1.4.4 Exercises: Calculate Hashes 1.4.5 Proof-of-Work Hash Functions 1.4.6 MAC and Key Derivation 1.5 HMAC and Key Derivation 1.5.1 HMAC Calculation - Examples 1.5.2 Exercises: Calculate HMAC 1.5.3 KDF: Deriving Key from Password 1.5.4 PBKDF2 1.5.5 Modern Key Derivation Functions 1.5.6 Scrypt 1.5.7 Bcrypt 1.5.8 Linux crypt() 1.5.9 Argon2 1.5.10 Password Encryption 1.5.11 Exercises: Password Encryption 1.5.12 Secure Random Generators 1.6 Pseudo-Random Numbers - Examples 1.6.1 Secure Random Generators (CSPRNG) 1.6.2 Exercises: Pseudo-Random Generator 1.6.3 Key Exchange and DHKE 1.7 Diffie–Hellman Key Exchange 1.7.1 DHKE - Examples 1.7.2 Exercises: DHKE Key Exchange 1.7.3 Encryption: Symmetric and Asymmetric 1.8 Symmetric Key Ciphers 1.9 Cipher Block Modes 1.9.1 Popular Symmetric Algorithms 1.9.2 The AES Cipher - Concepts 1.9.3 AES Encrypt / Decrypt - Examples 1.9.4 Ethereum Wallet Encryption 1.9.5 2 Exercises: AES Encrypt / Decrypt 1.9.6 ChaCha20-Poly1305 1.9.7 Exercises: ChaCha20-Poly1305 1.9.8 Asymmetric Key Ciphers 1.10 The RSA Cryptosystem - Concepts 1.10.1 RSA Encrypt / Decrypt - Examples 1.10.2 Exercises: RSA Encrypt / Decrypt 1.10.3 Elliptic Curve Cryptography (ECC) 1.10.4 ECDH Key Exchange 1.10.5 ECDH Key Exchange - Examples 1.10.6 Exercises: ECDH Key Exchange 1.10.7 ECC Encryption / Decryption 1.10.8 ECIES Hybrid Encryption Scheme 1.10.9 ECIES Encryption - Example 1.10.10 Exercises: ECIES Encrypt / Decrypt 1.10.11 Digital Signatures 1.11 RSA Signatures 1.11.1 RSA: Sign / Verify - Examples 1.11.2 Exercises: RSA Sign and Verify 1.11.3 ECDSA: Elliptic Curve Signatures 1.11.4 ECDSA: Sign / Verify - Examples 1.11.5 Exercises: ECDSA Sign and Verify 1.11.6 EdDSA and Ed25519 1.11.7 EdDSA: Sign / Verify - Examples 1.11.8 Exercises: EdDSA Sign and Verify 1.11.9 Quantum-Safe Cryptography 1.12 Quantum-Safe Signatures - Example 1.12.1 More Cryptographic Concepts 1.13 Digital Certificates - Example 1.13.1 TLS - Example 1.13.2 One-Time Passwords (OTP) - Example 1.13.3 Crypto Libraries for Developers 1.14 JavaScript Crypto Libraries 1.14.1 Python Crypto Libraries 1.14.2 C# Crypto Libraries 1.14.3 Java Crypto Libraries 1.14.4 Conclusion 1.15 3 4 Welcome Practical Cryptography for Developers - Free Book A modern practical book about cryptography for developers with code examples, covering core concepts like: hashes (like SHA-3 and BLAKE2), MAC codes (like HMAC and GMAC), key derivation functions (like Scrypt, Argon2), key agreement protocols (like DHKE, ECDH), symmetric ciphers (like AES and ChaCha20, cipher block modes, authenticated encryption, AEAD, AES-GCM, ChaCha20-Poly1305), asymmetric ciphers and public-key cryptosystems (RSA, ECC, ECIES), elliptic curve cryptography (ECC, secp256k1, curve25519), digital signatures (ECDSA and EdDSA), secure random numbers (PRNG, CSRNG) and quantum-safe cryptography, along with crypto libraries and developer tools, with a lots of code examples in Python and other languages. Author: Svetlin Nakov, PhD - http://www.nakov.com This book is free and open-source, published under the MIT license. Official GitHub repo: https://github.com/nakov/practical-cryptography-for-developers-book. Sofia, November 2018 Summary Welcome Preface Cryptography - Overview Hash Functions Crypto Hashes and Collisions Hash Functions: Applications Secure Hash Algorithms Hash Functions - Examples Exercises: Calculate Hashes Proof-of-Work Hash Functions MAC and Key Derivation HMAC and Key Derivation HMAC Calculation - Examples Exercises: Calculate HMAC KDF: Deriving Key from Password PBKDF2 Modern Key Derivation Functions Scrypt Bcrypt Linux crypt() Argon2 Password Encryption Exercises: Password Encryption Secure Random Generators Pseudo-Random Numbers - Examples Secure Random Generators (CSPRNG) Exercises: Pseudo-Random Generator Key Exchange and DHKE Diffie–Hellman Key Exchange DHKE - Examples Exercises: DHKE Key Exchange Encryption: Symmetric and Asymmetric 5 Welcome Symmetric Key Ciphers Cipher Block Modes Popular Symmetric Algorithms The AES Cipher - Concepts AES Encrypt / Decrypt - Examples Ethereum Wallet Encryption Exercises: AES Encrypt / Decrypt ChaCha20-Poly1305 Exercises: ChaCha20-Poly1305 Asymmetric Key Ciphers The RSA Cryptosystem - Concepts RSA Encrypt / Decrypt - Examples Exercises: RSA Encrypt / Decrypt Elliptic Curve Cryptography (ECC) ECDH Key Exchange ECDH Key Exchange - Examples Exercises: ECDH Key Exchange ECC Encryption / Decryption ECIES Hybrid Encryption Scheme ECIES Encryption - Example Exercises: ECIES Encrypt / Decrypt Digital Signatures RSA Signatures RSA: Sign / Verify - Examples Exercises: RSA Sign and Verify ECDSA: Elliptic Curve Signatures ECDSA: Sign / Verify - Examples Exercises: ECDSA Sign and Verify EdDSA and Ed25519 EdDSA: Sign / Verify - Examples Exercises: EdDSA Sign and Verify Quantum-Safe Cryptography Quantum-Safe Signatures - Example More Cryptographic Concepts Digital Certificates - Example TLS - Example One-Time Passwords (OTP) - Example Crypto Libraries for Developers JavaScript Crypto Libraries Python Crypto Libraries C# Crypto Libraries Java Crypto Libraries Conclusion Tags: cryptography, free, book, Nakov, Svetlin Nakov, hashes, hash function, SHA-256, SHA3, BLAKE2, RIPEMD, MAC, message authentication code, HMAC, KDF, key derivation, key derivation function, PBKDF2, Scrypt, Bcrypt, Argon2, password hashing, random generator, pseudo-random numbers, CSPRNG, secure random generator, key exchange, key agreement, Diffie-Hellman, DHKE, ECDH, symmetric ciphers, asymmetric ciphers, public key cryptosystems, symmetric cryptography, AES, Rijndael, cipher block mode, AES-CTR, AES-GCM, ChaCha20- Poly1305, authenticated encryption, encryption scheme, public key cryptography, RSA, ECC, elliptic curves, secp256k1, curve25519, EC points, EC domain parameters, ECDH key agreement, asymmetric encryption scheme, 6 Welcome hybrid encryption, ECIES, digital signature, RSA signature, DSA, ECDSA, EdDSA, ElGammal signature, Schnorr signature, quantum-safe cryptography, digital certificates, TLS, OAuth, multi-factor authentication, crypto libraries, Python cryptography, JavaScript cryptography, C# cryptography, Java cryptography, C++ cryptography, PHP cryptography. 7 Preface Preface ... Most books about cryptography are written either in too academic style with a lot of theory, like http://cacr.uwaterloo.ca/hac. ... Others are too old: ... ... Others are not bad, but not free: https://leanpub.com/crypto https://www.amazon.com/Cryptography-Developers-Tom-St-Denis/dp/1597491047 Crypto libraries come with limited and not consistently organized documentation, e.g. the Crypto++ Wiki https://www.cryptopp.com/wiki/Main_page. ... Now I am happy to publish a developer-friendly practical cryptography book. It holds just what developers need to know in order to use cryptography in their every day work. It does not cover the internals of the algortithms and how to design symmetric ciphers or authentication algorithms. It covers the basic understanding of the core cryptographic concepts and how to use them: libraries, tools, code examples. ... The book author Svetlin Nakov is involved with applied cryptography from 2005, when he published the book "Java for Digitally Signing Documents of the Web" (in Bulgarian), following his master thesis on a similar topic. ... It is not required to be strong mathematician to understand the cryptographic concepts from developer perspective. This book will teach you the basic concepts is almost math-free style. 8 Cryptography - Overview Overview of Modern Cryptography Cryptography has evolved from its first attempts (thousands years ago), through the first successful cryptographic algorithms for developers (like the now retired MD5 and DES) to modern crypto algorithms (like SHA-3, Argon2 and ChaCha20). Let's first introduce very shortly the basic cryptography concepts, that developers should know, like cryptographic hash functions (SHA-256, SHA3, RIPEMD and others), HMAC (hashed message authentication code), password to key derivation functions (like Scrypt), the Diffie-Hellman key-exchange protocol, symmetric key encryption schemes (like the AES cipher with CBC and CTR block modes) and asymmetric key encryption schemes with public and private keys (like the RSA cipher and elliptic curves-based cryptography / ECC, the secp256k1 curve and the Ed25519 cryptosystem), digital signatures and ECDSA, as well as the concept of entropy and secure random number generation and quantum-safe cryptography. Encrypt / Decrypt Message - Live Demo As a simple example, we shall demonstrate message encryption + decryption using the AES encryption algorithm. Play with this online tool: https://aesencryption.net. We shall learn later that behind this simple AES encryption, there are many algorithms and settings hidden inside, like password to key-derivation function and its parameters, block cipher mode, cipher initial vector, message authentication code and others. 9 Cryptography - Overview What is Cryptography? Cryptography is the science of providing security and protection of information. It is used everywhere in our digital world: when you open a Web site, send an email or connect to the WiFi network. What's why developers should have at least basic understanding of cryptography and how to use crypto algorithms and crypto libraries, to understand hashing, symmetric and asymmetric ciphers