Powerful crypto from the UNIX command line

by Girish Venkatachalam

Girish Venkatachalam is a UNIX hacker with more than a decade of networking and crypto programming experience. His hobbies include yoga,cycling, cooking and he runs his own business. Details here:

http://gayatri-hitech.com

http://spam-cheetah.com

I will be really surprised if a linux distro ships without OpenSSL pre installed. I know that it comes by default since it uses a very liberal license. The importance of OpenSSL toolkit for crypto cannot be overestimated.

OpenSSH project relies on OpenSSL for its backend crypto operations. Though named as an SSL library, it is a very comprehensive toolkit with all kinds of facilities related to cryptography. Even simple things like random number generation, base64 conversion, file integrity checking with SHA1 or prime number generation can be done with the OpenSSL command line tool.

OpenSSL is a crypto toolkit and it produces two libraries libcrypto.so and libssl.so. You could also link to it statically. The main purpose of the OpenSSL project is to provide library facilities for various operations related to SSL. But over time it has evolved into a fantastic command line utility that gets our job done as long as we know a thing or two about crypto.

Knowing a thing or two about crypto is not so easy. Most people have no idea what is meant by PKI. Most people do not understand why RSA keys are 1024 bits and why AES keys are 256 bits. How can the AES key be stronger? Crypto theory is very deep and mathematical in nature. And OpenSSL programming requires very advanced C skills. I remember how I struggled when I worked with the guts of OpenSSL. But for this article, we only want to look at some of the really useful features offered by such a rich toolkit. All from the command line.

OpenSSL also has a shell interface which gets invoked when you type openssl like this:

$ openssl                                                                      
OpenSSL>
 

But we do not have to go inside the shell at all. We can use command line switches for most of our needs. Nowadays you also have GnuPG for performing command line operations like file encryption and message signing. OpenSSL also comes in very handy when you wish to generate server certificates for your Apache HTTPS service. Or you might wish to create a public keypair for accessing your IMAP over SSL account or SMTP over SSL e-mail account.

Let us take a few simple examples.

File integrity checks help us detect file corruption and change. We want to detect a truncated upload or download. We also want to know whether a binary file has changed. With text files you can always use diff. Diff also is useful with binary data but the best approach would be to use a strong fingerprinting algorithm like SHA1 checksum.

There is a command called sha1sum on most linux boxes similar to the cksum utility of yore. OpenSSL can also do the same thing with this command.

$ openssl sha1 /etc/passwd                                                     
SHA1(/etc/passwd)= 61293afc53dd8465a28d49fc1d11676badcd0076

A SHA1 digest output has a constant length of 160 bits and it is in a binary format. This output has 40 bytes because the binary output is Hex encoded. BCD representation takes one byte of binary data and represents it as two ASCII bytes. Hence the output here is 40 characters/bytes.

Let us generate some random data just for fun.

$ openssl rand 1024

Oops. There is a problem here. Your screen gets garbled. What to do? We do not want raw binary data spat to our beautiful terminal. Instead we want to do one of 3 things. We could get a Hex encoded output like above, we could base64 encode it or we could write it to a file by redirection or ask OpenSSL itself to do it with the -out switch.

Base64 encoding is different from the BCD hex representation we saw above. OpenSSL is good with ASN1 encoding and Base64 encoding in addition to crypto stuff. Base64 uses 3 bytes of binary data and create 4 ASCII characters as output. So there is a fixed 33% increase in filesize when you base64 encode a file. Of course you have padding and other variations, but this is the simple math. 3 x 8 = 4 x 6 = 24.

Base64 output uses 2^6 or 64 ASCII characters to represent a byte. Nowadays Base64 encoding is used everywhere. A good example is for e-mail authentication protocols. SMTP authentication uses DIGEST authentication using base64 encoding of passwords.

Evidently OpenSSL is a lot more than this. I hope this gets you started.

References

  1. OpenSSL homepage
  2. GPG homepage





(OLDER) <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> Powerful crypto from the UNIX command line





More Articles by © Girish Venkatachalam



Kerio Connect Mailserver

Kerio Samepage

Kerio Control Firewall

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us