env AGE_PQC_WARNINGS=VERBOSE
exec age-keygen -o pq.txt
exec age-keygen -pq=false -o classic.txt

# classic recipients cause warnings on encryption and decryption, which list
# them if verbose
age -e -i classic.txt -o classic.age input
stderr 'warning: the encrypted file could be decrypted by a future quantum computer \(or other ECC cryptanalysis\)'
stderr 'hint: the following recipient is not post-quantum, and relies only on Elliptic Curve Cryptography'
stderr 'hint:     age1'
stderr 'hint: see https://age-encryption.org/pq-warning for further guidance'
age -d -i classic.txt classic.age
cmp stdout input
stderr 'warning: the encrypted file could be decrypted by a future quantum computer \(or other ECC cryptanalysis\)'
stderr 'hint: the following recipient is not post-quantum, and relies only on Elliptic Curve Cryptography'
stderr 'hint:     native X25519 recipient \(age1\.\.\.\)$'
stderr 'hint: see https://age-encryption.org/pq-warning for further guidance'

age -R key_ed25519.pub -R key_rsa.pub -o ssh.age input
stderr 'warning: the encrypted file could be decrypted by a future quantum computer \(or other ECC or RSA cryptanalysis\)'
stderr 'hint: the following recipient is not post-quantum, and relies only on Elliptic Curve Cryptography'
stderr 'hint:     ssh-ed25519 SHA256:[A-Za-z0-9+/]{43}$'
stderr 'hint: the following recipient is not post-quantum, and relies only on RSA'
stderr 'hint:     ssh-rsa SHA256:[A-Za-z0-9+/]{43}$'
age -d -i key_ed25519 ssh.age
cmp stdout input
stderr 'warning: the encrypted file could be decrypted by a future quantum computer \(or other ECC or RSA cryptanalysis\)'
stderr 'hint: the following recipient is not post-quantum, and relies only on Elliptic Curve Cryptography'
stderr 'hint:     SSH Ed25519 key$'
stderr 'hint: the following recipient is not post-quantum, and relies only on RSA'
stderr 'hint:     SSH RSA key$'

age -e -i key_ed25519 -o ssh.age input
stderr 'hint:     ssh-ed25519 SHA256:[A-Za-z0-9+/]{43}$'
age -d -i key_ed25519 ssh.age
cmp stdout input
stderr 'hint:     SSH Ed25519 key$'

exec age-keygen -pq=false -o classic2.txt
exec age-keygen -y -o recipients.txt classic.txt
exec age-keygen -y -o recipients2.txt classic2.txt
age -R recipients.txt -R recipients2.txt -o classic2.age input
stderr 'hint: the following recipients are not post-quantum, and rely only on Elliptic Curve Cryptography'
age -d -i classic.txt classic2.age
cmp stdout input
stderr 'hint: the following recipients are not post-quantum, and rely only on Elliptic Curve Cryptography'
stderr 'hint:     2 native X25519 recipients \(age1\.\.\.\)$'
age -R recipients.txt -R recipients2.txt -R key_ed25519.pub -o mixed.age input
age -d -i classic.txt mixed.age
cmp stdout input
stderr 'hint: the following recipients are not post-quantum, and rely only on Elliptic Curve Cryptography'
stderr 'hint:     2 native X25519 recipients \(age1\.\.\.\)$'
stderr 'hint:     1 SSH Ed25519 key$'

# post-quantum recipients don't
age -e -i pq.txt -o pq.age input
! stderr .
age -d -i pq.txt pq.age
cmp stdout input
! stderr .

# passphrases are post-quantum, including through a plugin
env AGE_PASSPHRASE=password
age -e -j batchpass -o batchpass.age input
! stderr .
age -d -j batchpass batchpass.age
cmp stdout input
! stderr .

# plugins are post-quantum on encryption only if they say so, and unrecognized
# recipient types don't cause warnings on decryption
age -r age1test10qdmzv9q -o plugin.age input
stderr 'warning: the encrypted file could be decrypted by a future quantum computer$'
stderr 'hint: the following recipient is not known to be post-quantum'
stderr 'hint:     age1test10qdmzv9q'
age -d -i plugin-key.txt plugin.age
cmp stdout input
! stderr .

# a failed decryption doesn't cause a warning
! age -d -i pq.txt classic.age
stderr 'no identity matched any of the recipients'
! stderr warning

# the warnings can be silenced
env AGE_PQC_WARNINGS=SILENCE_I_KNOW_ITS_NOT_SAFE
age -e -i classic.txt -o classic.age input
! stderr .
age -d -i classic.txt classic.age
cmp stdout input
! stderr .

# by default, the warnings are a single line
env AGE_PQC_WARNINGS=
age -e -i classic.txt -o classic.age input
stderr '^age: warning: the encrypted file could be decrypted by a future quantum computer, see https://age-encryption.org/pq-warning$'
! stderr hint
age -d -i classic.txt classic.age
cmp stdout input
stderr '^age: warning: the encrypted file could be decrypted by a future quantum computer, see https://age-encryption.org/pq-warning$'
! stderr hint
age -r age1test10qdmzv9q -o plugin.age input
stderr '^age: warning: the encrypted file could be decrypted by a future quantum computer, see https://age-encryption.org/pq-warning$'
! stderr hint

