하드코딩된 암호화 IV

Use of Hardcoded Cryptographic Initialization Value

설명

AES-CBC에서 같은 키와 고정된 초기화 벡터(IV)를 재사용하면 평문의 같은 첫 블록이 같은 암호문 블록으로 나타납니다.

잠재적 영향

암호문을 비교해 메시지 앞부분이 같은지 알아낼 수 있습니다. CBC만으로는 변조도 검출하지 못합니다.

해결 방법

검증된 라이브러리로 AES-CBC 암호화마다 예측할 수 없는 16바이트 IV를 생성하세요. 변조를 검출하려면 인증 암호화를 사용하세요. CBC를 유지한다면 별도 키로 IV와 암호문을 함께 인증하고 복호화 전에 태그를 검증하세요. 다른 모드에서는 해당 모드의 IV 요건을 따르세요.

예시

두 예시 모두 환경 변수 ENCRYTION_KEY를 읽습니다. 이 값을 UTF-8로 인코딩한 AES 키는 16, 24 또는 32바이트여야 합니다.

변경 전

python
# Unsafe AES encryption with hardcoded IV
import os
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad

key = os.environ["ENCRYTION_KEY"].encode()
cipher = AES.new(key, AES.MODE_CBC, b'This is an IV456') # 하드코딩된 IV
plaintext = b'This is a secret message.'
ciphertext = cipher.encrypt(pad(plaintext, AES.block_size))

변경 후

python
# Safe AES encryption with random IV
import os
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
from Crypto.Random import get_random_bytes

key = os.environ["ENCRYTION_KEY"].encode()
iv = get_random_bytes(16)  # 무작위로 생성된 IV
cipher = AES.new(key, AES.MODE_CBC, iv)
plaintext = b'This is a secret message.'
ciphertext = cipher.encrypt(pad(plaintext, AES.block_size))

get_random_bytes(16)은 AES.new에 전달할 IV를 생성합니다. 복호화할 때 사용할 수 있도록 암호문과 함께 보관하세요.

참조