Skip to content

Commit b56bb3b

Browse files
committed
sha hashes skip instead of strip serialization headers
1 parent d10d954 commit b56bb3b

File tree

8 files changed

+46
-22
lines changed

8 files changed

+46
-22
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: nanonext
22
Type: Package
33
Title: NNG (Nanomsg Next Gen) Lightweight Messaging Library
4-
Version: 0.12.0.9018
4+
Version: 0.12.0.9019
55
Description: R binding for NNG (Nanomsg Next Gen), a successor to ZeroMQ. NNG is
66
a socket library implementing 'Scalability Protocols', a reliable,
77
high-performance standard for common communications patterns including

NEWS.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# nanonext 0.12.0.9018 (development)
1+
# nanonext 0.12.0.9019 (development)
22

33
*Please note this version contains breaking behavioural changes - see updates below.*
44

@@ -7,7 +7,7 @@
77
* Default blocking behaviour of `send()` and `recv()` over Sockets and Contexts aligned to non-blocking (this change is facilitated by the introduction of synchronous context sends in NNG v1.6.0).
88
* Unserialization / decoding errors where the received message cannot be translated to the specified mode will output a message to stderr, but no longer generate a warning.
99
* `ncurl()`, `ncurl_aio()` and `ncurl_session()` now restrict 'header' and 'response' arguments to character vectors only, no longer accepting lists (for safety and performance).
10-
* SHA functions now strip the serialization header from serialized R objects. This ensures portability as the R version and native encoding information is written into the header. Note: hashes will be different to those obtained using prior versions of this package for serialized objects.
10+
* SHA functions now skip the serialization header for serialized R objects. This ensures portability as these contain R version and encoding information. Note: hashes will be different to those obtained using prior versions of this package for serialized objects.
1111
* `messenger()` specifying 'auth' now works reliably on endpoints using different R versions/platforms due to the above hashing portability fix.
1212
* `sha1()` is removed as a hash option.
1313
* Internal memory-efficiency and performance enhancements.

