Rijndael (AES) Encryption (ver. 2) for 4.0+/8.0+
The code appearing in RIJNDAEL2.ZIP (available below) offers a drop-in replacement for my previous PB Rijndael/AES implementation originally posted in 2002 and revised in 2005. The primary improvement involves a considerable speed enhancement due to use of ASM (including MMX instructions) and controlled data alignment. See the implementation notes here and in RIJNDAEL2.BAS for information about conditional compiling to control inclusion of non-MMX code.
* * *
Rijndael has been designated by the NIST as "the AES [Advanced Encryption Standard] algorithm" and is described in FIPS-197, which is available here.
Rijndael is free for any use public or private, commercial or non-commercial. This PowerBASIC implementation meets the AES standard for 16-byte block operations.
This PB implementation is hereby placed in the public domain. Use it as you wish. My hope is discourage reliance on home-grown encryption schemes in favor of well-examined, strong, freely available algorithms.
See the included TESTBED.BAS for an implementation example. All code requires compiler releases 4.0/8.0 or later.
Implementation Notes
- The algorithm operates on plaintext blocks of 16 bytes. Encryption of shorter blocks is possible only by padding the plaintext (often with zero bytes), which can be accomplished through several methods. The simplest method assumes that the final byte of plaintext always identifies the number of bytes of padding added, including the final byte itself.
Examples:Code:total plaintext bytes = 30 plaintext blocks encrypted: = 2 final block = chr$(x1 to x14) + chr$(0,2) total plaintext bytes = 40 plaintext blocks encrypted: = 3 final block = chr$(x1 to x8) + chr$(0,0,0,0,0,0,0,8)
- Encryption key lengths can range from 1 to 32 bytes (8 to 256 bits).
- Implementation is handled through an #INCLUDE file. No global data is employed. The code is thread-safe.
- By default, MMX code is run if the MMX capability is available; if unavailable, 32-bit code runs instead. Conditional compiling (through %TRUE/%FALSE setting of the %INCLUDE_NON_MMX equate) controls inclusion of the 32-bit capability. The 32-bit code appears in the file RIJNDAEL32.BAS.
- As presented, the code does not supply a ready-to-use encryption application. It offers necessary pieces only, as well as an illustration of their use. Always keep in mind that most encryption is broken because of implementation flaws and weaknesses.
- Like most encryption algorithms, Rijndael was designed on big-endian systems. For this reason, little-endian systems return correct test vector results only through considerable byte-swapping, with efficiency reduced as a result. Because it adds nothing to the encryption security, an efficient implementation can avoid the byte-swapping by compiling with the %RETURN_LITTLE_ENDIAN equate set to %FALSE.
- Because of the methods of data handling required for most encryption and hashing, PowerBASIC's LONGs should be used to assure correct bit-level results (as well as maximum speed).
Greg Turgeon 01/2007
powerbasic.com posting 11/2009
gturgeon_at_ssge_dot_net
Comment