History log of /openbsd-current/bin/md5/md5.c
Revision (<<< Hide revision tags) (Show revision tags >>>) Date Author Comments
# 1.98 31-Oct-2023 millert

cksum -c: support checking base64 digests in reverse mode too.

In reverse mode, the entry does not include the digest algorithm
so the specified algorithm (or the default) is used when checking.
OK deraadt@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE OPENBSD_7_1_BASE OPENBSD_7_2_BASE OPENBSD_7_3_BASE OPENBSD_7_4_BASE
# 1.97 19-Oct-2020 millert

Add explicit casts to double to quiet a clang warning.
OK deraddt@


# 1.96 03-Oct-2020 millert

Use ferror() to check for write error, not the fflush() return value.
We can't rely on buffering to catch write errors in fflush().
Based on a diff from Demi M. Obenour, OK kn@


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE
# 1.95 18-May-2019 otto

freezero() is of no use here, the computed digest is not a secret.
ok millert@


Revision tags: OPENBSD_6_5_BASE
# 1.94 25-Jan-2019 millert

I am retiring my old email address; replace it with my OpenBSD one.


Revision tags: OPENBSD_6_4_BASE
# 1.93 07-Sep-2018 cheloha

fgetln(3) -> getline(3); from Lauri Tirkkonen; ok millert@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.92 11-Sep-2017 millert

Use getrusage(2) for the built-in time trial instead of getttimeofday(2).
This makes it possible to measure just the ellapsed user time, which
is what we really care about when benchmarking an algorithm.
OK deraadt@


# 1.91 22-May-2017 deraadt

obvious use for freezero()


# 1.90 26-Apr-2017 millert

In -C mode, process all checksums that match the specified file(s),
not just the first one that matches. OK deraadt@


Revision tags: OPENBSD_6_1_BASE
# 1.89 16-Dec-2016 krw

Move declaration of an 'i' inside the scope of its use. Which in
turn is inside an #ifdef. Thus making the code clearer by eliminating
an #ifdef in the middle of the declarations.

Suggested by millert@


# 1.88 16-Dec-2016 krw

Eliminate some gcc warnings about 'unused variables', mostly by
adding appropriate #ifdef's around declarations.

ok millert@ (with a tweak I will commit separately)


# 1.87 03-Sep-2016 tedu

add sha512/256 support to check that the libc code is right :)


# 1.86 26-Aug-2016 guenther

Pull in <sys/time.h> for gettimeofday()


# 1.85 29-Jul-2016 tedu

all of the update functions take a size_t. correct type and casts.
ok deraadt millert


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.84 09-Dec-2015 mmcc

Remove NULL-check before free(). ok tb@


# 1.83 10-Oct-2015 deraadt

normalize the ordering of tame requests (particularily, "rpath wpath cpath",
which i have put in that order). this is not important, but helps look
for outliers which might be strange. it hints that "ioctl" should be
reassessed in a few places, to see if "tty" is better; that "unix" may
be used in some places where "route" could now work.


# 1.82 09-Oct-2015 deraadt

Change all tame callers to namechange to pledge(2).


# 1.81 04-Oct-2015 deraadt

Repair tame() error check to be == -1


# 1.80 03-Oct-2015 deraadt

right at startup, this can tame "stdio cpath rpath wpath". after getopt
-h has handled write/creating a file, we can drop to tame "stdio rpath"
since md5 will only read files after that.
i believe i involved lteo for this.


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.79 19-Jan-2015 deraadt

cleanup of the SMALL hiding from Mathias Svensson


# 1.78 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.77 13-Sep-2014 doug

Replace all queue *_END macro calls except CIRCLEQ_END with NULL.

CIRCLEQ_* is deprecated and not called in the tree. The other queue types
have *_END macros which were added for symmetry with CIRCLEQ_END. They are
defined as NULL. There's no reason to keep the other *_END macro calls.

ok millert@


Revision tags: OPENBSD_5_6_BASE
# 1.76 19-Jun-2014 millert

Fix memory leak in digest_file() on ferror(). OK tedu@ lteo@


# 1.75 26-Mar-2014 lteo

Ignore the -b option if cksum is called as-is (e.g. "cksum -b /bsd")
as documented on the cksum(1) man page.

"looks right" deraadt@
ok millert@


# 1.74 20-Mar-2014 tedu

purge sum and sysvsum code. ok deraadt jmc lteo millert
a few initial leftovers spotted by naddy


# 1.73 20-Mar-2014 naddy

remove insecure MD4 checksum algorithm; ok tedu@, man page ok jmc@

"A collision attack published in 2007 can find collisions for full
MD4 in less than two hash operations."


# 1.72 07-Mar-2014 lteo

When using the -C option, exit with an exit status of 1 if any of the
files specified on the command line do not exist in the checklist.

ok deraadt@ tedu@


Revision tags: OPENBSD_5_5_BASE
# 1.71 15-Jan-2014 jmc

add -C to the man page, and adjust usage();
ok lteo


# 1.70 15-Jan-2014 lteo

Add support for a -C option. It works on a checklist like -c but only
does the checksum comparison for selected files that are specified on
the command line.

idea discussed with deraadt@ and tedu@
manpage feedback jmc@
feedback/OK deraadt@ millert@


# 1.69 12-Jan-2014 deraadt

Also move case 'c' into the #ifdef for a smaller binary. It will fall into
default, giving a nice failure. I have not removed -c from the usage()
or getopt() because it is too much butchering...


# 1.68 11-Jan-2014 deraadt

use -DSHA2_ONLY to be more clear about what we are butchering
idea from tedu


# 1.67 10-Jan-2014 jmc

catch up to the fact that md5/sha* got merged, and document -c consistently;
some style and cleanup tweaks while here

ok deraadt


# 1.66 10-Jan-2014 lteo

When using a checklist, print MISSING for non-existent files.

Based on an earlier diff by tedu@
Requested by deraadt@
OK deraadt@


# 1.65 10-Jan-2014 tedu

the -c option is really a mode change, incompatible with other options.
(there are some others too, but -c is particularly misleading.) split it
out in synopsis and usage.
ok deraadt


# 1.64 08-Jan-2014 millert

Fix wrapping of usage message for cksum.


# 1.63 08-Jan-2014 millert

err() not errx() when fopen fails.


# 1.62 08-Jan-2014 millert

Remove NHASHES, it is not needed and is incompatible with #ifdef SMALL.
Remove style_hash and style_cksum, adjust the style of the selected
algorithms based on rflag and qflag instead.


# 1.61 08-Jan-2014 millert

Remove pmode, fix hfile open mode, check return value when writing
file name to stdout.


# 1.60 08-Jan-2014 deraadt

Add -DSMALL support to kill a lot of the unncecessary hashes and checksums
that the install media won't need
ok millert, but he used the word butcher!


# 1.59 08-Jan-2014 deraadt

Add support for -h hashfile. This is very useful with with -p, in a pipeline,
to deliver the hash output to a different location.
ok millert


# 1.58 23-Dec-2013 tedu

install a link as sha512. simplify program internals; there are only
two modes. ok deraadt gilles jca


# 1.57 21-Nov-2013 deraadt

add unsigned char casts for specific calls to ctype.h macros.
ok guenther step


Revision tags: OPENBSD_5_4_BASE
# 1.56 15-Apr-2013 millert

SHA-224 is to SHA-256 as SHA-384 is to SHA-512. It was added in a
later revision of FIPS-180. OK miod@ jmc@ guenther@ djm@


# 1.55 30-Mar-2013 lteo

Return an exit code of 1 if the file cannot be opened (e.g. file does
not exist), or if there is an error reading the file stream.

From Patrik Lundin, thanks!

ok deraadt


Revision tags: OPENBSD_5_3_BASE
# 1.54 04-Dec-2012 deraadt

replace sys/param.h with sys/types.h (and sometimes add limits.h if needed)
ok guenther


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.53 05-Jul-2011 tedu

fix memset sizeof, found by jsg. ok krw


Revision tags: OPENBSD_4_9_BASE
# 1.52 27-Oct-2010 millert

Use a 32KB data buffer instead of a 1KB buffer.


# 1.51 25-Oct-2010 millert

Use stdio when reading in the file to be checksummed. This can improve
performance due to the internal buffering stdio does wrt the optimal
read size (st_blksize). OK mikeb@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE OPENBSD_4_8_BASE
# 1.50 06-Sep-2008 djm

adapt to sha2(3) API changes; ok millert@


Revision tags: OPENBSD_4_4_BASE
# 1.49 29-Jul-2008 sobrado

md5(1), sha1(1), rmd160(1), cksum(1) and sum(1) have different options;
while here, some KNF improvements.

based on a diff for compress(1) written by millert@,
fixes from guenther@ and pyr@.

ok millert@, pyr@; (for the manual page tweaks) jmc@


Revision tags: OPENBSD_4_3_BASE
# 1.48 07-Nov-2007 chl

properly exit >0 when encountering bad md5 in cheklist

ok gilles@ millert@

From Tim van der Molen <tbvdm.lists at xs4all.nl>


# 1.47 31-Oct-2007 deraadt

clamp -ttttttttt test counter to 1B, so that it does not wrap negative;
from Nicholas Marriott, ok otto


Revision tags: OPENBSD_4_2_BASE
# 1.46 14-Apr-2007 grunk

if we define a constant for the OPTSTRING, we can as well use it.

ok tedu@


# 1.45 13-Apr-2007 tedu

allow multiple -t to increase the test count. cpus are getting too fast.
ok grunk. with manpage from grunk and jmc


# 1.44 29-Mar-2007 millert

Fix a cust & pasto
Correctly compute amount of base64 padding in checklist mode


# 1.43 29-Mar-2007 millert

Do not print a warning that no valid checksums were found if there
was a single fingerprint for a file that could not be opened.
The warning that the file could not be opened is sufficient.
From espie@


# 1.42 28-Mar-2007 millert

Supress warnings for the first getopt() pass. Otherwise,
any "unknown option" messages get printed twice.


# 1.41 27-Mar-2007 millert

Add base64 support to cksum and friends. Output encoding can
be set globally or on a per-algorithm basis when using multiple
hash functions. OK espie@ simon@ deraadt@; man help from jmc@


# 1.40 20-Mar-2007 thib

augment the -q option, now if specifed with -c (checklist)
it will only print out the failed cases.

OK millert@


Revision tags: OPENBSD_4_1_BASE
# 1.39 09-Jan-2007 deraadt

align usage message correctly


# 1.38 17-Nov-2006 jmc

simplify synopsis and usage(); ok millert


# 1.37 16-Nov-2006 millert

Add -r and -q options similar to what is supported by FreeBSD's md5
OK deraadt@ ckuethe@


# 1.36 10-Nov-2006 tom

Accept upper and lower case letters in a checksum, since this doesn't
alter the value of the digest. Nor, for that matter, does case affect
the name of the algorithm used.

Partly from Matthew Clarke, clamat (at) telus (dot) net.

ok djm@ millert@


Revision tags: OPENBSD_4_0_BASE
# 1.35 15-Mar-2006 dhill

Fix signed/unsigned comparisons by using size_t
Sprinkle some const

ok ray@ jaredy@


Revision tags: OPENBSD_3_9_BASE
# 1.34 20-Dec-2005 millert

Make "cksum -c" grok checklists generated with cksum, sum and sysvsum.
OK deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.33 17-Aug-2005 mpf

Exit unsuccessfully when comparing against a checklist file fails.
OK markus@ millert@


Revision tags: OPENBSD_3_7_BASE
# 1.32 29-Dec-2004 millert

Add support for multiple using algorithms. OK'd by several people
a while ago so get this out of my tree...


# 1.31 10-Dec-2004 millert

GNU md5sum uses tab not space to separate filename and hash. Fixes
parsing of GNU-style checklist files for files with spaces in them.
From Emil Mikulic; closes PR 4027


Revision tags: OPENBSD_3_6_BASE
# 1.30 04-May-2004 millert

sync usage w/ man page


# 1.29 02-May-2004 millert

Add support for cksum (3 varieties), md4, sha256, sha384, and sha512.
Consequently, cksum moves becomes a link to md5 and moves to /bin.
The cksum man page could use some polishing.


# 1.28 28-Apr-2004 millert

Use the FOO_DIGEST_LENGTH macros


Revision tags: OPENBSD_3_5_BASE
# 1.27 03-Mar-2004 millert

Extend "md5 -c" to parse the output of GNU md5sum.
Adapted from patches by Lawrence Teo and tedu@. OK tedu@


Revision tags: OPENBSD_3_4_BASE
# 1.26 21-Jul-2003 millert

Fix typo: unsigned -> unsigned char; from Thorsten Glaser


# 1.25 25-Jun-2003 deraadt

fix lots of proto issues


# 1.24 17-Jun-2003 millert

Sync with share/misc/license.template and add missing DARPA credit
where applicable.


# 1.23 11-Jun-2003 deraadt

ansification, art ok


# 1.22 03-Jun-2003 millert

Use an ISC-tyle license for all my code; it is simpler and more permissive.


# 1.21 28-Apr-2003 deraadt

return from main


# 1.20 23-Apr-2003 millert

strchr() should be strrchr(); zyrnix


