Represents an AES encryption transformer.
Beispiel #1
0
        public void GetPacketHeader_Throws_For_Null_Segment()
        {
            Action getPacketLength = () => RollingIv.GetPacketLength(null);

            getPacketLength
            .ShouldThrow <ArgumentNullException>();
        }
Beispiel #2
0
        public void GetPacketHeader_Throws_When_Segment_Shorter_Than_4_Bytes()
        {
            Action getPacketLength = () => RollingIv.GetPacketLength(new byte[] { 0x01, 0x02, 0x03 });

            getPacketLength
            .ShouldThrow <ArgumentException>()
            .WithMessagePrefix(SegmentMustBeLongerThan4());
        }
Beispiel #3
0
        public void Transform_Throws_On_Null_Buffer()
        {
            var algorithm = Mock.Of <ICryptoAlgorithm>();
            var rollingIv = new RollingIv(algorithm, new byte[] { 0x12, 0x34, 0x56, 0x78 }, 0x1234);

            rollingIv
            .Invoking(iv => iv.Transform(null))
            .ShouldThrow <ArgumentNullException>();
        }
Beispiel #4
0
        public void ValidateHeader_Throws_On_Null_Segment()
        {
            var algorithm = new Mock <ICryptoAlgorithm>(MockBehavior.Loose);
            var initialIv = new byte[] { 0x12, 0x34, 0x56, 0x78 };
            var rollingIv = new RollingIv(algorithm.Object, initialIv, 0x1234);

            rollingIv
            .Invoking(iv => iv.ValidateHeader(null))
            .ShouldThrow <ArgumentNullException>();
        }
Beispiel #5
0
        public void ConstructHeader_Throws_For_Packets_Shorter_Than_2_Bytes()
        {
            var algorithm = new Mock <ICryptoAlgorithm>(MockBehavior.Loose);
            var initialIv = new byte[] { 0x12, 0x34, 0x56, 0x78 };
            var rollingIv = new RollingIv(algorithm.Object, initialIv, 0x1234);

            rollingIv
            .Invoking(iv => iv.ConstructHeader(1).Whatever())
            .ShouldThrow <ArgumentOutOfRangeException>()
            .WithMessagePrefix(CommonStrings.PacketLengthMustBeMoreThan2Bytes);
        }
Beispiel #6
0
        public void ValidateHeader_Throws_When_Segment_Shorter_Than_4_Bytes()
        {
            var algorithm = new Mock <ICryptoAlgorithm>(MockBehavior.Loose);
            var initialIv = new byte[] { 0x12, 0x34, 0x56, 0x78 };
            var rollingIv = new RollingIv(algorithm.Object, initialIv, 0x1234);

            rollingIv
            .Invoking(iv => iv.ValidateHeader(new byte[] { 0x01, 0x02, 0x03 }))
            .ShouldThrow <ArgumentException>()
            .WithMessagePrefix(SegmentMustBeLongerThan4());
        }
Beispiel #7
0
        public void ConstructHeader_Throws_For_Packets_Shorter_Than_2_Bytes()
        {
            var algorithm = new Mock<ICryptoAlgorithm>(MockBehavior.Loose);
            var initialIv = new byte[] { 0x12, 0x34, 0x56, 0x78 };
            var rollingIv = new RollingIv(algorithm.Object, initialIv, 0x1234);

            rollingIv
                .Invoking(iv => iv.ConstructHeader(1).Whatever())
                .ShouldThrow<ArgumentOutOfRangeException>()
                .WithMessagePrefix(CommonStrings.PacketLengthMustBeMoreThan2Bytes);
        }
Beispiel #8
0
        public void Transform_Calls_TransformArraySegment_And_ShuffleIv()
        {
            var algorithm = new Mock <ICryptoAlgorithm>(MockBehavior.Loose);
            var initialIv = new byte[] { 0x12, 0x34, 0x56, 0x78 };
            var rollingIv = new RollingIv(algorithm.Object, initialIv, 0x1234);
            var data      = new byte[] { 0x12, 0x34 };

            rollingIv.Transform(data);

            algorithm.Verify(m => m.TransformArraySegment(AnyByteArray(), AnyByteArray(), 0, 2), Times.Once());
            algorithm.Verify(m => m.ShuffleIv(AnyByteArray()), Times.Once());
        }
