diff --git a/Editor/EncryptionVM/VirtualMachine.cs b/Editor/EncryptionVM/VirtualMachine.cs index d70c61b..1ca1a59 100644 --- a/Editor/EncryptionVM/VirtualMachine.cs +++ b/Editor/EncryptionVM/VirtualMachine.cs @@ -4,12 +4,13 @@ { public const int SecretKeyLength = 1024; - public readonly int[] secretKey; + public readonly int version; + public readonly string codeGenerationSecretKey; public readonly EncryptionInstructionWithOpCode[] opCodes; - public VirtualMachine(int[] secretKey, EncryptionInstructionWithOpCode[] opCodes) + public VirtualMachine(int version, string codeGenerationSecretKey, EncryptionInstructionWithOpCode[] opCodes) { - this.secretKey = secretKey; + this.codeGenerationSecretKey = codeGenerationSecretKey; this.opCodes = opCodes; } } diff --git a/Editor/EncryptionVM/VirtualMachineCodeGenerator.cs b/Editor/EncryptionVM/VirtualMachineCodeGenerator.cs new file mode 100644 index 0000000..e16e0ce --- /dev/null +++ b/Editor/EncryptionVM/VirtualMachineCodeGenerator.cs @@ -0,0 +1,27 @@ +using Obfuz.Utils; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Obfuz.EncryptionVM +{ + public class VirtualMachineCodeGenerator + { + private readonly string _vmCodeGenerateSecretKey; + private readonly IRandom _random; + private VirtualMachine _vm; + + public VirtualMachineCodeGenerator(string vmCodeGenerateSecretKey, int opCount) + { + _vmCodeGenerateSecretKey = vmCodeGenerateSecretKey; + _vm = new VirtualMachineCreator(_vmCodeGenerateSecretKey).CreateVirtualMachine(opCount); + } + + public void Generate(string outputFile) + { + + } + } +} diff --git a/Editor/EncryptionVM/VirtualMachineCreator.cs b/Editor/EncryptionVM/VirtualMachineCreator.cs index 2ebda84..4163b78 100644 --- a/Editor/EncryptionVM/VirtualMachineCreator.cs +++ b/Editor/EncryptionVM/VirtualMachineCreator.cs @@ -6,15 +6,18 @@ namespace Obfuz.EncryptionVM { public class VirtualMachineCreator { - private readonly int[] _vmGenerationSecretKey; + private readonly string _vmGenerationSecretKey; private readonly IRandom _random; public const int CodeGenerationSecretKeyLength = 1024; - public VirtualMachineCreator(string vmGenerationSecretKey, byte[] encryptionSecretKey) + public const int VirtualMachineVersion = 1; + + public VirtualMachineCreator(string vmGenerationSecretKey) { - _vmGenerationSecretKey = KeyGenerator.ConvertToIntKey(KeyGenerator.GenerateKey(vmGenerationSecretKey, CodeGenerationSecretKeyLength)); - _random = new RandomWithKey(encryptionSecretKey, 0); + _vmGenerationSecretKey = vmGenerationSecretKey; + byte[] byteGenerationSecretKey = KeyGenerator.GenerateKey(vmGenerationSecretKey, CodeGenerationSecretKeyLength); + _random = new RandomWithKey(byteGenerationSecretKey, 0); } private IEncryptionInstruction CreateRandomInstruction(int intSecretKeyLength) @@ -35,20 +38,25 @@ namespace Obfuz.EncryptionVM private EncryptionInstructionWithOpCode CreateEncryptOpCode(ushort code) { IEncryptionInstruction inst = CreateRandomInstruction(VirtualMachine.SecretKeyLength / sizeof(int)); - Assert.AreEqual(1234, inst.Decrypt(inst.Encrypt(1234, _vmGenerationSecretKey, 0x12345678), _vmGenerationSecretKey, 0x12345678)); return new EncryptionInstructionWithOpCode(code, inst); } public VirtualMachine CreateVirtualMachine(int opCodeCount) { - Assert.IsTrue(opCodeCount > 0); - Assert.AreEqual(0, opCodeCount & (opCodeCount - 1)); + if (opCodeCount < 64) + { + throw new System.Exception("OpCode count should be >= 64"); + } + if ((opCodeCount & (opCodeCount - 1)) != 0) + { + throw new System.Exception("OpCode count should be power of 2"); + } var opCodes = new EncryptionInstructionWithOpCode[opCodeCount]; for (int i = 0; i < opCodes.Length; i++) { opCodes[i] = CreateEncryptOpCode((ushort)i); } - return new VirtualMachine(_vmGenerationSecretKey, opCodes); + return new VirtualMachine(VirtualMachineVersion, _vmGenerationSecretKey, opCodes); } } } diff --git a/Editor/EncryptionVM/VirtualMachineSimulator.cs b/Editor/EncryptionVM/VirtualMachineSimulator.cs index 8e43a31..0c67adb 100644 --- a/Editor/EncryptionVM/VirtualMachineSimulator.cs +++ b/Editor/EncryptionVM/VirtualMachineSimulator.cs @@ -1,4 +1,5 @@ -using System; +using Obfuz.Utils; +using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Runtime.CompilerServices; @@ -14,10 +15,10 @@ namespace Obfuz.EncryptionVM private readonly EncryptionInstructionWithOpCode[] _opCodes; private readonly int[] _secretKey; - public VirtualMachineSimulator(VirtualMachine vm) + public VirtualMachineSimulator(VirtualMachine vm, byte[] byteSecretKey) { _opCodes = vm.opCodes; - _secretKey = vm.secretKey; + _secretKey = KeyGenerator.ConvertToIntKey(byteSecretKey); VerifyInstructions(); } diff --git a/Editor/Obfuscator.cs b/Editor/Obfuscator.cs index be33e0a..c903a42 100644 --- a/Editor/Obfuscator.cs +++ b/Editor/Obfuscator.cs @@ -31,27 +31,25 @@ namespace Obfuz private readonly Pipeline _pipeline = new Pipeline(); private readonly byte[] _secretKey; private readonly int _globalRandomSeed; - private readonly string _encryptionVmSecretKey; + private readonly string _encryptionVmGenerationSecretKey; + private readonly int _encryptionVmOpCodeCount; private ObfuscationPassContext _ctx; - public Obfuscator(List toObfuscatedAssemblyNames, - List notObfuscatedAssemblyNamesReferencingObfuscated, - List assemblySearchDirs, - string obfuscatedAssemblyOutputDir, - List obfuscationPasses, string rawSecretKey, int globalRandomSeed, string encryptionVmSecretKey) + public Obfuscator(ObfuscatorBuilder builder) { - _secretKey = KeyGenerator.GenerateKey(rawSecretKey, VirtualMachine.SecretKeyLength); - _globalRandomSeed = globalRandomSeed; - _encryptionVmSecretKey = encryptionVmSecretKey; + _secretKey = KeyGenerator.GenerateKey(builder.SecretKey, VirtualMachine.SecretKeyLength); + _globalRandomSeed = builder.GlobalRandomSeed; + _encryptionVmGenerationSecretKey = builder.EncryptionVmGenerationSecretKey; + _encryptionVmOpCodeCount = builder.EncryptionVmOpCodeCount; - _toObfuscatedAssemblyNames = toObfuscatedAssemblyNames; - _notObfuscatedAssemblyNamesReferencingObfuscated = notObfuscatedAssemblyNamesReferencingObfuscated; - _obfuscatedAssemblyOutputDir = obfuscatedAssemblyOutputDir; + _toObfuscatedAssemblyNames = builder.ToObfuscatedAssemblyNames; + _notObfuscatedAssemblyNamesReferencingObfuscated = builder.NotObfuscatedAssemblyNamesReferencingObfuscated; + _obfuscatedAssemblyOutputDir = builder.ObfuscatedAssemblyOutputDir; GroupByModuleEntityManager.Reset(); - _assemblyCache = new AssemblyCache(new PathAssemblyResolver(assemblySearchDirs.ToArray())); - foreach (var pass in obfuscationPasses) + _assemblyCache = new AssemblyCache(new PathAssemblyResolver(builder.AssemblySearchDirs.ToArray())); + foreach (var pass in builder.ObfuscationPasses) { _pipeline.AddPass(pass); } @@ -67,9 +65,9 @@ namespace Obfuz private IEncryptor CreateEncryptionVirtualMachine() { - var vmCreator = new VirtualMachineCreator(_encryptionVmSecretKey, _secretKey); - var vm = vmCreator.CreateVirtualMachine(1); - return new VirtualMachineSimulator(vm); + var vmCreator = new VirtualMachineCreator(_encryptionVmGenerationSecretKey); + var vm = vmCreator.CreateVirtualMachine(_encryptionVmOpCodeCount); + return new VirtualMachineSimulator(vm, _secretKey); } private void OnPreObfuscation() diff --git a/Editor/ObfuscatorBuilder.cs b/Editor/ObfuscatorBuilder.cs index 6dc8619..9d0287f 100644 --- a/Editor/ObfuscatorBuilder.cs +++ b/Editor/ObfuscatorBuilder.cs @@ -16,6 +16,7 @@ namespace Obfuz private string _secretKey; private int _globalRandomSeed; private string _encryptionVmGenerationSecretKey; + private int _encryptionVmOpCodeCount; private List _toObfuscatedAssemblyNames = new List(); private List _notObfuscatedAssemblyNamesReferencingObfuscated = new List(); private List _assemblySearchDirs = new List(); @@ -41,6 +42,12 @@ namespace Obfuz set => _encryptionVmGenerationSecretKey = value; } + public int EncryptionVmOpCodeCount + { + get => _encryptionVmOpCodeCount; + set => _encryptionVmOpCodeCount = value; + } + public List ToObfuscatedAssemblyNames { get => _toObfuscatedAssemblyNames; @@ -65,6 +72,8 @@ namespace Obfuz set => _obfuscatedAssemblyOutputDir = value; } + public List ObfuscationPasses { get => _obfuscationPasses; set => _obfuscationPasses = value; } + public void InsertTopPriorityAssemblySearchDirs(List assemblySearchDirs) { _assemblySearchDirs.InsertRange(0, assemblySearchDirs); @@ -78,11 +87,7 @@ namespace Obfuz public Obfuscator Build() { - return new Obfuscator(_toObfuscatedAssemblyNames, - _notObfuscatedAssemblyNamesReferencingObfuscated, - _assemblySearchDirs, - _obfuscatedAssemblyOutputDir, - _obfuscationPasses, _secretKey, _globalRandomSeed, _encryptionVmGenerationSecretKey); + return new Obfuscator(this); } public static ObfuscatorBuilder FromObfuzSettings(ObfuzSettings settings, BuildTarget target) @@ -92,6 +97,7 @@ namespace Obfuz _secretKey = settings.secretKey, _globalRandomSeed = settings.globalRandomSeed, _encryptionVmGenerationSecretKey = settings.encryptionVMSettings.codeGenerationSecretKey, + _encryptionVmOpCodeCount = settings.encryptionVMSettings.encryptionOpCodeCount, _toObfuscatedAssemblyNames = settings.toObfuscatedAssemblyNames.ToList(), _notObfuscatedAssemblyNamesReferencingObfuscated = settings.notObfuscatedAssemblyNamesReferencingObfuscated.ToList(), _assemblySearchDirs = settings.extraAssemblySearchDirs.ToList(), diff --git a/Editor/Settings/EncryptionVMSettings.cs b/Editor/Settings/EncryptionVMSettings.cs index 992d576..d600ba2 100644 --- a/Editor/Settings/EncryptionVMSettings.cs +++ b/Editor/Settings/EncryptionVMSettings.cs @@ -13,6 +13,9 @@ namespace Obfuz.Settings [Tooltip("secret key for generating encryption virtual machine source code")] public string codeGenerationSecretKey = "Obfuz"; + [Tooltip("encryption OpCode count, should be power of 2 and >= 64")] + public int encryptionOpCodeCount = 256; + [Tooltip("encryption virtual machine source code output dir")] public string codeOutputDir = "Assets/Obfuz"; }