Skip to main content

An abstraction layer for the cryptography used by Devolutions

Project description

devolutions-crypto

Build Status
Cryptographic library used in Devolutions products. It is made to be fast, easy to use and misuse-resistant.

Documentation

Usage

Overview

The library is splitted into multiple modules, which are explained below. When dealing with "managed" data, that includes an header and versionning, you deal with structures like Ciphertext, PublicKey, etc.

These all implements TryFrom<&[u8]> and Into<Vec<u8>> which are the implemented way to serialize and deserialize data.

use std::convert::TryFrom as _;
use devolutions_crypto::utils::generate_key;
use devolutions_crypto::ciphertext::{ encrypt, CiphertextVersion, Ciphertext };

let key: Vec<u8> = generate_key(32);

let data = b"somesecretdata";

let encrypted_data: Ciphertext = encrypt(data, &key, CiphertextVersion::Latest).expect("encryption shouldn't fail");

// The ciphertext can be serialized.
let encrypted_data_vec: Vec<u8> = encrypted_data.into();

// This data can be saved somewhere, passed to another language or over the network
// ...
// When you receive the data as a byte array, you can deserialize it into a struct using TryFrom

let ciphertext = Ciphertext::try_from(encrypted_data_vec.as_slice()).expect("deserialization shouldn't fail");

let decrypted_data = ciphertext.decrypt(&key).expect("The decryption shouldn't fail");

assert_eq!(decrypted_data, data);

Ciphertext

This module contains everything related to encryption. You can use it to encrypt and decrypt data using either a shared key of a keypair.
Either way, the encryption will give you a Ciphertext, which has a method to decrypt it.

Symmetric

use devolutions_crypto::utils::generate_key;
use devolutions_crypto::ciphertext::{ encrypt, CiphertextVersion, Ciphertext };

let key: Vec<u8> = generate_key(32);

let data = b"somesecretdata";

let encrypted_data: Ciphertext = encrypt(data, &key, CiphertextVersion::Latest).expect("encryption shouldn't fail");

let decrypted_data = encrypted_data.decrypt(&key).expect("The decryption shouldn't fail");

assert_eq!(decrypted_data, data);

Asymmetric

Here, you will need a PublicKey to encrypt data and the corresponding PrivateKey to decrypt it. You can generate them by using generate_keypair or derive_keypair in the Key module.

use devolutions_crypto::key::{generate_keypair, KeyVersion, KeyPair};
use devolutions_crypto::ciphertext::{ encrypt_asymmetric, CiphertextVersion, Ciphertext };

let keypair: KeyPair = generate_keypair(KeyVersion::Latest);

let data = b"somesecretdata";

let encrypted_data: Ciphertext = encrypt_asymmetric(data, &keypair.public_key, CiphertextVersion::Latest).expect("encryption shouldn't fail");

let decrypted_data = encrypted_data.decrypt_asymmetric(&keypair.private_key).expect("The decryption shouldn't fail");

assert_eq!(decrypted_data, data);

Key

For now, this module only deal with keypairs, as the symmetric keys are not wrapped yet.

Generation/Derivation

You have two ways to generate a KeyPair: Using generate_keypair will generate a random one, using derive_keypair will derive one from another password or key along with derivation parameters(including salt). Except in specific circumstances, you should use generate_keypair.

Asymmetric keys have two uses. They can be used to encrypt and decrypt data and to perform a key exchange.

generate_keypair

use devolutions_crypto::key::{generate_keypair, KeyVersion, KeyPair};

let keypair: KeyPair = generate_keypair(KeyVersion::Latest);

derive_keypair

use devolutions_crypto::Argon2Parameters;
use devolutions_crypto::key::{KeyVersion, KeyPair, derive_keypair};

let parameters: Argon2Parameters = Default::default();
let keypair: KeyPair = derive_keypair(b"thisisapassword", &parameters, KeyVersion::Latest).expect("derivation should not fail");

Key Exchange

The goal of using a key exchange is to get a shared secret key between two parties without making it possible for users listening on the conversation to guess that shared key.

  1. Alice and Bob generates a KeyPair each.
  2. Alice and Bob exchanges their PublicKey.
  3. Alice mix her PrivateKey with Bob's PublicKey. This gives her the shared key.
  4. Bob mixes his PrivateKey with Alice's PublicKey. This gives him the shared key.
  5. Both Bob and Alice has the same shared key, which they can use for symmetric encryption for further communications.
