ASCON Suite
hash.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Southern Storm Software, Pty Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef ASCON_HASH_H
24 #define ASCON_HASH_H
25 
33 #include <ascon/xof.h>
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
42 typedef struct
43 {
47 
51 typedef struct
52 {
56 
67 void ascon_hash(unsigned char *out, const unsigned char *in, size_t inlen);
68 
77 
89 
96 
107  (ascon_hash_state_t *state, const unsigned char *in, size_t inlen);
108 
117 void ascon_hash_finalize(ascon_hash_state_t *state, unsigned char *out);
118 
129 void ascon_hash_copy
130  (ascon_hash_state_t *dest, const ascon_hash_state_t *src);
131 
142 void ascon_hasha(unsigned char *out, const unsigned char *in, size_t inlen);
143 
152 
164 
171 
182  (ascon_hasha_state_t *state, const unsigned char *in, size_t inlen);
183 
192 void ascon_hasha_finalize(ascon_hasha_state_t *state, unsigned char *out);
193 
204 void ascon_hasha_copy
205  (ascon_hasha_state_t *dest, const ascon_hasha_state_t *src);
206 
207 #ifdef __cplusplus
208 } /* extern "C" */
209 
210 namespace ascon
211 {
212 
216 class hash
217 {
218 public:
222  inline hash()
223  {
224  ::ascon_hash_init(&m_state);
225  }
226 
232  inline hash(const ascon::hash &other)
233  {
234  ::ascon_hash_copy(&m_state, &other.m_state);
235  }
236 
240  inline ~hash()
241  {
242  ::ascon_hash_free(&m_state);
243  }
244 
252  inline hash &operator=(const ascon::hash &other)
253  {
254  if (this != &other) {
255  ::ascon_hash_free(&m_state);
256  ::ascon_hash_copy(&m_state, &other.m_state);
257  }
258  return *this;
259  }
260 
264  inline void reset()
265  {
266  ::ascon_hash_reinit(&m_state);
267  }
268 
275  inline void update(const unsigned char *data, size_t len)
276  {
277  ::ascon_hash_update(&m_state, data, len);
278  }
279 
289  inline void update(const char *str)
290  {
291  if (str) {
293  (&m_state, reinterpret_cast<const unsigned char *>(str),
294  ::strlen(str));
295  }
296  }
297 
303  inline void update(const ascon::byte_array& data)
304  {
305  ::ascon_hash_update(&m_state, data.data(), data.size());
306  }
307 
313  inline void finalize(unsigned char digest[ASCON_HASH_SIZE])
314  {
315  ::ascon_hash_finalize(&m_state, digest);
316  }
317 
325  {
327  ::ascon_hash_finalize(&m_state, vec.data());
328  return vec;
329  }
330 
338  static inline void digest
339  (unsigned char result[ASCON_HASH_SIZE],
340  const unsigned char *data, size_t len)
341  {
342  ::ascon_hash(result, data, len);
343  }
344 
350  inline ::ascon_hash_state_t *state() { return &m_state; }
351 
358  inline const ::ascon_hash_state_t *state() const { return &m_state; }
359 
360 #if !defined(ARDUINO) && !defined(ASCON_NO_STL)
361 
368  inline void update(const std::string& str)
369  {
371  (&m_state, reinterpret_cast<const unsigned char *>(str.data()),
372  str.size());
373  }
374 
375 #elif defined(ARDUINO)
376 
383  inline void update(const String& str)
384  {
386  (&m_state, reinterpret_cast<const unsigned char *>(str.c_str()),
387  str.length());
388  }
389 
390 #endif /* ARDUINO */
391 
392 private:
393  ::ascon_hash_state_t m_state;
394 };
395 
399 class hasha
400 {
401 public:
405  inline hasha()
406  {
407  ::ascon_hasha_init(&m_state);
408  }
409 
415  inline hasha(const ascon::hasha &other)
416  {
417  ::ascon_hasha_copy(&m_state, &other.m_state);
418  }
419 
423  inline ~hasha()
424  {
425  ::ascon_hasha_free(&m_state);
426  }
427 
435  inline hasha &operator=(const ascon::hasha &other)
436  {
437  if (this != &other) {
438  ::ascon_hasha_free(&m_state);
439  ::ascon_hasha_copy(&m_state, &other.m_state);
440  }
441  return *this;
442  }
443 
447  inline void reset()
448  {
449  ::ascon_hasha_reinit(&m_state);
450  }
451 
458  inline void update(const unsigned char *data, size_t len)
459  {
460  ::ascon_hasha_update(&m_state, data, len);
461  }
462 
472  inline void update(const char *str)
473  {
474  if (str) {
476  (&m_state, reinterpret_cast<const unsigned char *>(str),
477  ::strlen(str));
478  }
479  }
480 
486  inline void update(const ascon::byte_array& data)
487  {
488  ::ascon_hasha_update(&m_state, data.data(), data.size());
489  }
490 
496  inline void finalize(unsigned char digest[ASCON_HASHA_SIZE])
497  {
498  ::ascon_hasha_finalize(&m_state, digest);
499  }
500 
508  {
510  ::ascon_hasha_finalize(&m_state, vec.data());
511  return vec;
512  }
513 
521  static inline void digest
522  (unsigned char result[ASCON_HASH_SIZE],
523  const unsigned char *data, size_t len)
524  {
525  ::ascon_hasha(result, data, len);
526  }
527 
533  inline ::ascon_hasha_state_t *state() { return &m_state; }
534 
541  inline const ::ascon_hasha_state_t *state() const { return &m_state; }
542 
543 #if !defined(ARDUINO) && !defined(ASCON_NO_STL)
544 
551  inline void update(const std::string& str)
552  {
554  (&m_state, reinterpret_cast<const unsigned char *>(str.data()),
555  str.size());
556  }
557 
558 #elif defined(ARDUINO)
559 
566  inline void update(const String& str)
567  {
569  (&m_state, reinterpret_cast<const unsigned char *>(str.c_str()),
570  str.length());
571  }
572 
573 #endif /* ARDUINO */
574 
575 private:
576  ::ascon_hasha_state_t m_state;
577 };
578 
579 } /* namespace ascon */
580 
581 #endif /* __cplusplus */
582 
583 #endif
ASCON-HASH digest algorithm.
Definition: hash.h:217
ascon::byte_array finalize()
Finalizes this ASCON-HASH object and returns the digest as a byte array.
Definition: hash.h:324
void update(const std::string &str)
Updates this ASCON-HASH object with the contents of a standard C++ string.
Definition: hash.h:368
hash()
Constructs a new ASCON-HASH object.
Definition: hash.h:222
void finalize(unsigned char digest[ASCON_HASH_SIZE])
Finalizes this ASCON-HASH object and returns the digest.
Definition: hash.h:313
void update(const unsigned char *data, size_t len)
Updates this ASCON-HASH object with new input data.
Definition: hash.h:275
inline ::ascon_hash_state_t * state()
Gets a reference to the C version of the ASCON-HASH state.
Definition: hash.h:350
void update(const char *str)
Updates this ASCON-HASH object with the contents of a NUL-terminated C string.
Definition: hash.h:289
~hash()
Destroys this ASCON-HASH object.
Definition: hash.h:240
hash & operator=(const ascon::hash &other)
Copies the state of another ASCON-HASH object into this one.
Definition: hash.h:252
static void digest(unsigned char result[ASCON_HASH_SIZE], const unsigned char *data, size_t len)
Computes the ASCON-HASH digest of a block of input data.
Definition: hash.h:339
const ::ascon_hash_state_t * state() const
Gets a constant reference to the C version of the ASCON-HASH state.
Definition: hash.h:358
void reset()
Resets this ASCON-HASH object back to its initial state.
Definition: hash.h:264
hash(const ascon::hash &other)
Constructs a copy of another ASCON-HASH object.
Definition: hash.h:232
void update(const ascon::byte_array &data)
Updates this ASCON-HASH object with the contents of a byte array.
Definition: hash.h:303
ASCON-HASHA digest algorithm.
Definition: hash.h:400
hasha()
Constructs a new ASCON-HASHA object.
Definition: hash.h:405
void update(const char *str)
Updates this ASCON-HASHA object with the contents of a NUL-terminated C string.
Definition: hash.h:472
ascon::byte_array finalize()
Finalizes this ASCON-HASHA object and returns the digest as a byte array.
Definition: hash.h:507
void reset()
Resets this ASCON-HASHA object back to its initial state.
Definition: hash.h:447
void update(const std::string &str)
Updates this ASCON-HASHA object with the contents of a standard C++ string.
Definition: hash.h:551
void update(const unsigned char *data, size_t len)
Updates this ASCON-HASHA object with new input data.
Definition: hash.h:458
inline ::ascon_hasha_state_t * state()
Gets a reference to the C version of the ASCON-HASHA state.
Definition: hash.h:533
hasha(const ascon::hasha &other)
Constructs a copy of another ASCON-HASHA object.
Definition: hash.h:415
~hasha()
Destroys this ASCON-HASHA object.
Definition: hash.h:423
void update(const ascon::byte_array &data)
Updates this ASCON-HASHA object with the contents of a byte array.
Definition: hash.h:486
const ::ascon_hasha_state_t * state() const
Gets a constant reference to the C version of the ASCON-HASHA state.
Definition: hash.h:541
hasha & operator=(const ascon::hasha &other)
Copies the state of another ASCON-HASHA object into this one.
Definition: hash.h:435
static void digest(unsigned char result[ASCON_HASH_SIZE], const unsigned char *data, size_t len)
Computes the ASCON-HASHA digest of a block of input data.
Definition: hash.h:522
void finalize(unsigned char digest[ASCON_HASHA_SIZE])
Finalizes this ASCON-HASHA object and returns the digest.
Definition: hash.h:496
void ascon_hash_init(ascon_hash_state_t *state)
Initializes the state for an ASCON-HASH hashing operation.
Definition: ascon-hash.c:36
void ascon_hasha_finalize(ascon_hasha_state_t *state, unsigned char *out)
Returns the final hash value from an ASCON-HASHA hashing operation.
Definition: ascon-hasha.c:95
void ascon_hasha_reinit(ascon_hasha_state_t *state)
Re-initializes the state for an ASCON-HASHA hashing operation.
Definition: ascon-hasha.c:73
void ascon_hash_update(ascon_hash_state_t *state, const unsigned char *in, size_t inlen)
Updates an ASCON-HASH state with more input data.
Definition: ascon-hash.c:90
void ascon_hash_finalize(ascon_hash_state_t *state, unsigned char *out)
Returns the final hash value from an ASCON-HASH hashing operation.
Definition: ascon-hash.c:95
void ascon_hasha(unsigned char *out, const unsigned char *in, size_t inlen)
Hashes a block of input data with ASCON-HASHA.
Definition: ascon-hasha.c:27
void ascon_hasha_init(ascon_hasha_state_t *state)
Initializes the state for an ASCON-HASHA hashing operation.
Definition: ascon-hasha.c:36
void ascon_hasha_free(ascon_hasha_state_t *state)
Frees the ASCON-HASHA state and destroys any sensitive material.
Definition: ascon-hasha.c:84
void ascon_hasha_update(ascon_hasha_state_t *state, const unsigned char *in, size_t inlen)
Updates an ASCON-HASHA state with more input data.
Definition: ascon-hasha.c:90
void ascon_hash(unsigned char *out, const unsigned char *in, size_t inlen)
Hashes a block of input data with ASCON-HASH.
Definition: ascon-hash.c:27
void ascon_hasha_copy(ascon_hasha_state_t *dest, const ascon_hasha_state_t *src)
Clones a copy of an ASCON-HASHA state.
Definition: ascon-hasha.c:100
void ascon_hash_free(ascon_hash_state_t *state)
Frees the ASCON-HASH state and destroys any sensitive material.
Definition: ascon-hash.c:84
void ascon_hash_copy(ascon_hash_state_t *dest, const ascon_hash_state_t *src)
Clones a copy of an ASCON-HASH state.
Definition: ascon-hash.c:100
void ascon_hash_reinit(ascon_hash_state_t *state)
Re-initializes the state for an ASCON-HASH hashing operation.
Definition: ascon-hash.c:73
Definition: hash.h:211
std::vector< unsigned char > byte_array
C++ type for an array of bytes.
Definition: utility.h:109
ascon_state_t state
[snippet_key]
Definition: snippets.c:2
unsigned char data[8]
[snippet_key]
Definition: snippets.c:14
State information for the ASCON-HASH incremental mode.
Definition: hash.h:43
ascon_xof_state_t xof
Definition: hash.h:44
State information for the ASCON-HASHA incremental mode.
Definition: hash.h:52
ascon_xofa_state_t xof
Definition: hash.h:53
State information for ASCON-XOF incremental mode.
Definition: xof.h:61
State information for ASCON-XOFA incremental mode.
Definition: xof.h:72
ASCON-XOF and ASCON-XOFA extensible output functions (XOF's).
#define ASCON_HASH_SIZE
Size of the hash output for ASCON-HASH and the default hash output size for ASCON-XOF.
Definition: xof.h:43
#define ASCON_HASHA_SIZE
Size of the hash output for ASCON-HASHA and the default hash output size for ASCON-XOFA.
Definition: xof.h:49