MCCI TweetNaCl
TweetNaCl library adapted for embedded use
mcci_tweetnacl_stream.h
Go to the documentation of this file.
1 /*
2 
3 Module: mcci_tweetnacl_stream.h
4 
5 Function:
6  MCCI TweetNaCl equivalent of "crypto_stream.h"
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_stream_h_
23 #define _mcci_tweetnacl_stream_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 secret-key-crypto Secret-key cryptography
40 /// @{
41 /// \addtogroup crypto-stream Encryption
42 /// @{
43 
44 /****************************************************************************\
45 |
46 | Forward Types
47 |
48 \****************************************************************************/
49 
50 /// \brief abstract type for crypto keys
51 typedef struct mcci_tweetnacl_stream_key__s
52  {
53  /// the bytes of the key
54  unsigned char bytes[32];
56 
57 /// \brief abstract type for crypto nonces
58 typedef struct mcci_tweetnacl_stream_nonce_s
59  {
60  /// the bytes of the nonce
61  unsigned char bytes[32];
63 
64 ///
65 /// \brief perform a core hash round of Salsa20 encryption.
66 ///
67 /// \param[out] out pointer to a 64-byte buffer
68 /// \param[in] in pointer to a 16-byte input value
69 /// \param[in] key pointer to the 32-byte key
70 /// \param[in] expansion pointer to a 16-byte expansion vector, normally either
71 /// "expand 32-byte k" or "expand 16-byte k"
72 ///
73 /// \note Rarely used directly, but exported for convenience.
74 ///
75 /// \see http://www.crypto-it.net/eng/symmetric/salsa20.html
76 ///
77 static inline void
79  unsigned char *out,
80  const unsigned char *in,
81  const mcci_tweetnacl_stream_key_t *key,
82  const unsigned char *expansion
83  )
84  {
85  extern int crypto_core_salsa20_tweet(unsigned char *,const unsigned char *,const unsigned char *,const unsigned char *);
86  (void) crypto_core_salsa20_tweet(out, in, key->bytes, expansion);
87  }
88 
89 ///
90 /// \brief perform a core hash round of Salsa20 encryption.
91 ///
92 /// \param[out] out pointer to a 64-byte buffer
93 /// \param[in] in pointer to a 16-byte input value
94 /// \param[in] key pointer to the 32-byte key
95 /// \param[in] expansion pointer to a 16-byte expansion vector, normally
96 /// "expand 32-byte k"
97 ///
98 /// \note Rarely used directly, but exported for convenience.
99 ///
100 /// \see http://www.crypto-it.net/eng/symmetric/salsa20.html
101 ///
102 static inline void
104  unsigned char *out,
105  const unsigned char *in,
106  const mcci_tweetnacl_stream_key_t *key,
107  const unsigned char *expansion
108  )
109  {
110  extern int crypto_core_hsalsa20_tweet(unsigned char *,const unsigned char *,const unsigned char *,const unsigned char *);
111  (void) crypto_core_hsalsa20_tweet(
112  out,
113  in,
114  key->bytes,
115  expansion
116  );
117  }
118 
119 ///
120 /// \brief encrypt or decrypt text using Salsa20
121 ///
122 /// \param[out] pOutText pointer to buffer of size \p sizeText bytes.
123 /// \param[in] pInText pointer to buffer of size \p sizeText bytes. If NULL,
124 /// a string of zero bytes will be substituted.
125 /// \param[in] sizeText size of the input and output text buffers
126 /// \param[in] pNonce pointer to 8-byte nonce buffer
127 /// \param[in] pKey pointer to 32-byte key buffer.
128 ///
129 /// \note generally not used directly; use \ref mcci_tweetnacl_stream_xor() instead.
130 ///
131 /// \see http://www.crypto-it.net/eng/symmetric/salsa20.html
132 ///
133 static inline void
135  unsigned char *pOutText,
136  const unsigned char *pInText,
137  size_t sizeText,
138  const mcci_tweetnacl_stream_nonce_t *pNonce,
139  const mcci_tweetnacl_stream_key_t *pKey
140  )
141  {
142  extern int crypto_stream_salsa20_tweet_xor(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *);
143  (void) crypto_stream_salsa20_tweet_xor(
144  pOutText,
145  pInText,
146  sizeText,
147  pNonce->bytes,
148  pKey->bytes
149  );
150  }
151 
152 ///
153 /// \brief Generate stream of Salsa20 bytes
154 ///
155 /// \param[out] pOutText pointer to buffer of size \p sizeText bytes.
156 /// \param[in] sizeText size of the input and output text buffers
157 /// \param[in] pNonce pointer to 8-byte nonce buffer
158 /// \param[in] pKey pointer to 32-byte key buffer.
159 ///
160 /// \note generally not used directly; use \ref mcci_tweetnacl_stream() instead.
161 ///
162 /// \see http://www.crypto-it.net/eng/symmetric/salsa20.html
163 ///
164 static inline void
166  unsigned char *pOutText,
167  size_t sizeText,
168  const mcci_tweetnacl_stream_nonce_t *pNonce,
169  const mcci_tweetnacl_stream_key_t *pKey
170  )
171  {
172  extern int crypto_stream_salsa20_tweet(unsigned char *,unsigned long long,const unsigned char *,const unsigned char *);
173  (void) crypto_stream_salsa20_tweet(pOutText, sizeText, pNonce->bytes, pKey->bytes);
174  }
175 
176 ///
177 /// \brief Generate stream of crypto bytes
178 ///
179 /// \param[out] pOutText pointer to buffer of size \p sizeText bytes.
180 /// \param[in] sizeText size of the output text buffer
181 /// \param[in] pNonce pointer to 8-byte nonce, followed by 8-byte index buffer
182 /// \param[in] pKey pointer to 32-byte key buffer.
183 ///
184 /// \see https://nacl.cr.yp.to/stream.html
185 /// \see https://www.xsalsa20.com/
186 ///
187 static inline void
189  unsigned char *pOutText,
190  size_t sizeText,
191  const mcci_tweetnacl_stream_nonce_t *pNonce,
192  const mcci_tweetnacl_stream_key_t *pKey
193  )
194  {
195  extern int crypto_stream_xsalsa20_tweet(unsigned char *,unsigned long long,const unsigned char *,const unsigned char *);
196  (void) crypto_stream_xsalsa20_tweet(
197  pOutText,
198  sizeText,
199  pNonce->bytes,
200  pKey->bytes
201  );
202  }
203 
204 ///
205 /// \brief Encrypt or decrypt text (using xsalsa20)
206 ///
207 /// \param[out] pOutText pointer to buffer of size \p sizeText bytes.
208 /// \param[in] pInText pointer to buffer of size \p sizeText bytes.
209 /// \param[in] sizeText size of the output text buffer
210 /// \param[in] pNonce pointer to 8-byte nonce, followed by 8-byte index buffer
211 /// \param[in] pKey pointer to 32-byte key buffer.
212 ///
213 /// \see https://nacl.cr.yp.to/stream.html
214 /// \see https://www.xsalsa20.com/
215 /// \see crypto_onetimeauth
216 ///
217 
218 static inline void
220  unsigned char *pOutText,
221  const unsigned char *pInText,
222  size_t sizeText,
223  const mcci_tweetnacl_stream_nonce_t *pNonce,
224  const mcci_tweetnacl_stream_key_t *pKey
225  )
226  {
227  extern int crypto_stream_xsalsa20_tweet_xor(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *);
228  (void) crypto_stream_xsalsa20_tweet_xor(
229  pOutText,
230  pInText,
231  sizeText,
232  pNonce->bytes,
233  pKey->bytes
234  );
235  }
236 
237 /****************************************************************************\
238 |
239 | Post-Meta
240 |
241 \****************************************************************************/
242 
243 //--- close groups ---
244 /// @}
245 /// @}
246 
247 #ifdef __cplusplus
248 }
249 #endif
250 
251 #endif /* _mcci_tweetnacl_stream_h_ */
mcci_tweetnacl_stream_nonce_t
abstract type for crypto nonces
Definition: mcci_tweetnacl_stream.h:59
mcci_tweetnacl_stream_nonce_t::bytes
unsigned char bytes[32]
the bytes of the nonce
Definition: mcci_tweetnacl_stream.h:61
mcci_tweetnacl_stream_key_t
abstract type for crypto keys
Definition: mcci_tweetnacl_stream.h:52
mcci_tweetnacl_stream_key_t::bytes
unsigned char bytes[32]
the bytes of the key
Definition: mcci_tweetnacl_stream.h:54
mcci_tweetnacl_core_hsalsa20
static void mcci_tweetnacl_core_hsalsa20(unsigned char *out, const unsigned char *in, const mcci_tweetnacl_stream_key_t *key, const unsigned char *expansion)
perform a core hash round of Salsa20 encryption.
Definition: mcci_tweetnacl_stream.h:103
mcci_tweetnacl_stream
static void mcci_tweetnacl_stream(unsigned char *pOutText, size_t sizeText, const mcci_tweetnacl_stream_nonce_t *pNonce, const mcci_tweetnacl_stream_key_t *pKey)
Generate stream of crypto bytes.
Definition: mcci_tweetnacl_stream.h:188
mcci_tweetnacl_stream_salsa20
static void mcci_tweetnacl_stream_salsa20(unsigned char *pOutText, size_t sizeText, const mcci_tweetnacl_stream_nonce_t *pNonce, const mcci_tweetnacl_stream_key_t *pKey)
Generate stream of Salsa20 bytes.
Definition: mcci_tweetnacl_stream.h:165
mcci_tweetnacl_core_salsa20
static void mcci_tweetnacl_core_salsa20(unsigned char *out, const unsigned char *in, const mcci_tweetnacl_stream_key_t *key, const unsigned char *expansion)
perform a core hash round of Salsa20 encryption.
Definition: mcci_tweetnacl_stream.h:78
mcci_tweetnacl.h
mcci_tweetnacl_stream_salsa20_xor
static void mcci_tweetnacl_stream_salsa20_xor(unsigned char *pOutText, const unsigned char *pInText, size_t sizeText, const mcci_tweetnacl_stream_nonce_t *pNonce, const mcci_tweetnacl_stream_key_t *pKey)
encrypt or decrypt text using Salsa20
Definition: mcci_tweetnacl_stream.h:134
mcci_tweetnacl_stream_xor
static void mcci_tweetnacl_stream_xor(unsigned char *pOutText, const unsigned char *pInText, size_t sizeText, const mcci_tweetnacl_stream_nonce_t *pNonce, const mcci_tweetnacl_stream_key_t *pKey)
Encrypt or decrypt text (using xsalsa20)
Definition: mcci_tweetnacl_stream.h:219