Example #1
0
        /// <summary>
        /// Encrypts some data with some key under the use of a nonce and prepends the ciphertext with the nonce
        /// </summary>
        /// <returns>The data.</returns>
        /// <param name="data">Data.</param>
        /// <param name="key">Key.</param>
        /// <param name="nonce">Nonce.</param>
        public static byte[] EncryptData(byte[] data, byte[] key, byte[] nonce = null)
        {
            long cipherTextLength = data.Length + 16;

            byte[] cipherText = new byte[cipherTextLength];
            if (nonce == null)
            {
                nonce = StreamEncryption.GetRandomBytes(X_NONC_ESIZE);
            }

            int result = NativeLibsodium.crypto_aead_xchacha20poly1305_ietf_encrypt(
                cipherText,
                out cipherTextLength,
                data,
                data.Length,
                null, 0, null,
                nonce,
                key);

            if (result != 0)
            {
                throw new Exception("Encryption error");
            }

            byte[] cipherTextWithNonce = IEncryption.ConcatBytes(nonce, cipherText);

            return(cipherTextWithNonce);
        }
Example #2
0
        /// <summary>
        /// Encrypts data with the current session key.
        /// </summary>
        /// <returns>The encrypted data.</returns>
        /// <param name="message">Data to encrypt.</param>
        public byte[] EncryptWithSessionKey(byte[] message)
        {
            byte[] nonce = IEncryption.ConcatBytes(StreamEncryption.GetRandomBytes(X_NONC_ESIZE - 16), BitConverter.GetBytes(DateTime.UtcNow.Ticks), BitConverter.GetBytes(sessionSendKey.index));
            byte[] cipherTextWithNonce = EncryptData(message, GetCurrentSendKey(), nonce);

            // ratchat forward
            RatchetSendKey();

            return(cipherTextWithNonce);
        }
Example #3
0
        public override void Execute(CommandData data)
        {
            var returnData = ExecuteWithReturn(data);

            // set headers
            returnData.Type      = "response";
            returnData.Recipient = data.SenderId;
            returnData.SenderId  = data.Recipient;

            // wrap it into a container so receiver knows to what request it coresponds to
            returnData.message = IEncryption.ConcatBytes(BitConverter.GetBytes(data.MessageId), returnData.message);
            data.SendBack(returnData);
            //SendTo(data.sId, user.PublicId, "createdUser");
        }
Example #4
0
        /// <summary>
        /// Derives session keys from four key pairs and stores them in intern variables.
        /// </summary>
        /// <returns>Void.</returns>
        /// <param name="ePPK">E PP.</param>
        /// <param name="ePK">E P.</param>
        /// <param name="iPK">I P.</param>
        /// <param name="ePOTK">E POT.</param>
        private void DeriveSessionKeys(KeyPair ePPK, KeyPair ePK, KeyPair iPK, KeyPair ePOTK)
        {
            sessionReceiveKey.key = LibsodiumEncryption.Hash(IEncryption.ConcatBytes(ePPK.secretKey, ePK.secretKey, iPK.secretKey, ePOTK.secretKey));
            sessionSendKey.key    = LibsodiumEncryption.Hash(IEncryption.ConcatBytes(ePPK.publicKey, ePK.publicKey, iPK.publicKey, ePOTK.publicKey));

            /*
             * NotificationHandler.Instance.ShowNotification("",
             * "sessionReceiveKey is : " + Convert.ToBase64String(sessionReceiveKey) +
             * " sessionSendKey is : " + Convert.ToBase64String(sessionSendKey) +
             * " ePPK s is : " + Convert.ToBase64String(ePPK.secretKey) +
             * " ePK s is : " + Convert.ToBase64String(ePK.secretKey) +
             * " iPK s is : " + Convert.ToBase64String(iPK.secretKey) +
             * " ePOTK s is : " + Convert.ToBase64String(ePOTK.secretKey) +
             * " ePPK is : " + Convert.ToBase64String(ePPK.publicKey) +
             * " ePK is : " + Convert.ToBase64String(ePK.publicKey) +
             * " iPK is : " + Convert.ToBase64String(iPK.publicKey) +
             * " ePOTK is : " + Convert.ToBase64String(ePOTK.publicKey));*/
        }
Example #5
0
 /// <summary>
 /// Advances the key.
 /// </summary>
 /// <returns>The key.</returns>
 /// <param name="key">Key.</param>
 public static byte[] AdvanceKey(byte[] key)
 {
     return(LibsodiumEncryption.Hash(IEncryption.ConcatBytes(key, new byte[] { 0x02 })));
 }
Example #6
0
 /// <summary>
 /// Gets the enc key from the chainkey.
 /// Does this by appending 0x01 to and hashing the current key
 /// </summary>
 /// <returns>The enc key from chain.</returns>
 /// <param name="chainKey">Chain key.</param>
 public static byte[] GetEncKeyFromChain(ChainKey chainKey)
 {
     return(LibsodiumEncryption.Hash(IEncryption.ConcatBytes(chainKey.key, new byte[] { 0x01 })));
 }
Example #7
0
 /// <summary>
 /// Converts a chain key to an Encryption key for actual usage
 /// </summary>
 /// <returns>The encryption key.</returns>
 /// <param name="chainKey">Chain key.</param>
 private byte[] ChainToEncryptionKey(byte[] chainKey)
 {
     return(LibsodiumEncryption.Hash(IEncryption.ConcatBytes(chainKey, new byte[] { 0x01 })));
 }