1 | /* $NetBSD: cprng.h,v 1.12 2015/04/13 15:51:30 riastradh Exp $ */ |
2 | |
3 | /*- |
4 | * Copyright (c) 2011-2013 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 and Taylor R. Campbell. |
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 | * XXX Don't change this to _SYS_CPRNG_H or anything -- code outside |
34 | * this file relies on its name... (I'm looking at you, ipf!) |
35 | */ |
36 | #ifndef _CPRNG_H |
37 | #define _CPRNG_H |
38 | |
39 | #include <sys/types.h> |
40 | |
41 | #include <crypto/nist_ctr_drbg/nist_ctr_drbg.h> |
42 | #include <crypto/cprng_fast/cprng_fast.h> |
43 | |
44 | /* |
45 | * NIST SP800-90 says 2^19 bytes per request for the CTR_DRBG. |
46 | */ |
47 | #define CPRNG_MAX_LEN 524288 |
48 | |
49 | typedef struct cprng_strong cprng_strong_t; |
50 | |
51 | void cprng_init(void); |
52 | |
53 | #define CPRNG_INIT_ANY 0x00000001 |
54 | #define CPRNG_REKEY_ANY 0x00000002 |
55 | #define CPRNG_USE_CV 0x00000004 |
56 | #define CPRNG_HARD 0x00000008 |
57 | #define CPRNG_FMT "\177\020\ |
58 | b\0INIT_ANY\0\ |
59 | b\1REKEY_ANY\0\ |
60 | b\2USE_CV\0\ |
61 | b\3HARD\0" |
62 | |
63 | cprng_strong_t * |
64 | cprng_strong_create(const char *, int, int); |
65 | void cprng_strong_destroy(cprng_strong_t *); |
66 | size_t cprng_strong(cprng_strong_t *, void *, size_t, int); |
67 | |
68 | struct knote; /* XXX temp, for /dev/random */ |
69 | int cprng_strong_kqfilter(cprng_strong_t *, struct knote *); /* XXX " */ |
70 | int cprng_strong_poll(cprng_strong_t *, int); /* XXX " */ |
71 | |
72 | extern cprng_strong_t *kern_cprng; |
73 | |
74 | static inline uint32_t |
75 | cprng_strong32(void) |
76 | { |
77 | uint32_t r; |
78 | cprng_strong(kern_cprng, &r, sizeof(r), 0); |
79 | return r; |
80 | } |
81 | |
82 | static inline uint64_t |
83 | cprng_strong64(void) |
84 | { |
85 | uint64_t r; |
86 | cprng_strong(kern_cprng, &r, sizeof(r), 0); |
87 | return r; |
88 | } |
89 | |
90 | static inline unsigned int |
91 | cprng_strong_strength(cprng_strong_t *c) |
92 | { |
93 | return NIST_BLOCK_KEYLEN_BYTES; |
94 | } |
95 | |
96 | #endif /* _CPRNG_H */ |
97 | |