← DevSecOps← DevSecOps
DevSecOpsDevSecOps19 Th7, 2026Jul 19, 202629 phút đọc23 min read

Nền tảng Mật mã họcCryptography Fundamentals

Thuộc bộ kiến thức DevSecOps Roadmap.

Tổng quan

Cryptography (mật mã học) là nền tảng toán học mà hầu như mọi security control khác trong DevSecOps đều dựa vào: TLS trên đường truyền, secrets khi lưu trữ (at rest), container image được ký (signed), password storage, VPN tunnel, code-signing, JWT — tất cả, ở một tầng nào đó, đều quy về một số ít primitive mật mã: symmetric cipher, asymmetric key pair, và cryptographic hash function. Bạn không cần phải là một nhà mật mã học để làm DevSecOps, nhưng bạn cần nhận ra primitive nào giải quyết vấn đề nào, biết algorithm và mode cụ thể nào hiện được coi là an toàn, và — quan trọng nhất — biết khi nào nên dừng lại và dùng một thư viện đã được kiểm chứng thay vì tự sáng chế ra một scheme của riêng mình.

Cryptography cung cấp bốn thuộc tính bảo mật riêng biệt, và cần chính xác về việc cơ chế nào thực sự mang lại thuộc tính nào:

Thuộc tínhÝ nghĩaCơ chế điển hình
Confidentiality (Bảo mật)Chỉ bên được phép mới đọc được dữ liệuEncryption (symmetric/asymmetric)
Integrity (Toàn vẹn)Dữ liệu không bị thay đổi, dù vô tình hay cố ýHashing, MAC, AEAD cipher
Authenticity (Xác thực nguồn gốc)Dữ liệu thực sự đến từ người gửi được khẳng địnhDigital signature, MAC
Non-repudiation (Chống chối bỏ)Người gửi không thể sau đó phủ nhận đã gửiDigital signature (không phải MAC thuần)

Một sai lầm rất phổ biến là encrypt dữ liệu rồi mặc định rằng điều đó cũng bảo vệ integrity của nó. Encryption thuần (ví dụ AES chế độ CBC không kèm MAC) chỉ bảo vệ confidentiality — kẻ tấn công dù không đọc được plaintext vẫn có thể lật bit (bit-flip) hoặc cắt bớt ciphertext mà không bị phát hiện. Các thiết kế hiện đại dùng AEAD (Authenticated Encryption with Associated Data, ví dụ AES-GCM, ChaCha20-Poly1305) chính vì nó mang lại confidentiality integrity/authenticity cùng lúc.

Trong bối cảnh DevSecOps, cryptography xuất hiện khắp pipeline: TLS giữa mọi service-to-service call, hashing cho artifact integrity và định danh Git object, signing cho container image (Cosign/Sigstore) và commit (GPG/Sigstore), envelope encryption cho secrets trong vault, và password hashing ở bất cứ đâu ứng dụng lưu credential. Nắm vững nền tảng một lần sẽ đem lại lợi ích trên tất cả các bề mặt đó.

Kiến thức nền tảng

Symmetric encryption

Symmetric encryption dùng một shared key cho cả encryption lẫn decryption. Chuẩn phổ biến nhất là AES (Advanced Encryption Standard), một block cipher hoạt động trên khối 128-bit với key 128/192/256-bit. AES nhanh (thường được tăng tốc phần cứng qua AES-NI trên CPU hiện đại) và là công cụ chủ lực cho encryption khối lượng dữ liệu lớn — disk encryption, database-at-rest encryption, VPN payload, và giai đoạn bulk-data của TLS.

AES là block cipher, nên cần mode of operation để encrypt dữ liệu dài hơn một block:

ModeCó integrity không?Ghi chú
ECBKhôngKhông bao giờ dùng — các block plaintext giống nhau sinh ra ciphertext giống nhau, lộ pattern
CBCKhông (cần HMAC riêng)Cần padding, dễ bị padding-oracle attack nếu không xử lý cẩn thận
CTRKhông (cần HMAC riêng)Biến block cipher thành stream cipher; có thể chạy song song (parallelizable)
GCMCó (AEAD)Mặc định của ngành hiện nay; kết hợp encryption chế độ CTR với authentication tag tích hợp sẵn

Điểm yếu cốt lõi của symmetric encryption là key distribution. Nếu Alice và Bob muốn giao tiếp, cả hai cần cùng một secret key — nhưng làm sao đưa key đó đến bên kia qua một mạng có thể không an toàn, mà không bị kẻ tấn công chặn được? Đây chính xác là vấn đề mà asymmetric cryptography được phát minh ra để giải quyết, và đó là lý do các giao thức thực tế (TLS, Signal, SSH) dùng symmetric cipher cho bulk data nhưng dùng cơ chế asymmetric để thiết lập shared key ngay từ đầu.

Ví dụ — encrypt và decrypt một file bằng AES-256-GCM với OpenSSL:

# Sinh key 256-bit ngẫu nhiên và IV/nonce 96-bit (không bao giờ dùng lại nonce với cùng một key!)
openssl rand -hex 32 > aes.key
openssl rand -hex 12 > aes.iv

# Encrypt
openssl enc -aes-256-gcm \
  -K "$(cat aes.key)" -iv "$(cat aes.iv)" \
  -in plaintext.txt -out ciphertext.bin

