VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/src/Crypto/chachaRng.h
blob: a2cb4ce821c7dbee08f512ab7148f19c467d4322 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*	$OpenBSD: arc4random.c,v 1.54 2015/09/13 08:31:47 guenther Exp $	*/

/*
 * Copyright (c) 1996, David Mazieres <dm@uun.org>
 * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
 * Copyright (c) 2013, Markus Friedl <markus@openbsd.org>
 * Copyright (c) 2014, Theo de Raadt <deraadt@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * ChaCha based random number generator for OpenBSD.
 */

/*
 * Adapted for VeraCrypt
 */

#ifndef HEADER_Crypto_ChaChaRng
#define HEADER_Crypto_ChaChaRng

#include "chacha256.h"

#define CHACHA20RNG_KEYSZ	32
#define CHACHA20RNG_IVSZ	8
#define CHACHA20RNG_BLOCKSZ	64
#define CHACHA20RNG_RSBUFSZ	(16*CHACHA20RNG_BLOCKSZ)

#ifdef __cplusplus
extern "C" {
#endif

typedef void (*GetRandSeedFn)(unsigned char* pbRandSeed, size_t cbRandSeed);

typedef struct
{
	ChaCha256Ctx m_chachaCtx; /* ChaCha20 context */
	unsigned char m_rs_buf[CHACHA20RNG_RSBUFSZ];	/* keystream blocks */
	size_t m_rs_have;	/* valid bytes at end of rs_buf */
	size_t m_rs_count; /* bytes till reseed */
	GetRandSeedFn m_getRandSeedCallback;
} ChaCha20RngCtx;

/*  key length must be equal to 40 bytes (CHACHA20RNG_KEYSZ + CHACHA20RNG_IVSZ) */
void ChaCha20RngInit (ChaCha20RngCtx* pCtx, const unsigned char* key, GetRandSeedFn rngCallback, size_t InitialBytesToSkip);
void ChaCha20RngGetBytes (ChaCha20RngCtx* pCtx, unsigned char* buffer, size_t bufferLen);

#ifdef __cplusplus
}
#endif

#endif