Revision tags: OPENBSD_3_3_BASE
# 1.19 23-Mar-2003 millert

Do not closed stdin; Andrey Matveev


# 1.18 13-Mar-2003 millert

Cast 3 to size_t to shut up stupid gcc on 64bit platforms.


# 1.17 12-Mar-2003 millert

Add -c option similar to GNU md5sum.
Original patch from zyrnix but significantly modified by me.
deraadt@ OK


# 1.16 14-Jan-2003 millert

Report read() errors; noticed and OK by fgs@


Revision tags: OPENBSD_3_1_BASE OPENBSD_3_2_BASE
# 1.15 20-Jan-2002 camield

Swap TEST_BLOCK_LEN and TEST_BLOCK_COUNT in benchmark printf.

ok millert@


Revision tags: OPENBSD_3_0_BASE
# 1.14 06-Sep-2001 mpech

o) __progname aria;

millert@ ok.


# 1.13 03-Jun-2001 millert

Nope, that's not right either. Fix check for mutually exclusive args.


# 1.12 03-Jun-2001 espie

Obvious thinko: command line is wrong if it's too short, not too long.


# 1.11 02-Jun-2001 millert

Fix some typos and add missing test for string of 1 million a's.


# 1.10 02-Jun-2001 millert

New md5/sha1/rmd160 driver with a BSD copyright. Improvements:
o options that should be mutually exclusive are
o time trial works reasonably when it finishes in < 1 sec
o uses a function table instead of a bunch of globals


Revision tags: OPENBSD_2_6_BASE OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.9 07-Oct-1999 espie

One single malloc, with a union to get the largest needed size,
should be enough.


Revision tags: OPENBSD_2_3_BASE OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.8 02-Jan-1998 deraadt

use getopt; mph@pobox.com


Revision tags: OPENBSD_2_2_BASE
# 1.7 01-Aug-1997 deraadt

warn not warnx


# 1.6 17-Jul-1997 millert

Adds rmd160 functionality.


# 1.5 12-Jul-1997 millert

md5(1) is now capable of doing md4, md5 and sha1 digests. Currently
only md5 and sha1 are used.


# 1.4 20-Jun-1997 flipk

boost TEST_BLOCK_LEN and TEST_BLOCK_COUNT a bit. this makes '-t' more
useful on fast boxes.
also fflush(stdout) after printing the first test message so
the user knows its actually doing something


Revision tags: OPENBSD_2_1_BASE
# 1.3 24-Nov-1996 niklas

64-bit cleanup + pedantic -W flags


# 1.2 12-Nov-1996 niklas

-Wall happiness


Revision tags: OPENBSD_2_0_BASE
# 1.1 11-Jun-1996 deraadt

move to bin/md5 for `greater accessibility'


# 1.97 19-Oct-2020 millert

Add explicit casts to double to quiet a clang warning.
OK deraddt@


# 1.96 03-Oct-2020 millert

Use ferror() to check for write error, not the fflush() return value.
We can't rely on buffering to catch write errors in fflush().
Based on a diff from Demi M. Obenour, OK kn@


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE
# 1.95 18-May-2019 otto

freezero() is of no use here, the computed digest is not a secret.
ok millert@


Revision tags: OPENBSD_6_5_BASE
# 1.94 25-Jan-2019 millert

I am retiring my old email address; replace it with my OpenBSD one.


Revision tags: OPENBSD_6_4_BASE
# 1.93 07-Sep-2018 cheloha

fgetln(3) -> getline(3); from Lauri Tirkkonen; ok millert@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.92 11-Sep-2017 millert

Use getrusage(2) for the built-in time trial instead of getttimeofday(2).
This makes it possible to measure just the ellapsed user time, which
is what we really care about when benchmarking an algorithm.
OK deraadt@


# 1.91 22-May-2017 deraadt

obvious use for freezero()


# 1.90 26-Apr-2017 millert

In -C mode, process all checksums that match the specified file(s),
not just the first one that matches. OK deraadt@


Revision tags: OPENBSD_6_1_BASE
# 1.89 16-Dec-2016 krw

Move declaration of an 'i' inside the scope of its use. Which in
turn is inside an #ifdef. Thus making the code clearer by eliminating
an #ifdef in the middle of the declarations.

Suggested by millert@


# 1.88 16-Dec-2016 krw

Eliminate some gcc warnings about 'unused variables', mostly by
adding appropriate #ifdef's around declarations.

ok millert@ (with a tweak I will commit separately)


# 1.87 03-Sep-2016 tedu

add sha512/256 support to check that the libc code is right :)


# 1.86 26-Aug-2016 guenther

Pull in <sys/time.h> for gettimeofday()


# 1.85 29-Jul-2016 tedu

all of the update functions take a size_t. correct type and casts.
ok deraadt millert


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.84 09-Dec-2015 mmcc

Remove NULL-check before free(). ok tb@


# 1.83 10-Oct-2015 deraadt

normalize the ordering of tame requests (particularily, "rpath wpath cpath",
which i have put in that order). this is not important, but helps look
for outliers which might be strange. it hints that "ioctl" should be
reassessed in a few places, to see if "tty" is better; that "unix" may
be used in some places where "route" could now work.


# 1.82 09-Oct-2015 deraadt

Change all tame callers to namechange to pledge(2).


# 1.81 04-Oct-2015 deraadt

Repair tame() error check to be == -1


# 1.80 03-Oct-2015 deraadt

right at startup, this can tame "stdio cpath rpath wpath". after getopt
-h has handled write/creating a file, we can drop to tame "stdio rpath"
since md5 will only read files after that.
i believe i involved lteo for this.


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.79 19-Jan-2015 deraadt

cleanup of the SMALL hiding from Mathias Svensson


# 1.78 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.77 13-Sep-2014 doug

Replace all queue *_END macro calls except CIRCLEQ_END with NULL.

CIRCLEQ_* is deprecated and not called in the tree. The other queue types
have *_END macros which were added for symmetry with CIRCLEQ_END. They are
defined as NULL. There's no reason to keep the other *_END macro calls.

ok millert@


Revision tags: OPENBSD_5_6_BASE
# 1.76 19-Jun-2014 millert

Fix memory leak in digest_file() on ferror(). OK tedu@ lteo@


# 1.75 26-Mar-2014 lteo

Ignore the -b option if cksum is called as-is (e.g. "cksum -b /bsd")
as documented on the cksum(1) man page.

"looks right" deraadt@
ok millert@


# 1.74 20-Mar-2014 tedu

purge sum and sysvsum code. ok deraadt jmc lteo millert
a few initial leftovers spotted by naddy


# 1.73 20-Mar-2014 naddy

remove insecure MD4 checksum algorithm; ok tedu@, man page ok jmc@

"A collision attack published in 2007 can find collisions for full
MD4 in less than two hash operations."


# 1.72 07-Mar-2014 lteo

When using the -C option, exit with an exit status of 1 if any of the
files specified on the command line do not exist in the checklist.

ok deraadt@ tedu@


Revision tags: OPENBSD_5_5_BASE
# 1.71 15-Jan-2014 jmc

add -C to the man page, and adjust usage();
ok lteo


# 1.70 15-Jan-2014 lteo

Add support for a -C option. It works on a checklist like -c but only
does the checksum comparison for selected files that are specified on
the command line.

idea discussed with deraadt@ and tedu@
manpage feedback jmc@
feedback/OK deraadt@ millert@


# 1.69 12-Jan-2014 deraadt

Also move case 'c' into the #ifdef for a smaller binary. It will fall into
default, giving a nice failure. I have not removed -c from the usage()
or getopt() because it is too much butchering...


# 1.68 11-Jan-2014 deraadt

use -DSHA2_ONLY to be more clear about what we are butchering
idea from tedu


# 1.67 10-Jan-2014 jmc

catch up to the fact that md5/sha* got merged, and document -c consistently;
some style and cleanup tweaks while here

ok deraadt


# 1.66 10-Jan-2014 lteo

When using a checklist, print MISSING for non-existent files.

Based on an earlier diff by tedu@
Requested by deraadt@
OK deraadt@


# 1.65 10-Jan-2014 tedu

the -c option is really a mode change, incompatible with other options.
(there are some others too, but -c is particularly misleading.) split it
out in synopsis and usage.
ok deraadt


# 1.64 08-Jan-2014 millert

Fix wrapping of usage message for cksum.


# 1.63 08-Jan-2014 millert

err() not errx() when fopen fails.


# 1.62 08-Jan-2014 millert

Remove NHASHES, it is not needed and is incompatible with #ifdef SMALL.
Remove style_hash and style_cksum, adjust the style of the selected
algorithms based on rflag and qflag instead.


# 1.61 08-Jan-2014 millert

Remove pmode, fix hfile open mode, check return value when writing
file name to stdout.


# 1.60 08-Jan-2014 deraadt

Add -DSMALL support to kill a lot of the unncecessary hashes and checksums
that the install media won't need
ok millert, but he used the word butcher!


# 1.59 08-Jan-2014 deraadt

Add support for -h hashfile. This is very useful with with -p, in a pipeline,
to deliver the hash output to a different location.
ok millert


# 1.58 23-Dec-2013 tedu

install a link as sha512. simplify program internals; there are only
two modes. ok deraadt gilles jca


# 1.57 21-Nov-2013 deraadt

add unsigned char casts for specific calls to ctype.h macros.
ok guenther step


Revision tags: OPENBSD_5_4_BASE
# 1.56 15-Apr-2013 millert

SHA-224 is to SHA-256 as SHA-384 is to SHA-512. It was added in a
later revision of FIPS-180. OK miod@ jmc@ guenther@ djm@


# 1.55 30-Mar-2013 lteo

Return an exit code of 1 if the file cannot be opened (e.g. file does
not exist), or if there is an error reading the file stream.

From Patrik Lundin, thanks!

ok deraadt


Revision tags: OPENBSD_5_3_BASE
# 1.54 04-Dec-2012 deraadt

replace sys/param.h with sys/types.h (and sometimes add limits.h if needed)
ok guenther


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.53 05-Jul-2011 tedu

fix memset sizeof, found by jsg. ok krw


Revision tags: OPENBSD_4_9_BASE
# 1.52 27-Oct-2010 millert

Use a 32KB data buffer instead of a 1KB buffer.


# 1.51 25-Oct-2010 millert

Use stdio when reading in the file to be checksummed. This can improve
performance due to the internal buffering stdio does wrt the optimal
read size (st_blksize). OK mikeb@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE OPENBSD_4_8_BASE
# 1.50 06-Sep-2008 djm

adapt to sha2(3) API changes; ok millert@


Revision tags: OPENBSD_4_4_BASE
# 1.49 29-Jul-2008 sobrado

md5(1), sha1(1), rmd160(1), cksum(1) and sum(1) have different options;
while here, some KNF improvements.

based on a diff for compress(1) written by millert@,
fixes from guenther@ and pyr@.

ok millert@, pyr@; (for the manual page tweaks) jmc@


Revision tags: OPENBSD_4_3_BASE
# 1.48 07-Nov-2007 chl

properly exit >0 when encountering bad md5 in cheklist

ok gilles@ millert@

From Tim van der Molen <tbvdm.lists at xs4all.nl>


# 1.47 31-Oct-2007 deraadt

clamp -ttttttttt test counter to 1B, so that it does not wrap negative;
from Nicholas Marriott, ok otto


Revision tags: OPENBSD_4_2_BASE
# 1.46 14-Apr-2007 grunk

if we define a constant for the OPTSTRING, we can as well use it.

ok tedu@


# 1.45 13-Apr-2007 tedu

allow multiple -t to increase the test count. cpus are getting too fast.
ok grunk. with manpage from grunk and jmc


# 1.44 29-Mar-2007 millert

Fix a cust & pasto
Correctly compute amount of base64 padding in checklist mode


# 1.43 29-Mar-2007 millert

Do not print a warning that no valid checksums were found if there
was a single fingerprint for a file that could not be opened.
The warning that the file could not be opened is sufficient.
From espie@


# 1.42 28-Mar-2007 millert

Supress warnings for the first getopt() pass. Otherwise,
any "unknown option" messages get printed twice.


# 1.41 27-Mar-2007 millert

Add base64 support to cksum and friends. Output encoding can
be set globally or on a per-algorithm basis when using multiple
hash functions. OK espie@ simon@ deraadt@; man help from jmc@


# 1.40 20-Mar-2007 thib

augment the -q option, now if specifed with -c (checklist)
it will only print out the failed cases.

OK millert@


Revision tags: OPENBSD_4_1_BASE
# 1.39 09-Jan-2007 deraadt

align usage message correctly


# 1.38 17-Nov-2006 jmc

simplify synopsis and usage(); ok millert


# 1.37 16-Nov-2006 millert

Add -r and -q options similar to what is supported by FreeBSD's md5
OK deraadt@ ckuethe@


# 1.36 10-Nov-2006 tom

Accept upper and lower case letters in a checksum, since this doesn't
alter the value of the digest. Nor, for that matter, does case affect
the name of the algorithm used.

Partly from Matthew Clarke, clamat (at) telus (dot) net.

ok djm@ millert@


Revision tags: OPENBSD_4_0_BASE
# 1.35 15-Mar-2006 dhill

Fix signed/unsigned comparisons by using size_t
Sprinkle some const

ok ray@ jaredy@


Revision tags: OPENBSD_3_9_BASE
# 1.34 20-Dec-2005 millert

Make "cksum -c" grok checklists generated with cksum, sum and sysvsum.
OK deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.33 17-Aug-2005 mpf

Exit unsuccessfully when comparing against a checklist file fails.
OK markus@ millert@


Revision tags: OPENBSD_3_7_BASE
# 1.32 29-Dec-2004 millert

Add support for multiple using algorithms. OK'd by several people
a while ago so get this out of my tree...


# 1.31 10-Dec-2004 millert

GNU md5sum uses tab not space to separate filename and hash. Fixes
parsing of GNU-style checklist files for files with spaces in them.
From Emil Mikulic; closes PR 4027


Revision tags: OPENBSD_3_6_BASE
# 1.30 04-May-2004 millert

sync usage w/ man page


# 1.29 02-May-2004 millert

Add support for cksum (3 varieties), md4, sha256, sha384, and sha512.
Consequently, cksum moves becomes a link to md5 and moves to /bin.
The cksum man page could use some polishing.


# 1.28 28-Apr-2004 millert

Use the FOO_DIGEST_LENGTH macros


Revision tags: OPENBSD_3_5_BASE
# 1.27 03-Mar-2004 millert

Extend "md5 -c" to parse the output of GNU md5sum.
Adapted from patches by Lawrence Teo and tedu@. OK tedu@


Revision tags: OPENBSD_3_4_BASE
# 1.26 21-Jul-2003 millert

Fix typo: unsigned -> unsigned char; from Thorsten Glaser


# 1.25 25-Jun-2003 deraadt

fix lots of proto issues


# 1.24 17-Jun-2003 millert

Sync with share/misc/license.template and add missing DARPA credit
where applicable.


# 1.23 11-Jun-2003 deraadt

ansification, art ok


# 1.22 03-Jun-2003 millert

Use an ISC-tyle license for all my code; it is simpler and more permissive.


# 1.21 28-Apr-2003 deraadt

return from main


# 1.20 23-Apr-2003 millert

strchr() should be strrchr(); zyrnix


Revision tags: OPENBSD_3_3_BASE
# 1.19 23-Mar-2003 millert

Do not closed stdin; Andrey Matveev


# 1.18 13-Mar-2003 millert

Cast 3 to size_t to shut up stupid gcc on 64bit platforms.


# 1.17 12-Mar-2003 millert

Add -c option similar to GNU md5sum.
Original patch from zyrnix but significantly modified by me.
deraadt@ OK


# 1.16 14-Jan-2003 millert

Report read() errors; noticed and OK by fgs@


Revision tags: OPENBSD_3_1_BASE OPENBSD_3_2_BASE
# 1.15 20-Jan-2002 camield

Swap TEST_BLOCK_LEN and TEST_BLOCK_COUNT in benchmark printf.

ok millert@


Revision tags: OPENBSD_3_0_BASE
# 1.14 06-Sep-2001 mpech

o) __progname aria;

millert@ ok.


# 1.13 03-Jun-2001 millert

Nope, that's not right either. Fix check for mutually exclusive args.


# 1.12 03-Jun-2001 espie

Obvious thinko: command line is wrong if it's too short, not too long.


# 1.11 02-Jun-2001 millert

Fix some typos and add missing test for string of 1 million a's.


# 1.10 02-Jun-2001 millert

New md5/sha1/rmd160 driver with a BSD copyright. Improvements:
o options that should be mutually exclusive are
o time trial works reasonably when it finishes in < 1 sec
o uses a function table instead of a bunch of globals


Revision tags: OPENBSD_2_6_BASE OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.9 07-Oct-1999 espie

One single malloc, with a union to get the largest needed size,
should be enough.


Revision tags: OPENBSD_2_3_BASE OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.8 02-Jan-1998 deraadt

use getopt; mph@pobox.com


Revision tags: OPENBSD_2_2_BASE
# 1.7 01-Aug-1997 deraadt

warn not warnx


# 1.6 17-Jul-1997 millert

Adds rmd160 functionality.


# 1.5 12-Jul-1997 millert

md5(1) is now capable of doing md4, md5 and sha1 digests. Currently
only md5 and sha1 are used.


# 1.4 20-Jun-1997 flipk

boost TEST_BLOCK_LEN and TEST_BLOCK_COUNT a bit. this makes '-t' more
useful on fast boxes.
also fflush(stdout) after printing the first test message so
the user knows its actually doing something


Revision tags: OPENBSD_2_1_BASE
# 1.3 24-Nov-1996 niklas

64-bit cleanup + pedantic -W flags


# 1.2 12-Nov-1996 niklas

-Wall happiness


Revision tags: OPENBSD_2_0_BASE
# 1.1 11-Jun-1996 deraadt

move to bin/md5 for `greater accessibility'


# 1.96 03-Oct-2020 millert

Use ferror() to check for write error, not the fflush() return value.
We can't rely on buffering to catch write errors in fflush().
Based on a diff from Demi M. Obenour, OK kn@


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE
# 1.95 18-May-2019 otto

freezero() is of no use here, the computed digest is not a secret.
ok millert@


Revision tags: OPENBSD_6_5_BASE
# 1.94 25-Jan-2019 millert

I am retiring my old email address; replace it with my OpenBSD one.


Revision tags: OPENBSD_6_4_BASE
# 1.93 07-Sep-2018 cheloha

fgetln(3) -> getline(3); from Lauri Tirkkonen; ok millert@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.92 11-Sep-2017 millert

Use getrusage(2) for the built-in time trial instead of getttimeofday(2).
This makes it possible to measure just the ellapsed user time, which
is what we really care about when benchmarking an algorithm.
OK deraadt@


# 1.91 22-May-2017 deraadt

obvious use for freezero()


# 1.90 26-Apr-2017 millert

In -C mode, process all checksums that match the specified file(s),
not just the first one that matches. OK deraadt@


Revision tags: OPENBSD_6_1_BASE
# 1.89 16-Dec-2016 krw

Move declaration of an 'i' inside the scope of its use. Which in
turn is inside an #ifdef. Thus making the code clearer by eliminating
an #ifdef in the middle of the declarations.

Suggested by millert@


# 1.88 16-Dec-2016 krw

Eliminate some gcc warnings about 'unused variables', mostly by
adding appropriate #ifdef's around declarations.

ok millert@ (with a tweak I will commit separately)


# 1.87 03-Sep-2016 tedu

add sha512/256 support to check that the libc code is right :)


# 1.86 26-Aug-2016 guenther

Pull in <sys/time.h> for gettimeofday()


# 1.85 29-Jul-2016 tedu

all of the update functions take a size_t. correct type and casts.
ok deraadt millert


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.84 09-Dec-2015 mmcc

Remove NULL-check before free(). ok tb@


# 1.83 10-Oct-2015 deraadt

normalize the ordering of tame requests (particularily, "rpath wpath cpath",
which i have put in that order). this is not important, but helps look
for outliers which might be strange. it hints that "ioctl" should be
reassessed in a few places, to see if "tty" is better; that "unix" may
be used in some places where "route" could now work.


# 1.82 09-Oct-2015 deraadt

Change all tame callers to namechange to pledge(2).


# 1.81 04-Oct-2015 deraadt

Repair tame() error check to be == -1


# 1.80 03-Oct-2015 deraadt

right at startup, this can tame "stdio cpath rpath wpath". after getopt
-h has handled write/creating a file, we can drop to tame "stdio rpath"
since md5 will only read files after that.
i believe i involved lteo for this.


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.79 19-Jan-2015 deraadt

cleanup of the SMALL hiding from Mathias Svensson


# 1.78 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.77 13-Sep-2014 doug

Replace all queue *_END macro calls except CIRCLEQ_END with NULL.

CIRCLEQ_* is deprecated and not called in the tree. The other queue types
have *_END macros which were added for symmetry with CIRCLEQ_END. They are
defined as NULL. There's no reason to keep the other *_END macro calls.

ok millert@


Revision tags: OPENBSD_5_6_BASE
# 1.76 19-Jun-2014 millert

Fix memory leak in digest_file() on ferror(). OK tedu@ lteo@


# 1.75 26-Mar-2014 lteo

Ignore the -b option if cksum is called as-is (e.g. "cksum -b /bsd")
as documented on the cksum(1) man page.

"looks right" deraadt@
ok millert@


# 1.74 20-Mar-2014 tedu

purge sum and sysvsum code. ok deraadt jmc lteo millert
a few initial leftovers spotted by naddy


# 1.73 20-Mar-2014 naddy

remove insecure MD4 checksum algorithm; ok tedu@, man page ok jmc@

"A collision attack published in 2007 can find collisions for full
MD4 in less than two hash operations."


# 1.72 07-Mar-2014 lteo

When using the -C option, exit with an exit status of 1 if any of the
files specified on the command line do not exist in the checklist.

ok deraadt@ tedu@


Revision tags: OPENBSD_5_5_BASE
# 1.71 15-Jan-2014 jmc

add -C to the man page, and adjust usage();
ok lteo


# 1.70 15-Jan-2014 lteo

Add support for a -C option. It works on a checklist like -c but only
does the checksum comparison for selected files that are specified on
the command line.

idea discussed with deraadt@ and tedu@
manpage feedback jmc@
feedback/OK deraadt@ millert@


# 1.69 12-Jan-2014 deraadt

Also move case 'c' into the #ifdef for a smaller binary. It will fall into
default, giving a nice failure. I have not removed -c from the usage()
or getopt() because it is too much butchering...


# 1.68 11-Jan-2014 deraadt

use -DSHA2_ONLY to be more clear about what we are butchering
idea from tedu


# 1.67 10-Jan-2014 jmc

catch up to the fact that md5/sha* got merged, and document -c consistently;
some style and cleanup tweaks while here

ok deraadt


# 1.66 10-Jan-2014 lteo

When using a checklist, print MISSING for non-existent files.

Based on an earlier diff by tedu@
Requested by deraadt@
OK deraadt@


# 1.65 10-Jan-2014 tedu

the -c option is really a mode change, incompatible with other options.
(there are some others too, but -c is particularly misleading.) split it
out in synopsis and usage.
ok deraadt


# 1.64 08-Jan-2014 millert

Fix wrapping of usage message for cksum.


# 1.63 08-Jan-2014 millert

err() not errx() when fopen fails.


# 1.62 08-Jan-2014 millert

Remove NHASHES, it is not needed and is incompatible with #ifdef SMALL.
Remove style_hash and style_cksum, adjust the style of the selected
algorithms based on rflag and qflag instead.


# 1.61 08-Jan-2014 millert

Remove pmode, fix hfile open mode, check return value when writing
file name to stdout.


# 1.60 08-Jan-2014 deraadt

Add -DSMALL support to kill a lot of the unncecessary hashes and checksums
that the install media won't need
ok millert, but he used the word butcher!


# 1.59 08-Jan-2014 deraadt

Add support for -h hashfile. This is very useful with with -p, in a pipeline,
to deliver the hash output to a different location.
ok millert


# 1.58 23-Dec-2013 tedu

install a link as sha512. simplify program internals; there are only
two modes. ok deraadt gilles jca


# 1.57 21-Nov-2013 deraadt

add unsigned char casts for specific calls to ctype.h macros.
ok guenther step


Revision tags: OPENBSD_5_4_BASE
# 1.56 15-Apr-2013 millert

SHA-224 is to SHA-256 as SHA-384 is to SHA-512. It was added in a
later revision of FIPS-180. OK miod@ jmc@ guenther@ djm@


# 1.55 30-Mar-2013 lteo

Return an exit code of 1 if the file cannot be opened (e.g. file does
not exist), or if there is an error reading the file stream.

From Patrik Lundin, thanks!

ok deraadt


Revision tags: OPENBSD_5_3_BASE
# 1.54 04-Dec-2012 deraadt

replace sys/param.h with sys/types.h (and sometimes add limits.h if needed)
ok guenther


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.53 05-Jul-2011 tedu

fix memset sizeof, found by jsg. ok krw


Revision tags: OPENBSD_4_9_BASE
# 1.52 27-Oct-2010 millert

Use a 32KB data buffer instead of a 1KB buffer.


# 1.51 25-Oct-2010 millert

Use stdio when reading in the file to be checksummed. This can improve
performance due to the internal buffering stdio does wrt the optimal
read size (st_blksize). OK mikeb@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE OPENBSD_4_8_BASE
# 1.50 06-Sep-2008 djm

adapt to sha2(3) API changes; ok millert@


Revision tags: OPENBSD_4_4_BASE
# 1.49 29-Jul-2008 sobrado

md5(1), sha1(1), rmd160(1), cksum(1) and sum(1) have different options;
while here, some KNF improvements.

based on a diff for compress(1) written by millert@,
fixes from guenther@ and pyr@.

ok millert@, pyr@; (for the manual page tweaks) jmc@


Revision tags: OPENBSD_4_3_BASE
# 1.48 07-Nov-2007 chl

properly exit >0 when encountering bad md5 in cheklist

ok gilles@ millert@

From Tim van der Molen <tbvdm.lists at xs4all.nl>


# 1.47 31-Oct-2007 deraadt

