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