generichash_blake2b.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include <assert.h>
  2. #include <limits.h>
  3. #include <stdint.h>
  4. #include "blake2.h"
  5. #include "crypto_generichash_blake2b.h"
  6. #include "private/common.h"
  7. #include "private/implementations.h"
  8. int
  9. crypto_generichash_blake2b(unsigned char *out, size_t outlen,
  10. const unsigned char *in, unsigned long long inlen,
  11. const unsigned char *key, size_t keylen)
  12. {
  13. if (outlen <= 0U || outlen > BLAKE2B_OUTBYTES ||
  14. keylen > BLAKE2B_KEYBYTES || inlen > UINT64_MAX) {
  15. return -1;
  16. }
  17. assert(outlen <= UINT8_MAX);
  18. assert(keylen <= UINT8_MAX);
  19. return blake2b((uint8_t *) out, in, key, (uint8_t) outlen, (uint64_t) inlen,
  20. (uint8_t) keylen);
  21. }
  22. int
  23. crypto_generichash_blake2b_salt_personal(
  24. unsigned char *out, size_t outlen, const unsigned char *in,
  25. unsigned long long inlen, const unsigned char *key, size_t keylen,
  26. const unsigned char *salt, const unsigned char *personal)
  27. {
  28. if (outlen <= 0U || outlen > BLAKE2B_OUTBYTES ||
  29. keylen > BLAKE2B_KEYBYTES || inlen > UINT64_MAX) {
  30. return -1;
  31. }
  32. assert(outlen <= UINT8_MAX);
  33. assert(keylen <= UINT8_MAX);
  34. return blake2b_salt_personal((uint8_t *) out, in, key, (uint8_t) outlen,
  35. (uint64_t) inlen, (uint8_t) keylen, salt,
  36. personal);
  37. }
  38. int
  39. crypto_generichash_blake2b_init(crypto_generichash_blake2b_state *state,
  40. const unsigned char *key, const size_t keylen,
  41. const size_t outlen)
  42. {
  43. if (outlen <= 0U || outlen > BLAKE2B_OUTBYTES ||
  44. keylen > BLAKE2B_KEYBYTES) {
  45. return -1;
  46. }
  47. assert(outlen <= UINT8_MAX);
  48. assert(keylen <= UINT8_MAX);
  49. COMPILER_ASSERT(sizeof(blake2b_state) <= sizeof *state);
  50. if (key == NULL || keylen <= 0U) {
  51. if (blake2b_init((blake2b_state *) (void *) state, (uint8_t) outlen) != 0) {
  52. return -1; /* LCOV_EXCL_LINE */
  53. }
  54. } else if (blake2b_init_key((blake2b_state *) (void *) state, (uint8_t) outlen, key,
  55. (uint8_t) keylen) != 0) {
  56. return -1; /* LCOV_EXCL_LINE */
  57. }
  58. return 0;
  59. }
  60. int
  61. crypto_generichash_blake2b_init_salt_personal(
  62. crypto_generichash_blake2b_state *state, const unsigned char *key,
  63. const size_t keylen, const size_t outlen, const unsigned char *salt,
  64. const unsigned char *personal)
  65. {
  66. if (outlen <= 0U || outlen > BLAKE2B_OUTBYTES ||
  67. keylen > BLAKE2B_KEYBYTES) {
  68. return -1;
  69. }
  70. assert(outlen <= UINT8_MAX);
  71. assert(keylen <= UINT8_MAX);
  72. if (key == NULL || keylen <= 0U) {
  73. if (blake2b_init_salt_personal((blake2b_state *) (void *) state,
  74. (uint8_t) outlen, salt, personal) != 0) {
  75. return -1; /* LCOV_EXCL_LINE */
  76. }
  77. } else if (blake2b_init_key_salt_personal((blake2b_state *) (void *) state,
  78. (uint8_t) outlen, key,
  79. (uint8_t) keylen, salt,
  80. personal) != 0) {
  81. return -1; /* LCOV_EXCL_LINE */
  82. }
  83. return 0;
  84. }
  85. int
  86. crypto_generichash_blake2b_update(crypto_generichash_blake2b_state *state,
  87. const unsigned char *in,
  88. unsigned long long inlen)
  89. {
  90. return blake2b_update((blake2b_state *) (void *) state,
  91. (const uint8_t *) in, (uint64_t) inlen);
  92. }
  93. int
  94. crypto_generichash_blake2b_final(crypto_generichash_blake2b_state *state,
  95. unsigned char *out, const size_t outlen)
  96. {
  97. assert(outlen <= UINT8_MAX);
  98. return blake2b_final((blake2b_state *) (void *) state,
  99. (uint8_t *) out, (uint8_t) outlen);
  100. }
  101. int
  102. _crypto_generichash_blake2b_pick_best_implementation(void)
  103. {
  104. return blake2b_pick_best_implementation();
  105. }