Coverage Report

Created: 2023-09-25 06:33

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