ed25519-hash-custom.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. a custom hash must have a 512bit digest and implement:
  3. struct ed25519_hash_context;
  4. void ed25519_hash_init(ed25519_hash_context *ctx);
  5. void ed25519_hash_update(ed25519_hash_context *ctx, const uint8_t *in, size_t inlen);
  6. void ed25519_hash_final(ed25519_hash_context *ctx, uint8_t *hash);
  7. void ed25519_hash(uint8_t *hash, const uint8_t *in, size_t inlen);
  8. */
  9. #include "crypto.h"
  10. typedef struct ed25519_hash_context {
  11. crypto_digest_t *ctx;
  12. } ed25519_hash_context;
  13. static void
  14. ed25519_hash_init(ed25519_hash_context *ctx)
  15. {
  16. ctx->ctx = crypto_digest512_new(DIGEST_SHA512);
  17. }
  18. static void
  19. ed25519_hash_update(ed25519_hash_context *ctx, const uint8_t *in, size_t inlen)
  20. {
  21. crypto_digest_add_bytes(ctx->ctx, (const char *)in, inlen);
  22. }
  23. static void
  24. ed25519_hash_final(ed25519_hash_context *ctx, uint8_t *hash)
  25. {
  26. crypto_digest_get_digest(ctx->ctx, (char *)hash, DIGEST512_LEN);
  27. crypto_digest_free(ctx->ctx);
  28. ctx->ctx = NULL;
  29. }
  30. static void
  31. ed25519_hash(uint8_t *hash, const uint8_t *in, size_t inlen)
  32. {
  33. crypto_digest512((char *)hash, (const char *)in, inlen,
  34. DIGEST_SHA512);
  35. }