Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

PHP 

Constant-Time RFC 4648 (Base64, Base32, Base16) Encoding

Available on GitHub.

Background

RFC 4648 is an internet standard defined by the Internet Engineering Task Force (IETF) that specifies various ways to encode binary data for use in non-binary-safe transmissions; e.g. email.

These encoding schemes are useful for building cryptography features, especially when you need to store cryptographic key material in a configuration file (e.g. config.json).

However, the way these functions are normally implemented use table look-ups indexed by the data you are encoding.

So I wrote (and am releasing) a PHP implementation of RFC 4648 that performs encoding and decoding in constant-time (i.e. side-steps cache-timing attacks).
They are some strange part in your code, like this: https://github.com/paragonie/constant_ti...32.php#L25

But thanks for this share :)
(Mar 14, 2016 09:22 AM)Sakuto Wrote:  They are some strange part in your code, like this: https://github.com/paragonie/constant_ti...32.php#L25

But thanks for this share :)

Yeah, 7 in binary is 0000 0111.

([whatever] & 0000111) will result in a number between 0 and 7. If it equals 0, then we know we're padded properly, so we remove = characters until we run out of ='s.

After that check, if (length & 7) is not 0, then the message is invalid.
You know that you could just do it in a loop? Same for the $c1 = static::decode5Bits($chunk[2]);, use loop with array instead of repeated code.
Your project is good but it could be optimized I think :)
(Mar 14, 2016 10:09 AM)Sakuto Wrote:  You know that you could just do it in a loop? Same for the $c1 = static::decode5Bits($chunk[2]);, use loop with array instead of repeated code.
Your project is good but it could be optimized I think :)

I would be wary of optimizations that could lead to execution taking a variable amount of time.
Best thing actually is to try, if your unit test are properly written, we'll quickly figure out if it's changing anything :)
(Mar 14, 2016 10:15 AM)Sakuto Wrote:  Best thing actually is to try, if your unit test are properly written, we'll quickly figure out if it's changing anything :)

The tests don't test for nanosecond-scale time differences. :\
I've to admit that I don't know exactly the method used in your script, I'll read some paper on it, make my test and check if optimization can be made or not. Nice share by the way
(Mar 14, 2016 10:58 AM)Sakuto Wrote:  I've to admit that I don't know exactly the method used in your script, I'll read some paper on it, make my test and check if optimization can be made or not. Nice share by the way

Thanks. I'm not sure what papers would be relevant, except perhaps a logic table and the RFC, but feel free to suggest changes.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[PHP] by ZykerJan 14, 2022 03:05 AM 8 272 Jan 14, 2022 03:05 AM
Last Post: lunatichero
[Javascript]  by GeatzoMay 11, 2020 08:35 PM 2 101 May 11, 2020 08:35 PM
Last Post: SIM-3991524
[Javascript]  by AreWeRealApr 6, 2020 02:22 AM 5 215 Apr 6, 2020 02:22 AM
Last Post: AreWeReal