|
@ -1,4 +1,4 @@ |
|
|
/* $OpenBSD: siphash.c,v 1.3 2015/02/07 05:40:59 dlg Exp $ */ |
|
|
|
|
|
|
|
|
/* $OpenBSD: siphash.c,v 1.4 2015/02/20 11:51:03 tedu Exp $ */ |
|
|
|
|
|
|
|
|
/*- |
|
|
/*- |
|
|
* Copyright (c) 2013 Andre Oppermann <andre@FreeBSD.org> |
|
|
* Copyright (c) 2013 Andre Oppermann <andre@FreeBSD.org> |
|
@ -72,8 +72,8 @@ SipHash_Init(SIPHASH_CTX *ctx, const SIPHASH_KEY *key) |
|
|
void |
|
|
void |
|
|
SipHash_Update(SIPHASH_CTX *ctx, int rc, int rf, const void *src, size_t len) |
|
|
SipHash_Update(SIPHASH_CTX *ctx, int rc, int rf, const void *src, size_t len) |
|
|
{ |
|
|
{ |
|
|
const u_int8_t *ptr = src; |
|
|
|
|
|
size_t free, used; |
|
|
|
|
|
|
|
|
const uint8_t *ptr = src; |
|
|
|
|
|
size_t left, used; |
|
|
|
|
|
|
|
|
if (len == 0) |
|
|
if (len == 0) |
|
|
return; |
|
|
return; |
|
@ -82,13 +82,13 @@ SipHash_Update(SIPHASH_CTX *ctx, int rc, int rf, const void *src, size_t len) |
|
|
ctx->bytes += len; |
|
|
ctx->bytes += len; |
|
|
|
|
|
|
|
|
if (used > 0) { |
|
|
if (used > 0) { |
|
|
free = sizeof(ctx->buf) - used; |
|
|
|
|
|
|
|
|
left = sizeof(ctx->buf) - used; |
|
|
|
|
|
|
|
|
if (len >= free) { |
|
|
|
|
|
memcpy(&ctx->buf[used], ptr, free); |
|
|
|
|
|
|
|
|
if (len >= left) { |
|
|
|
|
|
memcpy(&ctx->buf[used], ptr, left); |
|
|
SipHash_CRounds(ctx, rc); |
|
|
SipHash_CRounds(ctx, rc); |
|
|
len -= free; |
|
|
|
|
|
ptr += free; |
|
|
|
|
|
|
|
|
len -= left; |
|
|
|
|
|
ptr += left; |
|
|
} else { |
|
|
} else { |
|
|
memcpy(&ctx->buf[used], ptr, len); |
|
|
memcpy(&ctx->buf[used], ptr, len); |
|
|
return; |
|
|
return; |
|
@ -109,22 +109,22 @@ SipHash_Update(SIPHASH_CTX *ctx, int rc, int rf, const void *src, size_t len) |
|
|
void |
|
|
void |
|
|
SipHash_Final(void *dst, SIPHASH_CTX *ctx, int rc, int rf) |
|
|
SipHash_Final(void *dst, SIPHASH_CTX *ctx, int rc, int rf) |
|
|
{ |
|
|
{ |
|
|
u_int64_t r; |
|
|
|
|
|
|
|
|
uint64_t r; |
|
|
|
|
|
|
|
|
r = SipHash_End(ctx, rc, rf); |
|
|
r = SipHash_End(ctx, rc, rf); |
|
|
|
|
|
|
|
|
*(u_int64_t *)dst = htole64(r); |
|
|
|
|
|
|
|
|
*(uint64_t *)dst = htole64(r); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
u_int64_t |
|
|
|
|
|
|
|
|
uint64_t |
|
|
SipHash_End(SIPHASH_CTX *ctx, int rc, int rf) |
|
|
SipHash_End(SIPHASH_CTX *ctx, int rc, int rf) |
|
|
{ |
|
|
{ |
|
|
u_int64_t r; |
|
|
|
|
|
size_t free, used; |
|
|
|
|
|
|
|
|
uint64_t r; |
|
|
|
|
|
size_t left, used; |
|
|
|
|
|
|
|
|
used = ctx->bytes % sizeof(ctx->buf); |
|
|
used = ctx->bytes % sizeof(ctx->buf); |
|
|
free = sizeof(ctx->buf) - used; |
|
|
|
|
|
memset(&ctx->buf[used], 0, free - 1); |
|
|
|
|
|
|
|
|
left = sizeof(ctx->buf) - used; |
|
|
|
|
|
memset(&ctx->buf[used], 0, left - 1); |
|
|
ctx->buf[7] = ctx->bytes; |
|
|
ctx->buf[7] = ctx->bytes; |
|
|
|
|
|
|
|
|
SipHash_CRounds(ctx, rc); |
|
|
SipHash_CRounds(ctx, rc); |
|
@ -136,7 +136,7 @@ SipHash_End(SIPHASH_CTX *ctx, int rc, int rf) |
|
|
return (r); |
|
|
return (r); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
u_int64_t |
|
|
|
|
|
|
|
|
uint64_t |
|
|
SipHash(const SIPHASH_KEY *key, int rc, int rf, const void *src, size_t len) |
|
|
SipHash(const SIPHASH_KEY *key, int rc, int rf, const void *src, size_t len) |
|
|
{ |
|
|
{ |
|
|
SIPHASH_CTX ctx; |
|
|
SIPHASH_CTX ctx; |
|
@ -175,7 +175,7 @@ SipHash_Rounds(SIPHASH_CTX *ctx, int rounds) |
|
|
static void |
|
|
static void |
|
|
SipHash_CRounds(SIPHASH_CTX *ctx, int rounds) |
|
|
SipHash_CRounds(SIPHASH_CTX *ctx, int rounds) |
|
|
{ |
|
|
{ |
|
|
u_int64_t m = letoh64(*(u_int64_t *)ctx->buf); |
|
|
|
|
|
|
|
|
uint64_t m = le64toh(*(uint64_t *)ctx->buf); |
|
|
|
|
|
|
|
|
ctx->v[3] ^= m; |
|
|
ctx->v[3] ^= m; |
|
|
SipHash_Rounds(ctx, rounds); |
|
|
SipHash_Rounds(ctx, rounds); |
|
|