2025-05-13 19:50:07 +08:00
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using Obfuz.EncryptionVM.Instructions;
|
2025-05-11 12:48:53 +08:00
|
|
|
|
using Obfuz.Utils;
|
2025-05-13 19:50:07 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2025-05-11 12:48:53 +08:00
|
|
|
|
using UnityEngine.Assertions;
|
2025-05-13 19:50:07 +08:00
|
|
|
|
using UnityEngine.UIElements;
|
2025-05-11 12:48:53 +08:00
|
|
|
|
|
2025-05-11 12:53:24 +08:00
|
|
|
|
namespace Obfuz.EncryptionVM
|
2025-05-11 12:48:53 +08:00
|
|
|
|
{
|
2025-05-11 12:53:24 +08:00
|
|
|
|
public class VirtualMachineCreator
|
2025-05-11 12:48:53 +08:00
|
|
|
|
{
|
2025-05-11 17:36:58 +08:00
|
|
|
|
private readonly string _vmGenerationSecretKey;
|
2025-05-11 12:48:53 +08:00
|
|
|
|
private readonly IRandom _random;
|
|
|
|
|
|
|
|
|
|
public const int CodeGenerationSecretKeyLength = 1024;
|
|
|
|
|
|
2025-05-11 17:36:58 +08:00
|
|
|
|
public const int VirtualMachineVersion = 1;
|
|
|
|
|
|
|
|
|
|
public VirtualMachineCreator(string vmGenerationSecretKey)
|
2025-05-11 12:48:53 +08:00
|
|
|
|
{
|
2025-05-11 17:36:58 +08:00
|
|
|
|
_vmGenerationSecretKey = vmGenerationSecretKey;
|
|
|
|
|
byte[] byteGenerationSecretKey = KeyGenerator.GenerateKey(vmGenerationSecretKey, CodeGenerationSecretKeyLength);
|
2025-05-13 08:56:19 +08:00
|
|
|
|
int[] intGenerationSecretKey = KeyGenerator.ConvertToIntKey(byteGenerationSecretKey);
|
|
|
|
|
_random = new RandomWithKey(intGenerationSecretKey, 0);
|
2025-05-11 12:48:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-05-13 19:50:07 +08:00
|
|
|
|
private readonly List<Func<IRandom, int, EncryptionInstructionBase>> _instructionCreators = new List<Func<IRandom, int, EncryptionInstructionBase>>
|
|
|
|
|
{
|
|
|
|
|
(r, len) => new AddInstruction(r.NextInt(), r.NextInt(len)),
|
|
|
|
|
(r, len) => new XorInstruction(r.NextInt(), r.NextInt(len)),
|
|
|
|
|
(r, len) => new BitRotateInstruction(r.NextInt(32), r.NextInt(len)),
|
|
|
|
|
(r, len) => new MultipleInstruction(r.NextInt() | 0x1, r.NextInt(len)),
|
|
|
|
|
(r, len) => new AddRotateXorInstruction(r.NextInt(), r.NextInt(len), r.NextInt(32), r.NextInt()),
|
|
|
|
|
(r, len) => new AddXorRotateInstruction(r.NextInt(), r.NextInt(len), r.NextInt(), r.NextInt(32)),
|
|
|
|
|
(r, len) => new XorAddRotateInstruction(r.NextInt(), r.NextInt(), r.NextInt(len), r.NextInt(32)),
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2025-05-11 12:48:53 +08:00
|
|
|
|
private IEncryptionInstruction CreateRandomInstruction(int intSecretKeyLength)
|
|
|
|
|
{
|
2025-05-13 19:50:07 +08:00
|
|
|
|
return _instructionCreators[_random.NextInt(_instructionCreators.Count)](_random, intSecretKeyLength);
|
2025-05-11 12:48:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private EncryptionInstructionWithOpCode CreateEncryptOpCode(ushort code)
|
|
|
|
|
{
|
2025-05-11 12:53:24 +08:00
|
|
|
|
IEncryptionInstruction inst = CreateRandomInstruction(VirtualMachine.SecretKeyLength / sizeof(int));
|
2025-05-11 12:48:53 +08:00
|
|
|
|
return new EncryptionInstructionWithOpCode(code, inst);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-11 12:53:24 +08:00
|
|
|
|
public VirtualMachine CreateVirtualMachine(int opCodeCount)
|
2025-05-11 12:48:53 +08:00
|
|
|
|
{
|
2025-05-11 17:36:58 +08:00
|
|
|
|
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");
|
|
|
|
|
}
|
2025-05-11 12:48:53 +08:00
|
|
|
|
var opCodes = new EncryptionInstructionWithOpCode[opCodeCount];
|
|
|
|
|
for (int i = 0; i < opCodes.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
opCodes[i] = CreateEncryptOpCode((ushort)i);
|
|
|
|
|
}
|
2025-05-11 17:36:58 +08:00
|
|
|
|
return new VirtualMachine(VirtualMachineVersion, _vmGenerationSecretKey, opCodes);
|
2025-05-11 12:48:53 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|