# Decrypt (cần đúng key + IV)
openssl enc -d -aes-256-gcm \
  -K "$(cat aes.key)" -iv "$(cat aes.iv)" \
  -in ciphertext.bin -out decrypted.txt

Vấn đề key distribution trong ví dụ này rất rõ ràng: aes.key phải được chuyển đến bên decrypt bằng cách nào đó, và nếu bị chặn trong quá trình truyền, toàn bộ encryption trở nên vô nghĩa.

Asymmetric encryption

Asymmetric (public-key) cryptography dùng một cặp key liên kết với nhau về mặt toán học: public key, có thể chia sẻ cho bất kỳ ai, và private key, tuyệt đối không được rời khỏi quyền kiểm soát của chủ sở hữu. Dữ liệu được encrypt bằng public key chỉ có thể decrypt bằng private key tương ứng, và — quan trọng hơn — dữ liệu được ký (sign) bằng private key có thể được xác minh (verify) bởi bất kỳ ai có public key. Thuộc tính duy nhất này loại bỏ hoàn toàn vấn đề key distribution: bạn có thể công khai public key của mình, và bất kỳ ai cũng có thể dùng nó để gửi cho bạn thứ mà chỉ bạn mới đọc được, mà không cần bất kỳ shared secret nào trước đó.

Hai họ thuật toán chủ đạo:

AlgorithmKích thước key (mức bảo mật tương đương)Tốc độGhi chú
RSA2048–4096 bitChậm hơn, đặc biệt ở kích thước lớnDựa trên độ khó của việc phân tích số nguyên tố lớn (factoring); vẫn được triển khai rộng rãi nhất
ECC (Elliptic Curve Cryptography)256–384 bit (ví dụ P-256, X25519)Nhanh hơn, key/signature nhỏ hơnDựa trên bài toán discrete logarithm trên đường cong elliptic; được ưu tiên cho TLS, SSH hiện đại, và mobile/IoT nơi kích thước key quan trọng

Vì các phép toán asymmetric tốn kém về mặt tính toán (chậm hơn khoảng 100–1000 lần so với symmetric cipher với cùng lượng dữ liệu), chúng được dùng để encrypt lượng dữ liệu nhỏ — phổ biến nhất là một symmetric session key, chứ không phải bulk payload.

Cặp key asymmetric cũng cho phép digital signature, mang lại authenticity, integrity, non-repudiation trong một cơ chế duy nhất: người ký hash message, encrypt hash đó bằng private key của mình, và bất kỳ ai có public key đều có thể decrypt hash đó rồi so sánh với hash của chính họ tính trên message. Nếu khớp, message được chứng minh là không bị thay đổi và chắc chắn đến từ chủ private key.

Ví dụ — tạo cặp key RSA, ký một file, và verify signature:

# Sinh RSA private key 2048-bit
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out private.pem

# Suy ra public key
openssl pkey -in private.pem -pubout -out public.pem

# Ký file (digest SHA-256 + RSA signature)
openssl dgst -sha256 -sign private.pem -out file.sig artifact.tar.gz

# Verify bằng public key
openssl dgst -sha256 -verify public.pem -signature file.sig artifact.tar.gz
# → "Verified OK"

Chính pattern này — hash, sign bằng private key, verify bằng public key — là nền tảng của container image signing (Sigstore/Cosign), signed Git commit, code-signing certificate, và certificate chain của TLS.

Hybrid encryption (vì sao TLS dùng cả hai)

Không có riêng symmetric hay asymmetric encryption nào lý tưởng cho giao thức mạng: symmetric nhanh nhưng không có cách nào trao đổi key an toàn; asymmetric giải quyết được key exchange nhưng lại quá chậm để encrypt hàng megabyte traffic. Hybrid encryption kết hợp cả hai, và đó chính xác là cách TLS hoạt động:

  1. Client và server thực hiện key exchange (ở TLS 1.3, là ephemeral Diffie-Hellman trên đường cong elliptic — ECDHE) để thống nhất một shared secret mà không bao giờ truyền trực tiếp nó.
  2. Shared secret đó được dùng để derive symmetric session key (thông qua key-derivation function, HKDF trong TLS 1.3).
  3. Toàn bộ dữ liệu ứng dụng thực tế cho phần còn lại của phiên được encrypt bằng symmetric cipher nhanh (AES-GCM hoặc ChaCha20-Poly1305) dùng các session key đó.

Asymmetric cryptography (cặp key của certificate server) chỉ được dùng để xác thực danh tính server — chứng minh public key thực sự thuộc về domain — chứ không dùng để encrypt bulk data. Đây là lý do TLS handshake tương đối tốn kém (một vài phép toán asymmetric) nhưng luồng dữ liệu được encrypt sau đó lại rẻ (thuần túy throughput symmetric AES).

Cryptographic hashing

Một cryptographic hash function nhận input độ dài bất kỳ và cho ra output độ dài cố định (gọi là “digest” hay “hash”), với ba thuộc tính khiến nó hữu ích cho bảo mật:

SHA-256 (thuộc họ SHA-2) là mặc định hiện nay cho kiểm tra integrity nói chung, định danh Git object (SHA-256 trong Git mới, SHA-1 trong lịch sử), chữ ký certificate TLS, và blockchain. SHA-3 là một chuẩn mới hơn, khác biệt về cấu trúc (dựa trên Keccak), có sẵn như một lựa chọn thay thế khi cần đa dạng hóa thuật toán.

MD5 và SHA-1 đã bị phá vỡ về mặt mật mã học và không được dùng cho bất kỳ mục đích bảo mật nào:

Cả hai vẫn có thể xuất hiện trong bối cảnh không liên quan bảo mật (ví dụ checksum nhanh để phát hiện file bị hỏng ngẫu nhiên, không giả định có kẻ tấn công), nhưng không bao giờ dùng cho signature, certificate, password storage, hoặc kiểm tra integrity nơi kẻ tấn công có thể cố tình giả mạo collision.

Ví dụ — tính và so sánh hash bằng OpenSSL:

$ openssl dgst -sha256 artifact.tar.gz
SHA2-256(artifact.tar.gz)= 8f14e45fceea167a5a36dedd4bea2543f9a4b3d0e8f9d3f1a2b3c4d5e6f7089

$ openssl dgst -md5 artifact.tar.gz
MD5(artifact.tar.gz)= 5d41402abc4b2a76b9719d911017c592   # ổn để làm checksum, KHÔNG dùng cho bảo mật

$ openssl dgst -sha1 artifact.tar.gz
SHA1(artifact.tar.gz)= aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d  # đã deprecated, không dùng

Một ứng dụng phổ biến của hashing trong CI/CD: xác minh artifact hoặc base image tải về khớp với digest đã biết là hợp lệ trước khi sử dụng (sha256sum -c checksums.txt, hoặc pull image theo digest — image@sha256:... — thay vì theo tag có thể thay đổi).

Password hashing: vì sao hash thông thường là công cụ sai

Đây là một trong những sai lầm phổ biến và hậu quả nghiêm trọng nhất trong application security: hash password bằng một hash tổng quát nhanh như SHA-256 (kể cả có salt) là không đủ. SHA-256 được thiết kế để nhanh — đó chính xác là điều bạn muốn cho checksum và signature, và chính xác là điều bạn không muốn cho password, vì kẻ tấn công có database password bị đánh cắp có thể tính hàng tỷ hash SHA-256 mỗi giây trên GPU, khiến brute-force và dictionary attack nhắm vào hash bị đánh cắp trở nên rẻ.

Password hashing đòi hỏi các thuật toán được thiết kế cố ý để chậm và tốn tài nguyên, có thể điều chỉnh (tunable) khi phần cứng ngày càng mạnh hơn:

AlgorithmCách tiếp cậnGhi chú
bcryptCPU-hard, dựa trên BlowfishĐược hỗ trợ rộng rãi, đã kiểm chứng qua thời gian từ 1999; có giới hạn độ dài password 72 byte
scryptCPU-hard và memory-hardThiết kế để chống lại phần cứng bẻ khóa chuyên dụng (custom ASIC/GPU) thông qua chi phí bộ nhớ
Argon2 (khuyến nghị Argon2id)CPU-hard và memory-hard, có thể tuneNgười chiến thắng Password Hashing Competition 2015; mặc định được OWASP khuyến nghị hiện nay

Hai khái niệm quan trọng bất kể bạn chọn algorithm nào:

# Ví dụ: hash Argon2id bằng CLI argon2 (minh họa, không phải OpenSSL — OpenSSL không hỗ trợ Argon2)
echo -n "correct horse battery staple" | argon2 "$(openssl rand -hex 16)" -id -t 3 -m 16 -p 4 -l 32 -e

Khuyến nghị hiện hành của OWASP (Password Storage Cheat Sheet 2024): ưu tiên Argon2id, dùng bcrypt nếu Argon2 không khả dụng, và scrypt như một lựa chọn thay thế khác — không bao giờ dùng riêng SHA-256/SHA-512/MD5, kể cả có salt.

TLS/SSL

TLS (Transport Layer Security; SSL là tiền thân đã deprecated — SSLv2/v3 và TLS 1.0/1.1 đều bị cấm bởi các chuẩn hiện đại) bảo mật dữ liệu khi truyền đi bằng đúng mô hình hybrid đã mô tả ở trên. Một cái nhìn đơn giản hóa về TLS 1.2 handshake:

Client                                              Server
  |------ ClientHello (cipher hỗ trợ, random) ------>|
  |<----- ServerHello (cipher được chọn, random) -----|
  |<----- Certificate (public key của server) ---------|
  |<----- ServerKeyExchange, ServerHelloDone ----------|
  |------ ClientKeyExchange (pre-master secret) ----->|
  |------ ChangeCipherSpec, Finished ------------------>|
  |<----- ChangeCipherSpec, Finished -------------------|
  |======= Dữ liệu ứng dụng đã encrypt (symmetric) ======|

Vậy là hai round trip đầy đủ trước khi bất kỳ dữ liệu ứng dụng nào được truyền. TLS 1.3 (RFC 8446) là một thiết kế lại đáng kể:

Forward secrecy đáng được nhấn mạnh riêng: nghĩa là ngay cả khi private key của server bị lộ trong tương lai, các session đã ghi lại trong quá khứ vẫn không thể decrypt được, vì symmetric key của mỗi session được derive từ một ephemeral key exchange không để lại secret nào có thể tái sử dụng. Đây là lý do TLS 1.3 bắt buộc ECDHE và các triển khai TLS 1.2 nên tắt static RSA key exchange.

