Skip to content

Commit

Permalink
Added higer-level functions that are not in-place
Browse files Browse the repository at this point in the history
  • Loading branch information
pdugre committed Nov 19, 2024
1 parent 7dc8b67 commit f11ae3f
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 52 deletions.
4 changes: 2 additions & 2 deletions XTS.NET.Benchmarks/XTS.NET.Benchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public void Encrypt()

byte[] ciphertext = (byte[])expectedPlaintext.Clone();

aes.EncryptXtsSector(ciphertext, key, sector);
aes.EncryptXtsSectorInPlace(ciphertext, key, sector);
}

[Benchmark]
Expand All @@ -165,6 +165,6 @@ public void Decrypt()

byte[] plaintext = (byte[])expectedCiphertext.Clone();

aes.DecryptXtsSector(plaintext, key, sector);
aes.DecryptXtsSectorInPlace(plaintext, key, sector);
}
}
34 changes: 30 additions & 4 deletions src/XTS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,34 @@ namespace XTS.NET
public static class XTS
{
private const byte GF_MOD = 135;
public static byte[] EncryptXts(this SymmetricAlgorithm alg, ReadOnlySpan<byte> input, ReadOnlySpan<byte> key, BigInteger sectorNum, int sectorSize)
{
byte[] buffer = input.ToArray();
alg.EncryptXtsInPlace(buffer, key, sectorNum, sectorSize);
return buffer;
}

public static byte[] DecryptXts(this SymmetricAlgorithm alg, ReadOnlySpan<byte> input, ReadOnlySpan<byte> key, BigInteger sectorNum, int sectorSize)
{
byte[] buffer = input.ToArray();
alg.DecryptXtsInPlace(buffer, key, sectorNum, sectorSize);
return buffer;
}
public static byte[] EncryptXtsSector(this SymmetricAlgorithm alg, ReadOnlySpan<byte> input, ReadOnlySpan<byte> key, BigInteger sectorNum)
{
byte[] buffer = input.ToArray();
alg.EncryptXtsSectorInPlace(buffer, key, sectorNum);
return buffer;
}

public static byte[] DecryptXtsSector(this SymmetricAlgorithm alg, ReadOnlySpan<byte> input, ReadOnlySpan<byte> key, BigInteger sectorNum)
{
byte[] buffer = input.ToArray();
alg.DecryptXtsSectorInPlace(buffer, key, sectorNum);
return buffer;
}

public static void EncryptXts(this SymmetricAlgorithm alg, byte[] buffer, ReadOnlySpan<byte> key, BigInteger sectorNum, int sectorSize)
public static void EncryptXtsInPlace(this SymmetricAlgorithm alg, byte[] buffer, ReadOnlySpan<byte> key, BigInteger sectorNum, int sectorSize)
{
SetupRawCipher(alg);

Expand Down Expand Up @@ -38,7 +64,7 @@ public static void EncryptXts(this SymmetricAlgorithm alg, byte[] buffer, ReadOn
}
}

public static void DecryptXts(this SymmetricAlgorithm alg, byte[] buffer, ReadOnlySpan<byte> key, BigInteger sectorNum, int sectorSize)
public static void DecryptXtsInPlace(this SymmetricAlgorithm alg, byte[] buffer, ReadOnlySpan<byte> key, BigInteger sectorNum, int sectorSize)
{
SetupRawCipher(alg);

Expand Down Expand Up @@ -68,7 +94,7 @@ public static void DecryptXts(this SymmetricAlgorithm alg, byte[] buffer, ReadOn
}
}

public static void EncryptXtsSector(this SymmetricAlgorithm alg, byte[] buffer, ReadOnlySpan<byte> key, BigInteger sectorNum)
public static void EncryptXtsSectorInPlace(this SymmetricAlgorithm alg, byte[] buffer, ReadOnlySpan<byte> key, BigInteger sectorNum)
{
SetupRawCipher(alg);

Expand All @@ -82,7 +108,7 @@ public static void EncryptXtsSector(this SymmetricAlgorithm alg, byte[] buffer,
ProcessXtsSector(encryptor, buffer, 0, buffer.Length, tweak, false);
}

public static void DecryptXtsSector(this SymmetricAlgorithm alg, byte[] buffer, ReadOnlySpan<byte> key, BigInteger sectorNum)
public static void DecryptXtsSectorInPlace(this SymmetricAlgorithm alg, byte[] buffer, ReadOnlySpan<byte> key, BigInteger sectorNum)
{
SetupRawCipher(alg);

Expand Down
76 changes: 38 additions & 38 deletions tests/IeeeVectorsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public void Vector1Test()

byte[] ciphertext = (byte[])expectedPlaintext.Clone();

aes.EncryptXtsSector(ciphertext, key, sector);
aes.EncryptXtsSectorInPlace(ciphertext, key, sector);
Assert.That(ciphertext, Is.EqualTo(expectedCiphertext));

aes.DecryptXtsSector(ciphertext, key, sector);
aes.DecryptXtsSectorInPlace(ciphertext, key, sector);
Assert.That(ciphertext, Is.EqualTo(expectedPlaintext));
}

Expand Down Expand Up @@ -69,10 +69,10 @@ public void Vector2Test()

byte[] ciphertext = (byte[])expectedPlaintext.Clone();

aes.EncryptXtsSector(ciphertext, key, sector);
aes.EncryptXtsSectorInPlace(ciphertext, key, sector);
Assert.That(ciphertext, Is.EqualTo(expectedCiphertext));

aes.DecryptXtsSector(ciphertext, key, sector);
aes.DecryptXtsSectorInPlace(ciphertext, key, sector);
Assert.That(ciphertext, Is.EqualTo(expectedPlaintext));
}

Expand Down Expand Up @@ -106,10 +106,10 @@ public void Vector3Test()

byte[] ciphertext = (byte[])expectedPlaintext.Clone();

aes.EncryptXtsSector(ciphertext, key, sector);
aes.EncryptXtsSectorInPlace(ciphertext, key, sector);
Assert.That(ciphertext, Is.EqualTo(expectedCiphertext));

aes.DecryptXtsSector(ciphertext, key, sector);
aes.DecryptXtsSectorInPlace(ciphertext, key, sector);
Assert.That(ciphertext, Is.EqualTo(expectedPlaintext));
}

