Skip to content

Commit

Permalink
adds SipHash-2-4
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Apr 2, 2024
1 parent a03fa3a commit caba4ce
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 73 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: secretbase
Type: Package
Title: Cryptographic Hash and Extendable-Output Functions
Version: 0.3.0.9006
Version: 0.3.0.9007
Description: Fast and memory-efficient streaming hash functions. Performs direct
hashing of strings, raw bytes, and files potentially larger than memory, as
well as hashing in-memory objects through R's serialization mechanism,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
export(sha256)
export(sha3)
export(siphash13)
export(siphash24)
useDynLib(secretbase, .registration = TRUE)
4 changes: 2 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# secretbase 0.3.0.9006 (development)
# secretbase 0.3.0.9007 (development)

* Adds HMAC generation to `sha256()`.
* Adds SipHash-1-3 pseudo-random function (PRF) as a fast, cryptographically-strong keyed hash.
* Adds SipHash pseudo-random function (PRF) as a fast, cryptographically-strong keyed hash.

# secretbase 0.3.0.1

Expand Down
31 changes: 20 additions & 11 deletions R/base.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@

# secretbase - Main Functions --------------------------------------------------

#' Cryptographic Hashing Using the SHA-3 Algorithms
#' SHA-3 Cryptographic Hash Algorithms and SHAKE256 XOF
#'
#' Returns a SHA-3 hash of the supplied object or file.
#' Returns a SHA-3 or SHAKE256 hash of the supplied object or file.
#'
#' @param x object to hash. A character string or raw vector (without
#' attributes) is hashed 'as is'. All other objects are stream hashed using
Expand Down Expand Up @@ -99,9 +99,9 @@ sha3 <- function(x, bits = 256L, convert = TRUE, file)
if (missing(file)) .Call(secretbase_sha3, x, bits, convert) else
.Call(secretbase_sha3_file, file, bits, convert)

#' Cryptographic Hashing Using the SHA-256 Algorithm
#' SHA-256 Cryptographic Hash Algorithm
#'
#' Returns a SHA-256 hash of the supplied object or file, or an HMAC if a secret
#' Returns a SHA-256 hash of the supplied object or file, or HMAC if a secret
#' key is supplied.
#'
#' @inheritParams sha3
Expand Down Expand Up @@ -144,10 +144,12 @@ sha256 <- function(x, key = NULL, convert = TRUE, file)
if (missing(file)) .Call(secretbase_sha256, x, key, convert) else
.Call(secretbase_sha256_file, file, key, convert)

#' Hashing Using the SipHash-1-3 Pseudorandom Function
#' SipHash Pseudorandom Function
#'
#' Returns a fast, cryptographically-strong SipHash-1-3 keyed hash of the
#' supplied object or file.
#' Returns a fast, cryptographically-strong SipHash keyed hash of the supplied
#' object or file. SipHash-1-3 is optimised for performance, whereas
#' SipHash-2-4 is recommended for security. Note: SipHash is not a
#' cryptographic hash algorithm.
#'
#' @inheritParams sha3
#' @param key [default NULL] a character string or raw vector comprising the 16
Expand Down Expand Up @@ -177,11 +179,11 @@ sha256 <- function(x, key = NULL, convert = TRUE, file)
#' # SipHash-1-3 hash as raw vector:
#' siphash13("secret base", convert = FALSE)
#'
#' # SipHash-1-3 hash using a character string key:
#' siphash13("secret", key = "base")
#' # SipHash-2-4 hash using a character string key:
#' siphash24("secret", key = "base")
#'
#' # SipHash-1-3 hash using a raw vector key:
#' siphash13("secret", key = charToRaw("base"))
#' # SipHash-2-4 hash using a raw vector key:
#' siphash24("secret", key = charToRaw("base"))
#'
#' # SipHash-1-3 hash a file:
#' file <- tempfile(); cat("secret base", file = file)
Expand All @@ -193,3 +195,10 @@ sha256 <- function(x, key = NULL, convert = TRUE, file)
siphash13 <- function(x, key = NULL, convert = TRUE, file)
if (missing(file)) .Call(secretbase_siphash13, x, key, convert) else
.Call(secretbase_siphash13_file, file, key, convert)

#' @rdname siphash13
#' @export
#'
siphash24 <- function(x, key = NULL, convert = TRUE, file)
if (missing(file)) .Call(secretbase_siphash24, x, key, convert) else
.Call(secretbase_siphash24_file, file, key, convert)
21 changes: 11 additions & 10 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Implementations include the SHA-256 and SHA-3 cryptographic hash functions, SHAK