# ALLOW_INSECURE is the default
env AGE_PQC_REQUIRED=ALLOW_INSECURE
age -e -i classic.txt -o classic.age input
stderr 'warning: the encrypted file could be decrypted'
age -d -i classic.txt classic.age
cmp stdout input
stderr 'warning: the encrypted file could be decrypted'

# ALLOW_ONLY_DECRYPT blocks encryption to classic recipients
env AGE_PQC_REQUIRED=ALLOW_ONLY_DECRYPT
! age -e -i classic.txt -o blocked.age input
stderr 'error: refusing to encrypt to recipients that are not post-quantum'
stderr 'hint: the following recipient is not post-quantum, and relies only on Elliptic Curve Cryptography'
stderr 'hint: see https://age-encryption.org/pq-warning for further guidance'
stderr 'hint: force anyway with AGE_PQC_REQUIRED=ALLOW_INSECURE'
! exists blocked.age
! age -r age1test10qdmzv9q -o blocked.age input
stderr 'error: refusing to encrypt to recipients that are not post-quantum'
! exists blocked.age
age -d -i classic.txt classic.age
cmp stdout input
stderr 'warning: the encrypted file could be decrypted'
age -e -i pq.txt -o pq.age input
! stderr .
age -e -j batchpass -o batchpass.age input
! stderr .

# YES also blocks decryption of files known not to be post-quantum
env AGE_PQC_REQUIRED=YES
! age -e -i classic.txt -o blocked.age input
stderr 'error: refusing to encrypt to recipients that are not post-quantum'
! exists blocked.age
! age -d -i classic.txt -o blocked.txt classic.age
stderr 'error: refusing to decrypt a file that is not encrypted to post-quantum recipients'
stderr 'hint: the following recipient is not post-quantum, and relies only on Elliptic Curve Cryptography'
stderr 'hint: see https://age-encryption.org/pq-warning for further guidance'
stderr 'hint: force anyway with AGE_PQC_REQUIRED=ALLOW_ONLY_DECRYPT'
! exists blocked.txt
age -d -i plugin-key.txt plugin.age
cmp stdout input
! stderr .
! age -d -i pq.txt plugins.age
stderr 'error: refusing to decrypt a file that is not encrypted to post-quantum recipients'
stderr 'hint: the following recipients are not post-quantum, and rely only on Elliptic Curve Cryptography'
stderr 'hint:     2 TPM recipients \(age1tpm1\.\.\.\)$'
stderr 'hint:     1 tlock recipient \(age1tlock1\.\.\.\)$'
stderr 'hint: the following recipient is not post-quantum, and relies only on Elliptic Curve Cryptography or RSA'
stderr 'hint:     GPG recipient \(age1gpg1\.\.\.\)$'
age -e -i pq.txt -o pq.age input
! stderr .
age -d -i pq.txt pq.age
cmp stdout input
! stderr .
age -d -j batchpass batchpass.age
cmp stdout input
! stderr .

# silencing the warnings doesn't lift the blocks
env AGE_PQC_WARNINGS=SILENCE_I_KNOW_ITS_NOT_SAFE
! age -e -i classic.txt -o blocked.age input
stderr 'error: refusing to encrypt to recipients that are not post-quantum'
! age -d -i classic.txt classic.age
stderr 'error: refusing to decrypt a file that is not encrypted to post-quantum recipients'
env AGE_PQC_WARNINGS=

# invalid values are rejected
env AGE_PQC_REQUIRED=NO
! age -e -i pq.txt -o blocked.age input
stderr 'error: invalid AGE_PQC_REQUIRED value "NO"'
! exists blocked.age
env AGE_PQC_REQUIRED=
env AGE_PQC_WARNINGS=1
! age -d -i pq.txt pq.age
stderr 'error: invalid AGE_PQC_WARNINGS value "1"'

-- input --
test
-- plugins.age --
age-encryption.org/v1
-> tpm-ecc AAAA AAAA
AAAA
-> tpm-ecc AAAA AAAA
AAAA
-> tlock 1 AAAA
AAAA
-> gpg-v1 AAAA 1
AAAA
-> mlkem768x25519 AAAA
AAAA
--- AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
-- plugin-key.txt --
AGE-PLUGIN-TEST-10Q32NLXM
-- key_ed25519 --
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
QyNTUxOQAAACB/aTuac9tiWRGrKEtixFlryYlGCPTOpdbmXN9RRmDF2gAAAKDgV/GC4Ffx
ggAAAAtzc2gtZWQyNTUxOQAAACB/aTuac9tiWRGrKEtixFlryYlGCPTOpdbmXN9RRmDF2g
AAAECvFoQXQzXgJLQ+Gz4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