Reputation: 423
I'm using AESCryptoServiceProvider which is provided in .net framework with CBC cipher mode and PKCS7 Padding Mode. After going through several articles I'm bit confused about generating IV and transmission as CBC cipher mode needs unique and unpredictable.
AESCryptoServiceProvider provides IV when a new instance created or called generateIV(). Though it states random IV is generated it does not mention whether it uses CSPRNG or which library. Can I rely on using IV that is generated using generateIV() or should I use RNGCryptoServiceProvider? Any other best practices in IV generation?
When transferring the IV should it be transferred plain text or should it be prepended with cipher text? Any best practices regarding transmission of IV?
Thanks in advance.
Upvotes: 0
Views: 1294
Reputation: 5264
In "AESCryptoServiceProvider", generateIV() method use internally CryptGenRandom()
of advapi32.dll
.
Method defination of GenerateIV() :
public override void GenerateIV() {
Contract.Ensures(IVValue != null && IVValue.Length == BlockSizeValue / 8);
Contract.Assert(m_cspHandle != null);
Contract.Assert(BlockSizeValue % 8 == 0);
byte[] iv = new byte[BlockSizeValue / 8];
if (!CapiNative.UnsafeNativeMethods.CryptGenRandom(m_cspHandle, iv.Length, iv)) {
throw new CryptographicException(Marshal.GetLastWin32Error());
}
IVValue = iv;
}
Signature of CryptGenRandom() :
/// <summary>
/// Fill a buffer with cryptographically random bytes
/// </summary>
[DllImport("advapi32", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CryptGenRandom(SafeCspHandle hProv,
int dwLen,
[Out, MarshalAs(UnmanagedType.LPArray)] byte[] pbBuffer);
Upvotes: 1