blob: 3fe07346852b2f33e61e31d0444861db83a506eb [file] [log] [blame]
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001/* $OpenBSD: digest.h,v 1.7 2014/12/21 22:27:56 djm Exp $ */
Damien Millerb3051d02014-01-10 10:58:53 +11002/*
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
18#ifndef _DIGEST_H
19#define _DIGEST_H
20
21/* Maximum digest output length */
22#define SSH_DIGEST_MAX_LENGTH 64
23
24/* Digest algorithms */
25#define SSH_DIGEST_MD5 0
26#define SSH_DIGEST_RIPEMD160 1
27#define SSH_DIGEST_SHA1 2
28#define SSH_DIGEST_SHA256 3
29#define SSH_DIGEST_SHA384 4
30#define SSH_DIGEST_SHA512 5
31#define SSH_DIGEST_MAX 6
32
Damien Millere6a40772014-07-03 21:25:03 +100033struct sshbuf;
Damien Miller4e8d9372014-02-04 11:02:42 +110034struct ssh_digest_ctx;
35
djm@openbsd.org56d1c832014-12-21 22:27:55 +000036/* Looks up a digest algorithm by name */
37int ssh_digest_alg_by_name(const char *name);
38
39/* Returns the algorithm name for a digest identifier */
40const char *ssh_digest_alg_name(int alg);
41
Damien Millerb3051d02014-01-10 10:58:53 +110042/* Returns the algorithm's digest length in bytes or 0 for invalid algorithm */
43size_t ssh_digest_bytes(int alg);
44
Damien Miller4e8d9372014-02-04 11:02:42 +110045/* Returns the block size of the digest, e.g. for implementing HMAC */
46size_t ssh_digest_blocksize(struct ssh_digest_ctx *ctx);
47
48/* Copies internal state of digest of 'from' to 'to' */
49int ssh_digest_copy_state(struct ssh_digest_ctx *from,
50 struct ssh_digest_ctx *to);
51
Damien Millerb3051d02014-01-10 10:58:53 +110052/* One-shot API */
53int ssh_digest_memory(int alg, const void *m, size_t mlen,
54 u_char *d, size_t dlen)
Damien Miller686c7d92014-05-15 14:37:03 +100055 __attribute__((__bounded__(__buffer__, 2, 3)))
56 __attribute__((__bounded__(__buffer__, 4, 5)));
Damien Miller86687062014-07-02 15:28:02 +100057int ssh_digest_buffer(int alg, const struct sshbuf *b, u_char *d, size_t dlen)
Damien Miller686c7d92014-05-15 14:37:03 +100058 __attribute__((__bounded__(__buffer__, 3, 4)));
Damien Millerb3051d02014-01-10 10:58:53 +110059
60/* Update API */
Damien Millerb3051d02014-01-10 10:58:53 +110061struct ssh_digest_ctx *ssh_digest_start(int alg);
62int ssh_digest_update(struct ssh_digest_ctx *ctx, const void *m, size_t mlen)
Damien Miller686c7d92014-05-15 14:37:03 +100063 __attribute__((__bounded__(__buffer__, 2, 3)));
Damien Miller86687062014-07-02 15:28:02 +100064int ssh_digest_update_buffer(struct ssh_digest_ctx *ctx,
65 const struct sshbuf *b);
Damien Millerb3051d02014-01-10 10:58:53 +110066int ssh_digest_final(struct ssh_digest_ctx *ctx, u_char *d, size_t dlen)
Damien Miller686c7d92014-05-15 14:37:03 +100067 __attribute__((__bounded__(__buffer__, 2, 3)));
Damien Millerb3051d02014-01-10 10:58:53 +110068void ssh_digest_free(struct ssh_digest_ctx *ctx);
69
70#endif /* _DIGEST_H */
71