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

-

## migrated from security.stackexchange.comSep 23 '13 at 7:35

This question came from our site for information security professionals.

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. – 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.

-
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. – Tom Leek Sep 22 '13 at 19:33
Thanks for your help! – warrior24 Sep 22 '13 at 19:55