Kiểm tra cấu hình TLS của một server đang chạy:

# Xem protocol version và cipher được negotiate
openssl s_client -connect example.com:443 -tls1_3 -brief

# Toàn bộ certificate chain
openssl s_client -connect example.com:443 -showcerts </dev/null 2>/dev/null

# Audit nhanh cipher/protocol (cần nmap kèm script ssl-enum-ciphers)
nmap --script ssl-enum-ciphers -p 443 example.com

“HTTPS everywhere” giờ là kỳ vọng mặc định, không còn là một bước hardening tùy chọn: trình duyệt gắn cờ HTTP thuần là “Not Secure,” HSTS (header Strict-Transport-Security) buộc trình duyệt chỉ kết nối qua HTTPS đến một domain (ngăn downgrade attack kiểu SSL-stripping), và CA tự động miễn phí (Let’s Encrypt qua ACME) đã xóa bỏ lý do lịch sử về chi phí/độ phức tạp cho việc không dùng TLS. Traffic service-to-service nội bộ trong một cluster (mTLS qua service mesh) ngày càng được áp dụng cùng tiêu chuẩn đó — “tin tưởng mạng nội bộ” không còn là mô hình bảo mật chấp nhận được (xem Zero Trust).

Giải mã (decode) một certificate

Một certificate public-key (X.509) gắn một public key với một danh tính, được ký bởi một Certificate Authority. Decode một certificate cho thấy cấu trúc trong thực tế:

$ openssl x509 -in cert.pem -noout -text
Certificate:
    Data:
        Version: 3 (0x2)
        Serial Number: 04:a1:9e:...
        Signature Algorithm: sha256WithRSAEncryption
        Issuer: C=US, O=Let's Encrypt, CN=R11
        Validity
            Not Before: Jan 10 00:00:00 2026 GMT
            Not After : Apr 10 23:59:59 2026 GMT
        Subject: CN=example.com
        Subject Public Key Info:
            Public Key Algorithm: id-ecPublicKey
                Public-Key: (256 bit)
                ASN1 OID: prime256v1
        X509v3 extensions:
            X509v3 Basic Constraints: critical
                CA:FALSE
            X509v3 Key Usage: critical
                Digital Signature
            X509v3 Extended Key Usage:
                TLS Web Server Authentication
            X509v3 Subject Alternative Name:
                DNS:example.com, DNS:www.example.com
            Authority Information Access:
                OCSP - URI:http://r11.o.lencr.org
                CA Issuers - URI:http://r11.i.lencr.org/
            X509v3 CRL Distribution Points:
                Full Name: URI:http://r11.c.lencr.org/91.crl
    Signature Algorithm: sha256WithRSAEncryption

Các trường quan trọng cần biết:

PKI (Public Key Infrastructure)

PKI là hệ thống chính sách, vai trò, và hạ tầng quản lý việc phát hành, phân phối, và thu hồi (revoke) certificate để public key có thể được tin cậy ở quy mô lớn.

Certificate chain of trust:

Root CA (tự ký, offline, nằm trong trust store của OS/browser)
   └── Intermediate CA (online, thực hiện việc ký hàng ngày)
         └── Leaf/end-entity certificate (example.com)

Root CA được giữ offline và air-gapped chính vì đó là trust anchor tối thượng — nếu bị compromise, mọi certificate từng được phát hành dưới nó đều đáng ngờ. Intermediate mới là bên thực sự ký leaf certificate hàng ngày, nên khi intermediate bị compromise (đã từng xảy ra, ví dụ vụ DigiNotar năm 2011) có thể được xử lý bằng cách revoke intermediate đó, thay vì revoke root.

Vòng đời của certificate:

  1. Issuance (phát hành) — bên đăng ký sinh một cặp key và một CSR (Certificate Signing Request), CA xác minh quyền kiểm soát domain/organization (các mức xác thực DV/OV/EV), sau đó ký certificate.
  2. Renewal (gia hạn) — trước khi hết hạn, một certificate mới được phát hành; tự động hóa (giao thức ACME, cert-manager trong Kubernetes) đã khiến certificate ngắn hạn trở nên khả thi bằng cách xử lý renewal mà không cần can thiệp thủ công.
  3. Revocation (thu hồi) — nếu private key bị lộ hoặc certificate bị phát hành sai, nó phải được vô hiệu hóa trước khi hết hạn tự nhiên:
    • CRL (Certificate Revocation List) — CA công bố một danh sách đã ký các serial number certificate bị revoke; client tải về và kiểm tra. Nhược điểm: danh sách có thể phình to, và đây là kiểm tra “soft-fail” ở hầu hết client (CRL offline thường bị bỏ qua).
    • OCSP (Online Certificate Status Protocol) — client truy vấn CA theo thời gian thực về trạng thái của một certificate cụ thể. Nhanh và nhẹ hơn CRL, nhưng tạo ra một phụ thuộc trực tiếp (live dependency) vào CA (độ trễ, rò rỉ privacy, và một sự cố ngoài dự kiến trở thành rủi ro fail-open) — được giảm nhẹ bằng OCSP stapling, trong đó chính server lấy và cache OCSP response đã ký rồi trình bày nó trong quá trình TLS handshake, để client không cần liên hệ CA nữa.

