#cipher #chacha #chacha20 #xchacha20 #crypto

no-std c2-chacha

ChaCha系列流密码

10个版本

0.3.3 2021年10月24日
0.3.1 2021年5月12日
0.3.0 2020年11月2日
0.2.4 2020年5月16日
0.1.0 2019年2月3日

#211 in 密码学

Download history 30746/week @ 2024-03-14 38921/week @ 2024-03-21 38350/week @ 2024-03-28 43977/week @ 2024-04-04 39791/week @ 2024-04-11 36271/week @ 2024-04-18 30259/week @ 2024-04-25 29234/week @ 2024-05-02 29374/week @ 2024-05-09 29248/week @ 2024-05-16 41311/week @ 2024-05-23 37870/week @ 2024-05-30 28523/week @ 2024-06-06 32329/week @ 2024-06-13 33855/week @ 2024-06-20 24188/week @ 2024-06-27

124,956 每月下载量
用于 156 个crate (6 直接)

MIT/Apache

145KB
4K SLoC

ChaCha系列流密码

特性

  • 纯Rust实现
  • 支持RustCrypto API
  • 基于稳定版Rust构建
  • 可移植
  • 速度快:在我的机器上(Xeon X5650,使用ppv-lite86),性能比手动优化的ASM SIMD实现(floodberry/chacha-opt)快15%以内
  • 兼容no-std(仅运行时算法选择需要std)

支持的变体

ChaCha20:用于TLS中的chacha20-poly1305,OpenSSH中的arc4random,BSDs中的arc4random,Linux /dev/urandom自4.8以来。

Ietf:IETF RFC 7539。更长的nonce,较短的块计数器。

XChaCha20:类似于XSalsa20构建;初始化过程中的混合步骤允许使用长nonce和完整的块计数器。

ChaCha12,ChaCha8:更快;安全性更低的保险系数。


lib.rs:

具有SIMD优化的纯Rust ChaCha

流密码使用

#[cfg(features = "std")]
fn demo() {
extern crate c2_chacha;

use c2_chacha::stream_cipher::{NewStreamCipher, SyncStreamCipher, SyncStreamCipherSeek};
use c2_chacha::{ChaCha20, ChaCha12};

let key = b"very secret key-the most secret.";
let iv = b"my nonce";
let plaintext = b"The quick brown fox jumps over the lazy dog.";

let mut buffer = plaintext.to_vec();
// create cipher instance
let mut cipher = ChaCha20::new_var(key, iv).unwrap();
// apply keystream (encrypt)
cipher.apply_keystream(&mut buffer);
// and decrypt it back
cipher.seek(0);
cipher.apply_keystream(&mut buffer);
// stream ciphers can be used with streaming messages
let mut cipher = ChaCha12::new_var(key, iv).unwrap();
for chunk in buffer.chunks_mut(3) {
    cipher.apply_keystream(chunk);
}
}

依赖项