/// <summary> /// Get a block cipher instance with specified initialization parameters /// </summary> /// /// <param name="BlockCipherType">The block cipher enumeration name</param> /// <param name="BlockSize">The cipher block size</param> /// <param name="RoundCount">The number of cipher rounds</param> /// <param name="ExtractorType">The ciphers key expansion engine</param> /// /// <returns>An initialized block cipher</returns> public static IBlockCipher GetInstance(BlockCiphers BlockCipherType, int BlockSize, int RoundCount, Digests ExtractorType = Digests.None) { switch (BlockCipherType) { case BlockCiphers.Rijndael: return(new RHX(BlockSize)); case BlockCiphers.RHX: return(new RHX(BlockSize, RoundCount, ExtractorType)); case BlockCiphers.Serpent: return(new SHX()); case BlockCiphers.SHX: return(new SHX(RoundCount, ExtractorType)); case BlockCiphers.Twofish: return(new THX()); case BlockCiphers.THX: return(new THX(RoundCount, ExtractorType)); case BlockCiphers.RSM: return(new RSM(RoundCount, BlockSize)); case BlockCiphers.TSM: return(new TSM(RoundCount)); default: throw new CryptoProcessingException("BlockCipherFromName:GetInstance", "The cipher engine is not supported!"); } }
/// <summary> /// Get a block cipher instance with default initialization parameters. /// <para>The default parameters for each HX extended cipher, 128 bit block-size, Kdf as SHA2-256, and rounds equal to a 512 key setting</para> /// </summary> /// /// <param name="BlockCipherType">The block cipher enumeration name</param> /// /// <returns>An initialized block cipher</returns> /// /// <exception cref="CryptoProcessingException">Thrown if the enumeration name is not supported</exception> public static IBlockCipher GetInstance(BlockCiphers BlockCipherType) { switch (BlockCipherType) { case BlockCiphers.Rijndael: return(new RHX()); case BlockCiphers.RHX: return(new RHX(16, 22, Digests.SHA256)); case BlockCiphers.Serpent: return(new SHX()); case BlockCiphers.SHX: return(new SHX(40, Digests.SHA256)); case BlockCiphers.Twofish: return(new THX()); case BlockCiphers.THX: return(new THX(20, Digests.SHA256)); case BlockCiphers.RSM: return(new RSM(42, 32)); case BlockCiphers.TSM: return(new TSM(32)); default: throw new CryptoProcessingException("BlockCipherFromName:GetInstance", "The cipher engine is not supported!"); } }
private IBlockCipher GetCipher(BlockCiphers RngEngine) { switch (RngEngine) { case BlockCiphers.RDX: return(new RDX()); case BlockCiphers.RHX: return(new RHX()); case BlockCiphers.RSM: return(new RSM()); case BlockCiphers.SHX: return(new SHX()); case BlockCiphers.SPX: return(new SPX()); case BlockCiphers.TFX: return(new TFX()); case BlockCiphers.THX: return(new THX()); case BlockCiphers.TSM: return(new TSM()); default: return(new RDX()); } }
/// <summary> /// DtmSessionStruct constructor /// </summary> /// /// <param name="EngineType">The Cryptographic <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.BlockCiphers">Engine</see> type</param> /// <param name="KeySize">The cipher Key Size in bytes</param> /// <param name="IvSize">Size of the cipher <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.IVSizes">Initialization Vector</see></param> /// <param name="RoundCount">The number of diffusion <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.RoundCounts">Rounds</see></param> /// <param name="KdfEngine">The <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.Digests">Digest</see> engine used to power the key schedule Key Derivation Function in HX and M series ciphers</param> /// /// <exception cref="System.ArgumentOutOfRangeException">Thrown if an invalid KeyId, MessageKey, or ExtensionKey is used</exception> public DtmSessionStruct(BlockCiphers EngineType = BlockCiphers.Rijndael, int KeySize = 32, IVSizes IvSize = IVSizes.V128, RoundCounts RoundCount = RoundCounts.R14, Digests KdfEngine = Digests.None) { this.EngineType = (byte)EngineType; this.KeySize = (short)KeySize; this.IvSize = (byte)IvSize; this.RoundCount = (byte)RoundCount; this.KdfEngine = (byte)KdfEngine; }
private IBlockCipher GetCipher(BlockCiphers RngEngine) { try { return(BlockCipherFromName.GetInstance(RngEngine)); } catch (Exception Ex) { throw new CryptoRandomException("CTRPrng:GetCipher", "The cipher could not be initialized!", Ex); } }
private IBlockCipher GetBlockCipher(BlockCiphers EngineType, int BlockSize, int RoundCount, Digests KdfEngine) { try { return(BlockCipherFromName.GetInstance(EngineType, BlockSize, RoundCount, KdfEngine)); } catch (Exception Ex) { throw new CryptoRandomException("CTRPrng:GetCipher", "The cipher could not be initialized!", Ex); } }
/// <summary> /// Initialize the structure with parameters for an CMAC generator /// </summary> /// /// <param name="KeySize">The Mac key size in bytes</param> /// <param name="EngineType">The symmetric block cipher <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.SymmetricEngines">Engine</see> type</param> /// <param name="IvSize">Size of the cipher <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.IVSizes">Initialization Vector</see></param> /// <param name="BlockSize">The cipher <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.BlockSizes">Block Size</see></param> /// <param name="RoundCount">The number of diffusion <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.RoundCounts">Rounds</see></param> /// <param name="KdfEngine">The <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.Digests">Digest</see> engine used to power the key schedule Key Derivation Function in HX and M series ciphers</param> public MacDescription(int KeySize, BlockCiphers EngineType, IVSizes IvSize, BlockSizes BlockSize = BlockSizes.B128, RoundCounts RoundCount = RoundCounts.R14, Digests KdfEngine = Digests.SHA512) { MacType = (int)Macs.CMAC; this.KeySize = KeySize; this.IvSize = (int)IvSize; HmacEngine = 0; this.EngineType = (int)EngineType; this.BlockSize = (int)BlockSize; this.RoundCount = (int)RoundCount; this.KdfEngine = (int)KdfEngine; }
IBlockCipher LoadCipher(BlockCiphers CipherType) { try { return(Helper.BlockCipherFromName.GetInstance(CipherType)); } catch (Exception ex) { throw new CryptoSymmetricException("CTR:LoadCipher", "The block cipher could not be instantiated!", ex); } }
/// <summary> /// Initialize the cipher mode using a block cipher type name /// </summary> /// /// <param name="CipherType">The formal enumeration name of a block cipher</param> /// /// <exception cref="System.ArgumentNullException">Thrown if an invalid Cipher type is used</exception> public CTR(BlockCiphers CipherType) { if (CipherType == BlockCiphers.None) { throw new CryptoSymmetricException("CTR:CTor", "The Cipher type can not be none!", new ArgumentNullException()); } m_disposeEngine = true; m_blockCipher = LoadCipher(CipherType); m_blockSize = m_blockCipher.BlockSize; Scope(); }
private ICipherMode GetCipherMode(CipherModes CipherType, BlockCiphers EngineType, int BlockSize, int RoundCount, Digests KdfEngine) { IBlockCipher engine = GetBlockCipher(EngineType, BlockSize, RoundCount, KdfEngine); try { return(CipherModeFromName.GetInstance(CipherType, engine)); } catch (Exception ex) { throw new CryptoProcessingException("CipherStream:GetCipherMode", ex); } }
private IBlockCipher GetCipher(BlockCiphers EngineType, Digests DigestType, RoundCounts Rounds, int BlockSize = 16) { switch (EngineType) { case BlockCiphers.Rijndael: return(new RHX(BlockSize, (int)Rounds, DigestType)); case BlockCiphers.Serpent: return(new SHX((int)Rounds, DigestType)); case BlockCiphers.Twofish: return(new THX((int)Rounds, DigestType)); } throw new Exception(); }
private int GetKeySize(BlockCiphers CipherEngine) { switch (CipherEngine) { case BlockCiphers.RHX: case BlockCiphers.RSM: case BlockCiphers.SHX: case BlockCiphers.THX: case BlockCiphers.TSM: return(320); default: return(32); } }
/// <summary> /// Initialize the class /// </summary> /// /// <param name="BlockEngine">The block cipher that powers the rng (default is RDX)</param> /// <param name="SeedEngine">The Seed engine used to create keyng material (default is CSPRsg)</param> /// <param name="BufferSize">The size of the cache of random bytes (must be more than 1024 to enable parallel processing)</param> /// <param name="KeySize">The key size (in bytes) of the symmetric cipher; a <c>0</c> value will auto size the key</param> public CTRPrng(BlockCiphers BlockEngine = BlockCiphers.RDX, SeedGenerators SeedEngine = SeedGenerators.CSPRsg, int BufferSize = 4096, int KeySize = 0) { if (BufferSize < 64) throw new CryptoRandomException("CTRPrng:Ctor", "Buffer size must be at least 64 bytes!", new ArgumentNullException()); _engineType = BlockEngine; _seedType = SeedEngine; _byteBuffer = new byte[BufferSize]; _bufferSize = BufferSize; if (KeySize > 0) _keySize = KeySize; else _keySize = GetKeySize(BlockEngine); Reset(); }
/// <summary> /// Initialize the class with the block cipher enumeration name /// </summary> /// <param name="EngineType">The block cipher enumeration name</param> /// /// <exception cref="CryptoMacException">Thrown if an invalid block size is used</exception> public CMAC(BlockCiphers EngineType) { IBlockCipher cipher = Helper.BlockCipherFromName.GetInstance(EngineType); if (cipher.BlockSize != 16 && cipher.BlockSize != 32) { throw new CryptoMacException("CMAC:Ctor", "Block size must be 128 or 256 bits!", new ArgumentException()); } m_disposeEngine = true; m_cipherMode = new CBC(cipher); m_blockSize = m_cipherMode.BlockSize; m_macSize = cipher.BlockSize; m_msgCode = new byte[m_blockSize]; m_wrkBuffer = new byte[m_blockSize]; m_wrkOffset = 0; }
/// <summary> /// Creates a CTR Bytes Generator using a block cipher type name /// </summary> /// /// <param name="CipherType">The formal enumeration name of a block cipher</param> /// /// <exception cref="CryptoSymmetricException">Thrown if an invalid Cipher type is used</exception> public CMG(BlockCiphers CipherType) { if (CipherType == BlockCiphers.None) { throw new CryptoSymmetricException("CMG:CTor", "The Cipher type can not be none!", new ArgumentNullException()); } m_disposeEngine = true; m_blockCipher = LoadCipher(CipherType); m_blockSize = m_blockCipher.BlockSize; m_legalKeySizes = new int[m_blockCipher.LegalKeySizes.Length]; for (int i = 0; i < m_blockCipher.LegalKeySizes.Length; ++i) { m_legalKeySizes[i] = m_blockCipher.LegalKeySizes[i] + m_blockCipher.BlockSize; } Scope(); }
/// <summary> /// Initialize the class with a Seed; note: the same seed will produce the same random output /// </summary> /// /// <param name="Seed">The Seed bytes used to initialize the digest counter; (min. length is key size + counter 16)</param> /// <param name="BlockEngine">The block cipher that powers the rng (default is RDX)</param> /// <param name="BufferSize">The size of the cache of random bytes (must be more than 1024 to enable parallel processing)</param> /// /// <exception cref="CryptoRandomException">Thrown if the seed is null or too small</exception> public CTRPrng(byte[] Seed, BlockCiphers BlockEngine = BlockCiphers.Rijndael, int BufferSize = 4096) { if (BufferSize < 64) { throw new CryptoRandomException("CTRPrng:Ctor", "Buffer size must be at least 64 bytes!", new ArgumentNullException()); } if (Seed == null) { throw new CryptoRandomException("CTRPrng:Ctor", "Seed can not be null!", new ArgumentNullException()); } if (GetKeySize(BlockEngine) < Seed.Length) { throw new CryptoRandomException("CTRPrng:Ctor", String.Format("The state seed is too small! must be at least {0} bytes", GetKeySize(BlockEngine)), new ArgumentException()); } m_engineType = BlockEngine; m_stateSeed = Seed; m_byteBuffer = new byte[BufferSize]; m_bufferSize = BufferSize; Reset(); }
/// <summary> /// Outputs expected values for 512 bit keys /// </summary> /// /// <returns>State</returns> public string Get512Vector(BlockCiphers EngineType, RoundCounts Rounds, int BlockSize = 16) { IBlockCipher engine = GetCipher(EngineType, Digests.None, Rounds, BlockSize); // rounds calc automatic int keyLen = 64; byte[] key = new byte[keyLen]; byte[] iv = new byte[engine.BlockSize]; ICipherMode cipher = new CTR(engine); for (int i = 0; i < keyLen; i++) { key[i] = (byte)i; } for (int i = 0; i < iv.Length; i++) { iv[i] = (byte)i; } cipher.Initialize(true, new KeyParams(key, iv)); return(MonteCarloTest(cipher)); }
/// <summary> /// Outputs expected values for the HX Ciphers /// </summary> /// /// <returns>State</returns> public string GetHXVector(BlockCiphers EngineType, Digests DigestType, RoundCounts Rounds) { IBlockCipher engine = GetCipher(EngineType, DigestType, Rounds); int keyLen = GetKeySize(engine); byte[] key = new byte[keyLen]; byte[] iv = new byte[engine.BlockSize]; ICipherMode cipher = new CTR(engine); for (int i = 0; i < keyLen; i++) { key[i] = (byte)i; } for (int i = 0; i < iv.Length; i++) { iv[i] = (byte)i; } cipher.Initialize(true, new KeyParams(key, iv)); return(MonteCarloTest(cipher)); }
/// <summary> /// Initialize the class /// </summary> /// /// <param name="BlockEngine">The block cipher that powers the rng (default is RDX)</param> /// <param name="SeedEngine">The Seed engine used to create keyng material (default is CSPRsg)</param> /// <param name="BufferSize">The size of the cache of random bytes (must be more than 1024 to enable parallel processing)</param> /// <param name="KeySize">The key size (in bytes) of the symmetric cipher; a <c>0</c> value will auto size the key</param> public CTRPrng(BlockCiphers BlockEngine = BlockCiphers.RDX, SeedGenerators SeedEngine = SeedGenerators.CSPRsg, int BufferSize = 4096, int KeySize = 0) { if (BufferSize < 64) { throw new CryptoRandomException("CTRPrng:Ctor", "Buffer size must be at least 64 bytes!", new ArgumentNullException()); } _engineType = BlockEngine; _seedType = SeedEngine; _byteBuffer = new byte[BufferSize]; _bufferSize = BufferSize; if (KeySize > 0) { _keySize = KeySize; } else { _keySize = GetKeySize(BlockEngine); } Reset(); }
/// <summary> /// Initialize the cipher mode using a block cipher type name /// </summary> /// /// <param name="CipherType">The formal enumeration name of a block cipher</param> /// <param name="RegisterSize">Register size in bytes; minimum is 1 byte, maximum is the Block Ciphers internal block size</param> /// /// <exception cref="CryptoSymmetricException">Thrown if an invalid Cipher type or register size is used</exception> public OFB(BlockCiphers CipherType, int RegisterSize = 16) { if (CipherType == BlockCiphers.None) { throw new CryptoSymmetricException("OFB:CTor", "The Cipher type can not be none!", new ArgumentNullException()); } if (RegisterSize == 0) { throw new CryptoSymmetricException("OFB:CTor", "The RegisterSize can not be zero!"); } m_blockCipher = LoadCipher(CipherType); if (RegisterSize > m_blockCipher.BlockSize) { throw new CryptoSymmetricException("OFB:CTor", "The RegisterSize can not be more than the ciphers block size!"); } m_disposeEngine = true; m_blockSize = RegisterSize; m_ofbIv = new byte[m_blockCipher.BlockSize]; m_ofbBuffer = new byte[m_blockCipher.BlockSize]; }
/// <summary> /// SessionParams constructor /// </summary> /// /// <param name="EngineType">The Cryptographic <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.BlockCiphers">Engine</see> type</param> /// <param name="KeySize">The cipher Key Size in bytes</param> /// <param name="IvSize">Size of the cipher <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.IVSizes">Initialization Vector</see></param> /// <param name="RoundCount">The number of diffusion <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.RoundCounts">Rounds</see></param> /// <param name="KdfEngine">The <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.Digests">Digest</see> engine used to power the key schedule Key Derivation Function in HX and M series ciphers</param> /// /// <exception cref="System.ArgumentOutOfRangeException">Thrown if an invalid KeyId, MessageKey, or ExtensionKey is used</exception> public DtmSession(BlockCiphers EngineType = BlockCiphers.RDX, int KeySize = 32, IVSizes IvSize = IVSizes.V128, RoundCounts RoundCount = RoundCounts.R14, Digests KdfEngine = Digests.SHA512) { this.EngineType = (byte)EngineType; this.KeySize = (short)KeySize; this.IvSize = (byte)IvSize; this.RoundCount = (byte)RoundCount; this.KdfEngine = (byte)KdfEngine; }
/// <summary> /// Initialize the class with a Seed; note: the same seed will produce the same random output /// </summary> /// /// <param name="Seed">The Seed bytes used to initialize the digest counter; (min. length is key size + counter 16)</param> /// <param name="BlockEngine">The block cipher that powers the rng (default is RDX)</param> /// <param name="BufferSize">The size of the cache of random bytes (must be more than 1024 to enable parallel processing)</param> /// /// <exception cref="CryptoRandomException">Thrown if the seed is null or too small</exception> public CTRPrng(byte[] Seed, BlockCiphers BlockEngine = BlockCiphers.RDX, int BufferSize = 4096) { if (BufferSize < 64) throw new CryptoRandomException("CTRPrng:Ctor", "Buffer size must be at least 64 bytes!", new ArgumentNullException()); if (Seed == null) throw new CryptoRandomException("CTRPrng:Ctor", "Seed can not be null!", new ArgumentNullException()); if (GetKeySize(BlockEngine) < Seed.Length) throw new CryptoRandomException("CTRPrng:Ctor", String.Format("The state seed is too small! must be at least {0} bytes", GetKeySize(BlockEngine)), new ArgumentException()); _engineType = BlockEngine; _stateSeed = Seed; _byteBuffer = new byte[BufferSize]; _bufferSize = BufferSize; Reset(); }
private IBlockCipher GetCipher(BlockCiphers RngEngine) { switch (RngEngine) { case BlockCiphers.RDX: return new RDX(); case BlockCiphers.RHX: return new RHX(); case BlockCiphers.RSM: return new RSM(); case BlockCiphers.SHX: return new SHX(); case BlockCiphers.SPX: return new SPX(); case BlockCiphers.TFX: return new TFX(); case BlockCiphers.THX: return new THX(); case BlockCiphers.TSM: return new TSM(); default: return new RDX(); } }
private int GetKeySize(BlockCiphers CipherEngine) { return(32); }
private int GetKeySize(BlockCiphers CipherEngine) { switch (CipherEngine) { case BlockCiphers.RHX: case BlockCiphers.RSM: case BlockCiphers.SHX: case BlockCiphers.THX: case BlockCiphers.TSM: return 320; default: return 32; } }
/// <summary> /// Initialize the structure with parameters for any supported type of Mac generator /// </summary> /// /// <param name="MacType">The type of Mac generator; Cmac, Hmac, or Vmac</param> /// <param name="KeySize">The mac/cipher key size in bytes</param> /// <param name="IvSize">Size of the Mac Initialization Vector</param> /// <param name="HmacEngine">The <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.Digests">Digest</see> engine used in the Hmac</param> /// <param name="EngineType">The symmetric block cipher <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.SymmetricEngines">Engine</see> type</param> /// <param name="RoundCount">The number of diffusion <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.RoundCounts">Rounds</see></param> /// <param name="BlockSize">The cipher <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.BlockSizes">Block Size</see></param> /// <param name="KdfEngine">The <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.Digests">Digest</see> engine used to power the key schedule Key Derivation Function in HX and M series ciphers</param> public MacDescription(Macs MacType, int KeySize, int IvSize, Digests HmacEngine = Digests.SHA512, BlockCiphers EngineType = BlockCiphers.Rijndael, RoundCounts RoundCount = RoundCounts.R14, BlockSizes BlockSize = BlockSizes.B128, Digests KdfEngine = Digests.SHA512) { this.MacType = (int)MacType; this.KeySize = KeySize; this.IvSize = IvSize; this.HmacEngine = (int)HmacEngine; this.EngineType = (int)EngineType; this.RoundCount = (int)RoundCount; this.BlockSize = (int)BlockSize; this.KdfEngine = (int)KdfEngine; }