rpfm_lib/encryption/mod.rs
1//---------------------------------------------------------------------------//
2// Copyright (c) 2017-2026 Ismael Gutiérrez González. All rights reserved.
3//
4// This file is part of the Rusted PackFile Manager (RPFM) project,
5// which can be found here: https://github.com/Frodo45127/rpfm.
6//
7// This file is licensed under the MIT license, which can be found here:
8// https://github.com/Frodo45127/rpfm/blob/master/LICENSE.
9//---------------------------------------------------------------------------//
10
11//! This module contains the code to decrypt encrypted data in Total War PackFiles.
12//!
13//! The [`Decryptable`] trait provides functions to decrypt various parts of encrypted PackFiles,
14//! including file data, file sizes, and file paths. An implementation for anything that implements
15//! [`ReadBytes`] + [`Read`] + [`Seek`] is provided.
16//!
17//! # Encryption Scheme
18//!
19//! Total War games use a custom encryption scheme with different keys for different parts of the PackFile:
20//! - **Index String Key**: 64-byte key for decrypting file paths
21//! - **Index U32 Key**: [`u32`] key for decrypting file sizes
22//! - **Data Key**: [`u64`] key for decrypting file data
23//!
24//! # Historical Context
25//!
26//! The encryption scheme has evolved over time:
27//! - **Shogun 2 to Arena**: Used older keys (now commented out in the code)
28//! - **Modern Games**: Use the current key set introduced after Arena
29//!
30//! [`Read`]: std::io::Read
31//! [`Seek`]: std::io::Seek
32
33use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
34
35use std::io::{Read, Seek};
36
37use crate::error::Result;
38use crate::binary::ReadBytes;
39
40// Old 64-byte key used in Arena and all the way back to Shogun 2 for decrypting file paths.
41// This key is no longer used but is kept for reference and backwards compatibility with older PackFiles.
42// static INDEX_STRING_KEY: &str = "L2{B3dPL7L*v&+Q3ZsusUhy[BGQn(Uq$f>JQdnvdlf{-K:>OssVDr#TlYU|13B}r";
43
44// Old u32 key used in Arena's encrypted PackFiles for decrypting file sizes.
45// This key is no longer used but is kept for reference and backwards compatibility with older PackFiles.
46// static INDEX_U32_KEY: u32 = 0x1509_1984;
47
48/// Current 64-byte key used for decrypting PackedFile paths in the encrypted index.
49///
50/// This key rotates through its 64 bytes during the decryption process. Each character of the
51/// encrypted path is XORed with the corresponding byte from this key (wrapping around after 64 bytes).
52static INDEX_STRING_KEY: [u8; 64] = *b"#:AhppdV-!PEfz&}[]Nv?6w4guU%dF5.fq:n*-qGuhBJJBm&?2tPy!geW/+k#pG?";
53
54/// Current [`u32`] key used for decrypting PackedFile sizes in the encrypted index.
55///
56/// This key is combined with a position-based secondary key during the XOR decryption process.
57static INDEX_U32_KEY: u32 = 0xE10B_73F4;
58
59/// Current [`u64`] key used for decrypting PackedFile data.
60///
61/// This key is used in 8-byte chunks to decrypt the actual file data. The decryption
62/// formula is: `decrypted = encrypted XOR (DATA_KEY * !position)`.
63static DATA_KEY: u64 = 0x8FEB_2A67_40A6_920E;
64
65/// Trait for decrypting encrypted PackFile data.
66///
67/// This trait provides methods to decrypt different parts of encrypted PackFiles using
68/// Total War's custom encryption scheme. The decryption uses three different keys for
69/// different types of data (file data, file sizes, and file paths).
70///
71/// # Implementation
72///
73/// This trait is automatically implemented for any type that implements
74/// [`ReadBytes`] + [`Read`] + [`Seek`].
75///
76/// [`Read`]: std::io::Read
77/// [`Seek`]: std::io::Seek
78pub trait Decryptable: ReadBytes + Read + Seek {
79
80 /// Decrypts the data of an encrypted PackedFile.
81 ///
82 /// This function decrypts data in 8-byte chunks using the DATA_KEY. The file is first
83 /// padded to a multiple of 8 bytes if needed, then decrypted chunk by chunk. Note that
84 /// the last chunk is NOT encrypted and is copied as-is.
85 ///
86 /// # Returns
87 ///
88 /// A [`Vec<u8>`] containing the decrypted data, or an error if decryption fails.
89 ///
90 /// # Examples
91 ///
92 /// ```ignore
93 /// use std::io::Cursor;
94 /// use rpfm_lib::encryption::Decryptable;
95 ///
96 /// let encrypted_data = vec![/* encrypted bytes */];
97 /// let mut cursor = Cursor::new(encrypted_data);
98 /// let decrypted = cursor.decrypt()?;
99 /// ```
100 fn decrypt(&mut self) -> Result<Vec<u8>> {
101
102 // First, make sure the file ends in a multiple of 8. If not, extend it with zeros.
103 // We need it because the decoding is done in packs of 8 bytes.
104 let ciphertext_len = self.len()? as usize;
105 let mut ciphertext = self.read_slice(ciphertext_len, false)?;
106 let size = ciphertext.len();
107 let padding = 8 - (size % 8);
108 if padding < 8 {
109 ciphertext.resize(size + padding, 0);
110 }
111
112 // Then decrypt the file in packs of 8. It's faster than in packs of 4.
113 let mut plaintext = Vec::with_capacity(ciphertext.len());
114 let mut edi: u64 = 0;
115 let chunks = ciphertext.len() / 8;
116 for i in 0..chunks {
117
118 // The last chunk is NOT ENCRYPTED.
119 let esi = edi as usize;
120 if i == chunks - 1 {
121 plaintext.extend_from_slice(&ciphertext[esi..esi + 8]);
122 } else {
123 let mut prod = DATA_KEY.wrapping_mul(!edi);
124 prod ^= (&ciphertext[esi..esi + 8]).read_u64::<LittleEndian>().unwrap();
125 plaintext.write_u64::<LittleEndian>(prod).unwrap();
126 }
127 edi += 8
128 }
129
130 // Remove the extra bytes we added in the first step.
131 plaintext.truncate(size);
132 Ok(plaintext)
133 }
134
135 /// Decrypts the size of a PackedFile from the encrypted index.
136 ///
137 /// This function reads and decrypts a [`u32`] value representing the size of a PackedFile.
138 /// The decryption uses both the INDEX_U32_KEY and a second key derived from the position
139 /// in the index.
140 ///
141 /// # Arguments
142 ///
143 /// * `second_key` - The secondary key, typically the number of PackedFiles after this one in the index.
144 ///
145 /// # Returns
146 ///
147 /// The decrypted file size as a [`u32`], or an error if reading fails.
148 ///
149 /// # Examples
150 ///
151 /// ```ignore
152 /// use std::io::Cursor;
153 /// use rpfm_lib::encryption::Decryptable;
154 ///
155 /// let encrypted_index = vec![/* encrypted index bytes */];
156 /// let mut cursor = Cursor::new(encrypted_index);
157 /// let packed_files_remaining = 5;
158 /// let size = cursor.decrypt_u32(packed_files_remaining)?;
159 /// ```
160 fn decrypt_u32(&mut self, second_key: u32) -> Result<u32> {
161 let bytes = self.read_u32()?;
162 Ok(bytes ^ INDEX_U32_KEY ^ !second_key)
163 }
164
165 /// Decrypts the path of a PackedFile from the encrypted index.
166 ///
167 /// This function reads and decrypts a null-terminated string representing the path of a PackedFile.
168 /// The decryption uses the INDEX_STRING_KEY (a 64-byte rotating key) and a second key derived
169 /// from the file's properties.
170 ///
171 /// # Arguments
172 ///
173 /// * `second_key` - The secondary key, typically derived from the file's decrypted size.
174 ///
175 /// # Returns
176 ///
177 /// The decrypted file path as a [`String`], or an error if reading fails.
178 ///
179 /// # Implementation Note
180 ///
181 /// This function reads the encrypted string byte-by-byte until a null terminator (0x00) is found.
182 /// Each byte is XORed with the corresponding byte from INDEX_STRING_KEY (rotating through the 64-byte key)
183 /// and the inverted second_key.
184 ///
185 /// # Examples
186 ///
187 /// ```ignore
188 /// use std::io::Cursor;
189 /// use rpfm_lib::encryption::Decryptable;
190 ///
191 /// let encrypted_index = vec![/* encrypted index bytes */];
192 /// let mut cursor = Cursor::new(encrypted_index);
193 /// let file_size = 1024u8;
194 /// let path = cursor.decrypt_string(file_size)?;
195 /// ```
196 fn decrypt_string(&mut self, second_key: u8) -> Result<String> {
197 let mut path: String = String::new();
198 let mut index = 0;
199 loop {
200 let character = self.read_u8()? ^ INDEX_STRING_KEY[index % INDEX_STRING_KEY.len()] ^ !second_key;
201 index += 1;
202 if character == 0 { break; }
203 path.push(character as char);
204 }
205 Ok(path)
206 }
207}
208
209impl<R: ReadBytes + Read + Seek> Decryptable for R {}