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

How do I create a SecretVec which length is unknown? #104

Closed
sandevins opened this issue May 3, 2023 · 1 comment
Closed

How do I create a SecretVec which length is unknown? #104

sandevins opened this issue May 3, 2023 · 1 comment

Comments

@sandevins
Copy link

sandevins commented May 3, 2023

In this example I want to encrypt a text that is stored on a file (I tried first to have it provided by stdin but I couldn't make it work).

use std::fs::File;
use std::io::Read;

use libsodium_sys as sodium;
use secrets::{SecretBox, SecretVec};

const KEY_LEN   : usize = sodium::crypto_secretbox_KEYBYTES   as _;
const NONCE_LEN : usize = sodium::crypto_secretbox_NONCEBYTES as _;
const MAC_LEN   : usize = sodium::crypto_secretbox_MACBYTES   as _;

pub fn get_ciphertext(key_path: &str, nonce_path: &str, text_path: &str) -> std::io::Result<SecretVec::<u8>>{
    let mut key        = SecretBox::<[u8; KEY_LEN]>::zero();
    let mut nonce      = [0; NONCE_LEN];
    let mut plaintext = SecretVec::zero(14);
    

    File::open(key_path)?
        .read_exact(key.borrow_mut().as_mut())?;

    File::open(nonce_path)?
        .read_exact(&mut nonce)?;

    File::open(text_path)?
        .read_exact(plaintext.borrow_mut().as_mut())?;

    Ok(SecretVec::<u8>::new(plaintext.len() - MAC_LEN, |s| {
        if -1 == unsafe {
            sodium::crypto_secretbox_easy(
                s.as_mut_ptr(),
                plaintext.borrow().as_ptr(),
                plaintext.len() as _,
                nonce.as_ptr(),
                key.borrow().as_ptr(),
            )
        } {
            panic!("failed");
        }
    }))
}

This is like the decrypt example but using the sodium::crypto_secretbox_easy instead.

It gives the error thread 'main' panicked at 'attempt to subtract with overflow' probably because of the size given by plaintext.len() - MAC_LEN.

Have you any idea on how to fix this?

@sandevins
Copy link
Author

Nevermind, I found the error. It was a dumb problem due to copying.

The libsodium call should be like this.

Ok(SecretVec::<u8>::new(plaintext.len() + MAC_LEN, |s| {
        if -1 == unsafe {
            sodium::crypto_secretbox_easy(
                s.as_mut_ptr(),
                plaintext.borrow().as_ptr(),
                plaintext.len() as _,
                nonce.as_ptr(),
                key.borrow().as_ptr(),
            )
        } {
            panic!("failed");
        }
    }))

The MAC len has to be added to the length.

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

1 participant