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 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

Using generate_keypair will generate a random 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);

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 Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

devolutions_crypto-0.9.0-cp311-none-win_amd64.whl (340.3 kB view details)

Uploaded CPython 3.11Windows x86-64

devolutions_crypto-0.9.0-cp311-cp311-manylinux_2_34_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.34+ x86-64

devolutions_crypto-0.9.0-cp311-cp311-macosx_10_7_x86_64.whl (446.6 kB view details)

Uploaded CPython 3.11macOS 10.7+ x86-64

devolutions_crypto-0.9.0-cp310-none-win_amd64.whl (340.2 kB view details)

Uploaded CPython 3.10Windows x86-64

devolutions_crypto-0.9.0-cp310-cp310-manylinux_2_34_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.34+ x86-64

devolutions_crypto-0.9.0-cp310-cp310-macosx_10_7_x86_64.whl (446.6 kB view details)

Uploaded CPython 3.10macOS 10.7+ x86-64

devolutions_crypto-0.9.0-cp39-none-win_amd64.whl (340.2 kB view details)

Uploaded CPython 3.9Windows x86-64

devolutions_crypto-0.9.0-cp39-cp39-manylinux_2_34_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.34+ x86-64

devolutions_crypto-0.9.0-cp39-cp39-macosx_10_7_x86_64.whl (446.8 kB view details)

Uploaded CPython 3.9macOS 10.7+ x86-64

devolutions_crypto-0.9.0-cp38-none-win_amd64.whl (340.2 kB view details)

Uploaded CPython 3.8Windows x86-64

devolutions_crypto-0.9.0-cp38-cp38-manylinux_2_34_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.34+ x86-64

devolutions_crypto-0.9.0-cp38-cp38-macosx_10_7_x86_64.whl (446.6 kB view details)

Uploaded CPython 3.8macOS 10.7+ x86-64

devolutions_crypto-0.9.0-cp37-none-win_amd64.whl (340.4 kB view details)

Uploaded CPython 3.7Windows x86-64

devolutions_crypto-0.9.0-cp37-cp37m-manylinux_2_34_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.34+ x86-64

devolutions_crypto-0.9.0-cp37-cp37m-macosx_10_7_x86_64.whl (446.5 kB view details)

Uploaded CPython 3.7mmacOS 10.7+ x86-64

File details

Details for the file devolutions_crypto-0.9.0-cp311-none-win_amd64.whl.

File metadata

File hashes

Hashes for devolutions_crypto-0.9.0-cp311-none-win_amd64.whl
Algorithm Hash digest
SHA256 f121f527d44d27840b372b1595fcf8cb3d87f3a989991615f638c4d7e2c73c06
MD5 c93032e97727392a387611688027f694
BLAKE2b-256 3ad56ab19f03af54c5af12de3abefc23854eafd1e30d6f640423e1c1a670045f

See more details on using hashes here.

File details

Details for the file devolutions_crypto-0.9.0-cp311-cp311-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for devolutions_crypto-0.9.0-cp311-cp311-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 013d19dd0d9915d8bf86e79d409851708dda6c448e81f51ede37130bbd6d238c
MD5 51668fe90506ce090ec745836f753aaf
BLAKE2b-256 f7ed96c6730d168e0c12bd3be8628f6a09ffef40f77f983cb359fea5bd76a83e

See more details on using hashes here.

File details

Details for the file devolutions_crypto-0.9.0-cp311-cp311-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for devolutions_crypto-0.9.0-cp311-cp311-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 7fb75cfb10378b7bbf6e2b6ce77768178495f035d753126ac213679b86bb1de8
MD5 e5df541120f06123d5c5cfbfbf04c3ab
BLAKE2b-256 6382fb4865033b65b2a65b3811db11fabaa5b59b5343851ab3e01f1328dfb38f

See more details on using hashes here.

File details

Details for the file devolutions_crypto-0.9.0-cp310-none-win_amd64.whl.

File metadata

File hashes

Hashes for devolutions_crypto-0.9.0-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 4b5c6578229d6ef9f03b00669b6702b503e5909963b59676a95dd95d06b81c95
MD5 669efb5835003a8af832f16eca76fba1
BLAKE2b-256 12167445d04d0bf7a7185fa4c97ae1ad25aefb488c5390b060524a5e206ed169

See more details on using hashes here.

File details

