GithubHelp home page GithubHelp logo

xlatbptr / rust-salsa20 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ivan-tymoshenko/rust-salsa20

0.0 0.0 0.0 160 KB

Fast implementation of salsa20 in safe rust

License: MIT License

Rust 100.00%

rust-salsa20's Introduction

TravisCI Crates.io Docs

Salsa20 stream cipher

Salsa20 is a stream cipher built on a pseudo-random function based on add-rotate-xor operations โ€” 32-bit addition, bitwise addition and rotation operations.

Optimization of doubleround calculations

The doubleround function modifies the initial matrix 10 times to encrypt every 64-byte block. For each subsequent 64-byte block in the matrix, only 8 bytes associated with the counter are changed. Immutable bytes can be used to pre-compute the result of the first doubleround call. A preliminary calculation of the original matrix optimizes the first call to the doubleround function, reducing the number of calls to the quarterround function from 8 to 4 with a counter of less than 2 ^ 32 and from 8 to 6 with a counter of more than 2 ^ 32, which reduces the algorithmic complexity of the Salsa20 function by 5 and 2.5 percent respectively.

Usage

To install rust-salsa20, add the following to your Cargo.toml:

[dependencies]
rust-salsa20 = "^0.3"

Examples

Generate

extern crate rust_salsa20;
use rust_salsa20::{Salsa20, Key::Key32};

fn main() {
    let key = Key32([
        0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
        17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
    ]);
    let nonce = [1, 2, 3, 4, 5, 6, 7, 8];
    let mut salsa = Salsa20::new(key, nonce, 0);
    let mut buffer = [0; 10];
    salsa.generate(&mut buffer);

    assert_eq!(buffer, [45, 134, 38, 166, 142, 36, 28, 146, 116, 157]);
}

Encrypt

extern crate rust_salsa20;
use rust_salsa20::{Salsa20, Key::Key32};

fn main() {
    let key = Key32([
        0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
        17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
    ]);
    let nonce = [1, 2, 3, 4, 5, 6, 7, 8];
    let mut salsa = Salsa20::new(key, nonce, 0);
    let mut buffer = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
    salsa.encrypt(&mut buffer);

    assert_eq!(buffer, [44, 132, 37, 162, 139, 34, 27, 154, 125, 157]);
}

Contributors

See github for full contributors list

rust-salsa20's People

Contributors

ivan-tymoshenko avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.