# [RFC][VOTE] Improve hash_hkdf() parameter

- Thread: https://externals.io/message/98639
- Original archive: https://news-web.php.net/php.internals/98639
- Started by Yasuo Ohgaki on 2017-03-25T22:29:47+00:00
- Messages: 37
- RFC: Improve hash_hkdf() parameter order and handling (Inactive) — https://externals.io/rfc/improve_hash_hkdf_parameter.md
- Page 1 of 2: messages 1 to 25 (25 per page, add `?page=N` to the URL)
- Next page: https://externals.io/message/98639.md?page=2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #98639 — Yasuo Ohgaki — 2017-03-25T22:29:47+00:00

URL: https://externals.io/message/98639#98639

Hi all,

Since `hash_hkdf()` is in PHP 7.1.2, I restarted vote.
I posted previous announce in discussion thread by mistake.

https://wiki.php.net/rfc/improve_hash_hkdf_parameter

Vote start: 2017-03-26
Vote end: 2017-04-07 UTC 23:59:59

Current `hash_hkdf()` function signature does not make sense.

 - HKDF is KEY derivation function, yet derivation KEY is the last option.
 - `hash_hkdf()` is simple `hash_hmac()` extension, yet it has totally
   different signature.
 - Return value is binary unlike other hash functions.
 - The signature is INSECURE.

Current signature is overly optimized very limited crypto operation
and cannot be optimal by above reasons.

Fortunately, almost all users are not using current `hash_hkdf()`.
It's only in 7.1.2/7.1.3 now. We should avoid yet another new inconsistent
and insecure function. It would be better to be fixed ASAP, IMHO.

I suggest you to disclose the reason why against this change.
Otherwise, you may be considered you don't understand crypto basic.
i.e. HKDF(IKM) security depends on PRK being secure. To make PRK
secure or more secure, "salt" parameter is required. "length" is irrelevant
for security.

Thank you for voting.

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #98640 — Yasuo Ohgaki — 2017-03-26T23:08:39+00:00

URL: https://externals.io/message/98639#98640 · In reply to: #98639

Hi all,



> I suggest you to disclose the reason why against this change.
> Otherwise, you may be considered you don't understand crypto basic.
> i.e. HKDF(IKM) security depends on PRK being secure. To make PRK
> secure or more secure, "salt" parameter is required. "length" is
> irrelevant
> for security.
>

I'll try to explain a bit more by examples.

HKDF is designed to obtain the best possible "cryptographically strong
hash value" (key) for various key derivation operations. Current signature
could lead to insecure/wrong usages. (We have similar experience with
our PHP functions. e.g. uniqid, crypt, etc)


Example #1 : Deriving strong 256 bit AES key from 128 bit AES key.

$new_key = hash_hkdf('sha256', $AES_128bit_key, 32); // Derive 256 bit AES
key from 128 bit key
// No additional entropy, thus $new_key is not strong 256 bit AES key.
// Far from the best possible.

Users must not do this with HKDF. The same $new_key quality can
be obtained by simple SHA-256 hashing which is faster. Without
"strong derivation key", HKDF is not useful at all. The optimal way is

$new_key = hash_hkdf('sha256', $AES_128bit_key, 0, '',
$strong_derivation_key);
// where $strong_derivation_key = random_bytes(32); or like.



Example #2 : Deriving strong key from week key such as user entered password

$new_key = hash_hkdf('sha256', 'p@ssword');
// Almost the same as hash('sha256', 'p@ssword'); All of us should know how
bad this is.
// Far from the best possible.

Users must not do this with HKDF. The same could be done with simple `hash()`.
Users must provide cryptographically strong derivation key, otherwise HKDF
is
useless.

$new_key = hash_hkdf('sha256', 'p@ssword', 0, '', $strong_derivation_key);
// where $strong_derivation_key = random_bytes(32); or like.
// Since input key material is weak, $strong_derivation_key must be secret


Example #3 : Deriving CSRF token from secret seed

$new_key = hash_hkdf('sha256', $secret_seed, 0, $version);
// Almost the same as hash('sha256', $secret_seed . $version);
// Far from the best possible.

Users must not do this with HKDF. The same could be done with simple `hash()`.
Users must provide cryptographically strong derivation key, otherwise HKDF
is
useless.

$new_key = hash_hkdf('sha256', $secret_seed, 0, $version,
$strong_derivation_key);
// where $strong_derivation_key = random_bytes(32); or like.



There are looong lists of this kind of insecure/wrong usage with current
signature.

If you understand how to derive "strong key" by HKDF, you should realize
current `hash_hkdf()` function signature is far from the best.

Detailed rationale is explained the PHP RFC, but it seems many of us does
not
understand this. HKDF is supposed to derive "strong key", why should we
encourage "weak key" derivations with non optimal signature?

Regards,

P.S. I strongly objected the current signature before 7.1 merge. Shouldn't
committer write RFC before commit in the first place? Especially for
released versions.

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #98641 — Stephen Reay — 2017-03-27T04:09:15+00:00

URL: https://externals.io/message/98639#98641 · In reply to: #98640

> 
> I'll try to explain a bit more by examples.
> 

Hi Yasuo,

It sounds to me like it is *possible* to currently use `hash_hkdf()` in a secure manner, but that you (and some others?) feel the arg order and default args are not conducive to safe/secure usage.

Given that the function is live in the wild, massively changing the order of things and defaults is an instant red flag for myself, and I believe a lot of other people.

To me this sounds more like an issue that could be relatively quickly improved by a documentation update that highlights how to securely use the function.

Yes, if there are more secure defaults that would be nice, but that ship has sailed, and the function was on it.


Just my 2 cents.


Cheers

Stephen

---

## #98709 — Yasuo Ohgaki — 2017-04-01T02:27:23+00:00

URL: https://externals.io/message/98639#98709 · In reply to: #98641

Hi Stephen,

On Mon, Mar 27, 2017 at 1:09 PM, Stephen Reay <php-lists@koalephant.com>
wrote:

>
> It sounds to me like it is *possible* to currently use `hash_hkdf()` in a
> secure manner, but that you (and some others?) feel the arg order and
> default args are not conducive to safe/secure usage.
>

It's _possible_, of course. Problem is _new_ function has
 - insecure signature (it ignores strong RFC 5689 recommendation)
 - inconsistent signature and return value (hash() and `hash_hmac()`)
 - no major use(application) for PHP apps (Length has almost no use with
PHP apps)

If users would like to generate arbitrary length hash from existing hash
value with _insecure_ way, they should
use new SHA-3 standards, i.e. SHA-3 already has 2 SHAKE algorithms that
generate arbitrary length hash value,
SHAKE128(M, d) and SHAKE256(M, d).
No reason to encourage less secure HKDF usage to obtain arbitrary length
hash value.

Current `hash_hkdf()` signature does not make much sense with regard to
cryptographically, consistency and
expected usage.

Given that the function is live in the wild, massively changing the order
> of things and defaults is an instant red flag for myself, and I believe a
> lot of other people.
>

Aside from it should not be merged into PHP 7.1 in the first place.
There are only 2 (or 3) bug fix versions released. Fixing mistake ASAP is
better. IMHO.


To me this sounds more like an issue that could be relatively quickly
> improved by a documentation update that highlights how to securely use the
> function.
>

While documentation may work, it seems silly for me to write,

  Even if "salt" is the last optional parameter, users must set appropriate
"salt" whenever it is possible for maximum key security.

for new function.

Yes, if there are more secure defaults that would be nice, but that ship
> has sailed, and the function was on it.


Thank you for your comment.
I would like to try to fix it at least.

To avoid this kind of confusions, we are better to have RFC if there is
strong objection.

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #98710 — Yasuo Ohgaki — 2017-04-01T02:50:53+00:00

URL: https://externals.io/message/98639#98710 · In reply to: #98709

