1*17418e98Schristos /* $NetBSD: digest.h,v 1.3 2021/03/05 17:47:16 christos Exp $ */ 29340b371Schristos /* $OpenBSD: digest.h,v 1.8 2017/05/08 22:57:38 djm Exp $ */ 35484a5efSchristos /* 45484a5efSchristos * Copyright (c) 2013 Damien Miller <djm@mindrot.org> 55484a5efSchristos * 65484a5efSchristos * Permission to use, copy, modify, and distribute this software for any 75484a5efSchristos * purpose with or without fee is hereby granted, provided that the above 85484a5efSchristos * copyright notice and this permission notice appear in all copies. 95484a5efSchristos * 105484a5efSchristos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 115484a5efSchristos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 125484a5efSchristos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 135484a5efSchristos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 145484a5efSchristos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 155484a5efSchristos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 165484a5efSchristos * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 175484a5efSchristos */ 185484a5efSchristos 195484a5efSchristos #ifndef _DIGEST_H 205484a5efSchristos #define _DIGEST_H 215484a5efSchristos 225484a5efSchristos /* Maximum digest output length */ 235484a5efSchristos #define SSH_DIGEST_MAX_LENGTH 64 245484a5efSchristos 255484a5efSchristos /* Digest algorithms */ 265484a5efSchristos #define SSH_DIGEST_MD5 0 279340b371Schristos #define SSH_DIGEST_SHA1 1 289340b371Schristos #define SSH_DIGEST_SHA256 2 299340b371Schristos #define SSH_DIGEST_SHA384 3 309340b371Schristos #define SSH_DIGEST_SHA512 4 319340b371Schristos #define SSH_DIGEST_MAX 5 325484a5efSchristos 335484a5efSchristos struct sshbuf; 345484a5efSchristos struct ssh_digest_ctx; 355484a5efSchristos 36e161120fSchristos /* Looks up a digest algorithm by name */ 37e161120fSchristos int ssh_digest_alg_by_name(const char *name); 38e161120fSchristos 39e161120fSchristos /* Returns the algorithm name for a digest identifier */ 40e161120fSchristos const char *ssh_digest_alg_name(int alg); 41e161120fSchristos 425484a5efSchristos /* Returns the algorithm's digest length in bytes or 0 for invalid algorithm */ 435484a5efSchristos size_t ssh_digest_bytes(int alg); 445484a5efSchristos 455484a5efSchristos /* Returns the block size of the digest, e.g. for implementing HMAC */ 465484a5efSchristos size_t ssh_digest_blocksize(struct ssh_digest_ctx *ctx); 475484a5efSchristos 485484a5efSchristos /* Copies internal state of digest of 'from' to 'to' */ 495484a5efSchristos int ssh_digest_copy_state(struct ssh_digest_ctx *from, 505484a5efSchristos struct ssh_digest_ctx *to); 515484a5efSchristos 525484a5efSchristos /* One-shot API */ 535484a5efSchristos int ssh_digest_memory(int alg, const void *m, size_t mlen, 545484a5efSchristos u_char *d, size_t dlen) 555484a5efSchristos __attribute__((__bounded__(__buffer__, 2, 3))) 565484a5efSchristos __attribute__((__bounded__(__buffer__, 4, 5))); 575484a5efSchristos int ssh_digest_buffer(int alg, const struct sshbuf *b, u_char *d, size_t dlen) 585484a5efSchristos __attribute__((__bounded__(__buffer__, 3, 4))); 595484a5efSchristos 605484a5efSchristos /* Update API */ 615484a5efSchristos struct ssh_digest_ctx *ssh_digest_start(int alg); 625484a5efSchristos int ssh_digest_update(struct ssh_digest_ctx *ctx, const void *m, size_t mlen) 635484a5efSchristos __attribute__((__bounded__(__buffer__, 2, 3))); 645484a5efSchristos int ssh_digest_update_buffer(struct ssh_digest_ctx *ctx, 655484a5efSchristos const struct sshbuf *b); 665484a5efSchristos int ssh_digest_final(struct ssh_digest_ctx *ctx, u_char *d, size_t dlen) 675484a5efSchristos __attribute__((__bounded__(__buffer__, 2, 3))); 685484a5efSchristos void ssh_digest_free(struct ssh_digest_ctx *ctx); 695484a5efSchristos 705484a5efSchristos #endif /* _DIGEST_H */ 715484a5efSchristos 72