siphash.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*!
  2. \ingroup SipHash
  3. \brief This function initializes SipHash with a key for a MAC size.
  4. \return 0 Returned upon successfully initializing
  5. \return BAD_FUNC_ARG Returned when siphash or key is NULL
  6. \return BAD_FUNC_ARG Returned when outSz is neither 8 nor 16
  7. \param siphash pointer to the SipHash structure to use for MACing
  8. \param key pointer to the 16-byte array
  9. \param outSz number of bytes to output as MAC
  10. _Example_
  11. \code
  12. SipHash siphash[1];
  13. unsigned char key[16] = { ... };
  14. byte macSz = 8; // 8 or 16
  15. if ((ret = wc_InitSipHash(siphash, key, macSz)) != 0) {
  16. WOLFSSL_MSG("wc_InitSipHash failed");
  17. }
  18. else if ((ret = wc_SipHashUpdate(siphash, data, len)) != 0) {
  19. WOLFSSL_MSG("wc_SipHashUpdate failed");
  20. }
  21. else if ((ret = wc_SipHashFinal(siphash, mac, macSz)) != 0) {
  22. WOLFSSL_MSG("wc_SipHashFinal failed");
  23. }
  24. \endcode
  25. \sa wc_SipHash
  26. \sa wc_SipHashUpdate
  27. \sa wc_SipHashFinal
  28. */
  29. int wc_InitSipHash(SipHash* siphash, const unsigned char* key,
  30. unsigned char outSz);
  31. /*!
  32. \ingroup SipHash
  33. \brief Can be called to continually hash the provided byte
  34. array of length len.
  35. \return 0 Returned upon successfully adding the data to the MAC
  36. \return BAD_FUNC_ARG Returned when siphash is NULL
  37. \return BAD_FUNC_ARG Returned when in is NULL and inSz is not zero
  38. \param siphash pointer to the SipHash structure to use for MACing
  39. \param in the data to be MACed
  40. \param inSz size of data to be MACed
  41. _Example_
  42. \code
  43. SipHash siphash[1];
  44. byte data[] = { Data to be MACed };
  45. word32 len = sizeof(data);
  46. if ((ret = wc_InitSipHash(siphash, key, macSz)) != 0) {
  47. WOLFSSL_MSG("wc_InitSipHash failed");
  48. }
  49. else if ((ret = wc_SipHashUpdate(siphash, data, len)) != 0) {
  50. WOLFSSL_MSG("wc_SipHashUpdate failed");
  51. }
  52. else if ((ret = wc_SipHashFinal(siphash, mac, macSz)) != 0) {
  53. WOLFSSL_MSG("wc_SipHashFinal failed");
  54. }
  55. \endcode
  56. \sa wc_SipHash
  57. \sa wc_InitSipHash
  58. \sa wc_SipHashFinal
  59. */
  60. int wc_SipHashUpdate(SipHash* siphash, const unsigned char* in,
  61. word32 inSz);
  62. /*!
  63. \ingroup SipHash
  64. \brief Finalizes MACing of data. Result is placed into out.
  65. \return 0 Returned upon successfully finalizing.
  66. \return BAD_FUNC_ARG Returned when siphash of out is NULL
  67. \return BAD_FUNC_ARG Returned when outSz is not the same as the initialized
  68. value
  69. \param siphash pointer to the SipHash structure to use for MACing
  70. \param out Byte array to hold MAC value
  71. \param outSz number of bytes to output as MAC
  72. _Example_
  73. \code
  74. SipHash siphash[1];
  75. byte mac[8] = { ... }; // 8 or 16 bytes
  76. byte macSz = sizeof(mac);
  77. if ((ret = wc_InitSipHash(siphash, key, macSz)) != 0) {
  78. WOLFSSL_MSG("wc_InitSipHash failed");
  79. }
  80. else if ((ret = wc_SipHashUpdate(siphash, data, len)) != 0) {
  81. WOLFSSL_MSG("wc_SipHashUpdate failed");
  82. }
  83. else if ((ret = wc_SipHashFinal(siphash, mac, macSz)) != 0) {
  84. WOLFSSL_MSG("wc_SipHashFinal failed");
  85. }
  86. \endcode
  87. \sa wc_SipHash
  88. \sa wc_InitSipHash
  89. \sa wc_SipHashUpdate
  90. */
  91. int wc_SipHashFinal(SipHash* siphash, unsigned char* out,
  92. unsigned char outSz);
  93. /*!
  94. \ingroup SipHash
  95. \brief This function one-shots the data using SipHash to calculate a MAC
  96. based on the key.
  97. \return 0 Returned upon successfully MACing
  98. \return BAD_FUNC_ARG Returned when key or out is NULL
  99. \return BAD_FUNC_ARG Returned when in is NULL and inSz is not zero
  100. \return BAD_FUNC_ARG Returned when outSz is neither 8 nor 16
  101. \param key pointer to the 16-byte array
  102. \param in the data to be MACed
  103. \param inSz size of data to be MACed
  104. \param out Byte array to hold MAC value
  105. \param outSz number of bytes to output as MAC
  106. _Example_
  107. \code
  108. unsigned char key[16] = { ... };
  109. byte data[] = { Data to be MACed };
  110. word32 len = sizeof(data);
  111. byte mac[8] = { ... }; // 8 or 16 bytes
  112. byte macSz = sizeof(mac);
  113. if ((ret = wc_SipHash(key, data, len, mac, macSz)) != 0) {
  114. WOLFSSL_MSG("wc_SipHash failed");
  115. }
  116. \endcode
  117. \sa wc_InitSipHash
  118. \sa wc_SipHashUpdate
  119. \sa wc_SipHashFinal
  120. */
  121. int wc_SipHash(const unsigned char* key, const unsigned char* in,
  122. word32 inSz, unsigned char* out, unsigned char outSz);