Repository navigation
PKCS5_PBKDF1 buffer overflow #874
Description
Activity
Another issue is the following
#include <sha.h> #include <pwdbased.h> int main(void) { const unsigned char password[8] = { 0 }; const unsigned char salt[8] = { 0 }; ::CryptoPP::PKCS5_PBKDF1<::CryptoPP::SHA1> pbkdf1; pbkdf1.DeriveKey( nullptr, 0, 0, password, sizeof(password), salt, sizeof(salt), 4); return 0; }
(It is a nonsensical use of the API, but my fuzzer tries all parameter combinations)
This causes
memcpy(derived, buffer, derivedLen);in
PKCS5_PBKDF1<T>::DeriveKeyto effectively bememcpy(NULL, buffer, 0);
memcpy'ing to NULL even when the
sizeis 0 is technically undefined behavior.Something like this would be better:
if ( derivedLen ) memcpy(derived, buffer, derivedLen);Or just return near the start of the function if
derivedLenis 0.Thanks for the find.
Yeah, something looks odd with
PKCS5_PBKDF1. It looks likePKCS5_PBKDF1,PKCS5_PBKDF2_HMACand friends overrideMaxDerivedKeyLength, but the base class usesMaxDerivedLength. That's a typo from cutting in theKeyDerivationFunctioninterface. Ugh...Here is Crypto++ 5.6.2
pwdbased.h.- added a commit that references this issue
on Aug 16, 2019 Cleared at Commit c0a5a06a8285 (pwdbased) and Commit e22700f741af (scrypt and hkdf).
PKCS5_PBKDF1will throw if the requested size is too large. That is the behavior specified by the standard. (It was supposed to throw in the past, butMaxDerivedKeyLengthwas not properly overridden).If you need to stretch a key, use
PKCS5_PBKDF2_HMAC. Or better, useHKDFclass nowadays.- added a commit that references this issue
on Aug 16, 2019
Results in a buffer overflow in
PKCS5_PBKDF1<T>::DeriveKeybecause:So with SHA1, 20 bytes are allocated, but because
derivedLenis 714, (714-20)=694 too many bytes are copied.