public void preprocess(IFixedArray <byte> mes) { // the amount of padding 448 mod 512, only applies to the last block for (int i = 0; i < MAX_BUFFER_SIZE; i++) { workingBuffer[i] = mes[i]; } if (Message.Last) { if (!Message.Set) { workingBuffer[Message.BufferSize] = 0x80; } ulong fullSize = (ulong)(Message.MessageSize << 3); workingBuffer[MAX_BUFFER_SIZE - 8] = (byte)(fullSize >> 0 & 0x00000000000000ff); workingBuffer[MAX_BUFFER_SIZE - 7] = (byte)(fullSize >> 8 & 0x00000000000000ff); workingBuffer[MAX_BUFFER_SIZE - 6] = (byte)(fullSize >> 16 & 0x00000000000000ff); workingBuffer[MAX_BUFFER_SIZE - 5] = (byte)(fullSize >> 24 & 0x00000000000000ff); workingBuffer[MAX_BUFFER_SIZE - 4] = (byte)(fullSize >> 32 & 0x00000000000000ff); workingBuffer[MAX_BUFFER_SIZE - 3] = (byte)(fullSize >> 40 & 0x00000000000000ff); workingBuffer[MAX_BUFFER_SIZE - 2] = (byte)(fullSize >> 48 & 0x00000000000000ff); workingBuffer[MAX_BUFFER_SIZE - 1] = (byte)(fullSize >> 56 & 0x00000000000000ff); } else if (Message.Set) { workingBuffer[Message.BufferSize] = 0x80; } }
public IndicesModel FindIndicesByArrayAndObjetive(IFixedArray fixedArray, int objetive) { var array = fixedArray.GetArray(); for (int i = 0; i < array.Length; i++) { var item1 = array[i]; for (int j = 0; j < array.Length; j++) { var item2 = array[j]; if (i != j && item1 + item2 == objetive) { return new IndicesModel { Indice1 = i, Indice2 = j } } ; } } return(null); } }
private void Expand128(IFixedArray <byte> key) { for (int i = 0; i < N_KEY_128 << 2; i += 4) { expandedKey128[i >> 2] = ((uint)key[i] << 24) | ((uint)key[i + 1] << 16) | ((uint)key[i + 2] << 8) | ((uint)key[i + 3]); Console.WriteLine($"i: {i>>2} key: {expandedKey128[i>>2].ToString("x8")}"); } for (int i = N_KEY_128; i < ROUND_SIZE_128; i++) { uint w = expandedKey128[i - 1]; if (i % N_KEY_128 == 0) { w = SubWord(LeftRotate(w, 8)) ^ Round[i / N_KEY_128]; } else if (N_KEY_128 > 6 && (i % N_KEY_128) == 4) { w = SubWord(w); } expandedKey128[i] = expandedKey128[i - N_KEY_128] ^ w; Console.WriteLine($"i: {i} i-1: {i-1} i-N_KEY {i-N_KEY_128} i/N_KEY_128: {i/N_KEY_128} key: {expandedKey128[i].ToString("x8")}"); } }
private void Expand128(IFixedArray <byte> key) { for (int i = 0; i < N_KEY_128 << 2; i += 4) { expandedKey128[i >> 2] = ((uint)key[i] << 24) | ((uint)key[i + 1] << 16) | ((uint)key[i + 2] << 8) | ((uint)key[i + 3]); } for (int i = N_KEY_128; i < 12; i++) { uint w = expandedKey128[i - 1]; if (i % N_KEY_128 == 0) { w = SubWord(LeftRotate(w, 8)) ^ Round[(i / N_KEY_128) - 1]; } else if (N_KEY_128 > 6 && (i % N_KEY_128) == 4) { w = SubWord(w); } expandedKey128[i] = expandedKey128[i - N_KEY_128] ^ w; } for (int i = 0; i < 4; i++) { Out.Key[i] = expandedKey128[8 + i]; } }
private void Expand128(IFixedArray <byte> key) { for (int i = 0; i < N_KEY_128; i++) { expandedKey128[4 * i + 0] = key[4 * i + 0]; expandedKey128[4 * i + 1] = key[4 * i + 1]; expandedKey128[4 * i + 2] = key[4 * i + 2]; expandedKey128[4 * i + 3] = key[4 * i + 3]; // Console.WriteLine($"i: {i} key: {expandedKey128[4*i].ToString("x2")}{expandedKey128[4*i+1].ToString("x2")}{expandedKey128[4*i+2].ToString("x2")}{expandedKey128[4*i+3].ToString("x2")}- {expandedKey128[i]}"); } for (int i = N_KEY_128; i < ROUND_SIZE_128; i++) { aes_tmp[0] = expandedKey128[4 * (i - 1) + 0]; aes_tmp[1] = expandedKey128[4 * (i - 1) + 1]; aes_tmp[2] = expandedKey128[4 * (i - 1) + 2]; aes_tmp[3] = expandedKey128[4 * (i - 1) + 3]; if (i % N_KEY_128 == 0) { RotWord(); SubWord(); // since rcon only has values in first byte we only xor with the first byte of a word aes_tmp[0] ^= (byte)(Round[i / N_KEY_128] >> 24); } expandedKey128[4 * i + 0] = (byte)(expandedKey128[4 * (i - N_KEY_128) + 0] ^ aes_tmp[0]); expandedKey128[4 * i + 1] = (byte)(expandedKey128[4 * (i - N_KEY_128) + 1] ^ aes_tmp[1]); expandedKey128[4 * i + 2] = (byte)(expandedKey128[4 * (i - N_KEY_128) + 2] ^ aes_tmp[2]); expandedKey128[4 * i + 3] = (byte)(expandedKey128[4 * (i - N_KEY_128) + 3] ^ aes_tmp[3]); // Console.WriteLine($"i: {i} i-1: {i-1} i-N_KEY {i-N_KEY_128} i/N_KEY_128: {i/N_KEY_128} key: {expandedKey128[4*i].ToString("x2")}{expandedKey128[4*i+1].ToString("x2")}{expandedKey128[4*i+2].ToString("x2")}{expandedKey128[4*i+3].ToString("x2")}"); } }
// Compares if incoming IP 'destination' is in the whitelisted range. public static bool IsIPinRange(byte[] low_source, byte[] high_source, byte[] low_dest, byte[] high_dest, IFixedArray <byte> ip_source, IFixedArray <byte> ip_dest) { // The return boolean starts false bool doesItMatch = false; // The whitelisted source/dest ranges uint low_source_uint = ByteArrayToUint.convert(low_source); uint high_source_uint = ByteArrayToUint.convert(high_source); uint low_dest_uint = ByteArrayToUint.convert(low_dest); uint high_dest_uint = ByteArrayToUint.convert(high_dest); // The incoming IP source/dest uint incoming_source_uint = ByteArrayToUint.convertIFixed(ip_source); uint incoming_dest_uint = ByteArrayToUint.convertIFixed(ip_dest); // Comparing if the incoming Source/Dest is WITHIN the legal whitelist range if ((low_source_uint <= incoming_source_uint && incoming_source_uint <= high_source_uint) && (low_dest_uint <= incoming_dest_uint && incoming_dest_uint <= high_dest_uint)) { doesItMatch = true; } // Returns TRUE if the incoming src/dst was in the whitelisted range return(doesItMatch); }
public static string ByteArrayToString(IFixedArray <byte> buffer) { byte[] tmp = new byte[buffer.Length]; for (int i = 0; i < buffer.Length; i++) { tmp[i] = buffer[i]; } return(BitConverter.ToString(tmp).Replace("-", "")); }
// The main function to calculate MD public void calculateMD5(IFixedArray <byte> mes) { preprocess(mes); fetchBlock(workingBuffer); // for(int i = 0; i < BLOCK_SIZE; i++) { // Console.Write(blockD[i]); // } // Console.WriteLine(); processBlock(); }
private void fetchBlock(IFixedArray <byte> buff) { for (int j = 0; j < 61; j += 4) { Out.buffer[j >> 2] = (((uint)buff[(j + 3)]) << 24) | (((uint)buff[(j + 2)]) << 16) | (((uint)buff[(j + 1)]) << 8) | (((uint)buff[(j)])); } }
private void Expand128(IFixedArray <byte> key) { for (int i = 0; i < N_KEY_128 << 2; i += 4) { Out.Key[i >> 2] = expandedKey128[i >> 2] = ((uint)key[i] << 24) | ((uint)key[i + 1] << 16) | ((uint)key[i + 2] << 8) | ((uint)key[i + 3]); } }
public static uint convertIFixed(IFixedArray <byte> byte_array) { // Bitshifting with 24,16, and 8 respectivly uint UintResult = (uint)(((byte_array[0] << 24)) | ((byte_array[1] << 16)) | ((byte_array[2] << 8)) | ((byte_array[3]))); return(UintResult); }
internal static unsafe ref T GetRef <T>(this IFixedArray <T> array, T *ptr, int index) where T : unmanaged { if (index > -1 && index < array.Length) { return(ref *(ptr + index)); } throw new IndexOutOfRangeException($"{nameof(array.GetType)}'s index can't be {index}"); }
private void IP_Match(byte[] dest_low, byte[] dest_high, IFixedArray <byte> outgoing_IP) { uint dest_low_uint = ByteArrayToUint.convert(dest_low); uint dest_high_uint = ByteArrayToUint.convert(dest_high); uint outgoing_IP_uint = ByteArrayToUint.convertIFixed(outgoing_IP); // if TRUE, it means that the given IP was in the blacklist range, and must be BLOCKED if ((dest_low_uint <= outgoing_IP_uint) && (outgoing_IP_uint <= dest_high_uint)) { ruleVerdict.Accepted = true; } else { ruleVerdict.Accepted = false; } }
// **************************************************************************** public static bool ipv4_checker(byte[] source, byte[] dest, IFixedArray <byte> incoming_source, IFixedArray <byte> incoming_dest) { bool doesItMatch = false; uint source_uint = ByteArrayToUint.convert(source); uint dest_uint = ByteArrayToUint.convert(dest); uint incoming_source_uint = ByteArrayToUint.convertIFixed(incoming_source); uint incoming_dest_uint = ByteArrayToUint.convertIFixed(incoming_dest); if ((source_uint == incoming_source_uint) && (dest_uint == incoming_dest_uint)) { doesItMatch = true; } return(doesItMatch); }
// **************************************************************************** public static bool DoesConnectExist(byte[] source, byte[] dest, uint port, IFixedArray <byte> incoming_source, IFixedArray <byte> incoming_dest, uint incoming_port) { bool doesItMatch = false; uint source_uint = ByteArrayToUint.convert(source); uint dest_uint = ByteArrayToUint.convert(dest); uint incoming_source_uint = ByteArrayToUint.convertIFixed(incoming_source); uint incoming_dest_uint = ByteArrayToUint.convertIFixed(incoming_dest); if (((source_uint == incoming_source_uint) && (dest_uint == incoming_dest_uint)) && (port == incoming_port)) { doesItMatch = true; } return(doesItMatch); }
private uint[] blockD = new uint[64]; // 16 for the message, 48 for SHA256 calcs. private void fetchBlock(IFixedArray <byte> buff) { for (int j = 0; j < 61; j += 4) { Out.buffer[j >> 2] = blockD[j >> 2] = (((uint)buff[j]) << 24) | (((uint)buff[(j + 1)]) << 16) | (((uint)buff[(j + 2)]) << 8) | (((uint)buff[(j + 3)])); } uint s0 = 0, s1 = 0; for (int j = 16; j < 64; j++) { s0 = rightrotate(blockD[j - 15], 7) ^ rightrotate(blockD[j - 15], 18) ^ (blockD[j - 15] >> 3); s1 = rightrotate(blockD[j - 2], 17) ^ rightrotate(blockD[j - 2], 19) ^ (blockD[j - 2] >> 10); Out.buffer[j] = blockD[j] = blockD[j - 16] + s0 + blockD[j - 7] + s1; } }
// The main function to calculate MD public void calculateSHA(IFixedArray <byte> mes) { preprocess(mes); fetchBlock(workingBuffer); processBlock(); }
public void Setup() { _twoSum = new TwoSumService(); _fixedArray = Substitute.For <IFixedArray>(); }