1*41768fc1Schristos /* $NetBSD: md-sha256.c,v 1.5 2017/04/18 18:41:46 christos Exp $ */
2313c6c94Schristos /* $OpenBSD: md-sha256.c,v 1.5 2006/08/03 03:34:42 deraadt Exp $ */
3313c6c94Schristos /*
4313c6c94Schristos * Copyright (c) 2005 Damien Miller <djm@openbsd.org>
5313c6c94Schristos *
6313c6c94Schristos * Permission to use, copy, modify, and distribute this software for any
7313c6c94Schristos * purpose with or without fee is hereby granted, provided that the above
8313c6c94Schristos * copyright notice and this permission notice appear in all copies.
9313c6c94Schristos *
10313c6c94Schristos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11313c6c94Schristos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12313c6c94Schristos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13313c6c94Schristos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14313c6c94Schristos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15313c6c94Schristos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16313c6c94Schristos * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17313c6c94Schristos */
18313c6c94Schristos
19313c6c94Schristos /* EVP wrapper for SHA256 */
20313c6c94Schristos #include "includes.h"
21*41768fc1Schristos __RCSID("$NetBSD: md-sha256.c,v 1.5 2017/04/18 18:41:46 christos Exp $");
22313c6c94Schristos
23313c6c94Schristos #include <sys/types.h>
24313c6c94Schristos
25313c6c94Schristos #include <openssl/evp.h>
26313c6c94Schristos
27313c6c94Schristos #include <sys/sha2.h>
28313c6c94Schristos #include <string.h>
29313c6c94Schristos
30313c6c94Schristos const EVP_MD *evp_ssh_sha256(void);
31313c6c94Schristos
32313c6c94Schristos static int
ssh_sha256_init(EVP_MD_CTX * ctxt)33313c6c94Schristos ssh_sha256_init(EVP_MD_CTX *ctxt)
34313c6c94Schristos {
35313c6c94Schristos SHA256_Init(ctxt->md_data);
36313c6c94Schristos return (1);
37313c6c94Schristos }
38313c6c94Schristos
39313c6c94Schristos static int
ssh_sha256_update(EVP_MD_CTX * ctxt,const void * data,size_t len)40313c6c94Schristos ssh_sha256_update(EVP_MD_CTX *ctxt, const void *data, size_t len)
41313c6c94Schristos {
42313c6c94Schristos SHA256_Update(ctxt->md_data, data, len);
43313c6c94Schristos return (1);
44313c6c94Schristos }
45313c6c94Schristos
46313c6c94Schristos static int
ssh_sha256_final(EVP_MD_CTX * ctxt,unsigned char * digest)47313c6c94Schristos ssh_sha256_final(EVP_MD_CTX *ctxt, unsigned char *digest)
48313c6c94Schristos {
49313c6c94Schristos SHA256_Final(digest, ctxt->md_data);
50313c6c94Schristos return (1);
51313c6c94Schristos }
52313c6c94Schristos
53313c6c94Schristos static int
ssh_sha256_cleanup(EVP_MD_CTX * ctxt)54313c6c94Schristos ssh_sha256_cleanup(EVP_MD_CTX *ctxt)
55313c6c94Schristos {
56313c6c94Schristos memset(ctxt->md_data, 0, sizeof(SHA256_CTX));
57313c6c94Schristos return (1);
58313c6c94Schristos }
59313c6c94Schristos
60313c6c94Schristos const EVP_MD *
evp_ssh_sha256(void)61313c6c94Schristos evp_ssh_sha256(void)
62313c6c94Schristos {
63313c6c94Schristos static EVP_MD ssh_sha256;
64313c6c94Schristos
65313c6c94Schristos memset(&ssh_sha256, 0, sizeof(ssh_sha256));
66313c6c94Schristos ssh_sha256.type = NID_undef;
67313c6c94Schristos ssh_sha256.md_size = SHA256_DIGEST_LENGTH;
68313c6c94Schristos ssh_sha256.init = ssh_sha256_init;
69313c6c94Schristos ssh_sha256.update = ssh_sha256_update;
70313c6c94Schristos ssh_sha256.final = ssh_sha256_final;
71313c6c94Schristos ssh_sha256.cleanup = ssh_sha256_cleanup;
72313c6c94Schristos ssh_sha256.block_size = SHA256_BLOCK_LENGTH;
73313c6c94Schristos ssh_sha256.ctx_size = sizeof(SHA256_CTX);
74313c6c94Schristos
75313c6c94Schristos return (&ssh_sha256);
76313c6c94Schristos }
77