sha3.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * cifra - embedded cryptography library
  3. * Written in 2014 by Joseph Birr-Pixton <jpixton@gmail.com>
  4. *
  5. * To the extent possible under law, the author(s) have dedicated all
  6. * copyright and related and neighboring rights to this software to the
  7. * public domain worldwide. This software is distributed without any
  8. * warranty.
  9. *
  10. * You should have received a copy of the CC0 Public Domain Dedication
  11. * along with this software. If not, see
  12. * <http://creativecommons.org/publicdomain/zero/1.0/>.
  13. */
  14. #ifndef SHA3_H
  15. #define SHA3_H
  16. #include <stddef.h>
  17. #include <stdint.h>
  18. #include "chash.h"
  19. /**
  20. * SHA3/Keccak
  21. * ===========
  22. * This implementation is compatible with FIPS 202,
  23. * rather than the original Keccak submission.
  24. *
  25. */
  26. /* .. c:macro:: CF_SHA3_224_HASHSZ
  27. * The output size of SHA3-224: 28 bytes. */
  28. #define CF_SHA3_224_HASHSZ 28
  29. /* .. c:macro:: CF_SHA3_256_HASHSZ
  30. * The output size of SHA3-256: 32 bytes. */
  31. #define CF_SHA3_256_HASHSZ 32
  32. /* .. c:macro:: CF_SHA3_384_HASHSZ
  33. * The output size of SHA3-384: 48 bytes. */
  34. #define CF_SHA3_384_HASHSZ 48
  35. /* .. c:macro:: CF_SHA3_512_HASHSZ
  36. * The output size of SHA3-512: 64 bytes. */
  37. #define CF_SHA3_512_HASHSZ 64
  38. /* .. c:macro:: CF_SHA3_224_BLOCKSZ
  39. * The block size of SHA3-224. */
  40. #define CF_SHA3_224_BLOCKSZ 144
  41. /* .. c:macro:: CF_SHA3_256_BLOCKSZ
  42. * The block size of SHA3-256. */
  43. #define CF_SHA3_256_BLOCKSZ 136
  44. /* .. c:macro:: CF_SHA3_384_BLOCKSZ
  45. * The block size of SHA3-384. */
  46. #define CF_SHA3_384_BLOCKSZ 104
  47. /* .. c:macro:: CF_SHA3_512_BLOCKSZ
  48. * The block size of SHA3-512. */
  49. #define CF_SHA3_512_BLOCKSZ 72
  50. /* We use bit-interleaved internal representation. This
  51. * stores a 64 bit quantity in two 32 bit words: one word
  52. * contains odd bits, the other even. This means 64-bit rotations
  53. * are cheaper to compute. */
  54. typedef struct
  55. {
  56. uint32_t odd, evn;
  57. } cf_sha3_bi;
  58. /* .. c:type:: cf_sha3_context
  59. * Incremental SHA3 hashing context.
  60. *
  61. * .. c:member:: cf_sha3_context.A
  62. * Intermediate state.
  63. *
  64. * .. c:member:: cf_sha3_context.partial
  65. * Unprocessed input.
  66. *
  67. * .. c:member:: cf_sha3_context.npartial
  68. * Number of bytes of unprocessed input.
  69. *
  70. * .. c:member:: cf_sha3_context.rate
  71. * Sponge absorption rate.
  72. *
  73. * .. c:member:: cf_sha3_context.rate
  74. * Sponge capacity.
  75. */
  76. typedef struct
  77. {
  78. /* State is a 5x5 block of 64-bit values, for Keccak-f[1600]. */
  79. cf_sha3_bi A[5][5];
  80. uint8_t partial[CF_SHA3_224_BLOCKSZ];
  81. size_t npartial;
  82. uint16_t rate, capacity; /* rate and capacity, in bytes. */
  83. } cf_sha3_context;
  84. /* -- _init functions -- */
  85. /* .. c:function:: $DECL */
  86. extern void cf_sha3_224_init(cf_sha3_context *ctx);
  87. /* .. c:function:: $DECL */
  88. extern void cf_sha3_256_init(cf_sha3_context *ctx);
  89. /* .. c:function:: $DECL */
  90. extern void cf_sha3_384_init(cf_sha3_context *ctx);
  91. /* .. c:function:: $DECL
  92. * Sets up `ctx` ready to hash a new message.
  93. */
  94. extern void cf_sha3_512_init(cf_sha3_context *ctx);
  95. /* -- _update functions -- */
  96. /* .. c:function:: $DECL */
  97. extern void cf_sha3_224_update(cf_sha3_context *ctx, const void *data, size_t nbytes);
  98. /* .. c:function:: $DECL */
  99. extern void cf_sha3_256_update(cf_sha3_context *ctx, const void *data, size_t nbytes);
  100. /* .. c:function:: $DECL */
  101. extern void cf_sha3_384_update(cf_sha3_context *ctx, const void *data, size_t nbytes);
  102. /* .. c:function:: $DECL
  103. * Hashes `nbytes` at `data`. Copies the data for processing later if there
  104. * isn't enough to make a full block.
  105. */
  106. extern void cf_sha3_512_update(cf_sha3_context *ctx, const void *data, size_t nbytes);
  107. /* -- _digest functions -- */
  108. /* .. c:function:: $DECL */
  109. extern void cf_sha3_224_digest(const cf_sha3_context *ctx, uint8_t hash[CF_SHA3_224_HASHSZ]);
  110. /* .. c:function:: $DECL */
  111. extern void cf_sha3_256_digest(const cf_sha3_context *ctx, uint8_t hash[CF_SHA3_256_HASHSZ]);
  112. /* .. c:function:: $DECL */
  113. extern void cf_sha3_384_digest(const cf_sha3_context *ctx, uint8_t hash[CF_SHA3_384_HASHSZ]);
  114. /* .. c:function:: $DECL
  115. * Finishes the hashing operation, writing result to `hash`.
  116. *
  117. * This leaves `ctx` unchanged.
  118. */
  119. extern void cf_sha3_512_digest(const cf_sha3_context *ctx, uint8_t hash[CF_SHA3_512_HASHSZ]);
  120. /* -- _digest_final functions -- */
  121. /* .. c:function:: $DECL */
  122. extern void cf_sha3_224_digest_final(cf_sha3_context *ctx, uint8_t hash[CF_SHA3_224_HASHSZ]);
  123. /* .. c:function:: $DECL */
  124. extern void cf_sha3_256_digest_final(cf_sha3_context *ctx, uint8_t hash[CF_SHA3_256_HASHSZ]);
  125. /* .. c:function:: $DECL */
  126. extern void cf_sha3_384_digest_final(cf_sha3_context *ctx, uint8_t hash[CF_SHA3_384_HASHSZ]);
  127. /* .. c:function:: $DECL
  128. * Finishes the hashing operation, writing result to `hash`.
  129. *
  130. * This destroys the contents of `ctx`.
  131. */
  132. extern void cf_sha3_512_digest_final(cf_sha3_context *ctx, uint8_t hash[CF_SHA3_512_HASHSZ]);
  133. /* .. c:var:: cf_sha3_224
  134. * .. c:var:: cf_sha3_256
  135. * .. c:var:: cf_sha3_384
  136. * .. c:var:: cf_sha3_512
  137. * Abstract interface to SHA3 functions. See :c:type:`cf_chash` for more information.
  138. */
  139. extern const cf_chash cf_sha3_224;
  140. extern const cf_chash cf_sha3_256;
  141. extern const cf_chash cf_sha3_384;
  142. extern const cf_chash cf_sha3_512;
  143. #endif