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