Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question regarding public key import. #78

Open
ghostidentity opened this issue Nov 1, 2024 · 2 comments
Open

Question regarding public key import. #78

ghostidentity opened this issue Nov 1, 2024 · 2 comments

Comments

@ghostidentity
Copy link

ghostidentity commented Nov 1, 2024

First of all, thanks for providing ML-KEM library for rust.
I'm checking documentation but I'm unable to find a resource on how to marshal encapsulation key (generated from keypair),.

What I want to achieve is that the c# application can initiate a key exchage and the server can return an encapsulation key to generate a sharedkey, in turn the server will consume the cipherText to create its own shared key.

I'm stuck on this challenge: in turn the server will consume the cipherText to create its own shared key.

   // assume the c# application already  receive the publickey and sharedkey, but the server has to consume the cipherText to generate its  own sharedkey
                let decoded_bytes = base64::engine::general_purpose::STANDARD
                    .decode(text.as_bytes())
                    .expect("Failed to decode Base64 private key");
                    const SIZE: usize = 128; 
                    let array: [u8; SIZE] = payload.as_ref().try_into().expect("Slice with incorrect length");
                    let encapsulation_key: EncapsulationKey<MlKem1024Params> = EncapsulationKey::<MlKem1024Params>::from_bytes(array);
                    
                    // Retrieve the encoded ciphertext
                    let encoded_ciphertext = encapsulation_key.as_bytes();
                    let shared_key =dk.decapsulate(&encoded_ciphertext).unwrap();
                    println!("Shared Key: : {:?}", shared_key);
             

                    let encrypted_response = Bytes::from(shared_key);
                let _ = client.publish(reply_to, encrypted_response).await;

Is there a plan to easily export public keys to external system, so its easy to generate shared key.

@ghostidentity
Copy link
Author

adding some errors:
{A4B7FFC2-937C-44F8-850F-7FF3FFB519A0}

@tarcieri
Copy link
Member

tarcieri commented Nov 2, 2024

@ghostidentity you don't ever appear to be calling the EncapsulationKey::encapsulate method, which would actually generate an encapsulated key message. Instead you're doing this:

// Retrieve the encoded ciphertext
let encoded_ciphertext = encapsulation_key.as_bytes();

...but that's the serialization of the encapsulation key itself. You still need to actually use that key to encrypt a message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants