]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/gcrypt-util.c
network: drop all checks of ipv6_disabled sysctl
[thirdparty/systemd.git] / src / basic / gcrypt-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #if HAVE_GCRYPT
4 #include <gcrypt.h>
5
6 #include "gcrypt-util.h"
7 #include "hexdecoct.h"
8
9 void initialize_libgcrypt(bool secmem) {
10 if (gcry_control(GCRYCTL_INITIALIZATION_FINISHED_P))
11 return;
12
13 assert_se(gcry_check_version("1.4.5"));
14
15 /* Turn off "secmem". Clients which wish to make use of this
16 * feature should initialize the library manually */
17 if (!secmem)
18 gcry_control(GCRYCTL_DISABLE_SECMEM);
19 gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
20 }
21
22 int string_hashsum(const char *s, size_t len, int md_algorithm, char **out) {
23 _cleanup_(gcry_md_closep) gcry_md_hd_t md = NULL;
24 size_t hash_size;
25 void *hash;
26 char *enc;
27
28 initialize_libgcrypt(false);
29
30 hash_size = gcry_md_get_algo_dlen(md_algorithm);
31 assert(hash_size > 0);
32
33 gcry_md_open(&md, md_algorithm, 0);
34 if (!md)
35 return -EIO;
36
37 gcry_md_write(md, s, len);
38
39 hash = gcry_md_read(md, 0);
40 if (!hash)
41 return -EIO;
42
43 enc = hexmem(hash, hash_size);
44 if (!enc)
45 return -ENOMEM;
46
47 *out = enc;
48 return 0;
49 }
50 #endif