aboutsummaryrefslogtreecommitdiffstats
path: root/libparc/parc/security/parc_SecureRandom.h
blob: e5c67fc3b46957493e4cebfcaff2b86e2c5677bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


/**
 * @file parc_SecureRandom.h
 * @brief A cryptographically secure pseudorandom number generator that reads
 * from a secure randomness source on the system, e.g., /dev/urandom.
 *
 */
#ifndef Libparc_parc_SecureRandom
#define Libparc_parc_SecureRandom

#include <stdbool.h>

#include <parc/algol/parc_Object.h>

struct parc_securerandom;
typedef struct parc_securerandom PARCSecureRandom;

/**
 * Increase the number of references to a `PARCSecureRandom` instance.
 *
 * Note that new `PARCSecureRandom` is not created,
 * only that the given `PARCSecureRandom` reference count is incremented.
 * Discard the reference by invoking `parcSecureRandom_Release`.
 *
 * @param [in] instance A pointer to a valid PARCSecureRandom instance.
 *
 * @return The same value as @p instance.
 *
 * Example:
 * @code
 * {
 *     PARCSecureRandom *a = parcSecureRandom_Create();
 *
 *     PARCSecureRandom *b = parcSecureRandom_Acquire();
 *
 *     parcSecureRandom_Release(&a);
 *     parcSecureRandom_Release(&b);
 * }
 * @endcode
 */
PARCSecureRandom *parcSecureRandom_Acquire(const PARCSecureRandom *instance);

/**
 * Create an instance of PARCSecureRandom
 *
 * @return non-NULL A pointer to a valid PARCSecureRandom instance.
 * @return NULL An error occurred.
 *
 * Example:
 * @code
 * {
 *     PARCSecureRandom *rng = parcSecureRandom_Create();
 *
 *     parcSecureRandom_Release(&rng);
 * }
 * @endcode
 */
PARCSecureRandom *parcSecureRandom_Create(void);

/**
 * Create an instance of PARCSecureRandom with a specific seed stored in
 * a `PARCBuffer` instance.
 *
 * @param [in] seed A `PARCBuffer` instance.
 *
 * @return non-NULL A pointer to a valid PARCSecureRandom instance.
 * @return NULL An error occurred.
 *
 * Example:
 * @code
 * {
 *     PARCBuffer *seed = ...
 *     PARCSecureRandom *rng = parcSecureRandom_CreateWithSeed(seed);
 *
 *     parcSecureRandom_Release(&rng);
 * }
 * @endcode
 */
PARCSecureRandom *parcSecureRandom_CreateWithSeed(PARCBuffer *seed);

/**
 * Generate a 32-bit unsigned integer from a `PARCSecureRandom` instance.
 *
 * @param [in] rng A `PARCSecureRandom` instance.
 *
 * @return non-NULL A pointer to a valid PARCSecureRandom instance.
 * @return NULL An error occurred.
 *
 * Example:
 * @code
 * {
 *     PARCSecureRandom *rng = parcSecureRandom_Create();
 *
 *     uint32_t nextWord = parcSecureRandom_Next(rng);
 *     // use the word
 * }
 * @endcode
 */
uint32_t parcSecureRandom_Next(PARCSecureRandom *rng);

/**
 * Fill a `PARCBuffer` instance with random bytes from a `PARCSecureRandom` instance.
 * The resultant `PARCBuffer` will be ready for reading, i.e., one does not need
 * to call `parcBuffer_Flip()` on the result.
 *
 * @param [in] rng A `PARCSecureRandom` instance.
 * @param [in] buffer A `PARCBuffer` instance to fill.
 *
 * @return non-NULL A pointer to a valid PARCSecureRandom instance.
 * @return NULL An error occurred.
 *
 * Example:
 * @code
 * {
 *     PARCSecureRandom *rng = parcSecureRandom_Create();
 *
 *     PARCBuffer *buffer = parcBuffer_Allocate(32);
 *     ssize_t numBytes = parcSecureRandom_NextBytes(rng, buffer);
 *     // use the buffer
 * }
 * @endcode
 */
ssize_t parcSecureRandom_NextBytes(PARCSecureRandom *rng, PARCBuffer *buffer);

/**
 * Determine if an instance of `PARCSecureRandom` is valid.
 *
 * Valid means the internal state of the type is consistent with its required current or future behaviour.
 * This may include the validation of internal instances of types.
 *
 * @param [in] instance A pointer to a valid PARCSecureRandom instance.
 *
 * @return true The instance is valid.
 * @return false The instance is not valid.
 *
 * Example:
 * @code
 * {
 *     PARCSecureRandom *rng = parcSecureRandom_Create();
 *
 *     if (parcSecureRandom_IsValid(rng)) {
 *         printf("Instance is valid.\n");
 *     }
 *
 *     parcSecureRandom_Release(&rng);
 * }
 * @endcode
 *
 */
bool parcSecureRandom_IsValid(const PARCSecureRandom *instance);

/**
 * Release a previously acquired reference to the given `PARCSecureRandom` instance,
 * decrementing the reference count for the instance.
 *
 * The pointer to the instance is set to NULL as a side-effect of this function.
 *
 * If the invocation causes the last reference to the instance to be released,
 * the instance is deallocated and the instance's implementation will perform
 * additional cleanup and release other privately held references.
 *
 * @param [in,out] instancePtr A pointer to a pointer to the instance to release.
 *
 * Example:
 * @code
 * {
 *     PARCSecureRandom *rng = parcSecureRandom_Create();
 *
 *     parcSecureRandom_Release(&rng);
 * }
 * @endcode
 */
void parcSecureRandom_Release(PARCSecureRandom **instancePtr);
#endif