I would like to know how much padding OpenSSL algorithms expect.

| improve this question | | | | |
  • $\begingroup$ The answers here will reflect the higher level primitives of course, if you directly use block encrypt then it will probably not pad at all. $\endgroup$ – Maarten Bodewes Sep 23 '13 at 14:21

The documentation says:

All the block ciphers normally use PKCS#5 padding also known as standard block padding

which is both informative, and slightly misleading. OpenSSL supports, by default, one stream cipher (RC4) and a variety of block ciphers (Blowfish, 3DES, AES...). The enc command (from the command-line tool) encrypts an input file into an OpenSSL-specific format, with a custom header (of length 16 bytes), and then the encrypted data as processed through the selected algorithm. For RC4, there is no padding: the encrypted data will have the same length as the source file (but there will be the extra 16-byte header). For block ciphers, it depends on the mode of operation:

  • CBC and ECB modes require that the input consists in an integral number of blocks, so padding must be applied to ensure that. PKCS#5 padding (identical to PKCS#7 padding) adds at least one byte, at most 255 bytes; OpenSSL will add the minimal number of bytes needed to reach the next multiple of the block size, so if blocks have size n, then padding will involve between 1 and n extra bytes (including). Block size depends on the algorithm: Blowfish and 3DES use 8-byte blocks, AES uses 16-byte blocks.

  • CFB and OFB modes don't require padding, so there is none applied.

| improve this answer | | | | |
  • $\begingroup$ By definition, when you decrypt, the decryption engine removes the padding. So that engine reliably detects the padding length; this is why PKCS#5 always adds at least one byte (otherwise, the padding would be ambiguous). However, you cannot know the padding length without decrypting. As for encryption, then just look at the length of the data before and after encryption. $\endgroup$ – Tom Leek Sep 22 '13 at 19:33
  • $\begingroup$ As of now, OpenSSL does not seem to append any custom header when encrypting the data. $\endgroup$ – Eugene Mayevski 'Callback Jun 22 '18 at 10:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy