[][src]Struct spritz_cipher::SpritzCipherContext

pub struct SpritzCipherContext { /* fields omitted */ }

The primary structure that contains the buffer and varirables for the Cipher

Methods

impl SpritzCipherContext[src]

pub fn compare(data_a: &[u8], data_b: &[u8]) -> Result<u8, SpritzCipherError>[src]

Timing-safe equality comparison for data_a and data_b.

This function can be used to compare the password's hash safely.

  • Parameter data_a: Data a to be compare with b.
  • Parameter data_b: Data b to be compare with a.

Return: Equality result.

  • Zero (0x00) if data_a equals data_b,
  • Non-zero value if they are NOT equal.
  • Error if the array lengths don't match
  • Probably should be replaced by a bool (Thoughts?)

Examples

use spritz_cipher::SpritzCipherContext;
const BUFFER_SIZE: usize = 24;
let mut msg: [u8;BUFFER_SIZE] = ['A' as u8;BUFFER_SIZE];
let mut buf: [u8;BUFFER_SIZE] = [ 65 as u8;BUFFER_SIZE];

assert_eq!(SpritzCipherContext::compare(&buf, &msg).unwrap(), 0);
buf[0] =  buf[0].wrapping_add(1);
assert_ne!(SpritzCipherContext::compare(&buf, &msg).unwrap(),0);

pub fn setup(key: &[u8]) -> SpritzCipherContext[src]

Clear the spritz cipher context by placing 0 in all locations.

To be replaced by Zeroize once I get it working. Setup the context with a key.

  • Parameter key: The key.

  • Return: A Context setup and ready to use.

pub fn setup_with_IV(key: &[u8], nonce: &[u8]) -> SpritzCipherContext[src]

Setup the context with a key and nonce/salt/iv.

  • Parameter key: The key.

  • Parameter nonce: The nonce (salt).

  • Return: A Context setup and ready to use.

pub fn random8(&mut self) -> u8[src]

Generates a random byte from the spritz context.

Probably shouldn't use this unless you need too

pub fn random32(&mut self) -> u32[src]

Generates four random bytes from the spritz context.

Probably shouldn't use this unless you need too

pub fn random32_uniform(&mut self, upper_bound: u32) -> u32[src]

Uniformity is achieved by generating new random numbers until the one returned is outside the range [0, 2**32 % upper_bound).

This guarantees the selected random number will be inside [232 % upper_bound, 232) which maps back to [0, upper_bound) after reduction modulo upper_bound.

random32_uniform() derives from OpenBSD's arc4random_uniform()

  • Parameter upper_bound: The roof, upper_bound - 1 is the largest number that can be returned.

  • Return: Random number less than upper_bound, 0 if upper_bound<2.

Probably shouldn't use this unless you need too

pub fn add_entropy(&mut self, entropy: &[u8])[src]

Add entropy to the spritz context using absorb().

  • Parameter entropy: The entropy array.

pub fn crypt(
    &mut self,
    data: &[u8],
    data_out: &mut [u8]
) -> Result<(), SpritzCipherError>
[src]

Encrypt or decrypt data chunk by XOR-ing it with the spritz keystream.

  • Parameter data: The data to encrypt or decrypt.
  • Parameter data_out: The output.

Returns an error if the array lengths don't match

   use spritz_cipher::SpritzCipherContext;
 
   const BUFFER_SIZE: usize = 24;
   const KEY_SIZE: usize = 32;
 
    use rand::prelude::*;
     
 
    /* Data to input */
    let mut msg: [u8;BUFFER_SIZE] = ['A' as u8;BUFFER_SIZE];
    let mut key = [0u8;KEY_SIZE];
    thread_rng().fill(&mut key);
    thread_rng().fill(&mut msg);
 
    let mut buf = [0 as u8;BUFFER_SIZE]; /* Output buffer */
     
    //Encrypt
    let mut context = SpritzCipherContext::setup(&key);
    context.crypt(&msg, &mut buf);
 
    //Decrypt
    let mut context = SpritzCipherContext::setup(&key);
    let buf2 = buf.clone();
    context.crypt(&buf2, &mut buf);
 
    /* Check the output */
    assert_eq!(SpritzCipherContext::compare(&buf, &msg).unwrap(), 0);

pub fn hash_setup() -> SpritzCipherContext[src]

Setup the spritz hash context.

  • Return: A Context setup and ready to use.

pub fn hash_update(&mut self, data: &[u8])[src]

Add a message/data chunk data to hash.

  • Parameter data: The data chunk to hash.

pub fn hash_final(&mut self, digest: &mut [u8])[src]

Output the hash digest.

  • Parameter digest: The digest (hash) output.

pub fn hash(digest: &mut [u8], data: &[u8])[src]

  • Parameter digest: The digest (hash) output.
  • Parameter data: The data to hash.
use spritz_cipher::SpritzCipherContext;
 
   const BUFFER_SIZE: usize = 32;

   let test_vector: [u8; BUFFER_SIZE] =
   [ 0xff, 0x8c, 0xf2, 0x68, 0x09, 0x4c, 0x87, 0xb9,
   0x5f, 0x74, 0xce, 0x6f, 0xee, 0x9d, 0x30, 0x03,
   0xa5, 0xf9, 0xfe, 0x69, 0x44, 0x65, 0x3c, 0xd5,
   0x0e, 0x66, 0xbf, 0x18, 0x9c, 0x63, 0xf6, 0x99
   ];
   let test_data: [u8; 7] = [ 'a' as u8, 'r' as u8, 'c' as u8, 'f' as u8, 'o' as u8, 'u' as u8, 'r' as u8 ];

   let mut digest = [0u8;BUFFER_SIZE]; /* Output buffer */
   let mut digest_2 = [0u8;BUFFER_SIZE]; /* Output buffer for chunk by chunk API */
 
   let mut context = SpritzCipherContext::hash_setup();
   /* For easy test: code add a byte each time */
   for byte in test_data.iter() {
       context.hash_update(&[*byte]);
   }
   context.hash_final(&mut digest_2);
 
   //Short cut the above steps by doing it all in one hit
   SpritzCipherContext::hash(&mut digest, &test_data);
 
   /* Check the output */
   assert_eq!(SpritzCipherContext::compare(&digest, &test_vector).unwrap(), 0);
 
   assert_eq!(SpritzCipherContext::compare(&digest_2, &test_vector).unwrap(), 0);

pub fn mac_setup(key: &[u8]) -> SpritzCipherContext[src]

  • Parameter key: The secret key.

  • Return: A Context setup and ready to use.

pub fn mac_update(&mut self, msg: &[u8])[src]

  • Parameter msg: The message chunk to be authenticated.

pub fn mac_final(&mut self, digest: &mut [u8])[src]

Output the message authentication code (MAC) digest.

  • Parameter digest: Message authentication code (MAC) digest output.

pub fn mac(digest: &mut [u8], msg: &mut [u8], key: &mut [u8])[src]

Message Authentication Code (MAC) function.

  • Parameter digest: Message authentication code (MAC) digest output.
  • Parameter msg: The message to be authenticated.
  • Parameter key: The secret key.
    /* Data to input */
   let mut msg: [u8;3] = ['A' as u8, 'B' as u8, 'C' as u8];
   let mut key: [u8;3] = [0x00, 0x01, 0x02];
 
   const BUFFER_SIZE: usize = 32;
   let test_vector: [u8; BUFFER_SIZE] =
   [ 0xbe, 0x8e, 0xdc, 0xf2, 0x76, 0xcf, 0x57, 0xb4,
   0x0e, 0xbc, 0x8e, 0x22, 0x43, 0x45, 0x7e, 0x3e,
   0xb7, 0xc6, 0x4d, 0x4e, 0x99, 0x1e, 0x93, 0x58,
   0xce, 0x81, 0xef, 0xb1, 0x6c, 0xce, 0xc7, 0xed
   ];
  
   let mut digest = [0u8;BUFFER_SIZE]; /* Output buffer */
 
   use spritz_cipher::SpritzCipherContext;
   SpritzCipherContext::mac(&mut digest, &mut msg, &mut key);
 
   /* Check the output */
   assert_eq!(SpritzCipherContext::compare(&digest, &test_vector).unwrap(), 0);

Trait Implementations

impl Drop for SpritzCipherContext[src]

impl Zeroize for SpritzCipherContext[src]

Auto Trait Implementations

Blanket Implementations

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<Z> Zeroize for Z where
    Z: DefaultIsZeroes
[src]