Skip to content

Commit 6da6267

Browse files
committed
feat: TABLE cipher add more tests
1 parent 74a3808 commit 6da6267

File tree

4 files changed

+110
-56
lines changed

4 files changed

+110
-56
lines changed

Diff for: Cargo.toml

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "shadowsocks-crypto"
3-
version = "0.5.4"
3+
version = "0.5.5"
44
authors = ["luozijun <[email protected]>", "ty <[email protected]>"]
55
edition = "2021"
66
license = "MIT"
@@ -70,3 +70,7 @@ subtle = { version = "2.5", optional = true }
7070

7171
[dev-dependencies]
7272
hex = "0.4"
73+
74+
[package.metadata.docs.rs]
75+
all-features = true
76+
rustdoc-args = ["--cfg", "docsrs"]

Diff for: src/kind.rs

+51-51
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ pub enum CipherCategory {
6161
None,
6262
/// Stream ciphers is used for OLD ShadowSocks protocol, which uses stream ciphers to encrypt data payloads
6363
#[cfg(feature = "v1-stream")]
64-
#[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
64+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-stream")))]
6565
Stream,
6666
/// AEAD ciphers is used in modern ShadowSocks protocol, which sends data in separate packets
6767
#[cfg(feature = "v1-aead")]
68-
#[cfg_attr(docrs, doc(cfg(feature = "v1-aead")))]
68+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-aead")))]
6969
Aead,
7070
/// AEAD ciphers 2022 with enhanced security
7171
#[cfg(feature = "v2")]
72-
#[cfg_attr(docrs, doc(cfg(feature = "v2")))]
72+
#[cfg_attr(docsrs, doc(cfg(feature = "v2")))]
7373
Aead2022,
7474
}
7575

