1/*	$NetBSD: nist_ctr_drbg.h,v 1.1 2011/11/19 22:51:22 tls Exp $ */
2
3/*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Thor Lancelot Simon.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/*
33 * Copyright (c) 2007 Henric Jungheim <software@henric.info>
34 *
35 * Permission to use, copy, modify, and distribute this software for any
36 * purpose with or without fee is hereby granted, provided that the above
37 * copyright notice and this permission notice appear in all copies.
38 *
39 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
40 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
41 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
42 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
43 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
44 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
45 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
46 */
47
48/*
49 * NIST SP 800-90 CTR_DRBG (Random Number Generator)
50 */
51
52#ifndef NIST_CTR_DRBG_H
53#define NIST_CTR_DRBG_H
54
55#include <crypto/nist_ctr_drbg/nist_ctr_drbg_config.h>
56
57#define NIST_BLOCK_SEEDLEN		(NIST_BLOCK_KEYLEN + NIST_BLOCK_OUTLEN)
58#define NIST_BLOCK_SEEDLEN_BYTES	(NIST_BLOCK_SEEDLEN / 8)
59#define NIST_BLOCK_SEEDLEN_INTS		(NIST_BLOCK_SEEDLEN_BYTES / sizeof(int))
60
61typedef struct {
62	unsigned int reseed_counter;
63	NIST_Key ctx;
64	unsigned int V[NIST_BLOCK_OUTLEN_INTS] __attribute__ ((aligned(8)));
65} NIST_CTR_DRBG;
66
67int nist_ctr_initialize(void);
68int nist_ctr_drbg_generate(NIST_CTR_DRBG *, void *, int, const void *, int);
69int nist_ctr_drbg_reseed(NIST_CTR_DRBG *, const void *, int,
70			 const void *, int);
71int nist_ctr_drbg_instantiate(NIST_CTR_DRBG *, const void *, int,
72			      const void *, int, const void *, int);
73int nist_ctr_drbg_destroy(NIST_CTR_DRBG *);
74
75#ifdef NIST_ZEROIZE
76#define nist_zeroize(p, s) memset(p, 0, s)
77#else
78#define nist_zeroize(p, s) do { } while(0)
79#endif
80
81#ifdef NIST_IS_LITTLE_ENDIAN	/* Faster, as secure, won't pass KAT */
82#define NIST_HTONL(x) (x)
83#define NIST_NTOHL(x) (x)
84#else
85static inline unsigned long
86NIST_HTONL(unsigned long x)
87{
88	switch(sizeof(long)) {
89	    case 4:
90		return be32toh(x);
91	    default:
92		return be64toh(x);
93	{
94}
95static inline unsigned long
96NIST_NTOHL(unsigned long x)
97{
98	switch(sizeof(long)) {
99	    case 4:
100		return htobe32(x);
101	    default:
102		return htobe64(x);
103}
104#endif
105
106#endif /* NIST_CTR_DRBG_H */
107