Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Secure Cell passphrase API: RustThemis #630

Merged
merged 6 commits into from
May 4, 2020
Merged

Conversation

ilammy
Copy link
Collaborator

@ilammy ilammy commented Apr 27, 2020

Add support of Secure Cell passphrase API to RustThemis. The API is described in RFC 3.8.

This only adds new API with its documentation. Existing API docs and tests for symmetric key API need an update to avoid using passphrases there. Since this PR is already massive, those changes will be submitted separately.

User notes

Passphrase-based interface of Secure Cell allows you to use short and memorable passphrases to secure your data. While symmetric keys are more secure, they are also longer and much harder for humans to remember.

Here is how you can use passphrases with Secure Cell in Rust:

use themis::secure_cell::SecureCell;

let cell = SecureCell::with_passphrase("secret")?.seal();

let message = b"precious message";

let encrypted = cell.encrypt(&message)?;
let decrypted = cell.decrypt(&encrypted)?;

assert_eq!(decrypted, message);

Passphrase API accepts passphrases as relatively short strings, suitable for human memory. Master key API uses randomly generated, long binary keys, which are more suitable for machines to remember. However, they are also much more efficient and generally more secure due to considerable length. You should prefer to use keys over passphrases if there are no humans involved. The interface is almost the same:

use themis::secure_cell::SecureCell;
use themis::keys::SymmetricKey;

// Generate a new key if you don't have one:
let master_key = SymmetricKey::new();
// Or use an existing value that you store somewhere:
let master_key = base64::decode("b0gyNlM4LTFKRDI5anFIRGJ4SmQyLGE7MXN5YWUzR2U=")?;

let cell = SecureCell::with_key(&master_key)?.seal();

let message = b"precious message";

let encrypted = cell.encrypt(&message)?;
let decrypted = cell.decrypt(&encrypted)?;

assert_eq!(decrypted, message);

Technical notes

There are no significant deviations from RFC 3.8.

The code sample is updated to be similar to other languages where it's a showcase for Secure Cell API rather than a copy of integration test tool. Secure Cell is not really designed for generic file encryption so we'd rather not give a wrong idea.

Checklist

  • Change is covered by automated tests
  • Benchmark results are attached (only Core)
  • The coding guidelines are followed
  • Public API has proper documentation
  • Example projects and code samples are up-to-date
  • Changelog is updated

Introduce new constructor "SecureCell::with_passphrase" which accepts
textual passphrases and returns a Secure Cell that handles them.

Currently only Seal mode supports passphrases so that's the only mode
that users are allowed to select.

The passphrase is accepted as AsRef<[u8]> to transparently allow use of
raw byte slices and other types, not limiting to AsRef<str> which could
be more appropriate (and more restrictive).

This commit does not update existing master key API for correct use of
symmetric keys and does not update API documentation as well. This will
be done separately.
Add "scell_seal_string_echo_pw" tool for Rust. Note that we need to add
it as a new binary in test tool crate. Testing infrastructure will build
and use it automatically based on the source file name.
Update the code sample like in other languages. Instead of a simple file
encryption tool (which is not a generally good use case for Secure Cell)
provide a showcase of Secure Cell APIs and how they impact the output.
@ilammy ilammy added the W-RustThemis 🦀 Wrapper: Rust-Themis, Rust API, Cargo crates label Apr 27, 2020
@ilammy
Copy link
Collaborator Author

ilammy commented Apr 27, 2020

This PR attempts to beat the usual pattern “more tests than code” by probably having more API docs than tests or code 😂 At least, they took comparable amount of time to porting SwiftThemis tests to Rust.

tests/rust/secure_cell.rs Show resolved Hide resolved
@ilammy ilammy merged commit 44e4763 into cossacklabs:master May 4, 2020
@ilammy ilammy deleted the kdf/rust branch May 4, 2020 17:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
W-RustThemis 🦀 Wrapper: Rust-Themis, Rust API, Cargo crates
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants