3 * The MD5 hash function, described in RFC 1321.
6 /* nettle, low-level cryptographics library
8 * Copyright (C) 2001 Niels Möller
10 * The nettle library is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or (at your
13 * option) any later version.
15 * The nettle library is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with the nettle library; see the file COPYING.LIB. If not, write to
22 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26 /* Based on public domain code hacked by Colin Plumb, Andrew Kuchling, and
39 #include "nettle-write.h"
42 md5_init(struct md5_ctx
*ctx
)
44 const uint32_t iv
[_MD5_DIGEST_LENGTH
] =
51 memcpy(ctx
->state
, iv
, sizeof(ctx
->state
));
52 ctx
->count_low
= ctx
->count_high
= 0;
56 #define COMPRESS(ctx, data) (_nettle_md5_compress((ctx)->state, (data)))
59 md5_update(struct md5_ctx
*ctx
,
63 MD_UPDATE(ctx
, length
, data
, COMPRESS
, MD_INCR(ctx
));
67 md5_digest(struct md5_ctx
*ctx
,
73 assert(length
<= MD5_DIGEST_SIZE
);
75 MD_PAD(ctx
, 8, COMPRESS
);
77 /* There are 512 = 2^9 bits in one block */
78 high
= (ctx
->count_high
<< 9) | (ctx
->count_low
>> 23);
79 low
= (ctx
->count_low
<< 9) | (ctx
->index
<< 3);
81 LE_WRITE_UINT32(ctx
->block
+ (MD5_DATA_SIZE
- 8), low
);
82 LE_WRITE_UINT32(ctx
->block
+ (MD5_DATA_SIZE
- 4), high
);
83 _nettle_md5_compress(ctx
->state
, ctx
->block
);
85 _nettle_write_le32(length
, digest
, ctx
->state
);