Agreeing a shared secret with ML-KEM or HQC, and sealing a message or a file so only the holder of a key can open it.
Each section is one help page: what its functions are for, and the
example of the page as it runs. ?name gives the arguments
and the value in full.
nqencap(), nqdecap():
nqencap() makes a fresh shared secret and its ciphertext
for a public key (ML-KEM, FIPS 203, or HQC); nqdecap()
recovers the shared secret from the ciphertext with the secret key.
k <- nqkeygen("ML-KEM-768")
e <- nqencap(k$pk, "ML-KEM-768")
identical(nqdecap(k$sk, e$ct, "ML-KEM-768"), e$ss)
#> [1] TRUE
# a changed ciphertext gives another secret, not an error
bad <- e$ct
bad[1] <- xor(bad[1], as.raw(1))
identical(nqdecap(k$sk, bad, "ML-KEM-768"), e$ss)
#> [1] FALSE
# HQC, a code-based KEM
h <- nqkeygen("HQC-128")
eh <- nqencap(h$pk, "HQC-128")
identical(nqdecap(h$sk, eh$ct, "HQC-128"), eh$ss)
#> [1] TRUEnqkseal(), nqkopen(), nqbox(),
nqunbox(): nqkseal() encrypts a message to an
ML-KEM public key (noqtis-kseal-1: ML-KEM, then ChaCha20-Poly1305 under
a key derived with HKDF-SHA-256); only the holder of the secret key
opens it with nqkopen(), and only with the same context.
nqbox() encrypts a file of any size the same way, in
chunks, in the format of noqtis box (noqtis-kstream-1),
holding one chunk in memory; nqunbox() opens it, writing
each chunk only once its tag verifies, and removes what it wrote when
one does not.
k <- nqkeygen("ML-KEM-768")
x <- nqkseal("a hidden list", k$pk, "ML-KEM-768", ctx = "trial 1")
rawToChar(nqkopen(x, k$sk, "ML-KEM-768", ctx = "trial 1"))
#> [1] "a hidden list"
f <- tempfile()
writeBin(as.raw(rep(0:255, 20)), f)
g <- tempfile()
h <- tempfile()
nqbox(f, g, k$pk, "ML-KEM-768", chunk = 1024)
nqunbox(g, h, k$sk, "ML-KEM-768")
identical(readBin(h, "raw", 5120), readBin(f, "raw", 5120))
#> [1] TRUE
unlink(c(f, g, h))nqboxto(), nqunboxfrom():
nqboxto() encrypts a file so that the secret key of any one
of several ML-KEM keys opens it, and with sender signs it;
nqunboxfrom() opens it with one recipient’s key and checks
who sent it. The format is the one
noqtis box -p ... -p ... -s writes
(noqtis-kstream-2); a file sealed to one key with
nqbox() is another format.
ann <- nqkeygen("ML-KEM-768")
bob <- nqkeygen("ML-KEM-1024")
me <- nqkeygen("ML-DSA-44")
f <- tempfile()
writeBin(as.raw(rep(0:255, 20)), f)
g <- tempfile()
h <- tempfile()
nqboxto(f, g, list(ann$pk, bob$pk), c("ML-KEM-768", "ML-KEM-1024"),
sender = me$sk, sender_alg = "ML-DSA-44"
)
nqunboxfrom(g, h, bob$sk, "ML-KEM-1024", sender = me$pk)
identical(readBin(h, "raw", 5120), readBin(f, "raw", 5120))
#> [1] TRUE
unlink(c(f, g, h))