Skip to content

Commit

Permalink
Add XChaCha20 stream (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
coolbutuseless authored May 9, 2024
1 parent c448ef9 commit 5bee60d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
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

0 comments on commit 5bee60d

Please sign in to comment.