Skip to content

Commit

Permalink
updates readme and removes restriction on content size
Browse files Browse the repository at this point in the history
  • Loading branch information
seanwatters committed Feb 4, 2024
1 parent 69c5222 commit b4919b7
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 80 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ license = "AGPL-3.0-only"
name = "ordinal_crypto"
readme = "README.md"
repository = "https://github.com/ordinarylabs/ordinal-crypto"
version = "0.6.2"
version = "0.6.3"

[dependencies]
base64 = "0.21.2"
Expand All @@ -39,4 +39,4 @@ criterion = { version = "0.4", features = ["html_reports"] }

[[bench]]
name = "lib"
harness = false
harness = false
61 changes: 6 additions & 55 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ The cryptography library for the Ordinal Platform
let (priv_key, pub_key) = ordinal_crypto::generate_exchange_keys();
let (fingerprint, verifying_key) = ordinal_crypto::signature::generate_fingerprint();

// MAX content size: 74.840698 mb
let content = vec![0u8; 1214];
let content = vec![0u8; 1215];

// MAX pub keys: 65,535
let pub_keys = vec![pub_key];
Expand All @@ -38,62 +37,14 @@ let decrypted_content = ordinal_crypto::content::decrypt(
assert_eq!(decrypted_content, content);
```

## Limits
## Format

- MAX public keys -> 65,535
- MAX content size -> 74.840698 mb

### Reasoning

IPv6 minimum MTU 1,280 bytes

each UDP packet in our system needs:
- IPv6 headers (40 bytes)
- UDP headers (8 bytes)
- uuid (16 bytes)
- position (2 bytes)

our base, usable packet size, is 1,214 bytes (1,280 bytes - 40 bytes - 8 bytes - 16 bytes - 2 bytes)

*total possible in a “payload” with a 16 bit position counter is 79.55949 mb (1,214 bytes * 65,535)*

Encryption Components:
- inner signature = 64 bytes
- keys count header = 2 bytes
- encrypted keys = 72 bytes (max is 65,535 or 4.71852 mb)
- one-time public key = 32 bytes
- Poly1305 MAC = 16 bytes
- nonce = 24 bytes
- keys count header = 2 bytes
- **MAX public keys (encrypted keys = 72 bytes * 65,535) = 4.71852 mb**

*remaining "payload" space is 74.840832 mb*

Destination Components:
- location = 16 bytes
- PUT key = 16 bytes
- TTL
- unit = 1 byte
- duration = 1 byte

*remaining "payload" space is 74.840798 mb*

Server Authentication Components:
- payload signature = 64 bytes
- token
- HMAC = 32 bytes
- id = 16 bytes
- exp = 8 bytes
- verifying key = 32 bytes
- capacity units = 1 byte
- write ops per CU = 2 bytes
- write bytes per CU = 4 bytes
- TTL cost multiplier
- hour = 1 byte
- day = 1 byte
- month = 1 byte
- year = 1 byte
- infinite = 1 byte

**MAX content size is 74.840698 mb**
- inner signature = 64 bytes (encrypted along with the content to preserve deniability)
- Poly1305 MAC = 16 bytes

## Security

Expand Down
16 changes: 8 additions & 8 deletions benches/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn bytes_32_decode_benchmark(c: &mut Criterion) {

fn aead_encrypt_benchmark(c: &mut Criterion) {
let key = [0u8; 32];
let content = [0u8; 12_140];
let content = [0u8; 1215];

c.bench_function("aead_encrypt", |b| {
b.iter(|| {
Expand All @@ -62,7 +62,7 @@ fn aead_encrypt_benchmark(c: &mut Criterion) {

fn aead_decrypt_benchmark(c: &mut Criterion) {
let key = [0u8; 32];
let content = [0u8; 12_140];
let content = [0u8; 1215];

let encrypted_content = ordinal_crypto::aead::encrypt(&key, &content).unwrap();

Expand All @@ -83,7 +83,7 @@ fn signature_generate_fingerprint_benchmark(c: &mut Criterion) {

fn signature_sign_benchmark(c: &mut Criterion) {
let (fingerprint, _) = ordinal_crypto::signature::generate_fingerprint();
let content = [0u8; 12_140];
let content = [0u8; 1215];

c.bench_function("signature_sign", |b| {
b.iter(|| {
Expand All @@ -94,7 +94,7 @@ fn signature_sign_benchmark(c: &mut Criterion) {

fn signature_verify_benchmark(c: &mut Criterion) {
let (fingerprint, verifying_key) = ordinal_crypto::signature::generate_fingerprint();
let content = [0u8; 12_140];
let content = [0u8; 1215];

let signature = ordinal_crypto::signature::sign(&fingerprint, &content);

Expand All @@ -115,7 +115,7 @@ fn generate_exchange_keys_benchmark(c: &mut Criterion) {

fn content_encrypt_benchmark(c: &mut Criterion) {
let (fingerprint, _) = ordinal_crypto::signature::generate_fingerprint();
let content = vec![0u8; 12_140];
let content = vec![0u8; 1215];
let (_, pub_key) = ordinal_crypto::generate_exchange_keys();
let pub_keys = vec![pub_key];

Expand All @@ -128,7 +128,7 @@ fn content_encrypt_benchmark(c: &mut Criterion) {

fn content_encrypt_multi_recipient_benchmark(c: &mut Criterion) {
let (fingerprint, _) = ordinal_crypto::signature::generate_fingerprint();
let content = vec![0u8; 12_140];
let content = vec![0u8; 1215];
let mut pub_keys = vec![];

for _ in 0..5000 {
Expand All @@ -145,7 +145,7 @@ fn content_encrypt_multi_recipient_benchmark(c: &mut Criterion) {

fn content_extract_components_for_key_position_benchmark(c: &mut Criterion) {
let (fingerprint, _) = ordinal_crypto::signature::generate_fingerprint();
let content = vec![0u8; 12_140];
let content = vec![0u8; 1215];
let mut pub_keys = vec![];

for _ in 0..5000 {
Expand All @@ -166,7 +166,7 @@ fn content_extract_components_for_key_position_benchmark(c: &mut Criterion) {

fn decrypt_content_benchmark(c: &mut Criterion) {
let (fingerprint, verifying_key) = ordinal_crypto::signature::generate_fingerprint();
let content = vec![0u8; 12_140];
let content = vec![0u8; 1215];
let (priv_key, pub_key) = ordinal_crypto::generate_exchange_keys();

let pub_keys = vec![pub_key];
Expand Down
20 changes: 6 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub mod bytes_32 {
///
/// ```rust
/// let key = [0u8; 32];
/// let content = vec![0u8; 1214];
/// let content = vec![0u8; 1215];
///
/// let encrypted_content = ordinal_crypto::aead::encrypt(&key, &content).unwrap();
/// assert_eq!(encrypted_content.len(), content.len() + 40);
Expand Down Expand Up @@ -130,7 +130,7 @@ pub mod aead {
/// ```rust
/// let (fingerprint, verifying_key) = ordinal_crypto::signature::generate_fingerprint();
///
/// let content = vec![0u8; 1214];
/// let content = vec![0u8; 1215];
///
/// let signature = ordinal_crypto::signature::sign(&fingerprint, &content);
///
Expand Down Expand Up @@ -198,8 +198,7 @@ pub fn generate_exchange_keys() -> ([u8; 32], [u8; 32]) {
/// let (priv_key, pub_key) = ordinal_crypto::generate_exchange_keys();
/// let (fingerprint, verifying_key) = ordinal_crypto::signature::generate_fingerprint();
///
/// // MAX content size: 74.840698 mb
/// let content = vec![0u8; 1214];
/// let content = vec![0u8; 1215];
///
/// // MAX pub keys: 65,535
/// let pub_keys = vec![pub_key];
Expand Down Expand Up @@ -234,10 +233,6 @@ pub mod content {
return Err("cannot encrypt for more than 65,535 public keys");
}

if content.len() > 74_840_698 {
return Err("cannot encrypt content larger than 74.840698 mb");
}

// generate components
let nonce = chacha20poly1305::XChaCha20Poly1305::generate_nonce(&mut rand_core::OsRng);
let content_key = chacha20poly1305::XChaCha20Poly1305::generate_key(&mut rand_core::OsRng);
Expand Down Expand Up @@ -305,9 +300,9 @@ pub mod content {
}

pub fn extract_components_for_key_position(
encrypted_content: &[u8],
encrypted_content: &Vec<u8>,
position: u16,
) -> Result<(Vec<u8>, [u8; ENCRYPTED_KEY_LENGTH]), &'static str> {
) -> Result<(&[u8], [u8; ENCRYPTED_KEY_LENGTH]), &'static str> {
let key_header_bytes: [u8; 2] = match encrypted_content[0..2].try_into() {
Ok(b) => b,
Err(_) => return Err("failed to convert keys header to bytes"),
Expand All @@ -324,10 +319,7 @@ pub mod content {
Err(_) => return Err("failed to convert content key to bytes"),
};

Ok((
encrypted_content[keys_end..].to_vec(),
encrypted_content_key,
))
Ok((&encrypted_content[keys_end..], encrypted_content_key))
}

pub fn decrypt(
Expand Down

0 comments on commit b4919b7

Please sign in to comment.