-
-
Notifications
You must be signed in to change notification settings - Fork 67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AES-CBC cipher works without providing an IV parameter. Is this correct? #44
Comments
Hello @mikelbikkel KeyParametersWithIV := TParametersWithIV.Create(TParameterUtilities.CreateKeyParameter('AES', arKey), IVBytes); |
It's tricky in the current implementation. Michel |
@mikelbikkel Yes the code base is still maintained just that there hasn't been any new features added due to time constraints. |
Happy to help. It will be my first PR ever, so i need some time to prepare. |
Describe the bug
A cipher with MODE = CBC requires an IV.
When I create a cipher "AES/CBC/PKCS7PADDING" and I do NOT provide an IV parameter, the cipher works (encrypts an decrypts) without any warning or error. I would expect an error if the IV parameter is not provided.
To Reproduce
var
FCipher: IBufferedCipher;
FParams: ICipherParameters;
begin
FCipher := TCipherUtilities.getCipher('AES/CBC/PKCS7PADDING');
FParams := TParameterUtilities.CreateKeyParameter('AES', arKey); // arKey is a TBytes with the key.
FCipher.Init(True, FParams); // I would expect an exception at this point: MISSING IV.
FCipher.FCipher.ProcessBytes(.... etc. // Encryption succeeds without IV.
end;
The issue seems to be at this point:
procedure TCbcBlockCipher.Init(forEncryption: Boolean; const parameters: ICipherParameters);
if Supports(Lparameters, IParametersWithIV, ivParam) then
begin
iv := ivParam.GetIV();
if (System.Length(iv) <> FblockSize) then
begin
raise EArgumentCryptoLibException.CreateRes(@SInvalidIVLength);
end;
System.Move(iv[0], FIV[0], System.Length(iv) * System.SizeOf(Byte));
end;
// MY REMARK: there is no "else"-branch that handles the missing IV parameter.
// The cipher works without IV because FIV is a 16-byte zero-filled array. These zeros are used as IV.
Reset();
Expected behavior
if Supports(Lparameters, IParametersWithIV, ivParam) then
begin
iv := ivParam.GetIV();
if (System.Length(iv) <> FblockSize) then
begin
raise EArgumentCryptoLibException.CreateRes(@SInvalidIVLength);
end;
System.Move(iv[0], FIV[0], System.Length(iv) * System.SizeOf(Byte));
else
raise EArgumentCryptoLibException.CreateRes(@SMissingIV);
end;
Raise an exception if IV is missing
Reset();
Screenshots
NA
Environment (please complete the following information):
Additional context
Add any other context about the problem here.
The text was updated successfully, but these errors were encountered: