Skip to content

Commit

Permalink
secretbase 1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Jul 22, 2024
1 parent 106499b commit 825b313
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 59 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, Extendable-Output and Base64 Functions
Version: 1.0.0.9000
Version: 1.0.1
Description: Fast and memory-efficient streaming hash functions and base64
encoding and decoding. Performs direct hashing of strings and raw vectors.
Stream hashes files potentially larger than memory, as well as in-memory
Expand Down
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# secretbase 1.0.0.9000 (development)
# secretbase 1.0.1

* Improved error message if argument 'convert' is not of logical type.

Expand Down
24 changes: 12 additions & 12 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ Performs direct hashing of strings and raw vectors. Stream hashes files potentia

Implementations include the SHA-256, SHA-3 and 'Keccak' cryptographic hash functions, SHAKE256 extendable-output function (XOF), and 'SipHash' pseudo-random function.

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). SHA-3 is based on the Keccak algorithm, designed by G. Bertoni, J. Daemen, M. Peeters and G. Van Assche.

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>

### Overview

```{r secretbase}
Expand All @@ -68,7 +62,7 @@ Character strings and raw vectors are hashed directly (as per the above).
All other objects are stream hashed using R serialization

- memory-efficient as performed 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)
- portable as always uses R serialization version 3, big-endian representation, skipping headers (which contain R version and native encoding information)

```{r streaming}
sha3(data.frame(a = 1, b = 2), bits = 224L)
Expand All @@ -94,7 +88,7 @@ Specify 'convert' as `NA` (and 'bits' as `32` for a single integer value):
shake256("秘密の基地の中", bits = 32L, 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>
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>[1]</sup>

#### Keccak

Expand All @@ -116,7 +110,7 @@ sha256("secret base", key = "秘密の基地の中")
#### SipHash

SipHash-1-3 is optimized for performance. <br />
Pass a character string or raw vector to 'key' - up to 16 bytes (128 bits) of the key data is used:
Pass a character string or raw vector of up to 16 bytes (128 bits) to 'key':
```{r siphash}
siphash13("secret base", key = charToRaw("秘密の基地の中"))
```
Expand Down Expand Up @@ -153,17 +147,23 @@ The current development version is available from R-universe:
install.packages("secretbase", repos = "https://shikokuchuo.r-universe.dev")
```

### Implementation Notes
### Implementation

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). SHA-3 is based on the Keccak algorithm, designed by G. Bertoni, J. Daemen, M. Peeters and G. Van Assche.

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 SHA-256, SHA-3, Keccak, and base64 implementations are based on those by the 'Mbed TLS' Trusted Firmware Project at <https://www.trustedfirmware.org/projects/mbed-tls>.

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>[2]</sup>

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>.

### References

[1] Jean-Philippe Aumasson and Daniel J. Bernstein (2012), *"SipHash: a fast short-input PRF"*, Paper 2012/351, Cryptology ePrint Archive, <https://ia.cr/2012/351>.
[1] Pierre L’Ecuyer, David Munger, Boris Oreshkin and Richard Simard (2017), *"Random numbers for parallel computers: Requirements and methods, with emphasis on GPUs"*, Mathematics and Computers in Simulation, Vol. 135, May 2017, pp. 3-17 [doi:10.1016/j.matcom.2016.05.00](https://doi.org/10.1016/j.matcom.2016.05.005).

[2] Pierre L’Ecuyer, David Munger, Boris Oreshkin and Richard Simard (2017), *"Random numbers for parallel computers: Requirements and methods, with emphasis on GPUs"*, Mathematics and Computers in Simulation, Vol. 135, May 2017, pp. 3-17 [doi:10.1016/j.matcom.2016.05.00](https://doi.org/10.1016/j.matcom.2016.05.005).
[2] Jean-Philippe Aumasson and Daniel J. Bernstein (2012), *"SipHash: a fast short-input PRF"*, Paper 2012/351, Cryptology ePrint Archive, <https://ia.cr/2012/351>.

### Links

Expand Down
45 changes: 22 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,6 @@ Implementations include the SHA-256, SHA-3 and ‘Keccak’ cryptographic
hash functions, SHAKE256 extendable-output function (XOF), and ‘SipHash’
pseudo-random function.

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).
SHA-3 is based on the Keccak algorithm, designed by G. Bertoni, J.
Daemen, M. Peeters and G. Van Assche.

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>

### Overview

``` r
Expand Down Expand Up @@ -78,7 +65,7 @@ All other objects are stream hashed using R serialization

- memory-efficient as performed without allocation of the serialized
object
- portable as always uses R serialization version 3 big-endian
- portable as always uses R serialization version 3, big-endian
representation, skipping headers (which contain R version and native
encoding information)

Expand Down Expand Up @@ -115,7 +102,7 @@ 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>
reproducibility. <sup>\[1\]</sup>

#### Keccak

Expand All @@ -141,8 +128,7 @@ sha256("secret base", key = "秘密の基地の中")
#### SipHash

SipHash-1-3 is optimized for performance. <br /> Pass a character string
or raw vector to ‘key’ - up to 16 bytes (128 bits) of the key data is
used:
or raw vector of up to 16 bytes (128 bits) to ‘key’:

``` r
siphash13("secret base", key = charToRaw("秘密の基地の中"))
Expand Down Expand Up @@ -192,12 +178,25 @@ The current development version is available from R-universe:
install.packages("secretbase", repos = "https://shikokuchuo.r-universe.dev")
```

### Implementation Notes
### Implementation

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).
SHA-3 is based on the Keccak algorithm, designed by G. Bertoni, J.
Daemen, M. Peeters and G. Van Assche.

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 SHA-256, SHA-3, Keccak, and base64 implementations are based on
those by the ‘Mbed TLS’ Trusted Firmware Project at
<https://www.trustedfirmware.org/projects/mbed-tls>.

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>\[2\]</sup>

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
Expand All @@ -206,16 +205,16 @@ Aumasson and Daniel J. Bernstein released to the public domain at

