SHAKE: the outlen of EVP_DigestFinalXOF() stays on the context as "xoflen" for the next operation

オープン
#33,859 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 2 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
56/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
c
領域
cryptography

調査の方向性

Start with EVP_DigestFinalXOF() in crypto/evp/digest.c and SHAKE re-initialisation in providers/implementations/digests/sha3_prov.c; read the XOF parameter handling and reproduce the reported cases in the linked gist. Check how the context behaves after re-initialisation when xoflen was implicit or explicitly set, and verify that a new operation no longer inherits the prior call's output length.

索引モデルが issue の本文から書いたものです。

説明

Summary

EVP_DigestFinalXOF(ctx, md, outlen) first stores outlen on the context as the xoflen parameter (crypto/evp/digest.c, "For backward compatibility we pass the XOFLEN via a param here ...") and never restores it. SHAKE's re-initialisation (keccak_init_params() in providers/implementations/digests/sha3_prov.c, used when EVP_DigestInit_ex2(ctx, NULL, NULL) or the same digest re-uses the provider context) does not reset md_size either. So the length argument of one EVP_DigestFinalXOF() call becomes a setting of the next operation:

  • EVP_MD_CTX_get_size_ex() after re-initialisation returns the previous call's outlen (40 in the test below); on a fresh context it returns -1. EVP_DigestInit(3) says that for an XOF it "returns the "xoflen" if it has been set, otherwise it returns 0".
  • EVP_DigestFinal_ex() without any xoflen set fails on a fresh SHAKE context, but succeeds after re-initialisation and writes outlen bytes from the earlier call (with a 200-byte earlier call, 200 bytes are written).
  • If the caller did set xoflen (e.g. 20) and then used EVP_DigestFinalXOF(ctx, md, 40) once, the context reports and uses 40 afterwards, not 20.

EVP_DigestInit(3) allows starting a new operation with EVP_DigestInit_ex2() after EVP_DigestFinalXOF() without EVP_MD_CTX_reset(), and describes outlen as the amount of output for that call only. EVP_MD_CTX_reset() or re-initialising with a different digest avoids the problem, because the provider context is recreated.

Reproducer

Tested on master 354f1cf6 and 4.0.3, SHAKE256 (SHAKE128 behaves the same):

EVP_MD_CTX *ctx = EVP_MD_CTX_new();
unsigned char out[256];
unsigned int len = 0;

EVP_DigestInit_ex2(ctx, EVP_shake256(), NULL);
EVP_DigestUpdate(ctx, "abc", 3);
EVP_DigestFinalXOF(ctx, out, 40);               /* 1 */

EVP_DigestInit_ex2(ctx, NULL, NULL);            /* 1: new operation */
printf("%d\n", EVP_MD_CTX_get_size_ex(ctx));    /* 40; fresh context: -1 */
EVP_DigestUpdate(ctx, "abc", 3);
printf("%d %u\n", EVP_DigestFinal_ex(ctx, out, &len), len);  /* 1 40; fresh context: 0 */

shake_xof_reinit.c at https://gist.github.com/GeekQAQ/e8044659d9ad0822b7a848126d4c6f7a covers the variants above (xoflen set to 20 first; EVP_DigestInit_ex(ctx, NULL, NULL); re-init with the same EVP_MD).

Suggested fix

Restore the context's previous xoflen after the provider's final call in EVP_DigestFinalXOF() (reference patch reference.patch at https://gist.github.com/GeekQAQ/e8044659d9ad0822b7a848126d4c6f7a, for reference only). This removes all the effects above and keeps an xoflen that the caller set explicitly. With the patch make test passes.

A related question: whether an xoflen set explicitly with EVP_MD_CTX_set_params() should survive re-initialisation at all is not written down. Today it survives EVP_DigestInit_ex2(ctx, NULL, NULL) and re-initialisation with the same digest, but not re-initialisation with a different digest. The alternative fix, resetting md_size in keccak_init_params(), would also drop the explicit setting.

主要言語
C
スター
31.5k
フォーク
11.6k
平均マージ
20分
マージ済み PR(30日)
2

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

openssl/openssl のほかの issue

openssl/openssl の issue をすべて見る

似ている issue

C の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。