blake2b.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #pragma once
  2. #include "common.h"
  3. #include <util/generic/ptr.h>
  4. namespace NArgonish {
  5. /**
  6. * Interface for all Blake2B instances
  7. */
  8. class IBlake2Base {
  9. public:
  10. virtual ~IBlake2Base() {
  11. }
  12. /**
  13. * Updates intermediate hash with an ui32 value
  14. * @param in integer to hash
  15. */
  16. virtual void Update(ui32 in) = 0;
  17. /**
  18. * Updates intermediate hash with an array of bytes
  19. * @param pin input
  20. * @param inlen input length
  21. */
  22. virtual void Update(const void* pin, size_t inlen) = 0;
  23. /**
  24. * Finalizes the hash calculation and returns the hash value
  25. * @param out output buffer
  26. * @param outlen output buffer length
  27. */
  28. virtual void Final(void* out, size_t outlen) = 0;
  29. };
  30. /**
  31. * A factory that creates Blake2B instances optimized for different instruction sets
  32. */
  33. class TBlake2BFactory {
  34. public:
  35. /**
  36. * Constructs the factory object
  37. * @param skipTest if true then the constructor skips runtime Blake2B test
  38. */
  39. TBlake2BFactory(bool skipTest = false);
  40. /**
  41. * Creates an instance of Blake2B hash algorithm.
  42. * The optimisation is selected automatically based on the cpuid instruction output.
  43. * @param outlen the output buffer length, this value takes part in hashing
  44. * @param key a secret key to make Blake2B work as a keyed hash function
  45. * @param keylen the secret key length
  46. * @return returns an unique_ptr containing Blake2B instance
  47. */
  48. THolder<IBlake2Base> Create(size_t outlen = 32, const ui8* key = nullptr, size_t keylen = 0) const;
  49. /**
  50. * Creates an instance of Blake2B hash algorithm optimized for the particular instruction set
  51. * @param instructionSet instruction set
  52. * @param outlen the output buffer length, this value takes part in hashing
  53. * @param key a secret key to make Blake2B work as a keyed hash function
  54. * @param keylen the secret key length
  55. * @return returns an unique_ptr containing Blake2B instance
  56. */
  57. THolder<IBlake2Base> Create(EInstructionSet instructionSet, size_t outlen = 32,
  58. const ui8* key = nullptr, size_t keylen = 0) const;
  59. /**
  60. * The function returns the best instruction set available on the current CPU
  61. * @return InstructionSet value
  62. */
  63. EInstructionSet GetInstructionSet() const;
  64. protected:
  65. EInstructionSet InstructionSet_ = EInstructionSet::REF;
  66. void QuickTest_() const;
  67. };
  68. }