MCCI TweetNaCl
TweetNaCl library adapted for embedded use
mcci_tweetnacl_secretbox.h
Go to the documentation of this file.
1 /*
2 
3 Module: mcci_tweetnacl_secretbox.h
4 
5 Function:
6  MCCI TweetNaCl equivalent of NaCl "crypto_secretbox.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  fullname, MCCI Corporation March 2021
19 
20 */
21 
22 #ifndef _mcci_tweetnacl_secretbox_h_
23 #define _mcci_tweetnacl_secretbox_h_ /* prevent multiple includes */
24 
25 #pragma once
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 /****************************************************************************\
32 |
33 | Meta
34 |
35 \****************************************************************************/
36 
37 /// \addtogroup secret-key-crypto Secret-key cryptography
38 /// @{
39 /// \addtogroup crypto-secretbox Authenticated encryptions
40 /// @{
41 
42 /****************************************************************************\
43 |
44 | Forward types
45 |
46 \****************************************************************************/
47 
48 /// \brief Reference structure for bytes requried to be zero at front of plaintext
49 typedef struct mcci_tweetnacl_secretbox_messagezero_s
50  {
51  unsigned char bytes[32];
53 
54 /// \brief Reference structure for bytes required to be zero at front of cihper text
55 typedef struct mcci_tweetnacl_secretbox_cipherzero_s
56  {
57  unsigned char bytes[16];
59 
60 /// \brief Reference structure for nonce bytes for secretbox.
61 typedef struct mcci_tweetnacl_secretbox_nonce_s
62  {
63  unsigned char bytes[24];
65 
66 /// \brief Reference structure for key bytes for secretbox.
67 typedef struct mcci_tweetnacl_secretbox_key_s
68  {
69  unsigned char bytes[32];
71 
72 /****************************************************************************\
73 |
74 | APIs
75 |
76 \****************************************************************************/
77 
78 ///
79 /// \brief Secret-key authenticated encryption (using xsalsa20)
80 ///
81 /// \param[out] pCipherText pointer to buffer of size \p sizeText bytes.
82 /// \param[in] pPlainText pointer to buffer of size \p sizeText bytes.
83 /// \param[in] sizeText size of the output text buffer
84 /// \param[in] pNonce pointer to 24-byte nonce
85 /// \param[in] pKey pointer to 32-byte key buffer.
86 ///
87 /// \return zero for successful encryption, non-zero for parameter validation failure.
88 ///
89 /// \note \p pPlainText must start with a string of
90 /// `sizeof(mcci_tweetnacl_secretbox_messagezero_t::bytes)` bytes of zero. The
91 /// first `sizeof(mcci_tweetnacl_secretbox_cipherzero_t::bytes)` bytes of
92 /// \p pCipherText will be zero. Thus, the real ciphertext data is from
93 /// `pCipherText + sizeof(mcci_tweetnacl_secretbox_cipherzero_t::bytes)` to
94 /// `pCihperText + sizeText - 1`.
95 ///
96 /// \see https://nacl.cr.yp.to/secretbox.html
97 ///
98 
99 static inline mcci_tweetnacl_result_t
101  unsigned char *pCipherText,
102  const unsigned char *pPlainText,
103  size_t sizeText,
104  const mcci_tweetnacl_secretbox_nonce_t *pNonce,
106  )
107  {
108  extern int crypto_secretbox_xsalsa20poly1305_tweet(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *);
109  return crypto_secretbox_xsalsa20poly1305_tweet(
110  pCipherText,
111  pPlainText,
112  sizeText,
113  pNonce->bytes,
114  pKey->bytes
115  );
116  }
117 
118 ///
119 /// \brief Secret-key authenticated decryption (using xsalsa20)
120 ///
121 /// \param[out] pPlainText pointer to buffer of size \p sizeText bytes.
122 /// \param[in] pCipherText pointer to buffer of size \p sizeText bytes.
123 /// \param[in] sizeText size of the output text buffer
124 /// \param[in] pNonce pointer to 24-byte nonce
125 /// \param[in] pKey pointer to 32-byte key buffer.
126 ///
127 /// \returns zero for successful decryption and authenticaion, non-zero otherwise.
128 ///
129 /// \note \p pCipherText must start with a string of
130 /// `sizeof(mcci_tweetnacl_secretbox_cipherzero_t::bytes)` bytes of zero. The
131 /// first `sizeof(mcci_tweetnacl_secretbox_messagezero_t::bytes)` bytes of
132 /// \p pPlainText will be zero. Thus, the real plaintext data is from
133 /// `pPlainText + sizeof(mcci_tweetnacl_secretbox_messagezero_t::bytes)` to
134 /// `pPlainText + sizeText - 1`.
135 ///
136 /// \see https://nacl.cr.yp.to/secretbox.html
137 ///
138 
139 static inline mcci_tweetnacl_result_t
141  unsigned char *pPlainText,
142  const unsigned char *pCipherText,
143  size_t sizeText,
144  const mcci_tweetnacl_secretbox_nonce_t *pNonce,
146  )
147  {
148  extern int crypto_secretbox_xsalsa20poly1305_tweet_open(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *);
149  return crypto_secretbox_xsalsa20poly1305_tweet_open(
150  pPlainText,
151  pCipherText,
152  sizeText,
153  pNonce->bytes,
154  pKey->bytes
155  );
156  }
157 
158 /****************************************************************************\
159 |
160 | Post-Meta
161 |
162 \****************************************************************************/
163 
164 //--- close groups ---
165 /// @}
166 /// @}
167 
168 
169 #ifdef __cplusplus
170 }
171 #endif
172 
173 #endif /* _mcci_tweetnacl_secretbox_h_ */
mcci_tweetnacl_secretbox
static mcci_tweetnacl_result_t mcci_tweetnacl_secretbox(unsigned char *pCipherText, const unsigned char *pPlainText, size_t sizeText, const mcci_tweetnacl_secretbox_nonce_t *pNonce, const mcci_tweetnacl_secretbox_key_t *pKey)
Secret-key authenticated encryption (using xsalsa20)
Definition: mcci_tweetnacl_secretbox.h:100
mcci_tweetnacl_result_t
int mcci_tweetnacl_result_t
symbolic type for result of TweetNaCl primitives
Definition: mcci_tweetnacl.h:92
mcci_tweetnacl_secretbox_nonce_t::bytes
unsigned char bytes[24]
Definition: mcci_tweetnacl_secretbox.h:63
mcci_tweetnacl_secretbox_key_t
Reference structure for key bytes for secretbox.
Definition: mcci_tweetnacl_secretbox.h:68
mcci_tweetnacl_secretbox_nonce_t
Reference structure for nonce bytes for secretbox.
Definition: mcci_tweetnacl_secretbox.h:62
mcci_tweetnacl_secretbox_open
static mcci_tweetnacl_result_t mcci_tweetnacl_secretbox_open(unsigned char *pPlainText, const unsigned char *pCipherText, size_t sizeText, const mcci_tweetnacl_secretbox_nonce_t *pNonce, const mcci_tweetnacl_secretbox_key_t *pKey)
Secret-key authenticated decryption (using xsalsa20)
Definition: mcci_tweetnacl_secretbox.h:140
mcci_tweetnacl_secretbox_cipherzero_t
Reference structure for bytes required to be zero at front of cihper text.
Definition: mcci_tweetnacl_secretbox.h:56
mcci_tweetnacl_secretbox_messagezero_t
Reference structure for bytes requried to be zero at front of plaintext.
Definition: mcci_tweetnacl_secretbox.h:50
mcci_tweetnacl_secretbox_key_t::bytes
unsigned char bytes[32]
Definition: mcci_tweetnacl_secretbox.h:69