Base class for cipher padding implementations
Esempio n. 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BlockCipher"/> class.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="blockSize">Size of the block.</param>
        /// <param name="mode">Cipher mode.</param>
        /// <param name="padding">Cipher padding.</param>
        /// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
        protected BlockCipher(byte[] key, int blockSize, CipherMode mode, CipherPadding padding)
            : base(key)
        {
            this._blockSize = blockSize;
            this._mode = mode;
            this._padding = padding;

            this._mode.Init(this);
        }
Esempio n. 2
0
		private int _x0, _x1, _x2, _x3;    // registers

		/// <summary>
		/// Initializes a new instance of the <see cref="SerpentCipher"/> class.
		/// </summary>
		/// <param name="key">The key.</param>
		/// <param name="mode">The mode.</param>
		/// <param name="padding">The padding.</param>
		/// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
		/// <exception cref="ArgumentException">Keysize is not valid for this algorithm.</exception>
		public SerpentCipher(byte[] key, CipherMode mode, CipherPadding padding)
			: base(key, 16, mode, padding)
		{
			var keySize = key.Length * 8;

			if (!(keySize == 128 || keySize == 192 || keySize == 256))
				throw new ArgumentException(string.Format("KeySize '{0}' is not valid for this algorithm.", keySize));

			this._workingKey = this.MakeWorkingKey(key);
		}
Esempio n. 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CastCipher"/> class.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="mode">The mode.</param>
        /// <param name="padding">The padding.</param>
        /// <exception cref="ArgumentNullException"><paramref name="key"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">Keysize is not valid for this algorithm.</exception>
        public CastCipher(byte[] key, CipherMode mode, CipherPadding padding)
            : base(key, 8, mode, padding)
        {
            var keySize = key.Length * 8;

            if (!(keySize >= 40 && keySize <= 128 && keySize % 8 == 0))
                throw new ArgumentException(string.Format("KeySize '{0}' is not valid for this algorithm.", keySize));

            SetKey(key);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="AesCipher"/> class.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="mode">The mode.</param>
        /// <param name="padding">The padding.</param>
        /// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
        /// <exception cref="ArgumentException">Keysize is not valid for this algorithm.</exception>
        public AesCipher(byte[] key, CipherMode mode, CipherPadding padding)
            : base(key, 16, mode, padding)
        {
            var keySize = key.Length * 8;

            if (!(keySize == 256 || keySize == 192 || keySize == 128))
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "KeySize '{0}' is not valid for this algorithm.", keySize));
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BlockCipher"/> class.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="blockSize">Size of the block.</param>
        /// <param name="mode">Cipher mode.</param>
        /// <param name="padding">Cipher padding.</param>
        /// <exception cref="ArgumentNullException"><paramref name="key"/> is <c>null</c>.</exception>
        protected BlockCipher(byte[] key, byte blockSize, CipherMode mode, CipherPadding padding)
            : base(key)
        {
            _blockSize = blockSize;
            _mode = mode;
            _padding = padding;

            if (_mode != null)
                _mode.Init(this);
        }
Esempio n. 6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CastCipher"/> class.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="mode">The mode.</param>
        /// <param name="padding">The padding.</param>
        /// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
        /// <exception cref="ArgumentException">Keysize is not valid for this algorithm.</exception>
        public CastCipher(byte[] key, CipherMode mode, CipherPadding padding)
            : base(key, 8, mode, padding)
        {
            var keySize = key.Length * 8;

            if (!(keySize >= 40 && keySize <= 128 && keySize % 8 == 0))
            {
                throw new ArgumentException(string.Format("KeySize '{0}' is not valid for this algorithm.", keySize));
            }

            this.SetKey(key);
        }
Esempio n. 7
0
        private int _x0, _x1, _x2, _x3;    // registers

        /// <summary>
        /// Initializes a new instance of the <see cref="SerpentCipher"/> class.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="mode">The mode.</param>
        /// <param name="padding">The padding.</param>
        /// <exception cref="ArgumentNullException"><paramref name="key"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">Keysize is not valid for this algorithm.</exception>
        public SerpentCipher(byte[] key, CipherMode mode, CipherPadding padding)
            : base(key, 16, mode, padding)
        {
            var keySize = key.Length * 8;

            if (!(keySize == 128 || keySize == 192 || keySize == 256))
            {
                throw new ArgumentException(string.Format("KeySize '{0}' is not valid for this algorithm.", keySize));
            }

            _workingKey = MakeWorkingKey(key);
        }
Esempio n. 8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BlowfishCipher"/> class.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="mode">The mode.</param>
        /// <param name="padding">The padding.</param>
        /// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
        /// <exception cref="ArgumentException">Keysize is not valid for this algorithm.</exception>
        public BlowfishCipher(byte[] key, CipherMode mode, CipherPadding padding)
            : base(key, 8, mode, padding)
        {
            var keySize = key.Length * 8;

            if (keySize < 1 || keySize > 448)
            {
                throw new ArgumentException(string.Format("KeySize '{0}' is not valid for this algorithm.", keySize));
            }

            _s0 = new uint[SboxSk];
            _s1 = new uint[SboxSk];
            _s2 = new uint[SboxSk];
            _s3 = new uint[SboxSk];
            _p  = new uint[PSize];

            SetKey(key);
        }
