
암호학
Key Management System키 관리 시스템 (KMS)
키 관리 시스템(KMS, Key Management System)은 암호화 키의 생성·배포·저장·교체·폐기 등 전체 수명주기를 중앙에서 관리하는 시스템이다. 데이터 보호의 핵심 인프라다.
키 수명주기
생성(Generate)
↓ TRNG 기반 안전한 난수
활성화(Activate)
↓ 실제 암호화에 사용
갱신(Renew/Rotate)
↓ 주기적 교체 (키 노출 위험 최소화)
비활성화(Deactivate)
↓ 신규 암호화 중단, 복호화만 허용
아카이브(Archive)
↓ 장기 보관 (감사, 분쟁 해결)
폐기(Destroy)
↓ 완전 삭제 (Zeroization)키 계층 구조
Master Key (CMK/KEK)
├─ HSM/TPM 보호, 절대 평문 노출 없음
└─ Data Encryption Key (DEK) 암호화에 사용
DEK (Data Encryption Key)
├─ CMK로 암호화되어 저장
├─ 실제 데이터 암호화에 사용
└─ 데이터마다 고유 DEK (엔벨로프 암호화)
엔벨로프 암호화:
데이터 → AES-256-GCM(DEK) → 암호화된 데이터
DEK → AES-256-GCM(CMK) → 암호화된 DEK
→ 저장: [암호화된 DEK + 암호화된 데이터]AWS KMS 활용
python
import boto3, base64
kms = boto3.client('kms', region_name='ap-northeast-2')
CMK_ID = 'arn:aws:kms:ap-northeast-2:123456789:key/abc-def'
def encrypt_data(plaintext: str) -> dict:
# DEK 생성 (CMK로 보호)
response = kms.generate_data_key(
KeyId=CMK_ID,
KeySpec='AES_256'
)
dek_plaintext = response['Plaintext'] # 메모리에서만 사용
dek_encrypted = response['CiphertextBlob'] # 저장용
# DEK로 데이터 암호화
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
import os
nonce = os.urandom(12)
ciphertext = AESGCM(dek_plaintext).encrypt(
nonce, plaintext.encode(), None
)
# DEK 메모리에서 삭제 (중요!)
del dek_plaintext
return {
'encrypted_dek': base64.b64encode(dek_encrypted).decode(),
'nonce': base64.b64encode(nonce).decode(),
'ciphertext': base64.b64encode(ciphertext).decode()
}
def decrypt_data(encrypted: dict) -> str:
# DEK 복호화 (KMS 호출)
dek = kms.decrypt(
CiphertextBlob=base64.b64decode(encrypted['encrypted_dek'])
)['Plaintext']
nonce = base64.b64decode(encrypted['nonce'])
ciphertext = base64.b64decode(encrypted['ciphertext'])
return AESGCM(dek).decrypt(nonce, ciphertext, None).decode()키 교체 (Key Rotation) 전략
자동 교체 (AWS KMS):
aws kms enable-key-rotation --key-id alias/my-key
# 매년 자동으로 CMK 교체 (기존 데이터 자동 재암호화)
수동 교체 전략:
1. 새 CMK 생성
2. 새 CMK로 새 데이터 암호화 시작
3. 구 CMK로 암호화된 데이터 점진적 재암호화
4. 모든 데이터 마이그레이션 완료 후 구 CMK 폐기
교체 주기 권장:
CMK: 연 1회
DEK: 데이터 볼륨 기반 (1GB 또는 1일)
TLS 세션키: 매 세션 (Forward Secrecy)