bool VolumeHeader::Decrypt (const ConstBufferPtr &encryptedData, const VolumePassword &password, const Pkcs5KdfList &keyDerivationFunctions, const EncryptionAlgorithmList &encryptionAlgorithms, const EncryptionModeList &encryptionModes)
{
    if (password.Size() < 1)
        throw PasswordEmpty (SRC_POS);

    ConstBufferPtr salt (encryptedData.GetRange (SaltOffset, SaltSize));
    SecureBuffer header (EncryptedHeaderDataSize);
    SecureBuffer headerKey (GetLargestSerializedKeySize());

    foreach (shared_ptr <Pkcs5Kdf> pkcs5, keyDerivationFunctions)
    {
        pkcs5->DeriveKey (headerKey, password, salt);

        foreach (shared_ptr <EncryptionMode> mode, encryptionModes)
        {
            if (typeid (*mode) != typeid (EncryptionModeXTS))
                mode->SetKey (headerKey.GetRange (0, mode->GetKeySize()));

            foreach (shared_ptr <EncryptionAlgorithm> ea, encryptionAlgorithms)
            {
                if (!ea->IsModeSupported (mode))
                    continue;

                /*
                printf("trying %ls, %ls, %ls\n", pkcs5->GetName().c_str(),
                							mode->GetName().c_str(),
                							ea->GetName().c_str()
                							);
                							*/


                if (typeid (*mode) == typeid (EncryptionModeXTS))
                {
                    ea->SetKey (headerKey.GetRange (0, ea->GetKeySize()));

                    mode = mode->GetNew();
                    mode->SetKey (headerKey.GetRange (ea->GetKeySize(), ea->GetKeySize()));
                }
                else
                {
                    ea->SetKey (headerKey.GetRange (LegacyEncryptionModeKeyAreaSize, ea->GetKeySize()));
                }

                ea->SetMode (mode);

                header.CopyFrom (encryptedData.GetRange (EncryptedHeaderDataOffset, EncryptedHeaderDataSize));
                ea->Decrypt (header);

                if (Deserialize (header, ea, mode))
                {
                    EA = ea;
                    Pkcs5 = pkcs5;
                    return true;
                }
            }
        }
    }
示例#2
0
	void VolumePassword::Set (const VolumePassword &password)
	{
		Set (password.DataPtr(), password.Size());
	}