@@ -81,180 +81,180 @@ pub enum CipherKind {
8181
NONE,
8282

8383
#[cfg(feature = "v1-stream")]
84-
#[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
84+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-stream")))]
8585
SS_TABLE,
8686
#[cfg(feature = "v1-stream")]
87-
#[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
87+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-stream")))]
8888
SS_RC4_MD5,
8989

9090
#[cfg(feature = "v1-stream")]
91-
#[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
91+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-stream")))]
9292
AES_128_CTR,
9393
#[cfg(feature = "v1-stream")]
94-
#[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
94+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-stream")))]
9595
AES_192_CTR,
9696
#[cfg(feature = "v1-stream")]
97-
#[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
97+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-stream")))]
9898
AES_256_CTR,
9999

100100
#[cfg(feature = "v1-stream")]
101-
#[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
101+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-stream")))]
102102
AES_128_CFB1,
103103
#[cfg(feature = "v1-stream")]
104-
#[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
104+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-stream")))]
105105
AES_128_CFB8,
106106
#[cfg(feature = "v1-stream")]
107-
#[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
107+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-stream")))]
108108
AES_128_CFB128,
109109
#[cfg(feature = "v1-stream")]
110-
#[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
110+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-stream")))]
111111
AES_192_CFB1,
112112
#[cfg(feature = "v1-stream")]
113-
#[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
113+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-stream")))]
114114
AES_192_CFB8,
115115
#[cfg(feature = "v1-stream")]
116-
#[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
116+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-stream")))]
117117
AES_192_CFB128,
118118
#[cfg(feature = "v1-stream")]
119-
#[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
119+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-stream")))]
120120
AES_256_CFB1,
121121
#[cfg(feature = "v1-stream")]
122-
#[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
122+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-stream")))]
123123
AES_256_CFB8,
124124
#[cfg(feature = "v1-stream")]
125-
#[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
125+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-stream")))]
126126
AES_256_CFB128,
127127

128128
#[cfg(feature = "v1-stream")]
129-
#[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
129+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-stream")))]
130130
AES_128_OFB,
131131
#[cfg(feature = "v1-stream")]
132-
#[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
132+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-stream")))]
133133
AES_192_OFB,
134134
#[cfg(feature = "v1-stream")]
135-
#[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
135+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-stream")))]
136136
AES_256_OFB,
137137

138138
#[cfg(feature = "v1-stream")]
139-
#[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
139+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-stream")))]
140140
CAMELLIA_128_CTR,
141141
#[cfg(feature = "v1-stream")]
142-
#[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
142+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-stream")))]
143143
CAMELLIA_192_CTR,
144144
#[cfg(feature = "v1-stream")]
145-
#[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
145+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-stream")))]
146146
CAMELLIA_256_CTR,
147147

148148
#[cfg(feature = "v1-stream")]
149-
#[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
149+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-stream")))]
150150
CAMELLIA_128_CFB1,
151151
#[cfg(feature = "v1-stream")]
152-
#[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
152+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-stream")))]
153153
CAMELLIA_128_CFB8,
154154
#[cfg(feature = "v1-stream")]
155-
#[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
155+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-stream")))]
156156
CAMELLIA_128_CFB128,
157157
#[cfg(feature = "v1-stream")]
158-
#[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
158+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-stream")))]
159159
CAMELLIA_192_CFB1,
160160
#[cfg(feature = "v1-stream")]
161-
#[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
161+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-stream")))]
162162
CAMELLIA_192_CFB8,
163163
#[cfg(feature = "v1-stream")]
164-
#[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
164+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-stream")))]
165165
CAMELLIA_192_CFB128,
166166
#[cfg(feature = "v1-stream")]
167-
#[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
167+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-stream")))]
168168
CAMELLIA_256_CFB1,
169169
#[cfg(feature = "v1-stream")]
170-
#[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
170+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-stream")))]
171171
CAMELLIA_256_CFB8,
172172
#[cfg(feature = "v1-stream")]
173-
#[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
173+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-stream")))]
174174
CAMELLIA_256_CFB128,
175175

176176
#[cfg(feature = "v1-stream")]
177-
#[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
177+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-stream")))]
178178
CAMELLIA_128_OFB,
179179
#[cfg(feature = "v1-stream")]
180-
#[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
180+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-stream")))]
181181
CAMELLIA_192_OFB,
182182
#[cfg(feature = "v1-stream")]
183-
#[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
183+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-stream")))]
184184
CAMELLIA_256_OFB,
185185

186186
#[cfg(feature = "v1-stream")]
187-
#[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
187+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-stream")))]
188188
RC4,
189189
// NOTE: IETF 版本
190190
#[cfg(feature = "v1-stream")]
191-
#[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
191+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-stream")))]
192192
CHACHA20,
193193

194194
// AEAD Cipher
195195
#[cfg(feature = "v1-aead")]
196-
#[cfg_attr(docrs, doc(cfg(feature = "v1-aead")))]
196+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-aead")))]
197197
/// AEAD_AES_128_GCM
198198
AES_128_GCM,
199199
#[cfg(feature = "v1-aead")]
200-
#[cfg_attr(docrs, doc(cfg(feature = "v1-aead")))]
200+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-aead")))]
201201
/// AEAD_AES_256_GCM
202202
AES_256_GCM,
203203

204204
#[cfg(feature = "v1-aead-extra")]
205-
#[cfg_attr(docrs, doc(cfg(feature = "v1-aead-extra")))]
205+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-aead-extra")))]
206206
/// AEAD_AES_128_CCM
207207
AES_128_CCM,
208208
#[cfg(feature = "v1-aead-extra")]
209-
#[cfg_attr(docrs, doc(cfg(feature = "v1-aead-extra")))]
209+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-aead-extra")))]
210210
/// AEAD_AES_256_CCM
211211
AES_256_CCM,
212212

213213
#[cfg(feature = "v1-aead-extra")]
214-
#[cfg_attr(docrs, doc(cfg(feature = "v1-aead-extra")))]
214+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-aead-extra")))]
215215
/// AEAD_AES_128_GCM_SIV
216216
AES_128_GCM_SIV,
217217
#[cfg(feature = "v1-aead-extra")]
218-
#[cfg_attr(docrs, doc(cfg(feature = "v1-aead-extra")))]
218+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-aead-extra")))]
219219
/// AEAD_AES_256_GCM_SIV
220220
AES_256_GCM_SIV,
221221

222222
// NOTE: IETF 版本
223223
#[cfg(feature = "v1-aead")]
224-
#[cfg_attr(docrs, doc(cfg(feature = "v1-aead")))]
224+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-aead")))]
225225
/// AEAD_CHACHA20_POLY1305
226226
CHACHA20_POLY1305,
227227

228228
#[cfg(feature = "v1-aead-extra")]
229-
#[cfg_attr(docrs, doc(cfg(feature = "v1-aead-extra")))]
229+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-aead-extra")))]
230230
/// AEAD_XCHACHA20_POLY1305
231231
XCHACHA20_POLY1305,
232232

233233
#[cfg(feature = "v1-aead-extra")]
234-
#[cfg_attr(docrs, doc(cfg(feature = "v1-aead-extra")))]
234+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-aead-extra")))]
235235
/// SM4_GCM
236236
SM4_GCM,
237237
#[cfg(feature = "v1-aead-extra")]
238-
#[cfg_attr(docrs, doc(cfg(feature = "v1-aead-extra")))]
238+
#[cfg_attr(docsrs, doc(cfg(feature = "v1-aead-extra")))]
239239
/// SM4_GCM
240240
SM4_CCM,
241241

242242
#[cfg(feature = "v2")]
243-
#[cfg_attr(docrs, doc(cfg(feature = "v2")))]
243+
#[cfg_attr(docsrs, doc(cfg(feature = "v2")))]
244244
/// 2022-blake3-aes-128-gcm
245245
AEAD2022_BLAKE3_AES_128_GCM,
246246

247247
#[cfg(feature = "v2")]
248-
#[cfg_attr(docrs, doc(cfg(feature = "v2")))]
248+
#[cfg_attr(docsrs, doc(cfg(feature = "v2")))]
249249
/// 2022-blake3-aes-128-gcm
250250
AEAD2022_BLAKE3_AES_256_GCM,
251251

252252
#[cfg(feature = "v2")]
253-
#[cfg_attr(docrs, doc(cfg(feature = "v2")))]
253+
#[cfg_attr(docsrs, doc(cfg(feature = "v2")))]
254254
/// 2022-blake3-chacha20-poly1305
255255
AEAD2022_BLAKE3_CHACHA20_POLY1305,
256256
#[cfg(feature = "v2-extra")]
257-
#[cfg_attr(docrs, doc(cfg(feature = "v2-extra")))]
257+
#[cfg_attr(docsrs, doc(cfg(feature = "v2-extra")))]
258258
/// 2022-blake3-chacha8-poly1305
259259
AEAD2022_BLAKE3_CHACHA8_POLY1305,
260260
}

