ASCON Suite
xof.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_XOF_H
24 #define ASCON_XOF_H
25 
33 #include <ascon/permutation.h>
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
43 #define ASCON_HASH_SIZE 32
44 
49 #define ASCON_HASHA_SIZE ASCON_HASH_SIZE
50 
55 #define ASCON_XOF_RATE 8
56 
60 typedef struct
61 {
63  unsigned char count;
64  unsigned char mode;
67 
71 typedef struct
72 {
74  unsigned char count;
75  unsigned char mode;
78 
92 void ascon_xof(unsigned char *out, const unsigned char *in, size_t inlen);
93 
102 
116 void ascon_xof_init_fixed(ascon_xof_state_t *state, size_t outlen);
117 
141  (ascon_xof_state_t *state, const char *function_name,
142  const unsigned char *custom, size_t customlen, size_t outlen);
143 
155 
168 void ascon_xof_reinit_fixed(ascon_xof_state_t *state, size_t outlen);
169 
184  (ascon_xof_state_t *state, const char *function_name,
185  const unsigned char *custom, size_t customlen, size_t outlen);
186 
193 
203 void ascon_xof_absorb
204  (ascon_xof_state_t *state, const unsigned char *in, size_t inlen);
205 
216  (ascon_xof_state_t *state, unsigned char *out, size_t outlen);
217 
229 
240 void ascon_xof_copy(ascon_xof_state_t *dest, const ascon_xof_state_t *src);
241 
255 void ascon_xofa(unsigned char *out, const unsigned char *in, size_t inlen);
256 
265 
279 void ascon_xofa_init_fixed(ascon_xofa_state_t *state, size_t outlen);
280 
304  (ascon_xofa_state_t *state, const char *function_name,
305  const unsigned char *custom, size_t customlen, size_t outlen);
306 
318 
331 void ascon_xofa_reinit_fixed(ascon_xofa_state_t *state, size_t outlen);
332 
347  (ascon_xofa_state_t *state, const char *function_name,
348  const unsigned char *custom, size_t customlen, size_t outlen);
349 
356 
367  (ascon_xofa_state_t *state, const unsigned char *in, size_t inlen);
368 
379  (ascon_xofa_state_t *state, unsigned char *out, size_t outlen);
380 
392 
404 
405 #ifdef __cplusplus
406 } /* extern "C" */
407 
408 #include <ascon/utility.h>
409 
410 namespace ascon
411 {
412 
439 template<size_t outlen>
441 {
442 public:
450  {
451  if (outlen == 0)
452  ::ascon_xof_init(&m_state);
453  else
454  ::ascon_xof_init_fixed(&m_state, outlen);
455  }
456 
465  {
466  ::ascon_xof_copy(&m_state, &other.m_state);
467  }
468 
478  inline explicit xof_with_output_length
479  (const char *function_name, const unsigned char *custom = 0,
480  size_t customlen = 0)
481  {
483  (&m_state, function_name, custom, customlen, outlen);
484  }
485 
495  (const char *function_name, const ascon::byte_array &custom)
496  {
498  (&m_state, function_name, custom.data(), custom.size(), outlen);
499  }
500 
505  {
506  ::ascon_xof_free(&m_state);
507  }
508 
519  {
520  if (this != &other) {
521  ::ascon_xof_free(&m_state);
522  ::ascon_xof_copy(&m_state, &other.m_state);
523  }
524  return *this;
525  }
526 
530  inline void reset()
531  {
532  if (outlen == 0)
533  ::ascon_xof_reinit(&m_state);
534  else
535  ::ascon_xof_reinit_fixed(&m_state, outlen);
536  }
537 
544  inline void absorb(const unsigned char *data, size_t len)
545  {
546  ::ascon_xof_absorb(&m_state, data, len);
547  }
548 
558  inline void absorb(const char *str)
559  {
560  if (str) {
562  (&m_state, reinterpret_cast<const unsigned char *>(str),
563  ::strlen(str));
564  }
565  }
566 
572  inline void absorb(const ascon::byte_array& data)
573  {
574  ::ascon_xof_absorb(&m_state, data.data(), data.size());
575  }
576 
583  inline void squeeze(unsigned char *data, size_t len)
584  {
585  ::ascon_xof_squeeze(&m_state, data, len);
586  }
587 
596  {
597  ascon::byte_array vec(len);
598  ::ascon_xof_squeeze(&m_state, vec.data(), len);
599  return vec;
600  }
601 
612  inline void pad()
613  {
614  ::ascon_xof_pad(&m_state);
615  }
616 
622  inline ::ascon_xof_state_t *state() { return &m_state; }
623 
629  inline const ::ascon_xof_state_t *state() const { return &m_state; }
630 
631 #if !defined(ARDUINO) && !defined(ASCON_NO_STL)
632 
639  inline void absorb(const std::string& str)
640  {
642  (&m_state, reinterpret_cast<const unsigned char *>(str.data()),
643  str.size());
644  }
645 
646 #elif defined(ARDUINO)
647 
654  inline void absorb(const String& str)
655  {
657  (&m_state, reinterpret_cast<const unsigned char *>(str.c_str()),
658  str.length());
659  }
660 
661 #endif /* ARDUINO */
662 
663 private:
664  ::ascon_xof_state_t m_state;
665 };
666 
693 template<size_t outlen>
695 {
696 public:
704  {
705  if (outlen == 0)
706  ::ascon_xofa_init(&m_state);
707  else
708  ::ascon_xofa_init_fixed(&m_state, outlen);
709  }
710 
719  {
720  ::ascon_xofa_copy(&m_state, &other.m_state);
721  }
722 
732  inline explicit xofa_with_output_length
733  (const char *function_name, const unsigned char *custom = 0,
734  size_t customlen = 0)
735  {
737  (&m_state, function_name, custom, customlen, outlen);
738  }
739 
749  (const char *function_name, const ascon::byte_array &custom)
750  {
752  (&m_state, function_name, custom.data(), custom.size(), outlen);
753  }
754 
759  {
760  ::ascon_xofa_free(&m_state);
761  }
762 
773  {
774  if (this != &other) {
775  ::ascon_xofa_free(&m_state);
776  ::ascon_xofa_copy(&m_state, &other.m_state);
777  }
778  return *this;
779  }
780 
784  inline void reset()
785  {
786  if (outlen == 0)
787  ::ascon_xofa_reinit(&m_state);
788  else
789  ::ascon_xofa_reinit_fixed(&m_state, outlen);
790  }
791 
798  inline void absorb(const unsigned char *data, size_t len)
799  {
800  ::ascon_xofa_absorb(&m_state, data, len);
801  }
802 
812  inline void absorb(const char *str)
813  {
814  if (str)
815  ::ascon_xofa_absorb(&m_state, str, ::strlen(str));
816  }
817 
823  inline void absorb(const ascon::byte_array& data)
824  {
825  ::ascon_xofa_absorb(&m_state, data.data(), data.size());
826  }
827 
834  inline void squeeze(unsigned char *data, size_t len)
835  {
836  ::ascon_xofa_squeeze(&m_state, data, len);
837  }
838 
846  inline ascon::byte_array squeeze(size_t len)
847  {
848  ascon::byte_array vec(len);
849  ::ascon_xofa_squeeze(&m_state, vec.data(), len);
850  return vec;
851  }
852 
863  inline void pad()
864  {
865  ::ascon_xofa_pad(&m_state);
866  }
867 
873  inline ::ascon_xofa_state_t *state() { return &m_state; }
874 
880  inline const ::ascon_xofa_state_t *state() const { return &m_state; }
881 
882 #if !defined(ARDUINO) && !defined(ASCON_NO_STL)
883 
890  inline void absorb(const std::string& str)
891  {
892  ::ascon_xofa_absorb(&m_state, str.data(), str.size());
893  }
894 
895 #elif defined(ARDUINO)
896 
903  inline void absorb(const String& str)
904  {
905  ::ascon_xofa_absorb(&m_state, str.c_str(), str.length());
906  }
907 
908 #endif /* ARDUINO */
909 
910 private:
911  ::ascon_xofa_state_t m_state;
912 };
913 
929 
945 
946 } /* namespace ascon */
947 
948 #endif /* __cplusplus */
949 
950 #endif
ASCON-XOF with a specific output length.
Definition: xof.h:441
void squeeze(unsigned char *data, size_t len)
Squeezes output data from this ASCON-XOF object.
Definition: xof.h:583
void absorb(const std::string &str)
Absorbs the contents of a standard C++ string into this ASCON-XOF object.
Definition: xof.h:639
const ::ascon_xof_state_t * state() const
Gets a constant reference to the C version of the ASCON-XOF state.
Definition: xof.h:629
xof_with_output_length(const char *function_name, const unsigned char *custom=0, size_t customlen=0)
Constructs a new ASCON-XOF object with a named function and customization string.
Definition: xof.h:479
void reset()
Resets this ASCON-XOF object back to the initial state.
Definition: xof.h:530
void absorb(const ascon::byte_array &data)
Absorbs the contents of a byte array into this ASCON-XOF object.
Definition: xof.h:572
inline ::ascon_xof_state_t * state()
Gets a reference to the C version of the ASCON-XOF state.
Definition: xof.h:622
~xof_with_output_length()
Destroys this ASCON-XOF object.
Definition: xof.h:504
xof_with_output_length()
Constucts a new ASCON-XOF object.
Definition: xof.h:449
void pad()
Absorbs enough zeroes into this ASCON-XOF object to pad the input to the next multiple of the block r...
Definition: xof.h:612
void absorb(const unsigned char *data, size_t len)
Absorbs more input data into this ASCON-XOF object.
Definition: xof.h:544
xof_with_output_length(const char *function_name, const ascon::byte_array &custom)
Constructs a new ASCON-XOF object with a named function and customization string.
Definition: xof.h:495
xof_with_output_length(const ascon::xof_with_output_length< outlen > &other)
Constructs a copy of another ASCON-XOF object.
Definition: xof.h:464
void absorb(const char *str)
Absorbs the contents of a NUL-terminated C string into this ASCON-XOF object.
Definition: xof.h:558
ascon::byte_array squeeze(size_t len)
Squeezes data out of this ASCON-XOF object as a byte array.
Definition: xof.h:595
ASCON-XOFA with a specific output length.
Definition: xof.h:695
void absorb(const ascon::byte_array &data)
Absorbs the contents of a byte array into this ASCON-XOFA object.
Definition: xof.h:823
xofa_with_output_length()
Constucts a new ASCON-XOFA object.
Definition: xof.h:703
void absorb(const std::string &str)
Absorbs the contents of a standard C++ string into this ASCON-XOFA object.
Definition: xof.h:890
void pad()
Absorbs enough zeroes into this ASCON-XOFA object to pad the input to the next multiple of the block ...
Definition: xof.h:863
void absorb(const char *str)
Absorbs the contents of a NUL-terminated C string into this ASCON-XOFA object.
Definition: xof.h:812
inline ::ascon_xofa_state_t * state()
Gets a reference to the C version of the ASCON-XOFA state.
Definition: xof.h:873
xofa_with_output_length(const ascon::xofa_with_output_length< outlen > &other)
Constructs a copy of another ASCON-XOFA object.
Definition: xof.h:718
void absorb(const unsigned char *data, size_t len)
Absorbs more input data into this ASCON-XOFA object.
Definition: xof.h:798
void reset()
Resets this ASCON-XOFA object back to the initial state.
Definition: xof.h:784
void squeeze(unsigned char *data, size_t len)
Squeezes output data from this ASCON-XOFA object.
Definition: xof.h:834
xofa_with_output_length(const char *function_name, const unsigned char *custom=0, size_t customlen=0)
Constructs a new ASCON-XOFA object with a named function and customization string.
Definition: xof.h:733
const ::ascon_xofa_state_t * state() const
Gets a constant reference to the C version of the ASCON-XOFA state.
Definition: xof.h:880
~xofa_with_output_length()
Destroys this ASCON-XOFA object.
Definition: xof.h:758
xofa_with_output_length(const char *function_name, const ascon::byte_array &custom)
Constructs a new ASCON-XOFA object with a named function and customization string.
Definition: xof.h:749
ascon::byte_array squeeze(size_t len)
Squeezes data out of this ASCON-XOFA object as a byte array.
Definition: xof.h:846
Definition: hash.h:211
std::vector< unsigned char > byte_array
C++ type for an array of bytes.
Definition: utility.h:109
xof_with_output_length< 0 > xof
ASCON-XOF object with arbitrary-length output.
Definition: xof.h:928
xofa_with_output_length< 0 > xofa
ASCON-XOFA object with arbitrary-length output.
Definition: xof.h:944
Direct access to the ASCON permutation primitive.
ascon_state_t state
[snippet_key]
Definition: snippets.c:2
unsigned char data[8]
[snippet_key]
Definition: snippets.c:14
State information for ASCON-XOF incremental mode.
Definition: xof.h:61
ascon_state_t state
Definition: xof.h:62
unsigned char mode
Definition: xof.h:64
unsigned char count
Definition: xof.h:63
State information for ASCON-XOFA incremental mode.
Definition: xof.h:72
unsigned char mode
Definition: xof.h:75
unsigned char count
Definition: xof.h:74
ascon_state_t state
Definition: xof.h:73
Structure of the internal state of the ASCON permutation.
Definition: permutation.h:63
System utilities of use to applications that use ASCON.
void ascon_xof_free(ascon_xof_state_t *state)
Frees the ASCON-XOF state and destroys any sensitive material.
Definition: ascon-xof.c:218
void ascon_xof_pad(ascon_xof_state_t *state)
Absorbs enough zeroes into an ASCON-XOF state to pad the input to the next multiple of the block rate...
Definition: ascon-xof.c:329
void ascon_xofa_squeeze(ascon_xofa_state_t *state, unsigned char *out, size_t outlen)
Squeezes output data from an ASCON-XOFA state.
Definition: ascon-xofa.c:277
void ascon_xof_reinit(ascon_xof_state_t *state)
Re-initializes the state for an ASCON-XOF hashing operation.
Definition: ascon-xof.c:183
void ascon_xofa_reinit(ascon_xofa_state_t *state)
Re-initializes the state for an ASCON-XOFA hashing operation.
Definition: ascon-xofa.c:181
void ascon_xofa_reinit_fixed(ascon_xofa_state_t *state, size_t outlen)
Re-initializes the state for an incremental ASCON-XOFA operation, with a fixed output length.
Definition: ascon-xofa.c:192
void ascon_xofa(unsigned char *out, const unsigned char *in, size_t inlen)
Hashes a block of input data with ASCON-XOFA and generates a fixed-length 32 byte output.
Definition: ascon-xofa.c:26
void ascon_xof_absorb(ascon_xof_state_t *state, const unsigned char *in, size_t inlen)
Absorbs more input data into an ASCON-XOF state.
Definition: ascon-xof.c:229
void ascon_xofa_reinit_custom(ascon_xofa_state_t *state, const char *function_name, const unsigned char *custom, size_t customlen, size_t outlen)
Re-nitializes the state for an incremental ASCON-XOFA operation, with a named function,...
Definition: ascon-xofa.c:204
void ascon_xof_init(ascon_xof_state_t *state)
Initializes the state for an ASCON-XOF hashing operation.
Definition: ascon-xof.c:37
void ascon_xofa_absorb(ascon_xofa_state_t *state, const unsigned char *in, size_t inlen)
Absorbs more input data into an ASCON-XOFA state.
Definition: ascon-xofa.c:227
void ascon_xofa_init_custom(ascon_xofa_state_t *state, const char *function_name, const unsigned char *custom, size_t customlen, size_t outlen)
Initializes the state for an incremental ASCON-XOFA operation, with a named function,...
Definition: ascon-xofa.c:144
void ascon_xofa_init_fixed(ascon_xofa_state_t *state, size_t outlen)
Initializes the state for an incremental ASCON-XOFA operation, with a fixed output length.
Definition: ascon-xofa.c:72
void ascon_xof_init_custom(ascon_xof_state_t *state, const char *function_name, const unsigned char *custom, size_t customlen, size_t outlen)
Initializes the state for an incremental ASCON-XOF operation, with a named function,...
Definition: ascon-xof.c:146
void ascon_xof_reinit_fixed(ascon_xof_state_t *state, size_t outlen)
Re-initializes the state for an incremental ASCON-XOF operation, with a fixed output length.
Definition: ascon-xof.c:194
void ascon_xof_reinit_custom(ascon_xof_state_t *state, const char *function_name, const unsigned char *custom, size_t customlen, size_t outlen)
Re-nitializes the state for an incremental ASCON-XOF operation, with a named function,...
Definition: ascon-xof.c:206
void ascon_xof_copy(ascon_xof_state_t *dest, const ascon_xof_state_t *src)
Clones a copy of an ASCON-XOF state.
Definition: ascon-xof.c:344
void ascon_xof_squeeze(ascon_xof_state_t *state, unsigned char *out, size_t outlen)
Squeezes output data from an ASCON-XOF state.
Definition: ascon-xof.c:279
void ascon_xofa_pad(ascon_xofa_state_t *state)
Absorbs enough zeroes into an ASCON-XOFA state to pad the input to the next multiple of the block rat...
Definition: ascon-xofa.c:328
void ascon_xofa_init(ascon_xofa_state_t *state)
Initializes the state for an ASCON-XOFA hashing operation.
Definition: ascon-xofa.c:35
void ascon_xof(unsigned char *out, const unsigned char *in, size_t inlen)
Hashes a block of input data with ASCON-XOF and generates a fixed-length 32 byte output.
Definition: ascon-xof.c:28
void ascon_xofa_copy(ascon_xofa_state_t *dest, const ascon_xofa_state_t *src)
Clones a copy of an ASCON-XOFA state.
Definition: ascon-xofa.c:343
void ascon_xofa_free(ascon_xofa_state_t *state)
Frees the ASCON-XOFA state and destroys any sensitive material.
Definition: ascon-xofa.c:216
void ascon_xof_init_fixed(ascon_xof_state_t *state, size_t outlen)
Initializes the state for an incremental ASCON-XOF operation, with a fixed output length.
Definition: ascon-xof.c:74