dalek-cryptography / x25519-dalek

X25519 elliptic curve Diffie-Hellman key exchange in pure-Rust, using curve25519-dalek.
BSD 3-Clause "New" or "Revised" License
328 stars 133 forks source link

awkward api due to code duplication between EphemeralSecret and StaticSecret #56

Closed benma closed 4 years ago

benma commented 4 years ago

new(), diffie_hellman(), pubkey computation etc. are all the same. This leads to a bit of an awkward use of the API downstream, as e.g. noise-rust-crypto is basically picking one of the two types arbitrarily to perform diffie hellman, or to compute a pubkey.

It would be great to have a `SecretKey`` that implements all those things, and have the two special secret types just wrap it.

hdevalence commented 4 years ago

Hmm, an EphemeralSecret is just a StaticSecret where the compiler enforces at compile-time that the key is only used once. If you don't want to have two code paths, this means that you're OK with relaxing this compile-time verification. So it seems like an alternate solution would be to do

type SecretKey = x25519_dalek::StaticSecret;

and use that SecretKey type for both static and ephemeral keys. Would that work?

benma commented 4 years ago

where the compiler enforces at compile-time that the key is only used once.

I see, so maybe it's better for the downstream library to also do that.

Thanks for the clarification.

hdevalence commented 4 years ago

Maybe, maybe not! I think that for a known protocol like Noise that already handles the static/ephemeral distinction, it may not be helpful to have two code paths with two different types. If it's not, you can use the type alias as above, but if it is, you can have two code paths.