/// <summary> /// Serialize a KeyParams class /// </summary> /// /// <param name="KeyObj">A KeyParams class</param> /// /// <returns>A stream containing the KeyParams data</returns> public static Stream Serialize(KeyParams KeyObj) { MemoryStream stream = new MemoryStream(); BinaryWriter writer = new BinaryWriter(stream); writer.Write(KeyObj.Key != null ? (short)KeyObj.Key.Length : (short)0); writer.Write(KeyObj.IV != null ? (short)KeyObj.IV.Length : (short)0); writer.Write(KeyObj.IKM != null ? (short)KeyObj.IKM.Length : (short)0); if (KeyObj.Key != null) { writer.Write(KeyObj.Key); } if (KeyObj.IV != null) { writer.Write(KeyObj.IV); } if (KeyObj.IKM != null) { writer.Write(KeyObj.IKM); } return(stream); }
/// <summary> /// Initialize the Cipher /// </summary> /// /// <param name="Encryption">Cipher is used. for encryption, false to decrypt</param> /// <param name="KeyParam">KeyParams containing key and vector</param> /// /// <exception cref="CryptoSymmetricException">Thrown if a null Key or IV is used</exception> public void Initialize(bool Encryption, KeyParams KeyParam) { if (KeyParam.Key == null) throw new CryptoSymmetricException("CFB:Initialize", "Key can not be null!", new ArgumentNullException()); if (KeyParam.IV == null) throw new CryptoSymmetricException("CFB:Initialize", "IV can not be null!", new ArgumentNullException()); byte[] iv = KeyParam.IV; int diff = _cfbIv.Length - iv.Length; Buffer.BlockCopy(iv, 0, _cfbIv, diff, iv.Length); Array.Clear(_cfbIv, 0, diff); _blockCipher.Initialize(true, KeyParam); _isEncryption = Encryption; _isInitialized = true; }
/// <summary> /// Initialize the Cipher. /// </summary> /// /// <param name="Encryption">Using Encryption or Decryption mode</param> /// <param name="KeyParam">Cipher key container.<para>The <see cref="LegalKeySizes"/> property contains valid sizes.</para></param> /// /// <exception cref="CryptoSymmetricException">Thrown if a null or invalid key is used</exception> public void Initialize(bool Encryption, KeyParams KeyParam) { if (KeyParam.Key == null) throw new CryptoSymmetricException("SHX:Initialize", "Invalid key! Key can not be null.", new ArgumentNullException()); if (KeyParam.Key.Length < LegalKeySizes[0]) throw new CryptoSymmetricException("SHX:Initialize", String.Format("Invalid key size! Key must be at least {0} bytes ({1} bit).", LegalKeySizes[0], LegalKeySizes[0] * 8), new ArgumentOutOfRangeException()); if ((KeyParam.Key.Length - _keyEngine.DigestSize) % _keyEngine.BlockSize != 0) throw new CryptoSymmetricException("SHX:Initialize", String.Format("Invalid key size! Key must be (length - IKm length: {0} bytes) + multiple of {1} block size.", _keyEngine.DigestSize, _keyEngine.BlockSize), new ArgumentOutOfRangeException()); _isEncryption = Encryption; // expand the key _expKey = ExpandKey(KeyParam.Key); // ready to transform data _isInitialized = true; }
/// <summary> /// Initialize the MAC /// </summary> /// /// <param name="KeyParam">A <see cref="KeyParams"/> containing Key and IV. /// <para>Uses the Key and IV fields of the KeyParams parameter. /// Key size must be one of the <c>LegalKeySizes</c> of the underlying cipher. /// IV size must be the ciphers blocksize. /// </para> /// </param> /// /// <exception cref="CryptoMacException">Thrown if an invalid Input size is chosen</exception> public void Initialize(KeyParams KeyParam) { if (KeyParam.Key == null) throw new CryptoMacException("CMAC:Initialize", "Key can not be null!", new ArgumentNullException()); byte[] tmpIv = new byte[_blockSize]; // convert for cipher KeyParams key = new KeyParams(KeyParam.Key, tmpIv); _cipherType.Initialize(true, key); _L = new byte[_tmpZeroes.Length]; _cipherType.Transform(_tmpZeroes, 0, _L, 0); _LU = DoubleLu(_L); _LU2 = DoubleLu(_LU); _cipherType.Initialize(true, key); _isInitialized = true; }
/// <summary> /// Initialize the Cipher /// </summary> /// /// <param name="Encryption">Cipher is used. for encryption, false to decrypt</param> /// <param name="KeyParam">The KeyParams containing key and vector</param> /// /// <exception cref="CryptoSymmetricException">Thrown if a null Key or IV is used</exception> public void Initialize(bool Encryption, KeyParams KeyParam) { if (KeyParam.Key == null) throw new CryptoSymmetricException("CTR:Initialize", "Key can not be null!", new ArgumentNullException()); if (KeyParam.IV == null) throw new CryptoSymmetricException("CTR:Initialize", "IV can not be null!", new ArgumentNullException()); _blockCipher.Initialize(true, KeyParam); _ctrVector = KeyParam.IV; _isEncryption = Encryption; _isInitialized = true; }
/// <summary> /// Initialize the MAC /// </summary> /// /// <param name="KeyParam">VMPCMAC Key and IV. /// <para>Uses the Key and IV fields of the <see cref="KeyParams"/> class. /// Key and IV must be between 1 and 768 bytes in length. /// Key and IV should be equal in size.</para> /// </param> /// /// <exception cref="CryptoMacException">Thrown if a null or invalid Key, or IV is used</exception> public void Initialize(KeyParams KeyParam) { if (KeyParam.Key == null) throw new CryptoMacException("VMPCMAC:Initialize", "VMPCMAC Initialize KeyParams must include a Key!", new ArgumentNullException()); if (KeyParam.IV == null) throw new CryptoMacException("VMPCMAC:Initialize", "VMPCMAC Initialize KeyParams must include an IV!", new ArgumentNullException()); _workingIV = KeyParam.IV; if (_workingIV == null || _workingIV.Length < 1 || _workingIV.Length > 768) throw new CryptoMacException("VMPCMAC:Initialize", "VMPCMAC requires 1 to 768 bytes of IV!", new ArgumentOutOfRangeException()); _workingKey = KeyParam.Key; Reset(); _isInitialized = true; }
/// <summary> /// Initialize the generator /// </summary> /// /// <param name="Salt">Salt value</param> /// /// <exception cref="CryptoGeneratorException">Thrown if a null Salt is used</exception> public void Initialize(byte[] Salt) { if (Salt == null) throw new CryptoGeneratorException("PKCS5:Initialize", "Salt can not be null!", new ArgumentNullException()); byte[] keyBytes = new byte[_digestMac.DigestSize]; Buffer.BlockCopy(Salt, 0, _Salt, 0, Salt.Length - _digestMac.DigestSize); Buffer.BlockCopy(Salt, _Salt.Length, keyBytes, 0, _digestMac.DigestSize); _macKey = new KeyParams(keyBytes); _isInitialized = true; }
/// <summary> /// Initialize the HMAC /// </summary> /// /// <param name="KeyParam">KeyParams containing HMAC Key. /// <para>Uses the Key field of the <see cref="KeyParams"/> class. /// Key should be equal in size to the <see cref="DigestSize"/></para> /// </param> public void Initialize(KeyParams KeyParam) { _shaHmac.Initialize(KeyParam); _isInitialized = true; }
/// <summary> /// Serialize a KeyParams class /// </summary> /// /// <param name="KeyObj">A KeyParams class</param> /// /// <returns>A stream containing the KeyParams data</returns> public static Stream Serialize(KeyParams KeyObj) { MemoryStream stream = new MemoryStream(); BinaryWriter writer = new BinaryWriter(stream); writer.Write(KeyObj.Key != null ? (short)KeyObj.Key.Length : (short)0); writer.Write(KeyObj.IV != null ? (short)KeyObj.IV.Length : (short)0); writer.Write(KeyObj.IKM != null ? (short)KeyObj.IKM.Length : (short)0); if (KeyObj.Key != null) writer.Write(KeyObj.Key); if (KeyObj.IV != null) writer.Write(KeyObj.IV); if (KeyObj.IKM != null) writer.Write(KeyObj.IKM); return stream; }
/// <summary> /// Initialize the Cipher /// </summary> /// /// <param name="Encryption">Cipher is used. for encryption, false to decrypt</param> /// <param name="KeyParam">KeyParam containing key and vector</param> /// /// <exception cref="CryptoSymmetricException">Thrown if a null Key or IV is used</exception> public void Initialize(bool Encryption, KeyParams KeyParam) { if (KeyParam.Key == null) throw new CryptoSymmetricException("CBC:Initialize", "Key can not be null!", new ArgumentNullException()); if (KeyParam.IV == null) throw new CryptoSymmetricException("CBC:Initialize", "IV can not be null!", new ArgumentNullException()); _blockCipher.Initialize(Encryption, KeyParam); _cbcIv = KeyParam.IV; _cbcNextIv = new byte[_cbcIv.Length]; _isEncryption = Encryption; _isInitialized = true; }
private void Dispose(bool Disposing) { if (!_isDisposed && Disposing) { try { if (_digestMac != null && _disposeEngine) { _digestMac.Dispose(); _digestMac = null; } if (_macKey != null) { _macKey.Dispose(); _macKey = null; } if (_Salt != null) { Array.Clear(_Salt, 0, _Salt.Length); _Salt = null; } } finally { _isDisposed = true; } } }
/// <summary> /// Initialize the generator /// </summary> /// /// <param name="Salt">Salt value</param> /// <param name="Ikm">Key material</param> /// <param name="Nonce">Nonce value</param> /// /// <exception cref="CryptoGeneratorException">Thrown if a null Salt or Ikm is used</exception> public void Initialize(byte[] Salt, byte[] Ikm, byte[] Nonce) { if (Salt == null) throw new CryptoGeneratorException("PKCS5:Initialize", "Salt can not be null!", new ArgumentNullException()); if (Ikm == null) throw new CryptoGeneratorException("PKCS5:Initialize", "IKM can not be null!", new ArgumentNullException()); _macKey = new KeyParams(Ikm); _Salt = new byte[Salt.Length + Nonce.Length]; Buffer.BlockCopy(Salt, 0, _Salt, 0, Salt.Length); Buffer.BlockCopy(Nonce, 0, _Salt, Salt.Length, Nonce.Length); _isInitialized = true; }
/// <summary> /// Initialize the generator /// </summary> /// /// <param name="Salt">Salt value</param> /// <param name="Ikm">Key material</param> /// /// <exception cref="CryptoGeneratorException">Thrown if a null Salt or Ikm is used</exception> public void Initialize(byte[] Salt, byte[] Ikm) { if (Salt == null) throw new CryptoGeneratorException("PKCS5:Initialize", "Salt can not be null!", new ArgumentNullException()); if (Ikm == null) throw new CryptoGeneratorException("PKCS5:Initialize", "IKM can not be null!", new ArgumentNullException()); _Salt = (byte[])Salt.Clone(); _macKey = new KeyParams(Ikm); _isInitialized = true; }
/// <summary> /// Initialize the Cipher. /// </summary> /// /// <param name="Encryption">Using Encryption or Decryption mode</param> /// <param name="KeyParam">Cipher key container. <para>The <see cref="LegalKeySizes"/> property contains valid sizes.</para></param> /// /// <exception cref="CryptoSymmetricException">Thrown if a null or invalid key is used</exception> public void Initialize(bool Encryption, KeyParams KeyParam) { if (KeyParam.Key == null) throw new CryptoSymmetricException("SPX:Initialize", "Invalid key! Key can not be null.", new ArgumentNullException()); if (KeyParam.Key.Length != 16 && KeyParam.Key.Length != 24 && KeyParam.Key.Length != 32 && KeyParam.Key.Length != 64) throw new CryptoSymmetricException("SPX:Initialize", "Invalid key size! Valid sizes are 16, 24, 32 and 64 bytes.", new ArgumentOutOfRangeException()); _isEncryption = Encryption; _expKey = ExpandKey(KeyParam.Key); }
/// <summary> /// Initialize the class and working variables. /// <para>When this constructor is used, <see cref="Initialize(KeyParams)"/> is called automatically.</para> /// </summary> /// /// <param name="Digest">Message Digest instance</param> /// <param name="Key">HMAC Key; passed to HMAC Initialize() through constructor</param> /// <param name="DisposeEngine">Dispose of digest engine when <see cref="Dispose()"/> on this class is called</param> /// /// <exception cref="CryptoMacException">Thrown if a null digest is used</exception> public HMAC(IDigest Digest, byte[] Key, bool DisposeEngine = true) { if (Digest == null) throw new CryptoMacException("HMAC:Ctor", "Digest can not be null!", new ArgumentNullException()); _disposeEngine = DisposeEngine; _msgDigest = Digest; _digestSize = Digest.DigestSize; _blockSize = Digest.BlockSize; _inputPad = new byte[_blockSize]; _outputPad = new byte[_blockSize]; KeyParams keyParam = new KeyParams(Key); Initialize(keyParam); keyParam.Dispose(); }
/// <summary> /// Initialize the Cipher /// </summary> /// /// <param name="Encryption">Cipher is used. for encryption, false to decrypt</param> /// <param name="KeyParam">The KeyParams containing key and vector</param> /// /// <exception cref="CryptoSymmetricException">Thrown if a null Key or IV is used</exception> public void Initialize(bool Encryption, KeyParams KeyParam) { if (KeyParam.Key == null) throw new CryptoSymmetricException("OFB:Initialize", "Key can not be null!", new ArgumentNullException()); if (KeyParam.IV == null) throw new CryptoSymmetricException("OFB:Initialize", "IV can not be null!", new ArgumentNullException()); _blockCipher.Initialize(true, KeyParam); byte[] iv = KeyParam.IV; if (iv.Length < _ofbIv.Length) { // prepend the supplied IV with zeros per FIPS PUB 81 Array.Copy(iv, 0, _ofbIv, _ofbIv.Length - iv.Length, iv.Length); for (int i = 0; i < _ofbIv.Length - iv.Length; i++) _ofbIv[i] = 0; } else { Array.Copy(iv, 0, _ofbIv, 0, _ofbIv.Length); } _isEncryption = Encryption; _isInitialized = true; }
/// <summary> /// Initialize the HMAC /// </summary> /// /// <param name="KeyParam">HMAC Key. /// <para>Uses the Key field of the <see cref="KeyParams"/> class, <c>Key</c> parameter. /// Key should be equal in size to the <see cref="DigestSize"/></para> /// </param> /// /// <exception cref="CryptoMacException">Thrown if the Key is null or less than digest size</exception> public void Initialize(KeyParams KeyParam) { if (KeyParam.Key == null) throw new CryptoMacException("HMAC:Initialize", "Key can not be null!", new ArgumentNullException()); _msgDigest.Reset(); int keyLength = KeyParam.Key.Length; // compress to digest size if (KeyParam.Key.Length > _blockSize) { _msgDigest.BlockUpdate(KeyParam.Key, 0, KeyParam.Key.Length); _msgDigest.DoFinal(_inputPad, 0); keyLength = _digestSize; } else { Array.Copy(KeyParam.Key, 0, _inputPad, 0, keyLength); } Array.Clear(_inputPad, keyLength, _blockSize - keyLength); Array.Copy(_inputPad, 0, _outputPad, 0, _blockSize); XOR(_inputPad, IPAD); XOR(_outputPad, OPAD); // initialise the digest _msgDigest.BlockUpdate(_inputPad, 0, _inputPad.Length); _isInitialized = true; }
/// <summary> /// Initialize the Cipher /// </summary> /// /// <param name="KeyParam">Cipher key container. /// <para>Uses the Key and IV fields of KeyParam. /// The <see cref="LegalKeySizes"/> property contains valid Key sizes. /// IV must be 8 bytes in size.</para> /// </param> /// /// <exception cref="System.ArgumentNullException">Thrown if a null key or iv is used</exception> /// <exception cref="System.ArgumentOutOfRangeException">Thrown if an invalid key or iv size is used</exception> public void Initialize(KeyParams KeyParam) { if (KeyParam.IV == null) throw new CryptoSymmetricException("Salsa20:Initialize", "Init parameters must include an IV!", new ArgumentException()); if (KeyParam.IV.Length != 8) throw new CryptoSymmetricException("Salsa20:Initialize", "Requires exactly 8 bytes of IV!", new ArgumentOutOfRangeException()); Reset(); if (KeyParam.Key == null) { if (!_isInitialized) throw new CryptoSymmetricException("ChaCha:Initialize", "Key can not be null for first initialisation!", new ArgumentException()); SetKey(null, KeyParam.IV); } else { if (KeyParam.Key.Length != 16 && KeyParam.Key.Length != 32) throw new CryptoSymmetricException("ChaCha:Initialize", "Key must be 16 or 32 bytes!", new ArgumentOutOfRangeException()); SetKey(KeyParam.Key, KeyParam.IV); } _isInitialized = true; }
/// <summary> /// Initialize the Cipher /// </summary> /// /// <param name="KeyParam">Cipher key container. /// <para>Uses the Key and IV fields of KeyParam. /// The <see cref="LegalKeySizes"/> property contains valid Key sizes. /// IV must be 8 bytes in size.</para> /// </param> /// /// <exception cref="System.ArgumentNullException">Thrown if a null key or iv is used</exception> /// <exception cref="System.ArgumentOutOfRangeException">Thrown if an invalid key or iv size is used</exception> public void Initialize(KeyParams KeyParam) { if (KeyParam.IV == null) throw new CryptoSymmetricException("Salsa20:Initialize", "Init parameters must include an IV!", new ArgumentException()); if (KeyParam.IV.Length != 8) throw new CryptoSymmetricException("Salsa20:Initialize", "Requires exactly 8 bytes of IV!", new ArgumentOutOfRangeException()); if (KeyParam.Key == null) throw new CryptoSymmetricException("Salsa20:Initialize", "Key can not be null!", new ArgumentException()); if (KeyParam.Key.Length != 16 && KeyParam.Key.Length != 32) throw new CryptoSymmetricException("Salsa20:Initialize", "Key must be 16 or 32 bytes!", new ArgumentOutOfRangeException()); if (DistributionCode == null) { if (KeyParam.Key.Length == 16) _dstCode = (byte[])TAU.Clone(); else _dstCode = (byte[])SIGMA.Clone(); } Reset(); SetKey(KeyParam.Key, KeyParam.IV); _isInitialized = true; }