Hi all,

  - insecure signature (it ignores strong RFC 5689 recommendation)
s/RFC 5689/RFC 5869/



>
> Given that the function is live in the wild, massively changing the order
> > […]
>
> Aside from it should not be merged into PHP 7.1 in the first place.
> There are only 2 (or 3) bug fix versions released. Fixing mistake ASAP is
> better. IMHO.
>
>
> To me this sounds more like an issue that could be relatively quickly
> > […]
>
> While documentation may work, it seems silly for me to write,
>
>   Even if "salt" is the last optional parameter, users must set
> appropriate "salt" whenever it is possible for maximum key security.
>

Another possible resolution could be reverting `hash_hkdf()` merge from 7.1
branch.
Basic `hash_hkdf()` operation could be done by `hash_hmac()` easily.

The merge should have had PHP RFC.
Reverting `hash_hkdf()` merge may work better.

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #98775 — Joe Watkins — 2017-04-12T10:46:51+00:00

URL: https://externals.io/message/98639#98775 · In reply to: #98710

Morning,

This RFC was left open for 5 days past the end of voting as declared on the
RFC.

I have closed the vote, and moved it out of voting section on RFC index.

Cheers
Joe



> Hi all,
>
>   - insecure signature (it ignores strong RFC 5689 recommendation)
> s/RFC 5689/RFC 5869/
>
> 
>
> > […]
> a
> > […]
> the
> > […]
>
> Another possible resolution could be reverting `hash_hkdf()` merge from 7.1
> branch.
> Basic `hash_hkdf()` operation could be done by `hash_hmac()` easily.
>
> The merge should have had PHP RFC.
> Reverting `hash_hkdf()` merge may work better.
>
> Regards,
>
> --
> Yasuo Ohgaki
> yohgaki@ohgaki.net

---

## #98778 — Yasuo Ohgaki — 2017-04-12T23:07:19+00:00

URL: https://externals.io/message/98639#98778 · In reply to: #98775

Hi Joe,



> This RFC was left open for 5 days past the end of voting as declared on
> the RFC.
>

Thank you, I forgot about this.
IMHO, it's a shame for us we should have inconsistent and insecure function
signature for a new function.

I'm going to update the manual to add warning notes and example usages
like advanced CRFS token dedicated for specific URL with expiration time.

I can think of length option only usage, but I cannot think usage that could
be useful for majority of PHP users like advanced CSRF token.

Andrey,

Could you give us some length only and length/info only example
that could be useful for most PHP users.
It should be safe and recommended usage.
I suppose you should have some good examples.

Thank you.

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #98781 — Pieter Hordijk — 2017-04-13T08:11:34+00:00

URL: https://externals.io/message/98639#98781 · In reply to: #98778

----- Original Message -----
> From: "Yasuo Ohgaki" <yohgaki@ohgaki.net>
> To: "Joe Watkins" <pthreads@pthreads.org>, "Andrey Andreev" <narf@devilix.net>
> Cc: internals@lists.php.net
> Sent: Thursday, April 13, 2017 1:07:19 AM
> Subject: Re: [PHP-DEV] [RFC][VOTE] Improve `hash_hkdf()` parameter

> Hi Joe,
> 
> 
> 
> > […]
> 
> Thank you, I forgot about this.
> IMHO, it's a shame for us we should have inconsistent and insecure function
> signature for a new function.
> 
> I'm going to update the manual to add warning notes and example usages
> like advanced CRFS token dedicated for specific URL with expiration time.
> 
> I can think of length option only usage, but I cannot think usage that could
> be useful for majority of PHP users like advanced CSRF token.

Is this really something we need in our official docs instead of for example
on a personal blog?

To be honest I am afraid of ending up with something like the current state
of the session docs. Which are imo way too broad / opinionated, non English,
contains utterly confusing examples and / or flat out wrong and broken examples.
Above already resulted in a stream of docs bugs regarding session pages
and a lot of confused readers.

By all means describe how functions work, but don't confuse readers with things
most people won't ever need or are better suited as a (series of) blog posts / 
Stack Overflow post(s).

My €0.02

cc-ing docs discussion to get them also involved in case somebody of the docs
team has an opinion.

> Andrey,
> 
> Could you give us some length only and length/info only example
> that could be useful for most PHP users.
> It should be safe and recommended usage.
> I suppose you should have some good examples.
> 
> Thank you.
> 
> --
> Yasuo Ohgaki
> yohgaki@ohgaki.net

---

## #98782 — Yasuo Ohgaki — 2017-04-13T08:38:21+00:00

URL: https://externals.io/message/98639#98782 · In reply to: #98781

Hi Peiter,

On Thu, Apr 13, 2017 at 5:11 PM, Pieter Hordijk <info@pieterhordijk.com>
wrote:

> To be honest I am afraid of ending up with something like the current state
> of the session docs. Which are imo way too broad / opinionated, non
> English,
> contains utterly confusing examples and / or flat out wrong and broken
> examples.
> Above already resulted in a stream of docs bugs regarding session pages
> and a lot of confused readers.
>

You may consider my opinion as my personal opinion. I don't know of other
than
me who had that opinion then.

After our session discussion, it seems OWASP adopted most of discussed
elements in their doc ;)

https://www.owasp.org/index.php/Session_Management_Cheat_Sheet

Regards,

P.S. My opinion is based on RFC 5869. In addition, it's totally nonsense to
me to have completely different signature for `hash_hkdf()`.
See the difference `hash_hmac()` and `hash_pbkdf2()`. `hash_pbkdf2()` is older
KDF function. I should have mention in the RFC :(

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #98783 — Yasuo Ohgaki — 2017-04-13T09:13:26+00:00

URL: https://externals.io/message/98639#98783 · In reply to: #98782

Hi Pieter,



>
> On Thu, Apr 13, 2017 at 5:11 PM, Pieter Hordijk <info@pieterhordijk.com>
> wrote:
>
> > […]
>
> You may consider my opinion as my personal opinion. I don't know of other
> than
> me who had that opinion then.
>
> After our session discussion, it seems OWASP adopted most of discussed
> elements in their doc ;)
>

I'm not exactly sure which part you consider as personal blog.

Current session management is too loose and insecure in many ways.
Since mandatory features for precise session management are not implemented,
the doc is intermediate.

I'm willing to improve the doc and appreciate improvement suggestions
always.
Feel free to send to my personal mail address.

Required information for precise and secure session management should be
in Precise Session Management RFC
https://wiki.php.net/rfc/precise_session_management

I appreciate if one could add missing documentation for precise session
management.

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #98786 — wout van gils — 2017-04-13T15:13:40+00:00

URL: https://externals.io/message/98639#98786

Kan iemand mij eindelijk eens uitschrijven.?

---

## #98787 — wout van gils — 2017-04-13T15:13:55+00:00

URL: https://externals.io/message/98639#98787

Kan iemand mij eindelijk eens uitschrijven.?


??


________________________________
Van: Pieter Hordijk <info@pieterhordijk.com>
Verzonden: donderdag 13 april 2017 08:11
Aan: Yasuo Ohgaki
CC: Joe Watkins; Andrey Andreev; internals@lists.php.net; phpdoc@lists.php.net
Onderwerp: [PHP-DOC] Re: [PHP-DEV] [RFC][VOTE] Improve `hash_hkdf()` parameter



----- Original Message -----
> From: "Yasuo Ohgaki" <yohgaki@ohgaki.net>
> To: "Joe Watkins" <pthreads@pthreads.org>, "Andrey Andreev" <narf@devilix.net>
> Cc: internals@lists.php.net
> Sent: Thursday, April 13, 2017 1:07:19 AM
> Subject: Re: [PHP-DEV] [RFC][VOTE] Improve `hash_hkdf()` parameter

