1 /* $NetBSD: md-sha256.c,v 1.5 2017/04/18 18:41:46 christos Exp $ */
2 /* $OpenBSD: md-sha256.c,v 1.5 2006/08/03 03:34:42 deraadt Exp $ */
3 /*
4 * Copyright (c) 2005 Damien Miller <djm@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 /* EVP wrapper for SHA256 */
20 #include "includes.h"
21 __RCSID("$NetBSD: md-sha256.c,v 1.5 2017/04/18 18:41:46 christos Exp $");
22
23 #include <sys/types.h>
24
25 #include <openssl/evp.h>
26
27 #include <sys/sha2.h>
28 #include <string.h>
29
30 const EVP_MD *evp_ssh_sha256(void);
31
32 static int
ssh_sha256_init(EVP_MD_CTX * ctxt)33 ssh_sha256_init(EVP_MD_CTX *ctxt)
34 {
35 SHA256_Init(ctxt->md_data);
36 return (1);
37 }
38
39 static int
ssh_sha256_update(EVP_MD_CTX * ctxt,const void * data,size_t len)40 ssh_sha256_update(EVP_MD_CTX *ctxt, const void *data, size_t len)
41 {
42 SHA256_Update(ctxt->md_data, data, len);
43 return (1);
44 }
45
46 static int
ssh_sha256_final(EVP_MD_CTX * ctxt,unsigned char * digest)47 ssh_sha256_final(EVP_MD_CTX *ctxt, unsigned char *digest)
48 {
49 SHA256_Final(digest, ctxt->md_data);
50 return (1);
51 }
52
53 static int
ssh_sha256_cleanup(EVP_MD_CTX * ctxt)54 ssh_sha256_cleanup(EVP_MD_CTX *ctxt)
55 {
56 memset(ctxt->md_data, 0, sizeof(SHA256_CTX));
57 return (1);
58 }
59
60 const EVP_MD *
evp_ssh_sha256(void)61 evp_ssh_sha256(void)
62 {
63 static EVP_MD ssh_sha256;
64
65 memset(&ssh_sha256, 0, sizeof(ssh_sha256));
66 ssh_sha256.type = NID_undef;
67 ssh_sha256.md_size = SHA256_DIGEST_LENGTH;
68 ssh_sha256.init = ssh_sha256_init;
69 ssh_sha256.update = ssh_sha256_update;
70 ssh_sha256.final = ssh_sha256_final;
71 ssh_sha256.cleanup = ssh_sha256_cleanup;
72 ssh_sha256.block_size = SHA256_BLOCK_LENGTH;
73 ssh_sha256.ctx_size = sizeof(SHA256_CTX);
74
75 return (&ssh_sha256);
76 }
77