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");

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.6.0.tar.gz (39.3 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.6.0-cp38-none-win_amd64.whl (246.2 kB view details)

Uploaded CPython 3.8Windows x86-64

devolutions_crypto-0.6.0-cp38-cp38-manylinux1_x86_64.whl (876.9 kB view details)

Uploaded CPython 3.8

devolutions_crypto-0.6.0-cp38-cp38-macosx_10_7_x86_64.whl (243.0 kB view details)

Uploaded CPython 3.8macOS 10.7+ x86-64

devolutions_crypto-0.6.0-cp37-none-win_amd64.whl (246.2 kB view details)

Uploaded CPython 3.7Windows x86-64

devolutions_crypto-0.6.0-cp37-cp37m-manylinux1_x86_64.whl (876.9 kB view details)

Uploaded CPython 3.7m

devolutions_crypto-0.6.0-cp37-cp37m-macosx_10_7_x86_64.whl (243.0 kB view details)

Uploaded CPython 3.7mmacOS 10.7+ x86-64

devolutions_crypto-0.6.0-cp36-none-win_amd64.whl (246.4 kB view details)

Uploaded CPython 3.6Windows x86-64

devolutions_crypto-0.6.0-cp36-cp36m-manylinux1_x86_64.whl (877.0 kB view details)

Uploaded CPython 3.6m

devolutions_crypto-0.6.0-cp36-cp36m-macosx_10_7_x86_64.whl (243.0 kB view details)

Uploaded CPython 3.6mmacOS 10.7+ x86-64

File details

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

File metadata

  • Download URL: devolutions_crypto-0.6.0.tar.gz
  • Upload date:
  • Size: 39.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.2

File hashes

Hashes for devolutions_crypto-0.6.0.tar.gz
Algorithm Hash digest
SHA256 be9b8502e716ebc77fb4657fd3e1bca11928caa519014c9453fd6a3d434a2a20
MD5 8c6cac3c8b7499df288ef6e4926e659b
BLAKE2b-256 d51a215cf338d735a379e7705e4a903149b682d2bc80d45395d635f717e26683

See more details on using hashes here.

File details

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

File metadata

  • Download URL: devolutions_crypto-0.6.0-cp38-none-win_amd64.whl
  • Upload date:
  • Size: 246.2 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.2

File hashes

Hashes for devolutions_crypto-0.6.0-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 b4ae50e9bb403ad9e2b3222f49486ac89b1e2ae31046c559dbecf2d5737647e6
MD5 fedf19b02b7463deaa6a51669e5f939d
BLAKE2b-256 b42ee36727f30447c4308142614f8c9ba84ec628603e5358dd3a78747a6b561b

See more details on using hashes here.

File details

Details for the file devolutions_crypto-0.6.0-cp38-cp38-manylinux1_x86_64.whl.

File metadata

  • Download URL: devolutions_crypto-0.6.0-cp38-cp38-manylinux1_x86_64.whl
  • Upload date:
  • Size: 876.9 kB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.2

File hashes

Hashes for devolutions_crypto-0.6.0-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 9b0164b3622ceceef943349c416a89a6b3767b865a462e0d782d3ff086766b70
MD5 48caa8be79acf99f0d7ef1d9064a7af6
BLAKE2b-256 932829825a5af8e4c43a9471e4ca8984368f860932e8ec8909ed6f5d9269e780

See more details on using hashes here.

File details

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

File metadata

  • Download URL: devolutions_crypto-0.6.0-cp38-cp38-macosx_10_7_x86_64.whl
  • Upload date:
  • Size: 243.0 kB
  • Tags: CPython 3.8, macOS 10.7+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.2

File hashes

Hashes for devolutions_crypto-0.6.0-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 aa56bc753a12e0c46410c2f7f5caaf9e5366f5140cf9981545327e8cd959cf78
MD5 d879af557f43aa3ea07a920d87082f00
BLAKE2b-256 4d984b7963ed04dbc8494bc722a2a6607054a4feb7135aab46ef6b1f6996af69

See more details on using hashes here.

File details

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

File metadata

  • Download URL: devolutions_crypto-0.6.0-cp37-none-win_amd64.whl
  • Upload date:
  • Size: 246.2 kB
  • Tags: CPython 3.7, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.2

File hashes

Hashes for devolutions_crypto-0.6.0-cp37-none-win_amd64.whl
Algorithm Hash digest
SHA256 4943e7e3daf1a4243638f063e4a4dd921f8e4c2979790713b28bf5600aabcdd4
MD5 a60a6233afc10f5688f32570fd94efb3
BLAKE2b-256 58eeccf8b06b1e0da53f940669fee4e61cb256bdc12922991b7f9902424fd3e4

See more details on using hashes here.

File details

Details for the file devolutions_crypto-0.6.0-cp37-cp37m-manylinux1_x86_64.whl.

File metadata

  • Download URL: devolutions_crypto-0.6.0-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 876.9 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.2

File hashes

Hashes for devolutions_crypto-0.6.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 8ff3033ed8c85bc539f4a6d330e8eaa38d12b3ab4db2ffc26fb3e17f7598a8d0
MD5 8e1c0cb0b1bc8a8bcbe2cade51d3ba4c
BLAKE2b-256 db93e84c4e4e366d19ed00f8312e5036b73c13f8feb0b6bbf9ea67a4f523e4ee

See more details on using hashes here.

File details

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

File metadata

  • Download URL: devolutions_crypto-0.6.0-cp37-cp37m-macosx_10_7_x86_64.whl
  • Upload date:
  • Size: 243.0 kB
  • Tags: CPython 3.7m, macOS 10.7+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.2

File hashes

Hashes for devolutions_crypto-0.6.0-cp37-cp37m-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 e9b18d3650ec105a7961276b10eebb7ff1254905452e83b74993a3c1057b5f66
MD5 fa8d4049a1b61e375ca009958a9ef2f7
BLAKE2b-256 33882d03f13b8614f75a41ac6777467840c40646146e70c9e8b4708d3ae947ef

See more details on using hashes here.

File details

Details for the file devolutions_crypto-0.6.0-cp36-none-win_amd64.whl.

File metadata

  • Download URL: devolutions_crypto-0.6.0-cp36-none-win_amd64.whl
  • Upload date:
  • Size: 246.4 kB
  • Tags: CPython 3.6, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.2

File hashes

Hashes for devolutions_crypto-0.6.0-cp36-none-win_amd64.whl
Algorithm Hash digest
SHA256 e2a5edb41e77f00f538878a885120711d24c1f123b73ce1a5a20d7130e8dfc16
MD5 6a23ce52a2dc289fa82d7a58dd74f794
BLAKE2b-256 2b0a1f3a161b24d076a7e6d36d2bea3944bdeafefed2bc2414626188135539ac

See more details on using hashes here.

File details

Details for the file devolutions_crypto-0.6.0-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

  • Download URL: devolutions_crypto-0.6.0-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 877.0 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.2

File hashes

Hashes for devolutions_crypto-0.6.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 6d47f4f2de7c76031f0dedeec33c09b3351814e644d4d6277a8a0d67fb0bf23b
MD5 e6303696959d0f19bcf624a40c44945a
BLAKE2b-256 fe34fef33b8ed9fa9d5ed06d791daf08efca893ea2c4449480a109cb13f481cd

See more details on using hashes here.

File details

Details for the file devolutions_crypto-0.6.0-cp36-cp36m-macosx_10_7_x86_64.whl.

File metadata

  • Download URL: devolutions_crypto-0.6.0-cp36-cp36m-macosx_10_7_x86_64.whl
  • Upload date:
  • Size: 243.0 kB
  • Tags: CPython 3.6m, macOS 10.7+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.2

File hashes

Hashes for devolutions_crypto-0.6.0-cp36-cp36m-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 68745e0367917e1e0767187d98a38c00c5ba5ec60e6e56ee29fd9edb85cdb53c
MD5 89ffdb573e4e88908bf2f03f11de85c7
BLAKE2b-256 24ae6b67d775431431ad9cb1523a153a2676ab20d815666fbf411d15074b5d0c

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