clamp -ttttttttt test counter to 1B, so that it does not wrap negative;
from Nicholas Marriott, ok otto


Revision tags: OPENBSD_4_2_BASE
# 1.46 14-Apr-2007 grunk

if we define a constant for the OPTSTRING, we can as well use it.

ok tedu@


# 1.45 13-Apr-2007 tedu

allow multiple -t to increase the test count. cpus are getting too fast.
ok grunk. with manpage from grunk and jmc


# 1.44 29-Mar-2007 millert

Fix a cust & pasto
Correctly compute amount of base64 padding in checklist mode


# 1.43 29-Mar-2007 millert

Do not print a warning that no valid checksums were found if there
was a single fingerprint for a file that could not be opened.
The warning that the file could not be opened is sufficient.
From espie@


# 1.42 28-Mar-2007 millert

Supress warnings for the first getopt() pass. Otherwise,
any "unknown option" messages get printed twice.


# 1.41 27-Mar-2007 millert

Add base64 support to cksum and friends. Output encoding can
be set globally or on a per-algorithm basis when using multiple
hash functions. OK espie@ simon@ deraadt@; man help from jmc@


# 1.40 20-Mar-2007 thib

augment the -q option, now if specifed with -c (checklist)
it will only print out the failed cases.

OK millert@


Revision tags: OPENBSD_4_1_BASE
# 1.39 09-Jan-2007 deraadt

align usage message correctly


# 1.38 17-Nov-2006 jmc

simplify synopsis and usage(); ok millert


# 1.37 16-Nov-2006 millert

Add -r and -q options similar to what is supported by FreeBSD's md5
OK deraadt@ ckuethe@


# 1.36 10-Nov-2006 tom

Accept upper and lower case letters in a checksum, since this doesn't
alter the value of the digest. Nor, for that matter, does case affect
the name of the algorithm used.

Partly from Matthew Clarke, clamat (at) telus (dot) net.

ok djm@ millert@


Revision tags: OPENBSD_4_0_BASE
# 1.35 15-Mar-2006 dhill

Fix signed/unsigned comparisons by using size_t
Sprinkle some const

ok ray@ jaredy@


Revision tags: OPENBSD_3_9_BASE
# 1.34 20-Dec-2005 millert

Make "cksum -c" grok checklists generated with cksum, sum and sysvsum.
OK deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.33 17-Aug-2005 mpf

Exit unsuccessfully when comparing against a checklist file fails.
OK markus@ millert@


Revision tags: OPENBSD_3_7_BASE
# 1.32 29-Dec-2004 millert

Add support for multiple using algorithms. OK'd by several people
a while ago so get this out of my tree...


# 1.31 10-Dec-2004 millert

GNU md5sum uses tab not space to separate filename and hash. Fixes
parsing of GNU-style checklist files for files with spaces in them.
From Emil Mikulic; closes PR 4027


Revision tags: OPENBSD_3_6_BASE
# 1.30 04-May-2004 millert

sync usage w/ man page


# 1.29 02-May-2004 millert

Add support for cksum (3 varieties), md4, sha256, sha384, and sha512.
Consequently, cksum moves becomes a link to md5 and moves to /bin.
The cksum man page could use some polishing.


# 1.28 28-Apr-2004 millert

Use the FOO_DIGEST_LENGTH macros


Revision tags: OPENBSD_3_5_BASE
# 1.27 03-Mar-2004 millert

Extend "md5 -c" to parse the output of GNU md5sum.
Adapted from patches by Lawrence Teo and tedu@. OK tedu@


Revision tags: OPENBSD_3_4_BASE
# 1.26 21-Jul-2003 millert

Fix typo: unsigned -> unsigned char; from Thorsten Glaser


# 1.25 25-Jun-2003 deraadt

fix lots of proto issues


# 1.24 17-Jun-2003 millert

Sync with share/misc/license.template and add missing DARPA credit
where applicable.


# 1.23 11-Jun-2003 deraadt

ansification, art ok


# 1.22 03-Jun-2003 millert

Use an ISC-tyle license for all my code; it is simpler and more permissive.


# 1.21 28-Apr-2003 deraadt

return from main


# 1.20 23-Apr-2003 millert

strchr() should be strrchr(); zyrnix


Revision tags: OPENBSD_3_3_BASE
# 1.19 23-Mar-2003 millert

Do not closed stdin; Andrey Matveev


# 1.18 13-Mar-2003 millert

Cast 3 to size_t to shut up stupid gcc on 64bit platforms.


# 1.17 12-Mar-2003 millert

Add -c option similar to GNU md5sum.
Original patch from zyrnix but significantly modified by me.
deraadt@ OK


# 1.16 14-Jan-2003 millert

Report read() errors; noticed and OK by fgs@


Revision tags: OPENBSD_3_1_BASE OPENBSD_3_2_BASE
# 1.15 20-Jan-2002 camield

Swap TEST_BLOCK_LEN and TEST_BLOCK_COUNT in benchmark printf.

ok millert@


Revision tags: OPENBSD_3_0_BASE
# 1.14 06-Sep-2001 mpech

o) __progname aria;

millert@ ok.


# 1.13 03-Jun-2001 millert

Nope, that's not right either. Fix check for mutually exclusive args.


# 1.12 03-Jun-2001 espie

Obvious thinko: command line is wrong if it's too short, not too long.


# 1.11 02-Jun-2001 millert

Fix some typos and add missing test for string of 1 million a's.


# 1.10 02-Jun-2001 millert

New md5/sha1/rmd160 driver with a BSD copyright. Improvements:
o options that should be mutually exclusive are
o time trial works reasonably when it finishes in < 1 sec
o uses a function table instead of a bunch of globals


Revision tags: OPENBSD_2_6_BASE OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.9 07-Oct-1999 espie

One single malloc, with a union to get the largest needed size,
should be enough.


Revision tags: OPENBSD_2_3_BASE OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.8 02-Jan-1998 deraadt

use getopt; mph@pobox.com


Revision tags: OPENBSD_2_2_BASE
# 1.7 01-Aug-1997 deraadt

warn not warnx


# 1.6 17-Jul-1997 millert

Adds rmd160 functionality.


# 1.5 12-Jul-1997 millert

md5(1) is now capable of doing md4, md5 and sha1 digests. Currently
only md5 and sha1 are used.


# 1.4 20-Jun-1997 flipk

boost TEST_BLOCK_LEN and TEST_BLOCK_COUNT a bit. this makes '-t' more
useful on fast boxes.
also fflush(stdout) after printing the first test message so
the user knows its actually doing something


Revision tags: OPENBSD_2_1_BASE
# 1.3 24-Nov-1996 niklas

64-bit cleanup + pedantic -W flags


# 1.2 12-Nov-1996 niklas

-Wall happiness


Revision tags: OPENBSD_2_0_BASE
# 1.1 11-Jun-1996 deraadt