Certificate pinning giúp một client cụ thể chống lại một sự cố PKI compromise rộng hơn: client hardcode (pin) certificate hoặc public key mong đợi (hoặc hash của nó) cho một service nhất định, và từ chối kết nối nếu certificate được trình bày không khớp — ngay cả khi nó vẫn được ký hợp lệ bởi một CA đáng tin cậy. Điều này bảo vệ trước trường hợp một CA bị compromise hoặc bị ép buộc phát hành một certificate giả mạo cho domain của bạn. Đánh đổi là sự mong manh về vận hành: pinning phải được cập nhật đồng bộ với việc xoay vòng (rotation) certificate, nếu không bạn sẽ tự khóa mình ra trong một lần gia hạn thông thường (nhiều sự cố lớn từng xảy ra chính vì lý do này). Thực tiễn hiện đại ưu tiên pin CA/intermediate hoặc dùng giám sát Expect-CT/Certificate Transparency thay vì pin leaf certificate một cách nghiêm ngặt, chính để giảm sự mong manh đó.

Thiết kế PKI và failover ở cấp doanh nghiệp:

Khái niệm chính

Key management

Quản lý key mật mã đúng cách thường khó hơn việc chọn đúng algorithm — và đây là nơi hầu hết các sự cố mật mã học trong thực tế thực sự xảy ra (hardcoded key, key bị commit vào source control, key không bao giờ được rotate, một key dùng cho tất cả mọi thứ).

Envelope encryption
  1. 01KMS / HSMMaster Key (KEK) nằm ở đây, không bao giờ rời đi
  2. encrypt/decrypt
    02Data Encryption Key (DEK)ngẫu nhiên, theo từng object/file
  3. encrypt/decrypt (cục bộ, nhanh, symmetric)
    03Dữ liệu thực tếpayload lớn

So sánh: symmetric vs. asymmetric vs. hashing

Symmetric encryptionAsymmetric encryptionHashing
Mục đíchConfidentialityConfidentiality, key exchange, digital signatureIntegrity, authenticity (khi kết hợp với signing), xác minh password
KeyMột shared secret keyCặp public/private keyKhông cần key (hoặc key cho HMAC)
Đảo ngược được không?Có (với key)Có (với private key)Không — one-way theo thiết kế
Tốc độNhanhChậm (chậm hơn 100–1000 lần so với symmetric)Nhanh (trừ password hash được thiết kế cố ý chậm)
Algorithm điển hìnhAES-256-GCM, ChaCha20-Poly1305RSA-2048/4096, ECC (P-256, X25519)SHA-256, SHA-3, BLAKE2; bcrypt/Argon2 cho password
Trường hợp sử dụng điển hìnhEncrypt bulk data, disk/DB encryption, dữ liệu session TLSKey exchange trong TLS handshake, certificate, ký code/commitChecksum, Git object ID, digest cho digital signature, lưu trữ password

Best Practices

Tài liệu tham khảo


Chủ đề liên quan: Nền tảng Networking cho Bảo mật · Identity & Access Management · Secure Coding & Bảo mật ứng dụng Web

Part of the DevSecOps Roadmap knowledge base.

Overview

Cryptography is the mathematical foundation that every other security control in DevSecOps ultimately leans on: TLS on the wire, secrets at rest, signed container images, password storage, VPN tunnels, code-signing, and JWTs all reduce, at some layer, to a handful of cryptographic primitives — symmetric ciphers, asymmetric key pairs, and cryptographic hash functions. You do not need to be a cryptographer to work in DevSecOps, but you do need to recognize which primitive solves which problem, know which specific algorithms and modes are currently considered safe, and — most importantly — know when to stop and use a vetted library instead of inventing your own scheme.

Cryptography provides four distinct security properties, and it’s worth being precise about which one each mechanism actually gives you:

PropertyWhat it meansTypical mechanism
ConfidentialityOnly authorized parties can read the dataEncryption (symmetric/asymmetric)
IntegrityData has not been altered, accidentally or maliciouslyHashing, MACs, AEAD ciphers
AuthenticityThe data really came from the claimed senderDigital signatures, MACs
Non-repudiationThe sender cannot later deny having sent itDigital signatures (not plain MACs)

A very common mistake is to encrypt data and assume that also protects its integrity. Plain encryption (e.g., AES in CBC mode with no MAC) protects confidentiality only — an attacker who cannot read the plaintext can often still flip bits or truncate ciphertext undetected. Modern designs use AEAD (Authenticated Encryption with Associated Data, e.g., AES-GCM, ChaCha20-Poly1305) specifically because it delivers confidentiality and integrity/authenticity together.

In a DevSecOps context, cryptography shows up everywhere in the pipeline: TLS between every service-to-service call, hashing for artifact integrity and Git commit addressing, signing for container images (Cosign/Sigstore) and commits (GPG/Sigstore), envelope encryption for secrets in a vault, and password hashing wherever your application stores credentials. Getting the fundamentals right once pays off across all of those surfaces.

Fundamentals

Symmetric encryption

