MCCI TweetNaCl
TweetNaCl library adapted for embedded use
mcci_tweetnacl_sign.h
Go to the documentation of this file.
1 /*
2 
3 Module: mcci_tweetnacl_sign.h
4 
5 Function:
6  Equivalent of NaCl "crypto_sign.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_sign_h_
23 #define _mcci_tweetnacl_sign_h_ /* prevent multiple includes */
24 
25 #pragma once
26 
27 #ifndef _mcci_tweetnacl_h_
28 # include "mcci_tweetnacl.h"
29 #endif
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 /****************************************************************************\
36 |
37 | Meta
38 |
39 \****************************************************************************/
40 
41 /// \addtogroup public-key-crypto Public-key cryptography
42 /// @{
43 /// \defgroup crypto-sign Signatures
44 /// @{
45 
46 /****************************************************************************\
47 |
48 | Forward types
49 |
50 \****************************************************************************/
51 
52 /// \brief Public Key for TweetNaCl sign operations
53 typedef struct mcci_tweetnacl_sign_publickey_s
54  {
55  /// the public key value. Note that this is shorter than the private key.
56  unsigned char bytes[32];
58 
59 /// \brief Private Key for TweetNaCl sign operations
60 typedef struct mcci_tweetnacl_sign_privatekey_s
61  {
62  /// the private key value. Note that this is longer than the public key.
63  unsigned char bytes[64];
65 
66 /// \brief Signature block for TweetNaCl sign operations
67 typedef struct mcci_tweetnacl_sign_signature_s
68  {
69  /// the signature block bytes. Per API, this is the maximum size.
70  unsigned char bytes[64];
72 
73 /****************************************************************************\
74 |
75 | APIs
76 |
77 \****************************************************************************/
78 
79 ///
80 /// \brief generate a private key and a corresponding public key
81 ///
82 /// \param[out] pPublicKey is set to the generated public key.
83 /// \param[in] pPrivateKey is set to the generated private key.
84 ///
85 /// \returns 0 for success, non-zero error code for failure.
86 ///
87 /// \note this function requires that \c randombytes() be implemented and
88 /// successful. Further, it requires a cryptographically secure string
89 /// of bytes when \c randombytes() succeeds.
90 ///
91 /// \see https://nacl.cr.yp.to/sign.html
92 ///
97  );
98 
99 ///
100 /// \brief Return size of signature, in bytes
101 ///
102 #define mcci_tweetnacl_sign_signature_size() \
103  sizeof(((mcci_tweetnacl_sign_signature_t *)NULL)->bytes)
104 
105 ///
106 /// \brief sign a message (typically a hash of the real message)
107 ///
108 /// \param[out] pSignedMessage points to buffer to received signed message.
109 /// \param[out] pSignedMessageSize points to cell to receive size of signed message.
110 /// \param[in] pMessage input message
111 /// \param[in] messageSize size of input message, in bytes
112 /// \param[in] pPrivateKey private key to be used to sign message.
113 ///
114 /// \returns zero if successfully signed; in which case \p *pSignedMessageSize is set to
115 /// the size. Otherwise non-zero, in which case \p *pSignedMessageSize is zero.
116 ///
117 /// \details
118 /// This is a wrapper for TweetNaCl's `crypto_sign()`, enforcing a few adjustments.
119 /// Size is a `size_t` rather than `unsigned long long`; if the input size is so
120 /// large that it would wrap around, we refuse to sign, and return a failure. Because
121 /// `crypto_sign()` productes an `unsigned long long` by reference, we have to stage
122 /// the result and narrow it when copying back to the client. We are careful to avoid
123 /// overflow, although overflow arguably is impossible.
124 ///
125 /// \note
126 /// The buffer at `pSignedMessage` must be at least `messageSize + mcci_tweetnacl_sign_signature_size()`
127 /// bytes long.
128 ///
131  unsigned char *pSignedMessage,
132  size_t *pSignedMessageSize,
133  const unsigned char *pMessage,
134  size_t messageSize,
135  const mcci_tweetnacl_sign_privatekey_t *pPrivateKey
136  );
137 
138 /// \brief given a signed message, verify and output signed contents
139 ///
140 /// \param[out] pMessage points to buffer to received verified message.
141 /// \param[out] pMessageSize points to cell to receive size of verified message.
142 /// \param[in] pSignedMessage input signed (opaque) message
143 /// \param[in] messageSize size of signed message, in bytes
144 /// \param[in] pPublicKey public key to be used to verify message.
145 ///
146 /// \returns zero if successfully verified; in which case \p pMessage[] is set to the
147 /// validated contents, and \p *pMessageSize is set to
148 /// the size. Otherwise non-zero, in which case \p pMessage[] may be changed
149 /// but should be ignored.
150 ///
151 /// \details
152 /// This is a wrapper for TweetNaCl's `crypto_sign_open()`, enforcing a few adjustments.
153 /// Size is a `size_t` rather than `unsigned long long`. Because
154 /// `crypto_sign_out()` productes an `unsigned long long` by reference, we have to stage
155 /// the result and narrow it when copying back to the client.
156 ///
157 /// \note
158 //// messageSize must be at least 64.
159 /// The buffer at `pMessage` must be at least `messageSize` - 64 bytes long.
160 ///
161 static inline mcci_tweetnacl_result_t
163  unsigned char *pMessage,
164  size_t *pMessageSize,
165  const unsigned char *pSignedMessage,
166  size_t messageSize,
167  const mcci_tweetnacl_sign_publickey_t *pPublicKey
168  )
169  {
170  extern int crypto_sign_ed25519_tweet_open(unsigned char *,unsigned long long *,const unsigned char *,unsigned long long,const unsigned char *);
171  unsigned long long ullMessageSize;
172  int result;
173 
174  ullMessageSize = 0;
175 
176  result = crypto_sign_ed25519_tweet_open(
177  pMessage,
178  &ullMessageSize,
179  pSignedMessage,
180  messageSize,
181  pPublicKey->bytes
182  );
183 
184  *pMessageSize = (size_t) ullMessageSize;
185  return result;
186  }
187 
188 /****************************************************************************\
189 |
190 | Post-Meta
191 |
192 \****************************************************************************/
193 
194 //--- close groups ---
195 /// @}
196 /// @}
197 
198 #ifdef __cplusplus
199 }
200 #endif
201 
202 #endif /* _mcci_tweetnacl_sign_h_ */
mcci_tweetnacl_result_t
int mcci_tweetnacl_result_t
symbolic type for result of TweetNaCl primitives
Definition: mcci_tweetnacl.h:92
mcci_tweetnacl_sign_publickey_t
Public Key for TweetNaCl sign operations.
Definition: mcci_tweetnacl_sign.h:54
mcci_tweetnacl_sign_publickey_t::bytes
unsigned char bytes[32]
the public key value. Note that this is shorter than the private key.
Definition: mcci_tweetnacl_sign.h:56
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_sign_keypair
mcci_tweetnacl_randombytes_error_t mcci_tweetnacl_sign_keypair(mcci_tweetnacl_sign_publickey_t *pPublicKey, mcci_tweetnacl_sign_privatekey_t *pPrivateKey)
generate a private key and a corresponding public key
mcci_tweetnacl_sign_open
static mcci_tweetnacl_result_t mcci_tweetnacl_sign_open(unsigned char *pMessage, size_t *pMessageSize, const unsigned char *pSignedMessage, size_t messageSize, const mcci_tweetnacl_sign_publickey_t *pPublicKey)
given a signed message, verify and output signed contents
Definition: mcci_tweetnacl_sign.h:162
mcci_tweetnacl.h
mcci_tweetnacl_sign
mcci_tweetnacl_result_t mcci_tweetnacl_sign(unsigned char *pSignedMessage, size_t *pSignedMessageSize, const unsigned char *pMessage, size_t messageSize, const mcci_tweetnacl_sign_privatekey_t *pPrivateKey)
sign a message (typically a hash of the real message)
mcci_tweetnacl_sign_privatekey_t
Private Key for TweetNaCl sign operations.
Definition: mcci_tweetnacl_sign.h:61
mcci_tweetnacl_sign_signature_t
Signature block for TweetNaCl sign operations.
Definition: mcci_tweetnacl_sign.h:68