Coverage Report

Created: 2025-11-24 06:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/dovecot/src/lib/hash-method.c
Line
Count
Source
1
/* Copyright (c) 2010-2018 Dovecot authors, see the included COPYING file */
2
3
#include "lib.h"
4
#include "md4.h"
5
#include "md5.h"
6
#include "sha1.h"
7
#include "sha2.h"
8
#include "sha3.h"
9
#include "hash-method.h"
10
11
const struct hash_method *hash_method_lookup(const char *name)
12
0
{
13
0
  unsigned int i;
14
15
0
  for (i = 0; hash_methods[i] != NULL; i++) {
16
0
    if (strcmp(hash_methods[i]->name, name) == 0)
17
0
      return hash_methods[i];
18
0
  }
19
0
  return NULL;
20
0
}
21
22
static void hash_method_init_size(void *context)
23
0
{
24
0
  uint64_t *ctx = context;
25
26
0
  *ctx = 0;
27
0
}
28
29
static void
30
hash_method_loop_size(void *context, const void *data ATTR_UNUSED, size_t size)
31
0
{
32
0
  uint64_t *ctx = context;
33
34
0
  *ctx += size;
35
0
}
36
37
static void hash_method_result_size(void *context, unsigned char *result_r)
38
0
{
39
0
  uint64_t *ctx = context;
40
41
0
  result_r[0] = (*ctx & 0xff00000000000000ULL) >> 56;
42
0
  result_r[1] = (*ctx & 0x00ff000000000000ULL) >> 48;
43
0
  result_r[2] = (*ctx & 0x0000ff0000000000ULL) >> 40;
44
0
  result_r[3] = (*ctx & 0x000000ff00000000ULL) >> 32;
45
0
  result_r[4] = (*ctx & 0x00000000ff000000ULL) >> 24;
46
0
  result_r[5] = (*ctx & 0x0000000000ff0000ULL) >> 16;
47
0
  result_r[6] = (*ctx & 0x000000000000ff00ULL) >> 8;
48
0
  result_r[7] = (*ctx & 0x00000000000000ffULL);
49
0
}
50
51
void hash_method_get_digest(const struct hash_method *meth,
52
          const void *data, size_t data_len,
53
          unsigned char *result_r)
54
2.97k
{
55
2.97k
  i_assert(meth != NULL);
56
2.97k
  i_assert(data_len == 0 || data != NULL);
57
2.97k
  unsigned char ctx[meth->context_size];
58
59
2.97k
  meth->init(ctx);
60
2.97k
  meth->loop(ctx, data == NULL ? "" : data, data_len);
61
2.97k
  meth->result(ctx, result_r);
62
2.97k
}
63
64
buffer_t *t_hash_data(const struct hash_method *meth,
65
          const void *data, size_t data_len)
66
0
{
67
0
  i_assert(meth != NULL);
68
0
  buffer_t *result = t_buffer_create(meth->digest_size);
69
0
  unsigned char *resptr = buffer_append_space_unsafe(result,
70
0
                 meth->digest_size);
71
72
0
  hash_method_get_digest(meth, data, data_len, resptr);
73
0
  return result;
74
0
}
75
76
static const struct hash_method hash_method_size = {
77
  .name = "size",
78
  .block_size = 1,
79
  .context_size = sizeof(uint64_t),
80
  .digest_size = sizeof(uint64_t),
81
82
  .init = hash_method_init_size,
83
  .loop = hash_method_loop_size,
84
  .result = hash_method_result_size
85
};
86
87
const struct hash_method *hash_methods[] = {
88
  &hash_method_md4,
89
  &hash_method_md5,
90
  &hash_method_sha1,
91
  &hash_method_sha256,
92
  &hash_method_sha384,
93
  &hash_method_sha512,
94
  &hash_method_sha3_256,
95
  &hash_method_sha3_512,
96
  &hash_method_size,
97
  NULL
98
};