Expand Down Expand Up @@ -263,10 +263,10 @@ public void Vector4Test()

byte[] ciphertext = (byte[])expectedPlaintext.Clone();

aes.EncryptXtsSector(ciphertext, key, sector);
aes.EncryptXtsSectorInPlace(ciphertext, key, sector);
Assert.That(ciphertext, Is.EqualTo(expectedCiphertext));

aes.DecryptXtsSector(ciphertext, key, sector);
aes.DecryptXtsSectorInPlace(ciphertext, key, sector);
Assert.That(ciphertext, Is.EqualTo(expectedPlaintext));
}

Expand Down Expand Up @@ -420,10 +420,10 @@ public void Vector5Test()

byte[] ciphertext = (byte[])expectedPlaintext.Clone();

aes.EncryptXtsSector(ciphertext, key, sector);
aes.EncryptXtsSectorInPlace(ciphertext, key, sector);
Assert.That(ciphertext, Is.EqualTo(expectedCiphertext));

aes.DecryptXtsSector(ciphertext, key, sector);
aes.DecryptXtsSectorInPlace(ciphertext, key, sector);
Assert.That(ciphertext, Is.EqualTo(expectedPlaintext));
}

Expand Down Expand Up @@ -577,10 +577,10 @@ public void Vector6Test()

byte[] ciphertext = (byte[])expectedPlaintext.Clone();

aes.EncryptXtsSector(ciphertext, key, sector);
aes.EncryptXtsSectorInPlace(ciphertext, key, sector);
Assert.That(ciphertext, Is.EqualTo(expectedCiphertext));

aes.DecryptXtsSector(ciphertext, key, sector);
aes.DecryptXtsSectorInPlace(ciphertext, key, sector);
Assert.That(ciphertext, Is.EqualTo(expectedPlaintext));
}

Expand Down Expand Up @@ -734,10 +734,10 @@ public void Vector7Test()

byte[] ciphertext = (byte[])expectedPlaintext.Clone();

aes.EncryptXtsSector(ciphertext, key, sector);
aes.EncryptXtsSectorInPlace(ciphertext, key, sector);
Assert.That(ciphertext, Is.EqualTo(expectedCiphertext));

aes.DecryptXtsSector(ciphertext, key, sector);
aes.DecryptXtsSectorInPlace(ciphertext, key, sector);
Assert.That(ciphertext, Is.EqualTo(expectedPlaintext));
}

Expand Down Expand Up @@ -891,10 +891,10 @@ public void Vector8Test()

byte[] ciphertext = (byte[])expectedPlaintext.Clone();

aes.EncryptXtsSector(ciphertext, key, sector);
aes.EncryptXtsSectorInPlace(ciphertext, key, sector);
Assert.That(ciphertext, Is.EqualTo(expectedCiphertext));

aes.DecryptXtsSector(ciphertext, key, sector);
aes.DecryptXtsSectorInPlace(ciphertext, key, sector);
Assert.That(ciphertext, Is.EqualTo(expectedPlaintext));
}

Expand Down Expand Up @@ -1048,10 +1048,10 @@ public void Vector9Test()