Symmetric encryption uses one shared secret key for both encryption and decryption. The dominant standard is AES (Advanced Encryption Standard), a block cipher operating on 128-bit blocks with 128/192/256-bit keys. AES is fast (often hardware-accelerated via AES-NI on modern CPUs) and is the workhorse for bulk data encryption — disk encryption, database-at-rest encryption, VPN payloads, and the bulk-data phase of TLS.

AES is a block cipher, so it needs a mode of operation to encrypt data longer than one block:

ModeProvides integrity?Notes
ECBNoNever use — identical plaintext blocks produce identical ciphertext blocks, leaking patterns
CBCNo (needs separate HMAC)Requires padding, vulnerable to padding-oracle attacks if not handled carefully
CTRNo (needs separate HMAC)Turns the block cipher into a stream cipher; parallelizable
GCMYes (AEAD)Industry default today; combines CTR-mode encryption with a built-in authentication tag

The core weakness of symmetric encryption is key distribution. If Alice and Bob want to communicate, they both need the same secret key — but how do you get that key to the other party over a network that might be hostile, without an attacker intercepting it? This is exactly the problem asymmetric cryptography was invented to solve, and it’s why real-world protocols (TLS, Signal, SSH) use symmetric ciphers for bulk data but asymmetric mechanisms to establish the shared key in the first place.

Example — encrypt and decrypt a file with AES-256-GCM using OpenSSL:

# Generate a random 256-bit key and a 96-bit IV/nonce (never reuse a nonce with the same key!)
openssl rand -hex 32 > aes.key
openssl rand -hex 12 > aes.iv

# Encrypt
openssl enc -aes-256-gcm \
  -K "$(cat aes.key)" -iv "$(cat aes.iv)" \
  -in plaintext.txt -out ciphertext.bin

# Decrypt (same key + IV required)
openssl enc -d -aes-256-gcm \
  -K "$(cat aes.key)" -iv "$(cat aes.iv)" \
  -in ciphertext.bin -out decrypted.txt

The key distribution problem in this example is obvious: aes.key has to reach the decrypting party somehow, and if it’s ever intercepted in transit, the encryption is worthless.

Asymmetric encryption

Asymmetric (public-key) cryptography uses a mathematically linked key pair: a public key, which can be shared with anyone, and a private key, which must never leave its owner’s control. Data encrypted with the public key can only be decrypted with the corresponding private key, and — critically — data signed with the private key can be verified by anyone holding the public key. This single property eliminates the key-distribution problem: you can publish your public key openly, and anyone can use it to send you something only you can read, without any prior shared secret.

The two dominant families:

AlgorithmKey size (comparable security)SpeedNotes
RSA2048–4096 bitSlower, especially at larger sizesBased on the difficulty of factoring large primes; still the most widely deployed
ECC (Elliptic Curve Cryptography)256–384 bit (e.g., P-256, X25519)Faster, smaller keys/signaturesBased on the elliptic curve discrete logarithm problem; preferred for modern TLS, SSH, and mobile/IoT where key size matters

Because asymmetric operations are computationally expensive (roughly 100–1000x slower than symmetric ciphers for equivalent data), they are used to encrypt small amounts of data — most commonly a symmetric session key, not the bulk payload itself.

Asymmetric key pairs also enable digital signatures, which give authenticity, integrity, and non-repudiation in one mechanism: the signer hashes the message, encrypts the hash with their private key, and anyone with the public key can decrypt that hash and compare it against their own hash of the message. If they match, the message is provably untampered and provably from the private-key holder.

Example — generate an RSA key pair, sign a file, and verify the signature:

# Generate a 2048-bit RSA private key
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out private.pem

# Derive the public key
openssl pkey -in private.pem -pubout -out public.pem

# Sign a file (SHA-256 digest + RSA signature)
openssl dgst -sha256 -sign private.pem -out file.sig artifact.tar.gz

# Verify with the public key
openssl dgst -sha256 -verify public.pem -signature file.sig artifact.tar.gz
# → "Verified OK"

This exact pattern — hash, sign with a private key, verify with the public key — is what underlies container image signing (Sigstore/Cosign), signed Git commits, code-signing certificates, and TLS certificate chains.

Hybrid encryption (why TLS uses both)

Neither symmetric nor asymmetric encryption alone is ideal for network protocols: symmetric is fast but has no way to safely exchange keys; asymmetric solves key exchange but is far too slow to encrypt megabytes of traffic. Hybrid encryption combines both, and it’s exactly what TLS does:

  1. The client and server perform a key exchange (in TLS 1.3, an ephemeral Diffie-Hellman exchange over an elliptic curve — ECDHE) to agree on a shared secret without ever transmitting it directly.
  2. That shared secret is used to derive symmetric session keys (via a key-derivation function, HKDF in TLS 1.3).
  3. All actual application data for the rest of the session is encrypted with the fast symmetric cipher (AES-GCM or ChaCha20-Poly1305) using those session keys.

Asymmetric cryptography (the server’s certificate key pair) is used only to authenticate the server’s identity — proving the public key really belongs to the domain — not to encrypt the bulk data. This is why TLS handshakes are relatively expensive (a handful of asymmetric operations) but the encrypted data stream afterward is cheap (pure symmetric AES throughput).

Cryptographic hashing

A cryptographic hash function takes an arbitrary-length input and produces a fixed-length output (the “digest” or “hash”), with three properties that make it useful for security:

