siphash.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* siphash.h
  2. *
  3. * Copyright (C) 2006-2022 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifndef WOLF_CRYPT_SIPHASH_H
  22. #define WOLF_CRYPT_SIPHASH_H
  23. #include <wolfssl/wolfcrypt/types.h>
  24. #if defined(WOLFSSL_SIPHASH)
  25. /* DESCRIPTION
  26. *
  27. * SipHash is a PseudoRandom Function (PRF) that can be used with small
  28. * messages (less than 256 bytes).
  29. * SipHash can be used for Message Authentication Codes (MACs) and as such must
  30. * be passed a secret key.
  31. * https://eprint.iacr.org/2012/351.pdf
  32. *
  33. * SipHash is commonly used in hash tables.
  34. * Do not use this as a hash not as a general purpose MAC.
  35. *
  36. * WOLFSSL_SIPHASH_CROUNDS and WOLFSSL_SIPHASH_DROUNDS can be defined at build
  37. * time to change the algorithm.
  38. * Default is SipHash-2-4:
  39. * WOLFSSL_SIPHASH_CROUNDS = 2
  40. * WOLFSSL_SIPHASH_DROUNDS = 4
  41. */
  42. #ifndef WOLFSSL_SIPHASH_CROUNDS
  43. /* Number of rounds to perform in compression operation. */
  44. #define WOLFSSL_SIPHASH_CROUNDS 2
  45. #endif /* WOLFSSL_SIPHASH_CROUNDS */
  46. #ifndef WOLFSSL_SIPHASH_DROUNDS
  47. /* Number of rounds to perform in final operation. */
  48. #define WOLFSSL_SIPHASH_DROUNDS 4
  49. #endif /* WOLFSSL_SIPHASH_DROUNDS */
  50. enum {
  51. SIPHASH_KEY_SIZE = 16, /* Key size of SipHash. */
  52. SIPHASH_BLOCK_SIZE = 8, /* Block size of SipHash. */
  53. SIPHASH_MAC_SIZE_8 = 8, /* Output an 8 byte MAC. */
  54. SIPHASH_MAC_SIZE_16 = 16, /* Output a 16 byte MAC. */
  55. };
  56. typedef struct SipHash SipHash;
  57. struct SipHash {
  58. /* Internal state. */
  59. word64 v[4];
  60. /* Cached message data. */
  61. byte cache[SIPHASH_BLOCK_SIZE];
  62. /* Number of bytes cached. */
  63. byte cacheCnt;
  64. /* Number of output bytes. */
  65. byte outSz;
  66. /* Number of input bytes processed. */
  67. word32 inCnt;
  68. };
  69. #ifdef __cplusplus
  70. extern "C" {
  71. #endif
  72. WOLFSSL_API int wc_InitSipHash(SipHash* sipHash, const unsigned char* key,
  73. unsigned char outSz);
  74. WOLFSSL_API int wc_SipHashUpdate(SipHash* sipHash, const unsigned char* in,
  75. word32 inSz);
  76. WOLFSSL_API int wc_SipHashFinal(SipHash* sipHash, unsigned char* out,
  77. unsigned char outSz);
  78. WOLFSSL_API int wc_SipHash(const unsigned char* key, const unsigned char* in,
  79. word32 inSz, unsigned char* out, unsigned char outSz);
  80. #ifdef __cplusplus
  81. } /* extern "C" */
  82. #endif
  83. #endif /* NO_AES && WOLFSSL_SIPHASH */
  84. #endif /* WOLF_CRYPT_SIPHASH_H */