Example #1
0
        protected /*virtual */ BufferSegment CalculateRecordMac(KeyParameter macKey, BufferSegment additionalData, byte[] buf, int off, int len)
        {
            Poly1305 mac = new Poly1305();

            mac.Init(macKey);

            UpdateRecordMacText(mac, additionalData);
            UpdateRecordMacText(mac, new BufferSegment(buf, off, len));
            UpdateRecordMacLength(mac, additionalData.Count);
            UpdateRecordMacLength(mac, len);

            return(MacUtilities.DoFinalOptimized(mac));
        }
Example #2
0
        /**
         * Calculate the MAC for some given data.
         *
         * @param type    The message type of the message.
         * @param message A byte-buffer containing the message.
         * @param offset  The number of bytes to skip, before the message starts.
         * @param length  The length of the message.
         * @return A new byte-buffer containing the MAC value.
         */
        public /*virtual */ BufferSegment CalculateMac(long seqNo, byte type, byte[] message, int offset, int length)
        {
            ProtocolVersion serverVersion = context.ServerVersion;
            bool            isSsl         = serverVersion.IsSsl;

            int macHeaderLength = isSsl ? 11 : 13;

            byte[] macHeader = BufferPool.Get(macHeaderLength, true);
            TlsUtilities.WriteUint64(seqNo, macHeader, 0);
            TlsUtilities.WriteUint8(type, macHeader, 8);
            if (!isSsl)
            {
                TlsUtilities.WriteVersion(serverVersion, macHeader, 9);
            }
            TlsUtilities.WriteUint16(length, macHeader, macHeaderLength - 2);

            mac.BlockUpdate(macHeader, 0, macHeaderLength);
            mac.BlockUpdate(message, offset, length);

            BufferPool.Release(macHeader);

            return(Truncate(MacUtilities.DoFinalOptimized(mac)));
        }