private void CompareMode(byte[] key, byte[] iv, byte[] data, PaddingModes Padding) { PaddingMode mngPad = PaddingMode.Zeros; if (Padding == PaddingModes.PKCS7) mngPad = PaddingMode.PKCS7; else if (Padding == PaddingModes.X923) mngPad = PaddingMode.ANSIX923; byte[] enc1 = EncryptRDX(key, iv, data, Padding); byte[] enc2 = EncryptManaged(key, iv, data, mngPad); // bizarre .Net bug: (sometimes) it will add a *full block* of padding for no reason! // even if input ends on a block boundary, will add a full block in PKCS7!! if (enc1.Length == enc2.Length) { if (Compare.AreEqual(enc1, enc2) == false) throw new Exception("Encrypted output is not equal!"); byte[] dec1 = DecryptRDX(key, iv, data, Padding); byte[] dec2 = DecryptManaged(key, iv, data, mngPad); if (Compare.AreEqual(dec1, dec2) == false) throw new Exception("Decrypted output is not equal to input data!"); } }
private void CompareMode(byte[] key, byte[] iv, byte[] data, PaddingModes Padding) { PaddingMode mngPad = PaddingMode.Zeros; if (Padding == PaddingModes.PKCS7) { mngPad = PaddingMode.PKCS7; } else if (Padding == PaddingModes.X923) { mngPad = PaddingMode.ANSIX923; } byte[] enc1 = EncryptRDX(key, iv, data, Padding); byte[] enc2 = EncryptManaged(key, iv, data, mngPad); // bizarre .Net bug: (sometimes) it will add a *full block* of padding for no reason! // even if input ends on a block boundary, will add a full block in PKCS7!! if (enc1.Length == enc2.Length) { if (Compare.AreEqual(enc1, enc2) == false) { throw new Exception("Encrypted output is not equal!"); } byte[] dec1 = DecryptRDX(key, iv, data, Padding); byte[] dec2 = DecryptManaged(key, iv, data, mngPad); if (Compare.AreEqual(dec1, dec2) == false) { throw new Exception("Decrypted output is not equal to input data!"); } } }
private IPadding GetPaddingMode(PaddingModes PaddingType) { try { return(PaddingFromName.GetInstance(PaddingType)); } catch (Exception ex) { throw new CryptoProcessingException("CipherStream:GetPaddingMode", ex); } }
static public TorchTensor Pad(TorchTensor input, long[] pad, PaddingModes mode = PaddingModes.Constant, double value = 0) { unsafe { fixed(long *psize = pad) { var res = THSNN_pad(input.Handle, (IntPtr)psize, pad.Length, (byte)mode, value); if (res == IntPtr.Zero) { Torch.CheckForErrors(); } return(new TorchTensor(res)); } } }
/// <summary> /// Initialize the class as a Block Cipher /// </summary> /// <param name="Cipher">Block Cipher instance</param> /// <param name="KeyParam">Key and vector material</param> /// <param name="Mode">Cipher mode</param> /// <param name="Padding">Padding type</param> public Transform(IBlockCipher Cipher, KeyParams KeyParam, CipherModes Mode = CipherModes.CTR, PaddingModes Padding = PaddingModes.X923) { this.KeyParam = KeyParam; if (Mode == CipherModes.CBC) this.CipherMode = new CBC(Cipher); else this.CipherMode = new CTR(Cipher); if (Padding == PaddingModes.PKCS7) this.Padding = new PKCS7(); else if (Padding == PaddingModes.X923) this.Padding = new X923(); this.IsStream = false; }
private byte[] EncryptRDX(byte[] Key, byte[] Vector, byte[] Data, PaddingModes Padding = PaddingModes.Zeros) { int blockSize = Vector.Length; int dataLen = Data.Length; int remainder = dataLen % blockSize; int blocks = Data.Length / blockSize; int alignedSize = blocks * blockSize; int lastBlock = alignedSize - blockSize == 0 ? blockSize : alignedSize - blockSize; int outSize = remainder > 0 ? alignedSize + blockSize : alignedSize; byte[] outputData = new byte[outSize]; IPadding pad; if (Padding == PaddingModes.PKCS7) { pad = new PKCS7(); } else if (Padding == PaddingModes.X923) { pad = new X923(); } else { pad = new ZeroPad(); } using (ICipherMode mode = new CBC(new RDX())) { mode.Cipher.BlockSize = blockSize; mode.Init(true, Key, Vector); for (int i = 0; i < alignedSize; i += blockSize) { mode.Transform(Data, i, outputData, i); } if (remainder > 0) { byte[] temp = new byte[blockSize]; Buffer.BlockCopy(Data, alignedSize, temp, 0, remainder); pad.AddPadding(temp, (int)remainder); mode.Transform(temp, 0, outputData, blockSize); } } return(outputData); }
/// <summary> /// Create a volume key file using a manual description of the cipher parameters. /// </summary> /// /// <param name="KeyCount">The number of key sets associated with this volume key</param> /// <param name="EngineType">The Cryptographic <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.SymmetricEngines">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="CipherType">The type of <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.CipherModes">Cipher Mode</see></param> /// <param name="PaddingType">The type of cipher <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.PaddingModes">Padding Mode</see></param> /// <param name="BlockSize">The cipher <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.BlockSizes">Block Size</see></param> /// <param name="Rounds">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> /// /// <returns>A populated VolumeKey</returns> public MemoryStream CreateKey(int KeyCount, SymmetricEngines EngineType, int KeySize, IVSizes IvSize, CipherModes CipherType, PaddingModes PaddingType, BlockSizes BlockSize, RoundCounts Rounds, Digests KdfEngine) { CipherDescription dsc = new CipherDescription() { EngineType = (int)EngineType, KeySize = KeySize, IvSize = (int)IvSize, CipherType = (int)CipherType, PaddingType = (int)PaddingType, BlockSize = (int)BlockSize, RoundCount = (int)Rounds, KdfEngine = (int)KdfEngine, }; return(CreateKey(dsc, KeyCount)); }
static public Tensor pad(Tensor input, long pad, PaddingModes mode = PaddingModes.Constant, double value = 0) { long[] correctedPad = new long[] { pad, pad, pad, pad }; unsafe { fixed(long *psize = correctedPad) { var res = THSNN_pad(input.Handle, (IntPtr)psize, 4, (byte)mode, value); if (res == IntPtr.Zero) { torch.CheckForErrors(); } return(new Tensor(res)); } } }
private byte[] DecryptRDX(byte[] Key, byte[] Vector, byte[] Data, PaddingModes Padding = PaddingModes.Zeros) { int blockSize = Vector.Length; int dataLen = Data.Length; int blocks = Data.Length / blockSize; int lastBlock = dataLen - blockSize == 0 ? blockSize : dataLen - blockSize; byte[] outputData = new byte[Data.Length]; IPadding pad; if (Padding == PaddingModes.PKCS7) { pad = new PKCS7(); } else if (Padding == PaddingModes.X923) { pad = new X923(); } else { pad = new ZeroPad(); } using (ICipherMode mode = new CBC(new RDX())) { mode.Cipher.BlockSize = blockSize; mode.Init(false, Key, Vector); for (int i = 0; i < dataLen; i += blockSize) { mode.Transform(Data, i, outputData, i); } int size = pad.GetPaddingLength(outputData); if (size > 0) { Array.Resize <byte>(ref outputData, dataLen - (size - 1)); } } return(outputData); }
/// <summary> /// Create a single use Key file using a manual description of the cipher parameters. /// </summary> /// /// <param name="KeyParam">An initialized and populated key material container</param> /// <param name="EngineType">The Cryptographic <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.SymmetricEngines">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="CipherType">The type of <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.CipherModes">Cipher Mode</see></param> /// <param name="PaddingType">The type of cipher <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.PaddingModes">Padding Mode</see></param> /// <param name="BlockSize">The cipher <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.BlockSizes">Block Size</see></param> /// <param name="Rounds">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> /// <param name="MacSize">The size of the HMAC message authentication code; a zeroed parameter means authentication is not enabled with this key</param> /// <param name="MacEngine">The HMAC <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.Digests">Digest</see> engine used to authenticate a message file encrypted with this key</param> /// /// <exception cref="System.ArgumentNullException">Thrown if a KeyParams member is null, but specified in the Header</exception> /// <exception cref="System.ArgumentOutOfRangeException">Thrown if a Header parameter does not match a KeyParams value</exception> public void Create(KeyParams KeyParam, SymmetricEngines EngineType, int KeySize, IVSizes IvSize, CipherModes CipherType, PaddingModes PaddingType, BlockSizes BlockSize, RoundCounts Rounds, Digests KdfEngine, int MacSize, Digests MacEngine) { CipherDescription dsc = new CipherDescription() { EngineType = (int)EngineType, KeySize = KeySize, IvSize = (int)IvSize, CipherType = (int)CipherType, PaddingType = (int)PaddingType, BlockSize = (int)BlockSize, RoundCount = (int)Rounds, KdfEngine = (int)KdfEngine, MacEngine = (int)MacEngine, MacKeySize = MacSize }; Create(dsc, KeyParam); }
/// <summary> /// Get a Padding Mode by name /// </summary> /// /// <param name="PaddingType">The padding enumeration name</param> /// /// <returns>An initialized padding mode</returns> /// /// <exception cref="CryptoProcessingException">Thrown if the enumeration name is not supported</exception> public static IPadding GetInstance(PaddingModes PaddingType) { switch (PaddingType) { case PaddingModes.ISO7816: return(new ISO7816()); case PaddingModes.PKCS7: return(new PKCS7()); case PaddingModes.TBC: return(new TBC()); case PaddingModes.X923: return(new X923()); default: throw new CryptoProcessingException("PaddingFromName:GetPadding", "The padding mode is not recognized!"); } }
static public Tensor pad(Tensor input, long[] pad, PaddingModes mode = PaddingModes.Constant, double value = 0) { // // The Pytorch documentation does not cover what is actually happening in the native code, as far as // the ordering of padding elements goes. This code converts from the documented order, to the actual. // See: https://pytorch.org/vision/stable/transforms.html#torchvision.transforms.functional.pad // long[] correctedPad = pad; if (input.ndim > 1) { switch (pad.Length) { case 1: correctedPad = new long[] { pad[0], pad[0], pad[0], pad[0] }; break; case 2: correctedPad = new long[] { pad[0], pad[0], pad[1], pad[1] }; break; case 4: correctedPad = new long[] { pad[0], pad[2], pad[1], pad[3] }; break; } } unsafe { fixed(long *psize = correctedPad) { var res = THSNN_pad(input.Handle, (IntPtr)psize, correctedPad.Length, (byte)mode, value); if (res == IntPtr.Zero) { torch.CheckForErrors(); } return(new Tensor(res)); } } }
internal Pad(long[] pad, PaddingModes mode = PaddingModes.Constant, double value = 0) { this.pad = pad; this.mode = mode; this.value = value; }
/// <summary> /// Create a volume key file using a manual description of the cipher parameters. /// </summary> /// /// <param name="KeyCount">The number of key sets associated with this volume key</param> /// <param name="EngineType">The Cryptographic <see cref="SymmetricEngines">Engine</see> type</param> /// <param name="KeySize">The cipher Key Size in bytes</param> /// <param name="IvSize">Size of the cipher <see cref="IVSizes">Initialization Vector</see></param> /// <param name="CipherType">The type of <see cref="CipherModes">Cipher Mode</see></param> /// <param name="PaddingType">The type of cipher <see cref="PaddingModes">Padding Mode</see></param> /// <param name="BlockSize">The cipher <see cref="BlockSizes">Block Size</see></param> /// <param name="Rounds">The number of diffusion <see cref="RoundCounts">Rounds</see></param> /// <param name="KdfEngine">The <see cref="Digests">Digest</see> engine used to power the key schedule Key Derivation Function in HX and M series ciphers</param> /// <param name="MacSize">The size of the HMAC message authentication code; a zeroed parameter means authentication is not enabled with this key</param> /// <param name="MacEngine">The HMAC <see cref="Digests">Digest</see> engine used to authenticate a message file encrypted with this key</param> /// /// <exception cref="System.IO.FileLoadException">A key file exists at the path specified</exception> /// <exception cref="System.UnauthorizedAccessException">The key file path is read only</exception> public void Create(int KeyCount, SymmetricEngines EngineType, int KeySize, IVSizes IvSize, CipherModes CipherType, PaddingModes PaddingType, BlockSizes BlockSize, RoundCounts Rounds, Digests KdfEngine, int MacSize, Digests MacEngine) { CipherDescription dsc = new CipherDescription() { EngineType = (int)EngineType, KeySize = KeySize, IvSize = (int)IvSize, CipherType = (int)CipherType, PaddingType = (int)PaddingType, BlockSize = (int)BlockSize, RoundCount = (int)Rounds, KdfEngine = (int)KdfEngine, MacEngine = (int)MacEngine, MacSize = MacSize }; Create(dsc, KeyCount); }
/// <summary> /// CipherDescription constructor /// </summary> /// /// <param name="EngineType">The Cryptographic <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.SymmetricEngines">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="CipherType">The type of <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.CipherModes">Cipher Mode</see></param> /// <param name="PaddingType">The type of cipher <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.PaddingModes">Padding Mode</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> digest engine used to power the key schedule Key Derivation Function</param> /// <param name="MacKeySize">The size of the HMAC key in bytes; a zeroed parameter means authentication is not enabled with this key</param> /// <param name="MacEngine">The HMAC <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.Digests">Digest</see> engine used to authenticate a message file encrypted with this key</param> /// /// <exception cref="System.ArgumentOutOfRangeException">Thrown if an invalid KeyId, MessageKey, or ExtensionKey is used</exception> public CipherDescription(SymmetricEngines EngineType, int KeySize, IVSizes IvSize, CipherModes CipherType, PaddingModes PaddingType, BlockSizes BlockSize, RoundCounts RoundCount, Digests KdfEngine = Digests.None, int MacKeySize = 0, Digests MacEngine = Digests.None) { this.EngineType = (int)EngineType; this.KeySize = KeySize; this.IvSize = (int)IvSize; this.CipherType = (int)CipherType; this.PaddingType = (int)PaddingType; this.BlockSize = (int)BlockSize; this.RoundCount = (int)RoundCount; this.KdfEngine = (int)KdfEngine; this.MacKeySize = MacKeySize; this.MacEngine = (int)MacEngine; }
private IPadding GetPadding(PaddingModes PaddingType) { if (PaddingType == PaddingModes.ISO7816) return new ISO7816(); else if (PaddingType == PaddingModes.PKCS7) return new PKCS7(); else if (PaddingType == PaddingModes.TBC) return new TBC(); else if (PaddingType == PaddingModes.X923) return new X923(); else return new PKCS7(); }
private byte[] EncryptRDX(byte[] Key, byte[] Vector, byte[] Data, PaddingModes Padding = PaddingModes.Zeros) { int blockSize = Vector.Length; int dataLen = Data.Length; int remainder = dataLen % blockSize; int blocks = Data.Length / blockSize; int alignedSize = blocks * blockSize; int lastBlock = alignedSize - blockSize == 0 ? blockSize : alignedSize - blockSize; int outSize = remainder > 0 ? alignedSize + blockSize : alignedSize; byte[] outputData = new byte[outSize]; IPadding pad; if (Padding == PaddingModes.PKCS7) pad = new PKCS7(); else if (Padding == PaddingModes.X923) pad = new X923(); else pad = new ZeroPad(); using (ICipherMode mode = new CBC(new RDX())) { mode.Cipher.BlockSize = blockSize; mode.Init(true, Key, Vector); for (int i = 0; i < alignedSize; i += blockSize) mode.Transform(Data, i, outputData, i); if (remainder > 0) { byte[] temp = new byte[blockSize]; Buffer.BlockCopy(Data, alignedSize, temp, 0, remainder); pad.AddPadding(temp, (int)remainder); mode.Transform(temp, 0, outputData, blockSize); } } return outputData; }
static public TorchTensor Conv3d(TorchTensor x, long inputChannel, long outputChannel, long kernelSize, long stride = 1, long padding = 0, long dilation = 1, PaddingModes paddingMode = PaddingModes.Zeros, long groups = 1, bool bias = true) { using (var d = Modules.Conv3d(inputChannel, outputChannel, kernelSize, stride, padding, dilation, paddingMode, groups, bias)) { return(d.forward(x)); } }
private byte[] DecryptRDX(byte[] Key, byte[] Vector, byte[] Data, PaddingModes Padding = PaddingModes.Zeros) { int blockSize = Vector.Length; int dataLen = Data.Length; int blocks = Data.Length / blockSize; int lastBlock = dataLen - blockSize == 0 ? blockSize : dataLen - blockSize; byte[] outputData = new byte[Data.Length]; IPadding pad; if (Padding == PaddingModes.PKCS7) pad = new PKCS7(); else if (Padding == PaddingModes.X923) pad = new X923(); else pad = new ZeroPad(); using (ICipherMode mode = new CBC(new RDX())) { mode.Cipher.BlockSize = blockSize; mode.Init(false, Key, Vector); for (int i = 0; i < dataLen; i += blockSize) mode.Transform(Data, i, outputData, i); int size = pad.GetPaddingLength(outputData); if (size > 0) Array.Resize<byte>(ref outputData, dataLen - (size - 1)); } return outputData; }
/// <summary> /// Pad the given image on all sides with the given “pad” value. /// </summary> /// <param name="padding"> /// Padding on each border. If a single int is provided this is used to pad all borders. /// If sequence of length 2 is provided this is the padding on left/right and top/bottom respectively. /// If a sequence of length 4 is provided this is the padding for the left, top, right and bottom borders respectively. /// </param> /// <param name="fill">Pixel fill value for constant fill.</param> /// <param name="mode"></param> static public ITransform Pad(long[] padding, PaddingModes mode = PaddingModes.Constant, double fill = 0) { return(new Pad(padding, mode, fill)); }
static public ConvTranspose1d ConvTranspose1d(long inputChannel, long outputChannel, long kernelSize, long stride = 1, long padding = 0, long outputPadding = 0, long dilation = 1, PaddingModes paddingMode = PaddingModes.Zeros, long groups = 1, bool bias = true) { var res = THSNN_ConvTranspose1d_ctor(inputChannel, outputChannel, kernelSize, stride, padding, outputPadding, dilation, (long)paddingMode, groups, bias, out var boxedHandle); if (res == IntPtr.Zero) { torch.CheckForErrors(); } return(new ConvTranspose1d(res, boxedHandle)); }
/// <summary> /// CipherDescription constructor /// </summary> /// /// <param name="EngineType">The Cryptographic <see cref="SymmetricEngines">Engine</see> type</param> /// <param name="KeySize">The cipher Key Size in bytes</param> /// <param name="IvSize">Size of the cipher <see cref="IVSizes">Initialization Vector</see></param> /// <param name="CipherType">The type of <see cref="CipherModes">Cipher Mode</see></param> /// <param name="PaddingType">The type of cipher <see cref="PaddingModes">Padding Mode</see></param> /// <param name="BlockSize">The cipher <see cref="BlockSizes">Block Size</see></param> /// <param name="RoundCount">The number of diffusion <see cref="RoundCounts">Rounds</see></param> /// <param name="KdfEngine">The <see cref="Digests">Digest</see> engine used to power the key schedule Key Derivation Function in HX and M series ciphers</param> /// <param name="MacSize">The size of the HMAC message authentication code; a zeroed parameter means authentication is not enabled with this key</param> /// <param name="MacEngine">The HMAC <see cref="Digests">Digest</see> engine used to authenticate a message file encrypted with this key</param> /// /// <exception cref="System.ArgumentOutOfRangeException">Thrown if an invalid KeyId, MessageKey, or ExtensionKey is used</exception> public CipherDescription(SymmetricEngines EngineType, int KeySize, IVSizes IvSize, CipherModes CipherType, PaddingModes PaddingType, BlockSizes BlockSize, RoundCounts RoundCount, Digests KdfEngine = Digests.SHA512, int MacSize = 64, Digests MacEngine = Digests.SHA512) { this.EngineType = (Int32)EngineType; this.KeySize = KeySize; this.IvSize = (Int32)IvSize; this.CipherType = (Int32)CipherType; this.PaddingType = (Int32)PaddingType; this.BlockSize = (Int32)BlockSize; this.RoundCount = (Int32)RoundCount; this.KdfEngine = (Int32)KdfEngine; this.MacSize = MacSize; this.MacEngine = (Int32)MacEngine; }