Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: change WORD to WORD32 #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions QAes/aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ void xor_buf(const BYTE in[], BYTE out[], size_t len)
/*******************
* AES - CBC
*******************/
void aes_encrypt_cbc(const BYTE in[], size_t in_len,const BYTE * lastBlock, BYTE out[], const WORD key[], int keysize, const BYTE iv[])
void aes_encrypt_cbc(const BYTE in[], size_t in_len,const BYTE * lastBlock, BYTE out[], const WORD32 key[], int keysize, const BYTE iv[])
{
BYTE buf_in[AES_BLOCK_SIZE], buf_out[AES_BLOCK_SIZE], iv_buf[AES_BLOCK_SIZE];
int blocks, idx;
Expand All @@ -258,7 +258,7 @@ void aes_encrypt_cbc(const BYTE in[], size_t in_len,const BYTE * lastBlock, BYTE
return;
}

void aes_decrypt_cbc(const BYTE in[], size_t in_len, BYTE out[], const WORD key[], int keysize, const BYTE iv[])
void aes_decrypt_cbc(const BYTE in[], size_t in_len, BYTE out[], const WORD32 key[], int keysize, const BYTE iv[])
{
BYTE buf_in[AES_BLOCK_SIZE], buf_out[AES_BLOCK_SIZE], iv_buf[AES_BLOCK_SIZE];
int blocks, idx;
Expand All @@ -279,7 +279,7 @@ void aes_decrypt_cbc(const BYTE in[], size_t in_len, BYTE out[], const WORD key[
return;
}

int aes_encrypt_cbc_mac(const BYTE in[], size_t in_len, BYTE out[], const WORD key[], int keysize, const BYTE iv[])
int aes_encrypt_cbc_mac(const BYTE in[], size_t in_len, BYTE out[], const WORD32 key[], int keysize, const BYTE iv[])
{
BYTE buf_in[AES_BLOCK_SIZE], buf_out[AES_BLOCK_SIZE], iv_buf[AES_BLOCK_SIZE];
int blocks, idx;
Expand Down Expand Up @@ -322,7 +322,7 @@ void increment_iv(BYTE iv[], int counter_size)

// Performs the encryption in-place, the input and output buffers may be the same.
// Input may be an arbitrary length (in bytes).
void aes_encrypt_ctr(const BYTE in[], size_t in_len, BYTE out[], const WORD key[], int keysize, const BYTE iv[])
void aes_encrypt_ctr(const BYTE in[], size_t in_len, BYTE out[], const WORD32 key[], int keysize, const BYTE iv[])
{
size_t idx = 0, last_block_length;
BYTE iv_buf[AES_BLOCK_SIZE], out_buf[AES_BLOCK_SIZE];
Expand All @@ -345,7 +345,7 @@ void aes_encrypt_ctr(const BYTE in[], size_t in_len, BYTE out[], const WORD key[
xor_buf(out_buf, &out[idx], in_len - idx); // Use the Most Significant bytes.
}

void aes_decrypt_ctr(const BYTE in[], size_t in_len, BYTE out[], const WORD key[], int keysize, const BYTE iv[])
void aes_decrypt_ctr(const BYTE in[], size_t in_len, BYTE out[], const WORD32 key[], int keysize, const BYTE iv[])
{
// CTR encryption is its own inverse function.
aes_encrypt_ctr(in, in_len, out, key, keysize, iv);
Expand All @@ -355,13 +355,13 @@ void aes_decrypt_ctr(const BYTE in[], size_t in_len, BYTE out[], const WORD key[
* AES - CCM
*******************/
// out_len = payload_len + assoc_len
int aes_encrypt_ccm(const BYTE payload[], WORD payload_len, const BYTE assoc[], unsigned short assoc_len,
const BYTE nonce[], unsigned short nonce_len, BYTE out[], WORD *out_len,
WORD mac_len, const BYTE key_str[], int keysize)
int aes_encrypt_ccm(const BYTE payload[], WORD32 payload_len, const BYTE assoc[], unsigned short assoc_len,
const BYTE nonce[], unsigned short nonce_len, BYTE out[], WORD32 *out_len,
WORD32 mac_len, const BYTE key_str[], int keysize)
{
BYTE temp_iv[AES_BLOCK_SIZE], counter[AES_BLOCK_SIZE], mac[16], *buf;
int end_of_buf, payload_len_store_size;
WORD key[60];
WORD32 key[60];

if (mac_len != 4 && mac_len != 6 && mac_len != 8 && mac_len != 10 &&
mac_len != 12 && mac_len != 14 && mac_len != 16)
Expand Down Expand Up @@ -418,13 +418,13 @@ int aes_encrypt_ccm(const BYTE payload[], WORD payload_len, const BYTE assoc[],

// plaintext_len = ciphertext_len - mac_len
// Needs a flag for whether the MAC matches.
int aes_decrypt_ccm(const BYTE ciphertext[], WORD ciphertext_len, const BYTE assoc[], unsigned short assoc_len,
const BYTE nonce[], unsigned short nonce_len, BYTE plaintext[], WORD *plaintext_len,
WORD mac_len, int *mac_auth, const BYTE key_str[], int keysize)
int aes_decrypt_ccm(const BYTE ciphertext[], WORD32 ciphertext_len, const BYTE assoc[], unsigned short assoc_len,
const BYTE nonce[], unsigned short nonce_len, BYTE plaintext[], WORD32 *plaintext_len,
WORD32 mac_len, int *mac_auth, const BYTE key_str[], int keysize)
{
BYTE temp_iv[AES_BLOCK_SIZE], counter[AES_BLOCK_SIZE], mac[16], mac_buf[16], *buf;
int end_of_buf, plaintext_len_store_size;
WORD key[60];
WORD32 key[60];

if (ciphertext_len <= mac_len)
return(FALSE);
Expand Down Expand Up @@ -541,7 +541,7 @@ void ccm_format_payload_data(BYTE buf[], int *end_of_buf, const BYTE payload[],
/////////////////

// Substitutes a word using the AES S-Box.
WORD SubWord(WORD word)
WORD32 SubWord(WORD32 word)
{
unsigned int result;

Expand All @@ -555,10 +555,10 @@ WORD SubWord(WORD word)
// Performs the action of generating the keys that will be used in every round of
// encryption. "key" is the user-supplied input key, "w" is the output key schedule,
// "keysize" is the length in bits of "key", must be 128, 192, or 256.
void aes_key_setup(const BYTE key[], WORD w[], int keysize)
void aes_key_setup(const BYTE key[], WORD32 w[], int keysize)
{
int Nb=4,Nr,Nk,idx;
WORD temp,Rcon[]={0x01000000,0x02000000,0x04000000,0x08000000,0x10000000,0x20000000,
WORD32 temp,Rcon[]={0x01000000,0x02000000,0x04000000,0x08000000,0x10000000,0x20000000,
0x40000000,0x80000000,0x1b000000,0x36000000,0x6c000000,0xd8000000,
0xab000000,0x4d000000,0x9a000000};

Expand Down Expand Up @@ -592,7 +592,7 @@ void aes_key_setup(const BYTE key[], WORD w[], int keysize)
// form of 4 integers (the "w" array). Each integer is XOR'd by one column of the state.
// Also performs the job of InvAddRoundKey(); since the function is a simple XOR process,
// it is its own inverse.
void AddRoundKey(BYTE state[][4], const WORD w[])
void AddRoundKey(BYTE state[][4], const WORD32 w[])
{
BYTE subkey[4];

Expand Down Expand Up @@ -929,7 +929,7 @@ void InvMixColumns(BYTE state[][4])
// (En/De)Crypt
/////////////////

void aes_encrypt(const BYTE in[], BYTE out[], const WORD key[], int keysize)
void aes_encrypt(const BYTE in[], BYTE out[], const WORD32 key[], int keysize)
{
BYTE state[4][4];

Expand Down Expand Up @@ -1002,7 +1002,7 @@ void aes_encrypt(const BYTE in[], BYTE out[], const WORD key[], int keysize)
out[15] = state[3][3];
}

void aes_decrypt(const BYTE in[], BYTE out[], const WORD key[], int keysize)
void aes_decrypt(const BYTE in[], BYTE out[], const WORD32 key[], int keysize)
{
BYTE state[4][4];

Expand Down Expand Up @@ -1087,7 +1087,7 @@ void print_state(BYTE state[][4])
}

// This prints the key (4 consecutive ints) used for a given round as a linear hex string.
void print_rnd_key(WORD key[])
void print_rnd_key(WORD32 key[])
{
int idx;

Expand Down
30 changes: 15 additions & 15 deletions QAes/aes.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

/**************************** DATA TYPES ****************************/
typedef unsigned char BYTE; // 8-bit byte
typedef unsigned int WORD; // 32-bit word, change to "long" for 16-bit machines
typedef unsigned int WORD32; // 32-bit word, change to "long" for 16-bit machines

/*********************** FUNCTION DECLARATIONS **********************/
///////////////////
Expand All @@ -28,17 +28,17 @@ typedef unsigned int WORD; // 32-bit word, change to "long" for 16-b
// 192 bits = 24 字节
// 256 bits = 32 字节
void aes_key_setup(const BYTE key[], // The key, must be 128, 192, or 256 bits
WORD w[], // Output key schedule to be used later
WORD32 w[], // Output key schedule to be used later
int keysize); // Bit length of the key, 128, 192, or 256

void aes_encrypt(const BYTE in[], // 16 bytes of plaintext
BYTE out[], // 16 bytes of ciphertext
const WORD key[], // From the key setup
const WORD32 key[], // From the key setup
int keysize); // Bit length of the key, 128, 192, or 256

void aes_decrypt(const BYTE in[], // 16 bytes of ciphertext
BYTE out[], // 16 bytes of plaintext
const WORD key[], // From the key setup
const WORD32 key[], // From the key setup
int keysize); // Bit length of the key, 128, 192, or 256

///////////////////
Expand All @@ -47,22 +47,22 @@ void aes_decrypt(const BYTE in[], // 16 bytes of ciphertext
void aes_encrypt_cbc(const BYTE in[], // Plaintext
size_t in_len, const BYTE * lastBlock, // Must be a multiple of AES_BLOCK_SIZE
BYTE out[], // Ciphertext, same length as plaintext
const WORD key[], // From the key setup
const WORD32 key[], // From the key setup
int keysize, // Bit length of the key, 128, 192, or 256
const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long

void aes_decrypt_cbc(const BYTE in[],
size_t in_len,
BYTE out[],
const WORD key[],
const WORD32 key[],
int keysize,
const BYTE iv[]);

//// Only output the CBC-MAC of the input.
//int aes_encrypt_cbc_mac(const BYTE in[], // plaintext
// size_t in_len, // Must be a multiple of AES_BLOCK_SIZE
// BYTE out[], // Output MAC
// const WORD key[], // From the key setup
// const WORD32 key[], // From the key setup
// int keysize, // Bit length of the key, 128, 192, or 256
// const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long

Expand All @@ -75,14 +75,14 @@ void aes_decrypt_cbc(const BYTE in[],
void aes_encrypt_ctr(const BYTE in[], // Plaintext
size_t in_len, // Any byte length
BYTE out[], // Ciphertext, same length as plaintext
const WORD key[], // From the key setup
const WORD32 key[], // From the key setup
int keysize, // Bit length of the key, 128, 192, or 256
const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long

void aes_decrypt_ctr(const BYTE in[], // Ciphertext
size_t in_len, // Any byte length
BYTE out[], // Plaintext, same length as ciphertext
const WORD key[], // From the key setup
const WORD32 key[], // From the key setup
int keysize, // Bit length of the key, 128, 192, or 256
const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long

Expand All @@ -91,14 +91,14 @@ void aes_decrypt_ctr(const BYTE in[], // Ciphertext
///////////////////
// Returns True if the input parameters do not violate any constraint.
int aes_encrypt_ccm(const BYTE plaintext[], // IN - Plaintext.
WORD plaintext_len, // IN - Plaintext length.
WORD32 plaintext_len, // IN - Plaintext length.
const BYTE associated_data[], // IN - Associated Data included in authentication, but not encryption.
unsigned short associated_data_len, // IN - Associated Data length in bytes.
const BYTE nonce[], // IN - The Nonce to be used for encryption.
unsigned short nonce_len, // IN - Nonce length in bytes.
BYTE ciphertext[], // OUT - Ciphertext, a concatination of the plaintext and the MAC.
WORD *ciphertext_len, // OUT - The length of the ciphertext, always plaintext_len + mac_len.
WORD mac_len, // IN - The desired length of the MAC, must be 4, 6, 8, 10, 12, 14, or 16.
WORD32 *ciphertext_len, // OUT - The length of the ciphertext, always plaintext_len + mac_len.
WORD32 mac_len, // IN - The desired length of the MAC, must be 4, 6, 8, 10, 12, 14, or 16.
const BYTE key[], // IN - The AES key for encryption.
int keysize); // IN - The length of the key in bits. Valid values are 128, 192, 256.

Expand All @@ -109,14 +109,14 @@ int aes_encrypt_ccm(const BYTE plaintext[], // IN - Plaintext.
// authentication enabled (mac_auth != NULL) and make a second call to that
// ignores authentication explicitly if the first call failes.
int aes_decrypt_ccm(const BYTE ciphertext[], // IN - Ciphertext, the concatination of encrypted plaintext and MAC.
WORD ciphertext_len, // IN - Ciphertext length in bytes.
WORD32 ciphertext_len, // IN - Ciphertext length in bytes.
const BYTE assoc[], // IN - The Associated Data, required for authentication.
unsigned short assoc_len, // IN - Associated Data length in bytes.
const BYTE nonce[], // IN - The Nonce to use for decryption, same one as for encryption.
unsigned short nonce_len, // IN - Nonce length in bytes.
BYTE plaintext[], // OUT - The plaintext that was decrypted. Will need to be large enough to hold ciphertext_len - mac_len.
WORD *plaintext_len, // OUT - Length in bytes of the output plaintext, always ciphertext_len - mac_len .
WORD mac_len, // IN - The length of the MAC that was calculated.
WORD32 *plaintext_len, // OUT - Length in bytes of the output plaintext, always ciphertext_len - mac_len .
WORD32 mac_len, // IN - The length of the MAC that was calculated.
int *mac_auth, // OUT - TRUE if authentication succeeded, FALSE if it did not. NULL pointer will ignore the authentication.
const BYTE key[], // IN - The AES key for decryption.
int keysize); // IN - The length of the key in BITS. Valid values are 128, 192, 256.
Expand Down
2 changes: 1 addition & 1 deletion QAes/qaeswrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class QAesWrap
void initPadding(const QByteArray & in,QByteArray & out,AesMode mode,PaddingMode pad) const;
private:
AesBit mbit;
WORD mpass[60];
WORD32 mpass[60];
BYTE msalt[AES_BLOCK_SIZE];
};

Expand Down