djm@openbsd.org | 7bdb2ee | 2017-05-08 22:57:38 +0000 | [diff] [blame] | 1 | /* $OpenBSD: digest.h,v 1.8 2017/05/08 22:57:38 djm Exp $ */ |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 2 | /* |
| 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 |
djm@openbsd.org | 7bdb2ee | 2017-05-08 22:57:38 +0000 | [diff] [blame] | 26 | #define SSH_DIGEST_SHA1 1 |
| 27 | #define SSH_DIGEST_SHA256 2 |
| 28 | #define SSH_DIGEST_SHA384 3 |
| 29 | #define SSH_DIGEST_SHA512 4 |
| 30 | #define SSH_DIGEST_MAX 5 |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 31 | |
Damien Miller | e6a4077 | 2014-07-03 21:25:03 +1000 | [diff] [blame] | 32 | struct sshbuf; |
Damien Miller | 4e8d937 | 2014-02-04 11:02:42 +1100 | [diff] [blame] | 33 | struct ssh_digest_ctx; |
| 34 | |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 35 | /* Looks up a digest algorithm by name */ |
| 36 | int ssh_digest_alg_by_name(const char *name); |
| 37 | |
| 38 | /* Returns the algorithm name for a digest identifier */ |
| 39 | const char *ssh_digest_alg_name(int alg); |
| 40 | |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 41 | /* Returns the algorithm's digest length in bytes or 0 for invalid algorithm */ |
| 42 | size_t ssh_digest_bytes(int alg); |
| 43 | |
Damien Miller | 4e8d937 | 2014-02-04 11:02:42 +1100 | [diff] [blame] | 44 | /* Returns the block size of the digest, e.g. for implementing HMAC */ |
| 45 | size_t ssh_digest_blocksize(struct ssh_digest_ctx *ctx); |
| 46 | |
| 47 | /* Copies internal state of digest of 'from' to 'to' */ |
| 48 | int ssh_digest_copy_state(struct ssh_digest_ctx *from, |
| 49 | struct ssh_digest_ctx *to); |
| 50 | |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 51 | /* One-shot API */ |
| 52 | int ssh_digest_memory(int alg, const void *m, size_t mlen, |
| 53 | u_char *d, size_t dlen) |
Damien Miller | 686c7d9 | 2014-05-15 14:37:03 +1000 | [diff] [blame] | 54 | __attribute__((__bounded__(__buffer__, 2, 3))) |
| 55 | __attribute__((__bounded__(__buffer__, 4, 5))); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 56 | int ssh_digest_buffer(int alg, const struct sshbuf *b, u_char *d, size_t dlen) |
Damien Miller | 686c7d9 | 2014-05-15 14:37:03 +1000 | [diff] [blame] | 57 | __attribute__((__bounded__(__buffer__, 3, 4))); |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 58 | |
| 59 | /* Update API */ |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 60 | struct ssh_digest_ctx *ssh_digest_start(int alg); |
| 61 | int ssh_digest_update(struct ssh_digest_ctx *ctx, const void *m, size_t mlen) |
Damien Miller | 686c7d9 | 2014-05-15 14:37:03 +1000 | [diff] [blame] | 62 | __attribute__((__bounded__(__buffer__, 2, 3))); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 63 | int ssh_digest_update_buffer(struct ssh_digest_ctx *ctx, |
| 64 | const struct sshbuf *b); |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 65 | int ssh_digest_final(struct ssh_digest_ctx *ctx, u_char *d, size_t dlen) |
Damien Miller | 686c7d9 | 2014-05-15 14:37:03 +1000 | [diff] [blame] | 66 | __attribute__((__bounded__(__buffer__, 2, 3))); |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 67 | void ssh_digest_free(struct ssh_digest_ctx *ctx); |
| 68 | |
| 69 | #endif /* _DIGEST_H */ |
| 70 | |