auth_hmacsha512256.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include <stddef.h>
  2. #include <stdint.h>
  3. #include <string.h>
  4. #include "crypto_auth_hmacsha512.h"
  5. #include "crypto_auth_hmacsha512256.h"
  6. #include "crypto_hash_sha512.h"
  7. #include "crypto_verify_32.h"
  8. #include "randombytes.h"
  9. #include "utils.h"
  10. size_t
  11. crypto_auth_hmacsha512256_bytes(void)
  12. {
  13. return crypto_auth_hmacsha512256_BYTES;
  14. }
  15. size_t
  16. crypto_auth_hmacsha512256_keybytes(void)
  17. {
  18. return crypto_auth_hmacsha512256_KEYBYTES;
  19. }
  20. size_t
  21. crypto_auth_hmacsha512256_statebytes(void)
  22. {
  23. return sizeof(crypto_auth_hmacsha512256_state);
  24. }
  25. void
  26. crypto_auth_hmacsha512256_keygen(
  27. unsigned char k[crypto_auth_hmacsha512256_KEYBYTES])
  28. {
  29. randombytes_buf(k, crypto_auth_hmacsha512256_KEYBYTES);
  30. }
  31. int
  32. crypto_auth_hmacsha512256_init(crypto_auth_hmacsha512256_state *state,
  33. const unsigned char *key, size_t keylen)
  34. {
  35. return crypto_auth_hmacsha512_init((crypto_auth_hmacsha512_state *) state,
  36. key, keylen);
  37. }
  38. int
  39. crypto_auth_hmacsha512256_update(crypto_auth_hmacsha512256_state *state,
  40. const unsigned char *in,
  41. unsigned long long inlen)
  42. {
  43. return crypto_auth_hmacsha512_update((crypto_auth_hmacsha512_state *) state,
  44. in, inlen);
  45. }
  46. int
  47. crypto_auth_hmacsha512256_final(crypto_auth_hmacsha512256_state *state,
  48. unsigned char *out)
  49. {
  50. unsigned char out0[64];
  51. crypto_auth_hmacsha512_final((crypto_auth_hmacsha512_state *) state, out0);
  52. memcpy(out, out0, 32);
  53. return 0;
  54. }
  55. int
  56. crypto_auth_hmacsha512256(unsigned char *out, const unsigned char *in,
  57. unsigned long long inlen, const unsigned char *k)
  58. {
  59. crypto_auth_hmacsha512256_state state;
  60. crypto_auth_hmacsha512256_init(&state, k,
  61. crypto_auth_hmacsha512256_KEYBYTES);
  62. crypto_auth_hmacsha512256_update(&state, in, inlen);
  63. crypto_auth_hmacsha512256_final(&state, out);
  64. return 0;
  65. }
  66. int
  67. crypto_auth_hmacsha512256_verify(const unsigned char *h,
  68. const unsigned char *in,
  69. unsigned long long inlen,
  70. const unsigned char *k)
  71. {
  72. unsigned char correct[32];
  73. crypto_auth_hmacsha512256(correct, in, inlen, k);
  74. return crypto_verify_32(h, correct) | (-(h == correct)) |
  75. sodium_memcmp(correct, h, 32);
  76. }