apr_siphash.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. SipHash reference C implementation
  18. Copyright (c) 2012-2014 Jean-Philippe Aumasson
  19. <[email protected]>
  20. Copyright (c) 2012-2014 Daniel J. Bernstein <[email protected]>
  21. To the extent possible under law, the author(s) have dedicated all copyright
  22. and related and neighboring rights to this software to the public domain
  23. worldwide. This software is distributed without any warranty.
  24. You should have received a copy of the CC0 Public Domain Dedication along
  25. with this software. If not, see
  26. <http://creativecommons.org/publicdomain/zero/1.0/>.
  27. */
  28. #ifndef APR_SIPHASH_H
  29. #define APR_SIPHASH_H
  30. #include "apr.h"
  31. #include "apu.h"
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. /**
  36. * @file apr_siphash.h
  37. * @brief APR-UTIL siphash library
  38. * "SipHash-c-d is a family of pseudorandom functions (a.k.a. keyed
  39. * hash functions) optimized for speed on short messages", designed by
  40. * Jean-Philippe Aumasson and Daniel J. Bernstein. It generates a 64bit
  41. * hash (or MAC) from the message and a 128bit key.
  42. * See http://cr.yp.to/siphash/siphash-20120620.pdf for the details,
  43. * c is the number of compression rounds, d the number of finalization
  44. * rounds; we also define fast implementations for c = 2 with d = 4 (aka
  45. * siphash-2-4), and c = 4 with d = 8 (aka siphash-4-8), as recommended
  46. * parameters per the authors.
  47. */
  48. /** size of the siphash digest */
  49. #define APR_SIPHASH_DSIZE 8
  50. /** size of the siphash key */
  51. #define APR_SIPHASH_KSIZE 16
  52. /**
  53. * @brief Computes SipHash-c-d, producing a 64bit (APR_SIPHASH_DSIZE) hash
  54. * from a message and a 128bit (APR_SIPHASH_KSIZE) secret key.
  55. * @param src The message
  56. * @param len The length of the message
  57. * @param key The secret key
  58. * @param c The number of compression rounds
  59. * @param d The number of finalization rounds
  60. * @return The hash value as a 64bit unsigned integer
  61. */
  62. APU_DECLARE(apr_uint64_t) apr_siphash(const void *src, apr_size_t len,
  63. const unsigned char key[APR_SIPHASH_KSIZE],
  64. unsigned int c, unsigned int d);
  65. /**
  66. * @brief Computes SipHash-c-d, producing a 64bit (APR_SIPHASH_DSIZE) hash
  67. * from a message and a 128bit (APR_SIPHASH_KSIZE) secret key, into a possibly
  68. * unaligned buffer (using the little endian representation as defined by the
  69. * authors for interoperabilty) usable as a MAC.
  70. * @param out The output buffer (or MAC)
  71. * @param src The message
  72. * @param len The length of the message
  73. * @param key The secret key
  74. * @param c The number of compression rounds
  75. * @param d The number of finalization rounds
  76. * @return The hash value as a 64bit unsigned integer
  77. */
  78. APU_DECLARE(void) apr_siphash_auth(unsigned char out[APR_SIPHASH_DSIZE],
  79. const void *src, apr_size_t len,
  80. const unsigned char key[APR_SIPHASH_KSIZE],
  81. unsigned int c, unsigned int d);
  82. /**
  83. * @brief Computes SipHash-2-4, producing a 64bit (APR_SIPHASH_DSIZE) hash
  84. * from a message and a 128bit (APR_SIPHASH_KSIZE) secret key.
  85. * @param src The message to hash
  86. * @param len The length of the message
  87. * @param key The secret key
  88. * @return The hash value as a 64bit unsigned integer
  89. */
  90. APU_DECLARE(apr_uint64_t) apr_siphash24(const void *src, apr_size_t len,
  91. const unsigned char key[APR_SIPHASH_KSIZE]);
  92. /**
  93. * @brief Computes SipHash-2-4, producing a 64bit (APR_SIPHASH_DSIZE) hash
  94. * from a message and a 128bit (APR_SIPHASH_KSIZE) secret key, into a possibly
  95. * unaligned buffer (using the little endian representation as defined by the
  96. * authors for interoperabilty) usable as a MAC.
  97. * @param out The output buffer (or MAC)
  98. * @param src The message
  99. * @param len The length of the message
  100. * @param key The secret key
  101. * @return The hash value as a 64bit unsigned integer
  102. */
  103. APU_DECLARE(void) apr_siphash24_auth(unsigned char out[APR_SIPHASH_DSIZE],
  104. const void *src, apr_size_t len,
  105. const unsigned char key[APR_SIPHASH_KSIZE]);
  106. /**
  107. * @brief Computes SipHash-4-8, producing a 64bit (APR_SIPHASH_DSIZE) hash
  108. * from a message and a 128bit (APR_SIPHASH_KSIZE) secret key.
  109. * @param src The message
  110. * @param len The length of the message
  111. * @param key The secret key
  112. * @return The hash value as a 64bit unsigned integer
  113. */
  114. APU_DECLARE(apr_uint64_t) apr_siphash48(const void *src, apr_size_t len,
  115. const unsigned char key[APR_SIPHASH_KSIZE]);
  116. /**
  117. * @brief Computes SipHash-4-8, producing a 64bit (APR_SIPHASH_DSIZE) hash
  118. * from a message and a 128bit (APR_SIPHASH_KSIZE) secret key, into a possibly
  119. * unaligned buffer (using the little endian representation as defined by the
  120. * authors for interoperabilty) usable as a MAC.
  121. * @param out The output buffer (or MAC)
  122. * @param src The message
  123. * @param len The length of the message
  124. * @param key The secret key
  125. * @return The hash value as a 64bit unsigned integer
  126. */
  127. APU_DECLARE(void) apr_siphash48_auth(unsigned char out[APR_SIPHASH_DSIZE],
  128. const void *src, apr_size_t len,
  129. const unsigned char key[APR_SIPHASH_KSIZE]);
  130. #ifdef __cplusplus
  131. }
  132. #endif
  133. #endif /* APR_SIPHASH_H */