kokke/tiny-AES-c

what could cause ecb mode decrypt to be slower than ecb mode encrypt?

davidalide opened this issue · 2 comments

on x86 linux, ecb decrypt seems 2x slower than encrypt
on stm32 M4f, i am seeing a factor of 6x difference

I am using the ecb encrypt/decrypt to implement aes-xts mode

I would think cbc mode would also show the same difference since both use underlying cipher, invcipher functions

openssl aes speed test doesn't show this difference so i don't think its fundamental to algorithm

kokke commented

Hi @davidalide and thanks for your interest :)

The encryption process is usually much faster than decryption. The reason has to do with the implementation.

The MixColumns-operation is faster to calculate than the InvMixColumns. This is usually alleviated a bit with HW-instructions like AES-NI (which is what the OpenSSL uses). That is probably the reason you see a larger difference on the software-implementation than on OpenSSL (2x vs 6x difference).

You can see some of the same effect in BearSSL: https://www.bearssl.org/speed.html

See these questions/answers for more context
https://crypto.stackexchange.com/questions/27872/aes-decryption-vs-encryption-speed
https://crypto.stackexchange.com/questions/5603/on-the-fly-computation-of-aes-round-keys-for-decryption

This is quite a slow implementation because of the way it is written (with compactness as main focus).

You can gain a lot of speed simply by choosing an implementation that uses lookup-tables.

I hope that helped answer your questions. Otherwise please feel free to get back :)