SHA-256 (part of the SHA-2 family) is the current default for general-purpose integrity checking, Git object addressing (SHA-256 in newer Git, SHA-1 historically), TLS certificate signatures, and blockchain. SHA-3 is a newer, structurally different standard (Keccak-based) available as an alternative when algorithm diversity is wanted.

MD5 and SHA-1 are cryptographically broken and must not be used for any security purpose:

Both may still appear in non-security contexts (e.g., a quick checksum for accidental file corruption where no adversary is assumed), but never for signatures, certificates, password storage, or integrity checks where an attacker might deliberately try to forge a collision.

Example — compute and compare hashes with OpenSSL:

$ openssl dgst -sha256 artifact.tar.gz
SHA2-256(artifact.tar.gz)= 8f14e45fceea167a5a36dedd4bea2543f9a4b3d0e8f9d3f1a2b3c4d5e6f7089

$ openssl dgst -md5 artifact.tar.gz
MD5(artifact.tar.gz)= 5d41402abc4b2a76b9719d911017c592   # fine as a checksum, NOT for security

$ openssl dgst -sha1 artifact.tar.gz
SHA1(artifact.tar.gz)= aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d  # deprecated, do not use

A common CI/CD use of hashing: verifying that a downloaded artifact or base image matches a known-good digest before use (sha256sum -c checksums.txt, or pulling images by digest — image@sha256:... — instead of by mutable tag).

Password hashing: why generic hashes are the wrong tool

This is one of the most common and consequential mistakes in application security: hashing a password with a fast general-purpose hash like SHA-256 (even with a salt) is insufficient. SHA-256 is designed to be fast — that’s exactly what you want for checksums and signatures, and exactly what you don’t want for passwords, because an attacker with a stolen password database can compute billions of SHA-256 hashes per second on a GPU, making brute-force and dictionary attacks against stolen hashes cheap.

Password hashing requires algorithms deliberately designed to be slow and resource-intensive, tunable as hardware gets faster:

AlgorithmApproachNotes
bcryptCPU-hard, based on BlowfishWidely supported, battle-tested since 1999; has a 72-byte password length limit
scryptCPU- and memory-hardDesigned to resist custom ASIC/GPU cracking hardware via memory cost
Argon2 (Argon2id recommended)CPU- and memory-hard, tunableWinner of the 2015 Password Hashing Competition; current OWASP-recommended default

Two concepts that matter regardless of which algorithm you pick:

# Example: Argon2id hashing via the argon2 CLI (illustrative, not OpenSSL — OpenSSL has no Argon2 support)
echo -n "correct horse battery staple" | argon2 "$(openssl rand -hex 16)" -id -t 3 -m 16 -p 4 -l 32 -e

OWASP’s current guidance (2024 Password Storage Cheat Sheet): prefer Argon2id, fall back to bcrypt if Argon2 isn’t available, and scrypt as another alternative — never SHA-256/SHA-512/MD5 alone, even salted.

TLS/SSL

TLS (Transport Layer Security; SSL is its deprecated predecessor — SSLv2/v3 and TLS 1.0/1.1 are all now forbidden by modern standards) secures data in transit using exactly the hybrid model described above. A simplified view of the TLS 1.2 handshake:

Client                                              Server
  |------ ClientHello (supported ciphers, random) -->|
  |<----- ServerHello (chosen cipher, random) --------|
  |<----- Certificate (server's public key) ----------|
  |<----- ServerKeyExchange, ServerHelloDone ----------|
  |------ ClientKeyExchange (pre-master secret) ----->|
  |------ ChangeCipherSpec, Finished ------------------>|
  |<----- ChangeCipherSpec, Finished -------------------|
  |======= Encrypted application data (symmetric) ======|

That’s two full round trips before any application data flows. TLS 1.3 (RFC 8446) is a substantial redesign that:

Forward secrecy deserves a callout: it means that even if a server’s private key is compromised in the future, past recorded sessions cannot be decrypted, because each session’s symmetric keys were derived from an ephemeral key exchange that leaves no reusable secret behind. This is why TLS 1.3 mandates ECDHE and TLS 1.2 deployments should disable static RSA key exchange.

Inspecting a live server’s TLS configuration:

# See negotiated protocol version and cipher
openssl s_client -connect example.com:443 -tls1_3 -brief

# Full certificate chain
openssl s_client -connect example.com:443 -showcerts </dev/null 2>/dev/null

# Quick cipher/protocol audit (requires nmap with ssl-enum-ciphers script)
nmap --script ssl-enum-ciphers -p 443 example.com

“HTTPS everywhere” is now the baseline expectation, not an optional hardening step: browsers flag plain HTTP as “Not Secure,” HSTS (Strict-Transport-Security header) forces browsers to only ever connect via HTTPS to a domain (preventing SSL-stripping downgrade attacks), and free automated CAs (Let’s Encrypt via ACME) removed the historical cost/complexity excuse for not using TLS. Internal service-to-service traffic inside a cluster (mTLS via a service mesh) is increasingly held to the same standard — “trust the network” is no longer an acceptable security model (see Zero Trust).

Decoding a certificate

A public-key (X.509) certificate binds a public key to an identity, signed by a Certificate Authority. Decoding one shows the structure in practice:

$ openssl x509 -in cert.pem -noout -text
Certificate:
    Data:
        Version: 3 (0x2)
        Serial Number: 04:a1:9e:...
        Signature Algorithm: sha256WithRSAEncryption
        Issuer: C=US, O=Let's Encrypt, CN=R11
        Validity
            Not Before: Jan 10 00:00:00 2026 GMT
            Not After : Apr 10 23:59:59 2026 GMT
        Subject: CN=example.com
        Subject Public Key Info:
            Public Key Algorithm: id-ecPublicKey
                Public-Key: (256 bit)
                ASN1 OID: prime256v1
        X509v3 extensions:
            X509v3 Basic Constraints: critical
                CA:FALSE
            X509v3 Key Usage: critical
                Digital Signature
            X509v3 Extended Key Usage:
                TLS Web Server Authentication
            X509v3 Subject Alternative Name:
                DNS:example.com, DNS:www.example.com
            Authority Information Access:
                OCSP - URI:http://r11.o.lencr.org
                CA Issuers - URI:http://r11.i.lencr.org/
            X509v3 CRL Distribution Points:
                Full Name: URI:http://r11.c.lencr.org/91.crl
    Signature Algorithm: sha256WithRSAEncryption

Key fields to know:

PKI (Public Key Infrastructure)

PKI is the system of policies, roles, and infrastructure that manages issuance, distribution, and revocation of certificates so that public keys can be trusted at scale.

Certificate chain of trust:

Root CA (self-signed, offline, in OS/browser trust store)
   └── Intermediate CA (online, does day-to-day signing)
         └── Leaf/end-entity certificate (example.com)

Root CAs are kept offline and air-gapped precisely because they are the ultimate trust anchor — if compromised, every certificate ever issued under them is suspect. Intermediates are the ones actually signing leaf certificates day to day, so an intermediate compromise (which has happened, e.g., the 2011 DigiNotar breach) can be contained by revoking that specific intermediate rather than the root.

Certificate lifecycle:

  1. Issuance — the applicant generates a key pair and a CSR (Certificate Signing Request), the CA validates domain/organization control (DV/OV/EV validation levels), then signs the certificate.
  2. Renewal — before expiry, a new certificate is issued; automation (ACME protocol, cert-manager in Kubernetes) has made short-lived certs practical by handling renewal without manual intervention.
  3. Revocation — if a private key is compromised or a certificate was mis-issued, it must be invalidated before its natural expiry:
    • CRL (Certificate Revocation List) — the CA publishes a signed list of revoked certificate serial numbers; clients download and check it. Downsides: lists can grow large, and it’s a “soft-fail” check in most clients (offline CRL is often just ignored).
    • OCSP (Online Certificate Status Protocol) — clients query the CA in real time for a single certificate’s status. Faster and lighter than CRL, but introduces a live dependency on the CA (latency, privacy leak, and an outage becomes a fail-open risk) — mitigated by OCSP stapling, where the server itself fetches and caches the signed OCSP response and presents it during the TLS handshake, so clients don’t have to contact the CA at all.

Certificate pinning hardens a specific client against a broader PKI compromise: the client hardcodes (pins) the expected certificate or public key (or its hash) for a given service, and refuses to connect if the presented certificate doesn’t match — even if it’s otherwise validly signed by a trusted CA. This defends against a compromised or coerced CA issuing a rogue certificate for your domain. The tradeoff is operational fragility: pinning must be updated in lockstep with certificate rotation, or you lock yourself out during a routine renewal (several major outages have been caused by exactly this). Modern practice favors pinning the CA/intermediate or using Expect-CT/Certificate Transparency monitoring over strict leaf-certificate pinning, precisely to reduce that fragility.

Enterprise PKI design and failover:

Key Concepts

Key management

Managing cryptographic keys correctly is often harder than choosing the right algorithm — and where most real-world cryptographic failures actually occur (hardcoded keys, keys committed to source control, keys never rotated, one key used for everything).

Envelope encryption
  1. 01KMS / HSMMaster Key (KEK) lives here, never leaves
  2. encrypts/decrypts
    02Data Encryption Key (DEK)random, per-object/per-file
  3. encrypts/decrypts (locally, fast, symmetric)
    03Actual datalarge payload

Comparison: symmetric vs. asymmetric vs. hashing

Symmetric encryptionAsymmetric encryptionHashing
PurposeConfidentialityConfidentiality, key exchange, digital signaturesIntegrity, authenticity (when combined with signing), password verification
KeysOne shared secret keyPublic/private key pairNo key (or a key for HMAC)
Reversible?Yes (with the key)Yes (with the private key)No — one-way by design
SpeedFastSlow (100–1000x slower than symmetric)Fast (except deliberately slow password hashes)
Typical algorithmsAES-256-GCM, ChaCha20-Poly1305RSA-2048/4096, ECC (P-256, X25519)SHA-256, SHA-3, BLAKE2; bcrypt/Argon2 for passwords
Typical use caseEncrypting bulk data, disk/DB encryption, TLS session dataTLS handshake key exchange, certificates, code/commit signingChecksums, Git object IDs, digital signature digests, password storage

Best Practices

References


Related topics: Networking Fundamentals for Security · Identity & Access Management · Secure Coding & Web Application Security