Coverage Report

Created: 2026-11-11 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/nettle/md5.c
Line
Count
Source
1
/* md5.c
2
3
   The MD5 hash function, described in RFC 1321.
4
5
   Copyright (C) 2001 Niels Möller
6
7
   This file is part of GNU Nettle.
8
9
   GNU Nettle is free software: you can redistribute it and/or
10
   modify it under the terms of either:
11
12
     * the GNU Lesser General Public License as published by the Free
13
       Software Foundation; either version 3 of the License, or (at your
14
       option) any later version.
15
16
   or
17
18
     * the GNU General Public License as published by the Free
19
       Software Foundation; either version 2 of the License, or (at your
20
       option) any later version.
21
22
   or both in parallel, as here.
23
24
   GNU Nettle is distributed in the hope that it will be useful,
25
   but WITHOUT ANY WARRANTY; without even the implied warranty of
26
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27
   General Public License for more details.
28
29
   You should have received copies of the GNU General Public License and
30
   the GNU Lesser General Public License along with this program.  If
31
   not, see http://www.gnu.org/licenses/.
32
*/
33
34
/* Based on public domain code hacked by Colin Plumb, Andrew Kuchling, and
35
 * Niels Möller. */
36
37
#if HAVE_CONFIG_H
38
# include "config.h"
39
#endif
40
41
#include <assert.h>
42
#include <string.h>
43
44
#include "md5.h"
45
46
#include "macros.h"
47
#include "nettle-write.h"
48
49
void
50
md5_init(struct md5_ctx *ctx)
51
1.31k
{
52
1.31k
  const uint32_t iv[_MD5_DIGEST_LENGTH] =
53
1.31k
    {
54
1.31k
      0x67452301,
55
1.31k
      0xefcdab89,
56
1.31k
      0x98badcfe,
57
1.31k
      0x10325476,
58
1.31k
    };
59
1.31k
  memcpy(ctx->state, iv, sizeof(ctx->state));
60
1.31k
  ctx->count = 0;
61
1.31k
  ctx->index = 0;
62
1.31k
}
63
64
1.46k
#define COMPRESS(ctx, data) (nettle_md5_compress((ctx)->state, (data)))
65
66
void
67
md5_update(struct md5_ctx *ctx,
68
     size_t length,
69
     const uint8_t *data)
70
1.31k
{
71
1.42k
  MD_UPDATE(ctx, length, data, COMPRESS, ctx->count++);
72
1.31k
}
73
74
void
75
md5_digest(struct md5_ctx *ctx,
76
     size_t length,
77
     uint8_t *digest)
78
658
{
79
658
  uint64_t bit_count;
80
  
81
658
  assert(length <= MD5_DIGEST_SIZE);
82
83
658
  MD_PAD(ctx, 8, COMPRESS);
84
85
  /* There are 512 = 2^9 bits in one block */
86
658
  bit_count = (ctx->count << 9) | (ctx->index << 3);
87
88
658
  LE_WRITE_UINT64(ctx->block + (MD5_BLOCK_SIZE - 8), bit_count);
89
658
  nettle_md5_compress(ctx->state, ctx->block);
90
91
658
  _nettle_write_le32(length, digest, ctx->state);
92
658
  md5_init(ctx);
93
658
}