FastAES - Fast AES implementation for Ruby in C

This is a lightweight, fast implementation of AES (the US government’s Advanced Encryption Standard, aka “Rijndael”), written in C for speed. You can read more on the Wikipedia AES Page. The algorithm itself was extracted from work by Christophe Devine for the open source Netcat clone sbd. According to the community, this is one of the best performing AES implementations available:

> With some exceptions your code performs better than all others in 
> enc[ryption]/dec[ryption]. Do you have an explanation of that fact? Thanks. 
>
Well, I've tried to make the code as simple and straightforward as 
possible; I also used a few basic tricks, like loop unrolling.

Since this library wraps the sbd implementation, it supports a subset of AES, specifically:

  • 128, 192, and 256-bit ciphers

  • Cipher Block Chaining (CBC) mode only

  • Encrypted blocks are padded at 16-bit boundaries (read more on padding)

You can read specifics about AES-CBC in the IPSec-related RFC 3602, if you really care that much.

Bottom line, this gem works. Fast.

Other Ruby AES gems

I couldn’t find any that worked worth a crap. The ruby-aes project has Ruby 1.9 bugs that have been open over two years now, crypt/rijndael doesn’t work on Ruby 1.9 and is SLOOOOOOW (as it’s written in Ruby), and some people even report getting inconsistent encryption results from other libraries.

So I grabbed some C reference code, wrapped a Ruby interface around it, and voíla.

C’mon people, it’s not that hard. It’s called Google. In my day, you had to actually WRITE the code.

Installation

gem install gemcutter
gem install fast-aes

Example

Simple encryption/decryption:

require 'fast-aes'

# key can be 128, 192, or 256 bits
key = '424b3b5c4d454c7a51376748255d7b7156585f543f776243227352746f' 

aes = FastAES.new(key)

text = "Hey there, how are you?"

data = aes.encrypt(text)

puts aes.decrypt(data)   # "Hey there, how are you?"

Pretty simple, jah?

Why AES?

SSL vs AES

I’m going to guess you’re using Ruby with Rails, which means you’re doing 90+% web development. In that case, if you need security, SSL is the obvious choice (and the right one).

But there will probably come a time, padawan, when you need a couple backend servers to talk - maybe job servers, or an admin port, or whatever. Maybe even a simple chat server.

You can use SSL for this if you want it to be time-consuming to setup, painful to maintain, and slow. Or you can use a different algorithm, such as AES. Setting up an SSH tunnel is another good alternative (although AES is faster, and setup is slightly easier).

AES vs Other Encryption Standards

There are a bizillion (literally!) different encryption standards out there. If you have a PhD, and can’t find a job, writing an encryption algorithm is a good thing to put on your resume - on that outside chance that someone will hire you and use it. If you don’t possess the talent to write an encryption standard, you can spend hours trying to crack one - for similar reasons. As a result, of the many encryption alternatives, most are either (a) cracked or (b) covered by patents.

Personally, when it comes to encryption, I think choosing what the US government chooses is a decent choice. They tend to be “security conscious.”

Special Note

As this software deals with encryption/decryption, please note there is NO WARRANTY, not even with regards to FITNESS FOR A PARTICULAR PURPOSE or NONINFRINGEMENT. This means if you use this library, and it turns out there’s a flaw in the implementation that results in your data being hacked, IT IS NOT MY FAULT. It’s YOUR responsibility to check the implementation of this library and algorithm. If you can’t understand C code, that’s NOT MY PROBLEM.

Author

Original AES C reference code by Christophe Devine. Thanks Christophe!

This gem copyright © 2010 Nate Wiger. Released under the MIT License.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.