win_crypto_ng::symmetric

Struct SymmetricAlgorithmKey

Source
pub struct SymmetricAlgorithmKey { /* private fields */ }
Expand description

Symmetric algorithm key

Implementations§

Source§

impl SymmetricAlgorithmKey

Source

pub fn key_size(&self) -> Result<usize>

Returns the key value size in bits.

§Examples
let key = algo.new_key("0123456789ABCDEF".as_bytes()).unwrap();
let key_size = key.key_size().unwrap();

assert_eq!(128, key_size);
Source

pub fn block_size(&self) -> Result<usize>

Returns the block size in bytes.

This can be useful to find what size the IV should be.

§Examples
let key = algo.new_key("0123456789ABCDEF".as_bytes()).unwrap();
let key_size = key.block_size().unwrap();

assert_eq!(16, key_size);
Source

pub fn set_msg_block_len(&mut self, len: usize) -> Result<()>

Sets the message block length.

This can be set on any key handle that has the CFB chaining mode set. By default, it is set to 1 for 8-bit CFB. Setting it to the block size in bytes causes full-block CFB to be used. For XTS keys it is used to set the size, in bytes, of the XTS Data Unit (commonly 512 or 4096).

See here for more info.

Source

pub fn encrypt( &self, iv: Option<&mut [u8]>, data: &[u8], padding: Option<Padding>, ) -> Result<Buffer>

Encrypts data using the symmetric key

The IV is not needed for ChainingMode::Ecb, so None should be used in this case.

For chaining modes needing an IV, None can be used and a default IV will be used. This is not documented on Microsoft’s website and is therefore not recommended.

The data is padded with zeroes to a multiple of the block size of the cipher. If the data length equals the block size of the cipher, one additional block of padding is appended to the data.

§Examples
let key = algo.new_key("0123456789ABCDEF".as_bytes()).unwrap();
let mut iv = b"_THIS_IS_THE_IV_".to_vec();
let plaintext = "THIS_IS_THE_DATA".as_bytes();
let ciphertext = key.encrypt(Some(&mut iv), plaintext, Some(Padding::Block)).unwrap();

assert_eq!(ciphertext.as_slice(), [
    0xE4, 0xD9, 0x90, 0x64, 0xA6, 0xA6, 0x5F, 0x7E,
    0x70, 0xDB, 0xF9, 0xDD, 0xE7, 0x0D, 0x6F, 0x6A,
    0x0C, 0xEC, 0xDB, 0xAD, 0x01, 0xB4, 0xB1, 0xDE,
    0xB4, 0x4A, 0xB8, 0xA0, 0xEA, 0x0E, 0x8F, 0x31]);
Source

pub fn decrypt( &self, iv: Option<&mut [u8]>, data: &[u8], padding: Option<Padding>, ) -> Result<Buffer>

Decrypts data using the symmetric key

The IV is not needed for ChainingMode::Ecb, so None should be used in this case.

For chaining modes needing an IV, None can be used and a default IV will be used. This is not documented on Microsoft’s website and is therefore not recommended.

§Examples
let key = algo.new_key("0123456789ABCDEF".as_bytes()).unwrap();
let mut iv = b"_THIS_IS_THE_IV_".to_vec();
let ciphertext = [
    0xE4, 0xD9, 0x90, 0x64, 0xA6, 0xA6, 0x5F, 0x7E,
    0x70, 0xDB, 0xF9, 0xDD, 0xE7, 0x0D, 0x6F, 0x6A,
    0x0C, 0xEC, 0xDB, 0xAD, 0x01, 0xB4, 0xB1, 0xDE,
    0xB4, 0x4A, 0xB8, 0xA0, 0xEA, 0x0E, 0x8F, 0x31
];
let plaintext = key.decrypt(Some(&mut iv), &ciphertext, Some(Padding::Block)).unwrap();

assert_eq!(&plaintext.as_slice()[..16], "THIS_IS_THE_DATA".as_bytes());

Trait Implementations§

Source§

impl<A: Algorithm, B: KeyBits> AsRef<SymmetricAlgorithmKey> for Key<A, B>

Source§

fn as_ref(&self) -> &SymmetricAlgorithmKey

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Debug for SymmetricAlgorithmKey

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<A: Algorithm, B: KeyBits> TryFrom<SymmetricAlgorithmKey> for Key<A, B>

Source§

type Error = SymmetricAlgorithmKey

The type returned in the event of a conversion error.
Source§

fn try_from(value: SymmetricAlgorithmKey) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.