Files
2nd/10_Wiki/Topics/Domain_Programming/DevOps_and_Security/Practical-Cryptography.md
T
Antigravity Agent c24165b8bc refactor(topics): 멀티 에이전트용 지식 재편 — _Common(공통 기본기) + Domain_* 구조
에이전트 8종(대화형/프로그래머 C·S/디자이너/설계자/기획자/QA/PD/PM)에게
[공통 기본 능력 + 롤별 Specialty] 2층으로 지식을 주입하기 위한 재분류.
문서 내용·포맷은 무수정, 폴더 이동만 (6,372개 문서 수 보존 확인).

- Topic_Programming → Domain_Programming (내부 구조 보존)
- Topic_Graphic → Domain_Design
- Topic_Business → Domain_Product
- Topic_General → Domain_General
- _Common 신설: Math(구 Topic_Math_Specialty), Reasoning(구 General/From_Thinking & Reasoning),
  Reasoning_Creativity(구 General/From_창의성), Communication(Poetic_Blog_Writing + From_writing)
- 타 도메인의 From_* 폴더는 유지 (출처 표기일 뿐, 이미 도메인에 맞게 분류된 문서)
- 빈 폴더 정리 (memory/procedures)
- 에이전트→폴더 매핑은 workspace의 .astra/agent-knowledge-map.json (9개 에이전트)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-11 11:05:56 +09:00

6.4 KiB

id, title, category, status, canonical_id, aliases, duplicate_of, source_trust_level, confidence_score, verification_status, tags, raw_sources, last_reinforced, github_commit, tech_stack
id title category status canonical_id aliases duplicate_of source_trust_level confidence_score verification_status tags raw_sources last_reinforced github_commit tech_stack
wiki-2026-0508-practical-cryptography Practical Cryptography 10_Wiki/Topics verified self
Applied Cryptography
Crypto Engineering
none A 0.9 applied
cryptography
security
encryption
2026-05-10 pending
language framework
Python/Go libsodium/cryptography

Practical Cryptography

매 한 줄

"매 don't roll your own crypto". 매 application engineer 의 task 는 매 well-vetted primitives (AES-GCM, ChaCha20-Poly1305, Ed25519, X25519) 의 correct composition — 매 algorithm 의 invention 아님. 2026 의 modern stack 은 libsodium, AWS KMS, age, Noise Protocol Framework 위 의 build.

매 핵심

매 Primitives (2026 baseline)

  • Symmetric AEAD: ChaCha20-Poly1305 (mobile/no-AES-NI), AES-256-GCM (server with AES-NI), AES-256-GCM-SIV (nonce-misuse resistant).
  • Asymmetric: X25519 (ECDH key agreement), Ed25519 (signing), Kyber-1024 (post-quantum KEM, NIST FIPS 203).
  • Hashing: BLAKE3 (fast), SHA-256 (interop), Argon2id (password hashing, 2026 default).
  • Key derivation: HKDF-SHA256 (key expansion), Argon2id (password → key).

매 Threat models

  • Confidentiality: encrypt-then-MAC, AEAD prevents IND-CCA2 attacks.
  • Integrity: HMAC, Poly1305, signatures.
  • Authenticity: signatures (Ed25519), authenticated key exchange (Noise).
  • Forward secrecy: ephemeral keys (X25519 per session).
  • Post-quantum: hybrid Kyber + X25519 (2026 TLS 1.3 default).

매 응용

  1. TLS 1.3 (transport security).
  2. Signal Protocol (E2EE messaging — Double Ratchet).
  3. age/rage (file encryption — replaces GPG).
  4. JWT/PASETO (stateless tokens — PASETO preferred).
  5. Password storage (Argon2id with per-user salt).

💻 패턴

AEAD encryption (ChaCha20-Poly1305 with libsodium)

from nacl.secret import SecretBox
from nacl.utils import random

key = random(SecretBox.KEY_SIZE)  # 32 bytes
box = SecretBox(key)

# Encrypt — nonce auto-generated, prepended to ciphertext
ciphertext = box.encrypt(b"sensitive data")

# Decrypt — fails with CryptoError on tampering
plaintext = box.decrypt(ciphertext)

