yiisoft/yii2 — pull request #4163 by tom-- (2014-07-01 18:16:26+00:00)
Title: 4131 key longer than hash

See discussions at https://github.com/yiisoft/yii2/pull/4089 and https://github.com/yiisoft/yii2/issues/4131

1. Fixes the PHP implementation of PBKDF2 so it does not truncate output key when requested key length is greater than the hash function length.

1. Reverted to automatic fallback to PHP implementation of PBKDF2 in the case that the PHP built-in `hash_pbkdf2()` is not available. (The PHP implementation is not a lot slower than the built-in and is therefore acceptable.) The hmac/pbkdf2 key derivation strategy config is no longer needed.

1. Add HKDF. This is useful when the user has a secure cryptographic key rather than a password to use for encryption. In this case there is no need to iterate. HKDF is much faster than PBKDF2.

1. Changed `Security::$deriveKeyStrategy` to select between `'password'` strategy (which uses PBKDF2) and `'key'` strategy (HKDF).

1. Changed the signature of `encrypt()` and `decrypt()` in two ways:

- Changed name of second param from `$password` to `$secret` so it can encompass both passwords and secure keys.
- Added optional third parameter `$info` which applies to HKDF, i.e. when `Security::$deriveKeyStrategy` = `'key'`. See https://tools.ietf.org/html/rfc5869 Section 3.2. Example: an application has a key for storing user data in a database and sets `$info` to the User ID so the encryption key for each user is different.

1. Updated the encryption unit tests according to the above changes.

1. Added unit tests for `deriveKeyHkdf()` and `deriveKeyPbkdf2()` using test vectors from the corresponding RFCs.