byte[] ciphertext = (byte[])expectedPlaintext.Clone();

aes.EncryptXtsSector(ciphertext, key, sector);
aes.EncryptXtsSectorInPlace(ciphertext, key, sector);
Assert.That(ciphertext, Is.EqualTo(expectedCiphertext));

aes.DecryptXtsSector(ciphertext, key, sector);
aes.DecryptXtsSectorInPlace(ciphertext, key, sector);
Assert.That(ciphertext, Is.EqualTo(expectedPlaintext));
}

Expand Down Expand Up @@ -1209,10 +1209,10 @@ public void Vector10Test()

byte[] ciphertext = (byte[])expectedPlaintext.Clone();

aes.EncryptXtsSector(ciphertext, key, sector);
aes.EncryptXtsSectorInPlace(ciphertext, key, sector);
Assert.That(ciphertext, Is.EqualTo(expectedCiphertext));

aes.DecryptXtsSector(ciphertext, key, sector);
aes.DecryptXtsSectorInPlace(ciphertext, key, sector);
Assert.That(ciphertext, Is.EqualTo(expectedPlaintext));
}

Expand Down Expand Up @@ -1370,10 +1370,10 @@ public void Vector11Test()

byte[] ciphertext = (byte[])expectedPlaintext.Clone();

aes.EncryptXtsSector(ciphertext, key, sector);
aes.EncryptXtsSectorInPlace(ciphertext, key, sector);
Assert.That(ciphertext, Is.EqualTo(expectedCiphertext));

aes.DecryptXtsSector(ciphertext, key, sector);
aes.DecryptXtsSectorInPlace(ciphertext, key, sector);
Assert.That(ciphertext, Is.EqualTo(expectedPlaintext));
}

Expand Down Expand Up @@ -1531,10 +1531,10 @@ public void Vector12Test()

byte[] ciphertext = (byte[])expectedPlaintext.Clone();

aes.EncryptXtsSector(ciphertext, key, sector);
aes.EncryptXtsSectorInPlace(ciphertext, key, sector);
Assert.That(ciphertext, Is.EqualTo(expectedCiphertext));

aes.DecryptXtsSector(ciphertext, key, sector);
aes.DecryptXtsSectorInPlace(ciphertext, key, sector);
Assert.That(ciphertext, Is.EqualTo(expectedPlaintext));
}

Expand Down Expand Up @@ -1692,10 +1692,10 @@ public void Vector13Test()

byte[] ciphertext = (byte[])expectedPlaintext.Clone();

aes.EncryptXtsSector(ciphertext, key, sector);
aes.EncryptXtsSectorInPlace(ciphertext, key, sector);
Assert.That(ciphertext, Is.EqualTo(expectedCiphertext));

aes.DecryptXtsSector(ciphertext, key, sector);
aes.DecryptXtsSectorInPlace(ciphertext, key, sector);
Assert.That(ciphertext, Is.EqualTo(expectedPlaintext));
}

Expand Down Expand Up @@ -1853,10 +1853,10 @@ public void Vector14Test()

byte[] ciphertext = (byte[])expectedPlaintext.Clone();

aes.EncryptXtsSector(ciphertext, key, sector);
aes.EncryptXtsSectorInPlace(ciphertext, key, sector);
Assert.That(ciphertext, Is.EqualTo(expectedCiphertext));

aes.DecryptXtsSector(ciphertext, key, sector);
aes.DecryptXtsSectorInPlace(ciphertext, key, sector);
Assert.That(ciphertext, Is.EqualTo(expectedPlaintext));
}

Expand Down Expand Up @@ -1888,10 +1888,10 @@ public void Vector15Test()

byte[] ciphertext = (byte[])expectedPlaintext.Clone();

aes.EncryptXtsSector(ciphertext, key, sector);
aes.EncryptXtsSectorInPlace(ciphertext, key, sector);
Assert.That(ciphertext, Is.EqualTo(expectedCiphertext));

aes.DecryptXtsSector(ciphertext, key, sector);
aes.DecryptXtsSectorInPlace(ciphertext, key, sector);
Assert.That(ciphertext, Is.EqualTo(expectedPlaintext));
}

Expand Down Expand Up @@ -1923,10 +1923,10 @@ public void Vector16Test()

byte[] ciphertext = (byte[])expectedPlaintext.Clone();

aes.EncryptXtsSector(ciphertext, key, sector);
aes.EncryptXtsSectorInPlace(ciphertext, key, sector);
Assert.That(ciphertext, Is.EqualTo(expectedCiphertext));

aes.DecryptXtsSector(ciphertext, key, sector);
aes.DecryptXtsSectorInPlace(ciphertext, key, sector);
Assert.That(ciphertext, Is.EqualTo(expectedPlaintext));
}

