dalek-cryptography / ed25519-dalek

Fast and efficient ed25519 signing and verification in Rust.
BSD 3-Clause "New" or "Revised" License
677 stars 222 forks source link

SigningKey cannot be deserialized from CBOR #315

Open Calmarius opened 8 months ago

Calmarius commented 8 months ago

The following code panics:

use ed25519_dalek::SigningKey;
use ciborium;
use rand;

fn main() {
    let mut csprng = rand::rngs::OsRng;

    let key = ed25519_dalek::SigningKey::generate(&mut csprng);

    // JSON serde works
    let json_str = serde_json::to_string(&key).unwrap();
    println!("Serialized JSON: {:?}", json_str);
    let _: SigningKey = serde_json::from_str(json_str.as_str()).unwrap();

    // CBOR serde doesn't work
    let mut serialized = Vec::<u8>::new();
    ciborium::into_writer(&key, &mut serialized).unwrap();
    println!("serialized: {:x?}", serialized);
    let _: SigningKey = ciborium::from_reader(serialized.as_slice()).unwrap(); // panics

    println!("Hello, world!");
}

Dependencies:

ciborium = "0.2.1"
ed25519-dalek = { version = "2.1.0", features = ["serde", "rand_core"] }
rand = "0.8.5"
serde = { version = "1.0.193", features = ["derive"] }
serde_json = "1.0.108"

Running results in:

Serialized JSON: "[62,94,122,7,201,143,23,39,141,27,130,149,78,220,112,4,31,90,202,131,52,70,82,144,223,151,232,138,27,78,87,132]"
serialized: [58, 20, 3e, 5e, 7a, 7, c9, 8f, 17, 27, 8d, 1b, 82, 95, 4e, dc, 70, 4, 1f, 5a, ca, 83, 34, 46, 52, 90, df, 97, e8, 8a, 1b, 4e, 57, 84]
thread 'main' panicked at src/main.rs:20:70:
called `Result::unwrap()` on an `Err` value: Semantic(None, "invalid type: byte array, expected An ed25519 signing (private) key")
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

The problem is that ciborium calls visit_bytes on the visitor but only visit_borrowed_bytes is implemented for the SingingKey class.

I haven't checked other structs, those may also be affected.

robjtede commented 8 months ago

This repo has moved to https://github.com/dalek-cryptography/curve25519-dalek. You'll want to re-file this over there.

marley1989 commented 3 months ago

Let them go