keithw/mosh — pull request #91 by kmcallister (2012-04-04 07:47:47+00:00)
Title: Rework allocation of aligned buffers, and add crypto tests

This patchset does a few things.

* Add a class for 16-byte-aligned buffers. This simplifies the core crypto routines, especially error-handling (which was already wrong in one place). The destructor of this class handles deallocation.

* Once we have the class, we can handle allocation without `posix_memalign` more robustly. Instead of guessing the right function to use, we `malloc` 15 bytes more than we need, and compute the aligned offset within. I've tested both branches of this `#if` on Linux with Valgrind.

* Add a test suite for the OCB-AES reference implementation, based on the [OCB3 test vectors](http://tools.ietf.org/html/draft-krovetz-ocb-03#appendix-A). This also uses `AlignedBuffer`, which is why it's in the same pull request.

* Add a simple test of the Mosh crypto layer. We encrypt and decrypt a bunch of random messages, interspersed with some random bad ciphertexts which we need to reject.

I am very confident in the changes to aligned allocation, but they do touch critical code and so I would appreciate a careful review.

These test programs do not use any test framework, though I'm open to the idea. They do follow a basic convention. Each one either succeeds and exits with code 0, or prints a failure message to stderr and exits with a non-zero code. Each also prints to stdout as it runs, unless the command-line argument `-q` is given. Eventually we can use this when running tests as part of a normal build.

In some places I chose randomized testing over fixed test vectors, which amounts to prioritizing coverage over reproducibility. My rationale is that these are not tests for guiding development, but rather basic sanity checks. If one of these tests ever fails, something is seriously wrong and we will start by examining the offending Mosh commit, not by analyzing the failing test case. That said, I've made the tests quite verbose (without `-q`) so that we can reconstruct failures to some degree, if we have these logs.