Ejemplo n.º 1
0
        public override void Digest(Span <byte> output)
        {
            SecurityAssert.AssertBuffer(output, HashSize / 8);
            SecurityAssert.Assert(WorkBufferEmpty);

            _y.CopyTo(output);
        }
Ejemplo n.º 2
0
        public void DecryptBlock(byte[] input, int inputOffset, byte[] output, int outputOffset)
        {
            SecurityAssert.Assert(_keyInitialised);
            SecurityAssert.AssertBuffer(input, inputOffset, BlockLength);
            SecurityAssert.AssertBuffer(output, outputOffset, BlockLength);

            var rounds = KeySize / 4 + 6;

            var state = ToState(input, inputOffset);

            AddRoundKey(state, rounds);

            for (var i = rounds - 1; i > 0; i--)
            {
                InvShiftRows(state);
                InvSubBytes(state);
                AddRoundKey(state, i);
                InvMixColumns(state);
            }

            InvShiftRows(state);
            InvSubBytes(state);
            AddRoundKey(state, 0);

            FromState(state, output, outputOffset);
        }
Ejemplo n.º 3
0
        public RC4KeyParameter(byte[] key, int offset, int length)
        {
            SecurityAssert.AssertBuffer(key, offset, length);

            Key = new byte[length];
            Array.Copy(key, offset, Key, 0, length);
        }
Ejemplo n.º 4
0
        public void Decrypt(byte[] input, int inputOffset, byte[] output, int outputOffset, int length)
        {
            SecurityAssert.AssertBuffer(input, inputOffset, length);
            SecurityAssert.AssertBuffer(output, outputOffset, length - TagLength);

            Cipher.DecryptFinal(Cipher.Decrypt(input, output));
        }
Ejemplo n.º 5
0
        public void EncryptBlock(byte[] input, int inputOffset, byte[] output, int outputOffset)
        {
            SecurityAssert.Assert(_keyInitialised);
            SecurityAssert.AssertBuffer(input, inputOffset, BlockLength);
            SecurityAssert.AssertBuffer(output, outputOffset, BlockLength);

            var rounds = KeySize / 4 + 6;

            var state = ToState(input, inputOffset);

            AddRoundKey(state, 0);

            for (var round = 1; round < rounds; round++)
            {
                SubBytes(state);
                ShiftRows(state);
                MixColumns(state);
                AddRoundKey(state, round);
            }

            SubBytes(state);
            ShiftRows(state);
            AddRoundKey(state, rounds);

            FromState(state, output, outputOffset);
        }
Ejemplo n.º 6
0
        public void Decrypt(byte[] input, int inputOffset, byte[] output, int outputOffset, int length)
        {
            SecurityAssert.AssertBuffer(input, inputOffset, length);
            SecurityAssert.NotNull(PrivateKey);

            var k = PrivateKey !.Modulus.GetByteLength();

            SecurityAssert.Assert(k >= 11);
            SecurityAssert.Assert(length == k);

            var c = OS2IP(input, inputOffset, length);
            var m = DecryptPrimative(c, PrivateKey !);

            var em = I2OSP(m, k);

            SecurityAssert.Assert(em[0] == 0 && em[1] == 2);

            var mIdx = 2;

            while (mIdx < k && em[mIdx] != 0)
            {
                mIdx++;
            }

            SecurityAssert.Assert(mIdx - 2 > 8);
            // advance past zero
            mIdx++;

            SecurityAssert.AssertBuffer(output, outputOffset, k - mIdx);
            Array.Copy(em, mIdx, output, outputOffset, k - mIdx);
        }
Ejemplo n.º 7
0
        public void Encrypt(byte[] input, int inputOffset, byte[] output, int outputOffset, int length)
        {
            SecurityAssert.AssertBuffer(input, inputOffset, length);
            SecurityAssert.NotNull(PublicKey);

            var k = PublicKey !.Modulus.GetByteLength();

            SecurityAssert.Assert(length <= k - 11);

            var ps = _random.RandomNonZeroBytes(k - length - 3);

            SecurityAssert.Assert(ps.Length >= 8);

            var em = new byte[k];

            em[0] = 0;
            em[1] = 2;
            Array.Copy(ps, 0, em, 2, ps.Length);
            em[ps.Length + 2] = 0;
            Array.Copy(input, inputOffset, em, ps.Length + 3, length);

            var m = OS2IP(em, 0, em.Length);
            var c = EncryptPrimative(m, PublicKey !);

            var result = I2OSP(c, k);

            SecurityAssert.AssertBuffer(output, outputOffset, result.Length);
            Array.Copy(result, 0, output, outputOffset, result.Length);
        }
