[...] If the wallet files are encrypted, the attacker must [...] simply wait for you to type it in. At that point, they can gain access to all of your private keys.
That's the point of OTP in addition to typing in a static password. The attacker is not able to push the button of Your Yubikey from inside the computer
or even plug it to the USB slot at first
The password safe "KeePass" has a YubiKey plugin, to exactly preventing such keylogger attacks. So it seems, that it is possible to protect a local software with OTP. What would be the difference between a online service (which is basically an application running on a webserver) and a local application anyway?
It gets pretty complicated from here... (and there are real experts who know far more than me that would make both our heads spin!
)
The wallet that's on your hard drive (or the KeePass file) is encrypted with a something called a "symmetric" key. That just means the the encryption key is the same as the decryption key, and it's
always the same until it's changed, typically manually by you.
With KeyPass, you can use your YubiKey in one of two different modes. In "static password" mode, the YubiKey simply remembers one portion of your password. The encryption/decryption key is created by adding the password you remember to the one the YubiKey remembers, and it's always the same (unless you manually change it). If there's malware on your computer, it can wait for both you and the YubiKey to input your passwords, and then the malware has all it needs to decrypt your KeyPass passwords (or your wallet file).
The second mode is called One-Time-Password mode (which sounds good, but keep reading...). This mode is similar to "static password" mode, but it automatically changes the symmetric encryption key each time you log in. This means that if a piece of malware captures the full password (both the one you type in and the one from the YubiKey which is different each time you log in), and if the malware then tries to use this password, it's probably too late because the symmetric key has already been changed and is no longer valid.
The problem is that there's a simple attack the malware can use to get around this. The malware takes a copy of the encrypted data
before you or the YubiKey enter your passwords, and stores it temporarily. The next time you and the YubiKey enter your passwords, the malware records the password. As I said above, at this point the symmetric key for the encrypted data is now automatically changed... but it's only changed for the "legitimate" file. The temporary file which the malware took a copy of doesn't have its key changed -- its password is still the same, it's the one that was captured by the malware. Now the malware has both the decryption key and the older encrypted file that the decryption key will work on.
With a good* online service, the malware never has access to the encrypted data because it's only stored on the server, and so it can't take a temporary copy of it for later decryption.
The bottom line is that while a YubiKey makes things a little more difficult for malware, it's doesn't really help that much for local wallets. Local wallets are only safe if there's no malware present on the system. Good* online wallets on the other hand
can have their security improved with a YubiKey (or similar).
* A good/strong online service has Bitcoin keys stored in (at least) two places: partially on your computer and partially on the server. At no time are the keys both stored in the same place. Blockchain.info doesn't fit this description, because there is only one key -- if you're malware infested, this one key can be stolen by malware on your computer. Of course, all online services require a certain trust level on your behalf...