1295367Sdes/* $OpenBSD: digest.h,v 1.7 2014/12/21 22:27:56 djm Exp $ */
2261287Sdes/*
3261287Sdes * Copyright (c) 2013 Damien Miller <djm@mindrot.org>
4261287Sdes *
5261287Sdes * Permission to use, copy, modify, and distribute this software for any
6261287Sdes * purpose with or without fee is hereby granted, provided that the above
7261287Sdes * copyright notice and this permission notice appear in all copies.
8261287Sdes *
9261287Sdes * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10261287Sdes * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11261287Sdes * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12261287Sdes * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13261287Sdes * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14261287Sdes * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15261287Sdes * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16261287Sdes */
17261287Sdes
18261287Sdes#ifndef _DIGEST_H
19261287Sdes#define _DIGEST_H
20261287Sdes
21261287Sdes/* Maximum digest output length */
22261287Sdes#define SSH_DIGEST_MAX_LENGTH	64
23261287Sdes
24261287Sdes/* Digest algorithms */
25261287Sdes#define SSH_DIGEST_MD5		0
26261287Sdes#define SSH_DIGEST_RIPEMD160	1
27261287Sdes#define SSH_DIGEST_SHA1		2
28261287Sdes#define SSH_DIGEST_SHA256	3
29261287Sdes#define SSH_DIGEST_SHA384	4
30261287Sdes#define SSH_DIGEST_SHA512	5
31261287Sdes#define SSH_DIGEST_MAX		6
32261287Sdes
33295367Sdesstruct sshbuf;
34264377Sdesstruct ssh_digest_ctx;
35264377Sdes
36295367Sdes/* Looks up a digest algorithm by name */
37295367Sdesint ssh_digest_alg_by_name(const char *name);
38295367Sdes
39295367Sdes/* Returns the algorithm name for a digest identifier */
40295367Sdesconst char *ssh_digest_alg_name(int alg);
41295367Sdes
42261287Sdes/* Returns the algorithm's digest length in bytes or 0 for invalid algorithm */
43261287Sdessize_t ssh_digest_bytes(int alg);
44261287Sdes
45264377Sdes/* Returns the block size of the digest, e.g. for implementing HMAC */
46264377Sdessize_t ssh_digest_blocksize(struct ssh_digest_ctx *ctx);
47264377Sdes
48264377Sdes/* Copies internal state of digest of 'from' to 'to' */
49264377Sdesint ssh_digest_copy_state(struct ssh_digest_ctx *from,
50264377Sdes    struct ssh_digest_ctx *to);
51264377Sdes
52261287Sdes/* One-shot API */
53261287Sdesint ssh_digest_memory(int alg, const void *m, size_t mlen,
54261287Sdes    u_char *d, size_t dlen)
55261287Sdes	__attribute__((__bounded__(__buffer__, 2, 3)))
56261287Sdes	__attribute__((__bounded__(__buffer__, 4, 5)));
57295367Sdesint ssh_digest_buffer(int alg, const struct sshbuf *b, u_char *d, size_t dlen)
58261287Sdes	__attribute__((__bounded__(__buffer__, 3, 4)));
59261287Sdes
60261287Sdes/* Update API */
61261287Sdesstruct ssh_digest_ctx *ssh_digest_start(int alg);
62261287Sdesint ssh_digest_update(struct ssh_digest_ctx *ctx, const void *m, size_t mlen)
63261287Sdes	__attribute__((__bounded__(__buffer__, 2, 3)));
64295367Sdesint ssh_digest_update_buffer(struct ssh_digest_ctx *ctx,
65295367Sdes    const struct sshbuf *b);
66261287Sdesint ssh_digest_final(struct ssh_digest_ctx *ctx, u_char *d, size_t dlen)
67261287Sdes	__attribute__((__bounded__(__buffer__, 2, 3)));
68261287Sdesvoid ssh_digest_free(struct ssh_digest_ctx *ctx);
69261287Sdes
70261287Sdes#endif /* _DIGEST_H */
71261287Sdes
72