The SHA-3 Secure Hash Standard was published by the National Institute of Standards and Technology (NIST) in 2015 at [doi:10.6028/NIST.FIPS.202](https://dx.doi.org/10.6028/NIST.FIPS.202). The SHA-256 Secure Hash Standard was published by NIST in 2002 at <https://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf>. The SipHash family of pseudo-random functions by Jean-Philippe Aumasson and Daniel J. Bernstein was published in 2012 at <https://ia.cr/2012/351>.<sup>[1]</sup>

The SHA-256 and SHA-3 implementations are based on those by the 'Mbed TLS' Trusted Firmware Project at <https://www.trustedfirmware.org/projects/mbed-tls>. The SipHash-1-3 implementation is based on that of Daniele Nicolodi, David Rheinsberg and Tom Gundersen at <https://github.com/c-util/c-siphash>, which is in turn based on the reference implementation by Jean-Philippe Aumasson and Daniel J. Bernstein released to the public domain at <https://github.com/veorq/SipHash>.
The SHA-256 and SHA-3 implementations are based on those by the 'Mbed TLS' Trusted Firmware Project at <https://www.trustedfirmware.org/projects/mbed-tls>. The SipHash implementation is based on that of Daniele Nicolodi, David Rheinsberg and Tom Gundersen at <https://github.com/c-util/c-siphash>, which is in turn based on the reference implementation by Jean-Philippe Aumasson and Daniel J. Bernstein released to the public domain at <https://github.com/veorq/SipHash>.

### Installation

Expand All @@ -47,9 +47,7 @@ install.packages("secretbase", repos = "https://shikokuchuo.r-universe.dev")

### Quick Start

`secretbase` offers the functions: `sha3()`, `sha256()` and `siphash13()`.

##### SHA-3 and XOF usage:
#### SHA-3 and XOF usage:

- For the SHA-3 cryptographic hash algorithm, specify 'bits' as `224`, `256`, `384` or `512`
- For the SHAKE256 extendable-output function (XOF), specify any other bit length
Expand All @@ -65,7 +63,7 @@ sha3("秘密の基地の中", bits = 512)
```

##### Hash arbitrary R objects:
#### Hash arbitrary R objects:

- Uses memory-efficient 'streaming' serialization, without allocation of the serialized object
- Portable as always uses R serialization version 3 big-endian representation, skipping headers (which contain R version and native encoding information)
Expand All @@ -76,7 +74,7 @@ sha3(data.frame(a = 1, b = 2), bits = 160)
sha3(NULL)
```

##### Hash files:
#### Hash files:

- Performed in a streaming fashion, accepting files larger than memory

Expand All @@ -88,7 +86,7 @@ sha3(file = file)
unlink(file)
```

##### Hash to integer:
#### Hash to integer:

- Specify 'convert' as `NA` (and 'bits' as `32` for a single integer value)
- May be supplied as deterministic random seeds for R's pseudo random number generators (RNGs)
Expand All @@ -101,21 +99,24 @@ sha3("秘密の基地の中", bits = 32, convert = NA)

For use in parallel computing, this is a valid method for reducing to a negligible probability that RNGs in each process may overlap. This may be especially suitable when first-best alternatives such as using recursive streams are too expensive or unable to preserve reproducibility. <sup>[2]</sup>

##### Generating a SHA-256 HMAC:
#### Generating a SHA-256 HMAC:

- Use `sha256()` passing a character string or raw vector to 'key'.

```{r hmac}
sha256("secret base", key = "秘密の基地の中")
```

##### Using SipHash:
#### Using SipHash:

- SipHash is a fast, cryptographically-strong keyed hash. The SipHash-1-3 parameters are optimized for performance.
- SipHash is a fast, cryptographically-strong keyed hash.
- Pass a character string or raw vector to 'key'. Up to 16 bytes (128 bits) of the key data is used.
- SipHash-1-3 is optimized for performance; SipHash-2-4 recommended for security.

```{r siphash}
siphash13("secret base", key = charToRaw("秘密の基地の中"))
siphash24("secret base", key = charToRaw("秘密の基地の中"))
```

### References
Expand Down
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Daniel J. Bernstein was published in 2012 at

The SHA-256 and SHA-3 implementations are based on those by the ‘Mbed
TLS’ Trusted Firmware Project at
<https://www.trustedfirmware.org/projects/mbed-tls>. The SipHash-1-3
<https://www.trustedfirmware.org/projects/mbed-tls>. The SipHash
implementation is based on that of Daniele Nicolodi, David Rheinsberg
and Tom Gundersen at <https://github.com/c-util/c-siphash>, which is in
turn based on the reference implementation by Jean-Philippe Aumasson and
Expand All @@ -57,10 +57,7 @@ install.packages("secretbase", repos = "https://shikokuchuo.r-universe.dev")

### Quick Start

`secretbase` offers the functions: `sha3()`, `sha256()` and
`siphash13()`.

##### SHA-3 and XOF usage:
#### SHA-3 and XOF usage:

- For the SHA-3 cryptographic hash algorithm, specify ‘bits’ as `224`,
`256`, `384` or `512`
Expand All @@ -81,7 +78,7 @@ sha3("秘密の基地の中", bits = 512)
#> [1] "e30cdc73f6575c40d55b5edc8eb4f97940f5ca491640b41612e02a05f3e59dd9c6c33f601d8d7a8e2ca0504b8c22f7bc69fa8f10d7c01aab392781ff4ae1e610"
```

##### Hash arbitrary R objects:
#### Hash arbitrary R objects:

- Uses memory-efficient ‘streaming’ serialization, without allocation of
the serialized object
Expand All @@ -97,7 +94,7 @@ sha3(NULL)
#> [1] "b3e37e4c5def1bfb2841b79ef8503b83d1fed46836b5b913d7c16de92966dcee"
```

##### Hash files:
#### Hash files:

- Performed in a streaming fashion, accepting files larger than memory

Expand All @@ -107,7 +104,7 @@ sha3(file = file)
#> [1] "a721d57570e7ce366adee2fccbe9770723c6e3622549c31c7cab9dbb4a795520"
```

##### Hash to integer:
#### Hash to integer:

- Specify ‘convert’ as `NA` (and ‘bits’ as `32` for a single integer
value)
Expand All @@ -129,7 +126,7 @@ be especially suitable when first-best alternatives such as using
recursive streams are too expensive or unable to preserve
reproducibility. <sup>\[2\]</sup>

##### Generating a SHA-256 HMAC:
#### Generating a SHA-256 HMAC:

- Use `sha256()` passing a character string or raw vector to ‘key’.

Expand All @@ -138,16 +135,20 @@ sha256("secret base", key = "秘密の基地の中")
#> [1] "ec58099ab21325e792bef8f1aafc0a70e1a7227463cfc410931112705d753392"
```

##### Using SipHash:
#### Using SipHash:

- SipHash is a fast, cryptographically-strong keyed hash. The
SipHash-1-3 parameters are optimized for performance.
- SipHash is a fast, cryptographically-strong keyed hash.
- Pass a character string or raw vector to ‘key’. Up to 16 bytes (128
bits) of the key data is used.
- SipHash-1-3 is optimized for performance; SipHash-2-4 recommended for
security.

``` r
siphash13("secret base", key = charToRaw("秘密の基地の中"))
#> [1] "a1f0a751892cc7dd"

siphash24("secret base", key = charToRaw("秘密の基地の中"))
#> [1] "1bedfe817cac0562"
```

### References
Expand Down
4 changes: 2 additions & 2 deletions man/sha256.Rd

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

4 changes: 2 additions & 2 deletions man/sha3.Rd

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

19 changes: 12 additions & 7 deletions man/siphash13.Rd

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

2 changes: 2 additions & 0 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ static const R_CallMethodDef callMethods[] = {
{"secretbase_sha256_file", (DL_FUNC) &secretbase_sha256_file, 3},
{"secretbase_siphash13", (DL_FUNC) &secretbase_siphash13, 3},
{"secretbase_siphash13_file", (DL_FUNC) &secretbase_siphash13_file, 3},
{"secretbase_siphash24", (DL_FUNC) &secretbase_siphash24, 3},
{"secretbase_siphash24_file", (DL_FUNC) &secretbase_siphash24_file, 3},
{NULL, NULL, 0}
};

Expand Down
3 changes: 3 additions & 0 deletions src/secret.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ typedef struct secretbase_sha256_context {

typedef struct secretbase_siphash_context {
int skip;
unsigned N;
CSipHash *ctx;
} secretbase_siphash_context;

Expand All @@ -105,5 +106,7 @@ SEXP secretbase_sha256(SEXP, SEXP, SEXP);
SEXP secretbase_sha256_file(SEXP, SEXP, SEXP);
SEXP secretbase_siphash13(SEXP, SEXP, SEXP);
SEXP secretbase_siphash13_file(SEXP, SEXP, SEXP);
SEXP secretbase_siphash24(SEXP, SEXP, SEXP);
SEXP secretbase_siphash24_file(SEXP, SEXP, SEXP);

#endif
Loading

0 comments on commit caba4ce

Please sign in to comment.