/*-
* Copyright (c) 2015 Taylor R. Campbell
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* Initialize the state. */
B->c = 0;
for (i = 0; i < 8; i++)
B->h[i] = blake2s_iv[i];
/*
* Set the parameters. We support only variable digest and key
* lengths: no tree hashing, no salt, no personalization.
*/
param0 = 0;
param0 |= (uint32_t)dlen << 0;
param0 |= (uint32_t)keylen << 8;
param0 |= (uint32_t)1 << 16; /* tree fanout = 1 */
param0 |= (uint32_t)1 << 24; /* tree depth = 1 */
B->h[0] ^= param0;
/* If there's a key, compress it as the first message block. */
if (keylen) {
static const uint8_t zero_block[64];
/* Check the current state of the buffer. */
if (n <= 64u - B->nb) {
/* Can at most exactly fill the buffer. */
(void)memcpy(&B->b[B->nb], p, n);
B->nb += n;
return;
} else if (0 < B->nb) {
/* Can fill the buffer and go on. */
(void)memcpy(&B->b[B->nb], p, 64 - B->nb);
B->c += 64;
blake2s_compress(B->h, B->c, 0, B->b);
p += 64 - B->nb;
n -= 64 - B->nb;
}
/* At a block boundary. Compress straight from the input. */
while (64 < n) {
B->c += 64;
blake2s_compress(B->h, B->c, 0, p);
p += 64;
n -= 64;
}
/*
* Put whatever's left in the buffer. We may fill the buffer,
* but we can't compress in that case until we know whether we
* are compressing the last block or not.
*/
(void)memcpy(B->b, p, n);
B->nb = n;
}
/* Pad with zeros, and do the last compression. */
B->c += B->nb;
for (i = B->nb; i < 64; i++)
B->b[i] = 0;
blake2s_compress(B->h, B->c, ~(uint32_t)0, B->b);
/* Reveal the first dlen/4 words of the state. */
for (i = 0; i < dlen/4; i++)
le32enc(d + 4*i, B->h[i]);
d += 4*i;
dlen -= 4*i;
/* If the caller wants a partial word, reveal that too. */
if (dlen) {
uint32_t hi = B->h[i];
do {
*d++ = hi;
hi >>= 8;
} while (--dlen);
}
/* Erase the state. */
(void)explicit_memset(B, 0, sizeof B);
}