8.2 Cryptography at Rest and in Transit: LUKS2, GPG, SSH, TLS & OpenSSL
Key Takeaways
Use a hash for an integrity comparison against trusted evidence, HMAC for shared-secret authentication and a password KDF for password-derived keys.
LUKS keyslots protect access to the volume key. Test an added credential while the original credential is still available.
On systemd systems, crypttab opens an encrypted device mapping. A separate mount definition can use the mapped device.
Require public-key authentication using the effective sshd configuration and test a second session before ending the first.
Compare certificate and private-key public keys separately from certificate chain and service-name validation.
8.2 Cryptography at Rest and in Transit: LUKS2, GPG, SSH, TLS & OpenSSL
Choose protection based on the threat. Encryption protects confidentiality. Authentication establishes which peer or key produced data, and integrity checks detect changes. A control can address more than one of these properties if its design provides them.
Encryption, hashes and keyed checks
| Mechanism | What it provides |
|---|---|
| Symmetric encryption | A shared secret controls encryption and decryption. AES and ChaCha20 are examples. Authenticated modes or constructions such as AES-GCM and ChaCha20-Poly1305 also check data integrity under the proper conditions. |
| Public-key cryptography | A private/public key pair supports a particular operation, such as signature or key agreement. Ed25519 is a signature algorithm and X25519 is used for key agreement. Use the algorithm and operation supported by the protocol. |
| SHA-256 | Produces a 256-bit digest. A checksum can detect changes when compared against an authentic reference. A hash alone does not authenticate the origin. |
| HMAC | A keyed computation using a hash, such as HMAC-SHA256. Parties with the secret key can check message integrity and authenticity. HMAC leaves the plaintext visible unless another operation encrypts it. |
| Password-based key derivation | A salted, costed construction derives a key from a passphrase. Argon2id is memory-hard and can increase the cost of offline guessing. Raw SHA-256 is a different tool. |
Keep the parameter choices and algorithms compatible with the deployment's vetted policy. Replace obsolete protocol versions and weak choices in the relevant context. MD5 and SHA-1 are unsuitable when new designs depend on collision resistance. Check security advisories and the actual signature or protocol in use. For example, OpenSSH's legacy SHA-1 ssh-rsa signature differs from an RSA key used with RSA/SHA-2 signatures.
File-level protection with GnuPG
GnuPG implements OpenPGP file encryption and signatures. In symmetric mode, gpg --symmetric report.txt prompts for a passphrase. For recipient encryption, verify the recipient key's fingerprint through a trusted channel before using an imported key. The sender identifies the recipient's public key and GnuPG encrypts the file for the matching private key holder. Do not treat the unverified UID string in a keyring as proof that the key belongs to the intended user.
On a lab keyring with the expected recipient key already verified:
gpg --encrypt --recipient labreader@example.test report.txt
gpg --output restored-report.txt --decrypt report.txt.gpg
gpg --detach-sign report.txt
gpg --verify report.txt.sig report.txt
The decrypt command needs an applicable secret key. The detached signature check uses the signer’s public key and must be tied to the intended signer. Use separate output filenames and check success before trusting a result. A file sent privately can also be signed when the receiver needs to verify its origin.
Block storage encryption with LUKS2
Linux dm-crypt exposes a decrypted view of encrypted block storage when a volume is unlocked. LUKS adds on-disk metadata and keyslots for credentials. The data is encrypted with a volume key. Ordinary bound keyslots protect access to that same key through different credentials, so adding another credential does not normally re-encrypt the data. LUKS2 uses structured metadata and has keyslot IDs 0 through 31. Actual slot capacity depends on the available keyslot area and key sizes.
Argon2id and Argon2i are available password-based KDFs with appropriate LUKS2 tooling, and some installations use PBKDF2. Inspect cryptsetup luksDump on the intended device for the actual format and settings. Use a verified backup and examine the devices before performing destructive initialization. While a volume is open, an authorized process may read the decrypted data through the mapped device. Ordinary encryption does not by itself guarantee tamper detection.
On an isolated Linux lab with device-mapper and loop support, use a fresh disposable directory and a new backing file to practice initial setup. Assume lab-admin.key is a separately provisioned and protected test keyfile.
truncate -s 64M ./luks-lab.img
cryptsetup luksFormat --type luks2 ./luks-lab.img
cryptsetup luksAddKey ./luks-lab.img ./lab-admin.key
cryptsetup open ./luks-lab.img lab_crypt
cryptsetup status lab_crypt
cryptsetup close lab_crypt
Format initializes the test container and prompts for the initial credential under these settings. Adding a keyfile normally requires an existing unlock credential and an available slot. The added credential leaves the existing one in place. For a storage lab that needs a filesystem, create it on the newly opened mapping only after confirming the target. Unmount any filesystem on the mapping before closing it. Verify approved recovery credentials before removing a keyslot.
On a systemd host that uses /etc/crypttab, the first two fields select the mapping name and encrypted source. The optional third and fourth fields choose key material and options. For example, a secondary volume might use:
data_crypt UUID=326753b9-b010-471d-a2b9-22b4f23c716c none luks
This illustrative UUID identifies the encrypted source. Use the actual LUKS UUID when configuring a system. The mapping is made available at /dev/mapper/data_crypt. With none, configured key sources can supply the key before a prompt is needed. A filesystem entry in /etc/fstab can depend on the mapped device. The system's boot and initramfs implementation determines when and how root or secondary mappings are set up. Check the implications of options such as discard before using them.
TLS, certificates and trust
TLS 1.0 and 1.1 are deprecated. TLS 1.2 and 1.3 can be used with an applicable modern policy and compatible endpoints. Determine the negotiated version and the server identity separately. OpenSSL and LibreSSL provide TLS and cryptographic tools and libraries with overlapping interfaces, but available features and CLI flags depend on the installed implementation and version.
A typical TLS service sends its endpoint certificate and necessary intermediate certificates. A client validates the chain to a trust anchor it accepts, checks the certificate properties, and compares the intended identity with the certificate's appropriate subject alternative names. Certificate expiry, clock and revocation policy can affect the result. A no-cost public CA such as Let's Encrypt can issue browser-trusted certificates through ACME. Commercial issuers may offer different validation products or support. Choose by client trust, validation needs and lifecycle, since price does not establish trust.
Prefer a chain trusted by the intended clients over a standalone self-signed service certificate. A private CA can serve controlled clients when its trust anchor is distributed securely. A self-signed root is commonly itself a trust anchor. Its trusted status comes from the client's configuration, not merely from the certificate's self-signature. On RHEL 9, approved system-wide trust anchors can be added under /etc/pki/ca-trust/source/anchors/ and processed with update-ca-trust extract. Debian's update-ca-certificates includes suitable local PEM certificates with a .crt suffix under /usr/local/share/ca-certificates/. An application may use its own trust store.
For OpenSSL 3, inspect an existing CSR using openssl req -in server.csr -text -noout and inspect the issued certificate with openssl x509 -in server.crt -noout -dates -issuer -ext subjectAltName. To verify that the issued certificate contains the public key for a specified private key, compare public components after both commands succeed:
openssl x509 -in server.crt -pubkey -noout > cert-public.pem
openssl pkey -in server.key -pubout -out key-public.pem
cmp cert-public.pem key-public.pem
For the supported key types in this workflow, matching public-key encodings establish the pair relationship. The files contain public components. Use separate certificate chain and hostname verification to establish peer trust, as in the TLS diagnostics in the security troubleshooting section.
In OpenSSH, enabling the public-key method still leaves other accepted methods available unless effective policy restricts them. For a supported SSH daemon with a public-key-only requirement, use AuthenticationMethods publickey with PubkeyAuthentication yes, PasswordAuthentication no, and KbdInteractiveAuthentication no as applicable. PermitRootLogin no independently forbids direct SSH root access. Check the effective settings for the target connection, including Match rules, and validate an approved key-based session before closing existing access.
WireGuard
WireGuard carries IP packets through an encrypted UDP tunnel. Each interface has a private key, and the configuration associates peer public keys with allowed tunnel addresses or prefixes. AllowedIPs selects peers for outgoing destinations and validates inner source addresses on incoming packets. The peer endpoint specifies where encapsulated packets can be sent. The host still needs appropriate routes and firewall policy.
In a new protected lab directory, run wg genkey with a restrictive umask before storing the output, then derive the public key with wg pubkey reading that file. Exchange only public keys through an authenticated channel. wg-quick can manage an interface on supported installations. For example, distinct peers can use 10.20.0.1/24 and 10.20.0.2/24 internally. A specific peer entry for the second host can select 10.20.0.2/32 without routing all traffic to that peer. Use wg show to inspect endpoint, handshake and transfer details and verify the intended traffic path.
- SHA standard, HMAC, Argon2, and GnuPG commands
- Cryptsetup formatting, adding a key, and systemd crypttab
- TLS deprecation, TLS service identity, OpenSSL x509, and LibreSSL
- OpenSSH server configuration and WireGuard cryptokey routing
- Let’s Encrypt issuance, RHEL 9 system-wide trust, and Debian local CA trust
On a systemd Linux host, an administrator wants boot to open an existing encrypted volume as /dev/mapper/labdata. Which file declares the mapping name and encrypted source, with optional key source and options?
/etc/fstab
/etc/ssh/sshd_config
/etc/security/faillock.conf
/etc/crypttab
In a disposable lab, ./luks-lab.img is an unformatted image and ./lab-admin.key is an existing protected key file. You have the initial passphrase and a free keyslot will be available. Which command sequence initializes LUKS2 and adds the key file while retaining the initial credential?
Run cryptsetup luksFormat --type luks2 ./luks-lab.img, then cryptsetup luksAddKey ./luks-lab.img ./lab-admin.key.
Run cryptsetup luksOpen ./luks-lab.img labdata, then cryptsetup luksClose labdata.
Run cryptsetup luksFormat --type luks2 ./luks-lab.img, then cryptsetup luksDump ./luks-lab.img.
Run cryptsetup luksFormat --type luks2 ./luks-lab.img twice, using a different credential each time.
On an OpenSSH server, a policy requires a public key for each eligible SSH login and prohibits direct root login. Which effective settings meet the policy, including separate password and keyboard-interactive controls?
PermitRootLogin no, PubkeyAuthentication yes, PasswordAuthentication no, KbdInteractiveAuthentication yes, AuthenticationMethods any
PermitRootLogin prohibit-password, PubkeyAuthentication yes, PasswordAuthentication no, KbdInteractiveAuthentication no, AuthenticationMethods publickey
PermitRootLogin no, PubkeyAuthentication yes, PasswordAuthentication no, KbdInteractiveAuthentication no, AuthenticationMethods publickey
PermitRootLogin no, PubkeyAuthentication yes, PasswordAuthentication no, KbdInteractiveAuthentication no, AuthenticationMethods password,publickey
Sections you finish are checked off in the contents.