How to change the return type of this function?

I will look at matasano cryptography problems using rust, with rust to implement AES. I have this function to do basic encryption in ECB mode (basically taken almost verbatim from the rust-crypto storage example ):

pub fn aes_enc_ecb_128(key: &[u8], data: &[u8]) 
                       -> Result<Vec<u8>, symmetriccipher::SymmetricCipherError> {
    let mut encryptor = aes::ecb_encryptor(
            aes::KeySize::KeySize128,
            key,
            blockmodes::NoPadding);
    let mut final_result = Vec::<u8>::new();
    let mut read_buffer = buffer::RefReadBuffer::new(data);
    let mut buffer = [0; 4096];
    let mut write_buffer = buffer::RefWriteBuffer::new(&mut buffer);

    loop {
        let result = encryptor.encrypt(&mut read_buffer,
                                       &mut write_buffer,
                                       true);

        final_result.extend(write_buffer
                            .take_read_buffer()
                            .take_remaining().iter().map(|&i| i));
        match result {
            Ok(BufferResult::BufferUnderflow) => break,
            Ok(_) => {},
            Err(e) => return Err(e)
        }
    }

    Ok(final_result)
}

The above version compiles without problems and works as expected. However, to fit the rest of my error handling scheme, I would like to change the return type to Result<Vec<u8>,&'static str>. This is the function with this change:

pub fn aes_enc_ecb_128(key: &[u8], data: &[u8]) 
                       -> Result<Vec<u8>, &'static str> {
    let mut encryptor = aes::ecb_encryptor(
            aes::KeySize::KeySize128,
            key,
            blockmodes::NoPadding);
    let mut final_result = Vec::<u8>::new();
    let mut read_buffer = buffer::RefReadBuffer::new(data);
    let mut buffer = [0; 4096];
    let mut write_buffer = buffer::RefWriteBuffer::new(&mut buffer);

    loop {
        let result = encryptor.encrypt(&mut read_buffer,
                                       &mut write_buffer,
                                       true);

        final_result.extend(write_buffer
                            .take_read_buffer()
                            .take_remaining().iter().map(|&i| i));
        match result {
            Ok(BufferResult::BufferUnderflow) => break,
            Ok(_) => {},
            Err(_) => return Err("Encryption failed")
        }
    }

    Ok(final_result)
}

When I try to compile this version, I get the following error (paths removed for clarity):

error: source trait is private
         let result = encryptor.encrypt(&mut read_buffer,
                                        &mut write_buffer,
                                        true);
error: source trait is private
let r = decryptor.decrypt(&mut read_buffer, &mut write_buffer, true);
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The only way I was able to change this type is to wrap the original function in a conversion function as follows:

pub fn converted_enc(key: &[u8], data: &[u8]) 
                       -> Result<Vec<u8>, &'static str> {
   match aes_enc_ecb_128(key,data) {
       Ok(v) => Ok(v),
       Err(_) => Err("Encryption failed")
   } 
}

, , API, ?

/:

rustc 1.2.0-nightly (0cc99f9cc 2015-05-17) (built 2015-05-18)
cargo 0.2.0-nightly (ac61996 2015-05-17) (built 2015-05-17)
+4
1

, .

use crypto::symmetriccipher::Encryptor; :

pub fn aes_enc_ecb_128(key: &[u8], data: &[u8]) 
                       -> Result<Vec<u8>, &'static str> {
    use crypto::symmetriccipher::Encryptor;
    let mut encryptor = aes::ecb_encryptor(
            aes::KeySize::KeySize128,
            key,
            blockmodes::NoPadding);
    let mut final_result = Vec::<u8>::new();
    let mut read_buffer = buffer::RefReadBuffer::new(data);
    let mut buffer = [0; 4096];
    let mut write_buffer = buffer::RefWriteBuffer::new(&mut buffer);

    loop {
        let result = encryptor.encrypt(&mut read_buffer,
                                       &mut write_buffer,
                                       true);

        final_result.extend(write_buffer
                            .take_read_buffer()
                            .take_remaining().iter().map(|&i| i));
        match result {
            Ok(BufferResult::BufferUnderflow) => break,
            Ok(_) => {},
            Err(_) => return Err("Encryption failed")
        }
    }

    Ok(final_result)
}
+2

All Articles