Ejemplo n.º 8
0
        public override void Digest(Span <byte> output)
        {
            SecurityAssert.AssertBuffer(output, HashSize / 8);

            var paddingLength = 64 - MessageSize % BlockSize / 8;

            if (paddingLength <= 8)
            {
                paddingLength += 64;
            }

            var padding = new byte[paddingLength];

            // first bit is 1
            padding[0] = 0x80;

            Array.Copy(EndianBitConverter.Big.GetBytes(MessageSize), 0, padding, paddingLength - 8, 8);

            Update(padding);
            SecurityAssert.Assert(WorkBufferEmpty);

            _complete = true;

            EndianBitConverter.Big.GetBytes(_h0).CopyTo(output);
            EndianBitConverter.Big.GetBytes(_h1).CopyTo(output.Slice(4));
            EndianBitConverter.Big.GetBytes(_h2).CopyTo(output.Slice(8));
            EndianBitConverter.Big.GetBytes(_h3).CopyTo(output.Slice(12));
            EndianBitConverter.Big.GetBytes(_h4).CopyTo(output.Slice(16));
        }
Ejemplo n.º 9
0
        public void Decrypt(byte[] input, int inputOffset, byte[] output, int outputOffset, int length)
        {
            SecurityAssert.AssertBuffer(input, inputOffset, length);
            SecurityAssert.AssertBuffer(output, outputOffset, length);

            Array.Copy(input, inputOffset, output, outputOffset, length);
        }
Ejemplo n.º 10
0
        public void AssertBuffer_WhenValid_ShouldDoNothing()
        {
            var a = new byte[] { 0, 1, 2, 3 };

            SecurityAssert.AssertBuffer(a, 0, a.Length);
            SecurityAssert.AssertBuffer(a, 3, 1);
            SecurityAssert.AssertBuffer(a, 1, 3);
        }
Ejemplo n.º 11
0
        public void UpdateVerification(byte[] buffer, int offset, int length)
        {
            SecurityAssert.AssertBuffer(buffer, offset, length);
            var output = new byte[length];

            Array.Copy(buffer, offset, output, 0, length);

            _messages.Add(output);
        }
Ejemplo n.º 12
0
        public void Decrypt(byte[] input, int inputOffset, byte[] output, int outputOffset, int length)
        {
            SecurityAssert.AssertBuffer(input, inputOffset, length);
            SecurityAssert.AssertBuffer(output, outputOffset, length);

            for (var i = 0; i < length; i += BlockCipher.BlockLength)
            {
                BlockCipher.DecryptBlock(input, inputOffset + i, output, outputOffset + i);
            }
        }
Ejemplo n.º 13
0
        public override void DecryptBlock(byte[] input, int inputOffset, byte[] output, int outputOffset)
        {
            SecurityAssert.Assert(IVInitialised);
            SecurityAssert.AssertBuffer(input, inputOffset, BlockLength);
            SecurityAssert.AssertBuffer(output, outputOffset, BlockLength);

            Cipher.DecryptBlock(input, inputOffset, output, outputOffset);

            BufferUtils.Xor(_workingIV, 0, output, outputOffset, BlockLength);
            Array.Copy(input, inputOffset, _workingIV, 0, BlockLength);
        }
Ejemplo n.º 14
0
        public void Encrypt(byte[] input, int inputOffset, byte[] output, int outputOffset, int length)
        {
            SecurityAssert.Assert(_keyInitialised);
            SecurityAssert.AssertBuffer(input, inputOffset, length);
            SecurityAssert.AssertBuffer(output, outputOffset, length);

            for (var offset = 0; offset < length; offset++)
            {
                output[outputOffset + offset] = (byte)(input[inputOffset + offset] ^ NextPGRA());
            }
        }
