Beispiel #1
0
        /**
         * Encrypt a message.
         *
         * @param paddedPlaintext The plaintext message bytes, optionally padded.
         * @return Ciphertext.
         * @throws NoSessionException
         */

        public byte[] Encrypt(byte[] paddedPlaintext)
        {
            lock (LOCK)
            {
                try
                {
                    SenderKeyRecord  record         = senderKeyStore.LoadSenderKey(senderKeyId);
                    SenderKeyState   senderKeyState = record.GetSenderKeyState();
                    SenderMessageKey senderKey      = senderKeyState.GetSenderChainKey().GetSenderMessageKey();
                    byte[]           ciphertext     = GetCipherText(senderKey.GetIv(), senderKey.GetCipherKey(), paddedPlaintext);

                    SenderKeyMessage senderKeyMessage = new SenderKeyMessage(senderKeyState.GetKeyId(),
                                                                             senderKey.GetIteration(),
                                                                             ciphertext,
                                                                             senderKeyState.GetSigningKeyPrivate());

                    senderKeyState.SetSenderChainKey(senderKeyState.GetSenderChainKey().GetNext());
                    senderKeyStore.StoreSenderKey(senderKeyId, record);

                    return(senderKeyMessage.Serialize());
                }
                catch (InvalidKeyIdException e)
                {
                    throw new NoSessionException(e);
                }
            }
        }
        public void AddSenderMessageKey(SenderMessageKey senderMessageKey)
        {
            SenderKeyStateStructure.Types.SenderMessageKey senderMessageKeyStructure = new SenderKeyStateStructure.Types.SenderMessageKey
            {
                Iteration = senderMessageKey.GetIteration(),
                Seed      = ByteString.CopyFrom(senderMessageKey.GetSeed())
            };
            _senderKeyStateStructure.SenderMessageKeys.Add(senderMessageKeyStructure);

            if (_senderKeyStateStructure.SenderMessageKeys.Count > MaxMessageKeys)
            {
                _senderKeyStateStructure.SenderMessageKeys.RemoveAt(0);
            }
        }
        public void AddSenderMessageKey(SenderMessageKey senderMessageKey)
        {
            SenderKeyStateStructure.Types.SenderMessageKey senderMessageKeyStructure =
                SenderKeyStateStructure.Types.SenderMessageKey.CreateBuilder()
                .SetIteration(senderMessageKey.GetIteration())
                .SetSeed(ByteString.CopyFrom(senderMessageKey.GetSeed()))
                .Build();

            SenderKeyStateStructure.Builder builder = this.senderKeyStateStructure.ToBuilder();
            builder.AddSenderMessageKeys(senderMessageKeyStructure);

            if (builder.SenderMessageKeysList.Count > MAX_MESSAGE_KEYS)
            {
                builder.SenderMessageKeysList.RemoveAt(0);
            }
            this.senderKeyStateStructure = builder.Build();
        }
Beispiel #4
0
        public void AddSenderMessageKey(SenderMessageKey senderMessageKey)
        {
            StorageProtos.SenderKeyStateStructure.Types.SenderMessageKey senderMessageKeyStructure =
                StorageProtos.SenderKeyStateStructure.Types.SenderMessageKey.CreateBuilder()
                .SetIteration(senderMessageKey.GetIteration())
                .SetSeed(ByteString.CopyFrom(senderMessageKey.GetSeed()))
                .Build();

            StorageProtos.SenderKeyStateStructure.Builder builder = _senderKeyStateStructure.ToBuilder();
            builder.AddSenderMessageKeys(senderMessageKeyStructure);

            if (builder.SenderMessageKeysList.Count > MaxMessageKeys)
            {
                builder.SenderMessageKeysList.RemoveAt(0);
            }
            _senderKeyStateStructure = builder.Build();
        }