protected override void CreateMessagingSocket(int idx, SymmetricKey key, out IMessagingSocket socket, out EndPoint endPoint) { endPoint = new IPEndPoint (_adrsGen.Next (), 10000); VirtualDatagramEventSocket sock = new VirtualDatagramEventSocket (_net, ((IPEndPoint)endPoint).Address); sock.Bind (endPoint); socket = new VirtualMessagingSocket (sock, true, _interrupter, DefaultRTO, DefaultRetryCount, 1024, 1024); }
public void EncryptionTest() { SymmetricKey key = new SymmetricKey (SymmetricAlgorithmType.Camellia, RNG.GetRNGBytes (16), RNG.GetRNGBytes (16)); IMessagingSocket[] msockets; EndPoint[] endPoints; EndPoint noRouteEP; CreateMessagingSockets (2, key, out msockets, out endPoints, out noRouteEP); msockets[1].AddInquiredHandler (typeof (byte[]), delegate (object sender, InquiredEventArgs e) { msockets[1].StartResponse (e, e.InquireMessage); }); try { byte[] data = new byte[msockets[0].MaxMessageSize]; while (true) { byte[] serialized; using (MemoryStream ms = new MemoryStream ()) { _formatter.Serialize (ms, data); ms.Close (); serialized = ms.ToArray (); } if (serialized.Length == msockets[0].MaxMessageSize) break; data = new byte[data.Length - 1]; } IAsyncResult ar = msockets[0].BeginInquire (data, endPoints[1], null, null); byte[] ret = msockets[0].EndInquire (ar) as byte[]; Assert.AreEqual (ret, data); } finally { DisposeAll (msockets); } }
public void Test1() { SymmetricAlgorithmType[] types = new SymmetricAlgorithmType[] { SymmetricAlgorithmType.None, SymmetricAlgorithmType.Camellia, SymmetricAlgorithmType.Rijndael }; byte[][][] key_iv_list = new byte[][][] { new byte[][] { null, null }, new byte[][] { RNG.GetRNGBytes (16), RNG.GetRNGBytes (16), RNG.GetRNGBytes (24), RNG.GetRNGBytes (16), RNG.GetRNGBytes (32), RNG.GetRNGBytes (16) }, new byte[][] { RNG.GetRNGBytes (16), RNG.GetRNGBytes (16), RNG.GetRNGBytes (24), RNG.GetRNGBytes (16), RNG.GetRNGBytes (32), RNG.GetRNGBytes (16) }, }; int[] data_length_list = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 64, 128, 129, 130 }; bool[] shuffle_list = new bool[] {true, false}; for (int i = 0; i < types.Length; i ++) { byte[][] key_ivs = key_iv_list[i]; for (int k = 0; k < key_ivs.Length - 1; k += 2) { int[] ivShuffleSize = new int[data_length_list.Length]; foreach (bool enableShuffle in shuffle_list) { SymmetricKey key = new SymmetricKey (types[i], key_ivs[k + 1], key_ivs[k], CipherModePlus.CBC, System.Security.Cryptography.PaddingMode.ISO10126, enableShuffle); for (int idx = 0; idx < data_length_list.Length; idx ++) { byte[] data = RNG.GetRNGBytes (data_length_list[idx]); byte[] e1 = key.Encrypt (data, 0, data.Length); byte[] p1 = key.Decrypt (e1, 0, e1.Length); Assert.AreEqual (data, p1); if (key.IV != null) { if (enableShuffle) ivShuffleSize[idx] = e1.Length; else Assert.AreEqual (ivShuffleSize[idx] - key.IV.Length, e1.Length); } } } } } }
public MessagingSocket(IDatagramEventSocket sock, bool ownSocket, SymmetricKey key, IFormatter formatter, object nullObject, IntervalInterrupter interrupter, IRTOAlgorithm rtoAlgo, int maxRetry, int retryBufferSize, int inquiryDupCheckSize) : base(sock, ownSocket, interrupter, rtoAlgo, maxRetry, retryBufferSize, inquiryDupCheckSize) { _key = (key != null ? key : SymmetricKey.NoneKey); _formatter = formatter; _nullObject = nullObject != null ? nullObject : NullObject.Instance; sock.Received += Socket_Received; _maxMsgSize = sock.MaxDatagramSize; if (_key.AlgorithmType != SymmetricAlgorithmType.None && key.IV != null) { _maxMsgSize -= _maxMsgSize % key.IV.Length; if (key.Padding != System.Security.Cryptography.PaddingMode.None) _maxMsgSize --; if (key.EnableIVShuffle) _maxMsgSize -= key.IV.Length; } _maxMsgSize -= MAX_HEADER_SIZE; }
protected void CreateMessagingSockets(int count, SymmetricKey key, out IMessagingSocket[] sockets, out EndPoint[] endPoints, out EndPoint noRouteEP) { sockets = new IMessagingSocket[count]; endPoints = new EndPoint[count]; noRouteEP = GetNoRouteEndPoint (); for (int i = 0; i < sockets.Length; i++) { IMessagingSocket sock; EndPoint ep; CreateMessagingSocket (i, key, out sock, out ep); sockets[i] = sock; endPoints[i] = ep; sockets[i].InquiredUnknownMessage += DefaultInquiredEventHandler; } }
protected abstract void CreateMessagingSocket(int idx, SymmetricKey key, out IMessagingSocket socket, out EndPoint endPoint);
protected override void CreateMessagingSocket(int idx, SymmetricKey key, out IMessagingSocket socket, out EndPoint endPoint) { UdpSocket udpSocket = UdpSocket.CreateIPv4 (); endPoint = new IPEndPoint (IPAddress.Loopback, 10000 + idx); udpSocket.Bind (endPoint); socket = new MessagingSocket (udpSocket, true, key, _formatter, null, _interrupter, DefaultRTO, DefaultRetryCount, 1024, 1024); }
public void Test2() { SymmetricKey key = new SymmetricKey (SymmetricAlgorithmType.Camellia, RNG.GetRNGBytes (16), RNG.GetRNGBytes (16), CipherModePlus.CBC, System.Security.Cryptography.PaddingMode.None, true); byte[] plain = RNG.GetRNGBytes (16); byte[] ciper = key.Encrypt (plain, 0, plain.Length); byte[] plain2 = key.Decrypt (ciper, 0, ciper.Length); Assert.AreEqual (plain, plain2); }