Skip to content

Commit bdbbc69

Browse files
authored
Merge pull request #173 from Dstack-TEE/sodiumbox
Implement sodiumbox
2 parents 59f66b9 + 7b47298 commit bdbbc69

File tree

10 files changed

+471
-70
lines changed

10 files changed

+471
-70
lines changed

Cargo.lock

Lines changed: 44 additions & 39 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ version = "0.5.0"
33
authors = ["Kevin Wang <[email protected]>", "Leechael <[email protected]>"]
44
edition = "2021"
55
license = "MIT"
6+
homepage = "https://github.com/Dstack-TEE/dstack"
7+
repository = "https://github.com/Dstack-TEE/dstack"
68

79
[workspace]
810
members = [
@@ -36,6 +38,7 @@ members = [
3638
"cert-client",
3739
"lspci",
3840
"sdk/rust",
41+
"sodiumbox",
3942
]
4043
resolver = "2"
4144

@@ -62,6 +65,7 @@ key-provider-client = { path = "key-provider-client" }
6265
dstack-types = { path = "dstack-types" }
6366
cert-client = { path = "cert-client" }
6467
lspci = { path = "lspci" }
68+
sodiumbox = { path = "sodiumbox" }
6569

6670
# Core dependencies
6771
anyhow = "1.0.97"
@@ -133,6 +137,10 @@ tokio-rustls = { version = "0.26.2", features = ["ring"] }
133137
x25519-dalek = { version = "2.0.1", features = ["static_secrets"] }
134138
sodiumoxide = "0.2.7"
135139
k256 = "0.13.4"
140+
# Additional RustCrypto dependencies for sealed box
141+
xsalsa20poly1305 = "0.9.0"
142+
salsa20 = "0.10"
143+
rand_core = "0.6.4"
136144

137145
# Certificate/DNS
138146
hickory-resolver = "0.24.4"

dstack-util/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ tdx-attest.workspace = true
3434
host-api = { workspace = true, features = ["client"] }
3535
cmd_lib.workspace = true
3636
toml.workspace = true
37-
key-provider-client.workspace = true
3837
dcap-qvl.workspace = true
3938
k256 = { workspace = true, features = ["ecdsa"] }
4039
dstack-types.workspace = true
@@ -44,6 +43,7 @@ cert-client.workspace = true
4443
x509-parser.workspace = true
4544
serde_yaml2.workspace = true
4645
bollard.workspace = true
46+
sodiumbox.workspace = true
4747

4848
[dev-dependencies]
4949
rand.workspace = true

dstack-util/src/host_api.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use host_api::{
55
client::{new_client, DefaultClient},
66
Notification,
77
};
8-
use key_provider_client::guest::{generate_keypair, open_sealed_box, PUBLICKEYBYTES};
98
use ra_tls::attestation::validate_tcb;
9+
use sodiumbox::{generate_keypair, open_sealed_box, PUBLICKEYBYTES};
1010
use tracing::warn;
1111

1212
pub(crate) struct KeyProvision {
@@ -67,7 +67,7 @@ impl HostApi {
6767
pub async fn get_sealing_key(&self) -> Result<KeyProvision> {
6868
let (pk, sk) = generate_keypair();
6969
let mut report_data = [0u8; 64];
70-
report_data[..PUBLICKEYBYTES].copy_from_slice(&pk.0);
70+
report_data[..PUBLICKEYBYTES].copy_from_slice(pk.as_bytes());
7171
let (_, quote) =
7272
tdx_attest::get_quote(&report_data, None).context("Failed to get quote")?;
7373

key-provider-client/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@ license.workspace = true
99
anyhow.workspace = true
1010
serde = { workspace = true, features = ["derive"] }
1111
serde_json.workspace = true
12-
sodiumoxide.workspace = true
1312
tokio = { workspace = true, features = ["net", "io-util"] }

key-provider-client/src/guest.rs

Lines changed: 0 additions & 26 deletions
This file was deleted.

key-provider-client/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
pub mod guest;
21
pub mod host;

sodiumbox/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "sodiumbox"
3+
version = "0.1.0"
4+
edition = "2021"
5+
description = "Pure Rust implementation of libsodium's sealed box encryption"
6+
license.workspace = true
7+
homepage.workspace = true
8+
repository.workspace = true
9+
10+
[dependencies]
11+
x25519-dalek.workspace = true
12+
xsalsa20poly1305.workspace = true
13+
salsa20.workspace = true
14+
rand_core.workspace = true
15+
blake2.workspace = true

sodiumbox/README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# SodiumBox
2+
3+
A pure Rust implementation of libsodium's sealed box encryption, compatible with libsodium/sodiumoxide but without any C dependencies.
4+
5+
## Overview
6+
7+
SodiumBox provides a standalone implementation of the sealed box functionality from libsodium (NaCl) using only pure Rust cryptographic libraries. It is designed to be a drop-in replacement for sodiumoxide's sealed box functionality.
8+
9+
The implementation uses modern, well-maintained Rust cryptographic libraries:
10+
11+
- `x25519-dalek` for Curve25519 key exchange
12+
- `xsalsa20poly1305` for authenticated encryption
13+
- `blake2` for key derivation
14+
- `salsa20` for the HSalsa20 function
15+
16+
## Features
17+
18+
- Generate X25519 keypairs for sealed box operations
19+
- Seal messages using a recipient's public key
20+
- Open sealed boxes created by libsodium/sodiumoxide
21+
- Pure Rust implementation with no C dependencies
22+
- Comprehensive test vectors based on libsodium's test suite
23+
24+
## Usage
25+
26+
```rust
27+
use sodiumbox::{generate_keypair, seal, open_sealed_box};
28+
29+
// Generate a new keypair
30+
let (public_key, secret_key) = generate_keypair();
31+
32+
// Create a message to encrypt
33+
let message = b"This is a secret message";
34+
35+
// Seal the message for the recipient
36+
let sealed_box = seal(message, &public_key);
37+
38+
// Open a sealed box
39+
let result = open_sealed_box(&sealed_box, &public_key, &secret_key);
40+
match result {
41+
Ok(plaintext) => println!("Decrypted message: {:?}", plaintext),
42+
Err(_) => println!("Failed to decrypt"),
43+
}
44+
```
45+
46+
## License
47+
48+
This crate is licensed under either of:
49+
50+
- Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
51+
- MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
52+
53+
at your option.

0 commit comments

Comments
 (0)