xref: /openbsd-src/usr.bin/ssh/hash.c (revision a2f7fe0909b7a4840a359a2e87e383cef18c730e)
1*a2f7fe09Sdjm /* $OpenBSD: hash.c,v 1.6 2019/11/29 00:11:21 djm Exp $ */
240af8438Snaddy /*
340af8438Snaddy  * Public domain. Author: Christian Weisgerber <naddy@openbsd.org>
440af8438Snaddy  * API compatible reimplementation of function from nacl
540af8438Snaddy  */
68ffbcf6dSmarkus 
78ffbcf6dSmarkus #include "crypto_api.h"
88ffbcf6dSmarkus 
9c0c5a1b7Snaddy #include <stdarg.h>
108ffbcf6dSmarkus 
11*a2f7fe09Sdjm #ifdef WITH_OPENSSL
12*a2f7fe09Sdjm #include <openssl/evp.h>
138ffbcf6dSmarkus 
14c0c5a1b7Snaddy int
crypto_hash_sha512(unsigned char * out,const unsigned char * in,unsigned long long inlen)15c0c5a1b7Snaddy crypto_hash_sha512(unsigned char *out, const unsigned char *in,
16c0c5a1b7Snaddy     unsigned long long inlen)
178ffbcf6dSmarkus {
188ffbcf6dSmarkus 
19*a2f7fe09Sdjm 	if (!EVP_Digest(in, inlen, out, NULL, EVP_sha512(), NULL))
20*a2f7fe09Sdjm 		return -1;
218ffbcf6dSmarkus 	return 0;
228ffbcf6dSmarkus }
23*a2f7fe09Sdjm 
24*a2f7fe09Sdjm #else
25*a2f7fe09Sdjm #include <sha2.h>
26*a2f7fe09Sdjm 
27*a2f7fe09Sdjm int
crypto_hash_sha512(unsigned char * out,const unsigned char * in,unsigned long long inlen)28*a2f7fe09Sdjm crypto_hash_sha512(unsigned char *out, const unsigned char *in,
29*a2f7fe09Sdjm     unsigned long long inlen)
30*a2f7fe09Sdjm {
31*a2f7fe09Sdjm 
32*a2f7fe09Sdjm 	SHA2_CTX ctx;
33*a2f7fe09Sdjm 
34*a2f7fe09Sdjm 	SHA512Init(&ctx);
35*a2f7fe09Sdjm 	SHA512Update(&ctx, in, inlen);
36*a2f7fe09Sdjm 	SHA512Final(out, &ctx);
37*a2f7fe09Sdjm 	return 0;
38*a2f7fe09Sdjm }
39*a2f7fe09Sdjm #endif /* WITH_OPENSSL */
40