Cocoon
Cocoon
is a protected container to wrap sensitive data with strong
encryption and format validation. A format of Cocoon
is developed
for the following practical cases:
- As a file format to organize simple secure storage:
- Key store.
- Password store.
- Sensitive data store.
- For encrypted data transfer:
- As a secure in-memory container.
Cocoon
is developed with security in mind. It aims to do the only one thing and do it
flawlessly. It has a minimal set of dependencies and a minimalist design to simplify control over
security aspects. It's a pure Rust implementation, and all dependencies are pure Rust
packages with disabled default features.
Problem
Whenever you need secure storage you reinvent the wheel: you have to take care of
how to encrypt data properly, how to store and transmit randomly generated
buffers, then to get data back, parse, and decrypt securely. Instead, you can use Cocoon
.
Basic Usage
📌 Wrap/Unwrap
One party wraps private data into a container using Cocoon::wrap
.
Another party (or the same one, or whoever knows the password) unwraps a private data
out of the container using Cocoon::unwrap
.
let cocoon = new;
let wrapped = cocoon.wrap?;
assert_ne!;
let unwrapped = cocoon.unwrap?;
assert_eq!;
📌 Dump/Parse
You can store data to file. Put data into Vec
container, the data is going to be
encrypted in place and stored in a file using the "cocoon" format.
let mut data = b"my secret data".to_vec;
let cocoon = new;
cocoon.dump?;
let data = cocoon.parse?;
assert_eq!;
📌 Encrypt/Decrypt
You can encrypt data in place and avoid re-allocations. The method operates with a detached
meta-data (a container format prefix) in the array on the stack. It is suitable for "no_std
"
build and whenever you want to evade re-allocations of a huge amount of data. You have to care
about how to store and transfer a data length and a container prefix though.
let mut data = "my secret data".to_owned.into_bytes;
let cocoon = from_crypto_rng;
let detached_prefix = cocoon.encrypt?;
assert_ne!;
cocoon.decrypt?;
assert_eq!;
Study Case
You implement a database of secrets that must be stored in an encrypted file using a user
password. There are a lot of ways how your database can be represented in memory and how
it could be serialized. You handle these aspects on your own, e.g. you can use
HashMap
to manage data and use borsh
, or bincode
,
to serialize the data. You can even compress a serialized buffer before encryption.
In the end, you use Cocoon
to put the final image into an encrypted container.
use BorshSerialize;
use ;
use HashMap;
use File;
// Your data can be represented in any way.
Cryptography
256-bit cryptography is chosen as a Cocoon
baseline.
Cipher (AEAD) | Key Derivation Function (KDF) |
---|---|
Chacha20-Poly1305 | PBKDF2-SHA256: 100000 iterations |
AES256-GCM |
- Key: 256-bit.
- Salt for KDF: random 128-bit + predefined part.
- Nonce for encryption: random 96-bit.
Key derivation parameters comply with NIST SP 800-132 recommendations (salt, iterations), and cipher parameters (key, nonce, length) fit requirements of a particular cipher. AEAD is chosen in order to authenticate encrypted data together with an unencrypted header.
Zeroization
Encryption key is wrapped into zeroizing container
(provided by zeroize
crate), which means that the key is erased automatically once it is dropped.
How It Works
See more implementation details on , e.g.
- the process of container creation,
- customizable crate features,
- and of course API.