General code cleanup and style refactor.

This commit is contained in:
2022-11-15 15:58:41 +00:00
parent 42e2251131
commit 8e64a9f10c
4 changed files with 109 additions and 111 deletions

View File

@@ -43,12 +43,12 @@
// libdvdcpxm (https://offog.org/git/dvdaexplorer/src/libdvdcpxm/)
// libdvdcss (https://www.videolan.org/developers/libdvdcss.html)
namespace Aaru.Decryption.DVD;
using System;
using System.Linq;
using Aaru.Decoders.DVD;
namespace Aaru.Decryption.DVD;
public class CSS
{
static readonly byte[,] _playerKeys =
@@ -386,7 +386,7 @@ public class CSS
byte[] key = response.Skip(4).Take(2048).ToArray();
for(uint i = 0; i < key.Length; i++)
key[i] ^= busKey[4 - i % busKey.Length];
key[i] ^= busKey[4 - (i % busKey.Length)];
return new CSS_CPRM.DiscKey
{
@@ -413,7 +413,7 @@ public class CSS
byte[] key = response.Skip(5).Take(5).ToArray();
for(uint i = 0; i < key.Length; i++)
key[i] ^= busKey[4 - i % busKey.Length];
key[i] ^= busKey[4 - (i % busKey.Length)];
return new CSS_CPRM.TitleKey
{
@@ -435,38 +435,38 @@ public class CSS
/// <returns>The encrypted key.</returns>
public static void EncryptKey(DvdCssKeyType keyType, uint variant, byte[] challenge, out byte[] key)
{
var bits = new byte[30];
var scratch = new byte[10];
byte[] bits = new byte[30];
byte[] scratch = new byte[10];
byte index = sizeof(byte) * 30;
var temp1 = new byte[5];
var temp2 = new byte[5];
byte[] temp1 = new byte[5];
byte[] temp2 = new byte[5];
byte carry = 0;
key = new byte[5];
for(var i = 9; i >= 0; --i)
for(int i = 9; i >= 0; --i)
scratch[i] = challenge[_permutationChallenge[(uint)keyType, i]];
var cssVariant = (byte)(keyType == 0 ? variant : _permutationVariant[(uint)keyType - 1, variant]);
byte cssVariant = (byte)(keyType == 0 ? variant : _permutationVariant[(uint)keyType - 1, variant]);
for(var i = 5; --i >= 0;)
for(int i = 5; --i >= 0;)
temp1[i] = (byte)(scratch[5 + i] ^ _secret[i] ^ _encryptTable2[i]);
var lfsr0 = (uint)((temp1[0] << 17) | (temp1[1] << 9) | ((temp1[2] & ~7) << 1) | 8 | (temp1[2] & 7));
var lfsr1 = (uint)((temp1[3] << 9) | 0x100 | temp1[4]);
uint lfsr0 = (uint)((temp1[0] << 17) | (temp1[1] << 9) | ((temp1[2] & ~7) << 1) | 8 | (temp1[2] & 7));
uint lfsr1 = (uint)((temp1[3] << 9) | 0x100 | temp1[4]);
do
{
byte val = 0;
for(var bit = 0; bit < 8; ++bit)
for(int bit = 0; bit < 8; ++bit)
{
var oLfsr0 = (byte)(((lfsr0 >> 24) ^ (lfsr0 >> 21) ^ (lfsr0 >> 20) ^ (lfsr0 >> 12)) & 1);
byte oLfsr0 = (byte)(((lfsr0 >> 24) ^ (lfsr0 >> 21) ^ (lfsr0 >> 20) ^ (lfsr0 >> 12)) & 1);
lfsr0 = (lfsr0 << 1) | oLfsr0;
var oLfsr1 = (byte)(((lfsr1 >> 16) ^ (lfsr1 >> 2)) & 1);
byte oLfsr1 = (byte)(((lfsr1 >> 16) ^ (lfsr1 >> 2)) & 1);
lfsr1 = (lfsr1 << 1) | oLfsr1;
var combined = (byte)(Convert.ToByte(oLfsr1 == 0) + carry + Convert.ToByte(oLfsr0 == 0));
byte combined = (byte)(Convert.ToByte(oLfsr1 == 0) + carry + Convert.ToByte(oLfsr0 == 0));
carry = (byte)((combined >> 1) & 1);
val |= (byte)((combined & 1) << bit);
}
@@ -474,10 +474,10 @@ public class CSS
bits[--index] = val;
} while(index > 0);
var cse = (byte)(_variants[cssVariant] ^ _encryptTable2[cssVariant]);
var term = 0;
byte cse = (byte)(_variants[cssVariant] ^ _encryptTable2[cssVariant]);
int term = 0;
for(var i = 5; --i >= 0; term = scratch[i])
for(int i = 5; --i >= 0; term = scratch[i])
{
index = (byte)(bits[25 + i] ^ scratch[i]);
index = (byte)(_encryptTable1[index] ^ ~_encryptTable2[index] ^ cse);
@@ -488,7 +488,7 @@ public class CSS
temp1[4] ^= temp1[0];
term = 0;
for(var i = 5; --i >= 0; term = temp1[i])
for(int i = 5; --i >= 0; term = temp1[i])
{
index = (byte)(bits[20 + i] ^ temp1[i]);
index = (byte)(_encryptTable1[index] ^ ~_encryptTable2[index] ^ cse);
@@ -499,7 +499,7 @@ public class CSS
temp2[4] ^= temp2[0];
term = 0;
for(var i = 5; --i >= 0; term = temp2[i])
for(int i = 5; --i >= 0; term = temp2[i])
{
index = (byte)(bits[15 + i] ^ temp2[i]);
index = (byte)(_encryptTable1[index] ^ ~_encryptTable2[index] ^ cse);
@@ -511,7 +511,7 @@ public class CSS
temp1[4] ^= temp1[0];
term = 0;
for(var i = 5; --i >= 0; term = temp1[i])
for(int i = 5; --i >= 0; term = temp1[i])
{
index = (byte)(bits[10 + i] ^ temp1[i]);
index = (byte)(_encryptTable1[index] ^ ~_encryptTable2[index] ^ cse);
@@ -523,7 +523,7 @@ public class CSS
temp2[4] ^= temp2[0];
term = 0;
for(var i = 5; --i >= 0; term = temp2[i])
for(int i = 5; --i >= 0; term = temp2[i])
{
index = (byte)(bits[5 + i] ^ temp2[i]);
index = (byte)(_encryptTable1[index] ^ ~_encryptTable2[index] ^ cse);
@@ -534,7 +534,7 @@ public class CSS
temp1[4] ^= temp1[0];
term = 0;
for(var i = 5; --i >= 0; term = temp1[i])
for(int i = 5; --i >= 0; term = temp1[i])
{
index = (byte)(bits[i] ^ temp1[i]);
index = (byte)(_encryptTable1[index] ^ ~_encryptTable2[index] ^ cse);
@@ -551,12 +551,13 @@ public class CSS
public static void DecryptKey(byte invert, byte[] cryptoKey, byte[] encryptedKey, out byte[] decryptedKey)
{
decryptedKey = new byte[5];
var k = new byte[5];
byte[] k = new byte[5];
var lfsr1Lo = (uint)(cryptoKey[0] | 0x100);
uint lfsr1Lo = (uint)(cryptoKey[0] | 0x100);
uint lfsr1Hi = cryptoKey[1];
var lfsr0 = (uint)(((cryptoKey[4] << 17) | (cryptoKey[3] << 9) | (cryptoKey[2] << 1)) + 8 - (cryptoKey[2] & 7));
uint lfsr0 = (uint)(((cryptoKey[4] << 17) | (cryptoKey[3] << 9) | (cryptoKey[2] << 1)) + 8 -
(cryptoKey[2] & 7));
lfsr0 = (uint)((_cssTable4[lfsr0 & 0xff] << 24) | (_cssTable4[(lfsr0 >> 8) & 0xff] << 16) |
(_cssTable4[(lfsr0 >> 16) & 0xff] << 8) | _cssTable4[(lfsr0 >> 24) & 0xff]);
@@ -565,11 +566,11 @@ public class CSS
for(uint i = 0; i < 5; i++)
{
var oLfsr1 = (byte)(_cssTable2[lfsr1Hi] ^ _cssTable3[lfsr1Lo]);
byte oLfsr1 = (byte)(_cssTable2[lfsr1Hi] ^ _cssTable3[lfsr1Lo]);
lfsr1Hi = lfsr1Lo >> 1;
lfsr1Lo = ((lfsr1Lo & 1) << 8) ^ oLfsr1;
oLfsr1 = _cssTable4[oLfsr1];
var oLfsr0 = (byte)(((((((lfsr0 >> 8) ^ lfsr0) >> 1) ^ lfsr0) >> 3) ^ lfsr0) >> 7);
byte oLfsr0 = (byte)(((((((lfsr0 >> 8) ^ lfsr0) >> 1) ^ lfsr0) >> 3) ^ lfsr0) >> 7);
lfsr0 = (lfsr0 >> 8) | ((uint)oLfsr0 << 24);
combined += (uint)((oLfsr0 ^ invert) + oLfsr1);
k[i] = (byte)(combined & 0xff);
@@ -636,7 +637,7 @@ public class CSS
keyData.All(k => k == 0))
return sectorData;
var decryptedBuffer = new byte[sectorData.Length];
byte[] decryptedBuffer = new byte[sectorData.Length];
for(uint j = 0; j < blocks; j++)
{
@@ -659,11 +660,11 @@ public class CSS
uint lfsr1Lo = (uint)(currentKey[0] ^ currentSector[0x54]) | 0x100;
uint lfsr1Hi = (uint)currentKey[1] ^ currentSector[0x55];
var lfsr0 = (uint)((currentKey[2] | (currentKey[3] << 8) | (currentKey[4] << 16)) ^
uint lfsr0 = (uint)((currentKey[2] | (currentKey[3] << 8) | (currentKey[4] << 16)) ^
(sectorData[0x56] | (sectorData[0x57] << 8) | (sectorData[0x58] << 16)));
uint oLfsr1 = lfsr0 & 7;
lfsr0 = lfsr0 * 2 + 8 - oLfsr1;
lfsr0 = (lfsr0 * 2) + 8 - oLfsr1;
uint combined = 0;
@@ -698,13 +699,13 @@ public class CSS
if(cmi.RegionInformation is 0xFF or 0x00)
return true;
return (rpc.RegionMask & 0x01) == (cmi.RegionInformation & 0x01) && (rpc.RegionMask & 0x01) != 0x01 ||
(rpc.RegionMask & 0x02) == (cmi.RegionInformation & 0x02) && (rpc.RegionMask & 0x02) != 0x02 ||
(rpc.RegionMask & 0x04) == (cmi.RegionInformation & 0x04) && (rpc.RegionMask & 0x04) != 0x04 ||
(rpc.RegionMask & 0x08) == (cmi.RegionInformation & 0x08) && (rpc.RegionMask & 0x08) != 0x08 ||
(rpc.RegionMask & 0x10) == (cmi.RegionInformation & 0x10) && (rpc.RegionMask & 0x10) != 0x10 ||
(rpc.RegionMask & 0x20) == (cmi.RegionInformation & 0x20) && (rpc.RegionMask & 0x20) != 0x20 ||
(rpc.RegionMask & 0x40) == (cmi.RegionInformation & 0x40) && (rpc.RegionMask & 0x40) != 0x40 ||
(rpc.RegionMask & 0x80) == (cmi.RegionInformation & 0x80) && (rpc.RegionMask & 0x80) != 0x80;
return ((rpc.RegionMask & 0x01) == (cmi.RegionInformation & 0x01) && (rpc.RegionMask & 0x01) != 0x01) ||
((rpc.RegionMask & 0x02) == (cmi.RegionInformation & 0x02) && (rpc.RegionMask & 0x02) != 0x02) ||
((rpc.RegionMask & 0x04) == (cmi.RegionInformation & 0x04) && (rpc.RegionMask & 0x04) != 0x04) ||
((rpc.RegionMask & 0x08) == (cmi.RegionInformation & 0x08) && (rpc.RegionMask & 0x08) != 0x08) ||
((rpc.RegionMask & 0x10) == (cmi.RegionInformation & 0x10) && (rpc.RegionMask & 0x10) != 0x10) ||
((rpc.RegionMask & 0x20) == (cmi.RegionInformation & 0x20) && (rpc.RegionMask & 0x20) != 0x20) ||
((rpc.RegionMask & 0x40) == (cmi.RegionInformation & 0x40) && (rpc.RegionMask & 0x40) != 0x40) ||
((rpc.RegionMask & 0x80) == (cmi.RegionInformation & 0x80) && (rpc.RegionMask & 0x80) != 0x80);
}
}

View File

@@ -34,14 +34,14 @@
// Copyright © 2020-2022 Rebecca Wallander
// ****************************************************************************/
namespace Aaru.Decryption.DVD;
using System;
using System.Linq;
using Aaru.Console;
using Aaru.Decoders.DVD;
using Aaru.Devices;
namespace Aaru.Decryption.DVD;
public sealed class Dump
{
const byte KEY_SIZE = 5;
@@ -69,7 +69,7 @@ public sealed class Dump
out double duration)
{
senseBuffer = new byte[64];
var cdb = new byte[12];
byte[] cdb = new byte[12];
buffer = new byte[8];
cdb[0] = (byte)ScsiCommands.ReportKey;
@@ -97,7 +97,7 @@ public sealed class Dump
out double duration)
{
senseBuffer = new byte[64];
var cdb = new byte[12];
byte[] cdb = new byte[12];
buffer = new byte[8];
cdb[0] = (byte)ScsiCommands.ReportKey;
@@ -125,7 +125,7 @@ public sealed class Dump
out double duration)
{
senseBuffer = new byte[64];
var cdb = new byte[12];
byte[] cdb = new byte[12];
buffer = Array.Empty<byte>();
cdb[0] = (byte)ScsiCommands.ReportKey;
@@ -154,7 +154,7 @@ public sealed class Dump
out double duration)
{
senseBuffer = new byte[64];
var cdb = new byte[12];
byte[] cdb = new byte[12];
buffer = new byte[8];
cdb[0] = (byte)ScsiCommands.ReportKey;
@@ -183,7 +183,7 @@ public sealed class Dump
out double duration)
{
senseBuffer = new byte[64];
var cdb = new byte[12];
byte[] cdb = new byte[12];
buffer = new byte[12];
cdb[0] = (byte)ScsiCommands.ReportKey;
@@ -211,7 +211,7 @@ public sealed class Dump
out double duration)
{
senseBuffer = new byte[64];
var cdb = new byte[12];
byte[] cdb = new byte[12];
buffer = new byte[16];
cdb[0] = (byte)ScsiCommands.ReportKey;
@@ -241,7 +241,7 @@ public sealed class Dump
uint timeout, out double duration)
{
senseBuffer = new byte[64];
var cdb = new byte[12];
byte[] cdb = new byte[12];
buffer = new byte[16];
cdb[0] = (byte)ScsiCommands.SendKey;
@@ -284,7 +284,7 @@ public sealed class Dump
out double duration)
{
senseBuffer = new byte[64];
var cdb = new byte[12];
byte[] cdb = new byte[12];
buffer = new byte[12];
cdb[0] = (byte)ScsiCommands.SendKey;
@@ -319,7 +319,7 @@ public sealed class Dump
public bool ReadDiscKey(out byte[] buffer, out byte[] senseBuffer, uint timeout, out double duration)
{
senseBuffer = new byte[64];
var cdb = new byte[12];
byte[] cdb = new byte[12];
buffer = new byte[2052];
cdb[0] = (byte)ScsiCommands.ReadDiscStructure;
@@ -349,9 +349,9 @@ public sealed class Dump
buffer = Array.Empty<byte>();
senseBuffer = new byte[64];
var sense = false;
var challenge = new byte[CHALLENGE_SIZE];
var key1 = new byte[KEY_SIZE];
bool sense = false;
byte[] challenge = new byte[CHALLENGE_SIZE];
byte[] key1 = new byte[KEY_SIZE];
byte variant = 0;
for(byte i = 0; i < 4; i++)
@@ -460,7 +460,7 @@ public sealed class Dump
BusKey = buffer;
senseBuffer = new byte[64];
var cdb = new byte[12];
byte[] cdb = new byte[12];
buffer = new byte[12];
cdb[0] = (byte)ScsiCommands.ReportKey;

View File

@@ -2,13 +2,10 @@ namespace Aaru.Decryption;
public enum DvdCssKeyClass : byte
{
DvdCssCppmOrCprm = 0,
RewritableSecurityServicesA = 1
DvdCssCppmOrCprm = 0, RewritableSecurityServicesA = 1
}
public enum DvdCssKeyType
{
Key1 = 0,
Key2 = 1,
BusKey = 2
Key1 = 0, Key2 = 1, BusKey = 2
}