Skip to content

Latest commit

 

History

History
74 lines (53 loc) · 13.7 KB

libsodium_overview.md

File metadata and controls

74 lines (53 loc) · 13.7 KB

Cross-platform cryptography

Libsodium overview

This chapter is about a library called Libsodium that is also know with NACL or SODIUM. It's most advantage is that it is available for almost all major frameworks and provides cryptography methods that are "state of the art".

You will find authenticated encryption, private-public-key encryption and private-public-key signatures but none of them is "old fashioned". Below you get an overview of the functionality of this library:

The authenticated encryption has it's basis in the XSalsa20 stream cipher, combined with Poly1305 MAC authentication ("secret box").

The private-public-key encryption uses the X25519 curve for key exchange, the XSalsa20 stream cipher combined with Poly1305 MAC for authenticated encryption ("crypto box").

A special form of the private-public-key encryption is the anonymus encryption (called "sealed cryto box") where just the private-public key pair of the recipient will be used.

For generating a digital signature the Ed25519 curve will be used.

Do you need a hash or "fingerprint" of a string or file? Ok, Libsodium uses the Blake2b hash algorithm for that task.

If want to derive an encryption key from a passphrase it will be generated with Argon2 algorithm.

Giving a summary you may say "I have never heard the names of the algorithms before" and you are right... me too! That's the reason why I did not try to find a "cross platform solution" as there is no AES, RSA or traditional EC curve cryptography available in Libsodium. When viewing the sourcecodes you will notice that all Libsodium programs are short compared to the traditional ones because it is a high developed library that does a lot in the background.

A more detailed overview is available with the original Libsodium documentation that is written in and for the "C" framework but the functionality is equal on all bindings for other languages.

But the good news are: there are bindings available for Java, PHP (built-in since PHP 7.x), C#, NodeJs and for browser usage and I'm providing solutions for all major cryptography tasks like key encryption, private-public encryption and signing. For all starting cryptography with Libsodium I strongly recommend to do this in PHP because Libsodium support is included (PHP version >= 7.3) and all of my examples will run "out of the box".

Here are my articles regarding Libsodium themes:

| Solution | Description | Jav | PHP | C# | CJs | Web | Pyt | Go | Dart | | ------ | :------: | :--: | :--: | :--: | :--: | :--: | :--: | :--: | |generate a random key | see the examples in authenticated string encryption with a random key | ✅ | ✅ | ✅ | ✅ | ✅ |
|generate a curve 25519 key pair | generate a curve X25519 private key and public key pair in Base64 encoding | ✅ | ✅ | ✅ | ✅ | ❌ |
|derive the curve X25519 public key from a secret key | generate the X25519 public key from a secret key in Base64 encoding | 🔜 | 🔜 | 🔜 | 🔜 | ✅ |
|generate a curve ED25519 key pair | generate a curve ED25519 private key and public key pair in Base64 encoding | ✅ | ✅ | ✅ | ✅ | ✅ | | | ✅ | |generate a curve ED25519 public key from a private key | generate an ED25519 public key from a private key in Base64 encoding | ✅ | ✅ | ✅ | ✅ | ✅ | | | ✅ |
| Argon 2 parameter | describes the parameters used in Argon 2 algorithm | | | | | | | | | Argon 2 password derivation function | derives an encryption key from a passphrase | ✅ | ✅ | ✅ | ✅ | ✅ |
|ChaCha20-Poly1305 authenticated string encryption with a random key | uses ChaCha20-Poly1305 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | |authenticated string encryption with a random key | uses "secret boxes" | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | |authenticated string encryption with a passphrase | uses "secret boxes" | 🔜 | 🔜 | 🔜 | 🔜 | 🔜 | 🔜 | |authenticated hybrid string encryption with a private-public key exchange | uses "crypto boxes" | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | |anonymizised authenticated string hybrid encryption with a private-public key | uses "sealed boxes" | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | |sign a string with a private-public key | uses an ED25519 curve signature | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |

Jav=Java, PHP=PHP, C#=Csharp, CJs=Javascript with Crypto-JS, Ndc=NodeJs with Crypto, Nfo=NodeJs with forge, Web=WebCrypto, Pyt=Python, Go=Golang, Dar=Dart

What additional libraries do I need to get the stuff working?

