poly1305.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*!
  2. \ingroup Poly1305
  3. \brief This function sets the key for a Poly1305 context structure,
  4. initializing it for hashing. Note: A new key should be set after
  5. generating a message hash with wc_Poly1305Final to ensure security.
  6. \return 0 Returned on successfully setting the key and initializing
  7. the Poly1305 structure
  8. \return BAD_FUNC_ARG Returned if the given key is not 32 bytes long,
  9. or the Poly1305 context is NULL
  10. \param ctx pointer to a Poly1305 structure to initialize
  11. \param key pointer to the buffer containing the key to use for hashing
  12. \param keySz size of the key in the buffer. Should be 32 bytes
  13. _Example_
  14. \code
  15. Poly1305 enc;
  16. byte key[] = { initialize with 32 byte key to use for hashing };
  17. wc_Poly1305SetKey(&enc, key, sizeof(key));
  18. \endcode
  19. \sa wc_Poly1305Update
  20. \sa wc_Poly1305Final
  21. */
  22. WOLFSSL_API int wc_Poly1305SetKey(Poly1305* poly1305, const byte* key,
  23. word32 kySz);
  24. /*!
  25. \ingroup Poly1305
  26. \brief This function updates the message to hash with the
  27. Poly1305 structure.
  28. \return 0 Returned on successfully updating the message to hash
  29. \return BAD_FUNC_ARG Returned if the Poly1305 structure is NULL
  30. \param ctx pointer to a Poly1305 structure for which to update
  31. the message to hash
  32. \param m pointer to the buffer containing the message which should
  33. be added to the hash
  34. \param bytes size of the message to hash
  35. _Example_
  36. \code
  37. Poly1305 enc;
  38. byte key[] = { }; // initialize with 32 byte key to use for encryption
  39. byte msg[] = { }; // initialize with message to hash
  40. wc_Poly1305SetKey(&enc, key, sizeof(key));
  41. if( wc_Poly1305Update(key, msg, sizeof(msg)) != 0 ) {
  42. // error updating message to hash
  43. }
  44. \endcode
  45. \sa wc_Poly1305SetKey
  46. \sa wc_Poly1305Final
  47. */
  48. WOLFSSL_API int wc_Poly1305Update(Poly1305* poly1305, const byte*, word32);
  49. /*!
  50. \ingroup Poly1305
  51. \brief This function calculates the hash of the input messages
  52. and stores the result in mac. After this is called, the key
  53. should be reset.
  54. \return 0 Returned on successfully computing the final MAC
  55. \return BAD_FUNC_ARG Returned if the Poly1305 structure is NULL
  56. \param ctx pointer to a Poly1305 structure with which to generate the MAC
  57. \param mac pointer to the buffer in which to store the MAC.
  58. Should be POLY1305_DIGEST_SIZE (16 bytes) wide
  59. _Example_
  60. \code
  61. Poly1305 enc;
  62. byte mac[POLY1305_DIGEST_SIZE]; // space for a 16 byte mac
  63. byte key[] = { }; // initialize with 32 byte key to use for encryption
  64. byte msg[] = { }; // initialize with message to hash
  65. wc_Poly1305SetKey(&enc, key, sizeof(key));
  66. wc_Poly1305Update(key, msg, sizeof(msg));
  67. if ( wc_Poly1305Final(&enc, mac) != 0 ) {
  68. // error computing final MAC
  69. }
  70. \endcode
  71. \sa wc_Poly1305SetKey
  72. \sa wc_Poly1305Update
  73. */
  74. WOLFSSL_API int wc_Poly1305Final(Poly1305* poly1305, byte* tag);
  75. /*!
  76. \ingroup Poly1305
  77. \brief Takes in an initialized Poly1305 struct that has a key
  78. loaded and creates a MAC (tag) using recent TLS AEAD padding scheme.
  79. \return 0 Success
  80. \return BAD_FUNC_ARG Returned if ctx, input, or tag is null or if
  81. additional is null and addSz is greater than 0 or if tagSz is less
  82. than WC_POLY1305_MAC_SZ.
  83. \param ctx Initialized Poly1305 struct to use
  84. \param additional Additional data to use
  85. \param addSz Size of additional buffer
  86. \param input Input buffer to create tag from
  87. \param sz Size of input buffer
  88. \param tag Buffer to hold created tag
  89. \param tagSz Size of input tag buffer (must be at least
  90. WC_POLY1305_MAC_SZ(16))
  91. _Example_
  92. \code
  93. Poly1305 ctx;
  94. byte key[] = { }; // initialize with 32 byte key to use for hashing
  95. byte additional[] = { }; // initialize with additional data
  96. byte msg[] = { }; // initialize with message
  97. byte tag[16];
  98. wc_Poly1305SetKey(&ctx, key, sizeof(key));
  99. if(wc_Poly1305_MAC(&ctx, additional, sizeof(additional), (byte*)msg,
  100. sizeof(msg), tag, sizeof(tag)) != 0)
  101. {
  102. // Handle the error
  103. }
  104. \endcode
  105. \sa wc_Poly1305SetKey
  106. \sa wc_Poly1305Update
  107. \sa wcPoly1305Final
  108. */
  109. WOLFSSL_API int wc_Poly1305_MAC(Poly1305* ctx, byte* additional, word32 addSz,
  110. byte* input, word32 sz, byte* tag, word32 tagSz);