Authenticated key exchange (X25519 + HKDF)

from cryptography.hazmat.primitives.asymmetric.x25519 import X25519PrivateKey
from cryptography.hazmat.primitives.kdf.hkdf import HKDF
from cryptography.hazmat.primitives import hashes

# Each party generates ephemeral keypair
alice_priv = X25519PrivateKey.generate()
bob_priv = X25519PrivateKey.generate()

# Compute shared secret
shared = alice_priv.exchange(bob_priv.public_key())

# Derive symmetric key — never use raw DH output as key
session_key = HKDF(
    algorithm=hashes.SHA256(),
    length=32,
    salt=None,
    info=b"session-v1",
).derive(shared)

Password hashing (Argon2id)

from argon2 import PasswordHasher

ph = PasswordHasher(
    time_cost=3,       # iterations
    memory_cost=65536, # 64 MiB
    parallelism=4,
)

hash = ph.hash("user-password")  # store this

# Verify (constant-time)
try:
    ph.verify(hash, "user-password")
    if ph.check_needs_rehash(hash):
        new_hash = ph.hash("user-password")  # parameter upgrade
except VerifyMismatchError:
    raise AuthError()

Digital signature (Ed25519)

from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey

priv = Ed25519PrivateKey.generate()
pub = priv.public_key()

signature = priv.sign(b"message")
pub.verify(signature, b"message")  # raises InvalidSignature on failure

Envelope encryption (KMS pattern)

import boto3
from cryptography.fernet import Fernet

kms = boto3.client("kms")

def encrypt_blob(plaintext: bytes, kms_key_id: str) -> dict:
    # Generate per-message data key
    resp = kms.generate_data_key(KeyId=kms_key_id, KeySpec="AES_256")
    data_key = resp["Plaintext"]
    encrypted_dk = resp["CiphertextBlob"]

    # Encrypt data with data key, discard plaintext data key
    f = Fernet(base64.urlsafe_b64encode(data_key))
    ct = f.encrypt(plaintext)

    return {"ciphertext": ct, "encrypted_key": encrypted_dk}

Constant-time comparison

import hmac

# WRONG — leaks length info via timing
if user_token == stored_token:
    pass

# RIGHT — constant time
if hmac.compare_digest(user_token, stored_token):
    pass

Post-quantum hybrid KEM (2026)

# liboqs-python — hybrid X25519 + Kyber768
from oqs import KeyEncapsulation
import nacl.public

# Classical X25519
x_priv = nacl.public.PrivateKey.generate()

# Post-quantum Kyber
with KeyEncapsulation("Kyber768") as kem:
    pq_pub = kem.generate_keypair()

# Combine both shared secrets via HKDF for hybrid security

매 결정 기준

상황 Approach
File encryption age (modern), libsodium SecretBox
Password hash Argon2id (never bcrypt for new systems)
Token format PASETO v4 (Ed25519) over JWT
Mobile/IoT AEAD ChaCha20-Poly1305
TLS 1.3 backend rustls or BoringSSL, hybrid PQ enabled
Signing Ed25519 (never RSA for new systems)

기본값: libsodium + Argon2id + Ed25519 + ChaCha20-Poly1305.

🔗 Graph

🤖 LLM 활용

언제: explain primitive choice, audit crypto code for misuse, suggest migration paths. 언제 X: never ask LLM to design new protocol — always defer to peer-reviewed designs (Noise, Signal).

안티패턴

  • Roll-your-own: custom XOR-based "encryption" — 매 broken in seconds.
  • ECB mode: leaks pattern (penguin image meme). Always GCM/CTR/CBC-with-MAC.
  • Static IV/nonce: catastrophic for GCM (key recovery). Always random or counter.
  • MD5/SHA-1: collision-broken. Never for security purposes.
  • bcrypt for new systems: Argon2id 2026 default.
  • String comparison for tokens: use hmac.compare_digest.

🧪 검증 / 중복

  • Verified (NIST FIPS 203/204/205, RFC 9180 HPKE, libsodium docs).
  • 신뢰도 A.

🕓 Changelog

날짜 변경
2026-05-08 Phase 1
2026-05-10 Manual cleanup — full primitives + 2026 PQ baseline