Diff for: src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
#![cfg_attr(docsrs, feature(doc_cfg))]
44

55
#[cfg(feature = "v1")]
6-
#[cfg_attr(docrs, doc(cfg(feature = "v1")))]
6+
#[cfg_attr(docsrs, doc(cfg(feature = "v1")))]
77
pub mod v1;
88

99
#[cfg(feature = "v2")]
10-
#[cfg_attr(docrs, doc(cfg(feature = "v2")))]
10+
#[cfg_attr(docsrs, doc(cfg(feature = "v2")))]
1111
pub mod v2;
1212

1313
pub mod kind;

Diff for: src/v1/streamcipher/table.rs

+52-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ impl Table {
2323
let a = u64::from_le_bytes([h[0], h[1], h[2], h[3], h[4], h[5], h[6], h[7]]);
2424

2525
let mut table = [0u64; Self::TABLE_SIZE];
26-
for (i, item) in table.iter_mut().enumerate().take(Self::TABLE_SIZE) {
27-
*item = i as u64;
26+
27+
for i in 0..table.len() {
28+
table[i] = i as u64;
2829
}
2930

3031
for i in 1..1024 {
@@ -74,3 +75,52 @@ fn test_table() {
7475

7576
assert_eq!(&cleartext[..], plaintext);
7677
}
78+
79+
#[test]
80+
fn test_table_box() {
81+
let key: &[u8] = b"password";
82+
let ebox: [u8; 256] = [
83+
157, 219, 245, 15, 85, 7, 195, 211, 55, 126, 37, 117, 249, 229, 98, 205, 254, 61, 137, 77, 253, 135, 138, 185,
84+
45, 100, 75, 97, 46, 22, 28, 84, 143, 160, 175, 136, 194, 2, 201, 173, 132, 155, 23, 174, 95, 54, 0, 239, 6,
85+
153, 180, 34, 149, 26, 19, 101, 203, 247, 214, 111, 127, 119, 81, 177, 53, 142, 13, 216, 115, 241, 202, 73, 48,
86+
86, 1, 11, 43, 125, 41, 121, 209, 193, 199, 51, 47, 32, 36, 90, 255, 156, 38, 108, 3, 99, 238, 179, 50, 237,
87+
158, 186, 110, 217, 76, 223, 118, 196, 107, 83, 39, 63, 9, 129, 72, 5, 56, 234, 91, 250, 224, 228, 251, 146,
88+
170, 151, 21, 10, 171, 114, 154, 172, 58, 78, 140, 197, 67, 35, 130, 92, 12, 31, 189, 166, 122, 29, 123, 113,
89+
215, 94, 165, 89, 221, 240, 93, 178, 150, 218, 220, 232, 144, 188, 65, 88, 52, 59, 139, 242, 71, 62, 182, 57,
90+
225, 147, 30, 17, 68, 243, 80, 44, 141, 4, 200, 42, 16, 102, 134, 246, 70, 244, 145, 124, 213, 8, 187, 66, 183,
91+
191, 40, 103, 162, 74, 87, 148, 230, 25, 120, 60, 233, 18, 176, 227, 184, 112, 20, 131, 109, 152, 14, 163, 49,
92+
24, 222, 181, 164, 133, 207, 104, 210, 236, 27, 106, 96, 64, 33, 116, 79, 206, 69, 212, 82, 169, 105, 235, 190,
93+
128, 226, 208, 168, 192, 167, 159, 161, 231, 204, 198, 248, 252,
94+
];
95+
let dbox: [u8; 256] = [
96+
46, 74, 37, 92, 179, 113, 48, 5, 191, 110, 125, 75, 138, 66, 216, 3, 182, 173, 207, 54, 212, 124, 29, 42, 219,
97+
203, 53, 228, 30, 143, 172, 139, 85, 232, 51, 135, 86, 10, 90, 108, 196, 78, 181, 76, 177, 24, 28, 84, 72, 218,
98+
96, 83, 162, 64, 45, 8, 114, 169, 130, 163, 205, 17, 167, 109, 231, 160, 193, 134, 174, 236, 186, 166, 112, 71,
99+
199, 26, 102, 19, 131, 234, 176, 62, 238, 107, 31, 4, 73, 200, 161, 149, 87, 116, 137, 152, 147, 44, 230, 27,
100+
14, 93, 25, 55, 183, 197, 225, 240, 229, 106, 91, 214, 100, 59, 211, 145, 127, 68, 233, 11, 104, 61, 204, 79,
101+
142, 144, 189, 77, 9, 60, 243, 111, 136, 213, 40, 223, 184, 21, 35, 18, 22, 164, 132, 178, 65, 32, 158, 188,
102+
121, 171, 201, 52, 154, 123, 215, 49, 128, 41, 89, 0, 98, 249, 33, 250, 198, 217, 222, 148, 141, 248, 246, 239,
103+
122, 126, 129, 39, 43, 34, 208, 63, 153, 95, 50, 221, 168, 194, 210, 23, 99, 192, 159, 140, 242, 195, 247, 81,
104+
36, 6, 105, 133, 253, 82, 180, 38, 70, 56, 252, 15, 235, 224, 245, 80, 226, 7, 237, 190, 58, 146, 67, 101, 155,
105+
1, 156, 150, 220, 103, 118, 170, 244, 209, 119, 13, 202, 251, 157, 206, 115, 241, 227, 97, 94, 47, 151, 69,
106+
165, 175, 187, 2, 185, 57, 254, 12, 117, 120, 255, 20, 16, 88,
107+
];
108+
109+
let cipher = Table::new(key, b"");
110+
assert_eq!(cipher.ebox, ebox);
111+
assert_eq!(cipher.dbox, dbox);
112+
}
113+
114+
#[test]
115+
fn test_table_encrypt() {
116+
let key: &[u8] = b"password";
117+
let plain_text: &[u8] = b"hello world";
118+
let cipher_text: &[u8] = &[118, 217, 39, 39, 129, 143, 228, 129, 56, 39, 110];
119+
120+
let mut cipher = Table::new(key, b"");
121+
122+
let mut text_buffer = plain_text.to_vec();
123+
cipher.encrypt_slice(&mut text_buffer);
124+
125+
assert_eq!(cipher_text, text_buffer);
126+
}

0 commit comments

Comments
 (0)