gost_modes/
lib.rs

1//! This crate contains generic implementation of [block cipher modes of
2//! operation][1] defined in [GOST R 34.13-2015].
3//!
4//! CTR, CFB and OFB modes are implemented in terms of traits from the [`cipher`] crate.
5//!
6//! MAC function defined in the GOST is implemented in the [`cmac`] crate.
7//!
8//! # Examples
9//! ```
10//! use gost_modes::{GostCbc, GostPadding, BlockMode, consts::U2};
11//! use kuznyechik::Kuznyechik;
12//! use hex_literal::hex;
13//!
14//! let key = hex!("
15//!     8899aabbccddeeff0011223344556677
16//!     fedcba98765432100123456789abcdef
17//! ");
18//! let iv = hex!("
19//!     1234567890abcef0a1b2c3d4e5f00112
20//!     23344556677889901213141516171819
21//! ");
22//! let pt = b"my secret message";
23//!
24//! type Cipher = GostCbc<Kuznyechik, GostPadding, U2>;
25//!
26//! let cipher = Cipher::new_from_slices(&key, &iv).unwrap();
27//! let ct = cipher.encrypt_vec(pt);
28//!
29//! let cipher = Cipher::new_from_slices(&key, &iv).unwrap();
30//! let buf = cipher.decrypt_vec(&ct).unwrap();
31//!
32//! assert_eq!(buf, &pt[..]);
33//!
34//! // OFB mode example
35//! use gost_modes::{GostOfb, StreamCipher, NewCipher};
36//!
37//! let mut cipher = GostOfb::<Kuznyechik, U2>::new_from_slices(&key, &iv).unwrap();
38//! let mut buf = pt.to_vec();
39//! cipher.apply_keystream(&mut buf);
40//! assert_eq!(buf, hex!("fddb196e81812e4174d1c9f741a3457a88"));
41//! ```
42//!
43//! [1]: https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation
44//! [GOST R 34.13-2015]: https://tc26.ru/standard/gost/GOST_R_3413-2015.pdf
45//! [`cipher`]: https://docs.rs/cipher/
46//! [`cmac`]: https://docs.rs/cmac/
47#![no_std]
48#![doc(
49    html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg",
50    html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg"
51)]
52#![deny(unsafe_code)]
53#![warn(missing_docs, rust_2018_idioms)]
54
55pub use block_modes;
56pub use block_modes::block_padding;
57pub use cipher::{self, consts};
58pub use generic_array;
59
60pub use block_modes::{BlockMode, Ecb};
61pub use cipher::{AsyncStreamCipher, NewCipher, StreamCipher, StreamCipherSeek};
62
63mod cbc;
64mod cfb;
65mod ctr128;
66mod ctr64;
67mod ofb;
68mod utils;
69
70/// Block padding procedure number 2 as defined in GOST R 34.13-2015
71///
72/// Fully equivalent to ISO 7816.
73pub type GostPadding = block_padding::Iso7816;
74
75pub use cbc::GostCbc;
76pub use cfb::GostCfb;
77pub use ctr128::GostCtr128;
78pub use ctr64::GostCtr64;
79pub use ofb::GostOfb;