Esempio n. 9
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TwofishCipher"/> class.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="mode">The mode.</param>
        /// <param name="padding">The padding.</param>
        /// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
        /// <exception cref="ArgumentException">Keysize is not valid for this algorithm.</exception>
        public TwofishCipher(byte[] key, CipherMode mode, CipherPadding padding)
            : base(key, 16, mode, padding)
        {
            var keySize = key.Length * 8;

            if (!(keySize == 128 || keySize == 192 || keySize == 256))
            {
                throw new ArgumentException(string.Format("KeySize '{0}' is not valid for this algorithm.", keySize));
            }

            //  TODO:   Refactor this algorithm

            // calculate the MDS matrix
            int[] m1 = new int[2];
            int[] mX = new int[2];
            int[] mY = new int[2];
            int   j;

            for (int i = 0; i < MAX_KEY_BITS; i++)
            {
                j     = P[0 + i] & 0xff;
                m1[0] = j;
                mX[0] = Mx_X(j) & 0xff;
                mY[0] = Mx_Y(j) & 0xff;

                j     = P[(1 * 256) + i] & 0xff;
                m1[1] = j;
                mX[1] = Mx_X(j) & 0xff;
                mY[1] = Mx_Y(j) & 0xff;

                gMDS0[i] = m1[P_00] | mX[P_00] << 8 | mY[P_00] << 16 | mY[P_00] << 24;

                gMDS1[i] = mY[P_10] | mY[P_10] << 8 | mX[P_10] << 16 | m1[P_10] << 24;

                gMDS2[i] = mX[P_20] | mY[P_20] << 8 | m1[P_20] << 16 | mY[P_20] << 24;

                gMDS3[i] = mX[P_30] | m1[P_30] << 8 | mY[P_30] << 16 | mX[P_30] << 24;
            }

            this.k64Cnt = key.Length / 8; // pre-padded ?
            this.SetKey(key);
        }
Esempio n. 10
0
        private readonly uint[] P;                          // the p-array

        /// <summary>
        /// Initializes a new instance of the <see cref="BlowfishCipher"/> class.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="mode">The mode.</param>
        /// <param name="padding">The padding.</param>
        /// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
        /// <exception cref="ArgumentException">Keysize is not valid for this algorithm.</exception>
        public BlowfishCipher(byte[] key, CipherMode mode, CipherPadding padding)
            : base(key, 8, mode, padding)
        {
            var keySize = key.Length * 8;

            if (keySize < 1 || keySize > 448)
            {
                throw new ArgumentException(string.Format("KeySize '{0}' is not valid for this algorithm.", keySize));
            }

            //  TODO:   Refactor this algorithm

            S0 = new uint[SBOX_SK];
            S1 = new uint[SBOX_SK];
            S2 = new uint[SBOX_SK];
            S3 = new uint[SBOX_SK];
            P  = new uint[P_SZ];

            this.SetKey(key);
        }
Esempio n. 11
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TwofishCipher"/> class.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="mode">The mode.</param>
        /// <param name="padding">The padding.</param>
        /// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
        /// <exception cref="ArgumentException">Keysize is not valid for this algorithm.</exception>
        public TwofishCipher(byte[] key, CipherMode mode, CipherPadding padding)
            : base(key, 16, mode, padding)
        {
            var keySize = key.Length * 8;

            if (!(keySize == 128 || keySize == 192 || keySize == 256))
            {
                throw new ArgumentException(string.Format("KeySize '{0}' is not valid for this algorithm.", keySize));
            }

            //  TODO:   Refactor this algorithm

            // calculate the MDS matrix
            var m1 = new int[2];
            var mX = new int[2];
            var mY = new int[2];

            for (var i = 0; i < MaxKeyBits; i++)
            {
                var j = P[0 + i] & 0xff;
                m1[0] = j;
                mX[0] = Mx_X(j) & 0xff;
                mY[0] = Mx_Y(j) & 0xff;

                j     = P[(1 * 256) + i] & 0xff;
                m1[1] = j;
                mX[1] = Mx_X(j) & 0xff;
                mY[1] = Mx_Y(j) & 0xff;

                _gMds0[i] = m1[P00] | mX[P00] << 8 | mY[P00] << 16 | mY[P00] << 24;

                _gMds1[i] = mY[P10] | mY[P10] << 8 | mX[P10] << 16 | m1[P10] << 24;

                _gMds2[i] = mX[P20] | mY[P20] << 8 | m1[P20] << 16 | mY[P20] << 24;

                _gMds3[i] = mX[P30] | m1[P30] << 8 | mY[P30] << 16 | mX[P30] << 24;
            }

            _k64Cnt = key.Length / 8; // pre-padded ?
            SetKey(key);
        }
