-
Notifications
You must be signed in to change notification settings - Fork 169
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
How to set CBC padding behavior? #209
Comments
This code is interesting: your padding is almost a perfect match for the PKCS#7 padding we use in Crypto except in the following cases:
This is a very strange way to use CBC. No padding at all is a roughly acceptable thing to do, I think, but the zero padding mode is deeply confusing. Is it really used? |
Zero padding is only used once (for debugging purposes), although I didn't implement that yet. It seems like there are other options for that too, but I'd have to ask someone at Oracle. It's not really necessary for the driver. But enabling no padding would be required in order to do the authentication properly |
So we can add an overload here that avoids the padding. It should be pretty easy really, it's much like the identical code with a check that the length is exactly a multiple of the block size and then skipping appending the final padding block. The existing implementation and the new one can funnel into the same shared code. Would you be open to writing that patch? |
Yeah, sure |
Do you know of any test files I could include for testing the version without padding @Lukasa? |
There isn't any good source for this: unpadded CBC is pretty rare. You may be able to find some in some RFCs. |
Motivation: As described in #209, I personally need this to migrate an oracle driver from a third party crypto lib to swift-crypto. I think other users might benefit from this addition too. Modifications: I've added an overload to the encrypt and decrypt methods of AES._CBC, allowing the user to configure if padding should be added or not. With noPadding set to true, an error will be thrown if the plaintext isn't a multiple of the block size. I've added the corresponding inline documentation. I've also added tests to ensure both encrypting and decrypting without padding work as expected. Although those tests might not be sufficient enough, because I couldn't find good resources online. I've created a bunch of random hex strings and encrypted/decrypted them using another implementation of paddingless CBC and checked if I receive the expected results. To further validate the feature, I've tested it as part of the authentication in oracle-nio, which worked in all test scenarios I've been running. Result: After merging this, it will be possible to use CBC without padding. This closes #209
Question Checklist
Question Subject
I'm currently using CryptoSwift for CBC de-/encryption and I'd like to move that to swift-crypto.
For my use-case I have to disable automatic padding, is that possible?
Question Description
Currently I have the following code to decrypt a CBC encrypted payload sent from an Oracle db server during authenticating:
I tried migrating it to swift-crypto and now it does automatic padding and fails with
CryptoKitError.incorrectParameterSize
, when trying to trim the padding:I guess I'll have the same problem on encryption.
This is the related code: https://github.com/lovetodream/oracle-nio/blob/main/Sources/OracleNIO/Helper/Crypto.swift
The text was updated successfully, but these errors were encountered: