AES-CBC implementation

Lazy8s

Golden Member
Jun 23, 2004
1,503
0
0
I need to implement AES-CBC (Advanced Encryption standard - Cipher Block Chaining Mode) for a project I'm doing. I don't have a lot of time. I have been trying to implement it based on the RFCs for AES and AES-CBC but I'm having trouble understanding how to implement it in C++. Does anyone know where an implementation exists or a pseudocode implementation or something to help me out? The RFC just goes into soooooooooooo much detail. I donly have to implement it for 128-bit string so all the extra stuff doesn't matter and the string will actually be 128 bits so I don't even need to pad it. What's really killing me are the S-boxes. Any help would be greatly appreciated.
 

Lazy8s

Golden Member
Jun 23, 2004
1,503
0
0
that was some great advice. The problem is I implemented 2 versions I found that way. they both give me the same encrypted string, but it doesn't seem to be correct!!

The RFC 3602 says:
4. Test Vectors

The first 4 test cases test AES-CBC encryption. Each test case
includes the key, the plaintext, and the resulting ciphertext. The
values of keys and data are either hexadecimal numbers (prefixed by
"0x") or ASCII character strings (surrounded by double quotes). If a
value is an ASCII character string, then the AES-CBC computation for
the corresponding test case DOES NOT include the trailing null
character ('\0') of the string. The computed cyphertext values are
all hexadecimal numbers.

The last 4 test cases illustrate sample ESP packets using AES-CBC for
encryption. All data are hexadecimal numbers (not prefixed by "0x").

These test cases were verified using 2 independent implementations:
the NIST AES-CBC reference implementation and an implementation
provided by the authors of the Rijndael algorithm
(http://csrc.nist.gov/encryption/aes/rijndael/
rijndael-unix-refc.tar).

Case #1: Encrypting 16 bytes (1 block) using AES-CBC with 128-bit key
Key : 0x06a9214036b8a15b512e03d534120006
IV : 0x3dafba429d9eb430b422da802c9fac41
Plaintext : "Single block msg"
Ciphertext: 0xe353779c1079aeb82708942dbe77181a


But when I use that IV and key, both of the programs give me:
CE696893A7170A24D882C6022E3FFA3D
as my encrypted string....this is driving me insane...
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
The RFC says "All data are hexadecimal numbers (not prefixed by "0x")" but the samples themselves -do- include 0x.

Did you copy-paste the samples with the "0x" left in the string?

It could be a byte-ordering issue instead. A C++ blowfish library we use at work uses a different byte ordering for the plaintext and ciphertext than most other perl and java libraries.
 

Lazy8s

Golden Member
Jun 23, 2004
1,503
0
0
I figured it out. Apparently the the algorithm uses two different functions, one takes hex values and the other wants the character representations. All this time I was verifying the calculations the code was doing without looking at the variables it was passing around. Once I started to realize it was converting chars to their hex representations in the sub-functions I looked at the functions I called and realized I had been doing the conversions myself for no reason.

Thanks again man.