move to bin/md5 for `greater accessibility'


# 1.95 18-May-2019 otto

freezero() is of no use here, the computed digest is not a secret.
ok millert@


Revision tags: OPENBSD_6_5_BASE
# 1.94 25-Jan-2019 millert

I am retiring my old email address; replace it with my OpenBSD one.


Revision tags: OPENBSD_6_4_BASE
# 1.93 07-Sep-2018 cheloha

fgetln(3) -> getline(3); from Lauri Tirkkonen; ok millert@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.92 11-Sep-2017 millert

Use getrusage(2) for the built-in time trial instead of getttimeofday(2).
This makes it possible to measure just the ellapsed user time, which
is what we really care about when benchmarking an algorithm.
OK deraadt@


# 1.91 22-May-2017 deraadt

obvious use for freezero()


# 1.90 26-Apr-2017 millert

In -C mode, process all checksums that match the specified file(s),
not just the first one that matches. OK deraadt@


Revision tags: OPENBSD_6_1_BASE
# 1.89 16-Dec-2016 krw

Move declaration of an 'i' inside the scope of its use. Which in
turn is inside an #ifdef. Thus making the code clearer by eliminating
an #ifdef in the middle of the declarations.

Suggested by millert@


# 1.88 16-Dec-2016 krw

Eliminate some gcc warnings about 'unused variables', mostly by
adding appropriate #ifdef's around declarations.

ok millert@ (with a tweak I will commit separately)


# 1.87 03-Sep-2016 tedu

add sha512/256 support to check that the libc code is right :)


# 1.86 26-Aug-2016 guenther

Pull in <sys/time.h> for gettimeofday()


# 1.85 29-Jul-2016 tedu

all of the update functions take a size_t. correct type and casts.
ok deraadt millert


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.84 09-Dec-2015 mmcc

Remove NULL-check before free(). ok tb@


# 1.83 10-Oct-2015 deraadt

normalize the ordering of tame requests (particularily, "rpath wpath cpath",
which i have put in that order). this is not important, but helps look
for outliers which might be strange. it hints that "ioctl" should be
reassessed in a few places, to see if "tty" is better; that "unix" may
be used in some places where "route" could now work.


# 1.82 09-Oct-2015 deraadt

Change all tame callers to namechange to pledge(2).


# 1.81 04-Oct-2015 deraadt

Repair tame() error check to be == -1


# 1.80 03-Oct-2015 deraadt

right at startup, this can tame "stdio cpath rpath wpath". after getopt
-h has handled write/creating a file, we can drop to tame "stdio rpath"
since md5 will only read files after that.
i believe i involved lteo for this.


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.79 19-Jan-2015 deraadt

cleanup of the SMALL hiding from Mathias Svensson


# 1.78 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.77 13-Sep-2014 doug

Replace all queue *_END macro calls except CIRCLEQ_END with NULL.

CIRCLEQ_* is deprecated and not called in the tree. The other queue types
have *_END macros which were added for symmetry with CIRCLEQ_END. They are
defined as NULL. There's no reason to keep the other *_END macro calls.

ok millert@


Revision tags: OPENBSD_5_6_BASE
# 1.76 19-Jun-2014 millert

Fix memory leak in digest_file() on ferror(). OK tedu@ lteo@


# 1.75 26-Mar-2014 lteo

Ignore the -b option if cksum is called as-is (e.g. "cksum -b /bsd")
as documented on the cksum(1) man page.

"looks right" deraadt@
ok millert@


# 1.74 20-Mar-2014 tedu

purge sum and sysvsum code. ok deraadt jmc lteo millert
a few initial leftovers spotted by naddy


# 1.73 20-Mar-2014 naddy

remove insecure MD4 checksum algorithm; ok tedu@, man page ok jmc@

"A collision attack published in 2007 can find collisions for full
MD4 in less than two hash operations."


# 1.72 07-Mar-2014 lteo

When using the -C option, exit with an exit status of 1 if any of the
files specified on the command line do not exist in the checklist.

ok deraadt@ tedu@


Revision tags: OPENBSD_5_5_BASE
# 1.71 15-Jan-2014 jmc

add -C to the man page, and adjust usage();
ok lteo


# 1.70 15-Jan-2014 lteo

Add support for a -C option. It works on a checklist like -c but only
does the checksum comparison for selected files that are specified on
the command line.

idea discussed with deraadt@ and tedu@
manpage feedback jmc@
feedback/OK deraadt@ millert@


# 1.69 12-Jan-2014 deraadt

Also move case 'c' into the #ifdef for a smaller binary. It will fall into
default, giving a nice failure. I have not removed -c from the usage()
or getopt() because it is too much butchering...


# 1.68 11-Jan-2014 deraadt

use -DSHA2_ONLY to be more clear about what we are butchering
idea from tedu


# 1.67 10-Jan-2014 jmc

catch up to the fact that md5/sha* got merged, and document -c consistently;
some style and cleanup tweaks while here

ok deraadt


# 1.66 10-Jan-2014 lteo

When using a checklist, print MISSING for non-existent files.

Based on an earlier diff by tedu@
Requested by deraadt@
OK deraadt@


# 1.65 10-Jan-2014 tedu

the -c option is really a mode change, incompatible with other options.
(there are some others too, but -c is particularly misleading.) split it
out in synopsis and usage.
ok deraadt


# 1.64 08-Jan-2014 millert

Fix wrapping of usage message for cksum.


# 1.63 08-Jan-2014 millert

err() not errx() when fopen fails.


# 1.62 08-Jan-2014 millert

Remove NHASHES, it is not needed and is incompatible with #ifdef SMALL.
Remove style_hash and style_cksum, adjust the style of the selected
algorithms based on rflag and qflag instead.


# 1.61 08-Jan-2014 millert

Remove pmode, fix hfile open mode, check return value when writing
file name to stdout.


# 1.60 08-Jan-2014 deraadt

Add -DSMALL support to kill a lot of the unncecessary hashes and checksums
that the install media won't need
ok millert, but he used the word butcher!


# 1.59 08-Jan-2014 deraadt

Add support for -h hashfile. This is very useful with with -p, in a pipeline,
to deliver the hash output to a different location.
ok millert


# 1.58 23-Dec-2013 tedu

install a link as sha512. simplify program internals; there are only
two modes. ok deraadt gilles jca


# 1.57 21-Nov-2013 deraadt

add unsigned char casts for specific calls to ctype.h macros.
ok guenther step


Revision tags: OPENBSD_5_4_BASE
# 1.56 15-Apr-2013 millert

SHA-224 is to SHA-256 as SHA-384 is to SHA-512. It was added in a
later revision of FIPS-180. OK miod@ jmc@ guenther@ djm@


# 1.55 30-Mar-2013 lteo

Return an exit code of 1 if the file cannot be opened (e.g. file does
not exist), or if there is an error reading the file stream.

From Patrik Lundin, thanks!

ok deraadt


Revision tags: OPENBSD_5_3_BASE
# 1.54 04-Dec-2012 deraadt

replace sys/param.h with sys/types.h (and sometimes add limits.h if needed)
ok guenther


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.53 05-Jul-2011 tedu

fix memset sizeof, found by jsg. ok krw


Revision tags: OPENBSD_4_9_BASE
# 1.52 27-Oct-2010 millert

Use a 32KB data buffer instead of a 1KB buffer.


# 1.51 25-Oct-2010 millert

Use stdio when reading in the file to be checksummed. This can improve
performance due to the internal buffering stdio does wrt the optimal
read size (st_blksize). OK mikeb@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE OPENBSD_4_8_BASE
# 1.50 06-Sep-2008 djm

adapt to sha2(3) API changes; ok millert@


Revision tags: OPENBSD_4_4_BASE
# 1.49 29-Jul-2008 sobrado

md5(1), sha1(1), rmd160(1), cksum(1) and sum(1) have different options;
while here, some KNF improvements.

based on a diff for compress(1) written by millert@,
fixes from guenther@ and pyr@.

ok millert@, pyr@; (for the manual page tweaks) jmc@


Revision tags: OPENBSD_4_3_BASE
# 1.48 07-Nov-2007 chl

properly exit >0 when encountering bad md5 in cheklist

ok gilles@ millert@

From Tim van der Molen <tbvdm.lists at xs4all.nl>


# 1.47 31-Oct-2007 deraadt

clamp -ttttttttt test counter to 1B, so that it does not wrap negative;
from Nicholas Marriott, ok otto


Revision tags: OPENBSD_4_2_BASE
# 1.46 14-Apr-2007 grunk

if we define a constant for the OPTSTRING, we can as well use it.

ok tedu@


# 1.45 13-Apr-2007 tedu

allow multiple -t to increase the test count. cpus are getting too fast.
ok grunk. with manpage from grunk and jmc


# 1.44 29-Mar-2007 millert

Fix a cust & pasto
Correctly compute amount of base64 padding in checklist mode


# 1.43 29-Mar-2007 millert

Do not print a warning that no valid checksums were found if there
was a single fingerprint for a file that could not be opened.
The warning that the file could not be opened is sufficient.
From espie@


# 1.42 28-Mar-2007 millert

Supress warnings for the first getopt() pass. Otherwise,
any "unknown option" messages get printed twice.


# 1.41 27-Mar-2007 millert

Add base64 support to cksum and friends. Output encoding can
be set globally or on a per-algorithm basis when using multiple
hash functions. OK espie@ simon@ deraadt@; man help from jmc@


# 1.40 20-Mar-2007 thib

augment the -q option, now if specifed with -c (checklist)
it will only print out the failed cases.

OK millert@


Revision tags: OPENBSD_4_1_BASE
# 1.39 09-Jan-2007 deraadt

align usage message correctly


# 1.38 17-Nov-2006 jmc

simplify synopsis and usage(); ok millert


# 1.37 16-Nov-2006 millert

Add -r and -q options similar to what is supported by FreeBSD's md5
OK deraadt@ ckuethe@


# 1.36 10-Nov-2006 tom

Accept upper and lower case letters in a checksum, since this doesn't
alter the value of the digest. Nor, for that matter, does case affect
the name of the algorithm used.

Partly from Matthew Clarke, clamat (at) telus (dot) net.

ok djm@ millert@


Revision tags: OPENBSD_4_0_BASE
# 1.35 15-Mar-2006 dhill

Fix signed/unsigned comparisons by using size_t
Sprinkle some const

ok ray@ jaredy@


Revision tags: OPENBSD_3_9_BASE
# 1.34 20-Dec-2005 millert

Make "cksum -c" grok checklists generated with cksum, sum and sysvsum.
OK deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.33 17-Aug-2005 mpf

Exit unsuccessfully when comparing against a checklist file fails.
OK markus@ millert@


Revision tags: OPENBSD_3_7_BASE
# 1.32 29-Dec-2004 millert

Add support for multiple using algorithms. OK'd by several people
a while ago so get this out of my tree...


# 1.31 10-Dec-2004 millert

GNU md5sum uses tab not space to separate filename and hash. Fixes
parsing of GNU-style checklist files for files with spaces in them.
From Emil Mikulic; closes PR 4027


Revision tags: OPENBSD_3_6_BASE
# 1.30 04-May-2004 millert

sync usage w/ man page


# 1.29 02-May-2004 millert

Add support for cksum (3 varieties), md4, sha256, sha384, and sha512.
Consequently, cksum moves becomes a link to md5 and moves to /bin.
The cksum man page could use some polishing.


# 1.28 28-Apr-2004 millert

Use the FOO_DIGEST_LENGTH macros


Revision tags: OPENBSD_3_5_BASE
# 1.27 03-Mar-2004 millert

Extend "md5 -c" to parse the output of GNU md5sum.
Adapted from patches by Lawrence Teo and tedu@. OK tedu@


Revision tags: OPENBSD_3_4_BASE
# 1.26 21-Jul-2003 millert

Fix typo: unsigned -> unsigned char; from Thorsten Glaser


# 1.25 25-Jun-2003 deraadt

fix lots of proto issues


# 1.24 17-Jun-2003 millert

Sync with share/misc/license.template and add missing DARPA credit
where applicable.


# 1.23 11-Jun-2003 deraadt

ansification, art ok


# 1.22 03-Jun-2003 millert

Use an ISC-tyle license for all my code; it is simpler and more permissive.


# 1.21 28-Apr-2003 deraadt

return from main


# 1.20 23-Apr-2003 millert

strchr() should be strrchr(); zyrnix


Revision tags: OPENBSD_3_3_BASE
# 1.19 23-Mar-2003 millert

Do not closed stdin; Andrey Matveev


# 1.18 13-Mar-2003 millert

Cast 3 to size_t to shut up stupid gcc on 64bit platforms.


# 1.17 12-Mar-2003 millert

Add -c option similar to GNU md5sum.
Original patch from zyrnix but significantly modified by me.
deraadt@ OK


# 1.16 14-Jan-2003 millert

Report read() errors; noticed and OK by fgs@


Revision tags: OPENBSD_3_1_BASE OPENBSD_3_2_BASE
# 1.15 20-Jan-2002 camield

Swap TEST_BLOCK_LEN and TEST_BLOCK_COUNT in benchmark printf.

ok millert@


Revision tags: OPENBSD_3_0_BASE
# 1.14 06-Sep-2001 mpech

o) __progname aria;

millert@ ok.


# 1.13 03-Jun-2001 millert

Nope, that's not right either. Fix check for mutually exclusive args.


# 1.12 03-Jun-2001 espie

Obvious thinko: command line is wrong if it's too short, not too long.


# 1.11 02-Jun-2001 millert

Fix some typos and add missing test for string of 1 million a's.


# 1.10 02-Jun-2001 millert

New md5/sha1/rmd160 driver with a BSD copyright. Improvements:
o options that should be mutually exclusive are
o time trial works reasonably when it finishes in < 1 sec
o uses a function table instead of a bunch of globals


Revision tags: OPENBSD_2_6_BASE OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.9 07-Oct-1999 espie

One single malloc, with a union to get the largest needed size,
should be enough.


Revision tags: OPENBSD_2_3_BASE OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.8 02-Jan-1998 deraadt

use getopt; mph@pobox.com


Revision tags: OPENBSD_2_2_BASE
# 1.7 01-Aug-1997 deraadt

warn not warnx


# 1.6 17-Jul-1997 millert

Adds rmd160 functionality.


# 1.5 12-Jul-1997 millert

md5(1) is now capable of doing md4, md5 and sha1 digests. Currently
only md5 and sha1 are used.


# 1.4 20-Jun-1997 flipk

boost TEST_BLOCK_LEN and TEST_BLOCK_COUNT a bit. this makes '-t' more
useful on fast boxes.
also fflush(stdout) after printing the first test message so
the user knows its actually doing something


Revision tags: OPENBSD_2_1_BASE
# 1.3 24-Nov-1996 niklas

64-bit cleanup + pedantic -W flags


# 1.2 12-Nov-1996 niklas

-Wall happiness


Revision tags: OPENBSD_2_0_BASE
# 1.1 11-Jun-1996 deraadt

move to bin/md5 for `greater accessibility'


# 1.94 25-Jan-2019 millert

I am retiring my old email address; replace it with my OpenBSD one.


Revision tags: OPENBSD_6_4_BASE
# 1.93 07-Sep-2018 cheloha

fgetln(3) -> getline(3); from Lauri Tirkkonen; ok millert@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.92 11-Sep-2017 millert

Use getrusage(2) for the built-in time trial instead of getttimeofday(2).
This makes it possible to measure just the ellapsed user time, which
is what we really care about when benchmarking an algorithm.
OK deraadt@


# 1.91 22-May-2017 deraadt

obvious use for freezero()


# 1.90 26-Apr-2017 millert

In -C mode, process all checksums that match the specified file(s),
not just the first one that matches. OK deraadt@


Revision tags: OPENBSD_6_1_BASE
# 1.89 16-Dec-2016 krw

Move declaration of an 'i' inside the scope of its use. Which in
turn is inside an #ifdef. Thus making the code clearer by eliminating
an #ifdef in the middle of the declarations.

Suggested by millert@


# 1.88 16-Dec-2016 krw

Eliminate some gcc warnings about 'unused variables', mostly by
adding appropriate #ifdef's around declarations.

ok millert@ (with a tweak I will commit separately)


# 1.87 03-Sep-2016 tedu

add sha512/256 support to check that the libc code is right :)


# 1.86 26-Aug-2016 guenther

Pull in <sys/time.h> for gettimeofday()


# 1.85 29-Jul-2016 tedu

all of the update functions take a size_t. correct type and casts.
ok deraadt millert


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.84 09-Dec-2015 mmcc

Remove NULL-check before free(). ok tb@


# 1.83 10-Oct-2015 deraadt

normalize the ordering of tame requests (particularily, "rpath wpath cpath",
which i have put in that order). this is not important, but helps look
for outliers which might be strange. it hints that "ioctl" should be
reassessed in a few places, to see if "tty" is better; that "unix" may
be used in some places where "route" could now work.


# 1.82 09-Oct-2015 deraadt

Change all tame callers to namechange to pledge(2).


# 1.81 04-Oct-2015 deraadt

Repair tame() error check to be == -1


# 1.80 03-Oct-2015 deraadt

right at startup, this can tame "stdio cpath rpath wpath". after getopt
-h has handled write/creating a file, we can drop to tame "stdio rpath"
since md5 will only read files after that.
i believe i involved lteo for this.


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.79 19-Jan-2015 deraadt

cleanup of the SMALL hiding from Mathias Svensson


# 1.78 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.77 13-Sep-2014 doug

Replace all queue *_END macro calls except CIRCLEQ_END with NULL.

CIRCLEQ_* is deprecated and not called in the tree. The other queue types
have *_END macros which were added for symmetry with CIRCLEQ_END. They are
defined as NULL. There's no reason to keep the other *_END macro calls.

ok millert@


Revision tags: OPENBSD_5_6_BASE
# 1.76 19-Jun-2014 millert

Fix memory leak in digest_file() on ferror(). OK tedu@ lteo@


# 1.75 26-Mar-2014 lteo

Ignore the -b option if cksum is called as-is (e.g. "cksum -b /bsd")
as documented on the cksum(1) man page.

"looks right" deraadt@
ok millert@


# 1.74 20-Mar-2014 tedu

purge sum and sysvsum code. ok deraadt jmc lteo millert
a few initial leftovers spotted by naddy


# 1.73 20-Mar-2014 naddy

remove insecure MD4 checksum algorithm; ok tedu@, man page ok jmc@

"A collision attack published in 2007 can find collisions for full
MD4 in less than two hash operations."


# 1.72 07-Mar-2014 lteo

When using the -C option, exit with an exit status of 1 if any of the
files specified on the command line do not exist in the checklist.

ok deraadt@ tedu@


Revision tags: OPENBSD_5_5_BASE
# 1.71 15-Jan-2014 jmc

add -C to the man page, and adjust usage();
ok lteo


# 1.70 15-Jan-2014 lteo

Add support for a -C option. It works on a checklist like -c but only
does the checksum comparison for selected files that are specified on
the command line.

idea discussed with deraadt@ and tedu@
manpage feedback jmc@
feedback/OK deraadt@ millert@


# 1.69 12-Jan-2014 deraadt

Also move case 'c' into the #ifdef for a smaller binary. It will fall into
default, giving a nice failure. I have not removed -c from the usage()
or getopt() because it is too much butchering...


# 1.68 11-Jan-2014 deraadt

use -DSHA2_ONLY to be more clear about what we are butchering
idea from tedu


# 1.67 10-Jan-2014 jmc

