Skip to main content

Calling OpenSSL libcrypto functions from Go

I've released on github three Go packages that present the same set of functions as crypto/md5, crypto/sha1 and crypto/rc4 but that use the OpenSSL libcrypto functions instead of the native Go implementation.

Testing by sending 4.4GB of data through these three functions yields the following results (the test document was the King James Bible concatenated 1,000 times and sent to MD5, SHA1 and RC4 in 4.4MB chunks). All tests were done using go version devel +7dc8d66efb6d Mon Jan 21 10:53:39 2013 +1100 linux/amd64.

Native Go404 MB/s123 MB/s111 MB/s
Via OpenSSL607 MB/s636 MB/s744 MB/s

These packages provide the same interface as the native Go implementations and should be drop in replacements. libcrypto must be available. The code is available in my go-openssl repository.

PS As this is the first time I've written any cgo code, I'd be happy to hear from more experienced Go programmers about improvements to my wrappers.

PPS As a comparison here is the output of the Go benchmark tests on the native Go implementations of MD5 and SHA1
% go test -bench=".*" crypto/md5
BenchmarkHash8Bytes          5000000       477 ns/op  16.74 MB/s
BenchmarkHash1K              1000000      2882 ns/op 355.26 MB/s
BenchmarkHash8K               100000     20061 ns/op 408.34 MB/s
BenchmarkHash8BytesUnaligned 5000000       477 ns/op  16.76 MB/s
BenchmarkHash1KUnaligned     1000000      2875 ns/op 356.15 MB/s
BenchmarkHash8KUnaligned      100000     20147 ns/op 406.60 MB/s
ok  crypto/md5 15.997s
% go test -bench=".*" crypto/sha1
BenchmarkHash8Bytes 2000000       799 ns/op  10.00 MB/s
BenchmarkHash1K      200000      9098 ns/op 112.54 MB/s
BenchmarkHash8K       50000     67341 ns/op 121.65 MB/s
ok  crypto/sha 18.364s
And here's the same with the OpenSSL wrapped versions:
% go test -bench=".*" md5
BenchmarkHash8Bytes          5000000       611 ns/op  13.09 MB/s
BenchmarkHash1K              1000000      2272 ns/op 450.54 MB/s
BenchmarkHash8K               100000     13816 ns/op 592.92 MB/s
BenchmarkHash8BytesUnaligned 5000000       616 ns/op  12.97 MB/s
BenchmarkHash1KUnaligned     1000000      2282 ns/op 448.59 MB/s
BenchmarkHash8KUnaligned      100000     14089 ns/op 581.44 MB/s
ok  md5 15.049s
% go test -bench=".*" sha1
BenchmarkHash8Bytes 5000000       625 ns/op  12.78 MB/s
BenchmarkHash1K     1000000      2249 ns/op 455.31 MB/s
BenchmarkHash8K      100000     13507 ns/op 606.46 MB/s
ok  sha1 7.525s


Popular posts from this blog

Your last name contains invalid characters

My last name is "Graham-Cumming". But here's a typical form response when I enter it:

Does the web site have any idea how rude it is to claim that my last name contains invalid characters? Clearly not. What they actually meant is: our web site will not accept that hyphen in your last name. But do they say that? No, of course not. They decide to shove in my face the claim that there's something wrong with my name.

There's nothing wrong with my name, just as there's nothing wrong with someone whose first name is Jean-Marie, or someone whose last name is O'Reilly.

What is wrong is that way this is being handled. If the system can't cope with non-letters and spaces it needs to say that. How about the following error message:

Our system is unable to process last names that contain non-letters, please replace them with spaces.

Don't blame me for having a last name that your system doesn't like, whose fault is that? Saying "Your last name …

All the symmetrical watch faces (and code to generate them)

If you ever look at pictures of clocks and watches in advertising they are set to roughly 10:10 which is meant to be the most attractive (smiling!) position for the hands. They are actually set to 10:09.14 if the hands are truly symmetrical. CC BY 2.0image by Shinji
I wanted to know what all the possible symmetrical watch faces are and so I wrote some code using Processing. Here's the output (there's one watch face missing, 00:00 or 12:00, because it's very boring):

The key to writing this is to figure out the relationship between the hour and minute hands when the watch face is symmetrical. In an hour the minute hand moves through 360° and the hour hand moves through 30° (12 hours are shown on the watch face and 360/12 = 30).
The core loop inside the program is this:   for (int h = 0; h <= 12; h++) {
    float m = (360-30*float(h))*2/13;
    int s = round(60*(m-floor(m)));
    int col = h%6;
    int row = floor(h/6);
    draw_clock((r+f)*(2*col+1), (r+f)*(row*2+1), r, h, floor(m…

Importing an existing SSL key/certificate pair into a Java keystore

I'm writing this blog post in case anyone else has to Google that. In Java 6 keytool has been improved so that it now becomes possible to import an existing key and certificate (say one you generated outside of the Java world) into a keystore.

You need: Java 6 and openssl.

1. Suppose you have a certificate and key in PEM format. The key is named host.key and the certificate host.crt.

2. The first step is to convert them into a single PKCS12 file using the command: openssl pkcs12 -export -in host.crt -inkey host.key > host.p12. You will be asked for various passwords (the password to access the key (if set) and then the password for the PKCS12 file being created).

3. Then import the PKCS12 file into a keystore using the command: keytool -importkeystore -srckeystore host.p12 -destkeystore host.jks -srcstoretype pkcs12. You now have a keystore named host.jks containing the certificate/key you need.

For the sake of completeness here's the output of a full session I performe…