Skip to content

Commit

Permalink
Endian fixes for the PKey encryption/decryption.
Browse files Browse the repository at this point in the history
The PKey crypto code simply treats the data to be encrypted or decrypted as one single BigInt and copies the data into a BigInt verbatim. However, a BigInt simply consists of a number of 32 bit integers in the native endian format, so the data copied to/from the BigInt needs to be byteswapped on big endian systems to yield the same result as on little endian systems.
  • Loading branch information
isojalka authored and hifi committed Mar 16, 2024
1 parent da253d3 commit e260535
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions common/pk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,21 @@ int PKey::Encrypt(void const* source, int slen, void* dest) const
** Perform the encryption of the block.
*/
BigInt temp = 0;
unsigned int* tempui = temp;
int i;

memmove(&temp, source, Plain_Block_Size());

for (i = 0; i < (Plain_Block_Size() + 3) / 4; i++) {
tempui[i] = htole32(tempui[i]);
}

temp = temp.exp_b_mod_c(Exponent, Modulus);

for (i = 0; i < (Crypt_Block_Size() + 3) / 4; i++) {
tempui[i] = le32toh(tempui[i]);
}

/*
** Move the cypher block to the destination.
*/
Expand Down Expand Up @@ -327,6 +339,8 @@ int PKey::Decrypt(void const* source, int slen, void* dest) const
{
int total = 0;
BigInt temp;
unsigned int* tempui = temp;
int i;

/*
** Decrypt the source data in full blocks. Partial blocks are not processed in any way.
Expand All @@ -338,8 +352,17 @@ int PKey::Decrypt(void const* source, int slen, void* dest) const
*/
temp = 0;
memmove(&temp, source, Crypt_Block_Size());

for (i = 0; i < (Crypt_Block_Size() + 3) / 4; i++) {
tempui[i] = le32toh(tempui[i]);
}

temp = temp.exp_b_mod_c(Exponent, Modulus);

for (i = 0; i < (Plain_Block_Size() + 3) / 4; i++) {
tempui[i] = htole32(tempui[i]);
}

/*
** Move the cypher block to the destination.
*/
Expand Down

0 comments on commit e260535

Please sign in to comment.