|
1 | 1 | use cipher::{ |
2 | | - BlockSizeUser, IvSizeUser, KeyIvInit, KeySizeUser, ParBlocksSizeUser, StreamCipher, |
3 | | - StreamCipherBackend, StreamCipherClosure, StreamCipherCore, StreamCipherCoreWrapper, |
4 | | - StreamCipherSeek, StreamCipherSeekCore, |
| 2 | + BlockSizeUser, IvSizeUser, KeyIvInit, KeySizeUser, ParBlocksSizeUser, StreamCipherBackend, |
| 3 | + StreamCipherClosure, StreamCipherCore, StreamCipherSeekCore, |
5 | 4 | consts::{U1, U4, U16}, |
6 | 5 | }; |
| 6 | +#[cfg(feature = "stream-wrapper")] |
| 7 | +use cipher::{StreamCipher, StreamCipherCoreWrapper, StreamCipherSeek}; |
| 8 | +use hex_literal::hex; |
7 | 9 |
|
8 | 10 | const KEY: [u8; 4] = [0, 1, 2, 3]; |
9 | 11 | const IV: [u8; 4] = [4, 5, 6, 7]; |
@@ -85,33 +87,40 @@ impl StreamCipherSeekCore for DummyStreamCipherCore { |
85 | 87 | #[cfg(feature = "stream-wrapper")] |
86 | 88 | pub type DummyStreamCipher = StreamCipherCoreWrapper<DummyStreamCipherCore>; |
87 | 89 |
|
| 90 | +#[test] |
| 91 | +fn dummy_stream_cipher_core() { |
| 92 | + let mut cipher = DummyStreamCipherCore::new(&KEY.into(), &IV.into()); |
| 93 | + assert_eq!(cipher.get_block_pos(), 0); |
| 94 | + |
| 95 | + let mut block = [0u8; 16].into(); |
| 96 | + cipher.write_keystream_block(&mut block); |
| 97 | + assert_eq!(block, hex!("e82393543cc96089305116003a417acc")); |
| 98 | + assert_eq!(cipher.get_block_pos(), 1); |
| 99 | + |
| 100 | + cipher.set_block_pos(200); |
| 101 | + assert_eq!(cipher.get_block_pos(), 200); |
| 102 | + |
| 103 | + cipher.write_keystream_block(&mut block); |
| 104 | + assert_eq!(block, hex!("28a96998fe4874ffb0ce9b046c6a9ddb")); |
| 105 | + assert_eq!(cipher.get_block_pos(), 201); |
| 106 | +} |
| 107 | + |
88 | 108 | #[test] |
89 | 109 | #[cfg(feature = "stream-wrapper")] |
90 | 110 | fn dummy_stream_cipher() { |
91 | 111 | let mut cipher = DummyStreamCipher::new(&KEY.into(), &IV.into()); |
92 | | - |
93 | 112 | assert_eq!(cipher.current_pos::<u64>(), 0); |
94 | 113 |
|
95 | 114 | let mut buf = [0u8; 20]; |
96 | | - |
97 | 115 | cipher.apply_keystream(&mut buf); |
| 116 | + assert_eq!(buf, hex!("e82393543cc96089305116003a417accd073384a")); |
98 | 117 | assert_eq!(cipher.current_pos::<usize>(), buf.len()); |
99 | 118 |
|
100 | | - let expected = [ |
101 | | - 232, 35, 147, 84, 60, 201, 96, 137, 48, 81, 22, 0, 58, 65, 122, 204, 208, 115, 56, 74, |
102 | | - ]; |
103 | | - assert_eq!(buf, expected); |
104 | | - |
105 | 119 | const SEEK_POS: usize = 500; |
106 | | - |
107 | 120 | cipher.seek(SEEK_POS); |
108 | 121 | cipher.apply_keystream(&mut buf); |
| 122 | + assert_eq!(buf, hex!("6b014c6a3c376b13c4720590d26147c5ebf334c5")); |
109 | 123 | assert_eq!(cipher.current_pos::<usize>(), SEEK_POS + buf.len()); |
110 | | - |
111 | | - let expected = [ |
112 | | - 107, 1, 76, 106, 60, 55, 107, 19, 196, 114, 5, 144, 210, 97, 71, 197, 235, 243, 52, 197, |
113 | | - ]; |
114 | | - assert_eq!(buf, expected); |
115 | 124 | } |
116 | 125 |
|
117 | 126 | #[test] |
|
0 commit comments