コンテンツへスキップ
uencoder

ChaCha20 暗号を使う方法

ストリーム暗号 ChaCha20 でデータを暗号化します。可能な限り Poly1305 による認証を組み合わせます。

问题

モバイルや IoT デバイスなど、ハードウェア AES アクセラレーションが使えない環境で、高速かつ安全なストリーム暗号が必要です。

前提条件

  • ストリーム暗号の理解
  • 認証付き暗号に関する知識

ステップ

  1. 1. Generate a key and nonce

    ChaCha20 uses a 256-bit key and a 96-bit nonce (when used with Poly1305). Generate both using a CSPRNG. The nonce must be unique per key, just like in GCM.

    入力
    256-bit key + 96-bit nonce
    输出
    32 + 12 random bytes
  2. 2. Encrypt with ChaCha20-Poly1305

    Use the authenticated ChaCha20-Poly1305 construction rather than raw ChaCha20. Poly1305 provides integrity, preventing ciphertext tampering. The cipher produces ciphertext and a 128-bit authentication tag.

    入力
    plaintext + key + nonce
    输出
    ciphertext + tag
  3. 3. Verify the tag on decryption

    On decryption, verify the Poly1305 tag before using the plaintext. If the tag does not match, reject the data. This prevents using tampered or corrupted ciphertext.

    入力
    ciphertext + tag + key
    输出
    plaintext (if tag valid)

期待される結果

AES ハードウェアアクセラレーションのない環境に適した、高速な認証付き暗号。

よくあるエラー

Using raw ChaCha20 without Poly1305
Raw ChaCha20 provides no integrity. Always use ChaCha20-Poly1305 for authenticated encryption.
Reusing the nonce
Like GCM, nonce reuse in ChaCha20-Poly1305 is catastrophic. Generate a unique nonce per encryption.

トラブルシューティング指引

  • If performance is the concern, ChaCha20-Poly1305 is often faster than AES-GCM on devices without AES-NI.
  • ChaCha20-Poly1305 is used in TLS 1.3 and WireGuard, proving its real-world adoption.

セキュリティ上の制限

  • ChaCha20-Poly1305 is a modern, well-analyzed cipher. Its main risk is nonce reuse, which must be prevented.
  • All operations are local.

関連ツール

参考链接