MCCI TweetNaCl
TweetNaCl library adapted for embedded use
mcci_tweetnacl_box.h
Go to the documentation of this file.
1 /*
2 
3 Module: mcci_tweetnacl_box.h
4 
5 Function:
6  Equivalent of NaCl "crypto_box.h" for MCCI TweetNaCl.
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_box_h_
23 #define _mcci_tweetnacl_box_h_ /* prevent multiple includes */
24 
25 #pragma once
26 
27 #include "mcci_tweetnacl.h"
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 /****************************************************************************\
34 |
35 | Meta
36 |
37 \****************************************************************************/
38 
39 /// \addtogroup public-key-crypto Public-key cryptography
40 /// @{
41 /// \addtogroup crypto-box Authenticated encryption
42 /// @{
43 
44 /****************************************************************************\
45 |
46 | Forward types
47 |
48 \****************************************************************************/
49 
50 /// \brief Reference structure for public key
51 typedef struct mcci_tweetnacl_box_publickey_s
52  {
53  unsigned char bytes[32];
55 
56 /// \brief Reference structure for private key
57 typedef struct mcci_tweetnacl_box_privatekey_s
58  {
59  unsigned char bytes[32];
61 
62 
63 /// \brief Reference structure for bytes required to be zero at front of plaintext
64 typedef struct mcci_tweetnacl_box_messagezero_s
65  {
66  unsigned char bytes[32];
68 
69 /// \brief Reference structure for bytes required to be zero at front of cihper text
70 typedef struct mcci_tweetnacl_box_cipherzero_s
71  {
72  unsigned char bytes[16];
74 
75 /// \brief Reference structure for nonce bytes for box.
76 typedef struct mcci_tweetnacl_box_nonce_s
77  {
78  unsigned char bytes[24];
80 
81 /// \brief Reference structure for precomputation bytes for box.
82 typedef struct mcci_tweetnacl_box_beforenm_s
83  {
84  unsigned char bytes[32];
86 
87 /****************************************************************************\
88 |
89 | APIs
90 |
91 \****************************************************************************/
92 
93 ///
94 /// \brief Generate a public/private key pair.
95 ///
96 /// \param[out] pPublicKey is set to the public key
97 /// \param[in] pPrivateKey is set to the private key
98 ///
99 /// \returns error code; zero for success, non-zero for failure.
100 ///
101 /// \note depends on an implementation of randombytes().
102 /// \see https://nacl.cr.yp.to/box.html
103 ///
106  mcci_tweetnacl_box_publickey_t *pPublicKey,
108  );
109 
110 ///
111 /// \brief Precompute for public-key authenticated cryptographic operations
112 ///
113 /// \param[out] k is set to the context
114 /// \param[in] pPublicKey, pPrivateKey are the public and private keys to be used for this operation.
115 ///
116 /// \see https://nacl.cr.yp.to/box.html
117 ///
118 static inline
121  const mcci_tweetnacl_box_publickey_t *pPublicKey,
122  const mcci_tweetnacl_box_privatekey_t *pPrivateKey
123  )
124  {
125  extern int crypto_box_curve25519xsalsa20poly1305_tweet_beforenm(unsigned char *,const unsigned char *,const unsigned char *);
126  (void) crypto_box_curve25519xsalsa20poly1305_tweet_beforenm(
127  k->bytes,
128  pPublicKey->bytes,
129  pPrivateKey->bytes
130  );
131  }
132 
133 ///
134 /// \brief Public-key authenticated encryption (precomputed)
135 ///
136 /// \param[out] pCipherText pointer to buffer of size \p sizeText bytes.
137 /// \param[in] pPlainText pointer to buffer of size \p sizeText bytes.
138 /// \param[in] sizeText size of the output text buffer
139 /// \param[in] pNonce pointer to 24-byte nonce
140 /// \param[in] pPrecomputed pointer to 32-byte precomputed buffer.
141 ///
142 /// \return true for successful encryption, false for parameter validation failure.
143 ///
144 /// \note \p pPlainText must start with a string of
145 /// `sizeof(mcci_tweetnacl_box_messagezero_t::bytes)` bytes of zero. The
146 /// first `sizeof(mcci_tweetnacl_box_cipherzero_t::bytes)` bytes of
147 /// \p pCipherText will be zero. Thus, the real ciphertext data is from
148 /// `pCipherText + sizeof(mcci_tweetnacl_box_cipherzero_t::bytes)` to
149 /// `pCipherText + sizeText - 1`.
150 ///
151 /// \return true if successful, false for failures [due to parameter problems only].
152 ///
153 /// \see https://nacl.cr.yp.to/box.html
154 ///
155 
156 static inline mcci_tweetnacl_result_t
158  unsigned char *pCipherText,
159  const unsigned char *pPlainText,
160  size_t sizeText,
161  const mcci_tweetnacl_box_nonce_t *pNonce,
162  const mcci_tweetnacl_box_beforenm_t *pPrecomputed
163  )
164  {
165  extern int crypto_box_curve25519xsalsa20poly1305_tweet_afternm(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *);
166  return crypto_box_curve25519xsalsa20poly1305_tweet_afternm(
167  pCipherText,
168  pPlainText,
169  sizeText,
170  pNonce->bytes,
171  pPrecomputed->bytes
172  );
173  }
174 
175 ///
176 /// \brief Public-key authenticated decryption (precomputed))
177 ///
178 /// \param[out] pPlainText pointer to buffer of size \p sizeText bytes.
179 /// \param[in] pCipherText pointer to buffer of size \p sizeText bytes.
180 /// \param[in] sizeText size of the output text buffer
181 /// \param[in] pNonce pointer to 24-byte nonce
182 /// \param[in] pPrecomputed pointer to 32-byte precomputed buffer.
183 ///
184 /// \returns zero for successful decryption and authenticaion, -1 otherwise.
185 ///
186 /// \note \p pCipherText must start with a string of
187 /// `sizeof(mcci_tweetnacl_box_cipherzero_t::bytes)` bytes of zero. The
188 /// first `sizeof(mcci_tweetnacl_box_messagezero_t::bytes)` bytes of
189 /// \p pPlainText will be zero. Thus, the real plaintext data is from
190 /// `pPlainText + sizeof(mcci_tweetnacl_box_messagezero_t::bytes)` to
191 /// `pPlainText + sizeText - 1`.
192 ///
193 /// \see https://nacl.cr.yp.to/box.html
194 ///
195 
196 static inline mcci_tweetnacl_result_t
198  unsigned char *pPlainText,
199  const unsigned char *pCipherText,
200  size_t sizeText,
201  const mcci_tweetnacl_box_nonce_t *pNonce,
202  const mcci_tweetnacl_box_beforenm_t *pPrecomputed
203  )
204  {
205  extern int crypto_box_curve25519xsalsa20poly1305_tweet_open_afternm(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *);
206  return crypto_box_curve25519xsalsa20poly1305_tweet_open_afternm(
207  pPlainText,
208  pCipherText,
209  sizeText,
210  pNonce->bytes,
211  pPrecomputed->bytes
212  );
213  }
214 
215 ///
216 /// \brief Public-key authenticated encryption
217 ///
218 /// \param[out] pCipherText pointer to buffer of size \p sizeText bytes.
219 /// \param[in] pPlainText pointer to buffer of size \p sizeText bytes.
220 /// \param[in] sizeText size of the output text buffer
221 /// \param[in] pNonce pointer to 24-byte nonce
222 /// \param[in] pPublicKey pointer to 32-byte public key of receiver
223 /// \param[in] pPrivateKey pointer to 32-byte private key of sender
224 ///
225 /// \return true for successful encryption, false for parameter validation failure.
226 ///
227 /// \note \p pPlainText must start with a string of
228 /// `sizeof(mcci_tweetnacl_box_messagezero_t::bytes)` bytes of zero. The
229 /// first `sizeof(mcci_tweetnacl_box_cipherzero_t::bytes)` bytes of
230 /// \p pCipherText will be zero. Thus, the real ciphertext data is from
231 /// `pCipherText + sizeof(mcci_tweetnacl_box_cipherzero_t::bytes)` to
232 /// `pCihperText + sizeText - 1`.
233 ///
234 /// \see https://nacl.cr.yp.to/box.html
235 ///
236 
237 static inline mcci_tweetnacl_result_t
239  unsigned char *pCipherText,
240  const unsigned char *pPlainText,
241  size_t sizeText,
242  const mcci_tweetnacl_box_nonce_t *pNonce,
243  const mcci_tweetnacl_box_publickey_t *pPublicKey,
244  const mcci_tweetnacl_box_privatekey_t *pPrivateKey
245  )
246  {
247  extern int crypto_box_curve25519xsalsa20poly1305_tweet(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *,const unsigned char *);
248  return crypto_box_curve25519xsalsa20poly1305_tweet(
249  pCipherText,
250  pPlainText,
251  sizeText,
252  pNonce->bytes,
253  pPublicKey->bytes,
254  pPrivateKey->bytes
255  );
256  }
257 
258 ///
259 /// \brief Public-key authenticated decryption
260 ///
261 /// \param[out] pPlainText pointer to buffer of size \p sizeText bytes.
262 /// \param[in] pCipherText pointer to buffer of size \p sizeText bytes.
263 /// \param[in] sizeText size of the output text buffer
264 /// \param[in] pNonce pointer to 24-byte nonce
265 /// \param[in] pPublicKey pointer to 32-byte public key of sender
266 /// \param[in] pPrivateKey pointer to 32-byte private key of receiver
267 ///
268 /// \returns 0 for successful decryption and authentication, -1 otherwise.
269 ///
270 /// \note \p pCipherText must start with a string of
271 /// `sizeof(mcci_tweetnacl_secretbox_cipherzero_t::bytes)` bytes of zero. The
272 /// first `sizeof(mcci_tweetnacl_secretbox_messagezero_t::bytes)` bytes of
273 /// \p pPlainText will be zero. Thus, the real plaintext data is from
274 /// `pPlainText + sizeof(mcci_tweetnacl_secretbox_messagezero_t::bytes)` to
275 /// `pPlainText + sizeText - 1`.
276 ///
277 /// \see https://nacl.cr.yp.to/box.html
278 ///
279 
280 static inline mcci_tweetnacl_result_t
282  unsigned char *pPlainText,
283  const unsigned char *pCipherText,
284  size_t sizeText,
285  const mcci_tweetnacl_box_nonce_t *pNonce,
286  const mcci_tweetnacl_box_publickey_t *pPublicKey,
287  const mcci_tweetnacl_box_privatekey_t *pPrivateKey
288  )
289  {
290  extern int crypto_box_curve25519xsalsa20poly1305_tweet_open(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *,const unsigned char *);
291  return crypto_box_curve25519xsalsa20poly1305_tweet_open(
292  pPlainText,
293  pCipherText,
294  sizeText,
295  pNonce->bytes,
296  pPublicKey->bytes,
297  pPrivateKey->bytes
298  );
299  }
300 
301 /****************************************************************************\
302 |
303 | Post-Meta
304 |
305 \****************************************************************************/
306 
307 //--- close groups ---
308 /// @}
309 /// @}
310 
311 #ifdef __cplusplus
312 }
313 #endif
314 
315 #endif /* _mcci_tweetnacl_box_h_ */
mcci_tweetnacl_box_privatekey_t
Reference structure for private key.
Definition: mcci_tweetnacl_box.h:58
mcci_tweetnacl_result_t
int mcci_tweetnacl_result_t
symbolic type for result of TweetNaCl primitives
Definition: mcci_tweetnacl.h:92
mcci_tweetnacl_box_publickey_t
Reference structure for public key.
Definition: mcci_tweetnacl_box.h:52
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_box_messagezero_t
Reference structure for bytes required to be zero at front of plaintext.
Definition: mcci_tweetnacl_box.h:65
mcci_tweetnacl_box
static mcci_tweetnacl_result_t mcci_tweetnacl_box(unsigned char *pCipherText, const unsigned char *pPlainText, size_t sizeText, const mcci_tweetnacl_box_nonce_t *pNonce, const mcci_tweetnacl_box_publickey_t *pPublicKey, const mcci_tweetnacl_box_privatekey_t *pPrivateKey)
Public-key authenticated encryption.
Definition: mcci_tweetnacl_box.h:238
mcci_tweetnacl_box_cipherzero_t
Reference structure for bytes required to be zero at front of cihper text.
Definition: mcci_tweetnacl_box.h:71
mcci_tweetnacl_box_open
static mcci_tweetnacl_result_t mcci_tweetnacl_box_open(unsigned char *pPlainText, const unsigned char *pCipherText, size_t sizeText, const mcci_tweetnacl_box_nonce_t *pNonce, const mcci_tweetnacl_box_publickey_t *pPublicKey, const mcci_tweetnacl_box_privatekey_t *pPrivateKey)
Public-key authenticated decryption.
Definition: mcci_tweetnacl_box.h:281
mcci_tweetnacl_box_privatekey_t::bytes
unsigned char bytes[32]
Definition: mcci_tweetnacl_box.h:59
mcci_tweetnacl_box_publickey_t::bytes
unsigned char bytes[32]
Definition: mcci_tweetnacl_box.h:53
mcci_tweetnacl_box_keypair
mcci_tweetnacl_randombytes_error_t mcci_tweetnacl_box_keypair(mcci_tweetnacl_box_publickey_t *pPublicKey, mcci_tweetnacl_box_privatekey_t *pPrivateKey)
Generate a public/private key pair.
mcci_tweetnacl_box_beforenm
static void mcci_tweetnacl_box_beforenm(mcci_tweetnacl_box_beforenm_t *k, const mcci_tweetnacl_box_publickey_t *pPublicKey, const mcci_tweetnacl_box_privatekey_t *pPrivateKey)
Precompute for public-key authenticated cryptographic operations.
Definition: mcci_tweetnacl_box.h:119
mcci_tweetnacl_box_open_afternm
static mcci_tweetnacl_result_t mcci_tweetnacl_box_open_afternm(unsigned char *pPlainText, const unsigned char *pCipherText, size_t sizeText, const mcci_tweetnacl_box_nonce_t *pNonce, const mcci_tweetnacl_box_beforenm_t *pPrecomputed)
Public-key authenticated decryption (precomputed))
Definition: mcci_tweetnacl_box.h:197
mcci_tweetnacl_box_beforenm_t
Reference structure for precomputation bytes for box.
Definition: mcci_tweetnacl_box.h:83
mcci_tweetnacl_box_afternm
static mcci_tweetnacl_result_t mcci_tweetnacl_box_afternm(unsigned char *pCipherText, const unsigned char *pPlainText, size_t sizeText, const mcci_tweetnacl_box_nonce_t *pNonce, const mcci_tweetnacl_box_beforenm_t *pPrecomputed)
Public-key authenticated encryption (precomputed)
Definition: mcci_tweetnacl_box.h:157
mcci_tweetnacl.h
mcci_tweetnacl_box_nonce_t::bytes
unsigned char bytes[24]
Definition: mcci_tweetnacl_box.h:78
mcci_tweetnacl_box_nonce_t
Reference structure for nonce bytes for box.
Definition: mcci_tweetnacl_box.h:77
mcci_tweetnacl_box_beforenm_t::bytes
unsigned char bytes[32]
Definition: mcci_tweetnacl_box.h:84