From: Richard Russon Date: Wed, 2 Aug 2017 14:15:18 +0000 (+0100) Subject: move the sha1 functions to the library X-Git-Tag: neomutt-20170907~56^2~10 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=63e532d40079460c31a0923ae847a52dc116b35f;p=neomutt move the sha1 functions to the library --- diff --git a/Makefile.am b/Makefile.am index 11687460c..08bdc8afa 100644 --- a/Makefile.am +++ b/Makefile.am @@ -80,7 +80,7 @@ AM_CPPFLAGS=-I. -I$(top_srcdir) $(GPGME_CFLAGS) EXTRA_mutt_SOURCES = browser.h mbyte.h mutt_idna.c mutt_idna.h \ mutt_lua.c mutt_sasl.c mutt_notmuch.c mutt_ssl.c mutt_ssl_gnutls.c \ - remailer.c remailer.h resize.c sha1.c url.h utf8.c wcwidth.c + remailer.c remailer.h resize.c url.h utf8.c wcwidth.c EXTRA_DIST = account.h attach.h bcache.h browser.h buffer.h buffy.h \ ChangeLog.md charset.h CODE_OF_CONDUCT.md compress.h copy.h \ @@ -92,13 +92,13 @@ EXTRA_DIST = account.h attach.h bcache.h browser.h buffer.h buffy.h \ mutt_socket.h mutt_ssl.h mutt_tunnel.h mx.h myvar.h nntp.h OPS \ OPS.CRYPT OPS.MIX OPS.NOTMUCH OPS.PGP OPS.SIDEBAR OPS.SMIME pager.h \ pgpewrap.c pop.h protos.h README.md README.SSL remailer.c remailer.h \ - rfc1524.h rfc2047.h rfc2231.h rfc3676.h rfc822.h sha1.h sidebar.h \ + rfc1524.h rfc2047.h rfc2231.h rfc3676.h rfc822.h sidebar.h \ sort.h txt2c.c txt2c.sh version.h \ keymap_alldefs.h EXTRA_SCRIPTS = -pgpring_SOURCES = extlib.c lib.c pgppubring.c sha1.c +pgpring_SOURCES = extlib.c lib.c pgppubring.c pgpring_LDADD = $(LIBOBJS) $(NCRYPT_LIBS) $(INTLLIBS) $(LIBLIB) pgpring_DEPENDENCIES = $(LIBOBJS) $(NCRYPT_DEPS) $(INTLDEPS) $(LIBLIBDEPS) diff --git a/lib/Makefile.am b/lib/Makefile.am index 331129a3c..55acb83e9 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -3,12 +3,12 @@ include $(top_srcdir)/flymake.am AUTOMAKE_OPTIONS = 1.6 foreign -EXTRA_DIST = lib.h lib_ascii.h lib_base64.h lib_date.h lib_exit.h lib_md5.h +EXTRA_DIST = lib.h lib_ascii.h lib_base64.h lib_date.h lib_exit.h lib_md5.h lib_sha1.h AM_CPPFLAGS = -I$(top_srcdir) noinst_LIBRARIES = liblib.a noinst_HEADERS = -liblib_a_SOURCES = lib_ascii.c lib_base64.c lib_date.c lib_exit.c lib_md5.c +liblib_a_SOURCES = lib_ascii.c lib_base64.c lib_date.c lib_exit.c lib_md5.c lib_sha1.c diff --git a/lib/lib.h b/lib/lib.h index 87a1d3309..04993dc04 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -33,6 +33,7 @@ * -# @subpage date * -# @subpage exit * -# @subpage md5 + * -# @subpage sha1 */ #ifndef _LIB_LIB_H @@ -43,5 +44,6 @@ #include "lib_date.h" #include "lib_exit.h" #include "lib_md5.h" +#include "lib_sha1.h" #endif /* _LIB_LIB_H */ diff --git a/sha1.c b/lib/lib_sha1.c similarity index 83% rename from sha1.c rename to lib/lib_sha1.c index 103be4edb..291cc68c8 100644 --- a/sha1.c +++ b/lib/lib_sha1.c @@ -2,25 +2,35 @@ * @file * Calculate the SHA1 checksum of a buffer * - * SHA-1 in C + * @authors + * Steve Reid + * Thomas Roessler * - * By Steve Reid , with small changes to make it - * fit into mutt by Thomas Roessler . + * @copyright + * Public Domain + */ + +/** + * @page sha1 Calculate the SHA1 checksum of a buffer + * + * Calculate the SHA1 cryptographic hash of a string, according to RFC3174. * - * 100% Public Domain. + * | Function | Description + * | :--------------- | :---------------------------------------- + * | sha1_final() | Add padding and return the message digest + * | sha1_init() | Initialize new context + * | sha1_transform() | Hash a single 512-bit block + * | sha1_update() | Run your data through this * - * Test Vectors (from FIPS PUB 180-1) - * "abc" - * A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D - * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" - * 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 - * A million repetitions of "a" - * 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F + * Test Vectors (from FIPS PUB 180-1): + * - "abc" yields `A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D` + * - "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" yields `84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1` + * - A million repetitions of "a" yields `34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F` */ #include "config.h" #include -#include "sha1.h" +#include "lib_sha1.h" #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) @@ -55,9 +65,10 @@ z += (w ^ x ^ y) + blk(i) + 0xCA62C1D6 + rol(v, 5); \ w = rol(w, 30); - /** * sha1_transform - Hash a single 512-bit block + * @param state Internal state of the transform + * @param buffer Data to transform * * This is the core of the algorithm. */ @@ -168,9 +179,9 @@ void sha1_transform(uint32_t state[5], const unsigned char buffer[64]) memset(block, '\0', sizeof(block)); } - /** * sha1_init - Initialize new context + * @param context SHA1 context */ void sha1_init(struct Sha1Ctx *context) { @@ -183,9 +194,11 @@ void sha1_init(struct Sha1Ctx *context) context->count[0] = context->count[1] = 0; } - /** * sha1_update - Run your data through this + * @param context SHA1 context + * @param data Data to be hashed + * @param len Length of data */ void sha1_update(struct Sha1Ctx *context, const unsigned char *data, uint32_t len) { @@ -212,9 +225,10 @@ void sha1_update(struct Sha1Ctx *context, const unsigned char *data, uint32_t le memcpy(&context->buffer[j], &data[i], len - i); } - /** * sha1_final - Add padding and return the message digest + * @param[out] digest Message digest (SHA1 sum) + * @param[in] context SHA1 context */ void sha1_final(unsigned char digest[20], struct Sha1Ctx *context) { diff --git a/lib/lib_sha1.h b/lib/lib_sha1.h new file mode 100644 index 000000000..8a36ac23b --- /dev/null +++ b/lib/lib_sha1.h @@ -0,0 +1,47 @@ +/** + * @file + * Calculate the SHA1 checksum of a buffer + * + * @authors + * Copyright (C) 2000 Steve Reid + * Copyright (C) 2000 Thomas Roessler + * Copyright (C) 2017 Richard Russon + * + * @copyright + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation, either version 2 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#ifndef _LIB_SHA1_H +#define _LIB_SHA1_H + +#include + +/** + * struct Sha1Ctx - Cursor for the SHA1 hashing + */ +struct Sha1Ctx +{ + uint32_t state[5]; + uint32_t count[2]; + unsigned char buffer[64]; +}; + +void sha1_final(unsigned char digest[20], struct Sha1Ctx *context); +void sha1_init(struct Sha1Ctx *context); +void sha1_transform(uint32_t state[5], const unsigned char buffer[64]); +void sha1_update(struct Sha1Ctx *context, const unsigned char *data, uint32_t len); + +#define SHA_DIGEST_LENGTH 20 + +#endif /* _LIB_SHA1_H */ diff --git a/pgppubring.c b/pgppubring.c index 394e1e6ec..3d2081ae4 100644 --- a/pgppubring.c +++ b/pgppubring.c @@ -47,7 +47,6 @@ #include "ncrypt/ncrypt.h" #include "ncrypt/pgplib.h" #include "ncrypt/pgppacket.h" -#include "sha1.h" extern char *optarg; extern int optind; diff --git a/po/POTFILES.in b/po/POTFILES.in index 43aed0e20..13c3944c5 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -64,6 +64,7 @@ lib/lib_base64.c lib/lib_date.c lib/lib_exit.c lib/lib_md5.c +lib/lib_sha1.c main.c mbox.c mbyte.c @@ -120,7 +121,6 @@ safe_asprintf.c score.c send.c sendlib.c -sha1.c sidebar.c signal.c smtp.c diff --git a/sha1.h b/sha1.h deleted file mode 100644 index 4760064ad..000000000 --- a/sha1.h +++ /dev/null @@ -1,33 +0,0 @@ -/** - * @file - * Calculate the SHA1 checksum of a buffer - * - * SHA-1 in C - * - * By Steve Reid , with small changes to make it - * fit into mutt by Thomas Roessler . - */ - -#ifndef _MUTT_SHA1_H -#define _MUTT_SHA1_H - -#include - -/** - * struct Sha1Ctx - Cursor for the SHA1 hashing - */ -struct Sha1Ctx -{ - uint32_t state[5]; - uint32_t count[2]; - unsigned char buffer[64]; -}; - -void sha1_transform(uint32_t state[5], const unsigned char buffer[64]); -void sha1_init(struct Sha1Ctx *context); -void sha1_update(struct Sha1Ctx *context, const unsigned char *data, uint32_t len); -void sha1_final(unsigned char digest[20], struct Sha1Ctx *context); - -#define SHA_DIGEST_LENGTH 20 - -#endif /* _MUTT_SHA1_H */