Beispiel #1
0
        private byte[] GetMac(uint messageVersion,
                              IdentityKey senderIdentityKey,
                              IdentityKey receiverIdentityKey,
                              byte[] macKey, byte[] serialized)
        {
            try
            {
                MemoryStream stream = new MemoryStream();
                if (messageVersion >= 3)
                {
                    byte[] sik = senderIdentityKey.GetPublicKey().Serialize();
                    stream.Write(sik, 0, sik.Length);
                    byte[] rik = receiverIdentityKey.GetPublicKey().Serialize();
                    stream.Write(rik, 0, rik.Length);
                }

                stream.Write(serialized, 0, serialized.Length);
                byte[] fullMac = Sign.Sha256Sum(macKey, stream.ToArray());
                return(ByteUtil.Trim(fullMac, MacLength));
            }
            catch (/*NoSuchAlgorithmException | java.security.InvalidKey*/ Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="recipientId"></param>
        /// <param name="identityKey"></param>
        public bool SaveIdentity(string recipientId, IdentityKey identityKey)
        {
            if (IdentitiesObjectDic.ContainsKey(recipientId))
            {
                IdentitiesObjectDic.Remove(recipientId);
            }

            IdentitiesObjectDic.Add(recipientId, new IdentitiesObject()
            {
                RecipientId = recipientId,
                PublicKey   = identityKey.GetPublicKey().Serialize()
            });

            return(true);
        }
        public bool SaveIdentity(string recipientId, IdentityKey identityKey)
        {
            IdentityKeysRepository identityKeysRepository = new IdentityKeysRepository();
            List <IdentityKeys>    identityKeys           = identityKeysRepository.GetIdentityKeys(recipientId);

            if (identityKeys != null && identityKeys.Count > 0)
            {
                IdentityKeys identity = identityKeys.First();
                identityKeysRepository.Delete(identity);
            }

            IdentityKeys newKeys = new IdentityKeys()
            {
                RecipientId = recipientId,
                PublicKey   = identityKey.GetPublicKey().Serialize()
            };

            return(identityKeysRepository.Save(newKeys));
        }
        public DeviceConsistencyMessage(DeviceConsistencyCommitment commitment, byte[] serialized, IdentityKey identityKey)
        {
            try
            {
                SignalProtos.DeviceConsistencyCodeMessage message = SignalProtos.DeviceConsistencyCodeMessage.ParseFrom(serialized);
                byte[] vrfOutputBytes = Curve.VerifyVrfSignature(identityKey.GetPublicKey(), commitment.ToByteArray(), message.Signature.ToByteArray());

                _generation = (int)message.Generation;
                _signature  = new DeviceConsistencySignature(message.Signature.ToByteArray(), vrfOutputBytes);
                _serialized = serialized;
            }
            catch (InvalidProtocolBufferException e)
            {
                throw new InvalidMessageException(e);
            }
            catch (InvalidKeyException e)
            {
                throw new InvalidMessageException(e);
            }
            catch (VrfSignatureVerificationFailedException e)
            {
                throw new InvalidMessageException(e);
            }
        }