catch up to the fact that md5/sha* got merged, and document -c consistently;
some style and cleanup tweaks while here

ok deraadt


# 1.66 10-Jan-2014 lteo

When using a checklist, print MISSING for non-existent files.

Based on an earlier diff by tedu@
Requested by deraadt@
OK deraadt@


# 1.65 10-Jan-2014 tedu

the -c option is really a mode change, incompatible with other options.
(there are some others too, but -c is particularly misleading.) split it
out in synopsis and usage.
ok deraadt


# 1.64 08-Jan-2014 millert

Fix wrapping of usage message for cksum.


# 1.63 08-Jan-2014 millert

err() not errx() when fopen fails.


# 1.62 08-Jan-2014 millert

Remove NHASHES, it is not needed and is incompatible with #ifdef SMALL.
Remove style_hash and style_cksum, adjust the style of the selected
algorithms based on rflag and qflag instead.


# 1.61 08-Jan-2014 millert

Remove pmode, fix hfile open mode, check return value when writing
file name to stdout.


# 1.60 08-Jan-2014 deraadt

Add -DSMALL support to kill a lot of the unncecessary hashes and checksums
that the install media won't need
ok millert, but he used the word butcher!


# 1.59 08-Jan-2014 deraadt

Add support for -h hashfile. This is very useful with with -p, in a pipeline,
to deliver the hash output to a different location.
ok millert


# 1.58 23-Dec-2013 tedu

install a link as sha512. simplify program internals; there are only
two modes. ok deraadt gilles jca


# 1.57 21-Nov-2013 deraadt

add unsigned char casts for specific calls to ctype.h macros.
ok guenther step


Revision tags: OPENBSD_5_4_BASE
# 1.56 15-Apr-2013 millert

SHA-224 is to SHA-256 as SHA-384 is to SHA-512. It was added in a
later revision of FIPS-180. OK miod@ jmc@ guenther@ djm@


# 1.55 30-Mar-2013 lteo

Return an exit code of 1 if the file cannot be opened (e.g. file does
not exist), or if there is an error reading the file stream.

From Patrik Lundin, thanks!

ok deraadt


Revision tags: OPENBSD_5_3_BASE
# 1.54 04-Dec-2012 deraadt

replace sys/param.h with sys/types.h (and sometimes add limits.h if needed)
ok guenther


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.53 05-Jul-2011 tedu

fix memset sizeof, found by jsg. ok krw


Revision tags: OPENBSD_4_9_BASE
# 1.52 27-Oct-2010 millert

Use a 32KB data buffer instead of a 1KB buffer.


# 1.51 25-Oct-2010 millert

Use stdio when reading in the file to be checksummed. This can improve
performance due to the internal buffering stdio does wrt the optimal
read size (st_blksize). OK mikeb@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE OPENBSD_4_8_BASE
# 1.50 06-Sep-2008 djm

adapt to sha2(3) API changes; ok millert@


Revision tags: OPENBSD_4_4_BASE
# 1.49 29-Jul-2008 sobrado

md5(1), sha1(1), rmd160(1), cksum(1) and sum(1) have different options;
while here, some KNF improvements.

based on a diff for compress(1) written by millert@,
fixes from guenther@ and pyr@.

ok millert@, pyr@; (for the manual page tweaks) jmc@


Revision tags: OPENBSD_4_3_BASE
# 1.48 07-Nov-2007 chl

properly exit >0 when encountering bad md5 in cheklist

ok gilles@ millert@

From Tim van der Molen <tbvdm.lists at xs4all.nl>


# 1.47 31-Oct-2007 deraadt

clamp -ttttttttt test counter to 1B, so that it does not wrap negative;
from Nicholas Marriott, ok otto


Revision tags: OPENBSD_4_2_BASE
# 1.46 14-Apr-2007 grunk

if we define a constant for the OPTSTRING, we can as well use it.

ok tedu@


# 1.45 13-Apr-2007 tedu

allow multiple -t to increase the test count. cpus are getting too fast.
ok grunk. with manpage from grunk and jmc


# 1.44 29-Mar-2007 millert

Fix a cust & pasto
Correctly compute amount of base64 padding in checklist mode


# 1.43 29-Mar-2007 millert

Do not print a warning that no valid checksums were found if there
was a single fingerprint for a file that could not be opened.
The warning that the file could not be opened is sufficient.
From espie@


# 1.42 28-Mar-2007 millert

Supress warnings for the first getopt() pass. Otherwise,
any "unknown option" messages get printed twice.


# 1.41 27-Mar-2007 millert

Add base64 support to cksum and friends. Output encoding can
be set globally or on a per-algorithm basis when using multiple
hash functions. OK espie@ simon@ deraadt@; man help from jmc@


# 1.40 20-Mar-2007 thib

augment the -q option, now if specifed with -c (checklist)
it will only print out the failed cases.

OK millert@


Revision tags: OPENBSD_4_1_BASE
# 1.39 09-Jan-2007 deraadt

align usage message correctly


# 1.38 17-Nov-2006 jmc

simplify synopsis and usage(); ok millert


# 1.37 16-Nov-2006 millert

Add -r and -q options similar to what is supported by FreeBSD's md5
OK deraadt@ ckuethe@


# 1.36 10-Nov-2006 tom

Accept upper and lower case letters in a checksum, since this doesn't
alter the value of the digest. Nor, for that matter, does case affect
the name of the algorithm used.

Partly from Matthew Clarke, clamat (at) telus (dot) net.

ok djm@ millert@


Revision tags: OPENBSD_4_0_BASE
# 1.35 15-Mar-2006 dhill

Fix signed/unsigned comparisons by using size_t
Sprinkle some const

ok ray@ jaredy@


Revision tags: OPENBSD_3_9_BASE
# 1.34 20-Dec-2005 millert

Make "cksum -c" grok checklists generated with cksum, sum and sysvsum.
OK deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.33 17-Aug-2005 mpf

Exit unsuccessfully when comparing against a checklist file fails.
OK markus@ millert@


Revision tags: OPENBSD_3_7_BASE
# 1.32 29-Dec-2004 millert

Add support for multiple using algorithms. OK'd by several people
a while ago so get this out of my tree...


# 1.31 10-Dec-2004 millert

GNU md5sum uses tab not space to separate filename and hash. Fixes
parsing of GNU-style checklist files for files with spaces in them.
From Emil Mikulic; closes PR 4027


Revision tags: OPENBSD_3_6_BASE
# 1.30 04-May-2004 millert

sync usage w/ man page


# 1.29 02-May-2004 millert

Add support for cksum (3 varieties), md4, sha256, sha384, and sha512.
Consequently, cksum moves becomes a link to md5 and moves to /bin.
The cksum man page could use some polishing.


# 1.28 28-Apr-2004 millert

Use the FOO_DIGEST_LENGTH macros


Revision tags: OPENBSD_3_5_BASE
# 1.27 03-Mar-2004 millert

Extend "md5 -c" to parse the output of GNU md5sum.
Adapted from patches by Lawrence Teo and tedu@. OK tedu@


Revision tags: OPENBSD_3_4_BASE
# 1.26 21-Jul-2003 millert

Fix typo: unsigned -> unsigned char; from Thorsten Glaser


# 1.25 25-Jun-2003 deraadt

fix lots of proto issues


# 1.24 17-Jun-2003 millert

Sync with share/misc/license.template and add missing DARPA credit
where applicable.


# 1.23 11-Jun-2003 deraadt

ansification, art ok


# 1.22 03-Jun-2003 millert

Use an ISC-tyle license for all my code; it is simpler and more permissive.


# 1.21 28-Apr-2003 deraadt

return from main


# 1.20 23-Apr-2003 millert

strchr() should be strrchr(); zyrnix


Revision tags: OPENBSD_3_3_BASE
# 1.19 23-Mar-2003 millert

Do not closed stdin; Andrey Matveev


# 1.18 13-Mar-2003 millert

Cast 3 to size_t to shut up stupid gcc on 64bit platforms.


# 1.17 12-Mar-2003 millert

Add -c option similar to GNU md5sum.
Original patch from zyrnix but significantly modified by me.
deraadt@ OK


# 1.16 14-Jan-2003 millert

Report read() errors; noticed and OK by fgs@


Revision tags: OPENBSD_3_1_BASE OPENBSD_3_2_BASE
# 1.15 20-Jan-2002 camield

Swap TEST_BLOCK_LEN and TEST_BLOCK_COUNT in benchmark printf.

ok millert@


Revision tags: OPENBSD_3_0_BASE
# 1.14 06-Sep-2001 mpech

o) __progname aria;

millert@ ok.


# 1.13 03-Jun-2001 millert

Nope, that's not right either. Fix check for mutually exclusive args.


# 1.12 03-Jun-2001 espie

Obvious thinko: command line is wrong if it's too short, not too long.


# 1.11 02-Jun-2001 millert

Fix some typos and add missing test for string of 1 million a's.


# 1.10 02-Jun-2001 millert

New md5/sha1/rmd160 driver with a BSD copyright. Improvements:
o options that should be mutually exclusive are
o time trial works reasonably when it finishes in < 1 sec
o uses a function table instead of a bunch of globals


Revision tags: OPENBSD_2_6_BASE OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.9 07-Oct-1999 espie

One single malloc, with a union to get the largest needed size,
should be enough.


Revision tags: OPENBSD_2_3_BASE OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.8 02-Jan-1998 deraadt

use getopt; mph@pobox.com


Revision tags: OPENBSD_2_2_BASE
# 1.7 01-Aug-1997 deraadt

warn not warnx


# 1.6 17-Jul-1997 millert

Adds rmd160 functionality.


# 1.5 12-Jul-1997 millert

md5(1) is now capable of doing md4, md5 and sha1 digests. Currently
only md5 and sha1 are used.


# 1.4 20-Jun-1997 flipk

boost TEST_BLOCK_LEN and TEST_BLOCK_COUNT a bit. this makes '-t' more
useful on fast boxes.
also fflush(stdout) after printing the first test message so
the user knows its actually doing something


Revision tags: OPENBSD_2_1_BASE
# 1.3 24-Nov-1996 niklas

64-bit cleanup + pedantic -W flags


# 1.2 12-Nov-1996 niklas

-Wall happiness


Revision tags: OPENBSD_2_0_BASE
# 1.1 11-Jun-1996 deraadt

