• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * This file is part of the openHiTLS project.
3  *
4  * openHiTLS is licensed under the Mulan PSL v2.
5  * You can use this software according to the terms and conditions of the Mulan PSL v2.
6  * You may obtain a copy of Mulan PSL v2 at:
7  *
8  *     http://license.coscl.org.cn/MulanPSL2
9  *
10  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11  * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12  * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13  * See the Mulan PSL v2 for more details.
14  */
15 
16 #ifndef CRYPT_ENTROPY_H
17 #define CRYPT_ENTROPY_H
18 
19 #include "hitls_build.h"
20 #ifdef HITLS_CRYPTO_ENTROPY
21 
22 #include <stdint.h>
23 #include <stdbool.h>
24 #include "crypt_types.h"
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 /**
31  * drbg1         drbg2         drbg3         drbgi
32  *   *             *             *             *
33  *         *         *         *        *
34  *                 *   *     *   *
35  *                        *
36  *                        *
37  *                   get-entropy
38  *                        *
39  *                   parent-drbg
40  *                        *
41  *                   get-entropy
42  *                        *
43  *                    seed-pool
44  *                        *
45  *                        *
46  *                 *   *    *    *
47  *           *       *        *         *
48  *     *          *             *             *
49  * hard-ES     sys-ES        hitls-ES      ES(add-in)
50  *                               *
51  *                          entropy-pool
52  *                               *
53  *                            CF/LFST
54  *                               *
55  *                               *
56  *                        *    *   *    *
57  *                 *         *       *         *
58  *          *             *             *             *
59  *     timestamp-NS   jitter-NS    interrup-NS     NS(add-in)
60  */
61 #ifdef HITLS_CRYPTO_ENTROPY_SYS
62 typedef struct ES_Entropy ENTROPY_EntropySource;
63 
64 typedef struct {
65     uint32_t algId;
66     void *md;
67 } ENTROPY_CFPara;
68 
69 /* Entropy source model APIs provided by HiTLS. */
70 
71 /* Creating an entropy source. */
72 ENTROPY_EntropySource *ENTROPY_EsNew(void);
73 
74 /* release entropy source. */
75 void ENTROPY_EsFree(ENTROPY_EntropySource *ctx);
76 
77 /* Initialize Entropy Source. */
78 int32_t ENTROPY_EsInit(ENTROPY_EntropySource *ctx);
79 
80 /* Deinitialize the entropy source. */
81 void ENTROPY_EsDeinit(ENTROPY_EntropySource *ctx);
82 
83 /* Interface for Setting the Entropy Source. */
84 int32_t ENTROPY_EsCtrl(ENTROPY_EntropySource *ctx, int32_t cmd, void *data, uint32_t len);
85 
86 /* Obtaining Entropy Data. */
87 uint32_t ENTROPY_EsEntropyGet(ENTROPY_EntropySource *ctx, uint8_t *data, uint32_t len);
88 
89 /* Collect entropy data. */
90 int32_t ENTROPY_EsEntropyGather(ENTROPY_EntropySource *es);
91 #endif
92 
93 typedef struct EntropySeedPool ENTROPY_SeedPool;
94 
95 
96 typedef uint32_t (*EntropyGet)(void *ctx, uint8_t *buf, uint32_t bufLen);
97 
98 /* create seed-pool handles */
99 ENTROPY_SeedPool *ENTROPY_SeedPoolNew(bool isCreateNullPool);
100 
101 /* Adding an entropy source */
102 int32_t ENTROPY_SeedPoolAddEs(ENTROPY_SeedPool *pool, const CRYPT_EAL_EsPara *para);
103 
104 /* Interface for releasing the seed pool */
105 void ENTROPY_SeedPoolFree(ENTROPY_SeedPool *pool);
106 
107 /* Interface for collecting entropy data */
108 uint32_t ENTROPY_SeedPoolCollect(ENTROPY_SeedPool *pool, bool isNpesUsed, uint32_t needEntropy,
109     uint8_t *data, uint32_t *len);
110 
111 /* Check whether the seed pool contains physical or non-physical entropy sources. */
112 bool ENTROPY_SeedPoolCheckState(ENTROPY_SeedPool *seedPool, bool isNpesUsed);
113 
114 /* Obtains the minimum entropy of the entropy source. */
115 uint32_t ENTROPY_SeedPoolGetMinEntropy(ENTROPY_SeedPool *seedPool);
116 
117 typedef int32_t (*ExternalConditioningFunction)(uint32_t algId, uint8_t *in, uint32_t inLen, uint8_t *out,
118     uint32_t *outLen);
119 
120 typedef struct EcfCtx {
121     uint32_t algId;
122     uint32_t outLen;
123     ExternalConditioningFunction conFunc;
124 } ENTROPY_ECFCtx;
125 
126 /**
127  * @brief Obtain full entropy bits
128  *
129  * @param ctx[IN] ecfCtx
130  * @param pool[IN] seed pool
131  * @param isNpesUsed[IN] whether the npes is available
132  * @param needEntropy[IN] the amount of entropy required
133  * @param data[OUT] data
134  * @param len[IN]  length
135  * @return  Success: CRYPT_SUCCESS
136  */
137 int32_t ENTROPY_GetFullEntropyInput(void *ctx, ENTROPY_SeedPool *pool, bool isNpesUsed, uint32_t needEntropy,
138     uint8_t *data, uint32_t len);
139 
140 #ifdef __cplusplus
141 }
142 #endif
143 
144 #endif // HITLS_CRYPTO_ENTROPY
145 
146 #endif // CRYPT_ENTROPY_H
147