Base class for cipher mode implementations
Ejemplo 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);
        }
Ejemplo 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);
		}
Ejemplo 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);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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);
		}
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
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)
 {
 }
Ejemplo n.º 8
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));
        }
Ejemplo n.º 9
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);
		}
Ejemplo n.º 10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DesCipher"/> 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 DesCipher(byte[] key, CipherMode mode, CipherPadding padding)
     : base(key, 8, mode, padding)
 {
 }
Ejemplo n.º 11
0
 public BlockCipherStub(byte[] key, byte blockSize, CipherMode mode, CipherPadding padding) : base(key, blockSize, mode, padding)
 {
 }