R/tls.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ tls_config <- function(client = NULL, server = NULL, pass = NULL, auth = is.null
9090
#' @details For arguments 'x' and 'key', a scalar string or raw vector (with no
9191
#' attributes) is hashed 'as is'.
9292
#'
93-
#' All other objects are first serialized using R serialization version 3,
94-
#' big-endian representation, with the headers stripped (for portability as
95-
#' these contain the R version number and native encoding information).
93+
#' All other objects are first serialized using R serialization v3 XDR, with
94+
#' headers skipped (for portability as these contain R version and encoding
95+
#' information).
9696
#'
9797
#' The result of hashing is always a byte sequence, which is converted to a
9898
#' character string hex representation if 'convert' is TRUE, or returned as

man/sha256.Rd

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core.c

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ SEXP mk_error(const int xc) {
4242

4343
}
4444

45-
SEXP eval_safe (void *call) {
45+
static SEXP eval_safe (void *call) {
4646
return Rf_eval((SEXP) call, R_GlobalEnv);
4747
}
4848

49-
void rl_reset(void *nothing, Rboolean jump) {
50-
if (jump && nothing == NULL)
49+
static void rl_reset(void *data, Rboolean jump) {
50+
if (jump && data == NULL)
5151
SET_TAG(nano_refHook, R_NilValue);
5252
}
5353

@@ -72,6 +72,32 @@ static void nano_write_bytes(R_outpstream_t stream, void *src, int len) {
7272

7373
}
7474

75+
static void nano_skip_bytes(R_outpstream_t stream, void *src, int len) {
76+
77+
nano_buf *buf = (nano_buf *) stream->data;
78+
if (buf->len <= NANONEXT_SERIAL_HEADERS) {
79+
buf->len--;
80+
buf->len = buf->len ? buf->len : NANONEXT_INIT_BUFSIZE;
81+
return;
82+
}
83+
84+
size_t req = buf->cur + (size_t) len;
85+
if (req > buf->len) {
86+
if (req > R_XLEN_T_MAX) {
87+
if (buf->len) R_Free(buf->buf);
88+
Rf_error("serialization exceeds max length of raw vector");
89+
}
90+
do {
91+
buf->len = buf->len * (double) (buf->len > 268435456 ? 1.2 : 2);
92+
} while (buf->len < req);
93+
buf->buf = R_Realloc(buf->buf, buf->len, unsigned char);
94+
}
95+
96+
memcpy(buf->buf + buf->cur, src, len);
97+
buf->cur += len;
98+
99+
}
100+
75101
static void nano_read_bytes(R_inpstream_t stream, void *dst, int len) {
76102

77103
nano_buf *buf = (nano_buf *) stream->data;
@@ -243,9 +269,11 @@ void nano_serialize_next(nano_buf *buf, const SEXP object) {
243269

244270
}
245271

246-
void nano_serialize_xdr(nano_buf *buf, const SEXP object) {
272+
void nano_serialize_xdr(nano_buf *buf, const SEXP object, const int skip) {
247273

248274
NANO_ALLOC(buf, NANONEXT_INIT_BUFSIZE);
275+
buf->len = skip ? NANONEXT_SERIAL_HEADERS : buf->len;
276+
249277
struct R_outpstream_st output_stream;
250278

251279
R_InitOutPStream(
@@ -254,7 +282,7 @@ void nano_serialize_xdr(nano_buf *buf, const SEXP object) {
254282
R_pstream_xdr_format,
255283
NANONEXT_SERIAL_VER,
256284
NULL,
257-
nano_write_bytes,
285+
skip ? nano_skip_bytes : nano_write_bytes,
258286
NULL,
259287
R_NilValue
260288
);

src/nanonext.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ typedef struct nano_cv_s {
117117
#include <mbedtls/sha512.h>
118118
#include <mbedtls/version.h>
119119

120-
#define NANO_SHLEN 23
121120
#define SHA224_KEY_SIZE 28
122121
#define SHA256_KEY_SIZE 32
123122
#define SHA384_KEY_SIZE 48
@@ -158,6 +157,7 @@ typedef struct nano_cv_s {
158157
#define ERROR_RET(xc) { Rf_warning("%d | %s", xc, nng_strerror(xc)); return mk_error(xc); }
159158
#define NANONEXT_INIT_BUFSIZE 8192
160159
#define NANONEXT_SERIAL_VER 3
160+
#define NANONEXT_SERIAL_HEADERS 6
161161
#define NANONEXT_LD_STRLEN 21
162162
#define NANO_ALLOC(x, sz) \
163163
(x)->buf = R_Calloc(sz, unsigned char); \
@@ -186,7 +186,7 @@ int nano_matcharg(const SEXP);
186186
int nano_matchargs(const SEXP);
187187
void nano_serialize(nano_buf *, const SEXP);
188188
void nano_serialize_next(nano_buf *, const SEXP);
189-
void nano_serialize_xdr(nano_buf *, const SEXP);
189+
void nano_serialize_xdr(nano_buf *, const SEXP, const int);
190190
SEXP nano_unserialize(unsigned char *, const size_t);
191191
SEXP rawToChar(const unsigned char *, const size_t);
192192
void dialer_finalizer(SEXP);

src/tls.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,7 @@ static nano_buf nano_any_buf(const SEXP x, const int strip) {
5757
}
5858
}
5959

60-
nano_serialize_xdr(&buf, x);
61-
if (strip) {
62-
buf.cur = buf.cur - NANO_SHLEN;
63-
memmove(buf.buf, buf.buf + NANO_SHLEN, buf.cur);
64-
}
60+
nano_serialize_xdr(&buf, x, strip);
6561

6662
return buf;
6763

tests/tests.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ nanotest(sha256("test") == "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d
518518
nanotest(length(sha256(c("one", "two"), key = "PRIVATE_key", convert = FALSE)) == 32L)
519519
nanotest(nchar(sha512(c(1.1, 2.2), as.raw(1L))) == 128L)
520520
nanotest(length(sha512("testing", convert = FALSE)) == 64L)
521-
nanotest(length(sha384(as.list(random(1e3L)), "KEY", convert = FALSE)) == 48L)
521+
nanotest(length(sha384(runif(5e3L), "KEY", convert = FALSE)) == 48L)
522522
nanotest(nchar(sha384(7000L, key = NULL, convert = TRUE)) == 96L)
523523
nanotest(length(sha224(c("one", "two"), key = "PRIVATE_key", convert = FALSE)) == 28L)
524524
nanotest(sha224(as.raw(0L)) == "fff9292b4201617bdc4d3053fce02734166a683d7d858a7f5f59b073")

0 commit comments

Comments
 (0)