move to bin/md5 for `greater accessibility'


# 1.93 07-Sep-2018 cheloha

fgetln(3) -> getline(3); from Lauri Tirkkonen; ok millert@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.92 11-Sep-2017 millert

Use getrusage(2) for the built-in time trial instead of getttimeofday(2).
This makes it possible to measure just the ellapsed user time, which
is what we really care about when benchmarking an algorithm.
OK deraadt@


# 1.91 22-May-2017 deraadt

obvious use for freezero()


# 1.90 26-Apr-2017 millert

In -C mode, process all checksums that match the specified file(s),
not just the first one that matches. OK deraadt@


Revision tags: OPENBSD_6_1_BASE
# 1.89 16-Dec-2016 krw

Move declaration of an 'i' inside the scope of its use. Which in
turn is inside an #ifdef. Thus making the code clearer by eliminating
an #ifdef in the middle of the declarations.

Suggested by millert@


# 1.88 16-Dec-2016 krw

Eliminate some gcc warnings about 'unused variables', mostly by
adding appropriate #ifdef's around declarations.

ok millert@ (with a tweak I will commit separately)


# 1.87 03-Sep-2016 tedu

add sha512/256 support to check that the libc code is right :)


# 1.86 26-Aug-2016 guenther

Pull in <sys/time.h> for gettimeofday()


# 1.85 29-Jul-2016 tedu

all of the update functions take a size_t. correct type and casts.
ok deraadt millert


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.84 09-Dec-2015 mmcc

Remove NULL-check before free(). ok tb@


# 1.83 10-Oct-2015 deraadt

normalize the ordering of tame requests (particularily, "rpath wpath cpath",
which i have put in that order). this is not important, but helps look
for outliers which might be strange. it hints that "ioctl" should be
reassessed in a few places, to see if "tty" is better; that "unix" may
be used in some places where "route" could now work.


# 1.82 09-Oct-2015 deraadt

Change all tame callers to namechange to pledge(2).


# 1.81 04-Oct-2015 deraadt

Repair tame() error check to be == -1


# 1.80 03-Oct-2015 deraadt

right at startup, this can tame "stdio cpath rpath wpath". after getopt
-h has handled write/creating a file, we can drop to tame "stdio rpath"
since md5 will only read files after that.
i believe i involved lteo for this.


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.79 19-Jan-2015 deraadt

cleanup of the SMALL hiding from Mathias Svensson


# 1.78 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.77 13-Sep-2014 doug

Replace all queue *_END macro calls except CIRCLEQ_END with NULL.

CIRCLEQ_* is deprecated and not called in the tree. The other queue types
have *_END macros which were added for symmetry with CIRCLEQ_END. They are
defined as NULL. There's no reason to keep the other *_END macro calls.

ok millert@


Revision tags: OPENBSD_5_6_BASE
# 1.76 19-Jun-2014 millert

Fix memory leak in digest_file() on ferror(). OK tedu@ lteo@


# 1.75 26-Mar-2014 lteo

Ignore the -b option if cksum is called as-is (e.g. "cksum -b /bsd")
as documented on the cksum(1) man page.

"looks right" deraadt@
ok millert@


# 1.74 20-Mar-2014 tedu

purge sum and sysvsum code. ok deraadt jmc lteo millert
a few initial leftovers spotted by naddy


# 1.73 20-Mar-2014 naddy

remove insecure MD4 checksum algorithm; ok tedu@, man page ok jmc@

"A collision attack published in 2007 can find collisions for full
MD4 in less than two hash operations."


# 1.72 07-Mar-2014 lteo

When using the -C option, exit with an exit status of 1 if any of the
files specified on the command line do not exist in the checklist.

ok deraadt@ tedu@


Revision tags: OPENBSD_5_5_BASE
# 1.71 15-Jan-2014 jmc

add -C to the man page, and adjust usage();
ok lteo


# 1.70 15-Jan-2014 lteo

Add support for a -C option. It works on a checklist like -c but only
does the checksum comparison for selected files that are specified on
the command line.

idea discussed with deraadt@ and tedu@
manpage feedback jmc@
feedback/OK deraadt@ millert@


# 1.69 12-Jan-2014 deraadt

Also move case 'c' into the #ifdef for a smaller binary. It will fall into
default, giving a nice failure. I have not removed -c from the usage()
or getopt() because it is too much butchering...


# 1.68 11-Jan-2014 deraadt

use -DSHA2_ONLY to be more clear about what we are butchering
idea from tedu


# 1.67 10-Jan-2014 jmc

catch up to the fact that md5/sha* got merged, and document -c consistently;
some style and cleanup tweaks while here

ok deraadt


# 1.66 10-Jan-2014 lteo

When using a checklist, print MISSING for non-existent files.

Based on an earlier diff by tedu@
Requested by deraadt@
OK deraadt@


# 1.65 10-Jan-2014 tedu

the -c option is really a mode change, incompatible with other options.
(there are some others too, but -c is particularly misleading.) split it
out in synopsis and usage.
ok deraadt


# 1.64 08-Jan-2014 millert

Fix wrapping of usage message for cksum.


# 1.63 08-Jan-2014 millert

err() not errx() when fopen fails.


# 1.62 08-Jan-2014 millert

Remove NHASHES, it is not needed and is incompatible with #ifdef SMALL.
Remove style_hash and style_cksum, adjust the style of the selected
algorithms based on rflag and qflag instead.


# 1.61 08-Jan-2014 millert

Remove pmode, fix hfile open mode, check return value when writing
file name to stdout.


# 1.60 08-Jan-2014 deraadt

Add -DSMALL support to kill a lot of the unncecessary hashes and checksums
that the install media won't need
ok millert, but he used the word butcher!


# 1.59 08-Jan-2014 deraadt

Add support for -h hashfile. This is very useful with with -p, in a pipeline,
to deliver the hash output to a different location.
ok millert


# 1.58 23-Dec-2013 tedu

install a link as sha512. simplify program internals; there are only
two modes. ok deraadt gilles jca


# 1.57 21-Nov-2013 deraadt

add unsigned char casts for specific calls to ctype.h macros.
ok guenther step


Revision tags: OPENBSD_5_4_BASE
# 1.56 15-Apr-2013 millert

SHA-224 is to SHA-256 as SHA-384 is to SHA-512. It was added in a
later revision of FIPS-180. OK miod@ jmc@ guenther@ djm@


# 1.55 30-Mar-2013 lteo

Return an exit code of 1 if the file cannot be opened (e.g. file does
not exist), or if there is an error reading the file stream.

From Patrik Lundin, thanks!

ok deraadt


Revision tags: OPENBSD_5_3_BASE
# 1.54 04-Dec-2012 deraadt

replace sys/param.h with sys/types.h (and sometimes add limits.h if needed)
ok guenther


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.53 05-Jul-2011 tedu

fix memset sizeof, found by jsg. ok krw


Revision tags: OPENBSD_4_9_BASE
# 1.52 27-Oct-2010 millert

Use a 32KB data buffer instead of a 1KB buffer.


# 1.51 25-Oct-2010 millert

Use stdio when reading in the file to be checksummed. This can improve
performance due to the internal buffering stdio does wrt the optimal
read size (st_blksize). OK mikeb@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE OPENBSD_4_8_BASE
# 1.50 06-Sep-2008 djm

adapt to sha2(3) API changes; ok millert@


Revision tags: OPENBSD_4_4_BASE
# 1.49 29-Jul-2008 sobrado

md5(1), sha1(1), rmd160(1), cksum(1) and sum(1) have different options;
while here, some KNF improvements.

based on a diff for compress(1) written by millert@,
fixes from guenther@ and pyr@.

ok millert@, pyr@; (for the manual page tweaks) jmc@


Revision tags: OPENBSD_4_3_BASE
# 1.48 07-Nov-2007 chl

properly exit >0 when encountering bad md5 in cheklist

ok gilles@ millert@

From Tim van der Molen <tbvdm.lists at xs4all.nl>


# 1.47 31-Oct-2007 deraadt

clamp -ttttttttt test counter to 1B, so that it does not wrap negative;
from Nicholas Marriott, ok otto


Revision tags: OPENBSD_4_2_BASE
# 1.46 14-Apr-2007 grunk

if we define a constant for the OPTSTRING, we can as well use it.

ok tedu@


# 1.45 13-Apr-2007 tedu

allow multiple -t to increase the test count. cpus are getting too fast.
ok grunk. with manpage from grunk and jmc


# 1.44 29-Mar-2007 millert

Fix a cust & pasto
Correctly compute amount of base64 padding in checklist mode


# 1.43 29-Mar-2007 millert

Do not print a warning that no valid checksums were found if there
was a single fingerprint for a file that could not be opened.
The warning that the file could not be opened is sufficient.
From espie@


# 1.42 28-Mar-2007 millert

Supress warnings for the first getopt() pass. Otherwise,
any "unknown option" messages get printed twice.


# 1.41 27-Mar-2007 millert

Add base64 support to cksum and friends. Output encoding can
be set globally or on a per-algorithm basis when using multiple
hash functions. OK espie@ simon@ deraadt@; man help from jmc@


# 1.40 20-Mar-2007 thib

augment the -q option, now if specifed with -c (checklist)
it will only print out the failed cases.

OK millert@


Revision tags: OPENBSD_4_1_BASE
# 1.39 09-Jan-2007 deraadt

align usage message correctly


# 1.38 17-Nov-2006 jmc

simplify synopsis and usage(); ok millert


# 1.37 16-Nov-2006 millert

Add -r and -q options similar to what is supported by FreeBSD's md5
OK deraadt@ ckuethe@


# 1.36 10-Nov-2006 tom

Accept upper and lower case letters in a checksum, since this doesn't
alter the value of the digest. Nor, for that matter, does case affect
the name of the algorithm used.

Partly from Matthew Clarke, clamat (at) telus (dot) net.

ok djm@ millert@


Revision tags: OPENBSD_4_0_BASE
# 1.35 15-Mar-2006 dhill

Fix signed/unsigned comparisons by using size_t
Sprinkle some const

ok ray@ jaredy@


Revision tags: OPENBSD_3_9_BASE
# 1.34 20-Dec-2005 millert

Make "cksum -c" grok checklists generated with cksum, sum and sysvsum.
OK deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.33 17-Aug-2005 mpf

Exit unsuccessfully when comparing against a checklist file fails.
OK markus@ millert@


Revision tags: OPENBSD_3_7_BASE
# 1.32 29-Dec-2004 millert

Add support for multiple using algorithms. OK'd by several people
a while ago so get this out of my tree...


# 1.31 10-Dec-2004 millert

GNU md5sum uses tab not space to separate filename and hash. Fixes
parsing of GNU-style checklist files for files with spaces in them.
From Emil Mikulic; closes PR 4027


Revision tags: OPENBSD_3_6_BASE
# 1.30 04-May-2004 millert

sync usage w/ man page


# 1.29 02-May-2004 millert

Add support for cksum (3 varieties), md4, sha256, sha384, and sha512.
Consequently, cksum moves becomes a link to md5 and moves to /bin.
The cksum man page could use some polishing.


# 1.28 28-Apr-2004 millert

Use the FOO_DIGEST_LENGTH macros


Revision tags: OPENBSD_3_5_BASE
# 1.27 03-Mar-2004 millert

Extend "md5 -c" to parse the output of GNU md5sum.
Adapted from patches by Lawrence Teo and tedu@. OK tedu@


Revision tags: OPENBSD_3_4_BASE
# 1.26 21-Jul-2003 millert

Fix typo: unsigned -> unsigned char; from Thorsten Glaser


# 1.25 25-Jun-2003 deraadt

fix lots of proto issues


# 1.24 17-Jun-2003 millert

Sync with share/misc/license.template and add missing DARPA credit
where applicable.


# 1.23 11-Jun-2003 deraadt

ansification, art ok


# 1.22 03-Jun-2003 millert

Use an ISC-tyle license for all my code; it is simpler and more permissive.


# 1.21 28-Apr-2003 deraadt

return from main


# 1.20 23-Apr-2003 millert

strchr() should be strrchr(); zyrnix


Revision tags: OPENBSD_3_3_BASE
# 1.19 23-Mar-2003 millert

Do not closed stdin; Andrey Matveev


# 1.18 13-Mar-2003 millert

Cast 3 to size_t to shut up stupid gcc on 64bit platforms.


# 1.17 12-Mar-2003 millert

Add -c option similar to GNU md5sum.
Original patch from zyrnix but significantly modified by me.
deraadt@ OK


# 1.16 14-Jan-2003 millert

Report read() errors; noticed and OK by fgs@


Revision tags: OPENBSD_3_1_BASE OPENBSD_3_2_BASE
# 1.15 20-Jan-2002 camield

Swap TEST_BLOCK_LEN and TEST_BLOCK_COUNT in benchmark printf.

ok millert@


Revision tags: OPENBSD_3_0_BASE
# 1.14 06-Sep-2001 mpech

o) __progname aria;

millert@ ok.


# 1.13 03-Jun-2001 millert

Nope, that's not right either. Fix check for mutually exclusive args.


# 1.12 03-Jun-2001 espie

Obvious thinko: command line is wrong if it's too short, not too long.


# 1.11 02-Jun-2001 millert

Fix some typos and add missing test for string of 1 million a's.


# 1.10 02-Jun-2001 millert

New md5/sha1/rmd160 driver with a BSD copyright. Improvements:
o options that should be mutually exclusive are
o time trial works reasonably when it finishes in < 1 sec
o uses a function table instead of a bunch of globals


Revision tags: OPENBSD_2_6_BASE OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.9 07-Oct-1999 espie

One single malloc, with a union to get the largest needed size,
should be enough.


Revision tags: OPENBSD_2_3_BASE OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.8 02-Jan-1998 deraadt

use getopt; mph@pobox.com


Revision tags: OPENBSD_2_2_BASE
# 1.7 01-Aug-1997 deraadt

warn not warnx


# 1.6 17-Jul-1997 millert

Adds rmd160 functionality.


# 1.5 12-Jul-1997 millert

md5(1) is now capable of doing md4, md5 and sha1 digests. Currently
only md5 and sha1 are used.


# 1.4 20-Jun-1997 flipk

boost TEST_BLOCK_LEN and TEST_BLOCK_COUNT a bit. this makes '-t' more
useful on fast boxes.
also fflush(stdout) after printing the first test message so
the user knows its actually doing something


Revision tags: OPENBSD_2_1_BASE
# 1.3 24-Nov-1996 niklas

64-bit cleanup + pedantic -W flags


# 1.2 12-Nov-1996 niklas

-Wall happiness


Revision tags: OPENBSD_2_0_BASE
# 1.1 11-Jun-1996 deraadt

move to bin/md5 for `greater accessibility'


Revision tags: OPENBSD_6_2_BASE
# 1.92 11-Sep-2017 millert

Use getrusage(2) for the built-in time trial instead of getttimeofday(2).
This makes it possible to measure just the ellapsed user time, which
is what we really care about when benchmarking an algorithm.
OK deraadt@


# 1.91 22-May-2017 deraadt

obvious use for freezero()


# 1.90 26-Apr-2017 millert

In -C mode, process all checksums that match the specified file(s),
not just the first one that matches. OK deraadt@


Revision tags: OPENBSD_6_1_BASE
# 1.89 16-Dec-2016 krw

Move declaration of an 'i' inside the scope of its use. Which in
turn is inside an #ifdef. Thus making the code clearer by eliminating
an #ifdef in the middle of the declarations.

Suggested by millert@


# 1.88 16-Dec-2016 krw

Eliminate some gcc warnings about 'unused variables', mostly by
adding appropriate #ifdef's around declarations.

ok millert@ (with a tweak I will commit separately)


# 1.87 03-Sep-2016 tedu

add sha512/256 support to check that the libc code is right :)


# 1.86 26-Aug-2016 guenther

Pull in <sys/time.h> for gettimeofday()


# 1.85 29-Jul-2016 tedu

all of the update functions take a size_t. correct type and casts.
ok deraadt millert


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.84 09-Dec-2015 mmcc

Remove NULL-check before free(). ok tb@


# 1.83 10-Oct-2015 deraadt

