KeccakSponge.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. Implementation by the Keccak, Keyak and Ketje Teams, namely, Guido Bertoni,
  3. Joan Daemen, Michaël Peeters, Gilles Van Assche and Ronny Van Keer, hereby
  4. denoted as "the implementer".
  5. For more information, feedback or questions, please refer to our websites:
  6. http://keccak.noekeon.org/
  7. http://keyak.noekeon.org/
  8. http://ketje.noekeon.org/
  9. To the extent possible under law, the implementer has waived all copyright
  10. and related or neighboring rights to the source code in this file.
  11. http://creativecommons.org/publicdomain/zero/1.0/
  12. */
  13. #ifndef _KeccakSponge_h_
  14. #define _KeccakSponge_h_
  15. /** General information
  16. *
  17. * The following type and functions are not actually implemented. Their
  18. * documentation is generic, with the prefix Prefix replaced by
  19. * - KeccakWidth200 for a sponge function based on Keccak-f[200]
  20. * - KeccakWidth400 for a sponge function based on Keccak-f[400]
  21. * - KeccakWidth800 for a sponge function based on Keccak-f[800]
  22. * - KeccakWidth1600 for a sponge function based on Keccak-f[1600]
  23. *
  24. * In all these functions, the rate and capacity must sum to the width of the
  25. * chosen permutation. For instance, to use the sponge function
  26. * Keccak[r=1344, c=256], one must use KeccakWidth1600_Sponge() or a combination
  27. * of KeccakWidth1600_SpongeInitialize(), KeccakWidth1600_SpongeAbsorb(),
  28. * KeccakWidth1600_SpongeAbsorbLastFewBits() and
  29. * KeccakWidth1600_SpongeSqueeze().
  30. *
  31. * The Prefix_SpongeInstance contains the sponge instance attributes for use
  32. * with the Prefix_Sponge* functions.
  33. * It gathers the state processed by the permutation as well as the rate,
  34. * the position of input/output bytes in the state and the phase
  35. * (absorbing or squeezing).
  36. */
  37. #ifdef DontReallyInclude_DocumentationOnly
  38. /** Function to evaluate the sponge function Keccak[r, c] in a single call.
  39. * @param rate The value of the rate r.
  40. * @param capacity The value of the capacity c.
  41. * @param input Pointer to the input message (before the suffix).
  42. * @param inputByteLen The length of the input message in bytes.
  43. * @param suffix Byte containing from 0 to 7 suffix bits
  44. * that must be absorbed after @a input.
  45. * These <i>n</i> bits must be in the least significant bit positions.
  46. * These bits must be delimited with a bit 1 at position <i>n</i>
  47. * (counting from 0=LSB to 7=MSB) and followed by bits 0
  48. * from position <i>n</i>+1 to position 7.
  49. * Some examples:
  50. * - If no bits are to be absorbed, then @a suffix must be 0x01.
  51. * - If the 2-bit sequence 0,0 is to be absorbed, @a suffix must be 0x04.
  52. * - If the 5-bit sequence 0,1,0,0,1 is to be absorbed, @a suffix must be 0x32.
  53. * - If the 7-bit sequence 1,1,0,1,0,0,0 is to be absorbed, @a suffix must be 0x8B.
  54. * .
  55. * @param output Pointer to the output buffer.
  56. * @param outputByteLen The desired number of output bytes.
  57. * @pre One must have r+c equal to the supported width of this implementation
  58. * and the rate a multiple of 8 bits (one byte) in this implementation.
  59. * @pre @a suffix ≠ 0x00
  60. * @return Zero if successful, 1 otherwise.
  61. */
  62. int Prefix_Sponge(unsigned int rate, unsigned int capacity, const unsigned char *input, size_t inputByteLen, unsigned char suffix, unsigned char *output, size_t outputByteLen);
  63. /**
  64. * Function to initialize the state of the Keccak[r, c] sponge function.
  65. * The phase of the sponge function is set to absorbing.
  66. * @param spongeInstance Pointer to the sponge instance to be initialized.
  67. * @param rate The value of the rate r.
  68. * @param capacity The value of the capacity c.
  69. * @pre One must have r+c equal to the supported width of this implementation
  70. * and the rate a multiple of 8 bits (one byte) in this implementation.
  71. * @return Zero if successful, 1 otherwise.
  72. */
  73. int Prefix_SpongeInitialize(Prefix_SpongeInstance *spongeInstance, unsigned int rate, unsigned int capacity);
  74. /**
  75. * Function to give input data bytes for the sponge function to absorb.
  76. * @param spongeInstance Pointer to the sponge instance initialized by Prefix_SpongeInitialize().
  77. * @param data Pointer to the input data.
  78. * @param dataByteLen The number of input bytes provided in the input data.
  79. * @pre The sponge function must be in the absorbing phase,
  80. * i.e., Prefix_SpongeSqueeze() or Prefix_SpongeAbsorbLastFewBits()
  81. * must not have been called before.
  82. * @return Zero if successful, 1 otherwise.
  83. */
  84. int Prefix_SpongeAbsorb(Prefix_SpongeInstance *spongeInstance, const unsigned char *data, size_t dataByteLen);
  85. /**
  86. * Function to give input data bits for the sponge function to absorb
  87. * and then to switch to the squeezing phase.
  88. * @param spongeInstance Pointer to the sponge instance initialized by Prefix_SpongeInitialize().
  89. * @param delimitedData Byte containing from 0 to 7 trailing bits
  90. * that must be absorbed.
  91. * These <i>n</i> bits must be in the least significant bit positions.
  92. * These bits must be delimited with a bit 1 at position <i>n</i>
  93. * (counting from 0=LSB to 7=MSB) and followed by bits 0
  94. * from position <i>n</i>+1 to position 7.
  95. * Some examples:
  96. * - If no bits are to be absorbed, then @a delimitedData must be 0x01.
  97. * - If the 2-bit sequence 0,0 is to be absorbed, @a delimitedData must be 0x04.
  98. * - If the 5-bit sequence 0,1,0,0,1 is to be absorbed, @a delimitedData must be 0x32.
  99. * - If the 7-bit sequence 1,1,0,1,0,0,0 is to be absorbed, @a delimitedData must be 0x8B.
  100. * .
  101. * @pre The sponge function must be in the absorbing phase,
  102. * i.e., Prefix_SpongeSqueeze() or Prefix_SpongeAbsorbLastFewBits()
  103. * must not have been called before.
  104. * @pre @a delimitedData ≠ 0x00
  105. * @return Zero if successful, 1 otherwise.
  106. */
  107. int Prefix_SpongeAbsorbLastFewBits(Prefix_SpongeInstance *spongeInstance, unsigned char delimitedData);
  108. /**
  109. * Function to squeeze output data from the sponge function.
  110. * If the sponge function was in the absorbing phase, this function
  111. * switches it to the squeezing phase
  112. * as if Prefix_SpongeAbsorbLastFewBits(spongeInstance, 0x01) was called.
  113. * @param spongeInstance Pointer to the sponge instance initialized by Prefix_SpongeInitialize().
  114. * @param data Pointer to the buffer where to store the output data.
  115. * @param dataByteLen The number of output bytes desired.
  116. * @return Zero if successful, 1 otherwise.
  117. */
  118. int Prefix_SpongeSqueeze(Prefix_SpongeInstance *spongeInstance, unsigned char *data, size_t dataByteLen);
  119. #endif
  120. #include <string.h>
  121. #include "align.h"
  122. #define KCP_DeclareSpongeStructure(prefix, size, alignment) \
  123. ALIGN(alignment) typedef struct prefix##_SpongeInstanceStruct { \
  124. unsigned char state[size]; \
  125. unsigned int rate; \
  126. unsigned int byteIOIndex; \
  127. int squeezing; \
  128. } prefix##_SpongeInstance;
  129. #define KCP_DeclareSpongeFunctions(prefix) \
  130. int prefix##_Sponge(unsigned int rate, unsigned int capacity, const unsigned char *input, size_t inputByteLen, unsigned char suffix, unsigned char *output, size_t outputByteLen); \
  131. int prefix##_SpongeInitialize(prefix##_SpongeInstance *spongeInstance, unsigned int rate, unsigned int capacity); \
  132. int prefix##_SpongeAbsorb(prefix##_SpongeInstance *spongeInstance, const unsigned char *data, size_t dataByteLen); \
  133. int prefix##_SpongeAbsorbLastFewBits(prefix##_SpongeInstance *spongeInstance, unsigned char delimitedData); \
  134. int prefix##_SpongeSqueeze(prefix##_SpongeInstance *spongeInstance, unsigned char *data, size_t dataByteLen);
  135. #ifndef KeccakP200_excluded
  136. #include "KeccakP-200-SnP.h"
  137. KCP_DeclareSpongeStructure(KeccakWidth200, KeccakP200_stateSizeInBytes, KeccakP200_stateAlignment)
  138. KCP_DeclareSpongeFunctions(KeccakWidth200)
  139. #endif
  140. #ifndef KeccakP400_excluded
  141. #include "KeccakP-400-SnP.h"
  142. KCP_DeclareSpongeStructure(KeccakWidth400, KeccakP400_stateSizeInBytes, KeccakP400_stateAlignment)
  143. KCP_DeclareSpongeFunctions(KeccakWidth400)
  144. #endif
  145. #ifndef KeccakP800_excluded
  146. #include "KeccakP-800-SnP.h"
  147. KCP_DeclareSpongeStructure(KeccakWidth800, KeccakP800_stateSizeInBytes, KeccakP800_stateAlignment)
  148. KCP_DeclareSpongeFunctions(KeccakWidth800)
  149. #endif
  150. #ifndef KeccakP1600_excluded
  151. #include "KeccakP-1600-SnP.h"
  152. KCP_DeclareSpongeStructure(KeccakWidth1600, KeccakP1600_stateSizeInBytes, KeccakP1600_stateAlignment)
  153. KCP_DeclareSpongeFunctions(KeccakWidth1600)
  154. #endif
  155. #ifndef KeccakP1600_excluded
  156. #include "KeccakP-1600-SnP.h"
  157. KCP_DeclareSpongeStructure(KeccakWidth1600_12rounds, KeccakP1600_stateSizeInBytes, KeccakP1600_stateAlignment)
  158. KCP_DeclareSpongeFunctions(KeccakWidth1600_12rounds)
  159. #endif
  160. #endif