Expand Down Expand Up @@ -1958,10 +1958,10 @@ public void Vector17Test()

byte[] ciphertext = (byte[])expectedPlaintext.Clone();

aes.EncryptXtsSector(ciphertext, key, sector);
aes.EncryptXtsSectorInPlace(ciphertext, key, sector);
Assert.That(ciphertext, Is.EqualTo(expectedCiphertext));

aes.DecryptXtsSector(ciphertext, key, sector);
aes.DecryptXtsSectorInPlace(ciphertext, key, sector);
Assert.That(ciphertext, Is.EqualTo(expectedPlaintext));
}

Expand Down Expand Up @@ -1993,10 +1993,10 @@ public void Vector18Test()

byte[] ciphertext = (byte[])expectedPlaintext.Clone();

aes.EncryptXtsSector(ciphertext, key, sector);
aes.EncryptXtsSectorInPlace(ciphertext, key, sector);
Assert.That(ciphertext, Is.EqualTo(expectedCiphertext));

aes.DecryptXtsSector(ciphertext, key, sector);
aes.DecryptXtsSectorInPlace(ciphertext, key, sector);
Assert.That(ciphertext, Is.EqualTo(expectedPlaintext));
}

Expand Down Expand Up @@ -2150,10 +2150,10 @@ public void Vector19Test()

byte[] ciphertext = (byte[])expectedPlaintext.Clone();

aes.EncryptXtsSector(ciphertext, key, sector);
aes.EncryptXtsSectorInPlace(ciphertext, key, sector);
Assert.That(ciphertext, Is.EqualTo(expectedCiphertext));

aes.DecryptXtsSector(ciphertext, key, sector);
aes.DecryptXtsSectorInPlace(ciphertext, key, sector);
Assert.That(ciphertext, Is.EqualTo(expectedPlaintext));
}

Expand Down
16 changes: 8 additions & 8 deletions tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public void EncryptDecryptTest()
Aes cipher = Aes.Create();

byte[] ciphertext = (byte[])expectedPlaintext.Clone();
cipher.EncryptXts(ciphertext, key, 0, 520);
cipher.EncryptXtsInPlace(ciphertext, key, 0, 520);

// Ensures some form of data transformation happened
Assert.That(ciphertext, Is.Not.EqualTo(expectedPlaintext));

cipher.DecryptXts(ciphertext, key, 0, 520);
cipher.DecryptXtsInPlace(ciphertext, key, 0, 520);

Assert.That(ciphertext, Is.EqualTo(expectedPlaintext));
}
Expand All @@ -54,12 +54,12 @@ public void EncryptDecrypt256Test()
Aes cipher = Aes.Create();

byte[] ciphertext = (byte[])expectedPlaintext.Clone();
cipher.EncryptXts(ciphertext, key, 0, 520);
cipher.EncryptXtsInPlace(ciphertext, key, 0, 520);

// Ensures some form of data transformation happened
Assert.That(ciphertext, Is.Not.EqualTo(expectedPlaintext));

cipher.DecryptXts(ciphertext, key, 0, 520);
cipher.DecryptXtsInPlace(ciphertext, key, 0, 520);

Assert.That(ciphertext, Is.EqualTo(expectedPlaintext));
}
Expand All @@ -80,12 +80,12 @@ public void NoStealingTestCase()

byte[] ciphertext = (byte[])expectedPlaintext.Clone();

cipher.EncryptXts(ciphertext, key, 0, 520);
cipher.EncryptXtsInPlace(ciphertext, key, 0, 520);

// Ensures some form of data transformation happened
Assert.That(ciphertext, Is.Not.EqualTo(expectedPlaintext));

cipher.DecryptXts(ciphertext, key, 0, 520);
cipher.DecryptXtsInPlace(ciphertext, key, 0, 520);

Assert.That(ciphertext, Is.EqualTo(expectedPlaintext));
}
Expand All @@ -106,12 +106,12 @@ public void ExactSectorTestCase()

byte[] ciphertext = (byte[])expectedPlaintext.Clone();

cipher.EncryptXts(ciphertext, key, 0, 100);
cipher.EncryptXtsInPlace(ciphertext, key, 0, 100);

// Ensures some form of data transformation happened
Assert.That(ciphertext, Is.Not.EqualTo(expectedPlaintext));

cipher.DecryptXts(ciphertext, key, 0, 100);
cipher.DecryptXtsInPlace(ciphertext, key, 0, 100);

Assert.That(ciphertext, Is.EqualTo(expectedPlaintext));
}
Expand Down

0 comments on commit f11ae3f

Please sign in to comment.