Ejemplo n.º 15
0
        public static BlockResult EncryptBlock(this IBlockCipher cipher, ReadOnlySpan <byte> input, Span <byte> output)
        {
            SecurityAssert.AssertBuffer(input, cipher.BlockLength);
            SecurityAssert.AssertBuffer(output, cipher.BlockLength);

            var inputBuffer  = input.Slice(0, cipher.BlockLength).ToArray();
            var outputBuffer = new byte[cipher.BlockLength];

            cipher.EncryptBlock(inputBuffer, 0, outputBuffer, 0);

            outputBuffer.CopyTo(output);

            return(new BlockResult(input.Slice(cipher.BlockLength), output.Slice(cipher.BlockLength)));
        }
Ejemplo n.º 16
0
        public override void Write(byte[] buffer, int offset, int count)
        {
            SecurityAssert.Assert(_active);
            SecurityAssert.AssertBuffer(buffer, offset, count);

            var data = new byte[count];

            Array.Copy(buffer, offset, data, 0, count);

            var versionConfig = Services.GetRequiredService <VersionConfig>();
            var record        = new Record(RecordType.Application, versionConfig.Version, data);

            var connection = Services.GetRequiredService <Connection>();

            connection.WriteRecord(record);
        }
Ejemplo n.º 17
0
        private void ProcessBlock(byte[] input, int inputOffset, byte[] output, int outputOffset)
        {
            SecurityAssert.AssertBuffer(input, inputOffset, BlockLength);
            SecurityAssert.AssertBuffer(output, outputOffset, BlockLength);

            var counterOutput = new byte[BlockLength];

            Cipher.EncryptBlock(_counter, 0, counterOutput, 0);

            // XOR input
            for (var i = 0; i < BlockLength; i++)
            {
                output[outputOffset + i] = (byte)(counterOutput[i] ^ input[inputOffset + i]);
            }

            // increment counter
            Inc();
        }
Ejemplo n.º 18
0
        public override void Digest(Span <byte> output)
        {
            SecurityAssert.AssertBuffer(output, HashSize / 8);

            // TODO same as SHA1...

            var paddingLength = 128 - MessageSize % BlockSize / 8;

            if (paddingLength <= 16)
            {
                paddingLength += 128;
            }

            var padding = new byte[paddingLength];

            // first bit is 1
            padding[0] = 0x80;

            // TODO messagesize should be 128-bits, this only 64 bits and the upper 64 are implicity zeroed in the padding
            Array.Copy(EndianBitConverter.Big.GetBytes(MessageSize), 0, padding, paddingLength - 8, 8);

            Update(padding);
            SecurityAssert.Assert(WorkBufferEmpty);

            _complete = true;

            EndianBitConverter.Big.GetBytes(_h0).CopyTo(output);
            EndianBitConverter.Big.GetBytes(_h1).CopyTo(output.Slice(8));
            EndianBitConverter.Big.GetBytes(_h2).CopyTo(output.Slice(16));
            EndianBitConverter.Big.GetBytes(_h3).CopyTo(output.Slice(24));
            EndianBitConverter.Big.GetBytes(_h4).CopyTo(output.Slice(32));
            EndianBitConverter.Big.GetBytes(_h5).CopyTo(output.Slice(40));

            if (_mode == Mode.SHA512)
            {
                EndianBitConverter.Big.GetBytes(_h6).CopyTo(output.Slice(48));
                EndianBitConverter.Big.GetBytes(_h7).CopyTo(output.Slice(56));
            }
        }
Ejemplo n.º 19
0
        public static AlertMessage Read(byte[] data)
        {
            SecurityAssert.AssertBuffer(data, 0, 2);

            return(new AlertMessage((AlertLevel)data[0], (AlertDescription)data[1]));
        }
Ejemplo n.º 20
0
        public void AssertBuffer_WhenOffsetIsNegative_ShouldThrowSecurityException()
        {
            var a = new byte[] { 0, 1, 2, 3 };

            Assert.Throws <SecurityException>(() => SecurityAssert.AssertBuffer(a, -1, 0));
        }
Ejemplo n.º 21
0
 public void AssertBuffer_WhenBufferIsNull_ShouldThrowSecurityException()
 {
     Assert.Throws <SecurityException>(() => SecurityAssert.AssertBuffer(null, 0, 0));
 }
Ejemplo n.º 22
0
        public void AssertBuffer_WhenOverflow_ShouldThrowSecurityException()
        {
            var a = new byte[] { 0, 1, 2, 3 };

            Assert.Throws <SecurityException>(() => SecurityAssert.AssertBuffer(a, 3, 3));
        }