xref: /xnu-10063.121.3/EXTERNAL_HEADERS/corecrypto/ccentropy.h (revision 2c2f96dc2b9a4408a43d3150ae9c105355ca3daa)
1*2c2f96dcSApple OSS Distributions /* Copyright (c) (2021) Apple Inc. All rights reserved.
2*2c2f96dcSApple OSS Distributions  *
3*2c2f96dcSApple OSS Distributions  * corecrypto is licensed under Apple Inc.’s Internal Use License Agreement (which
4*2c2f96dcSApple OSS Distributions  * is contained in the License.txt file distributed with corecrypto) and only to
5*2c2f96dcSApple OSS Distributions  * people who accept that license. IMPORTANT:  Any license rights granted to you by
6*2c2f96dcSApple OSS Distributions  * Apple Inc. (if any) are limited to internal use within your organization only on
7*2c2f96dcSApple OSS Distributions  * devices and computers you own or control, for the sole purpose of verifying the
8*2c2f96dcSApple OSS Distributions  * security characteristics and correct functioning of the Apple Software.  You may
9*2c2f96dcSApple OSS Distributions  * not, directly or indirectly, redistribute the Apple Software or any portions thereof.
10*2c2f96dcSApple OSS Distributions  */
11*2c2f96dcSApple OSS Distributions 
12*2c2f96dcSApple OSS Distributions #ifndef _CORECRYPTO_CCENTROPY_H_
13*2c2f96dcSApple OSS Distributions #define _CORECRYPTO_CCENTROPY_H_
14*2c2f96dcSApple OSS Distributions 
15*2c2f96dcSApple OSS Distributions #include <corecrypto/cc.h>
16*2c2f96dcSApple OSS Distributions #include <corecrypto/ccdigest.h>
17*2c2f96dcSApple OSS Distributions 
18*2c2f96dcSApple OSS Distributions // An interface to provide high-entropy seeds to RNGs.
19*2c2f96dcSApple OSS Distributions 
20*2c2f96dcSApple OSS Distributions typedef struct ccentropy_ctx ccentropy_ctx_t;
21*2c2f96dcSApple OSS Distributions 
22*2c2f96dcSApple OSS Distributions typedef int (*ccentropy_get_seed_fn_t)(ccentropy_ctx_t *ctx,
23*2c2f96dcSApple OSS Distributions                                        size_t seed_nbytes,
24*2c2f96dcSApple OSS Distributions                                        void *seed);
25*2c2f96dcSApple OSS Distributions 
26*2c2f96dcSApple OSS Distributions typedef int (*ccentropy_add_entropy_fn_t)(ccentropy_ctx_t *ctx,
27*2c2f96dcSApple OSS Distributions                                           uint32_t entropy_nsamples,
28*2c2f96dcSApple OSS Distributions                                           size_t entropy_nbytes,
29*2c2f96dcSApple OSS Distributions                                           const void *entropy);
30*2c2f96dcSApple OSS Distributions 
31*2c2f96dcSApple OSS Distributions // A descriptor for an entropy implementation.
32*2c2f96dcSApple OSS Distributions typedef struct ccentropy_info {
33*2c2f96dcSApple OSS Distributions     // This is a required function. Implementations should populate
34*2c2f96dcSApple OSS Distributions     // the seed with a full-entropy output. If they are temporarily
35*2c2f96dcSApple OSS Distributions     // unable due to insufficient entropy, they should return
36*2c2f96dcSApple OSS Distributions     // CCERR_OUT_OF_ENTROPY. If they are permanently unable they
37*2c2f96dcSApple OSS Distributions     // should return some other error (or abort).
38*2c2f96dcSApple OSS Distributions     ccentropy_get_seed_fn_t get_seed;
39*2c2f96dcSApple OSS Distributions 
40*2c2f96dcSApple OSS Distributions     // This is an optional function. The caller will provide a set of
41*2c2f96dcSApple OSS Distributions     // (potentially low-quality) entropy samples, and the
42*2c2f96dcSApple OSS Distributions     // implementation should mix these into its internal
43*2c2f96dcSApple OSS Distributions     // state. Implementations are free to omit this function if it
44*2c2f96dcSApple OSS Distributions     // does not make sense (e.g. see ccentropy_rng below).
45*2c2f96dcSApple OSS Distributions     ccentropy_add_entropy_fn_t add_entropy;
46*2c2f96dcSApple OSS Distributions } ccentropy_info_t;
47*2c2f96dcSApple OSS Distributions 
48*2c2f96dcSApple OSS Distributions // Common state for entropy implementations.
49*2c2f96dcSApple OSS Distributions struct ccentropy_ctx {
50*2c2f96dcSApple OSS Distributions     // A pointer to the descriptor.
51*2c2f96dcSApple OSS Distributions     const ccentropy_info_t *info;
52*2c2f96dcSApple OSS Distributions };
53*2c2f96dcSApple OSS Distributions 
54*2c2f96dcSApple OSS Distributions /*!
55*2c2f96dcSApple OSS Distributions   @function ccentropy_get_seed
56*2c2f96dcSApple OSS Distributions   @abstract Get a high-entropy seed.
57*2c2f96dcSApple OSS Distributions 
58*2c2f96dcSApple OSS Distributions   @param ctx The entropy context.
59*2c2f96dcSApple OSS Distributions   @param seed_nbytes The size of the seed requested.
60*2c2f96dcSApple OSS Distributions   @param seed A buffer to receive the seed.
61*2c2f96dcSApple OSS Distributions 
62*2c2f96dcSApple OSS Distributions   @return CCERR_OK on success; CCERR_OUT_OF_ENTROPY if entropy is
63*2c2f96dcSApple OSS Distributions   temporarily unavailable; some implementation-defined error (or
64*2c2f96dcSApple OSS Distributions   abort) otherwise.
65*2c2f96dcSApple OSS Distributions */
66*2c2f96dcSApple OSS Distributions int ccentropy_get_seed(ccentropy_ctx_t *ctx,
67*2c2f96dcSApple OSS Distributions                        size_t seed_nbytes,
68*2c2f96dcSApple OSS Distributions                        void *seed);
69*2c2f96dcSApple OSS Distributions 
70*2c2f96dcSApple OSS Distributions /*!
71*2c2f96dcSApple OSS Distributions   @function ccentropy_add_entropy
72*2c2f96dcSApple OSS Distributions   @abstract Add fresh entropy samples to the context.
73*2c2f96dcSApple OSS Distributions 
74*2c2f96dcSApple OSS Distributions   @param ctx The entropy context.
75*2c2f96dcSApple OSS Distributions   @param entropy_nsamples The count of samples included in this batch.
76*2c2f96dcSApple OSS Distributions   @param entropy_nbytes The size of the entropy payload in bytes.
77*2c2f96dcSApple OSS Distributions   @param entropy A buffer containing the fresh entropy samples.
78*2c2f96dcSApple OSS Distributions 
79*2c2f96dcSApple OSS Distributions   @return CCERR_OK on success; CCERR_NOT_SUPPORTED if this operation
80*2c2f96dcSApple OSS Distributions   is not supported for the implementation; some implementation-defined
81*2c2f96dcSApple OSS Distributions   error (or abort) otherwise.
82*2c2f96dcSApple OSS Distributions 
83*2c2f96dcSApple OSS Distributions   @discussion This operation is optional and will not be supported by
84*2c2f96dcSApple OSS Distributions   all implementations.
85*2c2f96dcSApple OSS Distributions */
86*2c2f96dcSApple OSS Distributions int ccentropy_add_entropy(ccentropy_ctx_t *ctx,
87*2c2f96dcSApple OSS Distributions                           uint32_t entropy_nsamples,
88*2c2f96dcSApple OSS Distributions                           size_t entropy_nbytes,
89*2c2f96dcSApple OSS Distributions                           const void *entropy);
90*2c2f96dcSApple OSS Distributions 
91*2c2f96dcSApple OSS Distributions // A simple wrapper around a ccrng instance. This implementation does
92*2c2f96dcSApple OSS Distributions // not support the add_entropy interface.
93*2c2f96dcSApple OSS Distributions typedef struct ccentropy_rng_ctx {
94*2c2f96dcSApple OSS Distributions     ccentropy_ctx_t entropy_ctx;
95*2c2f96dcSApple OSS Distributions     struct ccrng_state *rng_ctx;
96*2c2f96dcSApple OSS Distributions     size_t seed_max_nbytes;
97*2c2f96dcSApple OSS Distributions } ccentropy_rng_ctx_t;
98*2c2f96dcSApple OSS Distributions 
99*2c2f96dcSApple OSS Distributions /*!
100*2c2f96dcSApple OSS Distributions   @function ccentropy_rng_init
101*2c2f96dcSApple OSS Distributions   @abstract Wrap a ccrng instance in the ccentropy interface.
102*2c2f96dcSApple OSS Distributions 
103*2c2f96dcSApple OSS Distributions   @param ctx The entropy context.
104*2c2f96dcSApple OSS Distributions   @param rng_ctx The RNG to wrap.
105*2c2f96dcSApple OSS Distributions   @param seed_max_nbytes The maximum seed size that this RNG can provide.
106*2c2f96dcSApple OSS Distributions 
107*2c2f96dcSApple OSS Distributions   @return CCERR_OK on success.
108*2c2f96dcSApple OSS Distributions 
109*2c2f96dcSApple OSS Distributions   @discussion seed_max_nbytes should correspond to the security level
110*2c2f96dcSApple OSS Distributions   of the underlying RNG.
111*2c2f96dcSApple OSS Distributions */
112*2c2f96dcSApple OSS Distributions int ccentropy_rng_init(ccentropy_rng_ctx_t *ctx,
113*2c2f96dcSApple OSS Distributions                        struct ccrng_state *rng_ctx,
114*2c2f96dcSApple OSS Distributions                        size_t seed_max_nbytes);
115*2c2f96dcSApple OSS Distributions 
116*2c2f96dcSApple OSS Distributions // An entropy conditioner based on digest functions. We assume a fixed
117*2c2f96dcSApple OSS Distributions // per-sample entropy estimate measured in millibits
118*2c2f96dcSApple OSS Distributions // (i.e. mbits). This estimate should be determined via offline
119*2c2f96dcSApple OSS Distributions // analysis.
120*2c2f96dcSApple OSS Distributions typedef struct ccentropy_digest_ctx {
121*2c2f96dcSApple OSS Distributions     ccentropy_ctx_t entropy_ctx;
122*2c2f96dcSApple OSS Distributions     const struct ccdigest_info *digest_info;
123*2c2f96dcSApple OSS Distributions     ccdigest_ctx_decl(MAX_DIGEST_STATE_SIZE,
124*2c2f96dcSApple OSS Distributions                       MAX_DIGEST_BLOCK_SIZE,
125*2c2f96dcSApple OSS Distributions                       digest_ctx);
126*2c2f96dcSApple OSS Distributions     uint32_t entropy_mbits_per_sample;
127*2c2f96dcSApple OSS Distributions     uint32_t entropy_mbits;
128*2c2f96dcSApple OSS Distributions } ccentropy_digest_ctx_t;
129*2c2f96dcSApple OSS Distributions 
130*2c2f96dcSApple OSS Distributions #define CCENTROPY_MBITS_PER_BYTE ((uint32_t)(8000))
131*2c2f96dcSApple OSS Distributions 
132*2c2f96dcSApple OSS Distributions /*!
133*2c2f96dcSApple OSS Distributions   @function ccentropy_digest_init
134*2c2f96dcSApple OSS Distributions   @abstract Initialize a digest-based entropy conditioner.
135*2c2f96dcSApple OSS Distributions 
136*2c2f96dcSApple OSS Distributions   @param ctx The entropy context.
137*2c2f96dcSApple OSS Distributions   @param digest_info A descriptor for the digest.
138*2c2f96dcSApple OSS Distributions   @param entropy_mbits_per_sample An estimate of per-sample entropy measured in millibits.
139*2c2f96dcSApple OSS Distributions 
140*2c2f96dcSApple OSS Distributions   @return CCERR_OK on success.
141*2c2f96dcSApple OSS Distributions 
142*2c2f96dcSApple OSS Distributions   @discussion The estimated entropy per sample should be determined
143*2c2f96dcSApple OSS Distributions   via offline analysis.
144*2c2f96dcSApple OSS Distributions */
145*2c2f96dcSApple OSS Distributions int ccentropy_digest_init(struct ccentropy_digest_ctx *ctx,
146*2c2f96dcSApple OSS Distributions                           const struct ccdigest_info *digest_info,
147*2c2f96dcSApple OSS Distributions                           uint32_t entropy_mbits_per_sample);
148*2c2f96dcSApple OSS Distributions 
149*2c2f96dcSApple OSS Distributions #endif /* _CORECRYPTO_CCENTROPY_H_ */
150