digest.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* $OpenBSD: digest.h,v 1.8 2017/05/08 22:57:38 djm Exp $ */
  2. /*
  3. * Copyright (c) 2013 Damien Miller <djm@mindrot.org>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #ifndef _DIGEST_H
  18. #define _DIGEST_H
  19. /* Maximum digest output length */
  20. #define SSH_DIGEST_MAX_LENGTH 64
  21. /* Digest algorithms */
  22. #define SSH_DIGEST_MD5 0
  23. #define SSH_DIGEST_SHA1 1
  24. #define SSH_DIGEST_SHA256 2
  25. #define SSH_DIGEST_SHA384 3
  26. #define SSH_DIGEST_SHA512 4
  27. #define SSH_DIGEST_NULL 5
  28. #define SSH_DIGEST_MAX 6
  29. struct sshbuf;
  30. struct ssh_digest_ctx;
  31. #ifdef WITH_OPENSSL
  32. #include <openssl/evp.h>
  33. /* Converts internal digest representation to the OpenSSL one */
  34. const EVP_MD *ssh_digest_to_md(int digest_type);
  35. #endif
  36. /* Looks up a digest algorithm by name */
  37. int ssh_digest_alg_by_name(const char *name);
  38. /* Returns the algorithm name for a digest identifier */
  39. const char *ssh_digest_alg_name(int alg);
  40. /* Returns the algorithm's digest length in bytes or 0 for invalid algorithm */
  41. size_t ssh_digest_bytes(int alg);
  42. /* Returns the block size of the digest, e.g. for implementing HMAC */
  43. size_t ssh_digest_blocksize(struct ssh_digest_ctx *ctx);
  44. /* Copies internal state of digest of 'from' to 'to' */
  45. int ssh_digest_copy_state(struct ssh_digest_ctx *from,
  46. struct ssh_digest_ctx *to);
  47. /* One-shot API */
  48. int ssh_digest_memory(int alg, const void *m, size_t mlen,
  49. u_char *d, size_t dlen)
  50. __attribute__((__bounded__(__buffer__, 2, 3)))
  51. __attribute__((__bounded__(__buffer__, 4, 5)));
  52. int ssh_digest_buffer(int alg, const struct sshbuf *b, u_char *d, size_t dlen)
  53. __attribute__((__bounded__(__buffer__, 3, 4)));
  54. /* Update API */
  55. struct ssh_digest_ctx *ssh_digest_start(int alg);
  56. int ssh_digest_update(struct ssh_digest_ctx *ctx, const void *m, size_t mlen)
  57. __attribute__((__bounded__(__buffer__, 2, 3)));
  58. int ssh_digest_update_buffer(struct ssh_digest_ctx *ctx,
  59. const struct sshbuf *b);
  60. int ssh_digest_final(struct ssh_digest_ctx *ctx, u_char *d, size_t dlen)
  61. __attribute__((__bounded__(__buffer__, 2, 3)));
  62. void ssh_digest_free(struct ssh_digest_ctx *ctx);
  63. #endif /* _DIGEST_H */