X-Git-Url: https://irasnyder.com/gitweb/?a=blobdiff_plain;f=aes.cpp;h=dff959e2a05b1a43fd32f2fec1d9e561b0f44afc;hb=32b64c22ce1b63a4e80ef12097b7ff0c413f3b73;hp=1aa92c497f52133accd7a2b327182971181dee00;hpb=c22feadcd68a2702846bc5d9542f171b64d0ecd2;p=aes.git diff --git a/aes.cpp b/aes.cpp index 1aa92c4..dff959e 100644 --- a/aes.cpp +++ b/aes.cpp @@ -1,17 +1,15 @@ #include "aes.hpp" +#include /* static function prototypes */ static byteArray word2bytes (word input); static word bytes2word (byte b0, byte b1, byte b2, byte b3); -static void circular_left_shift (byteArray &bytes, int shift_amt); -static void circular_right_shift (byteArray &bytes, int shift_amt); static byte mult (const byte ax, const byte bx); static byte xtimes (const byte bx); static void printState (byteArray &bytes, std::string name); AES::AES (const byteArray& key) - : Nb(4) // This is constant in AES - , Nk(key.size() / 4) // This can be either 4, 6, or 8 (128, 192, or 256 bit) + : Nk(key.size() / 4) // This can be either 4, 6, or 8 (128, 192, or 256 bit) , Nr(Nk + Nb + 2) , keySchedule(Nb * (Nr+1), 0x00000000) { @@ -43,7 +41,15 @@ byteArray AES::encrypt (const byteArray& plaintext) const throw incorrectTextSizeException (); int round; - byteArray state (plaintext); + byteArray state; + + /* Copy the plaintext into the state matrix. It is copied in + * column-wise, because the AES Spec. does it this way. + * + * It also allows us to optimize ShiftRows later */ + for (int c=0; c> r*8) | (*w_ptr << ((4-r)*8)); +#else // BIG_ENDIAN + *w_ptr = (*w_ptr << r*8) | (*w_ptr >> ((4-r)*8)); #endif - - // CLS 0, 1, 2, 3 - circular_left_shift (temp, r); - -#if 0 - std::printf ("after cls(%d)=", r); - for (c=0; c> ((4-r)*8)); +#else // BIG_ENDIAN + *w_ptr = (*w_ptr >> (4-r)*8) | (*w_ptr << r*8); +#endif + w_ptr++; } } @@ -281,7 +320,7 @@ void AES::MixColumns (byteArray& state) const { /* Get this column */ for (c=0; c> 24); } wordArray AES::GetRoundKey (const int round) const @@ -385,10 +421,7 @@ void AES::AddRoundKey (byteArray& state, const wordArray& w) const byteArray wBytes = word2bytes (w[i]); for (j=0; j