[Cryptography] The GOTO Squirrel! [was GOTO Considered Harmful]

Eric Young eay at pobox.com
Mon Mar 3 07:53:53 EST 2014


On Fri, 2014-02-28 at 21:19 -0500, Andrew Righter wrote:
> I asked this on Twitter but perhaps this is now relevant here:
> 
> Are there unit testing framworks for crypto libs? I suppose they
> would do as Peter suggested below in depth running along side 
> assertions and check equalities. 

I must admit that for the crypto library I mostly develop now
(algorithms, not so much protocols), I have ruby bindings and I am doing
some positive and negative testing using rubys rspec testing framework.
I use the ruby FFI library to bind to my shared library.  The FFI style
bindings are nice because they are defined in your scripting language,
not some C/C++ glue code.  The main negative of using this system is you
don't get all full platform coverage.

BTW I always considered the 'goto err' programming style as a way of
doing
exceptions in C, but without the magic :-).

The way I now handle errors in my bignum library is to have an error
flag in the context that is always passed to functions.  It is checked
on function entry, if there is an error, this is returned, else do the
function.

So

if ((ret = bn_mul(bn_ctx,r,a,b)) != 0) goto err;
if ((ret = bn_add(bn_ctx,r,r,c)) != 0) goto err;
err:
/* cleanup */
return(ret);

becomes

bn_mul(bn_ctx,r,a,b);
bn_add(bn_ctx,r,r,c);
ret = bn_ctx->error;
/* cleanup */
return(ret);

It makes the code easier to read, and in this case, if the function
fails, I don't actually care that much about which line it was.  You do
need a little care to check ctx->error sometimes, especially if your
algorithm has loops.

eric



More information about the cryptography mailing list