]>
git.saurik.com Git - apple/xnu.git/blob - EXTERNAL_HEADERS/corecrypto/ccrng.h
5 * Created on 12/13/2010
7 * Copyright (c) 2010,2011,2013,2014,2015 Apple Inc. All rights reserved.
11 #ifndef _CORECRYPTO_CCRNG_H_
12 #define _CORECRYPTO_CCRNG_H_
14 #include <corecrypto/cc.h>
16 #define CCRNG_STATE_COMMON \
17 int (*generate)(struct ccrng_state *rng, size_t outlen, void *out);
20 @type struct ccrng_state
21 @abstract Default state structure. Do not instantiate. ccrng() returns a reference to this structure
29 @abstract Initializes an AES-CTR mode cryptographic random number generator and returns the statically-allocated rng object.
30 Getting a pointer to a ccrng has never been simpler!
31 Call this function, get an rng object and then pass the object to ccrng_generate() to generate randoms.
32 ccrng() may be called more than once. It returns pointer to the same object on all calls.
34 @result a cryptographically secure random number generator or NULL if fails
37 - It is significantly faster than using the system /dev/random
38 - FIPS Compliant: NIST SP800-80A + FIPS 140-2
39 - Seeded from the system entropy.
40 - Provides at least 128bit security if the system provide 2bit of entropy / byte.
41 - Entropy accumulation
42 - Backtracing resistance
43 - Prediction break with frequent (asynchronous) reseed
46 struct ccrng_state
*ccrng(int *error
);
49 @function ccrng_generate
50 @abstract Generate `outlen` bytes of output, stored in `out`, using ccrng_state `rng`.
52 @param rng `struct ccrng_state` representing the state of the RNG.
53 @param outlen Amount of random bytes to generate.
54 @param out Pointer to memory where random bytes are stored, of size at least `outlen`.
56 @result 0 on success and nonzero on failure.
58 #define ccrng_generate(rng, outlen, out) \
59 ((rng)->generate((struct ccrng_state *)(rng), (outlen), (out)))
62 @function ccrng_uniform
63 @abstract Generate a random value in @p [0, bound).
65 @param rng The state of the RNG.
66 @param bound The exclusive upper bound on the output.
67 @param rand A pointer to a single @p uint64_t to store the result.
69 @result Returns zero iff the operation is successful.
71 int ccrng_uniform(struct ccrng_state
*rng
, uint64_t bound
, uint64_t *rand
);
73 #endif /* _CORECRYPTO_CCRNG_H_ */