public OutgoingPushMessage Encrypt(SignalProtocolAddress destination, UnidentifiedAccess unidentifiedAccess, byte[] unpaddedMessage) { if (unidentifiedAccess != null) { SealedSessionCipher sessionCipher = new SealedSessionCipher(SignalProtocolStore, new SignalProtocolAddress(LocalAddress.E164number, 1)); PushTransportDetails transportDetails = new PushTransportDetails((uint)sessionCipher.GetSessionVersion(destination)); byte[] ciphertext = sessionCipher.Encrypt(destination, unidentifiedAccess.UnidentifiedCertificate, transportDetails.getPaddedMessageBody(unpaddedMessage)); String body = Base64.EncodeBytes(ciphertext); uint remoteRegistrationId = (uint)sessionCipher.GetRemoteRegistrationId(destination); return(new OutgoingPushMessage((uint)Envelope.Types.Type.UnidentifiedSender, destination.DeviceId, remoteRegistrationId, body)); } else { SessionCipher sessionCipher = new SessionCipher(SignalProtocolStore, destination); PushTransportDetails transportDetails = new PushTransportDetails(sessionCipher.getSessionVersion()); CiphertextMessage message = sessionCipher.encrypt(transportDetails.getPaddedMessageBody(unpaddedMessage)); uint remoteRegistrationId = sessionCipher.getRemoteRegistrationId(); string body = Base64.EncodeBytes(message.serialize()); var type = (message.getType()) switch { CiphertextMessage.PREKEY_TYPE => (uint)Envelope.Types.Type.PrekeyBundle, CiphertextMessage.WHISPER_TYPE => (uint)Envelope.Types.Type.Ciphertext, _ => throw new Exception("Bad type: " + message.getType()), }; return(new OutgoingPushMessage(type, destination.DeviceId, remoteRegistrationId, body)); } }
public void TestKeyDerivation() { byte[] key = new byte[32]; Arrays.fill(key, 0x02); byte[] result = UnidentifiedAccess.DeriveAccessKeyFrom(key); CollectionAssert.AreEqual(EXPECTED_RESULT, result); }
public bool VerifyUnidentifiedAccess(byte[] theirUnidentifiedAccessVerifier) { if (theirUnidentifiedAccessVerifier == null || theirUnidentifiedAccessVerifier.Length == 0) { return(false); } byte[] unidentifiedAccessKey = UnidentifiedAccess.DeriveAccessKeyFrom(Key); throw new NotImplementedException(); //TODO /* * Mac mac = Mac.getInstance("HmacSHA256"); * mac.init(new SecretKeySpec(unidentifiedAccessKey, "HmacSHA256")); * * byte[] ourUnidentifiedAccessVerifier = mac.doFinal(new byte[32]); * * return MessageDigest.isEqual(theirUnidentifiedAccessVerifier, ourUnidentifiedAccessVerifier); */ }
public bool VerifyUnidentifiedAccess(byte[] theirUnidentifiedAccessVerifier) { try { if (theirUnidentifiedAccessVerifier == null || theirUnidentifiedAccessVerifier.Length == 0) { return(false); } byte[] unidentifiedAccessKey = UnidentifiedAccess.DeriveAccessKeyFrom(key); HMAC mac = new HMACSHA256(unidentifiedAccessKey); byte[] ourUnidentifiedAccessVerifier = mac.ComputeHash(new byte[32]); return(Enumerable.SequenceEqual(theirUnidentifiedAccessVerifier, ourUnidentifiedAccessVerifier)); } catch (InvalidKeyException ex) { throw new ArgumentException(null, ex); } }