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 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
|
// Copyright (c) 2012-2014 Konstantin Isakov <ikm@zbackup.org>
// Part of ZBackup. Licensed under GNU GPLv2 or later + OpenSSL, see LICENSE
#include <string.h>
#include <algorithm>
#include "check.hh"
#include "encrypted_file.hh"
#include "endian.hh"
#include "page_size.hh"
#include "random.hh"
namespace EncryptedFile {
using Encryption::BlockSize;
InputStream::InputStream( char const * fileName, EncryptionKey const & key,
void const * iv_ ):
file( fileName, UnbufferedFile::ReadOnly ), filePos( 0 ), key( key ),
// Our buffer must be larger than BlockSize, as otherwise we won't be able
// to handle PKCS#7 padding properly
buffer( std::max( getPageSize(), ( unsigned ) BlockSize * 2 ) ),
fill( 0 ), remainder( 0 ), backedUp( false )
{
if ( key.hasKey() )
{
memcpy( iv, iv_, sizeof( iv ) );
// Since we use padding, file size should be evenly dividable by the cipher
// block size, and we should have at least one block
UnbufferedFile::Offset size = file.size();
if ( !size || size % BlockSize )
throw exIncorrectFileSize();
}
}
bool InputStream::Next( void const ** data, int * size )
{
// If we backed up, return the unconsumed data
if ( backedUp )
backedUp = false;
else
{
try
{
// Update adler32 for the previous block
adler32.add( start, fill );
// Read more data
if ( filePos && !remainder )
{
// Once we're read a full block, we always have a remainder. If not,
// this means we've hit the end of file already
fill = 0;
return false;
}
// If we have a remainder, move it to the beginning of buffer and make
// it start the next block
memmove( buffer.data(), start + fill, remainder );
start = buffer.data();
fill = file.read( start + remainder, buffer.size() - remainder ) +
remainder;
// remainder should techically be 0 now, but decrypt() will update it
// anyway
// remainder = 0;
decrypt();
}
catch( UnbufferedFile::exReadError & )
{
fill = 0; // To make sure state is remaining consistent
return false;
}
}
*data = start;
*size = fill;
filePos += fill;
return *size;
}
void InputStream::BackUp( int count )
{
CHECK( count >= 0, "count is negative" );
if ( !backedUp )
{
CHECK( (size_t) count <= fill, "Backing up too much" );
size_t consumed = fill - count;
adler32.add( start, consumed );
start += consumed;
fill = count;
filePos -= count;
backedUp = fill; // Don't make the next Next() return 0 bytes
}
else
{
CHECK( count == 0, "backing up after being backed up already" );
}
}
bool InputStream::Skip( int count )
{
CHECK( count >= 0, "count is negative" );
// We always need to read and decrypt data, as otherwise both the state of
// CBC and adler32 would be incorrect
void const * data;
int size;
while( count )
{
if ( !Next( &data, &size ) )
return false;
else
if ( size > count )
{
BackUp( size - count );
break;
}
else
count -= size;
}
return true;
}
int64_t InputStream::ByteCount() const
{
return filePos;
}
Adler32::Value InputStream::getAdler32()
{
// This makes all data consumed, if not already
BackUp( 0 );
return adler32.result();
}
void InputStream::read( void * buf, size_t size )
{
void const * data;
int avail;
char * n = ( char * ) buf;
while( size )
{
if ( !Next( &data, &avail ) )
throw exReadFailed();
else
if ( avail > ( ssize_t ) size )
{
memcpy( n, data, size );
BackUp( avail - size );
break;
}
else
{
memcpy( n, data, avail );
n += avail;
size -= avail;
}
}
}
void InputStream::checkAdler32()
{
Adler32::Value ours = getAdler32();
Adler32::Value r;
read( &r, sizeof( r ) );
if ( ours != fromLittleEndian( r ) )
throw exAdlerMismatch();
}
void InputStream::consumeRandomIv()
{
if ( key.hasKey() )
{
char iv[ Encryption::IvSize ];
read( iv, sizeof( iv ) ); // read() can throw exceptions, Skip() can't
}
}
void InputStream::decrypt()
{
if ( fill == buffer.size() )
{
// When we have the full buffer, we set the last block of it aside and
// treat the rest as the normal CBC sequence. The last block in the buffer
// may be the last block of file, in which case we would need to handle
// padding. That may happen the next time the function is called
remainder = BlockSize;
fill -= BlockSize;
doDecrypt();
}
else
{
// This is an end of file. Decrypt data treating the last block being
// padded
// Since we always have padding in the file and the last block is always
// set apart when reading full buffers, we must have at least one block
// to decrypt here
doDecrypt();
// Unpad the last block
if ( key.hasKey() )
fill -= BlockSize - Encryption::unpad( start + fill - BlockSize );
// We have not left any remainder this time
remainder = 0;
}
}
void InputStream::doDecrypt()
{
if ( !key.hasKey() )
return;
// Since we use padding, file size should be evenly dividable by the cipher's
// block size, and we should always have at least one block. When we get here,
// we would always get the proper fill value unless those characteristics are
// not met. We check for the same condition on construction, but the file
// size can change while we are reading it
// We don't throw an exception here as the interface we implement doesn't
// support them
CHECK( fill > 0 && !( fill % BlockSize ), "incorrect size of the encrypted "
"file - must be non-zero and in multiples of %u",
( unsigned ) BlockSize );
// Copy the next iv prior to decrypting the data in place, as it will
// not be available afterwards
char newIv[ Encryption::IvSize ];
memcpy( newIv, Encryption::getNextDecryptionIv( start, fill ),
sizeof( newIv ) );
// Decrypt the data
Encryption::decrypt( iv, key.getKey(), start, start, fill );
// Copy the new iv
memcpy( iv, newIv, sizeof( iv ) );
}
OutputStream::OutputStream( char const * fileName, EncryptionKey const & key,
void const * iv_ ):
file( fileName, UnbufferedFile::WriteOnly ), filePos( 0 ), key( key ),
buffer( getPageSize() ), start( buffer.data() ), avail( 0 ), backedUp( false )
{
if ( key.hasKey() )
memcpy( iv, iv_, sizeof( iv ) );
}
bool OutputStream::Next( void ** data, int * size )
{
// If we backed up, return the unconsumed data
if ( backedUp )
backedUp = false;
else
{
try
{
// Update adler32 for the previous block
adler32.add( start, avail );
// Encrypt and write the buffer if it had data
if ( filePos )
encryptAndWrite( buffer.size() );
start = buffer.data();
avail = buffer.size();
}
catch( UnbufferedFile::exWriteError & )
{
avail = 0; // To make sure state is remaining consistent
return false;
}
}
*data = start;
*size = avail;
filePos += avail;
return *size;
}
void OutputStream::BackUp( int count )
{
CHECK( count >= 0, "count is negative" );
if ( !backedUp )
{
CHECK( (size_t) count <= avail, "Backing up too much" );
size_t consumed = avail - count;
adler32.add( start, consumed );
start += consumed;
avail = count;
filePos -= count;
backedUp = avail; // Don't make the next Next() return 0 bytes
}
else
{
CHECK( count == 0, "backing up after being backed up already" );
}
}
int64_t OutputStream::ByteCount() const
{
return filePos;
}
Adler32::Value OutputStream::getAdler32()
{
// This makes all data consumed, if not already
BackUp( 0 );
return adler32.result();
}
void OutputStream::write( void const * buf, size_t size )
{
void * data;
int avail;
char const * n = ( char const * ) buf;
while( size )
{
if ( !Next( &data, &avail ) )
throw exReadFailed();
else
if ( avail > ( ssize_t ) size )
{
memcpy( data, n, size );
BackUp( avail - size );
break;
}
else
{
memcpy( data, n, avail );
n += avail;
size -= avail;
}
}
}
void OutputStream::writeAdler32()
{
Adler32::Value v = toLittleEndian( getAdler32() );
write( &v, sizeof( v ) );
}
void OutputStream::writeRandomIv()
{
if ( key.hasKey() )
{
char iv[ Encryption::IvSize ];
Random::genaratePseudo( iv, sizeof( iv ) );
write( iv, sizeof( iv ) );
}
}
void OutputStream::encryptAndWrite( size_t bytes )
{
if ( key.hasKey() )
{
CHECK( bytes > 0 && !( bytes % BlockSize ), "incorrect number of bytes to "
"encrypt and write - must be non-zero and in multiples of %u",
( unsigned ) BlockSize );
void const * nextIv = Encryption::encrypt( iv, key.getKey(), buffer.data(),
buffer.data(), bytes );
memcpy( iv, nextIv, sizeof( iv ) );
}
file.write( buffer.data(), bytes );
}
OutputStream::~OutputStream()
{
// This makes all data consumed, if not already
BackUp( 0 );
// If we have the full buffer, write it first
if ( start == buffer.data() + buffer.size() )
{
encryptAndWrite( buffer.size() );
start = buffer.data();
}
size_t bytesToWrite = start - buffer.data();
if ( key.hasKey() )
{
// Perform padding
size_t remainderSize = bytesToWrite % BlockSize;
Encryption::pad( start - remainderSize, remainderSize );
bytesToWrite += BlockSize - remainderSize;
}
encryptAndWrite( bytesToWrite );
}
}
|