Skip to content

Commit 6ebd9d4

Browse files
committed
common: add SHA-512 hash helper
1 parent ab58000 commit 6ebd9d4

4 files changed

Lines changed: 59 additions & 0 deletions

File tree

src/common/crypto.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "rfb/rfbconfig.h"
66

77
#define SHA1_HASH_SIZE 20
8+
#define SHA512_HASH_SIZE 64
89
#define MD5_HASH_SIZE 16
910

1011
/* Generates an MD5 hash of 'in' and writes it to 'out', which must be 16 bytes in size. */
@@ -13,6 +14,9 @@ int hash_md5(void *out, const void *in, const size_t in_len);
1314
/* Generates an SHA1 hash of 'in' and writes it to 'out', which must be 20 bytes in size. */
1415
int hash_sha1(void *out, const void *in, const size_t in_len);
1516

17+
/* Generates an SHA512 hash of 'in' and writes it to 'out', which must be 64 bytes in size. */
18+
int hash_sha512(void *out, const void *in, const size_t in_len);
19+
1620
/* Fill 'out' with 'len' random bytes. */
1721
void random_bytes(void *out, size_t len);
1822

src/common/crypto_included.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ int hash_sha1(void *out, const void *in, const size_t in_len)
4646
return 1;
4747
}
4848

49+
int hash_sha512(void *out, const void *in, const size_t in_len)
50+
{
51+
(void)out;
52+
(void)in;
53+
(void)in_len;
54+
return 0;
55+
}
56+
4957
void random_bytes(void *out, size_t len)
5058
{
5159

src/common/crypto_libgcrypt.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,31 @@ int hash_sha1(void *out, const void *in, const size_t in_len)
9999
return result;
100100
}
101101

102+
int hash_sha512(void *out, const void *in, const size_t in_len)
103+
{
104+
int result = 0;
105+
gcry_error_t error;
106+
gcry_md_hd_t sha512 = NULL;
107+
void *digest;
108+
109+
error = gcry_md_open(&sha512, GCRY_MD_SHA512, 0);
110+
if (gcry_err_code(error) != GPG_ERR_NO_ERROR)
111+
goto out;
112+
113+
gcry_md_write(sha512, in, in_len);
114+
115+
if(!(digest = gcry_md_read(sha512, GCRY_MD_SHA512)))
116+
goto out;
117+
118+
memcpy(out, digest, gcry_md_get_algo_dlen(GCRY_MD_SHA512));
119+
120+
result = 1;
121+
122+
out:
123+
gcry_md_close(sha512);
124+
return result;
125+
}
126+
102127
void random_bytes(void *out, size_t len)
103128
{
104129
gcry_randomize(out, len, GCRY_STRONG_RANDOM);

src/common/crypto_openssl.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,28 @@ int hash_sha1(void *out, const void *in, const size_t in_len)
6464
return 1;
6565
}
6666

67+
int hash_sha512(void *out, const void *in, const size_t in_len)
68+
{
69+
EVP_MD_CTX *ctx = NULL;
70+
int result = 0;
71+
unsigned int digest_len = 0;
72+
73+
if (!(ctx = EVP_MD_CTX_new()))
74+
goto out;
75+
if (!EVP_DigestInit_ex(ctx, EVP_sha512(), NULL))
76+
goto out;
77+
if (!EVP_DigestUpdate(ctx, in, in_len))
78+
goto out;
79+
if (!EVP_DigestFinal_ex(ctx, out, &digest_len))
80+
goto out;
81+
82+
result = digest_len == SHA512_HASH_SIZE;
83+
84+
out:
85+
EVP_MD_CTX_free(ctx);
86+
return result;
87+
}
88+
6789
void random_bytes(void *out, size_t len)
6890
{
6991
RAND_bytes(out, len);

0 commit comments

Comments
 (0)