### References

\[1\] Jean-Philippe Aumasson and Daniel J. Bernstein (2012), *“SipHash:
a fast short-input PRF”*, Paper 2012/351, Cryptology ePrint Archive,
<https://ia.cr/2012/351>.

\[2\] Pierre L’Ecuyer, David Munger, Boris Oreshkin and Richard Simard
\[1\] Pierre L’Ecuyer, David Munger, Boris Oreshkin and Richard Simard
(2017), *“Random numbers for parallel computers: Requirements and
methods, with emphasis on GPUs”*, Mathematics and Computers in
Simulation, Vol. 135, May 2017, pp. 3-17
[doi:10.1016/j.matcom.2016.05.00](https://doi.org/10.1016/j.matcom.2016.05.005).

\[2\] Jean-Philippe Aumasson and Daniel J. Bernstein (2012), *“SipHash:
a fast short-input PRF”*, Paper 2012/351, Cryptology ePrint Archive,
<https://ia.cr/2012/351>.

### Links

◈ secretbase R package: <https://shikokuchuo.net/secretbase/>
Expand Down
20 changes: 10 additions & 10 deletions src/base.c
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ static SEXP rawToChar(const unsigned char *buf, const size_t sz) {

}

static inline void nano_read_bytes(R_inpstream_t stream, void *dst, int len) {
static inline void sb_read_bytes(R_inpstream_t stream, void *dst, int len) {

nano_buf *buf = (nano_buf *) stream->data;
if (buf->cur + len > buf->len) Rf_error("unserialization error");
Expand All @@ -269,7 +269,7 @@ static inline void nano_read_bytes(R_inpstream_t stream, void *dst, int len) {

}

static inline void nano_write_bytes(R_outpstream_t stream, void *src, int len) {
static inline void sb_write_bytes(R_outpstream_t stream, void *src, int len) {

nano_buf *buf = (nano_buf *) stream->data;

Expand All @@ -287,7 +287,7 @@ static inline void nano_write_bytes(R_outpstream_t stream, void *src, int len) {

}

void nano_serialize(nano_buf *buf, const SEXP object) {
static void sb_serialize(nano_buf *buf, const SEXP object) {

NANO_ALLOC(buf, SB_INIT_BUFSIZE);

Expand All @@ -299,7 +299,7 @@ void nano_serialize(nano_buf *buf, const SEXP object) {
R_pstream_xdr_format,
SB_SERIAL_VER,
NULL,
nano_write_bytes,
sb_write_bytes,
NULL,
R_NilValue
);
Expand All @@ -308,7 +308,7 @@ void nano_serialize(nano_buf *buf, const SEXP object) {

}

SEXP nano_unserialize(unsigned char *buf, const size_t sz) {
static SEXP sb_unserialize(unsigned char *buf, const size_t sz) {

nano_buf nbuf;
struct R_inpstream_st input_stream;
Expand All @@ -322,7 +322,7 @@ SEXP nano_unserialize(unsigned char *buf, const size_t sz) {
(R_pstream_data_t) &nbuf,
R_pstream_xdr_format,
NULL,
nano_read_bytes,
sb_read_bytes,
NULL,
R_NilValue
);
Expand All @@ -331,7 +331,7 @@ SEXP nano_unserialize(unsigned char *buf, const size_t sz) {

}

static nano_buf nano_any_buf(const SEXP x) {
static nano_buf sb_any_buf(const SEXP x) {

nano_buf buf;

Expand All @@ -350,7 +350,7 @@ static nano_buf nano_any_buf(const SEXP x) {
}
}

nano_serialize(&buf, x);
sb_serialize(&buf, x);

resume:
return buf;
Expand All @@ -367,7 +367,7 @@ SEXP secretbase_base64enc(SEXP x, SEXP convert) {
SEXP out;
size_t olen;

nano_buf hash = nano_any_buf(x);
nano_buf hash = sb_any_buf(x);
xc = mbedtls_base64_encode(NULL, 0, &olen, hash.buf, hash.cur);
unsigned char *buf = R_Calloc(olen, unsigned char);
xc = mbedtls_base64_encode(buf, olen, &olen, hash.buf, hash.cur);
Expand Down Expand Up @@ -425,7 +425,7 @@ SEXP secretbase_base64dec(SEXP x, SEXP convert) {
out = rawToChar(buf, olen);
break;
default:
out = nano_unserialize(buf, olen);
out = sb_unserialize(buf, olen);
}

R_Free(buf);
Expand Down
12 changes: 6 additions & 6 deletions src/secret.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ static void mbedtls_sha3_finish(mbedtls_sha3_context *ctx, uint8_t *output, size

// secretbase - internals ------------------------------------------------------

static inline int nano_integer(SEXP x) {
static inline int sb_integer(SEXP x) {
int out;
switch (TYPEOF(x)) {
case INTSXP:
Expand All @@ -231,7 +231,7 @@ static inline int nano_integer(SEXP x) {
static void * (*const volatile secure_memset)(void *, int, size_t) = memset;
#endif

inline void clear_buffer(void *buf, size_t sz) {
inline void sb_clear_buffer(void *buf, size_t sz) {
#ifdef MBEDTLS_CT_ASM
memset(buf, 0, sz);
asm volatile ("" ::: "memory");
Expand Down Expand Up @@ -309,7 +309,7 @@ static void hash_object(mbedtls_sha3_context *ctx, const SEXP x) {

}

SEXP hash_to_sexp(unsigned char *buf, size_t sz, int conv) {
SEXP sb_hash_sexp(unsigned char *buf, size_t sz, int conv) {

SEXP out;
if (conv == 0) {
Expand Down Expand Up @@ -338,7 +338,7 @@ static SEXP secretbase_sha3_impl(const SEXP x, const SEXP bits, const SEXP conve

SB_ASSERT_LOGICAL(convert);
const int conv = SB_LOGICAL(convert);
const int bt = nano_integer(bits);
const int bt = sb_integer(bits);
mbedtls_sha3_id id;

if (offset < 0) {
Expand Down Expand Up @@ -369,9 +369,9 @@ static SEXP secretbase_sha3_impl(const SEXP x, const SEXP bits, const SEXP conve
mbedtls_sha3_starts(&ctx, id);
hash_func(&ctx, x);
mbedtls_sha3_finish(&ctx, buf, sz);
clear_buffer(&ctx, sizeof(mbedtls_sha3_context));
sb_clear_buffer(&ctx, sizeof(mbedtls_sha3_context));

return hash_to_sexp(buf, sz, conv);
return sb_hash_sexp(buf, sz, conv);

}

Expand Down
4 changes: 2 additions & 2 deletions src/secret.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ Rf_error("serialization exceeds max length of raw vector")
#define ERROR_FOPEN(x) Rf_error("file not found or no read permission at '%s'", x)
#define ERROR_FREAD(x) Rf_error("file read error at '%s'", x)

void clear_buffer(void *, size_t);
SEXP hash_to_sexp(unsigned char *, size_t, int);
void sb_clear_buffer(void *, size_t);
SEXP sb_hash_sexp(unsigned char *, size_t, int);

SEXP secretbase_base64enc(SEXP, SEXP);
SEXP secretbase_base64dec(SEXP, SEXP);
Expand Down
6 changes: 3 additions & 3 deletions src/secret2.c
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ static SEXP secretbase_sha256_impl(const SEXP x, const SEXP key, const SEXP conv
mbedtls_sha256_starts(&ctx);
hash_func(&ctx, x);
mbedtls_sha256_finish(&ctx, buf);
clear_buffer(&ctx, sizeof(mbedtls_sha256_context));
sb_clear_buffer(&ctx, sizeof(mbedtls_sha256_context));

} else {

Expand Down Expand Up @@ -506,11 +506,11 @@ static SEXP secretbase_sha256_impl(const SEXP x, const SEXP key, const SEXP conv
mbedtls_sha256_update(&ctx, opad, SB_SHA256_BLK);
mbedtls_sha256_update(&ctx, buf, SB_SHA256_SIZE);
mbedtls_sha256_finish(&ctx, buf);
clear_buffer(&ctx, sizeof(mbedtls_sha256_context));
sb_clear_buffer(&ctx, sizeof(mbedtls_sha256_context));

}

return hash_to_sexp(buf, SB_SHA256_SIZE, conv);
return sb_hash_sexp(buf, SB_SHA256_SIZE, conv);

}

Expand Down
2 changes: 1 addition & 1 deletion src/secret3.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ static SEXP secretbase_siphash_impl(const SEXP x, const SEXP key, const SEXP con
hash_func(&ctx, x);
hash = c_siphash_finalize(&ctx);

return hash_to_sexp((unsigned char *) &hash, SB_SIPH_SIZE, conv);
return sb_hash_sexp((unsigned char *) &hash, SB_SIPH_SIZE, conv);

}

Expand Down

0 comments on commit 825b313

Please sign in to comment.