> Hi Joe,
>
> 
>
> > […]
>
> Thank you, I forgot about this.
> IMHO, it's a shame for us we should have inconsistent and insecure function
> signature for a new function.
>
> I'm going to update the manual to add warning notes and example usages
> like advanced CRFS token dedicated for specific URL with expiration time.
>
> I can think of length option only usage, but I cannot think usage that could
> be useful for majority of PHP users like advanced CSRF token.

Is this really something we need in our official docs instead of for example
on a personal blog?

To be honest I am afraid of ending up with something like the current state
of the session docs. Which are imo way too broad / opinionated, non English,
contains utterly confusing examples and / or flat out wrong and broken examples.
Above already resulted in a stream of docs bugs regarding session pages
and a lot of confused readers.

By all means describe how functions work, but don't confuse readers with things
most people won't ever need or are better suited as a (series of) blog posts /
Stack Overflow post(s).

My €0.02

cc-ing docs discussion to get them also involved in case somebody of the docs
team has an opinion.

> Andrey,
>
> Could you give us some length only and length/info only example
> that could be useful for most PHP users.
> It should be safe and recommended usage.
> I suppose you should have some good examples.
>
> Thank you.
>
> --
> Yasuo Ohgaki
> yohgaki@ohgaki.net

---

## #98791 — Yasuo Ohgaki — 2017-04-13T21:22:26+00:00

URL: https://externals.io/message/98639#98791 · In reply to: #98781

Hi Pieter and all,

On Thu, Apr 13, 2017 at 5:11 PM, Pieter Hordijk <info@pieterhordijk.com>
wrote:

> Is this really something we need in our official docs instead of for
> example
> on a personal blog?
>

I wrote draft doc patch.
Please verify.

Index: en/reference/hash/functions/hash-hkdf.xml
===================================================================
--- en/reference/hash/functions/hash-hkdf.xml    (リビジョン 342317)
+++ en/reference/hash/functions/hash-hkdf.xml    (作業コピー)
@@ -3,7 +3,7 @@
 <refentry xml:id="function.hash-hkdf" xmlns="http://docbook.org/ns/docbook"
xmlns:xlink="http://www.w3.org/1999/xlink">
  <refnamediv>
   <refname>hash_hkdf</refname>
-  <refpurpose>Generate a HKDF key derivation of a supplied key
input</refpurpose>
+  <refpurpose>Derive secure new key from existing key by using
HKDF</refpurpose>
  </refnamediv>
  <refsect1 role="description">
   &reftitle.description;
@@ -16,6 +16,20 @@
    <methodparam
choice="opt"><type>string</type><parameter>salt</parameter><initializer>''</initializer></methodparam>
   </methodsynopsis>

+  <para>
+   RFC 5869 defines HKDF (HMAC based Key Derivation Function) which
+   is general purpose KDF. HKDF could be useful for many PHP
+   applications that require temporary keys, such CSRF token,
+   pre-signed key for URI, password for password protected
+   URI, and so on.
+  </para>
+  <note>
+    <para>
+      When info and length
+      is not required for your program, more efficient
+      <function>hash_hmac</function> could be used instead.
+    </para>
+  </note>
  </refsect1>
  <refsect1 role="parameters">
   &reftitle.parameters;
@@ -25,7 +39,7 @@
      <term><parameter>algo</parameter></term>
      <listitem>
       <para>
-       Name of selected hashing algorithm (i.e. "sha256", "sha512",
"haval160,4", etc..)
+       Name of selected hashing algorithm (i.e. "sha3-256", "sha3-512",
"sha256", "sha512", "haval160,4", etc..)
        See <function>hash_algos</function> for a list of supported
algorithms.
        <note>
         <para>
@@ -39,7 +53,7 @@
      <term><parameter>ikm</parameter></term>
      <listitem>
       <para>
-       Input keying material (raw binary). Cannot be empty.
+       Input keying material. Cannot be empty.
       </para>
      </listitem>
     </varlistentry>
@@ -60,7 +74,8 @@
      <term><parameter>info</parameter></term>
      <listitem>
       <para>
-       Application/context-specific info string.
+       Application/context-specific info string. Info is intended for
+       public information such as user ID, protocol version, etc.
       </para>
      </listitem>
     </varlistentry>
@@ -71,8 +86,32 @@
        Salt to use during derivation.
       </para>
       <para>
-       While optional, adding random salt significantly improves the
strength of HKDF.
+        While optional, adding random salt significantly improves the
+        strength of HKDF. Salt could be either secret or
+        non-secret. It is used as "Pre Shared Key" in many use cases.
+        Strong value is preferred. e.g. Use
<function>random_bytes</function>.
+        Optimal salt size is size of used hash algorithm.
       </para>
+      <warning>
+        <para>
+         Although salt is the last optional parameter, salt is the
+         most important parameter for key security. Omitted salt is
+         indication of inappropriate design in most cases. Users must
+         set appropriate salt value whenever it is possible. Omit salt
+         only when it cannot be used.
+        </para>
+        <para>
+         Strong salt is mandatory and must be kept secret when input
+         key is weak, otherwise input key security will not be kept.
+         Even when input key is strong, providing strong salt is the
+         best practice for the best possible key security.
+        </para>
+        <para>
+         Salt must not be able to be controlled by users. i.e. User
+         must not be able to set salt value and get derived key. User
+         controlled salt allows input key analysis to attackers.
+        </para>
+      </warning>
      </listitem>
     </varlistentry>
    </variablelist>
@@ -101,6 +140,99 @@
   &reftitle.examples;
   <para>
    <example>
