Example #1
0
        public virtual byte[] Unwrap(byte[] input, int inOff, int length)
        {
            if (this.forWrapping)
            {
                throw new InvalidOperationException("Not set for unwrapping");
            }
            if (input == null)
            {
                throw new InvalidCipherTextException("Null pointer as ciphertext");
            }
            int blockSize = this.engine.GetBlockSize();

            if (length % blockSize != 0)
            {
                throw new InvalidCipherTextException("Ciphertext not multiple of " + blockSize);
            }
            ParametersWithIV parameters = new ParametersWithIV(this.param, DesEdeWrapEngine.IV2);

            this.engine.Init(false, parameters);
            byte[] array = new byte[length];
            for (int num = 0; num != array.Length; num += blockSize)
            {
                this.engine.ProcessBlock(input, inOff + num, array, num);
            }
            byte[] array2 = DesEdeWrapEngine.reverse(array);
            this.iv = new byte[8];
            byte[] array3 = new byte[array2.Length - 8];
            Array.Copy(array2, 0, this.iv, 0, 8);
            Array.Copy(array2, 8, array3, 0, array2.Length - 8);
            this.paramPlusIV = new ParametersWithIV(this.param, this.iv);
            this.engine.Init(false, this.paramPlusIV);
            byte[] array4 = new byte[array3.Length];
            for (int num2 = 0; num2 != array4.Length; num2 += blockSize)
            {
                this.engine.ProcessBlock(array3, num2, array4, num2);
            }
            byte[] array5 = new byte[array4.Length - 8];
            byte[] array6 = new byte[8];
            Array.Copy(array4, 0, array5, 0, array4.Length - 8);
            Array.Copy(array4, array4.Length - 8, array6, 0, 8);
            if (!this.CheckCmsKeyChecksum(array5, array6))
            {
                throw new InvalidCipherTextException("Checksum inside ciphertext is corrupted");
            }
            return(array5);
        }
Example #2
0
		private void WrapTest(
			int     id,
			byte[]  kek,
			byte[]  iv,
			byte[]  input,
			byte[]  output)
		{
			IWrapper wrapper = new DesEdeWrapEngine();

			wrapper.Init(true, new ParametersWithIV(new DesEdeParameters(kek), iv));

			try
			{
				byte[] cText = wrapper.Wrap(input, 0, input.Length);
				if (!AreEqual(cText, output))
				{
					Fail(": failed wrap test " + id  + " expected " + Hex.ToHexString(output)
						+ " got " + Hex.ToHexString(cText));
				}
			}
			catch (Exception e)
			{
				Fail("failed wrap test exception: " + e.ToString(), e);
			}

			wrapper.Init(false, new DesEdeParameters(kek));

			try
			{
				byte[] pText = wrapper.Unwrap(output, 0, output.Length);
				if (!AreEqual(pText, input))
				{
					Fail("failed unwrap test " + id  + " expected " + Hex.ToHexString(input)
						+ " got " + Hex.ToHexString(pText));
				}
			}
			catch (Exception e)
			{
				Fail("failed unwrap test exception: " + e.ToString(), e);
			}
		}
Example #3
0
        public virtual byte[] Wrap(byte[] input, int inOff, int length)
        {
            if (!this.forWrapping)
            {
                throw new InvalidOperationException("Not initialized for wrapping");
            }
            byte[] array = new byte[length];
            Array.Copy(input, inOff, array, 0, length);
            byte[] array2 = this.CalculateCmsKeyChecksum(array);
            byte[] array3 = new byte[array.Length + array2.Length];
            Array.Copy(array, 0, array3, 0, array.Length);
            Array.Copy(array2, 0, array3, array.Length, array2.Length);
            int blockSize = this.engine.GetBlockSize();

            if (array3.Length % blockSize != 0)
            {
                throw new InvalidOperationException("Not multiple of block length");
            }
            this.engine.Init(true, this.paramPlusIV);
            byte[] array4 = new byte[array3.Length];
            for (int num = 0; num != array3.Length; num += blockSize)
            {
                this.engine.ProcessBlock(array3, num, array4, num);
            }
            byte[] array5 = new byte[this.iv.Length + array4.Length];
            Array.Copy(this.iv, 0, array5, 0, this.iv.Length);
            Array.Copy(array4, 0, array5, this.iv.Length, array4.Length);
            byte[]           array6     = DesEdeWrapEngine.reverse(array5);
            ParametersWithIV parameters = new ParametersWithIV(this.param, DesEdeWrapEngine.IV2);

            this.engine.Init(true, parameters);
            for (int num2 = 0; num2 != array6.Length; num2 += blockSize)
            {
                this.engine.ProcessBlock(array6, num2, array6, num2);
            }
            return(array6);
        }