diff options
Diffstat (limited to '')
-rw-r--r-- | marshal.c | 8 | ||||
-rw-r--r-- | unmarshal.c | 14 |
2 files changed, 10 insertions, 12 deletions
@@ -9,7 +9,7 @@ libsha2_marshal(const struct libsha2_state *restrict state, void *restrict buf_) size_t off = 0; if (buf) - *(int *)buf = 0; /* version */ + *(int *)buf = 1; /* version */ off += sizeof(int); if (buf) *(enum libsha2_algorithm *)&buf[off] = state->algorithm; @@ -23,9 +23,6 @@ libsha2_marshal(const struct libsha2_state *restrict state, void *restrict buf_) memcpy(&buf[off], state->k.b32, sizeof(state->k.b32)); off += sizeof(state->k.b32); if (buf) - memcpy(&buf[off], state->w.b32, sizeof(state->w.b32)); - off += sizeof(state->w.b32); - if (buf) memcpy(&buf[off], state->h.b32, sizeof(state->h.b32)); off += sizeof(state->h.b32); } else { @@ -33,9 +30,6 @@ libsha2_marshal(const struct libsha2_state *restrict state, void *restrict buf_) memcpy(&buf[off], state->k.b64, sizeof(state->k.b64)); off += sizeof(state->k.b64); if (buf) - memcpy(&buf[off], state->w.b64, sizeof(state->w.b64)); - off += sizeof(state->w.b64); - if (buf) memcpy(&buf[off], state->h.b64, sizeof(state->h.b64)); off += sizeof(state->h.b64); } diff --git a/unmarshal.c b/unmarshal.c index c4b2837..8c5780d 100644 --- a/unmarshal.c +++ b/unmarshal.c @@ -7,13 +7,15 @@ libsha2_unmarshal(struct libsha2_state *restrict state, const void *restrict buf { const char *restrict buf = buf_; size_t off = 0; + int version; if (bufsize < sizeof(int) + sizeof(enum libsha2_algorithm) + sizeof(size_t)) { errno = EINVAL; return 0; } - if (*(const int *)buf) { /* version */ + version = *(const int *)buf; + if (version < 0 || version > 1) { /* version */ errno = EINVAL; return 0; } @@ -33,8 +35,9 @@ libsha2_unmarshal(struct libsha2_state *restrict state, const void *restrict buf } memcpy(state->k.b32, &buf[off], sizeof(state->k.b32)); off += sizeof(state->k.b32); - memcpy(state->w.b32, &buf[off], sizeof(state->w.b32)); - off += sizeof(state->w.b32); + memset(state->w.b32, 0, sizeof(state->w.b32)); + if (version == 0) + off += sizeof(state->w.b32); memcpy(state->h.b32, &buf[off], sizeof(state->h.b32)); off += sizeof(state->h.b32); break; @@ -49,8 +52,9 @@ libsha2_unmarshal(struct libsha2_state *restrict state, const void *restrict buf } memcpy(state->k.b64, &buf[off], sizeof(state->k.b64)); off += sizeof(state->k.b64); - memcpy(state->w.b64, &buf[off], sizeof(state->w.b64)); - off += sizeof(state->w.b64); + memset(state->w.b64, 0, sizeof(state->w.b64)); + if (version == 0) + off += sizeof(state->w.b64); memcpy(state->h.b64, &buf[off], sizeof(state->h.b64)); off += sizeof(state->h.b64); break; |