Coverage Report

Created: 2023-03-26 08:33

/src/nettle/md5.c
Line
Count
Source (jump to first uncovered line)
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
0
{
52
0
  const uint32_t iv[_MD5_DIGEST_LENGTH] =
53
0
    {
54
0
      0x67452301,
55
0
      0xefcdab89,
56
0
      0x98badcfe,
57
0
      0x10325476,
58
0
    };
59
0
  memcpy(ctx->state, iv, sizeof(ctx->state));
60
0
  ctx->count = 0;
61
0
  ctx->index = 0;
62
0
}
63
64
0
#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
0
{
71
0
  MD_UPDATE(ctx, length, data, COMPRESS, ctx->count++);
72
0
}
73
74
void
75
md5_digest(struct md5_ctx *ctx,
76
     size_t length,
77
     uint8_t *digest)
78
0
{
79
0
  uint64_t bit_count;
80
  
81
0
  assert(length <= MD5_DIGEST_SIZE);
82
83
0
  MD_PAD(ctx, 8, COMPRESS);
84
85
  /* There are 512 = 2^9 bits in one block */
86
0
  bit_count = (ctx->count << 9) | (ctx->index << 3);
87
88
0
  LE_WRITE_UINT64(ctx->block + (MD5_BLOCK_SIZE - 8), bit_count);
89
0
  nettle_md5_compress(ctx->state, ctx->block);
90
91
0
  _nettle_write_le32(length, digest, ctx->state);
92
0
  md5_init(ctx);
93
0
}