Beispiel #9
0
        public void Transform_Calls_TransformArraySegment_And_ShuffleIv()
        {
            var algorithm = new Mock<ICryptoAlgorithm>(MockBehavior.Loose);
            var initialIv = new byte[] { 0x12, 0x34, 0x56, 0x78 };
            var rollingIv = new RollingIv(algorithm.Object, initialIv, 0x1234);
            var data = new byte[] { 0x12, 0x34 };

            rollingIv.Transform(data);

            algorithm.Verify(m => m.TransformArraySegment(AnyByteArray(), AnyByteArray(), 0, 2), Times.Once());
            algorithm.Verify(m => m.ShuffleIv(AnyByteArray()), Times.Once());
        }
 /// <summary>
 /// Attempts to extract the length of a packet from its header.
 /// </summary>
 /// <remarks>
 /// When overriding this method in a derived class,
 /// do not call the base implementation.
 /// </remarks>
 /// <param name="header">The header byte array to process.</param>
 /// <param name="length">A variable to hold the result.</param>
 /// <returns><see langword="true"/> if the extraction was successful; otherwise, <see langword="false"/>.</returns>
 public bool TryGetLength(byte[] header, out int length)
 {
     if (_decryptor.ValidateHeader(header))
     {
         length = RollingIv.GetPacketLength(header);
         return(true);
     }
     else
     {
         length = default(int);
         return(false);
     }
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="EndpointCrypto"/> class.
 /// </summary>
 /// <param name="encryptor">The IV used for encryption.</param>
 /// <param name="decryptor">The IV used for decryption.</param>
 private EndpointCrypto(RollingIv encryptor, RollingIv decryptor)
 {
     _encryptor = encryptor;
     _decryptor = decryptor;
 }
Beispiel #12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="EndpointCrypto"/> class.
 /// </summary>
 /// <param name="encryptor">The IV used for encryption.</param>
 /// <param name="decryptor">The IV used for decryption.</param>
 private EndpointCrypto(RollingIv encryptor, RollingIv decryptor)
 {
     this.encryptor = encryptor;
     this.decryptor = decryptor;
 }
Beispiel #13
0
 public void Transform_Throws_On_Null_Buffer()
 {
     var algorithm = Mock.Of<ICryptoAlgorithm>();
     var rollingIv = new RollingIv(algorithm, new byte[] { 0x12, 0x34, 0x56, 0x78 }, 0x1234);
     rollingIv
         .Invoking(iv => iv.Transform(null))
         .ShouldThrow<ArgumentNullException>();
 }
Beispiel #14
0
        public void ValidateHeader_Throws_When_Segment_Shorter_Than_4_Bytes()
        {
            var algorithm = new Mock<ICryptoAlgorithm>(MockBehavior.Loose);
            var initialIv = new byte[] { 0x12, 0x34, 0x56, 0x78 };
            var rollingIv = new RollingIv(algorithm.Object, initialIv, 0x1234);

            rollingIv
                .Invoking(iv => iv.ValidateHeader(new byte[] { 0x01, 0x02, 0x03 }))
                .ShouldThrow<ArgumentException>()
                .WithMessagePrefix(SegmentMustBeLongerThan4());
        }
Beispiel #15
0
        public void ValidateHeader_Throws_On_Null_Segment()
        {
            var algorithm = new Mock<ICryptoAlgorithm>(MockBehavior.Loose);
            var initialIv = new byte[] { 0x12, 0x34, 0x56, 0x78 };
            var rollingIv = new RollingIv(algorithm.Object, initialIv, 0x1234);

            rollingIv
                .Invoking(iv => iv.ValidateHeader(null))
                .ShouldThrow<ArgumentNullException>();
        }