--- title: "Key encapsulation and sealing to a key" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Key encapsulation and sealing to a key} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r, include = FALSE} # the vignette runs in full in GitHub CI and for the package tarball (NOT_CRAN), # on r-universe (MY_UNIVERSE), on the package website (IN_PKGDOWN) and in an # interactive session; when CRAN re-builds it in its check, none of these is set # and only this chunk runs if (!interactive() && !any(nzchar(Sys.getenv(c("NOT_CRAN", "MY_UNIVERSE", "IN_PKGDOWN"))))) { knitr::knit_exit() } knitr::opts_chunk$set(collapse = TRUE, comment = "#>") options(width = 88) library(noqtis) ``` 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. ## Key encapsulation `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. ```{r nqencap} k <- nqkeygen("ML-KEM-768") e <- nqencap(k$pk, "ML-KEM-768") identical(nqdecap(k$sk, e$ct, "ML-KEM-768"), e$ss) # 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) # 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) ``` ## Seal a message or a file to an ML-KEM key `nqkseal()`, `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. ```{r nqkseal} 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")) 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)) unlink(c(f, g, h)) ``` ## Seal a file to several keys, signed by its sender `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. ```{r nqboxto} 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)) unlink(c(f, g, h)) ```