|
Packit Service |
310c69 |
/*
|
|
Packit Service |
310c69 |
* Copyright (c) 2020 Red Hat, Inc.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* This program is free software; you can redistribute it and/or
|
|
Packit Service |
310c69 |
* modify it under the terms of the GNU General Public License
|
|
Packit Service |
310c69 |
* as published by the Free Software Foundation; either version 2
|
|
Packit Service |
310c69 |
* of the License, or (at your option) any later version.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* This program is distributed in the hope that it will be useful,
|
|
Packit Service |
310c69 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
310c69 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit Service |
310c69 |
* GNU General Public License for more details.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* You should have received a copy of the GNU General Public License
|
|
Packit Service |
310c69 |
* along with this program; if not, write to the Free Software
|
|
Packit Service |
310c69 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
Packit Service |
310c69 |
* 02110-1301, USA.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* $Id: //eng/uds-releases/jasper/src/uds/nonce.c#3 $
|
|
Packit Service |
310c69 |
*/
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
#include "nonce.h"
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
#include "murmur/MurmurHash3.h"
|
|
Packit Service |
310c69 |
#include "numeric.h"
|
|
Packit Service |
310c69 |
#include "random.h"
|
|
Packit Service |
310c69 |
#include "stringUtils.h"
|
|
Packit Service |
310c69 |
#include "timeUtils.h"
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*****************************************************************************/
|
|
Packit Service |
310c69 |
static uint64_t hashStuff(uint64_t start, const void *data, size_t len)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
uint32_t seed = start ^ (start >> 27);
|
|
Packit Service |
310c69 |
byte hashBuffer[16];
|
|
Packit Service |
310c69 |
MurmurHash3_x64_128(data, len, seed, hashBuffer);
|
|
Packit Service |
310c69 |
return getUInt64LE(hashBuffer + 4);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*****************************************************************************/
|
|
Packit Service |
310c69 |
static void *memput(void *buf, void *end, const void *data, size_t len)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
byte *bp = buf;
|
|
Packit Service |
310c69 |
byte *be = end;
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
size_t chunk = minSizeT(len, be - bp);
|
|
Packit Service |
310c69 |
memcpy(bp, data, chunk);
|
|
Packit Service |
310c69 |
return bp + chunk;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*****************************************************************************/
|
|
Packit Service |
310c69 |
size_t createUniqueNonceData(byte *buffer, size_t length)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
AbsTime now = currentTime(CLOCK_REALTIME);
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
byte *be = buffer + length;
|
|
Packit Service |
310c69 |
byte *bp = memput(buffer, be, &now, sizeof(now));
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
uint32_t rand = randomInRange(1, (1<<30) - 1);
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
bp = memput(bp, be, &rand, sizeof(rand));
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
while (bp < be) {
|
|
Packit Service |
310c69 |
size_t n = minSizeT(be - bp, bp - buffer);
|
|
Packit Service |
310c69 |
memcpy(bp, buffer, n);
|
|
Packit Service |
310c69 |
bp += n;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
return bp - buffer;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*****************************************************************************/
|
|
Packit Service |
310c69 |
uint64_t generateMasterNonce(const void *data, size_t len)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
return hashStuff(0xa1b1e0fc, data, len);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*****************************************************************************/
|
|
Packit Service |
310c69 |
uint64_t generateSecondaryNonce(uint64_t nonce,
|
|
Packit Service |
310c69 |
const void *data,
|
|
Packit Service |
310c69 |
size_t len)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
return hashStuff(nonce + 1, data, len);
|
|
Packit Service |
310c69 |
}
|