AWS KMS

The key vault. Symmetric or asymmetric master keys that never leave AWS unencrypted; every AWS "encryption at rest" toggle ultimately calls into KMS.

Két giữ chìa. Master key symmetric hoặc asymmetric, không bao giờ rời AWS ở dạng plain; mọi tick "encryption at rest" trong AWS đều gọi KMS phía dưới.

Symmetric vs asymmetricSymmetric vs asymmetric

Symmetric Asymmetric
AlgorithmThuật toán AES-256 RSA · ECC
OperationsThao tác Encrypt · Decrypt Encrypt/Decrypt or Sign/Verify
Public key exposed?Public key lộ? — Yes — downloadableCó — tải được
Use caseDùng khi In-AWS encrypt at restEncrypt at rest trong AWS Callers outside AWS can't hit KMSCaller ngoài AWS không gọi KMS được

Key types & who's in chargeLoại key & ai quản

Customer Managed AWS Managed AWS Owned
You create it?Bạn tạo? ✅ ❌ ❌
Key policy editable?Sửa key policy? ✅ ❌ (view + audit only)(chỉ xem + audit) ❌ (invisible)(không thấy)
Rotation cadenceChu kỳ rotate 90–2560 days · optionalngày · tuỳ chọn 365 days · forcedngày · bắt buộc AWS decidesAWS tự quyết
CloudTrail auditAudit CloudTrail ✅ ✅ ❌

Key material originNguồn key material

Chosen at key creation — cannot change later. Chọn khi tạo key — không đổi được sau này.

AWS_KMS (default)(mặc định) KMS generates & stores materialKMS sinh & giữ material
EXTERNAL (BYOK)(BYOK) You import material · manual rotationBạn import material · rotate thủ công
AWS_CLOUDHSM HYOK · key never leaves your HSMHYOK · key không rời HSM của bạn
EXTERNAL_KEY_STORE (XKS)(XKS) Keys stay outside AWS, behind your XKS proxyKey nằm ngoài AWS, sau XKS proxy của bạn
BYOK vs CloudHSM BYOK = "I bring my key, AWS holds it." CloudHSM = "I bring my safe, AWS hosts the rack — they never touch the key." BYOK = "Tôi mang chìa của tôi lên, AWS giữ hộ." CloudHSM = "Tôi mang két sắt lên, AWS chỉ cho thuê chỗ đặt — không chạm chìa."

Envelope encryptionEnvelope encryption

KMS caps the API request at 4 KB. For bigger blobs → ask KMS for a data key, encrypt locally, store the encrypted data key with the ciphertext. KMS giới hạn request tại 4 KB. Blob to hơn → xin KMS 1 data key, mã hoá tại chỗ, lưu encrypted data key cùng ciphertext.

1. AskXin GenerateDataKey → {plain, encrypted}
2. Encrypt dataMã hoá data AES-GCM locally · fastAES-GCM tại chỗ · nhanh
3. StoreLưu ciphertext + encrypted data keyciphertext + encrypted data key
4. WipeXoá plain data key from RAM — never persistplain data key khỏi RAM — không lưu
Encryption context Optional key-value AAD on symmetric calls — Decrypt must send back the exact same pairs, it's logged in CloudTrail, and kms:EncryptionContext conditions can pin a key to one resource. Key-value AAD tuỳ chọn trên call symmetric — Decrypt phải gửi lại đúng y các cặp đó, được log vào CloudTrail, và condition kms:EncryptionContext có thể khoá key theo từng resource.

Multi-Region keysMulti-Region key

  • Primary + replicas share the same key ID and material — ciphertext from one region decrypts in another without cross-region API calls.Primary + replica dùng chung key ID và material — ciphertext mã ở 1 region, giải ở region khác không cần gọi cross-region.
  • Not global — each region has its own KMS endpoint. Only one primary at a time; you can promote a replica.Không phải global — mỗi region có endpoint KMS riêng. Chỉ 1 primary một lúc; có thể promote replica.

Deletion & access gotchasGotcha xoá key & truy cập

  • ScheduleKeyDeletion = waiting period 7–30 days (default 30) · state PendingDeletion, key unusable · CancelKeyDeletion to abort. Imported material can be deleted instantly.ScheduleKeyDeletion = chờ 7–30 ngày (mặc định 30) · state PendingDeletion, key không dùng được · CancelKeyDeletion để huỷ. Imported material xoá ngay được.
  • Cross-account use needs both: key policy on the key and IAM policy on the caller. AWS Managed keys never work cross-account — classic exam trap.Dùng cross-account cần cả hai: key policy trên key và IAM policy phía caller. AWS Managed key không bao giờ dùng cross-account được — bẫy đề kinh điển.
  • kms:ViaService pins a key to one service (e.g. only via S3). Grants = temporary, API-only permissions — what AWS services use behind the scenes (EBS attach, RDS snapshots).kms:ViaService khoá key theo 1 service (vd chỉ qua S3). Grant = quyền tạm, chỉ qua API — thứ AWS service dùng ngầm (EBS attach, RDS snapshot).