# Is it safe to use cipher block sizing instead of padding?

I'm using this kind of technique instead of padding of cipher buffer (here goes pseudo-code):

struct {
int size;         //size of cipher buffer
byte[32] vector; //random IV (block size is 256 bits)
byte[] buffer;    //encrypted buffer
}


Cipher data encrypted using block cipher with CBC chaining using IV specified as vector

I'm storing this structure "as-is" and in order to decrypt it decrypting buffer block by block then just cutting buffer to size.

Question is: is it safe/secure? Any hints/criticizm?

• @barmaley, what is the danger of being able to modify the message? That depends on what you are using the encryption for. – otus Dec 25 '15 at 9:00
• @barmaley, a MAC rather than a hash, but essentially. – otus Dec 25 '15 at 10:51
• 256-bit block size, really? What cipher is this? – fgrieu Dec 25 '15 at 11:08
• it's Rijndael cipher – Barmaley Dec 25 '15 at 14:16
• Rijndael supports several block size, AES is Rijndael with a block size of 128-bits. – zaph Dec 26 '15 at 16:13

Yes, this is fine.

There is a practical disadvantage in space used, if you don't otherwise need to store the size in plaintext. A size field will usually take 32 or 64 bits, whereas typical padding adds one byte on average. Also, if you use encrypt-then-MAC you need to include the length as part of the authenticated data.

• Of course, the deciphering party should validate the size field: sign, maximum value. – fgrieu Dec 25 '15 at 15:29

It is generally fine EXCEPT the fact, that you should not think that one replaces another : it's the BEST practice to use both, but using one of them is better than not using anything at all. They're helping each other in terms of securing, and they are different things.

• Too weird to understand your point, dude )) – Barmaley Dec 25 '15 at 19:34
• @barmaley The point IS that padding increases the security just by being applied, because no "zerofill trail" and another weak conditions are present in the system. Regardless of the "key length"/"block size"/"bits elaborated". And the increase of "key length"/"block size"/"bits elaborated" is increasing the cost of cracking system(=makes the system more secure), even without any padding at all. They are complementary, they are not replacing each other and not a competing techniques. That's my point. – Alexey Vesnin Dec 25 '15 at 19:52
• Not really sure that padding increases security, in some cases it's vulnerability. See padding oracle attack – Barmaley Dec 25 '15 at 20:50
• @barmaley There are many types of padding and no strict standart for it : it's just a type of process. Design your own schema and embed into your system. – Alexey Vesnin Dec 25 '15 at 21:28
• I don't see any advantage from using both. Padding does not add security. Also, there definitely are standards for padding, like in PKCS#5. – otus Dec 26 '15 at 7:50