use devolutions_crypto::key::{generate_keypair, mix_key_exchange, KeyVersion, KeyPair};

let bob_keypair: KeyPair = generate_keypair(KeyVersion::Latest);
let alice_keypair: KeyPair = generate_keypair(KeyVersion::Latest);

let bob_shared = mix_key_exchange(&bob_keypair.private_key, &alice_keypair.public_key).expect("key exchange should not fail");

let alice_shared = mix_key_exchange(&alice_keypair.private_key, &bob_keypair.public_key).expect("key exchange should not fail");

// They now have a shared secret!
assert_eq!(bob_shared, alice_shared);

PasswordHash

You can use this module to hash a password and validate it afterward. This is the recommended way to verify a user password on login.

use devolutions_crypto::password_hash::{hash_password, PasswordHashVersion};

let password = b"somesuperstrongpa$$w0rd!";

let hashed_password = hash_password(password, 10000, PasswordHashVersion::Latest);

assert!(hashed_password.verify_password(b"somesuperstrongpa$$w0rd!"));
assert!(!hashed_password.verify_password(b"someweakpa$$w0rd!"));

SecretSharing

This module is used to generate a key that is splitted in multiple Share and that requires a specific amount of them to regenerate the key.
You can think of it as a "Break The Glass" scenario. You can generate a key using this, lock your entire data by encrypting it and then you will need, let's say, 3 out of the 5 administrators to decrypt the data. That data could also be an API key or password of a super admin account.

use devolutions_crypto::secret_sharing::{generate_shared_key, join_shares, SecretSharingVersion, Share};

// You want a key of 32 bytes, splitted between 5 people, and I want a 
// minimum of 3 of these shares to regenerate the key.
let shares: Vec<Share> = generate_shared_key(5, 3, 32, SecretSharingVersion::Latest).expect("generation shouldn't fail with the right parameters");

assert_eq!(shares.len(), 5);
let key = join_shares(&shares[2..5]).expect("joining shouldn't fail with the right shares");

Signature

This module is used to sign data using a keypair to certify its authenticity.

Generating Key Pairs

use devolutions_crypto::signing_key::{generate_signing_keypair, SigningKeyVersion, SigningKeyPair, SigningPublicKey};

let keypair: SigningKeyPair = generate_signing_keypair(SigningKeyVersion::Latest);

Signing Data

use devolutions_crypto::signature::{sign, Signature, SignatureVersion};

let signature: Signature = sign(b"this is some test data", &keypair, SignatureVersion::Latest);

Verifying the signature

use devolutions_crypto::signature::{sign, Signature, SignatureVersion};

assert!(signature.verify(b"this is some test data", &public_key));

Utils

These are a bunch of functions that can be useful when dealing with the library.

Key Generation

This is a method used to generate a random key. In almost all case, the length parameter should be 32.

use devolutions_crypto::utils::generate_key;

let key = generate_key(32);
assert_eq!(32, key.len());

Key Derivation

This is a method used to generate a key from a password or another key. Useful for password-dependant cryptography. Salt should be a random 16 bytes array if possible and iterations should be 10000 or configurable by the user.

use devolutions_crypto::utils::{generate_key, derive_key};
let key = b"this is a secret password";
let salt = generate_key(16);
let iterations = 10000;
let length = 32;

let new_key = derive_key(key, &salt, iterations, length);

assert_eq!(32, new_key.len());

Underlying algorithms

As of the current version:

  • Symmetric cryptography uses XChaCha20Poly1305
  • Asymmetric cryptography uses Curve25519.
  • Asymmetric encryption uses ECIES.
  • Key exchange uses x25519, or ECDH over Curve25519
  • Password Hashing uses PBKDF2-HMAC-SHA2-256
  • Secret Sharing uses Shamir Secret sharing over GF256

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

devolutions_crypto-0.8.0.tar.gz (45.7 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

devolutions_crypto-0.8.0-cp39-none-win_amd64.whl (277.1 kB view details)

Uploaded CPython 3.9Windows x86-64

devolutions_crypto-0.8.0-cp39-cp39-manylinux_2_24_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.24+ x86-64

devolutions_crypto-0.8.0-cp39-cp39-macosx_10_7_x86_64.whl (393.7 kB view details)

Uploaded CPython 3.9macOS 10.7+ x86-64

devolutions_crypto-0.8.0-cp38-none-win_amd64.whl (277.1 kB view details)

Uploaded CPython 3.8Windows x86-64

devolutions_crypto-0.8.0-cp38-cp38-manylinux_2_24_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.24+ x86-64

devolutions_crypto-0.8.0-cp38-cp38-macosx_10_7_x86_64.whl (393.7 kB view details)

Uploaded CPython 3.8macOS 10.7+ x86-64

devolutions_crypto-0.8.0-cp37-none-win_amd64.whl (277.0 kB view details)

Uploaded CPython 3.7Windows x86-64

devolutions_crypto-0.8.0-cp37-cp37m-manylinux_2_24_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.24+ x86-64

devolutions_crypto-0.8.0-cp37-cp37m-macosx_10_7_x86_64.whl (393.7 kB view details)

Uploaded CPython 3.7mmacOS 10.7+ x86-64

File details

Details for the file devolutions_crypto-0.8.0.tar.gz.

File metadata

  • Download URL: devolutions_crypto-0.8.0.tar.gz
  • Upload date:
  • Size: 45.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for devolutions_crypto-0.8.0.tar.gz
Algorithm Hash digest
SHA256 96c2cdace5424408692e1d23d5ee5ce2bb5035adf998c2dcf666ab91c3349c97
MD5 fe37732a8fa76d5a3b579aabb938f27b
BLAKE2b-256 34bc423dd97422e04111ac98a48569394deb59ab6d80ae4eaaa0151b2549de82

See more details on using hashes here.

File details

Details for the file devolutions_crypto-0.8.0-cp39-none-win_amd64.whl.

File metadata

  • Download URL: devolutions_crypto-0.8.0-cp39-none-win_amd64.whl
  • Upload date:
  • Size: 277.1 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for devolutions_crypto-0.8.0-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 82c43f1c50cc28fa5f9d9c24b5961afca57367bc5e13ab2130350deb19897de1
MD5 6e452c2db75f5210a7d5bbe42a9f5381
BLAKE2b-256 6c86cd052ff44aedd5a482a9b1e1b7b38e2a58f4ac0e825f0bf27ec5217f6deb

See more details on using hashes here.

File details

Details for the file devolutions_crypto-0.8.0-cp39-cp39-manylinux_2_24_x86_64.whl.

File metadata

  • Download URL: devolutions_crypto-0.8.0-cp39-cp39-manylinux_2_24_x86_64.whl
  • Upload date:
  • Size: 1.1 MB
  • Tags: CPython 3.9, manylinux: glibc 2.24+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for devolutions_crypto-0.8.0-cp39-cp39-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 1681fea52579124fd81cb1767d7d316fa2240161c1a8f0f613042dfecda3ce9e
MD5 bbcf50c85cdceabd683274c32c38540b
BLAKE2b-256 d950028439df327d940d92cef82ddf3b6b428e5bfdf882849334d900d8e363d7

See more details on using hashes here.

File details

Details for the file devolutions_crypto-0.8.0-cp39-cp39-macosx_10_7_x86_64.whl.

File metadata

  • Download URL: devolutions_crypto-0.8.0-cp39-cp39-macosx_10_7_x86_64.whl
  • Upload date:
  • Size: 393.7 kB
  • Tags: CPython 3.9, macOS 10.7+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for devolutions_crypto-0.8.0-cp39-cp39-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 c7ddba03ec1e25344e3d7acd8693758ba0ec5abeaddace15686e16642da942bd
MD5 7f4142da5094c06ad94f7936c877a2d1
BLAKE2b-256 8593a7b575d95ba57f1641c3d4e70b797eec4ec3c956e80dac0ae830bfcd197d

See more details on using hashes here.

File details

Details for the file devolutions_crypto-0.8.0-cp38-none-win_amd64.whl.

File metadata

  • Download URL: devolutions_crypto-0.8.0-cp38-none-win_amd64.whl
  • Upload date:
  • Size: 277.1 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for devolutions_crypto-0.8.0-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 a3cd1b5a1a1ff784ccf9019824e42009359b6c0fa08cb9cdc64d5b8d5035fef0
MD5 19e59a0d0910d2ddeca8770865f9cf35
BLAKE2b-256 dc171abbc28ad1916f79f9a0d25cc67d4d0fea2e7f8c52bc7bdbf336e53d17aa

See more details on using hashes here.

File details

Details for the file devolutions_crypto-0.8.0-cp38-cp38-manylinux_2_24_x86_64.whl.

File metadata

  • Download URL: devolutions_crypto-0.8.0-cp38-cp38-manylinux_2_24_x86_64.whl
  • Upload date:
  • Size: 1.1 MB
  • Tags: CPython 3.8, manylinux: glibc 2.24+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for devolutions_crypto-0.8.0-cp38-cp38-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 92056a4bfee319494e1b13ecf7f608df12135f127067c4b18881fb5d37b6c368
MD5 909426319c3caf817de3483ca5f9f819
BLAKE2b-256 84db051c870a4206d95a101a4b6c35d49aaaf7b1e879bb51ac3df109de4bcedf

See more details on using hashes here.

File details

Details for the file devolutions_crypto-0.8.0-cp38-cp38-macosx_10_7_x86_64.whl.

File metadata

  • Download URL: devolutions_crypto-0.8.0-cp38-cp38-macosx_10_7_x86_64.whl
  • Upload date:
  • Size: 393.7 kB
  • Tags: CPython 3.8, macOS 10.7+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for devolutions_crypto-0.8.0-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 1fa052e74157eb0a893d84083dfee6688a263cfdaa760ed90a82ee1f4b0a6032
MD5 2122afa22f1d59a4b0eff2c0a097e540
BLAKE2b-256 f4af19cb211679998086d56e6cc7152340f6efd2e3d90bbec74eb72587a1f878

See more details on using hashes here.

File details

Details for the file devolutions_crypto-0.8.0-cp37-none-win_amd64.whl.

File metadata

  • Download URL: devolutions_crypto-0.8.0-cp37-none-win_amd64.whl
  • Upload date:
  • Size: 277.0 kB
  • Tags: CPython 3.7, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for devolutions_crypto-0.8.0-cp37-none-win_amd64.whl
Algorithm Hash digest
SHA256 950ab20aaf23f6c37f2522c16b83287f44154c76a0b3edd0a5b6e4c0ee192032
MD5 aa0e252b0ccec1aa617fd02fde0b2a0a
BLAKE2b-256 f59fb4826fbf1e0a857296556ad68d5594f0dc590c6359246b2dbe6cbd5e2b0a

See more details on using hashes here.

File details

Details for the file devolutions_crypto-0.8.0-cp37-cp37m-manylinux_2_24_x86_64.whl.

File metadata

  • Download URL: devolutions_crypto-0.8.0-cp37-cp37m-manylinux_2_24_x86_64.whl
  • Upload date:
  • Size: 1.1 MB
  • Tags: CPython 3.7m, manylinux: glibc 2.24+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for devolutions_crypto-0.8.0-cp37-cp37m-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 f451bf40f4d088e0023e2d042b7380a42c2d5b08b52f88db9071967375ca0eb7
MD5 84d47d2ee64847c6ee5e08bdc1b58187
BLAKE2b-256 916b477889f51b7489e509dcbc20401e1d9b4c827baea71963fee185a393f248

See more details on using hashes here.

File details

Details for the file devolutions_crypto-0.8.0-cp37-cp37m-macosx_10_7_x86_64.whl.

File metadata

  • Download URL: devolutions_crypto-0.8.0-cp37-cp37m-macosx_10_7_x86_64.whl
  • Upload date:
  • Size: 393.7 kB
  • Tags: CPython 3.7m, macOS 10.7+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for devolutions_crypto-0.8.0-cp37-cp37m-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 a13a7cc5374680715ef89d63d62a1c7f2aac0fbca27ed4d0e60727c1505213b7
MD5 768c3bebeb19c479fb0dc1ddb6f6eb6c
BLAKE2b-256 45f860cea2dda18e0f9ea6befb4cb9ca5dbb3f9a72ea5246e9494cc658705fdd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page