1ce74bacaSMatthew Dillon /* $OpenBSD: digest.h,v 1.8 2017/05/08 22:57:38 djm Exp $ */ 236e94dc5SPeter Avalos /* 336e94dc5SPeter Avalos * Copyright (c) 2013 Damien Miller <djm@mindrot.org> 436e94dc5SPeter Avalos * 536e94dc5SPeter Avalos * Permission to use, copy, modify, and distribute this software for any 636e94dc5SPeter Avalos * purpose with or without fee is hereby granted, provided that the above 736e94dc5SPeter Avalos * copyright notice and this permission notice appear in all copies. 836e94dc5SPeter Avalos * 936e94dc5SPeter Avalos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 1036e94dc5SPeter Avalos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 1136e94dc5SPeter Avalos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 1236e94dc5SPeter Avalos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 1336e94dc5SPeter Avalos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 1436e94dc5SPeter Avalos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 1536e94dc5SPeter Avalos * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 1636e94dc5SPeter Avalos */ 1736e94dc5SPeter Avalos 1836e94dc5SPeter Avalos #ifndef _DIGEST_H 1936e94dc5SPeter Avalos #define _DIGEST_H 2036e94dc5SPeter Avalos 2136e94dc5SPeter Avalos /* Maximum digest output length */ 2236e94dc5SPeter Avalos #define SSH_DIGEST_MAX_LENGTH 64 2336e94dc5SPeter Avalos 2436e94dc5SPeter Avalos /* Digest algorithms */ 2536e94dc5SPeter Avalos #define SSH_DIGEST_MD5 0 26ce74bacaSMatthew Dillon #define SSH_DIGEST_SHA1 1 27ce74bacaSMatthew Dillon #define SSH_DIGEST_SHA256 2 28ce74bacaSMatthew Dillon #define SSH_DIGEST_SHA384 3 29ce74bacaSMatthew Dillon #define SSH_DIGEST_SHA512 4 30ce74bacaSMatthew Dillon #define SSH_DIGEST_MAX 5 3136e94dc5SPeter Avalos 3236e94dc5SPeter Avalos struct sshbuf; 3336e94dc5SPeter Avalos struct ssh_digest_ctx; 3436e94dc5SPeter Avalos 35*e9778795SPeter Avalos /* Looks up a digest algorithm by name */ 36*e9778795SPeter Avalos int ssh_digest_alg_by_name(const char *name); 37*e9778795SPeter Avalos 38*e9778795SPeter Avalos /* Returns the algorithm name for a digest identifier */ 39*e9778795SPeter Avalos const char *ssh_digest_alg_name(int alg); 40*e9778795SPeter Avalos 4136e94dc5SPeter Avalos /* Returns the algorithm's digest length in bytes or 0 for invalid algorithm */ 4236e94dc5SPeter Avalos size_t ssh_digest_bytes(int alg); 4336e94dc5SPeter Avalos 4436e94dc5SPeter Avalos /* Returns the block size of the digest, e.g. for implementing HMAC */ 4536e94dc5SPeter Avalos size_t ssh_digest_blocksize(struct ssh_digest_ctx *ctx); 4636e94dc5SPeter Avalos 4736e94dc5SPeter Avalos /* Copies internal state of digest of 'from' to 'to' */ 4836e94dc5SPeter Avalos int ssh_digest_copy_state(struct ssh_digest_ctx *from, 4936e94dc5SPeter Avalos struct ssh_digest_ctx *to); 5036e94dc5SPeter Avalos 5136e94dc5SPeter Avalos /* One-shot API */ 5236e94dc5SPeter Avalos int ssh_digest_memory(int alg, const void *m, size_t mlen, 5336e94dc5SPeter Avalos u_char *d, size_t dlen) 5436e94dc5SPeter Avalos __attribute__((__bounded__(__buffer__, 2, 3))) 5536e94dc5SPeter Avalos __attribute__((__bounded__(__buffer__, 4, 5))); 5636e94dc5SPeter Avalos int ssh_digest_buffer(int alg, const struct sshbuf *b, u_char *d, size_t dlen) 5736e94dc5SPeter Avalos __attribute__((__bounded__(__buffer__, 3, 4))); 5836e94dc5SPeter Avalos 5936e94dc5SPeter Avalos /* Update API */ 6036e94dc5SPeter Avalos struct ssh_digest_ctx *ssh_digest_start(int alg); 6136e94dc5SPeter Avalos int ssh_digest_update(struct ssh_digest_ctx *ctx, const void *m, size_t mlen) 6236e94dc5SPeter Avalos __attribute__((__bounded__(__buffer__, 2, 3))); 6336e94dc5SPeter Avalos int ssh_digest_update_buffer(struct ssh_digest_ctx *ctx, 6436e94dc5SPeter Avalos const struct sshbuf *b); 6536e94dc5SPeter Avalos int ssh_digest_final(struct ssh_digest_ctx *ctx, u_char *d, size_t dlen) 6636e94dc5SPeter Avalos __attribute__((__bounded__(__buffer__, 2, 3))); 6736e94dc5SPeter Avalos void ssh_digest_free(struct ssh_digest_ctx *ctx); 6836e94dc5SPeter Avalos 6936e94dc5SPeter Avalos #endif /* _DIGEST_H */ 7036e94dc5SPeter Avalos 71