Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add XChaCha20 stream #28

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export(sig_sign)
export(sig_verify)
export(simple_decrypt)
export(simple_encrypt)
export(xchacha20)
export(xsalsa20)
useDynLib(sodium,R_auth_sha256)
useDynLib(sodium,R_auth_sha512)
Expand Down Expand Up @@ -58,5 +59,6 @@ useDynLib(sodium,R_sodium_bin2hex)
useDynLib(sodium,R_sodium_hex2bin)
useDynLib(sodium,R_stream_chacha20)
useDynLib(sodium,R_stream_salsa20)
useDynLib(sodium,R_stream_xchacha20)
useDynLib(sodium,R_stream_xsalsa20)
useDynLib(sodium,R_xor)
11 changes: 11 additions & 0 deletions R/stream.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#' # Other stream ciphers
#' stream <- salsa20(10000, key, nonce8)
#' stream <- xsalsa20(10000, key, random(24))
#' stream <- xchacha20(10000, key, random(24))
#'
chacha20 <- function(size, key, nonce){
stopifnot(is.numeric(size))
Expand All @@ -56,6 +57,16 @@ chacha20 <- function(size, key, nonce){
.Call(R_stream_chacha20, size, key, nonce)
}

#' @export
#' @useDynLib sodium R_stream_xchacha20
#' @rdname stream
xchacha20 <- function(size, key, nonce){
stopifnot(is.numeric(size))
stopifnot(is.raw(key))
stopifnot(is.raw(nonce))
.Call(R_stream_xchacha20, size, key, nonce)
}

#' @export
#' @useDynLib sodium R_stream_salsa20
#' @rdname stream
Expand Down
4 changes: 4 additions & 0 deletions man/stream.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions src/stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ SEXP R_stream_chacha20(SEXP n, SEXP key, SEXP nonce){
return res;
}

SEXP R_stream_xchacha20(SEXP n, SEXP key, SEXP nonce){
if(LENGTH(key) != crypto_stream_xchacha20_KEYBYTES)
Rf_error("Invalid key, must be exactly %d bytes", crypto_stream_xchacha20_KEYBYTES);
if(LENGTH(nonce) != crypto_stream_xchacha20_NONCEBYTES)
Rf_error("Invalid nonce, must be exactly %d bytes", crypto_stream_xchacha20_NONCEBYTES);
unsigned long long clen = (unsigned long long) asReal(n);
SEXP res = allocVector(RAWSXP, clen);
crypto_stream_xchacha20(RAW(res), clen, RAW(nonce), RAW(key));
return res;
}

SEXP R_stream_salsa20(SEXP n, SEXP key, SEXP nonce){
if(LENGTH(key) != crypto_stream_salsa20_KEYBYTES)
Rf_error("Invalid key, must be exactly %d bytes", crypto_stream_salsa20_KEYBYTES);
Expand Down
Loading