Archive for the ‘Ada’ Category

Base64: Genesis.

Wednesday, November 7th, 2018

I wrote an Base64 Ada library as an exercise in FFA-style programming.

Why? First of all, to gain more experience using Ada, including the heavily-restricted Ada that appears to be the Republican standard1. Second, to improve the skill of writing branchless code, which is standard technique for implementing cryptographic algorithms. Even if the idea of branchless Base64 may make little sense security-wise2, code written without branches (thus, free from branch mispredictions) can still be very fast on current CPUs. In the end, SIMD code is written exactly this way.

Why Base64? It is a well-defined standard encoding that is popular on the Internet, therefore the chances that it will be useful are higher than in case of, say, Caesar cypher, or any other toy encoding. For example, a variant of Base64 is used in GPG.

The implementation itself is pretty straightforward, the only document you may need for understanding it is RFC 4648.

I also provide two test applications together with the library: abase64 and adebase64. abase64 is equivalent to 'base64 -w0' that reads input only through standard input, and outputs the encoded message to standard output without any whitespace3. The other test application, adebase64, does base64 decoding on command line: reads an encoded message as a single-line from standard input4, and writes the decoded message to standard output.

The patches are in Keccak, not sha512 format:

curl 'http://bvt-trace.net/vpatches/base64_genesis.vpatch' > base64_genesis.vpatch
curl 'http://bvt-trace.net/vpatches/base64_genesis.vpatch.bvt.sig' > base64_genesis.vpatch.bvt.sig
curl 'http://wot.deedbot.org/6CF3EFF892A7F23E7E798E5EBA6B8C054B962B68.asc' > bvt.asc
  1. And perhaps, find bugs/hacks in GNAT? The code I had to write as a workaround for this problem is rather ugly, so I will make a follow-up vpatch when I find a proper solution. []
  2. Why would you want to have a Base64 encoding before encryption? Why would you care about leaking bits of cyphertext? []
  3. If you need a default base64 behavior, use abase64 < input-file | fold -w 76. []
  4. Use tr -d '\n' as input filter to decode 'human-readable' message with line breaks. []