MCCI TweetNaCl
TweetNaCl library adapted for embedded use
mcci_tweetnacl_hash.h
Go to the documentation of this file.
1 /*
2 
3 Module: mcci_tweetnacl_hash.h
4 
5 Function:
6  Like NaCl "crypto_hash.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_hash_h_
23 #define _mcci_tweetnacl_hash_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 low-level-functions Low-level functions
42 /// @{
43 /// \addtogroup crypto-hash Hashing
44 /// @{
45 
46 /****************************************************************************\
47 |
48 | Forward types
49 |
50 \****************************************************************************/
51 
52 /// \brief abstract type for SHA512 signature blocks
53 typedef struct mcci_tweetnacl_sha512_s mcci_tweetnacl_sha512_t;
55  {
56  /// the bytes of the SHA512 signature.
57  unsigned char bytes[512 / 8];
58  };
59 
60 /****************************************************************************\
61 |
62 | APIs
63 |
64 \****************************************************************************/
65 
66 ///
67 /// \brief Calculate sha512 hash of message
68 ///
69 /// \param[out] pOut is set to the signature
70 /// \param[in] pMessage is the message to be hashed
71 /// \param[in] nMessage is the length of the message in bytes
72 ///
73 /// \see https://nacl.cr.yp.to/hash.html
74 ///
75 static inline void
77  mcci_tweetnacl_sha512_t *pOut,
78  const unsigned char *pMessage,
79  size_t nMessage
80  )
81  {
82  extern int crypto_hash_sha512_tweet(unsigned char *,const unsigned char *,unsigned long long);
83  (void) crypto_hash_sha512_tweet(pOut->bytes, pMessage, nMessage);
84  }
85 
86 ///
87 /// \brief Partial calculation of sha512 hash of message
88 ///
89 /// \param[inout] pState carries the current state
90 /// \param[in] pMessage is the message to be hashed
91 /// \param[in] nMessage is the length of the message in bytes
92 ///
93 /// \return number of bytes not processed.
94 ///
95 /// \see https://nacl.cr.yp.to/hash.html
96 ///
97 static inline size_t mcci_tweetnacl_hashblocks_sha512(
98  mcci_tweetnacl_sha512_t *pState,
99  const unsigned char *pMessage,
100  size_t nMessage
101  )
102  {
103  extern int crypto_hashblocks_sha512_tweet(unsigned char *,const unsigned char *,unsigned long long);
104  return (size_t) crypto_hashblocks_sha512_tweet(
105  pState->bytes,
106  pMessage,
107  nMessage
108  );
109  }
110 
111 ///
112 /// \brief Partial calculation of sha512 hash of message
113 ///
114 /// \param[out] pState is set to the initialization vector
115 ///
116 /// \see https://nacl.cr.yp.to/hash.html
117 ///
119  mcci_tweetnacl_sha512_t *pState
120  )
121  {
122  extern int crypto_hashblocks_sha512_tweet_mcci_init(unsigned char *);
123  (void) crypto_hashblocks_sha512_tweet_mcci_init(
124  pState->bytes
125  );
126  }
127 
128 ///
129 /// \brief Finish partial calculation of sha512 hash of message
130 ///
131 /// \param[inout] pHash carries the current state
132 /// \param[in] pMessage is the message to be hashed
133 /// \param[in] nMessage is the length of the message in bytes
134 ///
135 /// \details SHA512 processes the message in 128-byte chunks.
136 /// To accomodate variable-length text, SHA512 always appends
137 /// some bytes, containing enough info to unambigiously represent the
138 /// size of the message, even though its padding. This routine does
139 /// that, assuming that all but nMessage % 128 bytes have already been
140 /// incorporated in the hash.
141 ///
142 /// \see https://nacl.cr.yp.to/hash.html
143 ///
145  mcci_tweetnacl_sha512_t *pHash,
146  const unsigned char *pMessage,
147  size_t nMessage
148  )
149  {
150  extern int crypto_hashblocks_sha512_tweet_mcci_finish(unsigned char *,const unsigned char *,unsigned long long);
151  (void) crypto_hashblocks_sha512_tweet_mcci_finish(
152  pHash->bytes,
153  pMessage,
154  nMessage
155  );
156  }
157 
158 /****************************************************************************\
159 |
160 | Post-Meta
161 |
162 \****************************************************************************/
163 
164 //--- close groups ---
165 /// @}
166 /// @}
167 
168 #ifdef __cplusplus
169 }
170 #endif
171 
172 #endif /* _mcci_tweetnacl_hash_h_ */
mcci_tweetnacl_hash_sha512
static void mcci_tweetnacl_hash_sha512(mcci_tweetnacl_sha512_t *pOut, const unsigned char *pMessage, size_t nMessage)
Calculate sha512 hash of message.
Definition: mcci_tweetnacl_hash.h:76
mcci_tweetnacl_sha512_s::bytes
unsigned char bytes[512/8]
the bytes of the SHA512 signature.
Definition: mcci_tweetnacl_hash.h:57
mcci_tweetnacl_hashblocks_sha512_init
static void mcci_tweetnacl_hashblocks_sha512_init(mcci_tweetnacl_sha512_t *pState)
Partial calculation of sha512 hash of message.
Definition: mcci_tweetnacl_hash.h:118
mcci_tweetnacl_hashblocks_sha512
static size_t mcci_tweetnacl_hashblocks_sha512(mcci_tweetnacl_sha512_t *pState, const unsigned char *pMessage, size_t nMessage)
Partial calculation of sha512 hash of message.
Definition: mcci_tweetnacl_hash.h:97
mcci_tweetnacl.h
mcci_tweetnacl_sha512_s
abstract type for SHA512 signature blocks
Definition: mcci_tweetnacl_hash.h:55
mcci_tweetnacl_hashblocks_sha512_finish
static void mcci_tweetnacl_hashblocks_sha512_finish(mcci_tweetnacl_sha512_t *pHash, const unsigned char *pMessage, size_t nMessage)
Finish partial calculation of sha512 hash of message.
Definition: mcci_tweetnacl_hash.h:144