Debian Codegraph @

src/basic/gcrypt-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/syslog.h>
4
5 #include "sd-dlopen.h"
6
7 #include "gcrypt-util.h"
8 #include "log.h" /* IWYU pragma: keep */
9
10 #if HAVE_GCRYPT
11
12 static DLSYM_PROTOTYPE(gcry_control) = NULL;
13 static DLSYM_PROTOTYPE(gcry_check_version) = NULL;
14 DLSYM_PROTOTYPE(gcry_md_close) = NULL;
15 DLSYM_PROTOTYPE(gcry_md_copy) = NULL;
16 DLSYM_PROTOTYPE(gcry_md_ctl) = NULL;
17 DLSYM_PROTOTYPE(gcry_md_get_algo_dlen) = NULL;
18 DLSYM_PROTOTYPE(gcry_md_open) = NULL;
19 DLSYM_PROTOTYPE(gcry_md_read) = NULL;
20 DLSYM_PROTOTYPE(gcry_md_reset) = NULL;
21 DLSYM_PROTOTYPE(gcry_md_setkey) = NULL;
22 DLSYM_PROTOTYPE(gcry_md_write) = NULL;
23 DLSYM_PROTOTYPE(gcry_mpi_add) = NULL;
24 DLSYM_PROTOTYPE(gcry_mpi_add_ui) = NULL;
25 DLSYM_PROTOTYPE(gcry_mpi_cmp) = NULL;
26 DLSYM_PROTOTYPE(gcry_mpi_cmp_ui) = NULL;
27 DLSYM_PROTOTYPE(gcry_mpi_get_nbits) = NULL;
28 DLSYM_PROTOTYPE(gcry_mpi_invm) = NULL;
29 DLSYM_PROTOTYPE(gcry_mpi_mod) = NULL;
30 DLSYM_PROTOTYPE(gcry_mpi_mul) = NULL;
31 DLSYM_PROTOTYPE(gcry_mpi_mulm) = NULL;
32 DLSYM_PROTOTYPE(gcry_mpi_new) = NULL;
33 DLSYM_PROTOTYPE(gcry_mpi_powm) = NULL;
34 DLSYM_PROTOTYPE(gcry_mpi_print) = NULL;
35 DLSYM_PROTOTYPE(gcry_mpi_release) = NULL;
36 DLSYM_PROTOTYPE(gcry_mpi_scan) = NULL;
37 DLSYM_PROTOTYPE(gcry_mpi_set_ui) = NULL;
38 DLSYM_PROTOTYPE(gcry_mpi_sub) = NULL;
39 DLSYM_PROTOTYPE(gcry_mpi_subm) = NULL;
40 DLSYM_PROTOTYPE(gcry_mpi_sub_ui) = NULL;
41 DLSYM_PROTOTYPE(gcry_prime_check) = NULL;
42 DLSYM_PROTOTYPE(gcry_randomize) = NULL;
43 DLSYM_PROTOTYPE(gcry_strerror) = NULL;
44 #endif
45
46 int dlopen_gcrypt(int log_level) {
47 #if HAVE_GCRYPT
48 static void *gcrypt_dl = NULL;
49
50 SD_ELF_NOTE_DLOPEN(
51 "gcrypt",
52 "Support for journald forward-sealing",
53 SD_ELF_NOTE_DLOPEN_PRIORITY_SUGGESTED,
54 "libgcrypt.so.20");
55
56 return dlopen_many_sym_or_warn(
57 &gcrypt_dl,
58 "libgcrypt.so.20", log_level,
59 DLSYM_ARG(gcry_control),
60 DLSYM_ARG(gcry_check_version),
61 DLSYM_ARG(gcry_md_close),
62 DLSYM_ARG(gcry_md_copy),
63 DLSYM_ARG(gcry_md_ctl),
64 DLSYM_ARG(gcry_md_get_algo_dlen),
65 DLSYM_ARG(gcry_md_open),
66 DLSYM_ARG(gcry_md_read),
67 DLSYM_ARG(gcry_md_reset),
68 DLSYM_ARG(gcry_md_setkey),
69 DLSYM_ARG(gcry_md_write),
70 DLSYM_ARG(gcry_mpi_add),
71 DLSYM_ARG(gcry_mpi_add_ui),
72 DLSYM_ARG(gcry_mpi_cmp),
73 DLSYM_ARG(gcry_mpi_cmp_ui),
74 DLSYM_ARG(gcry_mpi_get_nbits),
75 DLSYM_ARG(gcry_mpi_invm),
76 DLSYM_ARG(gcry_mpi_mod),
77 DLSYM_ARG(gcry_mpi_mul),
78 DLSYM_ARG(gcry_mpi_mulm),
79 DLSYM_ARG(gcry_mpi_new),
80 DLSYM_ARG(gcry_mpi_powm),
81 DLSYM_ARG(gcry_mpi_print),
82 DLSYM_ARG(gcry_mpi_release),
83 DLSYM_ARG(gcry_mpi_scan),
84 DLSYM_ARG(gcry_mpi_set_ui),
85 DLSYM_ARG(gcry_mpi_sub),
86 DLSYM_ARG(gcry_mpi_subm),
87 DLSYM_ARG(gcry_mpi_sub_ui),
88 DLSYM_ARG(gcry_prime_check),
89 DLSYM_ARG(gcry_randomize),
90 DLSYM_ARG(gcry_strerror));
91 #else
92 return log_full_errno(log_level, SYNTHETIC_ERRNO(EOPNOTSUPP),
93 "gcrypt support is not compiled in.");
94 #endif
95 }
96
97 int initialize_libgcrypt(bool secmem) {
98 #if HAVE_GCRYPT
99 int r;
100
101 r = dlopen_gcrypt(LOG_DEBUG);
102 if (r < 0)
103 return r;
104
105 if (sym_gcry_control(GCRYCTL_INITIALIZATION_FINISHED_P))
106 return 0;
107
108 sym_gcry_control(GCRYCTL_SET_PREFERRED_RNG_TYPE, GCRY_RNG_TYPE_SYSTEM);
109 assert_se(sym_gcry_check_version("1.4.5"));
110
111 /* Turn off "secmem". Clients which wish to make use of this
112 * feature should initialize the library manually */
113 if (!secmem)
114 sym_gcry_control(GCRYCTL_DISABLE_SECMEM);
115
116 sym_gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
117
118 return 0;
119 #else
120 return -EOPNOTSUPP;
121 #endif
122 }