1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//! # No Chat Reports (NCR) Crypto
//!
//! The cryptography used to generate passwords and encrypted messages
//! exactly as the [No Chat Reports](https://github.com/Aizistral-Studios/No-Chat-Reports) Mod for Minecraft does.
//!
//! # Examples
//!
//! ```
//! use base64::{alphabet::Alphabet, engine::{GeneralPurpose, GeneralPurposeConfig}, Engine};
//! use ncr_crypto::{decrypt_with_passphrase, decode_and_verify};
//!
//! let passphrase = b"secret"; // Setting in NCR
//! // "Hello, world!" sent as a message in chat:
//! let alphabet = Alphabet::new("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\\").unwrap();
//! let b64 = GeneralPurpose::new(&alphabet, GeneralPurposeConfig::new());
//! let ciphertext = b64.decode("q2JCS\\M3yMnz+MtXDn4dd6xyqN94Dao=").unwrap();
//!
//! let decrypted = decrypt_with_passphrase(&ciphertext, passphrase);
//! let decoded = decode_and_verify(&decrypted);
//!
//! assert_eq!(decoded, Ok("#%Hello, world!"))
//! ```
//!
//! # How it works
//!
//! From reading the Source Code on Github it becomes clear how the mod does encryption:
//!
//! 1. You set a passphrase like "secret" in the UI
//! 2. The mod uses `PBKDF2_HMAC_SHA1` with a hardcoded salt and 65536 iterations to make your passphrase
//! into a hash of 16 bytes. This process takes the longest
//! 3. An Initialization Vector (IV) is generated from a random nonce value, and used in the encryption that follows
//! 4. The new hash becomes the key used for encrypting any messages you send with `AES-CFB8` encryption
//! 5. The ciphertext that comes from this encryption is appended to the nonce that was generated, and the final message
//! that is sent in Base64 encoding through the chat (note: `"#%"` is added as a prefix to the message before encrypting)
//!
//! Decrypting then is very similar, just in reverse:
//!
//! 1. Decode the message from Base64 into raw bytes
//! 2. Get the nonce from the message and generate the IV again with it
//! 2. Generate the hash from the secret passphrase again, and use it as the key for the AES encryption
//! 3. If the decrypted message starts with `"#%"`, the rest is printed decrypted in the chat
use ;
use ;
use ;
use pbkdf2_hmac_sha1;
use pbkdf2;
use PBKDF2_HMAC_SHA1;
use NonZeroU32;
type Aes128Cfb8Dec = Decryptor;
type Aes128Cfb8Enc = Encryptor;
/// Content salt for all passphrases ([source](https://github.com/Aizistral-Studios/No-Chat-Reports/blob/c2c60a03544952fe608bd65163cc0b2658e3c032/src/main/java/com/aizistral/nochatreports/encryption/AESEncryption.java#L57-L58))
///
/// Generated as follows:
///
/// ```
/// let mut salt = [0; 16];
/// java_rand::Random::new(1738389128127)
/// .next_bytes(&mut salt);
///
/// assert_eq!(salt, [45, 72, 24, 73, 11, 12, 10, 149, 250, 165, 68, 71, 1, 217, 153, 119]);
/// ```
pub const SALT: = ;
/// Generate a key from a passphrase
///
/// Use `PBKDF2_HMAC_SHA1` with a hardcoded salt and 65536 iterations to hash a passphrase into a 16-byte key
///
/// # Examples
///
/// ```
/// use base64::{alphabet::Alphabet, engine::{GeneralPurpose, GeneralPurposeConfig}, Engine};
/// use ncr_crypto::generate_key;
///
/// let passphrase = b"secret";
///
/// let key = generate_key(passphrase);
/// let alphabet = Alphabet::new("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\\").unwrap();
/// let b64 = GeneralPurpose::new(&alphabet, GeneralPurposeConfig::new());
///
/// assert_eq!(b64.encode(key), "474esvGYVuN83HpxbK1uFQ=="); // Can be seen in the NCR UI when typing the passphrase
/// ```
/// Encrypt a plaintext message with a given key
///
/// > **Warning**: This function does **not** append `"#%"` to the message before encrypting. NCR automatically does this when sending a message,
/// > so add it if you're planning to send a real message that NCR should recognize
///
/// NCR uses AES-CFB8 for encryption, with a 16-byte key. Generate a key from [`generate_key()`] using a passphrase, or
/// provide the raw bytes to this function. You can also use [`encrypt_with_passphrase()`] as a shorthand for doing both of these things
///
/// # Examples
///
/// ```
/// use base64::{alphabet::Alphabet, engine::{GeneralPurpose, GeneralPurposeConfig}, Engine};
/// use ncr_crypto::{encrypt, decrypt};
///
/// let alphabet = Alphabet::new("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\\").unwrap();
/// let b64 = GeneralPurpose::new(&alphabet, GeneralPurposeConfig::new());
/// let key = b64.decode("blfrngArk3chG6wzncOZ5A==").unwrap(); // Default key
/// let key = key.try_into().unwrap();
/// let plaintext = b"#%Hello, world!";
///
/// let encrypted = encrypt(plaintext, &key);
/// // Here `encrypted` is something random like [240, 28, 167, ..., 237, 3, 89]
/// let decrypted = decrypt(&encrypted, &key);
///
/// assert_eq!(decrypted, plaintext);
/// ```
/// Decrypt a ciphertext message with a given key
///
/// NCR uses AES-CFB8 for encryption, with a 16-byte key. Generate a key from [`generate_key()`] using a passphrase, or
/// provide the raw bytes to this function. You can also use [`decrypt_with_passphrase()`] as a shorthand for doing both of these things
///
/// # Examples
///
/// ```
/// use base64::{alphabet::Alphabet, engine::{GeneralPurpose, GeneralPurposeConfig}, Engine};
/// use ncr_crypto::decrypt;
///
/// let alphabet = Alphabet::new("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\\").unwrap();
/// let b64 = GeneralPurpose::new(&alphabet, GeneralPurposeConfig::new());
/// let key = b64.decode("blfrngArk3chG6wzncOZ5A==").unwrap(); // Default key
/// let ciphertext = b64.decode("NuhaeyIn3WJDHY\\W0X++EJKON32pDAA=").unwrap();
///
/// let decrypted = decrypt(&ciphertext, &key.try_into().unwrap());
///
/// assert_eq!(std::str::from_utf8(&decrypted).unwrap(), "#%Hello, world!");
/// ```
/// Encrypt a ciphertext message with a given passphrase
///
/// Shorthand for [`generate_key()`] and then [`encrypt()`]
///
/// # Examples
///
/// ```
/// use ncr_crypto::{encrypt_with_passphrase, decrypt_with_passphrase};
///
/// let passphrase = b"secret";
/// let plaintext = b"#%Hello, world!";
///
/// let encrypted = encrypt_with_passphrase(plaintext, passphrase);
/// // Here `encrypted` is something random like [240, 28, 167, ..., 237, 3, 89]
/// let decrypted = decrypt_with_passphrase(&encrypted, passphrase);
///
/// assert_eq!(decrypted, plaintext);
/// ```
/// Decrypt a ciphertext message with a given passphrase
///
/// Shorthand for [`generate_key()`] and then [`decrypt()`]
///
/// # Examples
///
/// ```
/// use base64::{alphabet::Alphabet, engine::{GeneralPurpose, GeneralPurposeConfig}, Engine};
/// use ncr_crypto::decrypt_with_passphrase;
///
/// let alphabet = Alphabet::new("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\\").unwrap();
/// let b64 = GeneralPurpose::new(&alphabet, GeneralPurposeConfig::new());
/// let passphrase = b"secret";
/// let ciphertext = b64.decode("q2JCS\\M3yMnz+MtXDn4dd6xyqN94Dao=").unwrap();
///
/// let decrypted = decrypt_with_passphrase(&ciphertext, passphrase);
///
/// assert_eq!(std::str::from_utf8(&decrypted).unwrap(), "#%Hello, world!");
/// ```
;
/// Verify if a message could be correctly decrypted
///
/// Decrypted message from NCR are always prefixed with "#%", and contain valid UTF8. This function verifies both of these things
/// and returns a Result containing the decoded `&str` or a `FormatError` in case it is not valid
///
/// # Examples
///
/// ```
/// use base64::{alphabet::Alphabet, engine::{GeneralPurpose, GeneralPurposeConfig}, Engine};
/// use ncr_crypto::{decode_and_verify, decrypt_with_passphrase};
///
/// let alphabet = Alphabet::new("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\\").unwrap();
/// let b64 = GeneralPurpose::new(&alphabet, GeneralPurposeConfig::new());
/// let passphrase = b"secret";
/// let ciphertext = b64.decode("q2JCS\\M3yMnz+MtXDn4dd6xyqN94Dao=").unwrap();
///
/// let decrypted = decrypt_with_passphrase(&ciphertext, passphrase);
/// let decoded = decode_and_verify(&decrypted);
///
/// assert_eq!(decoded, Ok("#%Hello, world!"));
/// ```
///
/// ```
/// use base64::{alphabet::Alphabet, engine::{GeneralPurpose, GeneralPurposeConfig}, Engine};
/// use ncr_crypto::{decode_and_verify, decrypt_with_passphrase, FormatError};
///
/// let alphabet = Alphabet::new("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\\").unwrap();
/// let b64 = GeneralPurpose::new(&alphabet, GeneralPurposeConfig::new());
/// let passphrase = b"wrong"; // Should be "secret"
/// let ciphertext = b64.decode("q2JCS\\M3yMnz+MtXDn4dd6xyqN94Dao=").unwrap();
///
/// let decrypted = decrypt_with_passphrase(&ciphertext, passphrase);
/// let decoded = decode_and_verify(&decrypted);
///
/// assert_eq!(decoded, Err(FormatError));
/// ```
///
/// ```
/// use ncr_crypto::{decode_and_verify, FormatError};
///
/// let bytes = b"Hello, world!"; // Without "#%" prefix
/// let decoded = decode_and_verify(bytes);
///
/// assert_eq!(decoded, Err(FormatError));
/// ```