/src/libgcrypt/src/global.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* global.c - global control functions |
2 | | * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 |
3 | | * 2004, 2005, 2006, 2008, 2011, |
4 | | * 2012 Free Software Foundation, Inc. |
5 | | * Copyright (C) 2013, 2014, 2017 g10 Code GmbH |
6 | | * |
7 | | * This file is part of Libgcrypt. |
8 | | * |
9 | | * Libgcrypt is free software; you can redistribute it and/or modify |
10 | | * it under the terms of the GNU Lesser general Public License as |
11 | | * published by the Free Software Foundation; either version 2.1 of |
12 | | * the License, or (at your option) any later version. |
13 | | * |
14 | | * Libgcrypt is distributed in the hope that it will be useful, |
15 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | | * GNU Lesser General Public License for more details. |
18 | | * |
19 | | * You should have received a copy of the GNU Lesser General Public |
20 | | * License along with this program; if not, see <http://www.gnu.org/licenses/>. |
21 | | */ |
22 | | |
23 | | #include <config.h> |
24 | | |
25 | | #include <stdio.h> |
26 | | #include <stdlib.h> |
27 | | #include <string.h> |
28 | | #include <stdarg.h> |
29 | | #include <ctype.h> |
30 | | #include <limits.h> |
31 | | #include <errno.h> |
32 | | #include <unistd.h> |
33 | | #ifdef HAVE_SYSLOG |
34 | | # include <syslog.h> |
35 | | #endif /*HAVE_SYSLOG*/ |
36 | | |
37 | | #include "g10lib.h" |
38 | | #include "gcrypt-testapi.h" |
39 | | #include "cipher.h" |
40 | | #include "stdmem.h" /* our own memory allocator */ |
41 | | #include "secmem.h" /* our own secmem allocator */ |
42 | | |
43 | | |
44 | | |
45 | | |
46 | | /**************** |
47 | | * flag bits: 0 : general cipher debug |
48 | | * 1 : general MPI debug |
49 | | */ |
50 | | static unsigned int debug_flags; |
51 | | |
52 | | /* gcry_control (GCRYCTL_SET_FIPS_MODE), sets this flag so that the |
53 | | initialization code switched fips mode on. */ |
54 | | static int force_fips_mode; |
55 | | |
56 | | /* Controlled by global_init(). */ |
57 | | int _gcry_global_any_init_done; |
58 | | |
59 | | /* |
60 | | * Functions called before and after blocking syscalls. |
61 | | * Initialized by global_init and used via |
62 | | * _gcry_pre_syscall and _gcry_post_syscall. |
63 | | */ |
64 | | static void (*pre_syscall_func)(void); |
65 | | static void (*post_syscall_func)(void); |
66 | | |
67 | | |
68 | | /* Memory management. */ |
69 | | |
70 | | static gcry_handler_alloc_t alloc_func; |
71 | | static gcry_handler_alloc_t alloc_secure_func; |
72 | | static gcry_handler_secure_check_t is_secure_func; |
73 | | static gcry_handler_realloc_t realloc_func; |
74 | | static gcry_handler_free_t free_func; |
75 | | static gcry_handler_no_mem_t outofcore_handler; |
76 | | static void *outofcore_handler_value; |
77 | | static int no_secure_memory; |
78 | | |
79 | | /* Prototypes. */ |
80 | | static gpg_err_code_t external_lock_test (int cmd); |
81 | | |
82 | | |
83 | | |
84 | | |
85 | | /* This is our handmade constructor. It gets called by any function |
86 | | likely to be called at startup. The suggested way for an |
87 | | application to make sure that this has been called is by using |
88 | | gcry_check_version. */ |
89 | | static void |
90 | | global_init (void) |
91 | 5 | { |
92 | 5 | gcry_error_t err = 0; |
93 | | |
94 | 5 | if (_gcry_global_any_init_done) |
95 | 1 | return; |
96 | 4 | _gcry_global_any_init_done = 1; |
97 | | |
98 | | /* Tell the random module that we have seen an init call. */ |
99 | 4 | _gcry_set_preferred_rng_type (0); |
100 | | |
101 | | /* Get the system call clamp functions. */ |
102 | 4 | if (!pre_syscall_func) |
103 | 4 | gpgrt_get_syscall_clamp (&pre_syscall_func, &post_syscall_func); |
104 | | |
105 | | /* See whether the system is in FIPS mode. This needs to come as |
106 | | early as possible but after ATH has been initialized. */ |
107 | 4 | _gcry_initialize_fips_mode (force_fips_mode); |
108 | | |
109 | | /* Before we do any other initialization we need to test available |
110 | | hardware features. */ |
111 | 4 | _gcry_detect_hw_features (); |
112 | | |
113 | | /* Initialize the modules - this is mainly allocating some memory and |
114 | | creating mutexes. */ |
115 | 4 | err = _gcry_cipher_init (); |
116 | 4 | if (err) |
117 | 0 | goto fail; |
118 | 4 | err = _gcry_md_init (); |
119 | 4 | if (err) |
120 | 0 | goto fail; |
121 | 4 | err = _gcry_mac_init (); |
122 | 4 | if (err) |
123 | 0 | goto fail; |
124 | 4 | err = _gcry_pk_init (); |
125 | 4 | if (err) |
126 | 0 | goto fail; |
127 | 4 | err = _gcry_primegen_init (); |
128 | 4 | if (err) |
129 | 0 | goto fail; |
130 | 4 | err = _gcry_secmem_module_init (); |
131 | 4 | if (err) |
132 | 0 | goto fail; |
133 | 4 | err = _gcry_mpi_init (); |
134 | 4 | if (err) |
135 | 0 | goto fail; |
136 | | |
137 | 4 | return; |
138 | | |
139 | 4 | fail: |
140 | 0 | BUG (); |
141 | 4 | } |
142 | | |
143 | | #ifdef ENABLE_HMAC_BINARY_CHECK |
144 | | # if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7 ) |
145 | | # define GCC_ATTR_CONSTRUCTOR __attribute__ ((__constructor__)) |
146 | | |
147 | | static void GCC_ATTR_CONSTRUCTOR |
148 | | _gcry_global_constructor (void) |
149 | | { |
150 | | force_fips_mode = _gcry_fips_to_activate (); |
151 | | if (force_fips_mode) |
152 | | { |
153 | | no_secure_memory = 1; |
154 | | global_init (); |
155 | | _gcry_fips_run_selftests (0); |
156 | | _gcry_random_close_fds (); |
157 | | no_secure_memory = 0; |
158 | | } |
159 | | } |
160 | | # endif |
161 | | #endif /* ENABLE_HMAC_BINARY_CHECK */ |
162 | | |
163 | | /* This function is called by the macro fips_is_operational and makes |
164 | | sure that the minimal initialization has been done. This is far |
165 | | from a perfect solution and hides problems with an improper |
166 | | initialization but at least in single-threaded mode it should work |
167 | | reliable. |
168 | | |
169 | | The reason we need this is that a lot of applications don't use |
170 | | Libgcrypt properly by not running any initialization code at all. |
171 | | They just call a Libgcrypt function and that is all what they want. |
172 | | Now with the FIPS mode, that has the side effect of entering FIPS |
173 | | mode (for security reasons, FIPS mode is the default if no |
174 | | initialization has been done) and bailing out immediately because |
175 | | the FSM is in the wrong state. If we always run the init code, |
176 | | Libgcrypt can test for FIPS mode and at least if not in FIPS mode, |
177 | | it will behave as before. Note that this on-the-fly initialization |
178 | | is only done for the cryptographic functions subject to FIPS mode |
179 | | and thus not all API calls will do such an initialization. */ |
180 | | int |
181 | | _gcry_global_is_operational (void) |
182 | 4 | { |
183 | 4 | if (!_gcry_global_any_init_done) |
184 | 4 | { |
185 | 4 | #ifdef HAVE_SYSLOG |
186 | 4 | syslog (LOG_USER|LOG_WARNING, "Libgcrypt warning: " |
187 | 4 | "missing initialization - please fix the application"); |
188 | 4 | #endif /*HAVE_SYSLOG*/ |
189 | 4 | global_init (); |
190 | 4 | } |
191 | 4 | return _gcry_fips_is_operational (); |
192 | 4 | } |
193 | | |
194 | | |
195 | | |
196 | | |
197 | | /* Version number parsing. */ |
198 | | |
199 | | /* This function parses the first portion of the version number S and |
200 | | stores it in *NUMBER. On success, this function returns a pointer |
201 | | into S starting with the first character, which is not part of the |
202 | | initial number portion; on failure, NULL is returned. */ |
203 | | static const char* |
204 | | parse_version_number( const char *s, int *number ) |
205 | 6 | { |
206 | 6 | int val = 0; |
207 | | |
208 | 6 | if( *s == '0' && isdigit(s[1]) ) |
209 | 0 | return NULL; /* leading zeros are not allowed */ |
210 | 7 | for ( ; isdigit(*s); s++ ) { |
211 | 7 | val *= 10; |
212 | 7 | val += *s - '0'; |
213 | 7 | } |
214 | 6 | *number = val; |
215 | 6 | return val < 0? NULL : s; |
216 | 6 | } |
217 | | |
218 | | /* This function breaks up the complete string-representation of the |
219 | | version number S, which is of the following struture: <major |
220 | | number>.<minor number>.<micro number><patch level>. The major, |
221 | | minor and micro number components will be stored in *MAJOR, *MINOR |
222 | | and *MICRO. |
223 | | |
224 | | On success, the last component, the patch level, will be returned; |
225 | | in failure, NULL will be returned. */ |
226 | | |
227 | | static const char * |
228 | | parse_version_string( const char *s, int *major, int *minor, int *micro ) |
229 | 2 | { |
230 | 2 | s = parse_version_number( s, major ); |
231 | 2 | if( !s || *s != '.' ) |
232 | 0 | return NULL; |
233 | 2 | s++; |
234 | 2 | s = parse_version_number( s, minor ); |
235 | 2 | if( !s || *s != '.' ) |
236 | 0 | return NULL; |
237 | 2 | s++; |
238 | 2 | s = parse_version_number( s, micro ); |
239 | 2 | if( !s ) |
240 | 0 | return NULL; |
241 | 2 | return s; /* patchlevel */ |
242 | 2 | } |
243 | | |
244 | | /* If REQ_VERSION is non-NULL, check that the version of the library |
245 | | is at minimum the requested one. Returns the string representation |
246 | | of the library version if the condition is satisfied; return NULL |
247 | | if the requested version is newer than that of the library. |
248 | | |
249 | | If a NULL is passed to this function, no check is done, but the |
250 | | string representation of the library is simply returned. */ |
251 | | const char * |
252 | | _gcry_check_version (const char *req_version) |
253 | 1 | { |
254 | 1 | const char *ver = VERSION; |
255 | 1 | int my_major, my_minor, my_micro; |
256 | 1 | int rq_major, rq_minor, rq_micro; |
257 | 1 | const char *my_plvl; |
258 | | |
259 | 1 | if (req_version && req_version[0] == 1 && req_version[1] == 1) |
260 | 0 | return _gcry_compat_identification (); |
261 | | |
262 | | /* Initialize library. */ |
263 | 1 | global_init (); |
264 | | |
265 | 1 | if ( !req_version ) |
266 | | /* Caller wants our version number. */ |
267 | 0 | return ver; |
268 | | |
269 | | /* Parse own version number. */ |
270 | 1 | my_plvl = parse_version_string( ver, &my_major, &my_minor, &my_micro ); |
271 | 1 | if ( !my_plvl ) |
272 | | /* very strange our own version is bogus. Shouldn't we use |
273 | | assert() here and bail out in case this happens? -mo. */ |
274 | 0 | return NULL; |
275 | | |
276 | | /* Parse requested version number. */ |
277 | 1 | if (!parse_version_string (req_version, &rq_major, &rq_minor, &rq_micro)) |
278 | 0 | return NULL; /* req version string is invalid, this can happen. */ |
279 | | |
280 | | /* Compare version numbers. */ |
281 | 1 | if ( my_major > rq_major |
282 | 1 | || (my_major == rq_major && my_minor > rq_minor) |
283 | 1 | || (my_major == rq_major && my_minor == rq_minor |
284 | 0 | && my_micro > rq_micro) |
285 | 1 | || (my_major == rq_major && my_minor == rq_minor |
286 | 0 | && my_micro == rq_micro)) |
287 | 1 | { |
288 | 1 | return ver; |
289 | 1 | } |
290 | | |
291 | 0 | return NULL; |
292 | 1 | } |
293 | | |
294 | | |
295 | | static void |
296 | | print_config (const char *what, gpgrt_stream_t fp) |
297 | 0 | { |
298 | 0 | int i; |
299 | 0 | const char *s; |
300 | |
|
301 | 0 | if (!what || !strcmp (what, "version")) |
302 | 0 | { |
303 | 0 | gpgrt_fprintf (fp, "version:%s:%x:%s:%x:\n", |
304 | 0 | VERSION, GCRYPT_VERSION_NUMBER, |
305 | 0 | GPGRT_VERSION, GPGRT_VERSION_NUMBER); |
306 | 0 | } |
307 | 0 | if (!what || !strcmp (what, "cc")) |
308 | 0 | { |
309 | 0 | gpgrt_fprintf (fp, "cc:%d:%s:\n", |
310 | 0 | #if GPGRT_VERSION_NUMBER >= 0x011b00 /* 1.27 */ |
311 | 0 | GPGRT_GCC_VERSION |
312 | | #else |
313 | | _GPG_ERR_GCC_VERSION /* Due to a bug in gpg-error.h. */ |
314 | | #endif |
315 | 0 | , |
316 | 0 | #ifdef __clang__ |
317 | 0 | "clang:" __VERSION__ |
318 | | #elif __GNUC__ |
319 | | "gcc:" __VERSION__ |
320 | | #else |
321 | | ":" |
322 | | #endif |
323 | 0 | ); |
324 | 0 | } |
325 | |
|
326 | 0 | if (!what || !strcmp (what, "ciphers")) |
327 | 0 | gpgrt_fprintf (fp, "ciphers:%s:\n", LIBGCRYPT_CIPHERS); |
328 | 0 | if (!what || !strcmp (what, "pubkeys")) |
329 | 0 | gpgrt_fprintf (fp, "pubkeys:%s:\n", LIBGCRYPT_PUBKEY_CIPHERS); |
330 | 0 | if (!what || !strcmp (what, "digests")) |
331 | 0 | gpgrt_fprintf (fp, "digests:%s:\n", LIBGCRYPT_DIGESTS); |
332 | |
|
333 | 0 | if (!what || !strcmp (what, "rnd-mod")) |
334 | 0 | { |
335 | 0 | gpgrt_fprintf (fp, "rnd-mod:" |
336 | | #if USE_RNDEGD |
337 | | "egd:" |
338 | | #endif |
339 | 0 | #if USE_RNDGETENTROPY |
340 | 0 | "getentropy:" |
341 | 0 | #endif |
342 | | #if USE_RNDOLDLINUX |
343 | | "oldlinux:" |
344 | | #endif |
345 | | #if USE_RNDUNIX |
346 | | "unix:" |
347 | | #endif |
348 | | #if USE_RNDW32 |
349 | | "w32:" |
350 | | #endif |
351 | 0 | "\n"); |
352 | 0 | } |
353 | |
|
354 | 0 | if (!what || !strcmp (what, "cpu-arch")) |
355 | 0 | { |
356 | 0 | gpgrt_fprintf (fp, "cpu-arch:" |
357 | 0 | #if defined(HAVE_CPU_ARCH_X86) |
358 | 0 | "x86" |
359 | | #elif defined(HAVE_CPU_ARCH_ALPHA) |
360 | | "alpha" |
361 | | #elif defined(HAVE_CPU_ARCH_SPARC) |
362 | | "sparc" |
363 | | #elif defined(HAVE_CPU_ARCH_MIPS) |
364 | | "mips" |
365 | | #elif defined(HAVE_CPU_ARCH_M68K) |
366 | | "m68k" |
367 | | #elif defined(HAVE_CPU_ARCH_PPC) |
368 | | "ppc" |
369 | | #elif defined(HAVE_CPU_ARCH_ARM) |
370 | | "arm" |
371 | | #endif |
372 | 0 | ":\n"); |
373 | 0 | } |
374 | |
|
375 | 0 | if (!what || !strcmp (what, "mpi-asm")) |
376 | 0 | gpgrt_fprintf (fp, "mpi-asm:%s:\n", _gcry_mpi_get_hw_config ()); |
377 | |
|
378 | 0 | if (!what || !strcmp (what, "hwflist")) |
379 | 0 | { |
380 | 0 | unsigned int hwfeatures, afeature; |
381 | |
|
382 | 0 | hwfeatures = _gcry_get_hw_features (); |
383 | 0 | gpgrt_fprintf (fp, "hwflist:"); |
384 | 0 | for (i=0; (s = _gcry_enum_hw_features (i, &afeature)); i++) |
385 | 0 | if ((hwfeatures & afeature)) |
386 | 0 | gpgrt_fprintf (fp, "%s:", s); |
387 | 0 | gpgrt_fprintf (fp, "\n"); |
388 | 0 | } |
389 | |
|
390 | 0 | if (!what || !strcmp (what, "fips-mode")) |
391 | 0 | { |
392 | | /* We use y/n instead of 1/0 for the stupid reason that |
393 | | * Emacsen's compile error parser would accidentally flag that |
394 | | * line when printed during "make check" as an error. The |
395 | | * second field is obsolete and thus empty (used to be used for |
396 | | * a so-called enforced-fips-mode). The third field has an |
397 | | * option static string describing the module versions; this is |
398 | | * an optional configure option. */ |
399 | 0 | gpgrt_fprintf (fp, "fips-mode:%c::%s:\n", |
400 | 0 | fips_mode ()? 'y':'n', |
401 | 0 | #ifdef FIPS_MODULE_VERSION |
402 | 0 | fips_mode () ? FIPS_MODULE_VERSION : "" |
403 | | #else |
404 | | "" |
405 | | #endif /* FIPS_MODULE_VERSION */ |
406 | 0 | ); |
407 | 0 | } |
408 | |
|
409 | 0 | if (!what || !strcmp (what, "rng-type")) |
410 | 0 | { |
411 | | /* The currently used RNG type. */ |
412 | 0 | unsigned int jver; |
413 | 0 | int active; |
414 | |
|
415 | 0 | i = _gcry_get_rng_type (0); |
416 | 0 | switch (i) |
417 | 0 | { |
418 | 0 | case GCRY_RNG_TYPE_STANDARD: s = "standard"; break; |
419 | 0 | case GCRY_RNG_TYPE_FIPS: s = "fips"; break; |
420 | 0 | case GCRY_RNG_TYPE_SYSTEM: s = "system"; break; |
421 | 0 | default: BUG (); |
422 | 0 | } |
423 | 0 | jver = _gcry_rndjent_get_version (&active); |
424 | 0 | gpgrt_fprintf (fp, "rng-type:%s:%d:%u:%d:\n", s, i, jver, active); |
425 | 0 | } |
426 | | |
427 | 0 | if (!what || !strcmp (what, "compliance")) |
428 | 0 | { |
429 | | /* Right now we have no certification for 1.9 so we return an |
430 | | * empty string. As soon as this version has been approved for |
431 | | * VS-Nfd we will put the string "de-vs" into the second |
432 | | * field. If further specifications are required they are added |
433 | | * as parameters to that field. Other certifications will go |
434 | | * into field 3 and so on. |
435 | | * field 1: keyword "compliance" |
436 | | * field 2: German VS-Nfd is marked with "de-vs" |
437 | | * field 3: reserved for FIPS. |
438 | | */ |
439 | 0 | gpgrt_fprintf (fp, "compliance:%s::\n", ""); |
440 | 0 | } |
441 | 0 | } |
442 | | |
443 | | |
444 | | /* With a MODE of 0 return a malloced string with configured features. |
445 | | * In that case a WHAT of NULL returns everything in the same way |
446 | | * GCRYCTL_PRINT_CONFIG would do. With a specific WHAT string only |
447 | | * the requested feature is returned (w/o the trailing LF. On error |
448 | | * NULL is returned. */ |
449 | | char * |
450 | | _gcry_get_config (int mode, const char *what) |
451 | 0 | { |
452 | 0 | gpgrt_stream_t fp; |
453 | 0 | int save_errno; |
454 | 0 | void *data; |
455 | 0 | char *p; |
456 | |
|
457 | 0 | if (mode) |
458 | 0 | { |
459 | 0 | gpg_err_set_errno (EINVAL); |
460 | 0 | return NULL; |
461 | 0 | } |
462 | | |
463 | 0 | fp = gpgrt_fopenmem (0, "w+b,samethread"); |
464 | 0 | if (!fp) |
465 | 0 | return NULL; |
466 | | |
467 | 0 | print_config (what, fp); |
468 | |
|
469 | 0 | if (!what) |
470 | 0 | { |
471 | | /* Null-terminate bulk output. */ |
472 | 0 | gpgrt_fwrite ("\0", 1, 1, fp); |
473 | 0 | } |
474 | |
|
475 | 0 | if (gpgrt_ferror (fp)) |
476 | 0 | { |
477 | 0 | save_errno = errno; |
478 | 0 | gpgrt_fclose (fp); |
479 | 0 | gpg_err_set_errno (save_errno); |
480 | 0 | return NULL; |
481 | 0 | } |
482 | | |
483 | 0 | gpgrt_rewind (fp); |
484 | 0 | if (gpgrt_fclose_snatch (fp, &data, NULL)) |
485 | 0 | { |
486 | 0 | save_errno = errno; |
487 | 0 | gpgrt_fclose (fp); |
488 | 0 | gpg_err_set_errno (save_errno); |
489 | 0 | return NULL; |
490 | 0 | } |
491 | | |
492 | 0 | if (!data) |
493 | 0 | { |
494 | | /* Nothing was printed (unknown value for WHAT). This is okay, |
495 | | * so clear ERRNO to indicate this. */ |
496 | 0 | gpg_err_set_errno (0); |
497 | 0 | return NULL; |
498 | 0 | } |
499 | | |
500 | | /* Strip trailing LF. */ |
501 | 0 | if (what && (p = strchr (data, '\n'))) |
502 | 0 | *p = 0; |
503 | |
|
504 | 0 | return data; |
505 | 0 | } |
506 | | |
507 | | |
508 | | |
509 | | |
510 | | #if _GCRY_GCC_VERSION >= 40200 |
511 | | # pragma GCC diagnostic push |
512 | | # pragma GCC diagnostic ignored "-Wswitch" |
513 | | #endif |
514 | | |
515 | | /* Command dispatcher function, acting as general control |
516 | | function. */ |
517 | | gcry_err_code_t |
518 | | _gcry_vcontrol (enum gcry_ctl_cmds cmd, va_list arg_ptr) |
519 | 0 | { |
520 | 0 | static int init_finished = 0; |
521 | 0 | gcry_err_code_t rc = 0; |
522 | |
|
523 | 0 | switch (cmd) |
524 | 0 | { |
525 | 0 | case GCRYCTL_ENABLE_M_GUARD: |
526 | 0 | rc = GPG_ERR_NOT_SUPPORTED; |
527 | 0 | break; |
528 | | |
529 | 0 | case GCRYCTL_ENABLE_QUICK_RANDOM: |
530 | 0 | _gcry_set_preferred_rng_type (0); |
531 | 0 | _gcry_enable_quick_random_gen (); |
532 | 0 | break; |
533 | | |
534 | 0 | case GCRYCTL_FAKED_RANDOM_P: |
535 | | /* Return an error if the RNG is faked one (e.g. enabled by |
536 | | ENABLE_QUICK_RANDOM. */ |
537 | 0 | if (_gcry_random_is_faked ()) |
538 | 0 | rc = GPG_ERR_GENERAL; /* Use as TRUE value. */ |
539 | 0 | break; |
540 | | |
541 | 0 | case GCRYCTL_DUMP_RANDOM_STATS: |
542 | 0 | _gcry_random_dump_stats (); |
543 | 0 | break; |
544 | | |
545 | 0 | case GCRYCTL_DUMP_MEMORY_STATS: |
546 | | /*m_print_stats("[fixme: prefix]");*/ |
547 | 0 | break; |
548 | | |
549 | 0 | case GCRYCTL_DUMP_SECMEM_STATS: |
550 | 0 | _gcry_secmem_dump_stats (0); |
551 | 0 | break; |
552 | | |
553 | 0 | case GCRYCTL_DROP_PRIVS: |
554 | 0 | global_init (); |
555 | 0 | _gcry_secmem_init (0); |
556 | 0 | break; |
557 | | |
558 | 0 | case GCRYCTL_DISABLE_SECMEM: |
559 | 0 | global_init (); |
560 | | /* When FIPS enabled, no effect at all. */ |
561 | 0 | if (!fips_mode ()) |
562 | 0 | no_secure_memory = 1; |
563 | 0 | break; |
564 | | |
565 | 0 | case GCRYCTL_INIT_SECMEM: |
566 | 0 | global_init (); |
567 | 0 | _gcry_secmem_init (va_arg (arg_ptr, unsigned int)); |
568 | 0 | if ((_gcry_secmem_get_flags () & GCRY_SECMEM_FLAG_NOT_LOCKED)) |
569 | 0 | rc = GPG_ERR_GENERAL; |
570 | 0 | break; |
571 | | |
572 | 0 | case GCRYCTL_TERM_SECMEM: |
573 | 0 | global_init (); |
574 | 0 | _gcry_secmem_term (); |
575 | 0 | break; |
576 | | |
577 | 0 | case GCRYCTL_DISABLE_SECMEM_WARN: |
578 | 0 | _gcry_set_preferred_rng_type (0); |
579 | 0 | _gcry_secmem_set_flags ((_gcry_secmem_get_flags () |
580 | 0 | | GCRY_SECMEM_FLAG_NO_WARNING)); |
581 | 0 | break; |
582 | | |
583 | 0 | case GCRYCTL_SUSPEND_SECMEM_WARN: |
584 | 0 | _gcry_set_preferred_rng_type (0); |
585 | 0 | _gcry_secmem_set_flags ((_gcry_secmem_get_flags () |
586 | 0 | | GCRY_SECMEM_FLAG_SUSPEND_WARNING)); |
587 | 0 | break; |
588 | | |
589 | 0 | case GCRYCTL_RESUME_SECMEM_WARN: |
590 | 0 | _gcry_set_preferred_rng_type (0); |
591 | 0 | _gcry_secmem_set_flags ((_gcry_secmem_get_flags () |
592 | 0 | & ~GCRY_SECMEM_FLAG_SUSPEND_WARNING)); |
593 | 0 | break; |
594 | | |
595 | 0 | case GCRYCTL_AUTO_EXPAND_SECMEM: |
596 | 0 | _gcry_secmem_set_auto_expand (va_arg (arg_ptr, unsigned int)); |
597 | 0 | break; |
598 | | |
599 | 0 | case GCRYCTL_USE_SECURE_RNDPOOL: |
600 | 0 | global_init (); |
601 | 0 | _gcry_secure_random_alloc (); /* Put random number into secure memory. */ |
602 | 0 | break; |
603 | | |
604 | 0 | case GCRYCTL_SET_RANDOM_SEED_FILE: |
605 | 0 | _gcry_set_preferred_rng_type (0); |
606 | 0 | _gcry_set_random_seed_file (va_arg (arg_ptr, const char *)); |
607 | 0 | break; |
608 | | |
609 | 0 | case GCRYCTL_UPDATE_RANDOM_SEED_FILE: |
610 | 0 | _gcry_set_preferred_rng_type (0); |
611 | 0 | if ( fips_is_operational () ) |
612 | 0 | _gcry_update_random_seed_file (); |
613 | 0 | break; |
614 | | |
615 | 0 | case GCRYCTL_SET_VERBOSITY: |
616 | 0 | _gcry_set_preferred_rng_type (0); |
617 | 0 | _gcry_set_log_verbosity (va_arg (arg_ptr, int)); |
618 | 0 | break; |
619 | | |
620 | 0 | case GCRYCTL_SET_DEBUG_FLAGS: |
621 | 0 | debug_flags |= va_arg (arg_ptr, unsigned int); |
622 | 0 | break; |
623 | | |
624 | 0 | case GCRYCTL_CLEAR_DEBUG_FLAGS: |
625 | 0 | debug_flags &= ~va_arg (arg_ptr, unsigned int); |
626 | 0 | break; |
627 | | |
628 | 0 | case GCRYCTL_DISABLE_INTERNAL_LOCKING: |
629 | | /* Not used anymore. */ |
630 | 0 | global_init (); |
631 | 0 | break; |
632 | | |
633 | 0 | case GCRYCTL_ANY_INITIALIZATION_P: |
634 | 0 | if (_gcry_global_any_init_done) |
635 | 0 | rc = GPG_ERR_GENERAL; |
636 | 0 | break; |
637 | | |
638 | 0 | case GCRYCTL_INITIALIZATION_FINISHED_P: |
639 | 0 | if (init_finished) |
640 | 0 | rc = GPG_ERR_GENERAL; /* Yes. */ |
641 | 0 | break; |
642 | | |
643 | 0 | case GCRYCTL_INITIALIZATION_FINISHED: |
644 | | /* This is a hook which should be used by an application after |
645 | | all initialization has been done and right before any threads |
646 | | are started. It is not really needed but the only way to be |
647 | | really sure that all initialization for thread-safety has |
648 | | been done. */ |
649 | 0 | if (! init_finished) |
650 | 0 | { |
651 | 0 | global_init (); |
652 | | /* Do only a basic random initialization, i.e. init the |
653 | | mutexes. */ |
654 | 0 | _gcry_random_initialize (0); |
655 | 0 | init_finished = 1; |
656 | | /* Force us into operational state if in FIPS mode. */ |
657 | 0 | (void)fips_is_operational (); |
658 | 0 | } |
659 | 0 | break; |
660 | | |
661 | 0 | case GCRYCTL_SET_THREAD_CBS: |
662 | | /* This is now a dummy call. We used to install our own thread |
663 | | library here. */ |
664 | 0 | _gcry_set_preferred_rng_type (0); |
665 | 0 | global_init (); |
666 | 0 | break; |
667 | | |
668 | 0 | case GCRYCTL_FAST_POLL: |
669 | 0 | _gcry_set_preferred_rng_type (0); |
670 | | /* We need to do make sure that the random pool is really |
671 | | initialized so that the poll function is not a NOP. */ |
672 | 0 | _gcry_random_initialize (1); |
673 | |
|
674 | 0 | if ( fips_is_operational () ) |
675 | 0 | _gcry_fast_random_poll (); |
676 | 0 | break; |
677 | | |
678 | 0 | case GCRYCTL_SET_RNDEGD_SOCKET: |
679 | | #if USE_RNDEGD |
680 | | _gcry_set_preferred_rng_type (0); |
681 | | rc = _gcry_rndegd_set_socket_name (va_arg (arg_ptr, const char *)); |
682 | | #else |
683 | 0 | rc = GPG_ERR_NOT_SUPPORTED; |
684 | 0 | #endif |
685 | 0 | break; |
686 | | |
687 | 0 | case GCRYCTL_SET_RANDOM_DAEMON_SOCKET: |
688 | 0 | rc = GPG_ERR_NOT_SUPPORTED; |
689 | 0 | break; |
690 | | |
691 | 0 | case GCRYCTL_USE_RANDOM_DAEMON: |
692 | 0 | rc = GPG_ERR_NOT_SUPPORTED; |
693 | 0 | break; |
694 | | |
695 | 0 | case GCRYCTL_CLOSE_RANDOM_DEVICE: |
696 | 0 | _gcry_random_close_fds (); |
697 | 0 | break; |
698 | | |
699 | | /* This command dumps information pertaining to the |
700 | | configuration of libgcrypt to the given stream. It may be |
701 | | used before the initialization has been finished but not |
702 | | before a gcry_version_check. See also gcry_get_config. */ |
703 | 0 | case GCRYCTL_PRINT_CONFIG: |
704 | 0 | { |
705 | 0 | FILE *fp = va_arg (arg_ptr, FILE *); |
706 | 0 | char *tmpstr; |
707 | 0 | _gcry_set_preferred_rng_type (0); |
708 | 0 | tmpstr = _gcry_get_config (0, NULL); |
709 | 0 | if (tmpstr) |
710 | 0 | { |
711 | 0 | if (fp) |
712 | 0 | fputs (tmpstr, fp); |
713 | 0 | else |
714 | 0 | log_info ("%s", tmpstr); |
715 | 0 | xfree (tmpstr); |
716 | 0 | } |
717 | 0 | } |
718 | 0 | break; |
719 | | |
720 | 0 | case GCRYCTL_OPERATIONAL_P: |
721 | | /* Returns true if the library is in an operational state. This |
722 | | is always true for non-fips mode. */ |
723 | 0 | _gcry_set_preferred_rng_type (0); |
724 | 0 | if (_gcry_fips_test_operational ()) |
725 | 0 | rc = GPG_ERR_GENERAL; /* Used as TRUE value */ |
726 | 0 | break; |
727 | | |
728 | 0 | case GCRYCTL_FIPS_MODE_P: |
729 | 0 | if (fips_mode ()) |
730 | 0 | rc = GPG_ERR_GENERAL; /* Used as TRUE value */ |
731 | 0 | break; |
732 | | |
733 | 0 | case GCRYCTL_FORCE_FIPS_MODE: |
734 | | /* Performing this command puts the library into fips mode. If |
735 | | the library has already been initialized into fips mode, a |
736 | | selftest is triggered. It is not possible to put the libraty |
737 | | into fips mode after having passed the initialization. */ |
738 | 0 | _gcry_set_preferred_rng_type (0); |
739 | 0 | if (!_gcry_global_any_init_done) |
740 | 0 | { |
741 | | /* Not yet initialized at all. Set a flag so that we are put |
742 | | into fips mode during initialization. */ |
743 | 0 | force_fips_mode = 1; |
744 | 0 | } |
745 | 0 | else |
746 | 0 | { |
747 | | /* Already initialized. If we are already operational we |
748 | | run a selftest. If not we use the is_operational call to |
749 | | force us into operational state if possible. */ |
750 | 0 | if (_gcry_fips_test_error_or_operational ()) |
751 | 0 | _gcry_fips_run_selftests (1); |
752 | 0 | if (_gcry_fips_is_operational ()) |
753 | 0 | rc = GPG_ERR_GENERAL; /* Used as TRUE value */ |
754 | 0 | } |
755 | 0 | break; |
756 | | |
757 | 0 | case GCRYCTL_NO_FIPS_MODE: |
758 | | /* Performing this command puts the library into non-fips mode, |
759 | | even if system has fips setting. It is not possible to put |
760 | | the libraty into non-fips mode after having passed the |
761 | | initialization. */ |
762 | 0 | _gcry_set_preferred_rng_type (0); |
763 | 0 | if (!_gcry_global_any_init_done) |
764 | 0 | { |
765 | | /* Not yet initialized at all. Set a flag so that we are put |
766 | | into non-fips mode during initialization. */ |
767 | 0 | force_fips_mode = 0; |
768 | 0 | } |
769 | 0 | else if (!init_finished) |
770 | 0 | { |
771 | | /* Already initialized. */ |
772 | 0 | _gcry_no_fips_mode_required = 1; |
773 | 0 | } |
774 | 0 | else |
775 | 0 | rc = GPG_ERR_GENERAL; |
776 | 0 | break; |
777 | | |
778 | 0 | case GCRYCTL_SELFTEST: |
779 | | /* Run a selftest. This works in fips mode as well as in |
780 | | standard mode. In contrast to the power-up tests, we use an |
781 | | extended version of the selftests. Returns 0 on success or an |
782 | | error code. */ |
783 | 0 | global_init (); |
784 | 0 | rc = _gcry_fips_run_selftests (1); |
785 | 0 | break; |
786 | | |
787 | 0 | case GCRYCTL_FIPS_SERVICE_INDICATOR_CIPHER: |
788 | | /* Get FIPS Service Indicator for a given symmetric algorithm and |
789 | | * optional mode. Returns GPG_ERR_NO_ERROR if algorithm is allowed or |
790 | | * GPG_ERR_NOT_SUPPORTED otherwise */ |
791 | 0 | rc = _gcry_fips_indicator_cipher (arg_ptr); |
792 | 0 | break; |
793 | | |
794 | 0 | case GCRYCTL_FIPS_SERVICE_INDICATOR_KDF: |
795 | | /* Get FIPS Service Indicator for a given KDF. Returns GPG_ERR_NO_ERROR |
796 | | * if algorithm is allowed or GPG_ERR_NOT_SUPPORTED otherwise */ |
797 | 0 | rc = _gcry_fips_indicator_kdf (arg_ptr); |
798 | 0 | break; |
799 | | |
800 | 0 | case GCRYCTL_FIPS_SERVICE_INDICATOR_FUNCTION: |
801 | | /* Get FIPS Service Indicator for a given function from the API. |
802 | | * Returns GPG_ERR_NO_ERROR if the function is allowed or |
803 | | * GPG_ERR_NOT_SUPPORTED otherwise */ |
804 | 0 | rc = _gcry_fips_indicator_function (arg_ptr); |
805 | 0 | break; |
806 | | |
807 | 0 | case PRIV_CTL_INIT_EXTRNG_TEST: /* Init external random test. */ |
808 | 0 | rc = GPG_ERR_NOT_SUPPORTED; |
809 | 0 | break; |
810 | 0 | case PRIV_CTL_RUN_EXTRNG_TEST: /* Run external DRBG test. */ |
811 | 0 | { |
812 | 0 | struct gcry_drbg_test_vector *test = |
813 | 0 | va_arg (arg_ptr, struct gcry_drbg_test_vector *); |
814 | 0 | unsigned char *buf = va_arg (arg_ptr, unsigned char *); |
815 | |
|
816 | 0 | if (buf) |
817 | 0 | rc = _gcry_rngdrbg_cavs_test (test, buf); |
818 | 0 | else |
819 | 0 | rc = _gcry_rngdrbg_healthcheck_one (test); |
820 | 0 | } |
821 | 0 | break; |
822 | 0 | case PRIV_CTL_DEINIT_EXTRNG_TEST: /* Deinit external random test. */ |
823 | 0 | rc = GPG_ERR_NOT_SUPPORTED; |
824 | 0 | break; |
825 | 0 | case PRIV_CTL_EXTERNAL_LOCK_TEST: /* Run external lock test */ |
826 | 0 | rc = external_lock_test (va_arg (arg_ptr, int)); |
827 | 0 | break; |
828 | 0 | case PRIV_CTL_DUMP_SECMEM_STATS: |
829 | 0 | _gcry_secmem_dump_stats (1); |
830 | 0 | break; |
831 | | |
832 | 0 | case GCRYCTL_DISABLE_HWF: |
833 | 0 | { |
834 | 0 | const char *name = va_arg (arg_ptr, const char *); |
835 | 0 | rc = _gcry_disable_hw_feature (name); |
836 | 0 | } |
837 | 0 | break; |
838 | | |
839 | 0 | case GCRYCTL_SET_ENFORCED_FIPS_FLAG: |
840 | | /* Obsolete - ignore */ |
841 | 0 | break; |
842 | | |
843 | 0 | case GCRYCTL_SET_PREFERRED_RNG_TYPE: |
844 | | /* This may be called before gcry_check_version. */ |
845 | 0 | { |
846 | 0 | int i = va_arg (arg_ptr, int); |
847 | | /* Note that we may not pass 0 to _gcry_set_preferred_rng_type. */ |
848 | 0 | if (i > 0) |
849 | 0 | _gcry_set_preferred_rng_type (i); |
850 | 0 | } |
851 | 0 | break; |
852 | | |
853 | 0 | case GCRYCTL_GET_CURRENT_RNG_TYPE: |
854 | 0 | { |
855 | 0 | int *ip = va_arg (arg_ptr, int*); |
856 | 0 | if (ip) |
857 | 0 | *ip = _gcry_get_rng_type (!_gcry_global_any_init_done); |
858 | 0 | } |
859 | 0 | break; |
860 | | |
861 | 0 | case GCRYCTL_DISABLE_LOCKED_SECMEM: |
862 | 0 | _gcry_set_preferred_rng_type (0); |
863 | 0 | _gcry_secmem_set_flags ((_gcry_secmem_get_flags () |
864 | 0 | | GCRY_SECMEM_FLAG_NO_MLOCK)); |
865 | 0 | break; |
866 | | |
867 | 0 | case GCRYCTL_DISABLE_PRIV_DROP: |
868 | 0 | _gcry_set_preferred_rng_type (0); |
869 | 0 | _gcry_secmem_set_flags ((_gcry_secmem_get_flags () |
870 | 0 | | GCRY_SECMEM_FLAG_NO_PRIV_DROP)); |
871 | 0 | break; |
872 | | |
873 | 0 | case GCRYCTL_INACTIVATE_FIPS_FLAG: |
874 | 0 | case GCRYCTL_REACTIVATE_FIPS_FLAG: |
875 | 0 | rc = GPG_ERR_NOT_IMPLEMENTED; |
876 | 0 | break; |
877 | | |
878 | 0 | case GCRYCTL_DRBG_REINIT: |
879 | 0 | { |
880 | 0 | const char *flagstr = va_arg (arg_ptr, const char *); |
881 | 0 | gcry_buffer_t *pers = va_arg (arg_ptr, gcry_buffer_t *); |
882 | 0 | int npers = va_arg (arg_ptr, int); |
883 | 0 | if (va_arg (arg_ptr, void *) || npers < 0) |
884 | 0 | rc = GPG_ERR_INV_ARG; |
885 | 0 | else if (_gcry_get_rng_type (!_gcry_global_any_init_done) |
886 | 0 | != GCRY_RNG_TYPE_FIPS) |
887 | 0 | rc = GPG_ERR_NOT_SUPPORTED; |
888 | 0 | else |
889 | 0 | rc = _gcry_rngdrbg_reinit (flagstr, pers, npers); |
890 | 0 | } |
891 | 0 | break; |
892 | | |
893 | 0 | case GCRYCTL_REINIT_SYSCALL_CLAMP: |
894 | 0 | if (!pre_syscall_func) |
895 | 0 | gpgrt_get_syscall_clamp (&pre_syscall_func, &post_syscall_func); |
896 | 0 | break; |
897 | | |
898 | 0 | default: |
899 | 0 | _gcry_set_preferred_rng_type (0); |
900 | 0 | rc = GPG_ERR_INV_OP; |
901 | 0 | } |
902 | | |
903 | 0 | return rc; |
904 | 0 | } |
905 | | |
906 | | #if _GCRY_GCC_VERSION >= 40200 |
907 | | # pragma GCC diagnostic pop |
908 | | #endif |
909 | | |
910 | | |
911 | | /* Set custom allocation handlers. This is in general not useful |
912 | | * because the libgcrypt allocation functions are guaranteed to |
913 | | * provide proper allocation handlers which zeroize memory if needed. |
914 | | * NOTE: All 5 functions should be set. */ |
915 | | void |
916 | | _gcry_set_allocation_handler (gcry_handler_alloc_t new_alloc_func, |
917 | | gcry_handler_alloc_t new_alloc_secure_func, |
918 | | gcry_handler_secure_check_t new_is_secure_func, |
919 | | gcry_handler_realloc_t new_realloc_func, |
920 | | gcry_handler_free_t new_free_func) |
921 | 0 | { |
922 | 0 | global_init (); |
923 | |
|
924 | 0 | if (fips_mode ()) |
925 | 0 | { |
926 | | /* In FIPS mode, we can not use custom allocation handlers because |
927 | | * fips requires explicit zeroization and we can not guarantee that |
928 | | * with custom free functions (and we can not do it transparently as |
929 | | * in free we do not know the zize). */ |
930 | 0 | return; |
931 | 0 | } |
932 | | |
933 | 0 | alloc_func = new_alloc_func; |
934 | 0 | alloc_secure_func = new_alloc_secure_func; |
935 | 0 | is_secure_func = new_is_secure_func; |
936 | 0 | realloc_func = new_realloc_func; |
937 | 0 | free_func = new_free_func; |
938 | 0 | } |
939 | | |
940 | | |
941 | | |
942 | | /**************** |
943 | | * Set an optional handler which is called in case the xmalloc functions |
944 | | * ran out of memory. This handler may do one of these things: |
945 | | * o free some memory and return true, so that the xmalloc function |
946 | | * tries again. |
947 | | * o Do whatever it like and return false, so that the xmalloc functions |
948 | | * use the default fatal error handler. |
949 | | * o Terminate the program and don't return. |
950 | | * |
951 | | * The handler function is called with 3 arguments: The opaque value set with |
952 | | * this function, the requested memory size, and a flag with these bits |
953 | | * currently defined: |
954 | | * bit 0 set = secure memory has been requested. |
955 | | */ |
956 | | void |
957 | | _gcry_set_outofcore_handler (int (*f)(void*, size_t, unsigned int), void *value) |
958 | 0 | { |
959 | 0 | global_init (); |
960 | |
|
961 | 0 | if (fips_mode () ) |
962 | 0 | { |
963 | 0 | log_info ("out of core handler ignored in FIPS mode\n"); |
964 | 0 | return; |
965 | 0 | } |
966 | | |
967 | 0 | outofcore_handler = f; |
968 | 0 | outofcore_handler_value = value; |
969 | 0 | } |
970 | | |
971 | | |
972 | | static gcry_err_code_t |
973 | | do_malloc (size_t n, unsigned int flags, void **mem) |
974 | 44.4M | { |
975 | 44.4M | gcry_err_code_t err = 0; |
976 | 44.4M | void *m; |
977 | | |
978 | 44.4M | if ((flags & GCRY_ALLOC_FLAG_SECURE) && !no_secure_memory) |
979 | 62.1k | { |
980 | 62.1k | if (alloc_secure_func) |
981 | 0 | m = (*alloc_secure_func) (n); |
982 | 62.1k | else |
983 | 62.1k | m = _gcry_private_malloc_secure (n, !!(flags & GCRY_ALLOC_FLAG_XHINT)); |
984 | 62.1k | } |
985 | 44.4M | else |
986 | 44.4M | { |
987 | 44.4M | if (alloc_func) |
988 | 0 | m = (*alloc_func) (n); |
989 | 44.4M | else |
990 | 44.4M | m = _gcry_private_malloc (n); |
991 | 44.4M | } |
992 | | |
993 | 44.4M | if (!m) |
994 | 50.4k | { |
995 | | /* Make sure that ERRNO has been set in case a user supplied |
996 | | memory handler didn't it correctly. */ |
997 | 50.4k | if (!errno) |
998 | 0 | gpg_err_set_errno (ENOMEM); |
999 | 50.4k | err = gpg_err_code_from_errno (errno); |
1000 | 50.4k | } |
1001 | 44.4M | else |
1002 | 44.4M | *mem = m; |
1003 | | |
1004 | 44.4M | return err; |
1005 | 44.4M | } |
1006 | | |
1007 | | void * |
1008 | | _gcry_malloc (size_t n) |
1009 | 44.4M | { |
1010 | 44.4M | void *mem = NULL; |
1011 | | |
1012 | 44.4M | do_malloc (n, 0, &mem); |
1013 | | |
1014 | 44.4M | return mem; |
1015 | 44.4M | } |
1016 | | |
1017 | | static void * |
1018 | | _gcry_malloc_secure_core (size_t n, int xhint) |
1019 | 62.1k | { |
1020 | 62.1k | void *mem = NULL; |
1021 | | |
1022 | 62.1k | do_malloc (n, (GCRY_ALLOC_FLAG_SECURE | (xhint? GCRY_ALLOC_FLAG_XHINT:0)), |
1023 | 62.1k | &mem); |
1024 | | |
1025 | 62.1k | return mem; |
1026 | 62.1k | } |
1027 | | |
1028 | | void * |
1029 | | _gcry_malloc_secure (size_t n) |
1030 | 0 | { |
1031 | 0 | return _gcry_malloc_secure_core (n, 0); |
1032 | 0 | } |
1033 | | |
1034 | | int |
1035 | | _gcry_is_secure (const void *a) |
1036 | 3.85M | { |
1037 | 3.85M | if (no_secure_memory) |
1038 | 0 | return 0; |
1039 | 3.85M | if (is_secure_func) |
1040 | 0 | return is_secure_func (a) ; |
1041 | 3.85M | return _gcry_private_is_secure (a); |
1042 | 3.85M | } |
1043 | | |
1044 | | static void * |
1045 | | _gcry_realloc_core (void *a, size_t n, int xhint) |
1046 | 306k | { |
1047 | 306k | void *p; |
1048 | | |
1049 | | /* To avoid problems with non-standard realloc implementations and |
1050 | | our own secmem_realloc, we divert to malloc and free here. */ |
1051 | 306k | if (!a) |
1052 | 26.7k | return _gcry_malloc (n); |
1053 | 280k | if (!n) |
1054 | 0 | { |
1055 | 0 | xfree (a); |
1056 | 0 | return NULL; |
1057 | 0 | } |
1058 | | |
1059 | 280k | if (realloc_func) |
1060 | 0 | p = realloc_func (a, n); |
1061 | 280k | else |
1062 | 280k | p = _gcry_private_realloc (a, n, xhint); |
1063 | 280k | if (!p && !errno) |
1064 | 0 | gpg_err_set_errno (ENOMEM); |
1065 | 280k | return p; |
1066 | 280k | } |
1067 | | |
1068 | | |
1069 | | void * |
1070 | | _gcry_realloc (void *a, size_t n) |
1071 | 79.7k | { |
1072 | 79.7k | return _gcry_realloc_core (a, n, 0); |
1073 | 79.7k | } |
1074 | | |
1075 | | |
1076 | | void |
1077 | | _gcry_free (void *p) |
1078 | 49.5M | { |
1079 | 49.5M | int save_errno; |
1080 | | |
1081 | 49.5M | if (!p) |
1082 | 5.12M | return; |
1083 | | |
1084 | | /* In case ERRNO is set we better save it so that the free machinery |
1085 | | may not accidentally change ERRNO. We restore it only if it was |
1086 | | already set to comply with the usual C semantic for ERRNO. */ |
1087 | 44.4M | save_errno = errno; |
1088 | 44.4M | if (free_func) |
1089 | 0 | free_func (p); |
1090 | 44.4M | else |
1091 | 44.4M | _gcry_private_free (p); |
1092 | | |
1093 | 44.4M | if (save_errno && save_errno != errno) |
1094 | 0 | gpg_err_set_errno (save_errno); |
1095 | 44.4M | } |
1096 | | |
1097 | | void * |
1098 | | _gcry_calloc (size_t n, size_t m) |
1099 | 373k | { |
1100 | 373k | size_t bytes; |
1101 | 373k | void *p; |
1102 | | |
1103 | 373k | bytes = n * m; /* size_t is unsigned so the behavior on overflow is |
1104 | | defined. */ |
1105 | 373k | if (m && bytes / m != n) |
1106 | 0 | { |
1107 | 0 | gpg_err_set_errno (ENOMEM); |
1108 | 0 | return NULL; |
1109 | 0 | } |
1110 | | |
1111 | 373k | p = _gcry_malloc (bytes); |
1112 | 373k | if (p) |
1113 | 373k | memset (p, 0, bytes); |
1114 | 373k | return p; |
1115 | 373k | } |
1116 | | |
1117 | | void * |
1118 | | _gcry_calloc_secure (size_t n, size_t m) |
1119 | 0 | { |
1120 | 0 | size_t bytes; |
1121 | 0 | void *p; |
1122 | |
|
1123 | 0 | bytes = n * m; /* size_t is unsigned so the behavior on overflow is |
1124 | | defined. */ |
1125 | 0 | if (m && bytes / m != n) |
1126 | 0 | { |
1127 | 0 | gpg_err_set_errno (ENOMEM); |
1128 | 0 | return NULL; |
1129 | 0 | } |
1130 | | |
1131 | 0 | p = _gcry_malloc_secure (bytes); |
1132 | 0 | if (p) |
1133 | 0 | memset (p, 0, bytes); |
1134 | 0 | return p; |
1135 | 0 | } |
1136 | | |
1137 | | |
1138 | | static char * |
1139 | | _gcry_strdup_core (const char *string, int xhint) |
1140 | 465k | { |
1141 | 465k | char *string_cp = NULL; |
1142 | 465k | size_t string_n = 0; |
1143 | | |
1144 | 465k | string_n = strlen (string); |
1145 | | |
1146 | 465k | if (_gcry_is_secure (string)) |
1147 | 0 | string_cp = _gcry_malloc_secure_core (string_n + 1, xhint); |
1148 | 465k | else |
1149 | 465k | string_cp = _gcry_malloc (string_n + 1); |
1150 | | |
1151 | 465k | if (string_cp) |
1152 | 465k | strcpy (string_cp, string); |
1153 | | |
1154 | 465k | return string_cp; |
1155 | 465k | } |
1156 | | |
1157 | | /* Create and return a copy of the null-terminated string STRING. If |
1158 | | * it is contained in secure memory, the copy will be contained in |
1159 | | * secure memory as well. In an out-of-memory condition, NULL is |
1160 | | * returned. */ |
1161 | | char * |
1162 | | _gcry_strdup (const char *string) |
1163 | 4.33k | { |
1164 | 4.33k | return _gcry_strdup_core (string, 0); |
1165 | 4.33k | } |
1166 | | |
1167 | | void * |
1168 | | _gcry_xmalloc( size_t n ) |
1169 | 41.7M | { |
1170 | 41.7M | void *p; |
1171 | | |
1172 | 41.7M | while ( !(p = _gcry_malloc( n )) ) |
1173 | 0 | { |
1174 | 0 | if ( fips_mode () |
1175 | 0 | || !outofcore_handler |
1176 | 0 | || !outofcore_handler (outofcore_handler_value, n, 0) ) |
1177 | 0 | { |
1178 | 0 | _gcry_fatal_error (gpg_err_code_from_errno (errno), NULL); |
1179 | 0 | } |
1180 | 0 | } |
1181 | 41.7M | return p; |
1182 | 41.7M | } |
1183 | | |
1184 | | void * |
1185 | | _gcry_xrealloc( void *a, size_t n ) |
1186 | 227k | { |
1187 | 227k | void *p; |
1188 | | |
1189 | 227k | while (!(p = _gcry_realloc_core (a, n, 1))) |
1190 | 0 | { |
1191 | 0 | if ( fips_mode () |
1192 | 0 | || !outofcore_handler |
1193 | 0 | || !outofcore_handler (outofcore_handler_value, n, |
1194 | 0 | _gcry_is_secure(a)? 3:2)) |
1195 | 0 | { |
1196 | 0 | _gcry_fatal_error (gpg_err_code_from_errno (errno), NULL ); |
1197 | 0 | } |
1198 | 0 | } |
1199 | 227k | return p; |
1200 | 227k | } |
1201 | | |
1202 | | void * |
1203 | | _gcry_xmalloc_secure( size_t n ) |
1204 | 62.1k | { |
1205 | 62.1k | void *p; |
1206 | | |
1207 | 62.1k | while (!(p = _gcry_malloc_secure_core (n, 1))) |
1208 | 0 | { |
1209 | 0 | if ( fips_mode () |
1210 | 0 | || !outofcore_handler |
1211 | 0 | || !outofcore_handler (outofcore_handler_value, n, 1) ) |
1212 | 0 | { |
1213 | 0 | _gcry_fatal_error (gpg_err_code_from_errno (errno), |
1214 | 0 | _("out of core in secure memory")); |
1215 | 0 | } |
1216 | 0 | } |
1217 | 62.1k | return p; |
1218 | 62.1k | } |
1219 | | |
1220 | | |
1221 | | void * |
1222 | | _gcry_xcalloc( size_t n, size_t m ) |
1223 | 8.87M | { |
1224 | 8.87M | size_t nbytes; |
1225 | 8.87M | void *p; |
1226 | | |
1227 | 8.87M | nbytes = n * m; |
1228 | 8.87M | if (m && nbytes / m != n) |
1229 | 0 | { |
1230 | 0 | gpg_err_set_errno (ENOMEM); |
1231 | 0 | _gcry_fatal_error(gpg_err_code_from_errno (errno), NULL ); |
1232 | 0 | } |
1233 | | |
1234 | 8.87M | p = _gcry_xmalloc ( nbytes ); |
1235 | 8.87M | memset ( p, 0, nbytes ); |
1236 | 8.87M | return p; |
1237 | 8.87M | } |
1238 | | |
1239 | | void * |
1240 | | _gcry_xcalloc_secure( size_t n, size_t m ) |
1241 | 62.1k | { |
1242 | 62.1k | size_t nbytes; |
1243 | 62.1k | void *p; |
1244 | | |
1245 | 62.1k | nbytes = n * m; |
1246 | 62.1k | if (m && nbytes / m != n) |
1247 | 0 | { |
1248 | 0 | gpg_err_set_errno (ENOMEM); |
1249 | 0 | _gcry_fatal_error(gpg_err_code_from_errno (errno), NULL ); |
1250 | 0 | } |
1251 | | |
1252 | 62.1k | p = _gcry_xmalloc_secure ( nbytes ); |
1253 | 62.1k | memset ( p, 0, nbytes ); |
1254 | 62.1k | return p; |
1255 | 62.1k | } |
1256 | | |
1257 | | char * |
1258 | | _gcry_xstrdup (const char *string) |
1259 | 460k | { |
1260 | 460k | char *p; |
1261 | | |
1262 | 460k | while ( !(p = _gcry_strdup_core (string, 1)) ) |
1263 | 0 | { |
1264 | 0 | size_t n = strlen (string); |
1265 | 0 | int is_sec = !!_gcry_is_secure (string); |
1266 | |
|
1267 | 0 | if (fips_mode () |
1268 | 0 | || !outofcore_handler |
1269 | 0 | || !outofcore_handler (outofcore_handler_value, n, is_sec) ) |
1270 | 0 | { |
1271 | 0 | _gcry_fatal_error (gpg_err_code_from_errno (errno), |
1272 | 0 | is_sec? _("out of core in secure memory"):NULL); |
1273 | 0 | } |
1274 | 0 | } |
1275 | | |
1276 | 460k | return p; |
1277 | 460k | } |
1278 | | |
1279 | | |
1280 | | /* Used before blocking system calls. */ |
1281 | | void |
1282 | | _gcry_pre_syscall (void) |
1283 | 75 | { |
1284 | 75 | if (pre_syscall_func) |
1285 | 0 | pre_syscall_func (); |
1286 | 75 | } |
1287 | | |
1288 | | |
1289 | | /* Used after blocking system calls. */ |
1290 | | void |
1291 | | _gcry_post_syscall (void) |
1292 | 75 | { |
1293 | 75 | if (post_syscall_func) |
1294 | 0 | post_syscall_func (); |
1295 | 75 | } |
1296 | | |
1297 | | |
1298 | | int |
1299 | | _gcry_get_debug_flag (unsigned int mask) |
1300 | 75.7k | { |
1301 | 75.7k | if ( fips_mode () ) |
1302 | 0 | return 0; |
1303 | 75.7k | return (debug_flags & mask); |
1304 | 75.7k | } |
1305 | | |
1306 | | |
1307 | | |
1308 | | /* It is often useful to get some feedback of long running operations. |
1309 | | This function may be used to register a handler for this. |
1310 | | The callback function CB is used as: |
1311 | | |
1312 | | void cb (void *opaque, const char *what, int printchar, |
1313 | | int current, int total); |
1314 | | |
1315 | | Where WHAT is a string identifying the the type of the progress |
1316 | | output, PRINTCHAR the character usually printed, CURRENT the amount |
1317 | | of progress currently done and TOTAL the expected amount of |
1318 | | progress. A value of 0 for TOTAL indicates that there is no |
1319 | | estimation available. |
1320 | | |
1321 | | Defined values for WHAT: |
1322 | | |
1323 | | "need_entropy" X 0 number-of-bytes-required |
1324 | | When running low on entropy |
1325 | | "primegen" '\n' 0 0 |
1326 | | Prime generated |
1327 | | '!' |
1328 | | Need to refresh the prime pool |
1329 | | '<','>' |
1330 | | Number of bits adjusted |
1331 | | '^' |
1332 | | Looking for a generator |
1333 | | '.' |
1334 | | Fermat tests on 10 candidates failed |
1335 | | ':' |
1336 | | Restart with a new random value |
1337 | | '+' |
1338 | | Rabin Miller test passed |
1339 | | "pk_elg" '+','-','.','\n' 0 0 |
1340 | | Only used in debugging mode. |
1341 | | "pk_dsa" |
1342 | | Only used in debugging mode. |
1343 | | */ |
1344 | | void |
1345 | | _gcry_set_progress_handler (void (*cb)(void *,const char*,int, int, int), |
1346 | | void *cb_data) |
1347 | 0 | { |
1348 | 0 | #if USE_DSA |
1349 | 0 | _gcry_register_pk_dsa_progress (cb, cb_data); |
1350 | 0 | #endif |
1351 | 0 | #if USE_ELGAMAL |
1352 | 0 | _gcry_register_pk_elg_progress (cb, cb_data); |
1353 | 0 | #endif |
1354 | 0 | _gcry_register_primegen_progress (cb, cb_data); |
1355 | 0 | _gcry_register_random_progress (cb, cb_data); |
1356 | 0 | } |
1357 | | |
1358 | | |
1359 | | |
1360 | | /* This is a helper for the regression test suite to test Libgcrypt's locks. |
1361 | | It works using a one test lock with CMD controlling what to do: |
1362 | | |
1363 | | 30111 - Allocate and init lock |
1364 | | 30112 - Take lock |
1365 | | 30113 - Release lock |
1366 | | 30114 - Destroy lock. |
1367 | | |
1368 | | This function is used by tests/t-lock.c - it is not part of the |
1369 | | public API! |
1370 | | */ |
1371 | | static gpg_err_code_t |
1372 | | external_lock_test (int cmd) |
1373 | 0 | { |
1374 | 0 | GPGRT_LOCK_DEFINE (testlock); |
1375 | 0 | gpg_err_code_t rc = 0; |
1376 | |
|
1377 | 0 | switch (cmd) |
1378 | 0 | { |
1379 | 0 | case 30111: /* Init Lock. */ |
1380 | 0 | rc = gpgrt_lock_init (&testlock); |
1381 | 0 | break; |
1382 | | |
1383 | 0 | case 30112: /* Take Lock. */ |
1384 | 0 | rc = gpgrt_lock_lock (&testlock); |
1385 | 0 | break; |
1386 | | |
1387 | 0 | case 30113: /* Release Lock. */ |
1388 | 0 | rc = gpgrt_lock_unlock (&testlock); |
1389 | 0 | break; |
1390 | | |
1391 | 0 | case 30114: /* Destroy Lock. */ |
1392 | 0 | rc = gpgrt_lock_destroy (&testlock); |
1393 | 0 | break; |
1394 | | |
1395 | 0 | default: |
1396 | 0 | rc = GPG_ERR_INV_OP; |
1397 | 0 | break; |
1398 | 0 | } |
1399 | | |
1400 | 0 | return rc; |
1401 | 0 | } |