1*a0ee8cc6SDag-Erling Smørgrav /* $OpenBSD: digest.h,v 1.6 2014/07/03 04:36:45 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 26f7167e0eSDag-Erling Smørgrav #define SSH_DIGEST_RIPEMD160 1 27f7167e0eSDag-Erling Smørgrav #define SSH_DIGEST_SHA1 2 28f7167e0eSDag-Erling Smørgrav #define SSH_DIGEST_SHA256 3 29f7167e0eSDag-Erling Smørgrav #define SSH_DIGEST_SHA384 4 30f7167e0eSDag-Erling Smørgrav #define SSH_DIGEST_SHA512 5 31f7167e0eSDag-Erling Smørgrav #define SSH_DIGEST_MAX 6 32f7167e0eSDag-Erling Smørgrav 33*a0ee8cc6SDag-Erling Smørgrav struct sshbuf; 34b83788ffSDag-Erling Smørgrav struct ssh_digest_ctx; 35b83788ffSDag-Erling Smørgrav 36f7167e0eSDag-Erling Smørgrav /* Returns the algorithm's digest length in bytes or 0 for invalid algorithm */ 37f7167e0eSDag-Erling Smørgrav size_t ssh_digest_bytes(int alg); 38f7167e0eSDag-Erling Smørgrav 39b83788ffSDag-Erling Smørgrav /* Returns the block size of the digest, e.g. for implementing HMAC */ 40b83788ffSDag-Erling Smørgrav size_t ssh_digest_blocksize(struct ssh_digest_ctx *ctx); 41b83788ffSDag-Erling Smørgrav 42b83788ffSDag-Erling Smørgrav /* Copies internal state of digest of 'from' to 'to' */ 43b83788ffSDag-Erling Smørgrav int ssh_digest_copy_state(struct ssh_digest_ctx *from, 44b83788ffSDag-Erling Smørgrav struct ssh_digest_ctx *to); 45b83788ffSDag-Erling Smørgrav 46f7167e0eSDag-Erling Smørgrav /* One-shot API */ 47f7167e0eSDag-Erling Smørgrav int ssh_digest_memory(int alg, const void *m, size_t mlen, 48f7167e0eSDag-Erling Smørgrav u_char *d, size_t dlen) 49f7167e0eSDag-Erling Smørgrav __attribute__((__bounded__(__buffer__, 2, 3))) 50f7167e0eSDag-Erling Smørgrav __attribute__((__bounded__(__buffer__, 4, 5))); 51*a0ee8cc6SDag-Erling Smørgrav int ssh_digest_buffer(int alg, const struct sshbuf *b, u_char *d, size_t dlen) 52f7167e0eSDag-Erling Smørgrav __attribute__((__bounded__(__buffer__, 3, 4))); 53f7167e0eSDag-Erling Smørgrav 54f7167e0eSDag-Erling Smørgrav /* Update API */ 55f7167e0eSDag-Erling Smørgrav struct ssh_digest_ctx *ssh_digest_start(int alg); 56f7167e0eSDag-Erling Smørgrav int ssh_digest_update(struct ssh_digest_ctx *ctx, const void *m, size_t mlen) 57f7167e0eSDag-Erling Smørgrav __attribute__((__bounded__(__buffer__, 2, 3))); 58*a0ee8cc6SDag-Erling Smørgrav int ssh_digest_update_buffer(struct ssh_digest_ctx *ctx, 59*a0ee8cc6SDag-Erling Smørgrav const struct sshbuf *b); 60f7167e0eSDag-Erling Smørgrav int ssh_digest_final(struct ssh_digest_ctx *ctx, u_char *d, size_t dlen) 61f7167e0eSDag-Erling Smørgrav __attribute__((__bounded__(__buffer__, 2, 3))); 62f7167e0eSDag-Erling Smørgrav void ssh_digest_free(struct ssh_digest_ctx *ctx); 63f7167e0eSDag-Erling Smørgrav 64f7167e0eSDag-Erling Smørgrav #endif /* _DIGEST_H */ 65f7167e0eSDag-Erling Smørgrav 66