|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* Copyright 2005-2016 The OpenSSL Project Authors. All Rights Reserved.
|
|
Packit Service |
084de1 |
*
|
|
Packit Service |
084de1 |
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
Packit Service |
084de1 |
* this file except in compliance with the License. You can obtain a copy
|
|
Packit Service |
084de1 |
* in the file LICENSE in the source distribution or at
|
|
Packit Service |
084de1 |
* https://www.openssl.org/source/license.html
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/**
|
|
Packit Service |
084de1 |
* The Whirlpool hashing function.
|
|
Packit Service |
084de1 |
*
|
|
Packit Service |
084de1 |
* See
|
|
Packit Service |
084de1 |
* P.S.L.M. Barreto, V. Rijmen,
|
|
Packit Service |
084de1 |
* ``The Whirlpool hashing function,''
|
|
Packit Service |
084de1 |
* NESSIE submission, 2000 (tweaked version, 2001),
|
|
Packit Service |
084de1 |
* <https://www.cosic.esat.kuleuven.ac.be/nessie/workshop/submissions/whirlpool.zip>
|
|
Packit Service |
084de1 |
*
|
|
Packit Service |
084de1 |
* Based on "@version 3.0 (2003.03.12)" by Paulo S.L.M. Barreto and
|
|
Packit Service |
084de1 |
* Vincent Rijmen. Lookup "reference implementations" on
|
|
Packit Service |
084de1 |
* <http://planeta.terra.com.br/informatica/paulobarreto/>
|
|
Packit Service |
084de1 |
*
|
|
Packit Service |
084de1 |
* =============================================================================
|
|
Packit Service |
084de1 |
*
|
|
Packit Service |
084de1 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS
|
|
Packit Service |
084de1 |
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
Packit Service |
084de1 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
Packit Service |
084de1 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE
|
|
Packit Service |
084de1 |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
Packit Service |
084de1 |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
Packit Service |
084de1 |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
|
|
Packit Service |
084de1 |
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
Packit Service |
084de1 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
|
Packit Service |
084de1 |
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
|
Packit Service |
084de1 |
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
Packit Service |
084de1 |
*
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* OpenSSL-specific implementation notes.
|
|
Packit Service |
084de1 |
*
|
|
Packit Service |
084de1 |
* WHIRLPOOL_Update as well as one-stroke WHIRLPOOL both expect
|
|
Packit Service |
084de1 |
* number of *bytes* as input length argument. Bit-oriented routine
|
|
Packit Service |
084de1 |
* as specified by authors is called WHIRLPOOL_BitUpdate[!] and
|
|
Packit Service |
084de1 |
* does not have one-stroke counterpart.
|
|
Packit Service |
084de1 |
*
|
|
Packit Service |
084de1 |
* WHIRLPOOL_BitUpdate implements byte-oriented loop, essentially
|
|
Packit Service |
084de1 |
* to serve WHIRLPOOL_Update. This is done for performance.
|
|
Packit Service |
084de1 |
*
|
|
Packit Service |
084de1 |
* Unlike authors' reference implementation, block processing
|
|
Packit Service |
084de1 |
* routine whirlpool_block is designed to operate on multi-block
|
|
Packit Service |
084de1 |
* input. This is done for performance.
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
#include <openssl/crypto.h>
|
|
Packit Service |
084de1 |
#include "wp_local.h"
|
|
Packit Service |
084de1 |
#include <string.h>
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
int WHIRLPOOL_Init(WHIRLPOOL_CTX *c)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
memset(c, 0, sizeof(*c));
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
int WHIRLPOOL_Update(WHIRLPOOL_CTX *c, const void *_inp, size_t bytes)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* Well, largest suitable chunk size actually is
|
|
Packit Service |
084de1 |
* (1<<(sizeof(size_t)*8-3))-64, but below number is large enough for not
|
|
Packit Service |
084de1 |
* to care about excessive calls to WHIRLPOOL_BitUpdate...
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
size_t chunk = ((size_t)1) << (sizeof(size_t) * 8 - 4);
|
|
Packit Service |
084de1 |
const unsigned char *inp = _inp;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
while (bytes >= chunk) {
|
|
Packit Service |
084de1 |
WHIRLPOOL_BitUpdate(c, inp, chunk * 8);
|
|
Packit Service |
084de1 |
bytes -= chunk;
|
|
Packit Service |
084de1 |
inp += chunk;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
if (bytes)
|
|
Packit Service |
084de1 |
WHIRLPOOL_BitUpdate(c, inp, bytes * 8);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
void WHIRLPOOL_BitUpdate(WHIRLPOOL_CTX *c, const void *_inp, size_t bits)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
size_t n;
|
|
Packit Service |
084de1 |
unsigned int bitoff = c->bitoff,
|
|
Packit Service |
084de1 |
bitrem = bitoff % 8, inpgap = (8 - (unsigned int)bits % 8) & 7;
|
|
Packit Service |
084de1 |
const unsigned char *inp = _inp;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* This 256-bit increment procedure relies on the size_t being natural
|
|
Packit Service |
084de1 |
* size of CPU register, so that we don't have to mask the value in order
|
|
Packit Service |
084de1 |
* to detect overflows.
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
c->bitlen[0] += bits;
|
|
Packit Service |
084de1 |
if (c->bitlen[0] < bits) { /* overflow */
|
|
Packit Service |
084de1 |
n = 1;
|
|
Packit Service |
084de1 |
do {
|
|
Packit Service |
084de1 |
c->bitlen[n]++;
|
|
Packit Service |
084de1 |
} while (c->bitlen[n] == 0
|
|
Packit Service |
084de1 |
&& ++n < (WHIRLPOOL_COUNTER / sizeof(size_t)));
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
#ifndef OPENSSL_SMALL_FOOTPRINT
|
|
Packit Service |
084de1 |
reconsider:
|
|
Packit Service |
084de1 |
if (inpgap == 0 && bitrem == 0) { /* byte-oriented loop */
|
|
Packit Service |
084de1 |
while (bits) {
|
|
Packit Service |
084de1 |
if (bitoff == 0 && (n = bits / WHIRLPOOL_BBLOCK)) {
|
|
Packit Service |
084de1 |
whirlpool_block(c, inp, n);
|
|
Packit Service |
084de1 |
inp += n * WHIRLPOOL_BBLOCK / 8;
|
|
Packit Service |
084de1 |
bits %= WHIRLPOOL_BBLOCK;
|
|
Packit Service |
084de1 |
} else {
|
|
Packit Service |
084de1 |
unsigned int byteoff = bitoff / 8;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
bitrem = WHIRLPOOL_BBLOCK - bitoff; /* re-use bitrem */
|
|
Packit Service |
084de1 |
if (bits >= bitrem) {
|
|
Packit Service |
084de1 |
bits -= bitrem;
|
|
Packit Service |
084de1 |
bitrem /= 8;
|
|
Packit Service |
084de1 |
memcpy(c->data + byteoff, inp, bitrem);
|
|
Packit Service |
084de1 |
inp += bitrem;
|
|
Packit Service |
084de1 |
whirlpool_block(c, c->data, 1);
|
|
Packit Service |
084de1 |
bitoff = 0;
|
|
Packit Service |
084de1 |
} else {
|
|
Packit Service |
084de1 |
memcpy(c->data + byteoff, inp, bits / 8);
|
|
Packit Service |
084de1 |
bitoff += (unsigned int)bits;
|
|
Packit Service |
084de1 |
bits = 0;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
c->bitoff = bitoff;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
} else /* bit-oriented loop */
|
|
Packit Service |
084de1 |
#endif
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
/*-
|
|
Packit Service |
084de1 |
inp
|
|
Packit Service |
084de1 |
|
|
|
Packit Service |
084de1 |
+-------+-------+-------
|
|
Packit Service |
084de1 |
|||||||||||||||||||||
|
|
Packit Service |
084de1 |
+-------+-------+-------
|
|
Packit Service |
084de1 |
+-------+-------+-------+-------+-------
|
|
Packit Service |
084de1 |
|||||||||||||| c->data
|
|
Packit Service |
084de1 |
+-------+-------+-------+-------+-------
|
|
Packit Service |
084de1 |
|
|
|
Packit Service |
084de1 |
c->bitoff/8
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
while (bits) {
|
|
Packit Service |
084de1 |
unsigned int byteoff = bitoff / 8;
|
|
Packit Service |
084de1 |
unsigned char b;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
#ifndef OPENSSL_SMALL_FOOTPRINT
|
|
Packit Service |
084de1 |
if (bitrem == inpgap) {
|
|
Packit Service |
084de1 |
c->data[byteoff++] |= inp[0] & (0xff >> inpgap);
|
|
Packit Service |
084de1 |
inpgap = 8 - inpgap;
|
|
Packit Service |
084de1 |
bitoff += inpgap;
|
|
Packit Service |
084de1 |
bitrem = 0; /* bitoff%8 */
|
|
Packit Service |
084de1 |
bits -= inpgap;
|
|
Packit Service |
084de1 |
inpgap = 0; /* bits%8 */
|
|
Packit Service |
084de1 |
inp++;
|
|
Packit Service |
084de1 |
if (bitoff == WHIRLPOOL_BBLOCK) {
|
|
Packit Service |
084de1 |
whirlpool_block(c, c->data, 1);
|
|
Packit Service |
084de1 |
bitoff = 0;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
c->bitoff = bitoff;
|
|
Packit Service |
084de1 |
goto reconsider;
|
|
Packit Service |
084de1 |
} else
|
|
Packit Service |
084de1 |
#endif
|
|
Packit Service |
084de1 |
if (bits > 8) {
|
|
Packit Service |
084de1 |
b = ((inp[0] << inpgap) | (inp[1] >> (8 - inpgap)));
|
|
Packit Service |
084de1 |
b &= 0xff;
|
|
Packit Service |
084de1 |
if (bitrem)
|
|
Packit Service |
084de1 |
c->data[byteoff++] |= b >> bitrem;
|
|
Packit Service |
084de1 |
else
|
|
Packit Service |
084de1 |
c->data[byteoff++] = b;
|
|
Packit Service |
084de1 |
bitoff += 8;
|
|
Packit Service |
084de1 |
bits -= 8;
|
|
Packit Service |
084de1 |
inp++;
|
|
Packit Service |
084de1 |
if (bitoff >= WHIRLPOOL_BBLOCK) {
|
|
Packit Service |
084de1 |
whirlpool_block(c, c->data, 1);
|
|
Packit Service |
084de1 |
byteoff = 0;
|
|
Packit Service |
084de1 |
bitoff %= WHIRLPOOL_BBLOCK;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
if (bitrem)
|
|
Packit Service |
084de1 |
c->data[byteoff] = b << (8 - bitrem);
|
|
Packit Service |
084de1 |
} else { /* remaining less than or equal to 8 bits */
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
b = (inp[0] << inpgap) & 0xff;
|
|
Packit Service |
084de1 |
if (bitrem)
|
|
Packit Service |
084de1 |
c->data[byteoff++] |= b >> bitrem;
|
|
Packit Service |
084de1 |
else
|
|
Packit Service |
084de1 |
c->data[byteoff++] = b;
|
|
Packit Service |
084de1 |
bitoff += (unsigned int)bits;
|
|
Packit Service |
084de1 |
if (bitoff == WHIRLPOOL_BBLOCK) {
|
|
Packit Service |
084de1 |
whirlpool_block(c, c->data, 1);
|
|
Packit Service |
084de1 |
byteoff = 0;
|
|
Packit Service |
084de1 |
bitoff %= WHIRLPOOL_BBLOCK;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
if (bitrem)
|
|
Packit Service |
084de1 |
c->data[byteoff] = b << (8 - bitrem);
|
|
Packit Service |
084de1 |
bits = 0;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
c->bitoff = bitoff;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
int WHIRLPOOL_Final(unsigned char *md, WHIRLPOOL_CTX *c)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
unsigned int bitoff = c->bitoff, byteoff = bitoff / 8;
|
|
Packit Service |
084de1 |
size_t i, j, v;
|
|
Packit Service |
084de1 |
unsigned char *p;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
bitoff %= 8;
|
|
Packit Service |
084de1 |
if (bitoff)
|
|
Packit Service |
084de1 |
c->data[byteoff] |= 0x80 >> bitoff;
|
|
Packit Service |
084de1 |
else
|
|
Packit Service |
084de1 |
c->data[byteoff] = 0x80;
|
|
Packit Service |
084de1 |
byteoff++;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* pad with zeros */
|
|
Packit Service |
084de1 |
if (byteoff > (WHIRLPOOL_BBLOCK / 8 - WHIRLPOOL_COUNTER)) {
|
|
Packit Service |
084de1 |
if (byteoff < WHIRLPOOL_BBLOCK / 8)
|
|
Packit Service |
084de1 |
memset(&c->data[byteoff], 0, WHIRLPOOL_BBLOCK / 8 - byteoff);
|
|
Packit Service |
084de1 |
whirlpool_block(c, c->data, 1);
|
|
Packit Service |
084de1 |
byteoff = 0;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
if (byteoff < (WHIRLPOOL_BBLOCK / 8 - WHIRLPOOL_COUNTER))
|
|
Packit Service |
084de1 |
memset(&c->data[byteoff], 0,
|
|
Packit Service |
084de1 |
(WHIRLPOOL_BBLOCK / 8 - WHIRLPOOL_COUNTER) - byteoff);
|
|
Packit Service |
084de1 |
/* smash 256-bit c->bitlen in big-endian order */
|
|
Packit Service |
084de1 |
p = &c->data[WHIRLPOOL_BBLOCK / 8 - 1]; /* last byte in c->data */
|
|
Packit Service |
084de1 |
for (i = 0; i < WHIRLPOOL_COUNTER / sizeof(size_t); i++)
|
|
Packit Service |
084de1 |
for (v = c->bitlen[i], j = 0; j < sizeof(size_t); j++, v >>= 8)
|
|
Packit Service |
084de1 |
*p-- = (unsigned char)(v & 0xff);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
whirlpool_block(c, c->data, 1);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (md) {
|
|
Packit Service |
084de1 |
memcpy(md, c->H.c, WHIRLPOOL_DIGEST_LENGTH);
|
|
Packit Service |
084de1 |
OPENSSL_cleanse(c, sizeof(*c));
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
unsigned char *WHIRLPOOL(const void *inp, size_t bytes, unsigned char *md)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
WHIRLPOOL_CTX ctx;
|
|
Packit Service |
084de1 |
static unsigned char m[WHIRLPOOL_DIGEST_LENGTH];
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (md == NULL)
|
|
Packit Service |
084de1 |
md = m;
|
|
Packit Service |
084de1 |
WHIRLPOOL_Init(&ctx;;
|
|
Packit Service |
084de1 |
WHIRLPOOL_Update(&ctx, inp, bytes);
|
|
Packit Service |
084de1 |
WHIRLPOOL_Final(md, &ctx;;
|
|
Packit Service |
084de1 |
return md;
|
|
Packit Service |
084de1 |
}
|