]>
Commit | Line | Data |
---|---|---|
91e023d8 ZJS |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2012 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU Lesser General Public License as published by | |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | Lesser General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU Lesser General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
349cc4a5 | 22 | #if HAVE_GCRYPT |
91e023d8 ZJS |
23 | #include <gcrypt.h> |
24 | ||
91e023d8 | 25 | #include "gcrypt-util.h" |
b68f10bf | 26 | #include "hexdecoct.h" |
91e023d8 ZJS |
27 | |
28 | void initialize_libgcrypt(bool secmem) { | |
29 | const char *p; | |
30 | if (gcry_control(GCRYCTL_INITIALIZATION_FINISHED_P)) | |
31 | return; | |
32 | ||
33 | p = gcry_check_version("1.4.5"); | |
34 | assert(p); | |
35 | ||
96d49011 | 36 | /* Turn off "secmem". Clients which wish to make use of this |
91e023d8 ZJS |
37 | * feature should initialize the library manually */ |
38 | if (!secmem) | |
39 | gcry_control(GCRYCTL_DISABLE_SECMEM); | |
40 | gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0); | |
41 | } | |
4ac2ca1b ZJS |
42 | |
43 | int string_hashsum(const char *s, size_t len, int md_algorithm, char **out) { | |
44 | gcry_md_hd_t md = NULL; | |
45 | size_t hash_size; | |
46 | void *hash; | |
47 | char *enc; | |
48 | ||
49 | initialize_libgcrypt(false); | |
50 | ||
51 | hash_size = gcry_md_get_algo_dlen(md_algorithm); | |
52 | assert(hash_size > 0); | |
53 | ||
54 | gcry_md_open(&md, md_algorithm, 0); | |
55 | if (!md) | |
56 | return -EIO; | |
57 | ||
58 | gcry_md_write(md, s, len); | |
59 | ||
60 | hash = gcry_md_read(md, 0); | |
61 | if (!hash) | |
62 | return -EIO; | |
63 | ||
64 | enc = hexmem(hash, hash_size); | |
65 | if (!enc) | |
66 | return -ENOMEM; | |
67 | ||
68 | *out = enc; | |
69 | return 0; | |
70 | } | |
b68f10bf | 71 | #endif |