+    <title>URI specific CSRF token that supports expiration by
<function>hash_hkdf</function></title>
+    <programlisting role="php">
+<![CDATA[
+<?php
+define('CSRF_TOKEN_EXPIRE', 180); // CSRF token expiration
+define('CSRF_TOKENS', 5); // Last 5 CSRF tokens are valid
+
+/**************************************
+ * Implementation note
+ *
+ * It uses "counter" for CSRF expiration management.
+ * "counter" is very low entropy, but input key is strong and
+ * CSRF_TOKEN_SEED is short term key. It should be OK.
+ *
+ * This CSRF token implementation has pros and cons
+ *
+ * Pros
+ *  - A CSRF token is valid only for specific URI.
+ *  - No database is required for URI specific CSRF tokens.
+ *  - Only CSRF token is required. i.e. No timestamp parameter.
+ *  - When user is active, a CSRF token is valid upto CSRF_TOKEN_EXPIRE *
CSRF_TOKENS sec.
+ *  - Even when user had long idle time, CSRF token is valid.
+ *  - CSRF token will expire eventually.
+ *  - Invalidating all active CSRF tokens could be done by
unset($_SESSION['CSRF_TOKEN_SEED']).
+ *    It is recommended to reset CSRF tokens by login/logout event at
least.
+ *    It may be good idea to invalidate all of older CSRF tokens when idle
time is long.
+ *
+ * Cons
+ *  - There could be no CSRF expiration time.
+ *
+ * Precise CSRF token expiration is easy. Just add timestamp parameter
+ * as "info" and check it.
+ **************************************/
+
+session_start();
+if (empty($_SESSION['CSRF_TOKEN_SEED'])) {
+    $_SESSION['CSRF_TOKEN_SEED'] = random_bytes(32);
+    $_SESSION['CSRF_TOKEN_COUNT'] = 1;
+    $_SESSION['CSRF_TOKEN_EXPIRE'] = `time()`;
+}
+
+
+function csrf_get_token($uri) {
+    // Check expiration
+    if ($_SESSION['CSRF_TOKEN_EXPIRE'] + CSRF_TOKEN_EXPIRE < `time()`) {
+        $_SESSION['CSRF_TOKEN_COUNT']++;
+        $_SESSION['CSRF_TOKEN_EXPIRE'] = `time()`;
+    }
+    // Equivalent(NOT exactly the same) value by using `hash_hmac()`
+    // return hash_hmac('sha3-256', hash_hmac('sha3-256',
$_SESSION['CSRF_TOKEN_SEED'], $_SESSION['CSRF_TOKEN_COUNT']), $uri);
+    return hash_hkdf('sha3-256', $_SESSION['CSRF_TOKEN_SEED'], 0, $uri,
$_SESSION['CSRF_TOKEN_COUNT']);
+}
+
+function csrf_validate_token($csrf_token, $uri) {
+    for($i = 0; $i < CSRF_TOKENS; $i++) {
+        // Equivalent(NOT exactly the same) value by using `hash_hmac()`
+        // $token = hash_hmac('sha3-256', hash_hmac('sha3-256',
$_SESSION['CSRF_TOKEN_SEED'], $_SESSION['CSRF_TOKEN_COUNT'] - $i), $uri);
+        $token = hash_hkdf('sha3-256', $_SESSION['CSRF_TOKEN_SEED'], 0,
$uri, $_SESSION['CSRF_TOKEN_COUNT'] - $i);
+        if (hash_equals($csrf_token, $token)) {
+            return TRUE;
+        }
+    }
+    return FALSE;
+}
+
+
+//// Generating CSRF token ////
+// $uri is target URI that browser POSTs form data
+$uri = 'https://example.com/some_form/';
+$csrf_token = csrf_get_token($uri);
+// embed $csrf_token to your form
+
+//// Validating CSRF token ////
+$csrf_token = $_POST['csrf_token'] ?? '';
+if (!csrf_validate_token($csrf_token, $_SERVER['REQUEST_URI'])) {
+    // Invalid CSRF token
+    throw new Exception('CSRF token validation error');
+}
+// valid request
+?>
+]]>
+    </programlisting>
+    <para>
+     Common CSRF token uses the same token value for a session and all
+     URI. This example CSRF token expires and is specific to a
+     URI. i.e. CSRF token http://example.com/form_A/ is not valid for
+     http://example.com/form_B/ Since token value is computed, no
+     database is required.
+    </para>
+   </example>
+  </para>
+  <para>
+   <example>
     <title><function>hash_hkdf</function> example</title>
     <programlisting role="php">
 <![CDATA[
@@ -124,6 +256,30 @@
     </para>
    </example>
   </para>
+  <para>
+   <example>
+    <title><function>hash_hkdf</function> bad example</title>
+    <para>
+     Users must not simply extend input key material length. HKDF does
+     not add additional entropy automatically.  Therefore, weak key
+     remains weak unless strong salt is supplied. Following is bad
+     example.
+    </para>
+    <programlisting role="php">
+<![CDATA[
+<?php
+$inputKey = get_my_aes128_key(); // AES 128 bit key
+
+// Derive AES 256 key from AES 128 key
+$encryptionKey = hash_hkdf('sha256', $inputKey, 32, 'aes-256-encryption');
+// Users should not do this. $encryptionKey only has 128 bit
+// entropy while it should have 256 bit entropy.
+// To derive strong AES 256 key, strong enough salt is required.
+?>
+]]>
+    </programlisting>
+   </example>
+  </para>
  </refsect1>

  <refsect1 role="seealso">
@@ -130,6 +286,7 @@
   &reftitle.seealso;
   <para>
    <simplelist>
+    <member><function>hash_hmac</function></member>
     <member><function>hash_pbkdf2</function></member>
     <member><link xlink:href="&url.rfc;5869">RFC 5869</link></member>
     <member><link
xlink:href="&url.git.hub;narfbg/hash_hkdf_compat">userland
implementation</link></member>

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #98792 — Yasuo Ohgaki — 2017-04-13T21:49:54+00:00

URL: https://externals.io/message/98639#98792 · In reply to: #98791

Hi all,



>
> On Thu, Apr 13, 2017 at 5:11 PM, Pieter Hordijk <info@pieterhordijk.com>
> wrote:
>
> > […]
>
> I wrote draft doc patch.
> Please verify.
>

I used "very low entropy salt" for this CSRF token because "Input key is
strong, very low
entropy salt is acceptable". To avoid confusions, I revised the doc patch.

Index: en/reference/hash/functions/hash-hkdf.xml
===================================================================
--- en/reference/hash/functions/hash-hkdf.xml    (リビジョン 342317)
+++ en/reference/hash/functions/hash-hkdf.xml    (作業コピー)
@@ -3,7 +3,7 @@
 <refentry xml:id="function.hash-hkdf" xmlns="http://docbook.org/ns/docbook"
xmlns:xlink="http://www.w3.org/1999/xlink">
  <refnamediv>
   <refname>hash_hkdf</refname>
-  <refpurpose>Generate a HKDF key derivation of a supplied key
input</refpurpose>
+  <refpurpose>Derive secure new key from existing key by using
HKDF</refpurpose>
  </refnamediv>
  <refsect1 role="description">
   &reftitle.description;
@@ -16,6 +16,20 @@
    <methodparam choice="opt"><type>string</type><parameter>salt</
parameter><initializer>''</initializer></methodparam>
   </methodsynopsis>

+  <para>
+   RFC 5869 defines HKDF (HMAC based Key Derivation Function) which
+   is general purpose KDF. HKDF could be useful for many PHP
+   applications that require temporary keys, such CSRF token,
+   pre-signed key for URI, password for password protected
+   URI, and so on.
+  </para>
+  <note>
+    <para>
+      When info and length
+      is not required for your program, more efficient
+      <function>hash_hmac</function> could be used instead.
+    </para>
+  </note>
  </refsect1>
  <refsect1 role="parameters">
   &reftitle.parameters;
@@ -25,7 +39,7 @@
      <term><parameter>algo</parameter></term>
      <listitem>
       <para>
-       Name of selected hashing algorithm (i.e. "sha256", "sha512",
"haval160,4", etc..)
+       Name of selected hashing algorithm (i.e. "sha3-256", "sha3-512",
"sha256", "sha512", "haval160,4", etc..)
        See <function>hash_algos</function> for a list of supported
algorithms.
        <note>
         <para>
@@ -39,7 +53,7 @@
      <term><parameter>ikm</parameter></term>
      <listitem>
       <para>
-       Input keying material (raw binary). Cannot be empty.
+       Input keying material. Cannot be empty.
       </para>
      </listitem>
     </varlistentry>
@@ -60,7 +74,8 @@
      <term><parameter>info</parameter></term>
      <listitem>
       <para>
-       Application/context-specific info string.
+       Application/context-specific info string. Info is intended for
+       public information such as user ID, protocol version, etc.
       </para>
      </listitem>
     </varlistentry>
@@ -71,8 +86,34 @@
        Salt to use during derivation.
       </para>
       <para>
-       While optional, adding random salt significantly improves the
strength of HKDF.
+        While optional, adding random salt significantly improves the
+        strength of HKDF. Salt could be either secret or
+        non-secret. It is used as "Pre Shared Key" in many use cases.
+        Strong value is preferred. e.g. Use <function>random_bytes</
function>.
+        Optimal salt size is size of used hash algorithm.
       </para>
+      <warning>
+        <para>
+         Although salt is the last optional parameter, salt is the
+         most important parameter for key security. Omitted salt is
+         indication of inappropriate design in most cases. Users must
+         set appropriate salt value whenever it is possible. Omit salt
+         only when it cannot be used.
+        </para>
+        <para>
+         Strong salt is mandatory and must be kept secret when input
+         key is weak, otherwise input key security will not be kept.
+         When input key is strong, low entropy salt is acceptable.
+         However, providing strong salt is the best practice for the
+         best possible key security. Strong salt is strongly recommended
+         long life input keys.
+        </para>
+        <para>
+         Salt must not be able to be controlled by users. i.e. User
+         must not be able to set salt value and get derived key. User
+         controlled salt allows input key analysis to attackers.
+        </para>
+      </warning>
      </listitem>
     </varlistentry>
    </variablelist>
@@ -101,6 +142,99 @@
   &reftitle.examples;
   <para>
    <example>
+    <title>URI specific CSRF token that supports expiration by
<function>hash_hkdf</function></title>
+    <programlisting role="php">
+<![CDATA[
+<?php
+define('CSRF_TOKEN_EXPIRE', 180); // CSRF token expiration
+define('CSRF_TOKENS', 5); // Last 5 CSRF tokens are valid
+
+/**************************************
+ * Implementation note
+ *
+ * It uses "counter" for CSRF expiration management.
+ * "counter" is very low entropy, but input key is strong and
+ * CSRF_TOKEN_SEED is short term key. It should be OK.
+ *
+ * This CSRF token implementation has pros and cons
+ *
+ * Pros
+ *  - A CSRF token is valid only for specific URI.
+ *  - No database is required for URI specific CSRF tokens.
+ *  - Only CSRF token is required. i.e. No timestamp parameter.
+ *  - When user is active, a CSRF token is valid upto CSRF_TOKEN_EXPIRE *
CSRF_TOKENS sec.
+ *  - Even when user had long idle time, CSRF token is valid.
+ *  - CSRF token will expire eventually.
+ *  - Invalidating all active CSRF tokens could be done by
unset($_SESSION['CSRF_TOKEN_SEED']).
+ *    It is recommended to reset CSRF tokens by login/logout event at
least.
+ *    It may be good idea to invalidate all of older CSRF tokens when idle
time is long.
+ *
+ * Cons
+ *  - There could be no CSRF expiration time.
+ *
+ * Precise CSRF token expiration is easy. Just add timestamp parameter
+ * as "info" and check it.
+ **************************************/
+
+session_start();
+if (empty($_SESSION['CSRF_TOKEN_SEED'])) {
+    $_SESSION['CSRF_TOKEN_SEED'] = random_bytes(32);
+    $_SESSION['CSRF_TOKEN_COUNT'] = 1;
+    $_SESSION['CSRF_TOKEN_EXPIRE'] = `time()`;
+}
+
+
+function csrf_get_token($uri) {
+    // Check expiration
+    if ($_SESSION['CSRF_TOKEN_EXPIRE'] + CSRF_TOKEN_EXPIRE < `time()`) {
+        $_SESSION['CSRF_TOKEN_COUNT']++;
+        $_SESSION['CSRF_TOKEN_EXPIRE'] = `time()`;
+    }
+    // Equivalent(NOT exactly the same) value by using `hash_hmac()`
+    // return hash_hmac('sha3-256', hash_hmac('sha3-256',
$_SESSION['CSRF_TOKEN_SEED'], $_SESSION['CSRF_TOKEN_COUNT']), $uri);
+    return hash_hkdf('sha3-256', $_SESSION['CSRF_TOKEN_SEED'], 0, $uri,
$_SESSION['CSRF_TOKEN_COUNT']);
+}
+
+function csrf_validate_token($csrf_token, $uri) {
+    for($i = 0; $i < CSRF_TOKENS; $i++) {
+        // Equivalent(NOT exactly the same) value by using `hash_hmac()`
+        // $token = hash_hmac('sha3-256', hash_hmac('sha3-256',
$_SESSION['CSRF_TOKEN_SEED'], $_SESSION['CSRF_TOKEN_COUNT'] - $i), $uri);
+        $token = hash_hkdf('sha3-256', $_SESSION['CSRF_TOKEN_SEED'], 0,
$uri, $_SESSION['CSRF_TOKEN_COUNT'] - $i);
+        if (hash_equals($csrf_token, $token)) {
+            return TRUE;
+        }
+    }
+    return FALSE;
+}
+
+
+//// Generating CSRF token ////
+// $uri is target URI that browser POSTs form data
+$uri = 'https://example.com/some_form/';
+$csrf_token = csrf_get_token($uri);
+// embed $csrf_token to your form
+
+//// Validating CSRF token ////
+$csrf_token = $_POST['csrf_token'] ?? '';
+if (!csrf_validate_token($csrf_token, $_SERVER['REQUEST_URI'])) {
+    // Invalid CSRF token
+    throw new Exception('CSRF token validation error');
+}
+// valid request
+?>
+]]>
+    </programlisting>
+    <para>
+     Common CSRF token uses the same token value for a session and all
+     URI. This example CSRF token expires and is specific to a
+     URI. i.e. CSRF token http://example.com/form_A/ is not valid for
+     http://example.com/form_B/ Since token value is computed, no
+     database is required.
+    </para>
+   </example>
+  </para>
+  <para>
+   <example>
     <title><function>hash_hkdf</function> example</title>
     <programlisting role="php">
 <![CDATA[
@@ -124,6 +258,30 @@
     </para>
    </example>
   </para>
+  <para>
+   <example>
+    <title><function>hash_hkdf</function> bad example</title>
+    <para>
+     Users must not simply extend input key material length. HKDF does
+     not add additional entropy automatically.  Therefore, weak key
+     remains weak unless strong salt is supplied. Following is bad
+     example.
+    </para>
+    <programlisting role="php">
+<![CDATA[
+<?php
+$inputKey = get_my_aes128_key(); // AES 128 bit key
+
+// Derive AES 256 key from AES 128 key
+$encryptionKey = hash_hkdf('sha256', $inputKey, 32, 'aes-256-encryption');
+// Users should not do this. $encryptionKey only has 128 bit
+// entropy while it should have 256 bit entropy.
+// To derive strong AES 256 key, strong enough salt is required.
+?>
+]]>
+    </programlisting>
+   </example>
+  </para>
  </refsect1>

  <refsect1 role="seealso">
@@ -130,6 +288,7 @@
   &reftitle.seealso;
   <para>
    <simplelist>
+    <member><function>hash_hmac</function></member>
     <member><function>hash_pbkdf2</function></member>
     <member><link xlink:href="&url.rfc;5869">RFC 5869</link></member>
     <member><link xlink:href="&url.git.hub;narfbg/hash_hkdf_compat">userland
implementation</link></member>

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #98797 — Nikita Popov — 2017-04-14T09:24:53+00:00

URL: https://externals.io/message/98639#98797 · In reply to: #98791

> Hi Pieter and all,
>
> On Thu, Apr 13, 2017 at 5:11 PM, Pieter Hordijk <info@pieterhordijk.com>
> wrote:
>
> > […]
>
> I wrote draft doc patch.
> Please verify.
>
> Index: en/reference/hash/functions/hash-hkdf.xml
> ===================================================================
> --- en/reference/hash/functions/hash-hkdf.xml    (リビジョン 342317)
> +++ en/reference/hash/functions/hash-hkdf.xml    (作業コピー)
> @@ -3,7 +3,7 @@
>  <refentry xml:id="function.hash-hkdf" xmlns="http://docbook.org/ns/
> docbook"
> xmlns:xlink="http://www.w3.org/1999/xlink">
>   <refnamediv>
>    <refname>hash_hkdf</refname>
> -  <refpurpose>Generate a HKDF key derivation of a supplied key
> input</refpurpose>
> +  <refpurpose>Derive secure new key from existing key by using
> HKDF</refpurpose>
>   </refnamediv>
>   <refsect1 role="description">
>    &reftitle.description;
> @@ -16,6 +16,20 @@
>     <methodparam
> choice="opt"><type>string</type><parameter>salt</
> parameter><initializer>''</initializer></methodparam>
>    </methodsynopsis>
>
> +  <para>
> +   RFC 5869 defines HKDF (HMAC based Key Derivation Function) which
> +   is general purpose KDF. HKDF could be useful for many PHP
> +   applications that require temporary keys, such CSRF token,
> +   pre-signed key for URI, password for password protected
> +   URI, and so on.
> +  </para>
> +  <note>
> +    <para>
> +      When info and length
> +      is not required for your program, more efficient
> +      <function>hash_hmac</function> could be used instead.
> +    </para>
> +  </note>
>   </refsect1>
>   <refsect1 role="parameters">
>    &reftitle.parameters;
> @@ -25,7 +39,7 @@
>       <term><parameter>algo</parameter></term>
>       <listitem>
>        <para>
> -       Name of selected hashing algorithm (i.e. "sha256", "sha512",
> "haval160,4", etc..)
> +       Name of selected hashing algorithm (i.e. "sha3-256", "sha3-512",
> "sha256", "sha512", "haval160,4", etc..)
>         See <function>hash_algos</function> for a list of supported
> algorithms.
>         <note>
>          <para>
> @@ -39,7 +53,7 @@
>       <term><parameter>ikm</parameter></term>
>       <listitem>
>        <para>
> -       Input keying material (raw binary). Cannot be empty.
> +       Input keying material. Cannot be empty.
>        </para>
>       </listitem>
>      </varlistentry>
> @@ -60,7 +74,8 @@
>       <term><parameter>info</parameter></term>
>       <listitem>
>        <para>
> -       Application/context-specific info string.
> +       Application/context-specific info string. Info is intended for
> +       public information such as user ID, protocol version, etc.
>        </para>
>       </listitem>
>      </varlistentry>
> @@ -71,8 +86,32 @@
>         Salt to use during derivation.
>        </para>
>        <para>
> -       While optional, adding random salt significantly improves the
> strength of HKDF.
> +        While optional, adding random salt significantly improves the
> +        strength of HKDF. Salt could be either secret or
> +        non-secret. It is used as "Pre Shared Key" in many use cases.
> +        Strong value is preferred. e.g. Use
> <function>random_bytes</function>.
> +        Optimal salt size is size of used hash algorithm.
>        </para>
> +      <warning>
> +        <para>
> +         Although salt is the last optional parameter, salt is the
> +         most important parameter for key security. Omitted salt is
> +         indication of inappropriate design in most cases. Users must
> +         set appropriate salt value whenever it is possible. Omit salt
> +         only when it cannot be used.
> +        </para>
> +        <para>
> +         Strong salt is mandatory and must be kept secret when input
> +         key is weak, otherwise input key security will not be kept.
> +         Even when input key is strong, providing strong salt is the
> +         best practice for the best possible key security.
> +        </para>
> +        <para>
> +         Salt must not be able to be controlled by users. i.e. User
> +         must not be able to set salt value and get derived key. User
> +         controlled salt allows input key analysis to attackers.
> +        </para>
> +      </warning>
>       </listitem>
>      </varlistentry>
>     </variablelist>
> @@ -101,6 +140,99 @@
>    &reftitle.examples;
>    <para>
>     <example>
> +    <title>URI specific CSRF token that supports expiration by
> <function>hash_hkdf</function></title>
> +    <programlisting role="php">
> +<![CDATA[
> +<?php
> +define('CSRF_TOKEN_EXPIRE', 180); // CSRF token expiration
> +define('CSRF_TOKENS', 5); // Last 5 CSRF tokens are valid
> +
> +/**************************************
> + * Implementation note
> + *
> + * It uses "counter" for CSRF expiration management.
> + * "counter" is very low entropy, but input key is strong and
> + * CSRF_TOKEN_SEED is short term key. It should be OK.
> + *
> + * This CSRF token implementation has pros and cons
> + *
> + * Pros
> + *  - A CSRF token is valid only for specific URI.
> + *  - No database is required for URI specific CSRF tokens.
> + *  - Only CSRF token is required. i.e. No timestamp parameter.
> + *  - When user is active, a CSRF token is valid upto CSRF_TOKEN_EXPIRE *
> CSRF_TOKENS sec.
> + *  - Even when user had long idle time, CSRF token is valid.
> + *  - CSRF token will expire eventually.
> + *  - Invalidating all active CSRF tokens could be done by
> unset($_SESSION['CSRF_TOKEN_SEED']).
> + *    It is recommended to reset CSRF tokens by login/logout event at
> least.
> + *    It may be good idea to invalidate all of older CSRF tokens when idle
> time is long.
> + *
> + * Cons
> + *  - There could be no CSRF expiration time.
> + *
> + * Precise CSRF token expiration is easy. Just add timestamp parameter
> + * as "info" and check it.
> + **************************************/
> +
> +session_start();
> +if (empty($_SESSION['CSRF_TOKEN_SEED'])) {
> +    $_SESSION['CSRF_TOKEN_SEED'] = random_bytes(32);
> +    $_SESSION['CSRF_TOKEN_COUNT'] = 1;
> +    $_SESSION['CSRF_TOKEN_EXPIRE'] = `time()`;
> +}
> +
> +
> +function csrf_get_token($uri) {
> +    // Check expiration
> +    if ($_SESSION['CSRF_TOKEN_EXPIRE'] + CSRF_TOKEN_EXPIRE < `time()`) {
> +        $_SESSION['CSRF_TOKEN_COUNT']++;
> +        $_SESSION['CSRF_TOKEN_EXPIRE'] = `time()`;
> +    }
> +    // Equivalent(NOT exactly the same) value by using `hash_hmac()`
> +    // return hash_hmac('sha3-256', hash_hmac('sha3-256',
> $_SESSION['CSRF_TOKEN_SEED'], $_SESSION['CSRF_TOKEN_COUNT']), $uri);
> +    return hash_hkdf('sha3-256', $_SESSION['CSRF_TOKEN_SEED'], 0, $uri,
> $_SESSION['CSRF_TOKEN_COUNT']);
> +}
> +
> +function csrf_validate_token($csrf_token, $uri) {
> +    for($i = 0; $i < CSRF_TOKENS; $i++) {
> +        // Equivalent(NOT exactly the same) value by using `hash_hmac()`
> +        // $token = hash_hmac('sha3-256', hash_hmac('sha3-256',
> $_SESSION['CSRF_TOKEN_SEED'], $_SESSION['CSRF_TOKEN_COUNT'] - $i), $uri);
> +        $token = hash_hkdf('sha3-256', $_SESSION['CSRF_TOKEN_SEED'], 0,
> $uri, $_SESSION['CSRF_TOKEN_COUNT'] - $i);
> +        if (hash_equals($csrf_token, $token)) {
> +            return TRUE;
> +        }
> +    }
> +    return FALSE;
> +}
> +
> +
> +//// Generating CSRF token ////
> +// $uri is target URI that browser POSTs form data
> +$uri = 'https://example.com/some_form/';
> +$csrf_token = csrf_get_token($uri);
> +// embed $csrf_token to your form
> +
> +//// Validating CSRF token ////
> +$csrf_token = $_POST['csrf_token'] ?? '';
> +if (!csrf_validate_token($csrf_token, $_SERVER['REQUEST_URI'])) {
> +    // Invalid CSRF token
> +    throw new Exception('CSRF token validation error');
> +}
> +// valid request
> +?>
> +]]>
> +    </programlisting>
> +    <para>
> +     Common CSRF token uses the same token value for a session and all
> +     URI. This example CSRF token expires and is specific to a
> +     URI. i.e. CSRF token http://example.com/form_A/ is not valid for
> +     http://example.com/form_B/ Since token value is computed, no
> +     database is required.
> +    </para>
> +   </example>
> +  </para>
> +  <para>
> +   <example>
>      <title><function>hash_hkdf</function> example</title>
>      <programlisting role="php">
>  <![CDATA[
> @@ -124,6 +256,30 @@
>      </para>
>     </example>
>    </para>
> +  <para>
> +   <example>
> +    <title><function>hash_hkdf</function> bad example</title>
> +    <para>
> +     Users must not simply extend input key material length. HKDF does
> +     not add additional entropy automatically.  Therefore, weak key
> +     remains weak unless strong salt is supplied. Following is bad
> +     example.
> +    </para>
> +    <programlisting role="php">
> +<![CDATA[
> +<?php
> +$inputKey = get_my_aes128_key(); // AES 128 bit key
> +
> +// Derive AES 256 key from AES 128 key
> +$encryptionKey = hash_hkdf('sha256', $inputKey, 32, 'aes-256-encryption');
> +// Users should not do this. $encryptionKey only has 128 bit
> +// entropy while it should have 256 bit entropy.
> +// To derive strong AES 256 key, strong enough salt is required.
> +?>
> +]]>
> +    </programlisting>
> +   </example>
> +  </para>
>   </refsect1>
>
>   <refsect1 role="seealso">
> @@ -130,6 +286,7 @@
>    &reftitle.seealso;
>    <para>
>     <simplelist>
> +    <member><function>hash_hmac</function></member>
>      <member><function>hash_pbkdf2</function></member>
>      <member><link xlink:href="&url.rfc;5869">RFC 5869</link></member>
>      <member><link
> xlink:href="&url.git.hub;narfbg/hash_hkdf_compat">userland
> implementation</link></member>
>

Strong -1 on these docs changes. They are wrong and they will confuse users
about when and how HKDF should be used.

I have no idea where you got the idea that HKDF is supposed to be used for
CSRF token generation, but it isn't. I did not check whether your code is
correct and secure, but CSRF token generation is certainly not a common or
typical application of HKDF and as such should not be present in the
documentation.

Your "bad example" is actually pretty much the textbook use-case for HKDF.
The way you wrote it (get a AES-256 key from an AES-128 key) doesn't make
much sense, but the general principle of extracting two keys (for
encryption and authentication) from a single key is one of *the* use-cases
of HKDF. It is also, contrary to your statement in the documentation
snippet, perfectly cryptographically sound. A salt is not required for this
case. A salt *may* be beneficial, but for entirely different reasons (as
Scott pointed out, for many block cipher modes fixed encryption keys only
have a lifetime of around 2^64 encryptions, past which point IV collisions
are to be expected -- a salt in key derivation could mitigate this.)

Nikita

---

## #98798 — Pieter Hordijk — 2017-04-14T09:45:48+00:00

URL: https://externals.io/message/98639#98798 · In reply to: #98797

----- Original Message -----
> From: "Nikita Popov" <nikita.ppv@gmail.com>
> To: "Yasuo Ohgaki" <yohgaki@ohgaki.net>
> Cc: "Pieter Hordijk" <info@pieterhordijk.com>, "Joe Watkins" <pthreads@pthreads.org>, "Andrey Andreev"
> <narf@devilix.net>, "internals" <internals@lists.php.net>, "phpdoc" <phpdoc@lists.php.net>
> Sent: Friday, April 14, 2017 11:24:53 AM
> Subject: Re: [PHP-DEV] [RFC][VOTE] Improve `hash_hkdf()` parameter

> On Thu, Apr 13, 2017 at 11:22 PM, Yasuo Ohgaki < [ mailto:yohgaki@ohgaki.net |
> yohgaki@ohgaki.net ] > wrote:

> > […]

> > […]

> > […]


> > […]

> > […]

> > […]

> > […]

As I already said yesterday and which you chose to ignore. The manual should
describe the functions and the paramaters and that's it. Maybe add a
warning / note where it is warranted, or even link to some external resource
(e.g. owasp) when it really is needed.

However when reading the above changes I think: great blog post or nice
OSS library shared on github for people to use. Imo all these examples
and opinionated talk about things being mandatory have no place in our manual.

Look at the page of crypt (http://php.net/manual/en/function.crypt.php). There
is a short text in the intro about password hashing with password_hash and a
warning.

Look at the page of openssl_encrypt (http://php.net/manual/en/function.openssl-encrypt.php).
Nowhere on that page is being said it is mandatory to not use ECB mode.

So as far as I'm concerned I personally don't agree with the above changes, because
it's trying to use the manual for something it's not meant for (describing functions
and their parameters).

So please just keep it at that and write a blog post / open source library
for everything else.

I have the feeling you keep adding your own personal preferences to the manual.

Also note I am not even talking about the actual technical implications as Nikita
done below.

So a big -1 from me too.

> Strong -1 on these docs changes. They are wrong and they will confuse users
> about when and how HKDF should be used.

> I have no idea where you got the idea that HKDF is supposed to be used for CSRF
> token generation, but it isn't. I did not check whether your code is correct
> and secure, but CSRF token generation is certainly not a common or typical
> application of HKDF and as such should not be present in the documentation.

> Your "bad example" is actually pretty much the textbook use-case for HKDF. The
> way you wrote it (get a AES-256 key from an AES-128 key) doesn't make much
> sense, but the general principle of extracting two keys (for encryption and
> authentication) from a single key is one of *the* use-cases of HKDF. It is
> also, contrary to your statement in the documentation snippet, perfectly
> cryptographically sound. A salt is not required for this case. A salt *may* be
> beneficial, but for entirely different reasons (as Scott pointed out, for many
> block cipher modes fixed encryption keys only have a lifetime of around 2^64
> encryptions, past which point IV collisions are to be expected -- a salt in key
> derivation could mitigate this.)

---

## #98800 — Philip Olson — 2017-04-14T17:47:48+00:00

URL: https://externals.io/message/98639#98800 · In reply to: #98798

Hello,

The PHP documentation has a separate place for detailed examples. 
For example:

  http://php.net/manual/en/book.inclued.php
  http://php.net/manual/en/inclued.examples-implementation.php

The same could be done for ext/hash which today lacks an Examples 
section:

  http://php.net/manual/en/book.hash.php

This way the function's docs (hash_hkdf() in this case) remain as 
reference material while detailed examples live elsewhere.

Regards,
Philip

---

## #98804 — Yasuo Ohgaki — 2017-04-14T23:51:27+00:00

URL: https://externals.io/message/98639#98804 · In reply to: #98797

Hi Nikita,



> Strong -1 on these docs changes. They are wrong and they will confuse
> users about when and how HKDF should be used.
>
> I have no idea where you got the idea that HKDF is supposed to be used for
> CSRF token generation, but it isn't. I did not check whether your code is
> correct and secure, but CSRF token generation is certainly not a common or
> typical application of HKDF and as such should not be present in the
> documentation.
>
> Your "bad example" is actually pretty much the textbook use-case for HKDF.
> The way you wrote it (get a AES-256 key from an AES-128 key) doesn't make
> much sense, but the general principle of extracting two keys (for
> encryption and authentication) from a single key is one of *the* use-cases
> of HKDF. It is also, contrary to your statement in the documentation
> snippet, perfectly cryptographically sound. A salt is not required for this
> case. A salt *may* be beneficial, but for entirely different reasons (as
> Scott pointed out, for many block cipher modes fixed encryption keys only
> have a lifetime of around 2^64 encryptions, past which point IV collisions
> are to be expected -- a salt in key derivation could mitigate this.)
>

It seems you consider HKDF as very specific function for very specific
crypt task
which is wrong by the RFC 5869 intention.
The RFC 5869 explicitly mentions as

  4.  Applications of HKDF
   HKDF is intended for use in a wide variety of KDF applications.

Why we must limit HKDF usage for certain crypt tasks even if it is
designed for _general_ Key Derivation tasks?

Key derivations in authentication is very common task.
CSRF token is "Key that validates the _authentic_ request".
It is obvious that expiration enabled URI specific CSRF token is
a lot secure than common static CSRF tokens that are valid for all requests.
How this could be bad example?

128 bit entropy key for AES 256 is simply bad practice like
$aes256key = hash('sha256', 'mypassword', true);

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #98805 — Yasuo Ohgaki — 2017-04-15T00:17:32+00:00

URL: https://externals.io/message/98639#98805 · In reply to: #98798

Hi Pieter,

On Fri, Apr 14, 2017 at 6:45 PM, Pieter Hordijk <info@pieterhordijk.com>
wrote:

>
> I have the feeling you keep adding your own personal preferences to the
> manual.


No, not at all.
My opinions are either based on concrete logic or
recommendations based reliable sources.

I improved `hash_hkdf()` manual farther more based on RFC 5869 descriptions.
https://gist.github.com/anonymous/ace4fa267f20041676f265fe58c3f1ea

Please verify it again.

Regards,

P.S. I'm a human, so I make mistakes. I appreciate if one could
point out when my logic is wrong.

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #98849 — Yasuo Ohgaki — 2017-04-22T19:14:13+00:00

URL: https://externals.io/message/98639#98849 · In reply to: #98805

Hi all,



> My opinions are either based on concrete logic or
> recommendations based reliable sources.
>
> I improved `hash_hkdf()` manual farther more based on RFC 5869 descriptions.
> https://gist.github.com/anonymous/ace4fa267f20041676f265fe58c3f1ea
>
> Please verify it again.
>

I would like to finish documentation.

RFC 5869 clearly states HKDF is a generic key derivation function.

Omitting salt when key does not have enough entropy is obvious
bad practice or mistake. Even when key has enough entropy, long
life key (IKM) requires good salt for the best key security. These
could be understood from the RFC and other basic crypt theory.

I'll commit the doc in a few days if there is no more comments on this.

Andrey, (Or anyone who vote no for the PHP RFC)

Could you show some good example `hash_hkdf()` usages that justify
current function signature? I suppose you should have few common and
good examples.

I cannot think of any common/good example that uses length only or
length/info only. No good example is shown so far.

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #98850 — Niklas Keller — 2017-04-22T19:32:01+00:00

URL: https://externals.io/message/98639#98850 · In reply to: #98849

2017-04-22 21:14 GMT+02:00 Yasuo Ohgaki <yohgaki@ohgaki.net>:

> Hi all,
>
> 
>
> > […]
> descriptions.
> > […]
>
> I would like to finish documentation.
>
> RFC 5869 clearly states HKDF is a generic key derivation function.
>
> Omitting salt when key does not have enough entropy is obvious
> bad practice or mistake. Even when key has enough entropy, long
> life key (IKM) requires good salt for the best key security. These
> could be understood from the RFC and other basic crypt theory.
>
> I'll commit the doc in a few days if there is no more comments on this.
>

What the... there were multiple concerns regarding the changes already. I'm
hereby expressing another strong -1 on these.

Regards, Niklas


> Andrey, (Or anyone who vote no for the PHP RFC)
>
> Could you show some good example `hash_hkdf()` usages that justify
> current function signature? I suppose you should have few common and
> good examples.
>
> I cannot think of any common/good example that uses length only or
> length/info only. No good example is shown so far.
>
> Regards,
>
> --
> Yasuo Ohgaki
> yohgaki@ohgaki.net

---

## #98851 — Yasuo Ohgaki — 2017-04-22T19:37:44+00:00

URL: https://externals.io/message/98639#98851 · In reply to: #98850

Hi Niklas,



>
> What the... there were multiple concerns regarding the changes already.
> I'm hereby expressing another strong -1 on these.
>

Instead of posting your feeling, please post logic behind your idea.
Most of the changes are based on what is _written_ in the RFC 5869

I'm a bit tired with arguments without valid logic.

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #98854 — Andrey Andreev — 2017-04-22T22:24:22+00:00

URL: https://externals.io/message/98639#98854 · In reply to: #98851

Hi,


> Hi Niklas,
>
> 
> > […]
>
>
> Instead of posting your feeling, please post logic behind your idea.
> Most of the changes are based on what is _written_ in the RFC 5869
>
> I'm a bit tired with arguments without valid logic.

You're tired? Yasuo, the reason why you're not receiving replies
unless you say "I'll commit in a few days if there are no more
comments" is because everybody is tired of talking to you.

If you want examples, search GitHub for PHP code utilizing HKDF - you
will see that most projects use it without a salt, including
https://github.com/defuse/php-encryption - pretty much the best PHP
userspace crypto library today. And I'm only saying "most" because I
can't be bothered to go through literally all of them; I've found NONE
that do use the salt.
You will also find zero projects using it for CSRF protection.

The vote ended with 1 Yes (you) and 14 No; not a single person has
agreed with you so far, and most have explicitly stated strong
disagreement with your proposed changes. Yet you insist on pushing
your *personal opinion*, ignoring everybody else and acting as if ~80
mails haven't already been exchanged.

How is it even possible that you still believe that everybody is wrong
and you alone are right? Give it up already.

Cheers,
Andrey.

---

## #98869 — Yasuo Ohgaki — 2017-04-25T00:28:38+00:00

URL: https://externals.io/message/98639#98869 · In reply to: #98854

Hi Andrey,



>
> You're tired? Yasuo, the reason why you're not receiving replies
> unless you say "I'll commit in a few days if there are no more
> comments" is because everybody is tired of talking to you.
>
> If you want examples, search GitHub for PHP code utilizing HKDF - you
> will see that most projects use it without a salt, including
> https://github.com/defuse/php-encryption - pretty much the best PHP
> userspace crypto library today. And I'm only saying "most" because I
> can't be bothered to go through literally all of them; I've found NONE
> that do use the salt.
>

Wrong.
I don't think the author wouldn't make such mistake, so I checked.

    /**
     * Derives authentication and encryption keys from the secret, using a
slow
     * key derivation function if the secret is a password.
     *
     * @param string $salt
     *
     * @throws Ex\EnvironmentIsBrokenException
     *
     * @return DerivedKeys
     */
    public function deriveKeys($salt)
    {
        if (Core::ourStrlen($salt) !== Core::SALT_BYTE_SIZE) {
            throw new Ex\EnvironmentIsBrokenException('Bad salt.');
        }

        if ($this->secret_type === self::SECRET_TYPE_KEY) {
            $akey = Core::HKDF(
                Core::HASH_FUNCTION_NAME,
                $this->secret->getRawBytes(),
                Core::KEY_BYTE_SIZE,
                Core::AUTHENTICATION_INFO_STRING,
                $salt
            );
            $ekey = Core::HKDF(
                Core::HASH_FUNCTION_NAME,
                $this->secret->getRawBytes(),
                Core::KEY_BYTE_SIZE,
                Core::ENCRYPTION_INFO_STRING,
                $salt
            );
            return new DerivedKeys($akey, $ekey);
        } elseif ($this->secret_type === self::SECRET_TYPE_PASSWORD) {





> You will also find zero projects using it for CSRF protection.
>

You obviously does not understand HKDF RFC at all. (And don't read my reply)
It seems you consider HKDF as a specific KDF, but it is _not_.

HKDF is designed as general purpose KDF. It is clearly stated in RFC 5869

4 <https://tools.ietf.org/html/rfc5869#section-4>.  Applications of HKDF

   HKDF is intended for use in a wide variety of KDF applications.


Just because you cannot think of how general purpose KDF could be used
for other purposes, it does not mean it should not be used other purposes.
Especially when it is designed for general purpose in the first place.


The vote ended with 1 Yes (you) and 14 No; not a single person has
> agreed with you so far, and most have explicitly stated strong
> disagreement with your proposed changes. Yet you insist on pushing
> your *personal opinion*, ignoring everybody else and acting as if ~80
> mails haven't already been exchanged.
>

> How is it even possible that you still believe that everybody is wrong
> and you alone are right? Give it up already.


Prove my idea in the manual (or my RFC) is wrong by logic, rather than FUD.

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #98871 — Yasuo Ohgaki — 2017-04-25T00:47:09+00:00

URL: https://externals.io/message/98639#98871 · In reply to: #98869

> I don't think the author wouldn't make such mistake, so I checked.
>

Oops. Double denial.
I thought the author wouldn't make such mistake, so I checked.

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

Next page: https://externals.io/message/98639.md?page=2