Below I'm providing the names and download links of all libraries I have used to run the examples. Please note that I did not take a deep care of the license terms of the libraries - please check them before using them in any (commercial or private) context.

Framework library library is used in... and documentation source and download link
Java Bouncy Castle version 1.68 Argon 2 password derivation function documentation: https://github.com/bcgit/bc-java GitHub: https://github.com/bcgit/bc-java Maven: https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk15on
Java TweetNaclFast.java Libsodium crypto box authenticated hybrid string encryption, Libsodium sealed box authenticated hybrid string encryption documentation: README.md GitHub: https://github.com/InstantWebP2P/tweetnacl-java direct link:
Java Blake2b Libsodium sealed box authenticated hybrid string encryption documentation: README.md GitHub: https://github.com/alphazero/Blake2b
Java xsalsa20poly1305 version 0.11.0 Libsodium crypto box authenticated hybrid string encryption documentation: https://github.com/codahale/xsalsa20poly1305] Github: https://github.com/codahale/xsalsa20poly1305 Maven: https://mvnrepository.com/artifact/com.codahale/xsalsa20poly1305
PHP built-in since PHP 7 >= 7.2.0 https://www.php.net/manual/en/book.sodium.php GitHub: https://github.com/paragonie/pecl-libsodium-doc
C# Argon 2 library Argon 2 password derivation function documentation: https://github.com/kmaragon/Konscious.Security.Cryptography https://github.com/kmaragon/Konscious.Security.Cryptography
C# libsodium.core version 1.2.3 Libsodium crypto box authenticated hybrid string encryption, Libsodium sealed box authenticated hybrid string encryption documentation https://github.com/tabrath/libsodium-core NuGet: https://www.nuget.org/packages/Sodium.Core/
NodeJs argon 2 version 0.27.1 Argon 2 password derivation function documentation: https://github.com/ranisalt/node-argon2#readme https://github.com/ranisalt/node-argon2
NodeJs TweetNaCl.js version 1.0.3 Libsodium crypto box authenticated hybrid string encryption, Libsodium sealed box authenticated hybrid string encryption Basic library https://github.com/dchest/tweetnacl-js https://github.com/dchest/tweetnacl-js.
NodeJs Tweetnacl-util-js version 0.15.1 Libsodium crypto box authenticated hybrid string encryption, Libsodium sealed box authenticated hybrid string encryption neccessary for data encoding https://github.com/dchest/tweetnacl-util-js https://github.com/dchest/tweetnacl-util-js
Browser argon 2 browser Argon 2 password derivation function documentation: https://github.com/antelle/argon2-browser https://github.com/antelle/argon2-browser
Browser TweetNaCl.js version 1.0.3 Libsodium crypto box authenticated hybrid string encryption, Libsodium sealed box authenticated hybrid string encryption Basic library https://github.com/dchest/tweetnacl-js https://github.com/dchest/tweetnacl-js. My programs are tested with nacl.min.js
Browser Tweetnacl-util-js version 0.15.1 Libsodium crypto box authenticated hybrid string encryption, Libsodium sealed box authenticated hybrid string encryption neccessary for data encoding https://github.com/dchest/tweetnacl-util-js https://github.com/dchest/tweetnacl-util-js I used this file: nacl-util.js
Python pynacl version 1.4.0 used in generate a curve ED25519 public key from a private key, sign a string with a private-public key, authenticated string encryption with a random key, authenticated hybrid string encryption with a private-public key exchange, anonymizised authenticated string hybrid encryption with a private-public key documentation: https://pynacl.readthedocs.io/en/latest/ https://github.com/pyca/pynacl/
Python pycryptodome version 3.9.9 used in ChaCha20-Poly1305 authenticated string encryption with a random key documentation https://www.pycryptodome.org/ https://github.com/Legrandin/pycryptodome
Dart pointycastle version 3.1.1 used in ChaCha20-Poly1305 authenticated string encryption with a random key documentation https://github.com/bcgit/pc-dart
Dart flutter_sodium 0.20 used in sign a string with a private-public key, authenticated string encryption with a random key, authenticated hybrid string encryption with a private-public key exchange, anonymizised authenticated string hybrid encryption with a private-public key documentation: https://github.com/firstfloorsoftware/flutter_sodium

Last update: Aug. 17th 2021

Back to the main page: readme.md