normalize the ordering of tame requests (particularily, "rpath wpath cpath",
which i have put in that order). this is not important, but helps look
for outliers which might be strange. it hints that "ioctl" should be
reassessed in a few places, to see if "tty" is better; that "unix" may
be used in some places where "route" could now work.


# 1.82 09-Oct-2015 deraadt

Change all tame callers to namechange to pledge(2).


# 1.81 04-Oct-2015 deraadt

Repair tame() error check to be == -1


# 1.80 03-Oct-2015 deraadt

right at startup, this can tame "stdio cpath rpath wpath". after getopt
-h has handled write/creating a file, we can drop to tame "stdio rpath"
since md5 will only read files after that.
i believe i involved lteo for this.


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.79 19-Jan-2015 deraadt

cleanup of the SMALL hiding from Mathias Svensson


# 1.78 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.77 13-Sep-2014 doug

Replace all queue *_END macro calls except CIRCLEQ_END with NULL.

CIRCLEQ_* is deprecated and not called in the tree. The other queue types
have *_END macros which were added for symmetry with CIRCLEQ_END. They are
defined as NULL. There's no reason to keep the other *_END macro calls.

ok millert@


Revision tags: OPENBSD_5_6_BASE
# 1.76 19-Jun-2014 millert

Fix memory leak in digest_file() on ferror(). OK tedu@ lteo@


# 1.75 26-Mar-2014 lteo

Ignore the -b option if cksum is called as-is (e.g. "cksum -b /bsd")
as documented on the cksum(1) man page.

"looks right" deraadt@
ok millert@


# 1.74 20-Mar-2014 tedu

purge sum and sysvsum code. ok deraadt jmc lteo millert
a few initial leftovers spotted by naddy


# 1.73 20-Mar-2014 naddy

remove insecure MD4 checksum algorithm; ok tedu@, man page ok jmc@

"A collision attack published in 2007 can find collisions for full
MD4 in less than two hash operations."


# 1.72 07-Mar-2014 lteo

When using the -C option, exit with an exit status of 1 if any of the
files specified on the command line do not exist in the checklist.

ok deraadt@ tedu@


Revision tags: OPENBSD_5_5_BASE
# 1.71 15-Jan-2014 jmc

add -C to the man page, and adjust usage();
ok lteo


# 1.70 15-Jan-2014 lteo

Add support for a -C option. It works on a checklist like -c but only
does the checksum comparison for selected files that are specified on
the command line.

idea discussed with deraadt@ and tedu@
manpage feedback jmc@
feedback/OK deraadt@ millert@


# 1.69 12-Jan-2014 deraadt

Also move case 'c' into the #ifdef for a smaller binary. It will fall into
default, giving a nice failure. I have not removed -c from the usage()
or getopt() because it is too much butchering...


# 1.68 11-Jan-2014 deraadt

use -DSHA2_ONLY to be more clear about what we are butchering
idea from tedu


# 1.67 10-Jan-2014 jmc

catch up to the fact that md5/sha* got merged, and document -c consistently;
some style and cleanup tweaks while here

ok deraadt


# 1.66 10-Jan-2014 lteo

When using a checklist, print MISSING for non-existent files.

Based on an earlier diff by tedu@
Requested by deraadt@
OK deraadt@


# 1.65 10-Jan-2014 tedu

the -c option is really a mode change, incompatible with other options.
(there are some others too, but -c is particularly misleading.) split it
out in synopsis and usage.
ok deraadt


# 1.64 08-Jan-2014 millert

Fix wrapping of usage message for cksum.


# 1.63 08-Jan-2014 millert

err() not errx() when fopen fails.


# 1.62 08-Jan-2014 millert

Remove NHASHES, it is not needed and is incompatible with #ifdef SMALL.
Remove style_hash and style_cksum, adjust the style of the selected
algorithms based on rflag and qflag instead.


# 1.61 08-Jan-2014 millert

Remove pmode, fix hfile open mode, check return value when writing
file name to stdout.


# 1.60 08-Jan-2014 deraadt

Add -DSMALL support to kill a lot of the unncecessary hashes and checksums
that the install media won't need
ok millert, but he used the word butcher!


# 1.59 08-Jan-2014 deraadt

Add support for -h hashfile. This is very useful with with -p, in a pipeline,
to deliver the hash output to a different location.
ok millert


# 1.58 23-Dec-2013 tedu

install a link as sha512. simplify program internals; there are only
two modes. ok deraadt gilles jca


# 1.57 21-Nov-2013 deraadt

add unsigned char casts for specific calls to ctype.h macros.
ok guenther step


Revision tags: OPENBSD_5_4_BASE
# 1.56 15-Apr-2013 millert

SHA-224 is to SHA-256 as SHA-384 is to SHA-512. It was added in a
later revision of FIPS-180. OK miod@ jmc@ guenther@ djm@


# 1.55 30-Mar-2013 lteo

Return an exit code of 1 if the file cannot be opened (e.g. file does
not exist), or if there is an error reading the file stream.

From Patrik Lundin, thanks!

ok deraadt


Revision tags: OPENBSD_5_3_BASE
# 1.54 04-Dec-2012 deraadt

replace sys/param.h with sys/types.h (and sometimes add limits.h if needed)
ok guenther


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.53 05-Jul-2011 tedu

fix memset sizeof, found by jsg. ok krw


Revision tags: OPENBSD_4_9_BASE
# 1.52 27-Oct-2010 millert

Use a 32KB data buffer instead of a 1KB buffer.


# 1.51 25-Oct-2010 millert

Use stdio when reading in the file to be checksummed. This can improve
performance due to the internal buffering stdio does wrt the optimal
read size (st_blksize). OK mikeb@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE OPENBSD_4_8_BASE
# 1.50 06-Sep-2008 djm

adapt to sha2(3) API changes; ok millert@


Revision tags: OPENBSD_4_4_BASE
# 1.49 29-Jul-2008 sobrado

md5(1), sha1(1), rmd160(1), cksum(1) and sum(1) have different options;
while here, some KNF improvements.

based on a diff for compress(1) written by millert@,
fixes from guenther@ and pyr@.

ok millert@, pyr@; (for the manual page tweaks) jmc@


Revision tags: OPENBSD_4_3_BASE
# 1.48 07-Nov-2007 chl

properly exit >0 when encountering bad md5 in cheklist

ok gilles@ millert@

From Tim van der Molen <tbvdm.lists at xs4all.nl>


# 1.47 31-Oct-2007 deraadt

clamp -ttttttttt test counter to 1B, so that it does not wrap negative;
from Nicholas Marriott, ok otto


Revision tags: OPENBSD_4_2_BASE
# 1.46 14-Apr-2007 grunk

if we define a constant for the OPTSTRING, we can as well use it.

ok tedu@


# 1.45 13-Apr-2007 tedu

allow multiple -t to increase the test count. cpus are getting too fast.
ok grunk. with manpage from grunk and jmc


# 1.44 29-Mar-2007 millert

Fix a cust & pasto
Correctly compute amount of base64 padding in checklist mode


# 1.43 29-Mar-2007 millert

Do not print a warning that no valid checksums were found if there
was a single fingerprint for a file that could not be opened.
The warning that the file could not be opened is sufficient.
From espie@


# 1.42 28-Mar-2007 millert

Supress warnings for the first getopt() pass. Otherwise,
any "unknown option" messages get printed twice.


# 1.41 27-Mar-2007 millert

Add base64 support to cksum and friends. Output encoding can
be set globally or on a per-algorithm basis when using multiple
hash functions. OK espie@ simon@ deraadt@; man help from jmc@


# 1.40 20-Mar-2007 thib

augment the -q option, now if specifed with -c (checklist)
it will only print out the failed cases.

OK millert@


Revision tags: OPENBSD_4_1_BASE
# 1.39 09-Jan-2007 deraadt

align usage message correctly


# 1.38 17-Nov-2006 jmc

simplify synopsis and usage(); ok millert


# 1.37 16-Nov-2006 millert

Add -r and -q options similar to what is supported by FreeBSD's md5
OK deraadt@ ckuethe@


# 1.36 10-Nov-2006 tom

Accept upper and lower case letters in a checksum, since this doesn't
alter the value of the digest. Nor, for that matter, does case affect
the name of the algorithm used.

Partly from Matthew Clarke, clamat (at) telus (dot) net.

ok djm@ millert@


Revision tags: OPENBSD_4_0_BASE
# 1.35 15-Mar-2006 dhill

Fix signed/unsigned comparisons by using size_t
Sprinkle some const

ok ray@ jaredy@


Revision tags: OPENBSD_3_9_BASE
# 1.34 20-Dec-2005 millert

Make "cksum -c" grok checklists generated with cksum, sum and sysvsum.
OK deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.33 17-Aug-2005 mpf

Exit unsuccessfully when comparing against a checklist file fails.
OK markus@ millert@


Revision tags: OPENBSD_3_7_BASE
# 1.32 29-Dec-2004 millert

Add support for multiple using algorithms. OK'd by several people
a while ago so get this out of my tree...


# 1.31 10-Dec-2004 millert

GNU md5sum uses tab not space to separate filename and hash. Fixes
parsing of GNU-style checklist files for files with spaces in them.
From Emil Mikulic; closes PR 4027


Revision tags: OPENBSD_3_6_BASE
# 1.30 04-May-2004 millert

sync usage w/ man page


# 1.29 02-May-2004 millert

Add support for cksum (3 varieties), md4, sha256, sha384, and sha512.
Consequently, cksum moves becomes a link to md5 and moves to /bin.
The cksum man page could use some polishing.


# 1.28 28-Apr-2004 millert

Use the FOO_DIGEST_LENGTH macros


Revision tags: OPENBSD_3_5_BASE
# 1.27 03-Mar-2004 millert

Extend "md5 -c" to parse the output of GNU md5sum.
Adapted from patches by Lawrence Teo and tedu@. OK tedu@


Revision tags: OPENBSD_3_4_BASE
# 1.26 21-Jul-2003 millert

Fix typo: unsigned -> unsigned char; from Thorsten Glaser


# 1.25 25-Jun-2003 deraadt

fix lots of proto issues


# 1.24 17-Jun-2003 millert

Sync with share/misc/license.template and add missing DARPA credit
where applicable.


# 1.23 11-Jun-2003 deraadt

ansification, art ok


# 1.22 03-Jun-2003 millert

Use an ISC-tyle license for all my code; it is simpler and more permissive.


# 1.21 28-Apr-2003 deraadt

return from main


# 1.20 23-Apr-2003 millert

strchr() should be strrchr(); zyrnix


Revision tags: OPENBSD_3_3_BASE
# 1.19 23-Mar-2003 millert

Do not closed stdin; Andrey Matveev


# 1.18 13-Mar-2003 millert

Cast 3 to size_t to shut up stupid gcc on 64bit platforms.


# 1.17 12-Mar-2003 millert

Add -c option similar to GNU md5sum.
Original patch from zyrnix but significantly modified by me.
deraadt@ OK


# 1.16 14-Jan-2003 millert

Report read() errors; noticed and OK by fgs@


Revision tags: OPENBSD_3_1_BASE OPENBSD_3_2_BASE
# 1.15 20-Jan-2002 camield

Swap TEST_BLOCK_LEN and TEST_BLOCK_COUNT in benchmark printf.

ok millert@


Revision tags: OPENBSD_3_0_BASE
# 1.14 06-Sep-2001 mpech

o) __progname aria;

millert@ ok.


# 1.13 03-Jun-2001 millert

Nope, that's not right either. Fix check for mutually exclusive args.


# 1.12 03-Jun-2001 espie

Obvious thinko: command line is wrong if it's too short, not too long.


# 1.11 02-Jun-2001 millert

Fix some typos and add missing test for string of 1 million a's.


# 1.10 02-Jun-2001 millert

New md5/sha1/rmd160 driver with a BSD copyright. Improvements:
o options that should be mutually exclusive are
o time trial works reasonably when it finishes in < 1 sec
o uses a function table instead of a bunch of globals


Revision tags: OPENBSD_2_6_BASE OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.9 07-Oct-1999 espie

One single malloc, with a union to get the largest needed size,
should be enough.


Revision tags: OPENBSD_2_3_BASE OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.8 02-Jan-1998 deraadt

use getopt; mph@pobox.com


Revision tags: OPENBSD_2_2_BASE
# 1.7 01-Aug-1997 deraadt

warn not warnx


# 1.6 17-Jul-1997 millert

Adds rmd160 functionality.


# 1.5 12-Jul-1997 millert

md5(1) is now capable of doing md4, md5 and sha1 digests. Currently
only md5 and sha1 are used.


# 1.4 20-Jun-1997 flipk

boost TEST_BLOCK_LEN and TEST_BLOCK_COUNT a bit. this makes '-t' more
useful on fast boxes.
also fflush(stdout) after printing the first test message so
the user knows its actually doing something


Revision tags: OPENBSD_2_1_BASE
# 1.3 24-Nov-1996 niklas

64-bit cleanup + pedantic -W flags


# 1.2 12-Nov-1996 niklas

-Wall happiness


Revision tags: OPENBSD_2_0_BASE
# 1.1 11-Jun-1996 deraadt

move to bin/md5 for `greater accessibility'