win-crypto-ng
Safe Rust bindings to Microsoft Windows Cryptography API : Next Generation (CNG)
CNG are cryptographic primitives and utilities provided by the operating system and/or hardware. It is available since Windows Vista and replaces the now deprecated CryptoAPI.
The primitives do not depend on OpenSSL or other libraries of the sort, they are provided by Microsoft and/or by the hardware manufacturer. They are the primitives used in kernel space programs. Therefore, if you are using Microsoft Windows, you already accepted to trust these primitives.
CNG Features
- Validated by FIPS 140-2 and part of the Target of Evaluation for the Windows Common Criteria certification
- Full support for NSA Suite B algorithms
- Kernel support (not through the Rust bindings)
- Auditing in the key storage provider (KSP)
- Thread safe
Supported features in Rust
- Asymmetric encryption (RSA)
- Digital signatures
- Supported algorithms: RSA, DSA, ECDSA.
- Key exchange
- Supported algorithms: DH, ECDH.
- Symmetric encryption
- Supported algorithms: AES, DES, DES-X, RC2, 3DES, 3DES-112.
- Supported chaining modes: ECB, CBC, CFB.
- Hash functions
- Supported algorithms: SHA-1, SHA-256, SHA-384, SHA-512, MD2, MD4, MD5.
- Cryptographically secure random number generation
More to come
Cargo features
zeroize
- Useszeroize
crate to zero intermediate buffers on destructionrand
- Implementsrand
crate traits for the CNG-provided CSPRNG (cryptographically secure pseudorandom number generator)block-cipher
- Implementsblock-cipher
traits for CNG block ciphers.
By default, only the zeroize
feature is enabled.
Examples
Asymmetric encryption (RSA)
use ;
let key = builder.key_bits.build.unwrap;
let plaintext = b"This is an important message.";
let padding = Some;
let ciphertext = key.encrypt.unwrap;
assert_eq!;
let decoded = key.decrypt.unwrap;
assert_eq!;
Digital signatures
use ;
use ;
use HashAlgorithmId;
let key = builder.key_bits.build.unwrap;
let data: = .collect;
let padding = pkcs1;
let signature = key.sign.expect;
key.verify.expect;
key.verify.expect_err;
key.verify.expect_err;
Symmetric encryption
use ;
const KEY: &'static str = "0123456789ABCDEF";
const IV: &'static str = "asdfqwerasdfqwer";
const DATA: &'static str = "This is a test.";
let iv = IV.as_bytes.to_vec;
let algo = open.unwrap;
let key = algo.new_key.unwrap;
let ciphertext = key.encrypt.unwrap;
let plaintext = key.decrypt.unwrap;
assert_eq!;
Hash functions
use ;
const DATA: &'static str = "This is a test.";
let algo = open.unwrap;
let mut hash = algo.new_hash.unwrap;
hash.hash.unwrap;
let result = hash.finish.unwrap;
assert_eq!;
Cryptographically secure random number generator
use ;
let mut buffer = ;
let rng = system_preferred;
rng.gen_random.unwrap;
assert_ne!;
License
Licensed under the 3-Clause BSD License. See LICENSE.md for more details.
Copyright (c) 2019-2020 Émile Grégoire. All rights reserved.