All Projects → tleonhardt → practical_cryptography_engineering

tleonhardt / practical_cryptography_engineering

Licence: Apache-2.0 license
Cryptography code examples using libsodium and mbedtls C libraries and Python cryptography and PyNaCl modules

Programming Languages

c
50402 projects - #5 most used programming language
C++
36643 projects - #6 most used programming language
python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to practical cryptography engineering

Kalium
Java binding to the Networking and Cryptography (NaCl) library with the awesomeness of libsodium
Stars: ✭ 203 (+238.33%)
Mutual labels:  libsodium
dryoc
Don't Roll Your Own Crypto: pure-Rust, hard to misuse cryptography library
Stars: ✭ 163 (+171.67%)
Mutual labels:  libsodium
awesome-practical-posts
I sum up the articles of tech blogs and share that.
Stars: ✭ 64 (+6.67%)
Mutual labels:  practical
Nsec
A modern and easy-to-use cryptographic library for .NET Core based on libsodium
Stars: ✭ 217 (+261.67%)
Mutual labels:  libsodium
libgodium
Pure Go implementation of cryptographic APIs found in libsodium
Stars: ✭ 46 (-23.33%)
Mutual labels:  libsodium
iot security
《密码技术与物联网安全——mbedtls开发实战》示例代码
Stars: ✭ 111 (+85%)
Mutual labels:  mbedtls
Libsodium Jni
(Android) Networking and Cryptography Library (NaCL) JNI binding. JNI is utilized for fastest access to native code. Accessible either in Android or Java application. Uses SWIG to generate Java JNI bindings. SWIG definitions are extensible to other languages.
Stars: ✭ 157 (+161.67%)
Mutual labels:  libsodium
rawr-x3dh
TypeScript Implementation of X3DH
Stars: ✭ 51 (-15%)
Mutual labels:  libsodium
soda
Libsodium bindings for Erlang
Stars: ✭ 17 (-71.67%)
Mutual labels:  libsodium
secrets
Not Yet Another Password Manager written in Go using libsodium
Stars: ✭ 28 (-53.33%)
Mutual labels:  libsodium
sodalite
tweetnacl in rust
Stars: ✭ 26 (-56.67%)
Mutual labels:  libsodium
molch
An implementation of the axolotl ratchet based on libsodium.
Stars: ✭ 24 (-60%)
Mutual labels:  libsodium
salty
Portable NaCl-powered encryption
Stars: ✭ 26 (-56.67%)
Mutual labels:  libsodium
Pgsodium
Modern cryptography for PostgreSQL using libsodium.
Stars: ✭ 202 (+236.67%)
Mutual labels:  libsodium
privoxy-windows
A windows port of Privoxy project
Stars: ✭ 15 (-75%)
Mutual labels:  mbedtls
Enacl
Erlang bindings for NaCl / libsodium
Stars: ✭ 159 (+165%)
Mutual labels:  libsodium
libsalty
Elixir bindings for libsodium (NIF)
Stars: ✭ 20 (-66.67%)
Mutual labels:  libsodium
lazysodium-java
A Java implementation of the Libsodium crypto library. For the lazy dev.
Stars: ✭ 110 (+83.33%)
Mutual labels:  libsodium
Kryptor
A simple, modern, and secure encryption and signing tool that aims to be a better version of age and Minisign.
Stars: ✭ 267 (+345%)
Mutual labels:  libsodium
libsodium-cmake
Wrapper around the libsodium repository providing good integration with CMake when using FetchContent or adding it as a submodule.
Stars: ✭ 21 (-65%)
Mutual labels:  libsodium

Practical Cryptography Engineering

This repository contains some practical code examples of using the following cryptography libraries:

  • libsodium
    • A modern, portable, easy to use crypto library written in C with a small number of high quality primitives
    • Focuses on making it easy to use cryptography correctly
  • mbedTLS
    • An ultra-portable crypto library written in C which should build anywhere
    • Provides a wide range of the most common cryptographic primitives and associated infrastructure
  • cryptography
    • Python's "standard" cryptographic library which is a wrapper around OpenSSL
    • Provides almost all cryptographic primitives you would want in Python
  • PyNaCl
    • Python bindings for libsodium (very partial wrapper around libsodium)
    • Provides a few nice cryptographic primitives not currently available in the cryptography module

File Contents

Build-related and Miscellaneous

  • CMakeLists.txt
    • CMake file for building the mbedTLS C code projects
  • mbedtls
    • Directory containing the mbedTLS C code
  • sodium
    • Directory containing libsodium examples, headers, and Windows pre-compiled library
    • See the Readme.md in this directory for more info on these examples

Symmetric Encryption

These code examples use an AES block cipher with a 256-bit key in Galois Counter Mode (GCM). The C code examples use the mbedTLS library, while the Python examples use the cryptography module.

  • aes_gcm.c
    • Simple self-contained C code example of using AES-256 in Galois Counter Mode (GCM) using hard-coded everything
  • aes_gcm_cryptography.py
    • Simple self-contained Python code example identical to the above
  • aesgcm_file.c
    • C code example of file-based AES-256 GCM, works with aesgcm_file.py
    • Takes arguments on command line and produces output to file
  • aesgcm_file.py
    • Python code example of file-based AES-256 GCM, works with aesgcm_file.c

