From f06d505218bd3f8c05e9c64f0705bfa3066f665c Mon Sep 17 00:00:00 2001 From: "Ira W. Snyder" Date: Mon, 22 Oct 2007 10:46:26 -0700 Subject: [PATCH] Const-ify Nb, Nk, Nr This nets a little bit (~1.5%) of performance and makes all of the routines a little more safe by making sure that class constants cannot be changed except in the constructor. Signed-off-by: Ira W. Snyder --- aes.cpp | 3 +-- aes.hpp | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/aes.cpp b/aes.cpp index e4aef0b..883d3b3 100644 --- a/aes.cpp +++ b/aes.cpp @@ -9,8 +9,7 @@ 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) { diff --git a/aes.hpp b/aes.hpp index 43808d4..5d2acd8 100644 --- a/aes.hpp +++ b/aes.hpp @@ -29,9 +29,21 @@ class AES byteArray decrypt (const byteArray& ciphertext) const; private: - unsigned int Nb; - unsigned int Nk; - unsigned int Nr; + /* Block size in words -- Always constant in AES. + * + * We also might as well make this static and share it between + * all instances of AES. */ + static const unsigned int Nb = 4; + + /* Key size in words -- can be 4, 6, or 8. + * + * Once it is set by the constructor, it will never change */ + const unsigned int Nk; + + /* Number of rounds -- depends on key size. + * + * Once it is set by the constructor, it will never change */ + const unsigned int Nr; wordArray keySchedule; -- 2.25.1