Skip to content

Commit

Permalink
Add const qualifiers to input arguments (#64)
Browse files Browse the repository at this point in the history
* Add const qualifiers to input arguments

To make clear which arguments are changed and which are guaranteed
to remain as they are.

This also prevents unintentionally changing something that should
be immutable.
  • Loading branch information
markusfisch authored Apr 30, 2022
1 parent 36d7ea2 commit f694d56
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 deletions.
40 changes: 22 additions & 18 deletions src/AES.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "AES.h"

AES::AES(AESKeyLength keyLength) {
AES::AES(const AESKeyLength keyLength) {
this->Nb = 4;
switch (keyLength) {
case AESKeyLength::AES_128:
Expand All @@ -20,8 +20,8 @@ AES::AES(AESKeyLength keyLength) {
blockBytesLen = 4 * this->Nb * sizeof(unsigned char);
}

unsigned char *AES::EncryptECB(unsigned char in[], unsigned int inLen,
unsigned char key[]) {
unsigned char *AES::EncryptECB(const unsigned char in[], unsigned int inLen,
const unsigned char key[]) {
CheckLength(inLen);
unsigned char *out = new unsigned char[inLen];
unsigned char *roundKeys = new unsigned char[4 * Nb * (Nr + 1)];
Expand All @@ -35,8 +35,8 @@ unsigned char *AES::EncryptECB(unsigned char in[], unsigned int inLen,
return out;
}

unsigned char *AES::DecryptECB(unsigned char in[], unsigned int inLen,
unsigned char key[]) {
unsigned char *AES::DecryptECB(const unsigned char in[], unsigned int inLen,
const unsigned char key[]) {
CheckLength(inLen);
unsigned char *out = new unsigned char[inLen];
unsigned char *roundKeys = new unsigned char[4 * Nb * (Nr + 1)];
Expand All @@ -50,8 +50,9 @@ unsigned char *AES::DecryptECB(unsigned char in[], unsigned int inLen,
return out;
}

unsigned char *AES::EncryptCBC(unsigned char in[], unsigned int inLen,
unsigned char key[], unsigned char *iv) {
unsigned char *AES::EncryptCBC(const unsigned char in[], unsigned int inLen,
const unsigned char key[],
const unsigned char *iv) {
CheckLength(inLen);
unsigned char *out = new unsigned char[inLen];
unsigned char *block = new unsigned char[blockBytesLen];
Expand All @@ -70,8 +71,9 @@ unsigned char *AES::EncryptCBC(unsigned char in[], unsigned int inLen,
return out;
}

unsigned char *AES::DecryptCBC(unsigned char in[], unsigned int inLen,
unsigned char key[], unsigned char *iv) {
unsigned char *AES::DecryptCBC(const unsigned char in[], unsigned int inLen,
const unsigned char key[],
const unsigned char *iv) {
CheckLength(inLen);
unsigned char *out = new unsigned char[inLen];
unsigned char *block = new unsigned char[blockBytesLen];
Expand All @@ -90,8 +92,9 @@ unsigned char *AES::DecryptCBC(unsigned char in[], unsigned int inLen,
return out;
}

unsigned char *AES::EncryptCFB(unsigned char in[], unsigned int inLen,
unsigned char key[], unsigned char *iv) {
unsigned char *AES::EncryptCFB(const unsigned char in[], unsigned int inLen,
const unsigned char key[],
const unsigned char *iv) {
CheckLength(inLen);
unsigned char *out = new unsigned char[inLen];
unsigned char *block = new unsigned char[blockBytesLen];
Expand All @@ -112,8 +115,9 @@ unsigned char *AES::EncryptCFB(unsigned char in[], unsigned int inLen,
return out;
}

unsigned char *AES::DecryptCFB(unsigned char in[], unsigned int inLen,
unsigned char key[], unsigned char *iv) {
unsigned char *AES::DecryptCFB(const unsigned char in[], unsigned int inLen,
const unsigned char key[],
const unsigned char *iv) {
CheckLength(inLen);
unsigned char *out = new unsigned char[inLen];
unsigned char *block = new unsigned char[blockBytesLen];
Expand Down Expand Up @@ -141,7 +145,7 @@ void AES::CheckLength(unsigned int len) {
}
}

void AES::EncryptBlock(unsigned char in[], unsigned char out[],
void AES::EncryptBlock(const unsigned char in[], unsigned char out[],
unsigned char *roundKeys) {
unsigned char **state = new unsigned char *[4];
state[0] = new unsigned char[4 * Nb];
Expand Down Expand Up @@ -179,7 +183,7 @@ void AES::EncryptBlock(unsigned char in[], unsigned char out[],
delete[] state;
}

void AES::DecryptBlock(unsigned char in[], unsigned char out[],
void AES::DecryptBlock(const unsigned char in[], unsigned char out[],
unsigned char *roundKeys) {
unsigned char **state = new unsigned char *[4];
state[0] = new unsigned char[4 * Nb];
Expand Down Expand Up @@ -316,7 +320,7 @@ void AES::Rcon(unsigned char *a, int n) {
a[1] = a[2] = a[3] = 0;
}

void AES::KeyExpansion(unsigned char key[], unsigned char w[]) {
void AES::KeyExpansion(const unsigned char key[], unsigned char w[]) {
unsigned char *temp = new unsigned char[4];
unsigned char *rcon = new unsigned char[4];

Expand Down Expand Up @@ -390,8 +394,8 @@ void AES::InvShiftRows(unsigned char **state) {
ShiftRow(state, 3, Nb - 3);
}

void AES::XorBlocks(unsigned char *a, unsigned char *b, unsigned char *c,
unsigned int len) {
void AES::XorBlocks(const unsigned char *a, const unsigned char *b,
unsigned char *c, unsigned int len) {
for (unsigned int i = 0; i < len; i++) {
c[i] = a[i] ^ b[i];
}
Expand Down
36 changes: 18 additions & 18 deletions src/AES.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,41 +46,41 @@ class AES {

void CheckLength(unsigned int len);

void KeyExpansion(unsigned char key[], unsigned char w[]);
void KeyExpansion(const unsigned char key[], unsigned char w[]);

void EncryptBlock(unsigned char in[], unsigned char out[],
void EncryptBlock(const unsigned char in[], unsigned char out[],
unsigned char key[]);

void DecryptBlock(unsigned char in[], unsigned char out[],
void DecryptBlock(const unsigned char in[], unsigned char out[],
unsigned char key[]);

void XorBlocks(unsigned char *a, unsigned char *b, unsigned char *c,
unsigned int len);
void XorBlocks(const unsigned char *a, const unsigned char *b,
unsigned char *c, unsigned int len);

std::vector<unsigned char> ArrayToVector(unsigned char *a, unsigned int len);

unsigned char *VectorToArray(std::vector<unsigned char> &a);

public:
explicit AES(AESKeyLength keyLength = AESKeyLength::AES_256);
explicit AES(const AESKeyLength keyLength = AESKeyLength::AES_256);

unsigned char *EncryptECB(unsigned char in[], unsigned int inLen,
unsigned char key[]);
unsigned char *EncryptECB(const unsigned char in[], unsigned int inLen,
const unsigned char key[]);

unsigned char *DecryptECB(unsigned char in[], unsigned int inLen,
unsigned char key[]);
unsigned char *DecryptECB(const unsigned char in[], unsigned int inLen,
const unsigned char key[]);

unsigned char *EncryptCBC(unsigned char in[], unsigned int inLen,
unsigned char key[], unsigned char *iv);
unsigned char *EncryptCBC(const unsigned char in[], unsigned int inLen,
const unsigned char key[], const unsigned char *iv);

unsigned char *DecryptCBC(unsigned char in[], unsigned int inLen,
unsigned char key[], unsigned char *iv);
unsigned char *DecryptCBC(const unsigned char in[], unsigned int inLen,
const unsigned char key[], const unsigned char *iv);

unsigned char *EncryptCFB(unsigned char in[], unsigned int inLen,
unsigned char key[], unsigned char *iv);
unsigned char *EncryptCFB(const unsigned char in[], unsigned int inLen,
const unsigned char key[], const unsigned char *iv);

unsigned char *DecryptCFB(unsigned char in[], unsigned int inLen,
unsigned char key[], unsigned char *iv);
unsigned char *DecryptCFB(const unsigned char in[], unsigned int inLen,
const unsigned char key[], const unsigned char *iv);

std::vector<unsigned char> EncryptECB(std::vector<unsigned char> in,
std::vector<unsigned char> key);
Expand Down

0 comments on commit f694d56

Please sign in to comment.