弱い暗号アルゴリズムや危険な暗号方式の使用

弱い暗号アルゴリズムと安全でない暗号モード

説明

DES は鍵が短く、DES と 3DES の 64 ビットブロックではデータ量の増加とともに衝突のリスクが高まります。RC2、RC4、IDEA などの旧式暗号も置き換えてください。ECB は平文ブロックの繰り返しを隠せず、MD5 と SHA-1 は衝突に弱いハッシュです。

想定される影響

暗号化したデータの漏えい、署名の偽造、高速なハッシュで保存したパスワードの推測につながるおそれがあります。適用対象の NIST や PCI DSS の暗号要件を満たせない場合もあります。

対処方法

  • 暗号化には AES-128 以上と GCM または CCM を使います。CBC が必要なら、別の鍵で IV と暗号文をまとめて認証し、復号前にタグを検証してください。IV、nonce、ソルトは SecureRandom で生成し、同じ鍵で IV や nonce を再利用しないでください。
  • 衝突耐性が必要なハッシュには SHA-256 以上を使います。SHA1withRSA や MD5withRSA の署名は、2048 ビット以上の鍵を使う SHA256withRSA または P-256 以上の曲線を使う ECDSA に置き換えてください。
  • パスワードは個別のソルトと十分な処理コストを設定した Argon2id、scrypt、bcrypt、PBKDF2 などで保存します。PBKDF2-HMAC-SHA256 は 600,000 回以上とし、サーバーで処理時間を確認してください。
  • コードと設定で許可するアルゴリズムと鍵長を制限します。鍵は安全に保管・更新し、必要に応じて KMS や HSM を使ってください。暗号ライブラリやプロバイダーも更新します。

例

変更前

java
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import java.security.MessageDigest;
import java.security.PrivateKey;
import java.security.Signature;

public class LegacyCrypto {
    // Insecure: ECB exposes plaintext patterns
    public byte[] encryptEcb(byte[] plaintext, SecretKey key) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return cipher.doFinal(plaintext);
    }

    // Insecure: MD5 is collision-prone and fast to brute-force
    public byte[] weakDigest(byte[] data) throws Exception {
        MessageDigest md = MessageDigest.getInstance("MD5");
        return md.digest(data);
    }

    // Insecure: SHA1-based signature is deprecated
    public byte[] signSha1(byte[] data, PrivateKey privKey) throws Exception {
        Signature sig = Signature.getInstance("SHA1withRSA");
        sig.initSign(privKey);
        sig.update(data);
        return sig.sign();
    }
}

変更後

java
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import java.nio.ByteBuffer;
import java.security.PrivateKey;
import java.security.SecureRandom;
import java.security.Signature;
import java.util.Base64;

public class ModernCrypto {
    // Secure: AES-GCM (AEAD) with random 96-bit nonce, 128-bit tag
    public byte[] encryptGcm(byte[] plaintext, SecretKey key) throws Exception {
        byte[] nonce = new byte[12];
        SecureRandom.getInstanceStrong().nextBytes(nonce);
        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
        GCMParameterSpec spec = new GCMParameterSpec(128, nonce);
        cipher.init(Cipher.ENCRYPT_MODE, key, spec);
        byte[] ct = cipher.doFinal(plaintext);
        return ByteBuffer.allocate(nonce.length + ct.length).put(nonce).put(ct).array();
    }

    // Secure password hashing with PBKDF2 (salt + high iteration)
    public String hashPassword(char[] password) throws Exception {
        byte[] salt = new byte[16];
        SecureRandom.getInstanceStrong().nextBytes(salt);
        PBEKeySpec spec = new PBEKeySpec(password, salt, 600_000, 256);
        SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
        byte[] hash = skf.generateSecret(spec).getEncoded();
        return Base64.getEncoder().encodeToString(salt) + ":" + Base64.getEncoder().encodeToString(hash);
    }

    // Strong signature with SHA-256
    public byte[] signStrong(byte[] data, PrivateKey privKey) throws Exception {
        Signature sig = Signature.getInstance("SHA256withRSA");
        sig.initSign(privKey);
        sig.update(data);
        return sig.sign();
    }
}

例では getInstanceStrong で生成した 12 バイトの nonce を、タグ付きの GCM 暗号文の先頭に付けています。128 ビットのタグは改ざんを検出します。再送を防ぐには別途プロトコル側の対策が必要です。パスワード用のメソッドは 16 バイトのソルトとハッシュを返し、署名用のメソッドは SHA256withRSA で署名します。

参考資料