LCOV - code coverage report
Current view: top level - basic - gcrypt-util.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 21 24 87.5 %
Date: 2019-08-22 15:41:25 Functions: 2 2 100.0 %

          Line data    Source code
       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          11 : void initialize_libgcrypt(bool secmem) {
      10          11 :         if (gcry_control(GCRYCTL_INITIALIZATION_FINISHED_P))
      11           9 :                 return;
      12             : 
      13           2 :         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           2 :         if (!secmem)
      18           2 :                 gcry_control(GCRYCTL_DISABLE_SECMEM);
      19           2 :         gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
      20             : }
      21             : 
      22           4 : int string_hashsum(const char *s, size_t len, int md_algorithm, char **out) {
      23           4 :         _cleanup_(gcry_md_closep) gcry_md_hd_t md = NULL;
      24             :         size_t hash_size;
      25             :         void *hash;
      26             :         char *enc;
      27             : 
      28           4 :         initialize_libgcrypt(false);
      29             : 
      30           4 :         hash_size = gcry_md_get_algo_dlen(md_algorithm);
      31           4 :         assert(hash_size > 0);
      32             : 
      33           4 :         gcry_md_open(&md, md_algorithm, 0);
      34           4 :         if (!md)
      35           0 :                 return -EIO;
      36             : 
      37           4 :         gcry_md_write(md, s, len);
      38             : 
      39           4 :         hash = gcry_md_read(md, 0);
      40           4 :         if (!hash)
      41           0 :                 return -EIO;
      42             : 
      43           4 :         enc = hexmem(hash, hash_size);
      44           4 :         if (!enc)
      45           0 :                 return -ENOMEM;
      46             : 
      47           4 :         *out = enc;
      48           4 :         return 0;
      49             : }
      50             : #endif

Generated by: LCOV version 1.14