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

Supported features in Rust

More to come

Examples

Symmetric encryption

```rust use wincryptong::symmetric::{ChainingMode, SymmetricAlgorithm, SymmetricAlgorithmId};

const KEY: &'static str = "0123456789ABCDEF"; const IV: &'static str = "asdfqwerasdfqwer"; const DATA: &'static str = "This is a test.";

let algo = SymmetricAlgorithm::open(SymmetricAlgorithmId::Aes, ChainingMode::Cbc).unwrap(); let key = algo.newkey(KEY.asbytes()).unwrap(); let ciphertext = key.encrypt(Some(IV.asbytes()), DATA.asbytes()).unwrap(); let plaintext = key.decrypt(Some(IV.asbytes()), ciphertext.asslice()).unwrap();

asserteq!(std::str::fromutf8(&plaintext.as_slice()[..DATA.len()]).unwrap(), DATA); ```

Hash functions

```rust use wincryptong::hash::{HashAlgorithm, HashAlgorithmId};

const DATA: &'static str = "This is a test.";

let algo = HashAlgorithm::open(HashAlgorithmId::Sha256).unwrap(); let mut hash = algo.newhash().unwrap(); hash.hash(DATA.asbytes()).unwrap(); let result = hash.finish().unwrap();

asserteq!(result.asslice(), &[ 0xA8, 0xA2, 0xF6, 0xEB, 0xE2, 0x86, 0x69, 0x7C, 0x52, 0x7E, 0xB3, 0x5A, 0x58, 0xB5, 0x53, 0x95, 0x32, 0xE9, 0xB3, 0xAE, 0x3B, 0x64, 0xD4, 0xEB, 0x0A, 0x46, 0xFB, 0x65, 0x7B, 0x41, 0x56, 0x2C, ]); ```

License

Licensed under the 3-Clause BSD License. See LICENSE.md for more details.

Copyright (c) 2019 Émile Grégoire. All rights reserved.