Skip to content

Commit

Permalink
BSC-01-008
Browse files Browse the repository at this point in the history
- Checksum prefix is now INSIDE the checksum and useful ...
  • Loading branch information
bitbeans committed May 4, 2015
1 parent 41edf20 commit 79ff751
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 25 deletions.
14 changes: 6 additions & 8 deletions StreamCryptor/Model/EncryptedFileChunk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,9 @@ public class EncryptedFileChunk
/// <param name="chunkChecksumLength">The length of the checksum.</param>
public void SetChunkChecksum(byte[] ephemeralKey, int chunkChecksumLength)
{
ChunkChecksum = ArrayHelpers.ConcatArrays(_checksumChunkPrefix,
GenericHash.Hash(ArrayHelpers.ConcatArrays(Chunk,
Utils.IntegerToLittleEndian(ChunkLength)),
Utils.GetEphemeralHashKey(ephemeralKey), chunkChecksumLength));
ChunkChecksum = GenericHash.Hash(ArrayHelpers.ConcatArrays(_checksumChunkPrefix, Chunk,
Utils.IntegerToLittleEndian(ChunkLength)),
Utils.GetEphemeralHashKey(ephemeralKey), chunkChecksumLength);
}

/// <summary>
Expand All @@ -58,10 +57,9 @@ public void SetChunkChecksum(byte[] ephemeralKey, int chunkChecksumLength)
/// <exception cref="BadFileChunkException"></exception>
public void ValidateChunkChecksum(byte[] ephemeralKey, int chunkChecksumLength)
{
var chunkChecksum = ArrayHelpers.ConcatArrays(_checksumChunkPrefix,
GenericHash.Hash(
ArrayHelpers.ConcatArrays(Chunk, Utils.IntegerToLittleEndian(ChunkLength)),
Utils.GetEphemeralHashKey(ephemeralKey), chunkChecksumLength));
var chunkChecksum = GenericHash.Hash(
ArrayHelpers.ConcatArrays(_checksumChunkPrefix, Chunk, Utils.IntegerToLittleEndian(ChunkLength)),
Utils.GetEphemeralHashKey(ephemeralKey), chunkChecksumLength);
if (!chunkChecksum.SequenceEqual(ChunkChecksum))
{
throw new BadFileChunkException("Wrong checksum, file could be damaged or manipulated!");
Expand Down
12 changes: 6 additions & 6 deletions StreamCryptor/Model/EncryptedFileFooter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public void SetFooterChecksum(byte[] chunkCount, byte[] chunkOverallLength, byte
int footerChecksumLength)
{
//generate and set the Footerchecksum
FooterChecksum = ArrayHelpers.ConcatArrays(_checksumFooterPrefix,
GenericHash.Hash(ArrayHelpers.ConcatArrays(chunkCount, chunkOverallLength),
Utils.GetEphemeralHashKey(ephemeralKey), footerChecksumLength));
FooterChecksum = GenericHash.Hash(ArrayHelpers.ConcatArrays(_checksumFooterPrefix,
chunkCount, chunkOverallLength),
Utils.GetEphemeralHashKey(ephemeralKey), footerChecksumLength);
}

/// <summary>
Expand All @@ -46,10 +46,10 @@ public void SetFooterChecksum(byte[] chunkCount, byte[] chunkOverallLength, byte
public void ValidateFooterChecksum(byte[] chunkCount, byte[] chunkOverallLength, byte[] ephemeralKey,
int footerChecksumLength)
{
var footerChecksum = ArrayHelpers.ConcatArrays(_checksumFooterPrefix, GenericHash.Hash(
ArrayHelpers.ConcatArrays(chunkCount, chunkOverallLength),
var footerChecksum = GenericHash.Hash(
ArrayHelpers.ConcatArrays(_checksumFooterPrefix, chunkCount, chunkOverallLength),
Utils.GetEphemeralHashKey(ephemeralKey),
footerChecksumLength));
footerChecksumLength);
//check the file footer
if (!footerChecksum.SequenceEqual(FooterChecksum))
{
Expand Down
21 changes: 10 additions & 11 deletions StreamCryptor/Model/EncryptedFileHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,13 @@ public void ProtectFileName(string fileName, int fileNameLength)
/// <param name="headerChecksumLength">The length of the checksum.</param>
public void SetHeaderChecksum(int headerChecksumLength)
{
HeaderChecksum = ArrayHelpers.ConcatArrays(_checksumHeaderPrefix,
GenericHash.Hash(ArrayHelpers.ConcatArrays(BaseNonce,
FilenameNonce, Filename,
Utils.IntegerToLittleEndian(Version),
Key,
BitConverter.GetBytes(UnencryptedFileLength)),
Utils.GetEphemeralHashKey(UnencryptedEphemeralKey),
headerChecksumLength));
HeaderChecksum = GenericHash.Hash(ArrayHelpers.ConcatArrays(_checksumHeaderPrefix, BaseNonce,
FilenameNonce, Filename,
Utils.IntegerToLittleEndian(Version),
Key,
BitConverter.GetBytes(UnencryptedFileLength)),
Utils.GetEphemeralHashKey(UnencryptedEphemeralKey),
headerChecksumLength);
}

/// <summary>
Expand All @@ -151,14 +150,14 @@ public void SetHeaderChecksum(int headerChecksumLength)
/// <exception cref="BadFileHeaderException"></exception>
public void ValidateHeaderChecksum(byte[] ephemeralKey, int headerChecksumLength)
{
var headerChecksum = ArrayHelpers.ConcatArrays(_checksumHeaderPrefix, GenericHash.Hash(
ArrayHelpers.ConcatArrays(BaseNonce,
var headerChecksum = GenericHash.Hash(
ArrayHelpers.ConcatArrays(_checksumHeaderPrefix, BaseNonce,
FilenameNonce, Filename,
Utils.IntegerToLittleEndian(Version),
Key,
BitConverter.GetBytes(UnencryptedFileLength)),
Utils.GetEphemeralHashKey(ephemeralKey),
headerChecksumLength));
headerChecksumLength);
if (!headerChecksum.SequenceEqual(HeaderChecksum))
{
throw new BadFileHeaderException("Malformed file header: file could be damaged or manipulated!");
Expand Down

0 comments on commit 79ff751

Please sign in to comment.