Skip to content

Commit bea84e2

Browse files
committed
Check if output buffer is too small.
It really seems like libsodium (whose entire purpose is to make crypto idiot-proof) making me mess with these details is a flaw in the API design. Also, correct Hungarian.
1 parent d885e72 commit bea84e2

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

src/common/crypto_libsodium.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,25 @@ bool AES_GCM_EncryptContext::Encrypt(
5050
void *pEncryptedDataAndTag, uint32 *pcbEncryptedDataAndTag,
5151
const void *pAdditionalAuthenticationData, size_t cbAuthenticationData
5252
) {
53-
unsigned long long pcbEncryptedDataAndTag_longlong = *pcbEncryptedDataAndTag;
5453

54+
// Make sure caller's buffer is big enough to hold the result.
55+
if ( cbPlaintextData + crypto_aead_aes256gcm_ABYTES > *pcbEncryptedDataAndTag )
56+
{
57+
*pcbEncryptedDataAndTag = 0;
58+
return false;
59+
}
60+
61+
unsigned long long cbEncryptedDataAndTag_longlong;
5562
crypto_aead_aes256gcm_encrypt_afternm(
56-
static_cast<unsigned char*>( pEncryptedDataAndTag ), &pcbEncryptedDataAndTag_longlong,
63+
static_cast<unsigned char*>( pEncryptedDataAndTag ), &cbEncryptedDataAndTag_longlong,
5764
static_cast<const unsigned char*>( pPlaintextData ), cbPlaintextData,
5865
static_cast<const unsigned char*>(pAdditionalAuthenticationData), cbAuthenticationData,
5966
nullptr,
6067
static_cast<const unsigned char*>( pIV ),
6168
static_cast<const crypto_aead_aes256gcm_state*>( m_ctx )
6269
);
6370

64-
*pcbEncryptedDataAndTag = pcbEncryptedDataAndTag_longlong;
71+
*pcbEncryptedDataAndTag = cbEncryptedDataAndTag_longlong;
6572

6673
return true;
6774
}
@@ -72,17 +79,23 @@ bool AES_GCM_DecryptContext::Decrypt(
7279
void *pPlaintextData, uint32 *pcbPlaintextData,
7380
const void *pAdditionalAuthenticationData, size_t cbAuthenticationData
7481
) {
75-
unsigned long long pcbPlaintextData_longlong;
76-
82+
// Make sure caller's buffer is big enough to hold the result
83+
if ( cbEncryptedDataAndTag > *pcbPlaintextData + crypto_aead_aes256gcm_ABYTES )
84+
{
85+
*pcbPlaintextData = 0;
86+
return false;
87+
}
88+
89+
unsigned long long cbPlaintextData_longlong;
7790
const int nDecryptResult = crypto_aead_aes256gcm_decrypt_afternm(
78-
static_cast<unsigned char*>( pPlaintextData ), &pcbPlaintextData_longlong,
91+
static_cast<unsigned char*>( pPlaintextData ), &cbPlaintextData_longlong,
7992
nullptr,
8093
static_cast<const unsigned char*>( pEncryptedDataAndTag ), cbEncryptedDataAndTag,
8194
static_cast<const unsigned char*>( pAdditionalAuthenticationData ), cbAuthenticationData,
8295
static_cast<const unsigned char*>( pIV ), static_cast<const crypto_aead_aes256gcm_state*>( m_ctx )
8396
);
8497

85-
*pcbPlaintextData = pcbPlaintextData_longlong;
98+
*pcbPlaintextData = cbPlaintextData_longlong;
8699

87100
return nDecryptResult == 0;
88101
}

0 commit comments

Comments
 (0)