public static bool Verify(BroadcastMessageDto broadcastMessage, byte[] modulus, byte[] signature) { var data = broadcastMessage?.SourceIdHash.Concat(broadcastMessage.DestinationIdHash). Concat(broadcastMessage.Payload) .ToArray(); return(Verify(data, modulus, signature)); }
public static void Main(string[] args) { Identity A = new Identity(new IdentityManager(), "A"); Identity B = new Identity(new IdentityManager(), "B"); //Identity C = new Identity(new IdentityManager(), "C"); Identity D = new Identity(new IdentityManager(), "D"); //Identity E = new Identity(new IdentityManager(), "E"); //Identity F = new Identity(new IdentityManager(), "F"); //Identity G = new Identity(new IdentityManager(), "G"); //Identity H = new Identity(new IdentityManager(), "H"); //Identity I = new Identity(new IdentityManager(), "I"); A.GenerateRootChain(); B.AddTrustChain(A.GenerateNewChain(B.PublicIdentity, Permission.Invite | Permission.Broadcast, Permission.Invite, "B")); //C.AddTrustChain(B.GenerateNewChain(C.PublicIdentity, Permission.Invite, Permission.Invite, "C")); D.AddTrustChain(A.GenerateNewChain(D.PublicIdentity, Permission.Invite, Permission.Invite, "D")); //E.AddTrustChain(C.GenerateNewChain(E.PublicIdentity, Permission.Invite, Permission.Invite, "E")); //F.AddTrustChain(D.GenerateNewChain(F.PublicIdentity, Permission.Invite, Permission.None, "F")); //G.AddTrustChain(E.GenerateNewChain(G.PublicIdentity, Permission.Invite, Permission.Invite, "G")); //H.AddTrustChain(C.GenerateNewChain(H.PublicIdentity, Permission.None, Permission.None, "H")); //I.AddTrustChain(F.GenerateNewChain(I.PublicIdentity, Permission.None, Permission.None, "I")); B.ValidateAndAdd(D.TrustChain); D.ValidateAndAdd(B.TrustChain); //bool ba = B.ValidateAndAdd(TrustChainUtil.SerializeTrustChain(A.TrustChain)); //bool bd = B.ValidateAndAdd(TrustChainUtil.SerializeTrustChain(D.TrustChain)); //bool bf = B.ValidateAndAdd(TrustChainUtil.SerializeTrustChain(F.TrustChain)); //bool bi = B.ValidateAndAdd(TrustChainUtil.SerializeTrustChain(I.TrustChain)); //bool ig = I.ValidateAndAdd(TrustChainUtil.SerializeTrustChain(G.TrustChain)); //bool ic = I.ValidateAndAdd(TrustChainUtil.SerializeTrustChain(C.TrustChain)); //bool ia = I.ValidateAndAdd(TrustChainUtil.SerializeTrustChain(A.TrustChain)); //Console.WriteLine($"\nvalidated chains {ba} {bd} {bf} {bi}"); //Console.WriteLine($"validated chains {ig} {ic} {ia}"); //Console.WriteLine(TrustChainUtil.TrustChainToString(G.TrustChain)); //Identity alice = new Identity(new IdentityManager(), "alice"); //Identity bob = new Identity(new IdentityManager(), "bob"); //Identity eve = new Identity(new IdentityManager(), "eve"); //string message = "This is a long message being sent over a cryptographically secure channel."; //byte[] data = alice.GenerateMessage(message, bob.PublicIdentity); //Console.WriteLine($"Message len {data.Length}"); //string received = eve.ReadMessage(data); //Console.WriteLine($"Got '{received}' from alice"); var encoded = B.EncodePacket(Encoding.UTF8.GetBytes("This is a broadcast test #415 (to B from D)"), D.PublicIdentity); byte[] fakePayload = new byte[2 * CryptoUtil.HASH_SIZE + CryptoUtil.ASYM_KEY_SIZE_BYTES]; Buffer.BlockCopy(A.PublicIdentityHash, 0, fakePayload, 0, CryptoUtil.HASH_SIZE); Buffer.BlockCopy(D.PublicIdentityHash, 0, fakePayload, CryptoUtil.HASH_SIZE, CryptoUtil.HASH_SIZE); Buffer.BlockCopy(encoded.Payload, 0, fakePayload, 2 * CryptoUtil.HASH_SIZE, CryptoUtil.ASYM_KEY_SIZE_BYTES); byte[] fakeSignature = CryptoUtil.Sign(fakePayload, A.privateKeyDebug); BroadcastMessageDto fake = new BroadcastMessageDto(); fake.SourceIdHash = A.PublicIdentityHash; fake.DestinationIdHash = D.PublicIdentityHash; fake.Payload = encoded.Payload; fake.Signature = fakeSignature; byte[] decoded; if (D.TryDecodePayload(fake, out decoded)) { Console.WriteLine(BitConverter.ToString(fake.Payload)); Console.WriteLine(Encoding.UTF8.GetString(decoded)); } else { Console.WriteLine("Not our packet"); } }
public bool TryDecodePayload(BroadcastMessageDto broadcastMessage, out byte[] decryptedPayload) { var senderHash = broadcastMessage.SourceIdHash; var receiverHash = broadcastMessage.DestinationIdHash; var payload = broadcastMessage.Payload; var signature = broadcastMessage.Signature; byte[] totalMessage; using (var ms = new MemoryStream()) using (var writer = new BinaryWriter(ms)) { writer.Write(senderHash); writer.Write(receiverHash); writer.Write(payload); totalMessage = ms.ToArray(); } var senderNode = identityManager.LookupIdentity(senderHash); if (senderNode == null) { // throw new InvalidStateException("Sender has not recognized"); decryptedPayload = null; return(false); } if (!CryptoUtil.Verify(totalMessage, senderNode.ThisId, signature)) { // throw new CryptographicException("Could not verify message"); decryptedPayload = null; return(false); } // message is now verified byte[] symmetricKey; if (receiverHash.SequenceEqual(BROADCAST_ID)) { // broadcast decryptedPayload = payload; return(true); } else if (receiverHash.SequenceEqual(identityHash)) { // unicast to us var decryptedSenderAndMessage = CryptoUtil.AsymmetricDecrypt(payload, privateKey); if (!decryptedSenderAndMessage.Take(CryptoUtil.HASH_SIZE).SequenceEqual(senderHash)) { // BREACH BREACH BREACH DEPLOY SECURITY COUNTER MEASURES // throw new CryptographicException("DATA WAS BAD THERE ARE BAD PEOPLE HERE THEY MUST BE KEPT OUT"); decryptedPayload = null; return(false); } decryptedPayload = decryptedSenderAndMessage.Skip(CryptoUtil.HASH_SIZE).ToArray(); return(true); } else if (identityManager.TryLookupMulticastKey(IdentityHash.GetFlyweight(receiverHash), out symmetricKey)) { // multicast var iv = payload.Take(CryptoUtil.IV_SIZE).ToArray(); var encryptedMessage = payload.Skip(CryptoUtil.IV_SIZE).ToArray(); var messageAndInnerSignature = CryptoUtil.SymmetricDecrypt(encryptedMessage, symmetricKey, iv); var innerSignature = messageAndInnerSignature.Skip(messageAndInnerSignature.Length - CryptoUtil.ASYM_KEY_SIZE_BYTES).ToArray(); var message = messageAndInnerSignature.Take(messageAndInnerSignature.Length - CryptoUtil.ASYM_KEY_SIZE_BYTES).ToArray(); if (!CryptoUtil.Verify(message, senderNode.ThisId, innerSignature)) { // throw new CryptographicException("Could not verify inner signature"); decryptedPayload = null; return(false); } decryptedPayload = message; return(true); } else { // unknown multi/unicast decryptedPayload = null; return(false); } }