The following example uses the PyNaCl wrapper around libsodium along with the SecretBox authenticated encryption API.

  • nacl_symmetric_gen.py
    • Generates a random 256-bit (32-byte) secret symmetric key and saves it to a file
    • Then uses it to encrypt a fixed message and verify that it can decrypt it and get the same message

Key Exchange

These code examples use an Elliptic-curve Diffie-Hellman ECDH key agreement protocol to establish a shared secret over an insecure channel. The C code examples use the mbedTLS library, while the Python examples use the cryptography module.

  • ecdh.c
    • Elliptic Curve Diffie-Hellman key exchange C code example
  • ecdh.py
    • Elliptic Curve Diffie-Hellman key exchange Python code example

Key Derivation

These code examples demonstrate how to use a Key Derivation Function KDF to derive one or more shared keys from a shared secret.

  • kdf.c
    • Key Derivation Function (KDF) C code example
  • kdf.py
    • Key Derivation Function (KDF) Python code example

Digital Signatures

These examples use the PyNaCl wrapper around libsodium to support public-key digital signatures using the Ed25519 algorithm.

  • nacl_genkey.py
    • Generates a random ed25519 SigningKey/VerifyingKey key pair for use with a digital signature system
  • nacl_sign.py
    • Uses PyNaCl to sign a message using ed25519 digital signature algorithm
  • nacl_verify.py
    • Uses PyNaCl to verify an ed25519 signature for a given message

These examples use RSA-PSS digital signatures. The C code examples use the mbedTLS library, while the Python examples use the cryptography module.

  • rsa_signature.c
    • RSA Signature C code example
  • rsa_signature.py
    • RSA Signature Python code example

Building

libsodium C examples

The libsodium C code examples are all in the sodium directory and can be built using the Cmake cross-platform build tool along with your platform default C compiler installed on Windows, macOS, or Linux.

The first stage of building is the same on all platforms:

cd sodium
rm -rf build
mkdir build
cd build
cmake ..

The second stage of building is platform dependent and will create the following executable files:

  • hello_sodium
  • nacl_keygen
  • nacl_sign
  • nacl_verify
  • symmetric_decrypt
  • symmetric_encrypt
  • symmetric_keygen
  • test_ed25519
  • test_pynacl_compatibility

Linux or macOS

make

This produces the executable files directly in the build directory.

Windows

devenv hello_sodium.sln /build Debug

This creates the executable files under the build\Debug directory.

Python examples

The Python examples are located in the root directory and should work with Python 3.4 or newer. The Python examples require a mix of the following Python packages:

The required dependencies can easily be installed using Pipenv:

pipenv install

Then a shell using the underlying virtual environment can be entered with:

pipenv shell

Inside that Pipenv shell, any of the examples can be ran directly. e.g.:

python ./aes_gcm_cryptography.py

The Python examples are intended to interoperate with either the libsodium or mbedTLS C code examples. Thus encryption or signing can be done in C and decryption or verifying can be done in Python or vice versa.

mbedtls C examples

The mbedTLS C code examples are located in the root directory and build mbedTLS from source from the mbedtls directory.

Build requires CMake and platform default C compiler installed and works on both Windows, macOS, and Linux.

The first stage of building is the same on all platforms:

rm -rf build
mkdir build
cd build
cmake ..

The second stage of building is platform dependent ...

Linux or macOS

make

This produces the following executable files directly in the build directory:

  • aes_gcm
  • aesgcm_file
  • ecdh
  • kdf
  • rsa_signature

Windows

devenv mbed_AES.sln /build Debug

This creates the following executable files under the build\Debug directory:

  • aes_gcm.exe
  • aesgcm_file.exe
  • ecdh.exe
  • kdf.exe
  • rsa_signature.exe

Where to learn more about cryptography

Books

  • Cryptography Engineering by Niels Ferguson, Bruce Schneier, and Tadayoshi Kohno
    • Extremely well written and easy to understand
    • Focuses on the practical aspects that often result in weak crypto when used incorrectly
    • Discusses how to build an entire cryptographic system from the ground up
  • Understanding Cryptography by Christof Paar, Jan Pelzl, and Bart Preneel
    • Amazing book which makes it relatively easy to teach yourself cryptography
    • Website
    • YouTube lecture videos
    • Solutions Manual, Lecture Slides

Online Courses

  • Cryptography I
    • Taught by Stanford University professor Dan Boneh
    • Available for free on Coursera
  • Applied Cryptography
    • Taught by University of Virginia professor Dave Evans
    • Available for free on Udacity

Presentation

This repository includes the following basic intro presentation:

Disclaimer

I am not an expert in cryptography. I am just a software developer who wanted to learn more about how to use cryptography effectively. If anyone looking at this is an expert in cryptography and happens to notice any weaknesses, inaccuracies, or mistakes and/or has constructive feedback for how to improve the examples then Pull Requests or Issues would be sincerely appreciated.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].