MCCI TweetNaCl
TweetNaCl library adapted for embedded use
mcci_tweetnacl.h
Go to the documentation of this file.
1 /*
2 
3 Module: mcci_tweetnacl.h
4 
5 Function:
6  Equivalent wrapper for tweetnacl.h minimizing namespace pollution
7 
8 Copyright and License:
9  This file copyright (C) 2021 by
10 
11  MCCI Corporation
12  3520 Krums Corners Road
13  Ithaca, NY 14850
14 
15  See accompanying LICENSE file for copyright and license information.
16 
17 Author:
18  Terry Moore, MCCI Corporation March 2021
19 
20 */
21 
22 #ifndef _mcci_tweetnacl_h_
23 #define _mcci_tweetnacl_h_ /* prevent multiple includes */
24 
25 #pragma once
26 
27 #include <stdbool.h>
28 #include <stdlib.h>
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 /****************************************************************************\
35 |
36 | Meta
37 |
38 \****************************************************************************/
39 
40 /// \addtogroup low-level-functions Low-level functions
41 /// @{
42 /// \addtogroup mcci-tweetnacl Framework
43 /// \addtogroup string-comparison String Comparison
44 
45 /****************************************************************************\
46 |
47 | Forward Types
48 |
49 \****************************************************************************/
50 
51 ///
52 ///
53 /// \brief abstract type for randombytes driver context
54 ///
55 /// This abstract type simply is passed through unmodified, and may be used
56 /// for any needed dynamic context for the random bytes implementation.
57 ///
58 /// \ingroup mcci-tweetnacl
59 ///
60 typedef struct mcci_tweetnacl_randombytes_driver_s *mcci_tweetnacl_randombytes_handle_t;
61 
62 ///
63 /// \brief error codes from mcci_tweetnacl_randombytes_fn_t implementations errors
64 ///
65 /// \ingroup mcci-tweetnacl
66 ///
67 typedef enum mcci_tweetnacl_randombytes_error_e
68  {
69  MCCI_TWEETNACL_RANDOMBYTES_ERROR_SUCCESS = 0, ///< success (not an error)
70  MCCI_TWEETNACL_RANDOMBYTES_ERROR_UNKNOWN = 1, ///< bad param to mcci_tweetnacl_hal_randombytes_raise()
71  MCCI_TWEETNACL_RANDOMBYTES_ERROR_NOT_INITIALIZED = 2, ///< random number driver not intialized
73  MCCI_TWEETNACL_RANDOMBYTES_ERROR_CRYPTO_API_FAILED = 4, ///< the related crypto API failed
75 
76 ///
77 /// \brief symbolic type for result of TweetNaCl primitives
78 ///
79 /// \ingroup mcci-tweetnacl
80 ///
81 /// \details
82 /// NaCl and TweetNaCl primitives generally return 0 for success,
83 /// and non-zero (generally -1) for failures. We formerly converted
84 /// to boolean as part of the API wrapper, but this proved to be
85 /// a bad idea; the wrappers are too close to the underlying
86 /// primitives. Now we return the same value returned by the primitive,
87 /// but we use a typedef for clarity.
88 ///
89 /// \see mcci_tweetnacl_result_is_success()
90 /// \see mcci_tweetnacl_result_e
91 ///
93 
94 ///
95 /// \brief result codes for TweetNaCl primitives
96 ///
97 /// \ingroup mcci-tweetnacl
98 ///
99 /// \details
100 /// We define symbolic constants for success and the typical failure
101 /// code. Note, however, that *any* non-zero value is a failure;
102 /// codes should be tested using mcci_tweetnacl_result_is_success(),
103 /// not by direct comparison. These codes should be used in `return`
104 /// statements and other contexts where a result code is prepared,
105 /// but not to analyze results.
106 ///
107 /// \see mcci_tweetnacl_result_t
108 ///
110  {
111  MCCI_TWEETNACL_RESULT_SUCCESS, ///< API succeeded
112  MCCI_TWEETNACL_RESULT_FAILED = -1, ///< API failed
113  };
114 
115 ///
116 /// \brief symbolic type for function implementing local random-number generator
117 ///
118 /// \param[in] hDriver is the driver handle supplied to MCCI TweetNaCl at initialization.
119 /// \param[out] pOutBuffer points to the buffer to be filled
120 /// \param[in] nBuffer specifies the size of \p pOutBuffer.
121 ///
122 /// \returns error code; 0 for success, non-zero for failure.
123 ///
124 /// \note This function must provide a cryptographically strong RNG.
125 /// If not provided, mcci_tweetnacl_sign_keypair() and mcci_tweetnacl_box_kaypair()
126 /// will not be available.
127 ///
128 /// \ingroup mcci-tweetnacl
129 ///
133  unsigned char *pOutBuffer,
134  size_t nBuffer
135  );
136 
137 /****************************************************************************\
138 |
139 | APIs
140 |
141 \****************************************************************************/
142 
143 ///
144 /// \brief check whether an API result code indicates succcess
145 ///
146 /// \param [in] resultCode
147 ///
148 /// \returns \c true if result code indicates success, \c false otherwise.
149 ///
150 /// \ingroup mcci-tweetnacl
151 ///
152 static inline bool
154  mcci_tweetnacl_result_t resultCode
155  )
156  {
157  return resultCode == 0;
158  }
159 
160 /// \brief Compare two 16-byte buffers, in a time-invariant fashion
161 ///
162 /// \param[in] x,y buffers to be compared.
163 ///
164 /// \return true if the two buffers are equal, otherwise false.
165 ///
166 /// \see https://nacl.cr.yp.to/verify.html
167 ///
168 /// \ingroup string-comparison
169 ///
170 static inline mcci_tweetnacl_result_t
172  const unsigned char *x,
173  const unsigned char *y
174  )
175  {
176  extern int crypto_verify_16_tweet(const unsigned char *,const unsigned char *);
177  return crypto_verify_16_tweet(x, y);
178  }
179 
180 ///
181 /// \brief Compare two 32-byte buffers, in a time-invariant fashion
182 ///
183 /// \param[in] x,y buffers to be compared.
184 ///
185 /// \see https://nacl.cr.yp.to/verify.html
186 ///
187 /// \ingroup string-comparison
188 ///
189 static inline mcci_tweetnacl_result_t
191  const unsigned char *x,
192  const unsigned char *y
193  )
194  {
195  extern int crypto_verify_32_tweet(const unsigned char *,const unsigned char *);
196  return crypto_verify_32_tweet(x, y);
197  }
198 
199 ///
200 /// \brief Compare two 64-byte buffers, in a time-invariant fashion
201 ///
202 /// \param[in] x,y buffers to be compared.
203 ///
204 /// \see https://nacl.cr.yp.to/verify.html
205 ///
206 /// \ingroup string-comparison
207 ///
208 static inline mcci_tweetnacl_result_t
210  const unsigned char *x,
211  const unsigned char *y
212  )
213  {
214  extern int crypto_verify_64_tweet_mcci(const unsigned char *,const unsigned char *);
215  return crypto_verify_64_tweet_mcci(x, y);
216  }
217 
218 ///
219 /// \brief setup the random number generator connection
220 ///
221 /// \param[in] pRandomBytesFn points to the function to be called by `randombytes`.
222 /// \param[in] hDriver is an optional driver handle to be passed to the random
223 /// byte generator.
224 ///
225 /// \returns \c true if the random number generator was set, \c false if the driver
226 /// could not be established.
227 ///
228 /// \ingroup mcci-tweetnacl
229 ///
232  mcci_tweetnacl_randombytes_fn_t *pRandomBytesFn,
234  );
235 
236 ///
237 /// \brief Get the last error reported in the \c randombytes() mechanism.
238 ///
239 /// \returns last error code posted.
240 ///
241 /// \note The client must explicity call
242 /// mcci_tweetnacl_hal_randombytes_setlasterror() to clear the
243 /// last error if desired.
244 ///
245 /// \ingroup mcci-tweetnacl
246 ///
247 /// \see mcci_tweetnacl_hal_randombytes_setlasterror()
248 ///
249 
252 
253 ///
254 /// \brief Change the last error cell for the \c randombytes() mechanism.
255 ///
256 /// \ingroup mcci-tweetnacl
257 ///
258 /// \see mcci_tweetnacl_hal_randombytes_getlasterror()
259 ///
260 
261 void
264  );
265 
266 
267 /****************************************************************************\
268 |
269 | Post-Meta
270 |
271 \****************************************************************************/
272 
273 //--- close groups ---
274 /// @}
275 
276 #ifdef __cplusplus
277 }
278 #endif
279 
280 #endif /* _mcci_tweetnacl_h_ */
mcci_tweetnacl_result_t
int mcci_tweetnacl_result_t
symbolic type for result of TweetNaCl primitives
Definition: mcci_tweetnacl.h:92
MCCI_TWEETNACL_RANDOMBYTES_ERROR_INVALID_PARAMETER
@ MCCI_TWEETNACL_RANDOMBYTES_ERROR_INVALID_PARAMETER
invalid parameter
Definition: mcci_tweetnacl.h:72
mcci_tweetnacl_verify_16
static mcci_tweetnacl_result_t mcci_tweetnacl_verify_16(const unsigned char *x, const unsigned char *y)
Compare two 16-byte buffers, in a time-invariant fashion.
Definition: mcci_tweetnacl.h:171
MCCI_TWEETNACL_RANDOMBYTES_ERROR_UNKNOWN
@ MCCI_TWEETNACL_RANDOMBYTES_ERROR_UNKNOWN
bad param to mcci_tweetnacl_hal_randombytes_raise()
Definition: mcci_tweetnacl.h:70
mcci_tweetnacl_randombytes_error_t
mcci_tweetnacl_randombytes_error_t
error codes from mcci_tweetnacl_randombytes_fn_t implementations errors
Definition: mcci_tweetnacl.h:68
MCCI_TWEETNACL_RANDOMBYTES_ERROR_CRYPTO_API_FAILED
@ MCCI_TWEETNACL_RANDOMBYTES_ERROR_CRYPTO_API_FAILED
the related crypto API failed
Definition: mcci_tweetnacl.h:73
mcci_tweetnacl_result_is_success
static bool mcci_tweetnacl_result_is_success(mcci_tweetnacl_result_t resultCode)
check whether an API result code indicates succcess
Definition: mcci_tweetnacl.h:153
MCCI_TWEETNACL_RESULT_SUCCESS
@ MCCI_TWEETNACL_RESULT_SUCCESS
API succeeded.
Definition: mcci_tweetnacl.h:111
mcci_tweetnacl_verify_32
static mcci_tweetnacl_result_t mcci_tweetnacl_verify_32(const unsigned char *x, const unsigned char *y)
Compare two 32-byte buffers, in a time-invariant fashion.
Definition: mcci_tweetnacl.h:190
MCCI_TWEETNACL_RESULT_FAILED
@ MCCI_TWEETNACL_RESULT_FAILED
API failed.
Definition: mcci_tweetnacl.h:112
mcci_tweetnacl_hal_randombytes_getlasterror
mcci_tweetnacl_randombytes_error_t mcci_tweetnacl_hal_randombytes_getlasterror(void)
Get the last error reported in the randombytes() mechanism.
mcci_tweetnacl_randombytes_fn_t
mcci_tweetnacl_randombytes_error_t() mcci_tweetnacl_randombytes_fn_t(mcci_tweetnacl_randombytes_handle_t hDriver, unsigned char *pOutBuffer, size_t nBuffer)
symbolic type for function implementing local random-number generator
Definition: mcci_tweetnacl.h:131
mcci_tweetnacl_result_e
mcci_tweetnacl_result_e
result codes for TweetNaCl primitives
Definition: mcci_tweetnacl.h:110
MCCI_TWEETNACL_RANDOMBYTES_ERROR_NOT_INITIALIZED
@ MCCI_TWEETNACL_RANDOMBYTES_ERROR_NOT_INITIALIZED
random number driver not intialized
Definition: mcci_tweetnacl.h:71
mcci_tweetnacl_verify_64
static mcci_tweetnacl_result_t mcci_tweetnacl_verify_64(const unsigned char *x, const unsigned char *y)
Compare two 64-byte buffers, in a time-invariant fashion.
Definition: mcci_tweetnacl.h:209
mcci_tweetnacl_randombytes_handle_t
struct mcci_tweetnacl_randombytes_driver_s * mcci_tweetnacl_randombytes_handle_t
abstract type for randombytes driver context
Definition: mcci_tweetnacl.h:60
mcci_tweetnacl_configure_randombytes
mcci_tweetnacl_result_t mcci_tweetnacl_configure_randombytes(mcci_tweetnacl_randombytes_fn_t *pRandomBytesFn, mcci_tweetnacl_randombytes_handle_t hDriver)
setup the random number generator connection
mcci_tweetnacl_hal_randombytes_setlasterror
void mcci_tweetnacl_hal_randombytes_setlasterror(mcci_tweetnacl_randombytes_error_t lastError)
Change the last error cell for the randombytes() mechanism.
MCCI_TWEETNACL_RANDOMBYTES_ERROR_SUCCESS
@ MCCI_TWEETNACL_RANDOMBYTES_ERROR_SUCCESS
success (not an error)
Definition: mcci_tweetnacl.h:69