public Packet Auth(PublicKey remoteNodeId, EncryptionHandshake handshake) { handshake.RemoteNodeId = remoteNodeId; handshake.InitiatorNonce = _cryptoRandom.GenerateRandomBytes(32); handshake.EphemeralPrivateKey = _ephemeralGenerator.Generate(); byte[] staticSharedSecret = Proxy.EcdhSerialized(remoteNodeId.Bytes, _privateKey.KeyBytes); byte[] forSigning = staticSharedSecret.Xor(handshake.InitiatorNonce); AuthEip8Message authMessage = new AuthEip8Message(); authMessage.Nonce = handshake.InitiatorNonce; authMessage.PublicKey = _privateKey.PublicKey; authMessage.Signature = _ecdsa.Sign(handshake.EphemeralPrivateKey, new Keccak(forSigning)); byte[] authData = _messageSerializationService.Serialize(authMessage); int size = authData.Length + 32 + 16 + 65; // data + MAC + IV + pub byte[] sizeBytes = size.ToBigEndianByteArray().Slice(2, 2); byte[] packetData = _eciesCipher.Encrypt( remoteNodeId, authData, sizeBytes); handshake.AuthPacket = new Packet(Bytes.Concat(sizeBytes, packetData)); return(handshake.AuthPacket); }
protected byte[] Serialize(byte[] type, byte[] data) { byte[] payload = Bytes.Concat(type[0], data); Keccak toSign = Keccak.Compute(payload); Signature signature = _ecdsa.Sign(_privateKey, toSign); byte[] signatureBytes = Bytes.Concat(signature.Bytes, signature.RecoveryId); byte[] mdc = Keccak.Compute(Bytes.Concat(signatureBytes, type, data)).Bytes; return(Bytes.Concat(mdc, signatureBytes, type, data)); }
protected byte[] Serialize(byte type, Span <byte> data) { Span <byte> result = new byte[32 + 1 + data.Length + 64 + 1].AsSpan(); result[32 + 65] = type; data.CopyTo(result.Slice(32 + 65 + 1, data.Length)); Span <byte> payload = result.Slice(32 + 65); Keccak toSign = Keccak.Compute(payload); Signature signature = _ecdsa.Sign(_privateKey, toSign); signature.Bytes.AsSpan().CopyTo(result.Slice(32, 64)); result[32 + 64] = signature.RecoveryId; Span <byte> forMdc = result.Slice(32); Keccak mdc = Keccak.Compute(forMdc); mdc.Bytes.AsSpan().CopyTo(result.Slice(0, 32)); return(result.ToArray()); }
/// <summary> /// Signs the node record with own private key. /// </summary> /// <param name="nodeRecord"></param> public void Sign(NodeRecord nodeRecord) { nodeRecord.Signature = _ecdsa.Sign(_privateKey, nodeRecord.ContentHash); }