Repo of no-std crates for my personal embedded projects

Sachy's crypto scheme lmao #13

open opened by sachy.dev targeting main from sachy-crypto
Labels

None yet.

assignee

None yet.

Participants 1
AT URI
at://did:plc:usjm3ynnir6y4inkcdovrfei/sh.tangled.repo.pull/3mhklndgukc22
+29 -8
Interdiff #2 #3
+1
Cargo.lock
··· 1552 1552 dependencies = [ 1553 1553 "chacha20poly1305", 1554 1554 "dhkem", 1555 + "elliptic-curve", 1555 1556 "k256", 1556 1557 "sha2 0.11.0-rc.5", 1557 1558 ]
Cargo.toml

This file has not been changed.

+1
sachy-crypto/Cargo.toml
··· 12 12 k256 = { version = "=0.14.0-rc.8", default-features = false, features = ["ecdh", "getrandom"] } 13 13 sha2 = { version = "=0.11.0-rc.5", default-features = false, features = [] } 14 14 dhkem = { version = "0.1.0-rc.0", features = ["getrandom", "k256"] } 15 + elliptic-curve = { version = "0.14.0-rc.28", default-features = false, features = ["ecdh"] }
+27 -8
sachy-crypto/src/lib.rs
··· 14 14 use dhkem::{ 15 15 Encapsulate, Kem, Secp256k1DecapsulationKey, Secp256k1EncapsulationKey, Secp256k1Kem, 16 16 TryDecapsulate, 17 - kem::{Ciphertext, SharedKey, TryKeyInit}, 17 + kem::{Ciphertext, SharedKey}, 18 18 }; 19 - use k256::{ecdh::SharedSecret, elliptic_curve::subtle::ConstantTimeEq}; 19 + use elliptic_curve::sec1::{FromSec1Point, ToSec1Point}; 20 + use k256::{Sec1Point, ecdh::SharedSecret, elliptic_curve::subtle::ConstantTimeEq}; 20 21 21 22 extern crate alloc; 22 23 ··· 33 34 34 35 pub struct ClientHandshake(Secp256k1DecapsulationKey); 35 36 37 + pub struct EncapsulatedPublicKey(Secp256k1EncapsulationKey); 38 + 39 + impl EncapsulatedPublicKey { 40 + pub fn serialize(&self) -> Sec1Point { 41 + self.0.to_sec1_point(true) 42 + } 43 + 44 + pub fn deserialize(buf: &[u8]) -> Result<Self, HandshakeError> { 45 + Ok(Self( 46 + Secp256k1EncapsulationKey::from_sec1_bytes(buf).map_err(|_| HandshakeError)?, 47 + )) 48 + } 49 + 50 + pub fn encapsulate(&self) -> (Ciphertext<Secp256k1Kem>, SharedKey<Secp256k1Kem>) { 51 + self.0.encapsulate() 52 + } 53 + } 54 + 36 55 impl ClientHandshake { 37 - pub fn send() -> (Secp256k1EncapsulationKey, Self) { 56 + pub fn send() -> (EncapsulatedPublicKey, Self) { 38 57 let (decap, encap) = Secp256k1Kem::generate_keypair(); 39 58 40 - (encap, Self(decap)) 59 + (EncapsulatedPublicKey(encap), Self(decap)) 41 60 } 42 61 43 62 pub fn finish( ··· 58 77 59 78 impl ServerHandshake { 60 79 pub fn receive(buf: &[u8]) -> Result<(Ciphertext<Secp256k1Kem>, Self), HandshakeError> { 61 - let encap = Secp256k1EncapsulationKey::new_from_slice(buf).map_err(|_| HandshakeError)?; 80 + let encap = EncapsulatedPublicKey::deserialize(buf)?; 62 81 63 82 let (ciphertext, sk) = encap.encapsulate(); 64 83 ··· 244 263 245 264 #[cfg(test)] 246 265 mod tests { 247 - use dhkem::kem::KeyExport; 248 - 249 266 use super::*; 250 267 251 268 #[test] ··· 257 274 258 275 let (ek, client) = ClientHandshake::send(); 259 276 260 - let (ciphertext, server) = ServerHandshake::receive(&ek.to_bytes())?; 277 + // Pretend to send ek across the webz: client -> server 278 + let (ciphertext, server) = ServerHandshake::receive(ek.serialize().as_bytes())?; 261 279 280 + // Pretend to send ciphertext across the webz: server -> client 262 281 let client_transport = client.finish(&ciphertext, &psk)?; 263 282 let server_transport = server.finish(&psk)?; 264 283

History

8 rounds 0 comments
sign up or login to add to the discussion
1 commit
expand
Sachy's crypto scheme lmao
2/2 success
expand
no conflicts, ready to merge
expand 0 comments
1 commit
expand
Sachy's crypto scheme lmao
2/2 failed
expand
expand 0 comments
1 commit
expand
Sachy's crypto scheme lmao
2/2 success
expand
expand 0 comments
1 commit
expand
Sachy's crypto scheme lmao
2/2 success
expand
expand 0 comments
sachy.dev submitted #3
1 commit
expand
Sachy's crypto scheme lmao
2/2 success
expand
expand 0 comments
1 commit
expand
Sachy's crypto scheme lmao
2/2 success
expand
expand 0 comments
1 commit
expand
Sachy's crypto scheme lmao
2/2 success
expand
expand 0 comments
sachy.dev submitted #0
1 commit
expand
Sachy's crypto scheme lmao
2/2 success
expand
expand 0 comments