Esempio n. 12
0
		/// <summary>
		/// Initializes a new instance of the <see cref="TwofishCipher"/> class.
		/// </summary>
		/// <param name="key">The key.</param>
		/// <param name="mode">The mode.</param>
		/// <param name="padding">The padding.</param>
		/// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
		/// <exception cref="ArgumentException">Keysize is not valid for this algorithm.</exception>
		public TwofishCipher(byte[] key, CipherMode mode, CipherPadding padding)
			: base(key, 16, mode, padding)
		{
			var keySize = key.Length * 8;

			if (!(keySize == 128 || keySize == 192 || keySize == 256))
				throw new ArgumentException(string.Format("KeySize '{0}' is not valid for this algorithm.", keySize));

			//  TODO:   Refactor this algorithm

			// calculate the MDS matrix
			int[] m1 = new int[2];
			int[] mX = new int[2];
			int[] mY = new int[2];
			int j;

			for (int i = 0; i < MAX_KEY_BITS; i++)
			{
				j = P[0, i] & 0xff;
				m1[0] = j;
				mX[0] = Mx_X(j) & 0xff;
				mY[0] = Mx_Y(j) & 0xff;

				j = P[1, i] & 0xff;
				m1[1] = j;
				mX[1] = Mx_X(j) & 0xff;
				mY[1] = Mx_Y(j) & 0xff;

				gMDS0[i] = m1[P_00] | mX[P_00] << 8 | mY[P_00] << 16 | mY[P_00] << 24;

				gMDS1[i] = mY[P_10] | mY[P_10] << 8 | mX[P_10] << 16 | m1[P_10] << 24;

				gMDS2[i] = mX[P_20] | mY[P_20] << 8 | m1[P_20] << 16 | mY[P_20] << 24;

				gMDS3[i] = mX[P_30] | m1[P_30] << 8 | mY[P_30] << 16 | mX[P_30] << 24;
			}

			this.k64Cnt = key.Length / 8; // pre-padded ?
			this.SetKey(key);
		}
Esempio n. 13
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TripleDesCipher"/> class.
 /// </summary>
 /// <param name="key">The key.</param>
 /// <param name="mode">The mode.</param>
 /// <param name="padding">The padding.</param>
 /// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
 public TripleDesCipher(byte[] key, CipherMode mode, CipherPadding padding)
     : base(key, mode, padding)
 {
 }
Esempio n. 14
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AesCipher"/> class.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="mode">The mode.</param>
        /// <param name="padding">The padding.</param>
        /// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
        /// <exception cref="ArgumentException">Keysize is not valid for this algorithm.</exception>
        public AesCipher(byte[] key, CipherMode mode, CipherPadding padding)
            : base(key, 16, mode, padding)
        {
            var keySize = key.Length * 8;

            if (!(keySize == 256 || keySize == 192 || keySize == 128))
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "KeySize '{0}' is not valid for this algorithm.", keySize));
        }
Esempio n. 15
0
		private readonly uint[] P;                  // the p-array

		/// <summary>
		/// Initializes a new instance of the <see cref="BlowfishCipher"/> class.
		/// </summary>
		/// <param name="key">The key.</param>
		/// <param name="mode">The mode.</param>
		/// <param name="padding">The padding.</param>
		/// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
		/// <exception cref="ArgumentException">Keysize is not valid for this algorithm.</exception>
		public BlowfishCipher(byte[] key, CipherMode mode, CipherPadding padding)
			: base(key, 8, mode, padding)
		{
			var keySize = key.Length * 8;

			if (keySize < 1 || keySize > 448)
				throw new ArgumentException(string.Format("KeySize '{0}' is not valid for this algorithm.", keySize));

			//  TODO:   Refactor this algorithm

			S0 = new uint[SBOX_SK];
			S1 = new uint[SBOX_SK];
			S2 = new uint[SBOX_SK];
			S3 = new uint[SBOX_SK];
			P = new uint[P_SZ];

			this.SetKey(key);
		}
Esempio n. 16
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BlowfishCipher"/> class.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="mode">The mode.</param>
        /// <param name="padding">The padding.</param>
        /// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
        /// <exception cref="ArgumentException">Keysize is not valid for this algorithm.</exception>
        public BlowfishCipher(byte[] key, CipherMode mode, CipherPadding padding)
            : base(key, 8, mode, padding)
        {
            var keySize = key.Length * 8;

            if (keySize < 1 || keySize > 448)
                throw new ArgumentException(string.Format("KeySize '{0}' is not valid for this algorithm.", keySize));

            _s0 = new uint[SboxSk];
            _s1 = new uint[SboxSk];
            _s2 = new uint[SboxSk];
            _s3 = new uint[SboxSk];
            _p = new uint[PSize];

            SetKey(key);
        }
Esempio n. 17
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TripleDesCipher"/> class.
 /// </summary>
 /// <param name="key">The key.</param>
 /// <param name="mode">The mode.</param>
 /// <param name="padding">The padding.</param>
 /// <exception cref="ArgumentNullException"><paramref name="key"/> is <c>null</c>.</exception>
 public TripleDesCipher(byte[] key, CipherMode mode, CipherPadding padding)
     : base(key, mode, padding)
 {
 }
Esempio n. 18
0
 public BlockCipherStub(byte[] key, byte blockSize, CipherMode mode, CipherPadding padding) : base(key, blockSize, mode, padding)
 {
 }