Details for the file devolutions_crypto-0.9.0-cp310-cp310-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for devolutions_crypto-0.9.0-cp310-cp310-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 34ade599144bdedf2e9baa86f1ce1875a54f7e492d8bc8396603502a4caa37ea
MD5 77d5ec78e131f19cc7f5094893c90750
BLAKE2b-256 94f142f4be1f5b8f3cfa6f065789ed1056efbea47e93afccde95fb38a97473a0

See more details on using hashes here.

File details

Details for the file devolutions_crypto-0.9.0-cp310-cp310-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for devolutions_crypto-0.9.0-cp310-cp310-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 f5e9cf4218198b526bfc2a2a854425785e27995ffd8d2644586df0b45cb390d2
MD5 2641f4a2456ee2c7f629fa0fe32cdad7
BLAKE2b-256 eaf2f63ad6b6013d3f8efafc5dbf4c7f035485e3a5155b47a3368e1eb6bb54e1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for devolutions_crypto-0.9.0-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 3f4672e478eab3daa4aa538d7b0c30faa36c3b537df0fd874a96ed35145c2b43
MD5 b20b0f194dcdb0aea6f017ec91922b66
BLAKE2b-256 0d5cbc1d31b81f59ed8a1e0ab6f38c3e38ba0cecfd640b1384cbd4f5f7471777

See more details on using hashes here.

File details

Details for the file devolutions_crypto-0.9.0-cp39-cp39-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for devolutions_crypto-0.9.0-cp39-cp39-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 901b6496a94f8b89d035a62575855ec5f367da979f6415c81ae42b39c19aeafb
MD5 676ed3f8b9088f3f99afa398e7f83235
BLAKE2b-256 06ad5cf65defe085bc2b281e0ba63f490505774ff4df21f07ecae82aa26cd270

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for devolutions_crypto-0.9.0-cp39-cp39-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 6e931269d39c9568eb287585818e4abece1821704bbc28724ddabc744f5dc25d
MD5 092ff9892233ce998c9cd00178597365
BLAKE2b-256 fe69e5c9e58bb12be89919270e4791358fa1f5db9ca8dc665e8fe1f42a48925f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for devolutions_crypto-0.9.0-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 70a0966c0a1a49a6f86d06c2e93f9e338c667b7a2cffe471cb6008c21dea4699
MD5 e5538c97c48359b4d96838b481872029
BLAKE2b-256 7e75d435d52fc04b5c1287375f81a5e9641b1aabf88d3ddd9ec2768c6f46829e

See more details on using hashes here.

File details

Details for the file devolutions_crypto-0.9.0-cp38-cp38-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for devolutions_crypto-0.9.0-cp38-cp38-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 4bbcc108b181fe9a6cb4a3b0fb8a9fba569c6b233d0ffb9d5e9d6d49bab60c4a
MD5 d03f9c3d29a4c68ccb8bad7dccfdda69
BLAKE2b-256 3942f69ac17a5a918172660e5979eb6a1e7f1c5b831895cceb838fa00ce029a3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for devolutions_crypto-0.9.0-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 a4c9b21254ea4ab97a9dd66e924896a39a555685fddbf74e57b85195554c14b4
MD5 a41acc71c276d42b3202117bee6fb474
BLAKE2b-256 a710809def3e65f70d495dbba46bc92d6d4102643bdf4ff80e0fe52ec2b70dbd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for devolutions_crypto-0.9.0-cp37-none-win_amd64.whl
Algorithm Hash digest
SHA256 efa1c795f83ee71192602c75571298f79bb9851cc00f556a049e6eefbcfcbf00
MD5 25a9f4b665ecf853d4af241d96c18366
BLAKE2b-256 6c036d647350edb38f6001aef6f5db04bca81eb4d66131bd874a2ee4093ba529

See more details on using hashes here.

File details

Details for the file devolutions_crypto-0.9.0-cp37-cp37m-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for devolutions_crypto-0.9.0-cp37-cp37m-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 a716cc59636cabf18963659bdddc109d187f41f58d62e8e22d4806d05a284d9d
MD5 26b75911b8039f996fd149260a756178
BLAKE2b-256 02cc66e76c28cabc24d407fa8c6c383a41014722c65146c959b4a040bc96a168

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for devolutions_crypto-0.9.0-cp37-cp37m-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 319534cadfe8c3f082184da220487333e747275a50091562bd8d5505f49d6baa
MD5 a4ba932e72650b5c36bffcdcb5fd8493
BLAKE2b-256 f948f65ac390863369a224a47c35962b3dd2bb725b5c267e3e375e0dec5734a6

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