@@ -17,6 +17,14 @@ struct Header {
17
17
uint8_t reserved[14 ];
18
18
};
19
19
20
+ template <typename T> static inline T FixEndianness (T val) { return val; }
21
+
22
+ #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
23
+ template <> static inline uint16_t FixEndianness (uint16_t val) { return __builtin_bswap16 (val); }
24
+ template <> static inline uint32_t FixEndianness (uint32_t val) { return __builtin_bswap32 (val); }
25
+ template <> static inline uint64_t FixEndianness (uint64_t val) { return __builtin_bswap64 (val); }
26
+ #endif
27
+
20
28
static const char * MMapEncryptedFileErrors[] = {
21
29
" " ,
22
30
" Encrypted file doesn't have a full header" ,
@@ -130,7 +138,7 @@ class MMapEncryptedFile
130
138
131
139
inline size_t size () const {
132
140
file_.assertFileIsOpen ();
133
- return reinterpret_cast <Header*>(file_.data ())->size ;
141
+ return FixEndianness ( reinterpret_cast <Header*>(file_.data ())->size );
134
142
}
135
143
136
144
inline size_t capacity () const { return file_.size () - sizeof (Header); }
@@ -146,13 +154,13 @@ class MMapEncryptedFile
146
154
}
147
155
148
156
if (newSize < size ()) {
149
- // the order is important here to avoid incosistencies
150
- reinterpret_cast <Header*>(file_.data ())->size = newSize;
157
+ // the order is important here to avoid inconsistencies
158
+ reinterpret_cast <Header*>(file_.data ())->size = FixEndianness (( uint64_t ) newSize) ;
151
159
file_.resize (newSize + sizeof (Header), strictResize);
152
160
} else {
153
- // the order is important here to avoid incosistencies
161
+ // the order is important here to avoid inconsistencies
154
162
file_.resize (newSize + sizeof (Header), strictResize);
155
- reinterpret_cast <Header*>(file_.data ())->size = newSize;
163
+ reinterpret_cast <Header*>(file_.data ())->size = FixEndianness (( uint64_t ) newSize) ;
156
164
}
157
165
}
158
166
@@ -175,21 +183,21 @@ class MMapEncryptedFile
175
183
}
176
184
177
185
Header* header = reinterpret_cast <Header*>(file_.data ());
178
- if (header->magic != MAGIC) {
186
+ if (FixEndianness ( header->magic ) != MAGIC) {
179
187
return 2 ;
180
188
}
181
- if (header->version != 1 ) {
189
+ if (FixEndianness ( header->version ) != 1 ) {
182
190
return 3 ;
183
191
}
184
- if (header->size + sizeof (Header) > file_.size ()) {
192
+ if (FixEndianness ( header->size ) + sizeof (Header) > file_.size ()) {
185
193
return 4 ;
186
194
}
187
195
188
196
// decrypt data key
189
197
uint8_t dataKey[16 ];
190
198
aes_.decryptBlock (header->encryptedDataKey , dataKey);
191
199
192
- if (header->keyHash != hashIVAndKey (header->iv , dataKey)) {
200
+ if (FixEndianness ( header->keyHash ) != hashIVAndKey (header->iv , dataKey)) {
193
201
return 5 ;
194
202
}
195
203
@@ -201,8 +209,8 @@ class MMapEncryptedFile
201
209
file_.resize (sizeof (Header), true );
202
210
uint8_t dataKey[16 ];
203
211
Header* header = reinterpret_cast <Header*>(file_.data ());
204
- header->magic = MAGIC;
205
- header->version = 1 ;
212
+ header->magic = FixEndianness ( MAGIC) ;
213
+ header->version = FixEndianness (( uint16_t ) 1 ) ;
206
214
header->size = 0 ;
207
215
// generate a random data key and encrypt it
208
216
fillRandom (dataKey, 16 );
@@ -211,7 +219,7 @@ class MMapEncryptedFile
211
219
// generate a random IV
212
220
fillRandom (header->iv , 16 );
213
221
// calculate the hash of the IV and the data key
214
- header->keyHash = hashIVAndKey (header->iv , dataKey);
222
+ header->keyHash = FixEndianness ( hashIVAndKey (header->iv , dataKey) );
215
223
}
216
224
};
217
225
0 commit comments