The library supports AES-128 encryption and decryption. It also handles AES-192 and AES-256, but these have not been tested.
On an imp001, a 16-byte blob is normally encrypted or decrypted in 6ms.
On an imp001, a 16-byte blob is normally encrypted or decrypted in 7ms; a 32-byte blob in 13ms (resulting in a 1ms overhead from CBC; which also means you could just always use the CBC version, independent of whether you actually need chaining or not).
The library operates exclusively on blobs, ie. all values passed into class instances and their methods should be blobs, and library methods all return blobs. For developers’ convenience, a helper function that converts a hexadecimal string into a blob is provided by the library: hexStringToBlob(string).
The library can be used in both agent and device code.
To add this library to your project, place
at the top of your code.
The constructor creates an instance of the AES class initialized with the specified key, which must be 128 bits (16 bytes), 192 bits (24 bytes) or 256 bits (32 bytes) long.
#require "AES.lib.nut:1.0.0" local aes = AES(keyBlob);
This method encrypts the specified value using the key with which the AES instance was initialized. valueBlob must be 16-byte blob.
The function returns a blob containing the result of the encryption process.
For string-to-blob conversions, please use hexStringToBlob().
local encrypted = aes.encrypt(rawValue);
This method decrypts the specified cipher. The value of cipherBlob must be a 16-byte blob.
The function returns a blob containing the result of the decryption process.
local decrypted = aes.decrypt(encrypted);
A helper function which converts a hexadecimal string into a blob, which it returns. The function supports one specific string format: just hexadecimal digits/characters, such as
ea32d4b183f0988984f1d536f15fd1f2, without an
No other characters are supported and may result in unpredictable behavior.
Examples of string formats that are not supported:
local keyBlob = AES.hexStringToBlob("86bd8a144720b6b0650cbde99a0db485"); local aes = AES(keyBlob); local valueBlob = AES.hexStringToBlob("6aac72463f833e7df7335433feb4dab2"); local cipherBlob = aes.encrypt(valueBlob);
Creates an instance of the AES.CBC class. The key parameter is a blob containing the encryption key; iv is the initialization vector, which must be 16 bytes long.
This method encrypts the specified value: a blob with a length that must be a multiple of 16 bytes.
This method decrypts the specified cipher. The length of cipherBlob must be a multiple of 16 bytes.
local key = AES.hexStringToBlob("11111111111111111111111111111111"); local iv = AES.hexStringToBlob("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); local cbc = AES.CBC(key, iv); local value = AES.hexStringToBlob("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); local encrypted = cbc.encrypt(value); local decrypted = cbc.decrypt(encrypted);
The Electric Imp Dev Center documents the latest version of the library. For past versions, please see the Electric Imp public GitHub repos listed below.
The AES library is licensed under the MIT License.