From: Pietro Cerutti Date: Wed, 24 Jan 2018 12:17:35 +0000 (+0000) Subject: MD5 - Add and use an API to process a NULL-terminated string X-Git-Tag: neomutt-20180223~31^2~2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d0d142e1b4dfb47630b2b2e3a13e61429762f7ed;p=neomutt MD5 - Add and use an API to process a NULL-terminated string --- diff --git a/hcache/hcache.c b/hcache/hcache.c index 886503652..08d7e7808 100644 --- a/hcache/hcache.c +++ b/hcache/hcache.c @@ -754,14 +754,14 @@ header_cache_t *mutt_hcache_open(const char *path, const char *folder, hcache_na /* Mix in user's spam list */ for (spam = SpamList; spam; spam = spam->next) { - mutt_md5_process_bytes(spam->regex->pattern, strlen(spam->regex->pattern), &ctx); - mutt_md5_process_bytes(spam->template, strlen(spam->template), &ctx); + mutt_md5_process(spam->regex->pattern, &ctx); + mutt_md5_process(spam->template, &ctx); } /* Mix in user's nospam list */ for (nospam = NoSpamList; nospam; nospam = nospam->next) { - mutt_md5_process_bytes(nospam->regex->pattern, strlen(nospam->regex->pattern), &ctx); + mutt_md5_process(nospam->regex->pattern, &ctx); } /* Get a hash and take its bytes as an (unsigned int) hash version */ diff --git a/imap/auth_cram.c b/imap/auth_cram.c index a56a5e188..6d0baf95b 100644 --- a/imap/auth_cram.c +++ b/imap/auth_cram.c @@ -58,10 +58,9 @@ static void hmac_md5(const char *password, char *challenge, unsigned char *respo unsigned char ipad[MD5_BLOCK_LEN], opad[MD5_BLOCK_LEN]; unsigned char secret[MD5_BLOCK_LEN + 1]; unsigned char hash_passwd[MD5_DIGEST_LEN]; - size_t secret_len, chal_len; + size_t secret_len; secret_len = strlen(password); - chal_len = strlen(challenge); /* passwords longer than MD5_BLOCK_LEN bytes are substituted with their MD5 * digests */ @@ -88,7 +87,7 @@ static void hmac_md5(const char *password, char *challenge, unsigned char *respo /* inner hash: challenge and ipadded secret */ mutt_md5_init_ctx(&ctx); mutt_md5_process_bytes(ipad, MD5_BLOCK_LEN, &ctx); - mutt_md5_process_bytes(challenge, chal_len, &ctx); + mutt_md5_process(challenge, &ctx); mutt_md5_finish_ctx(&ctx, response); /* outer hash: inner hash and opadded secret */ diff --git a/mutt/md5.c b/mutt/md5.c index 792207a6b..86b225774 100644 --- a/mutt/md5.c +++ b/mutt/md5.c @@ -33,6 +33,7 @@ * | mutt_md5_init_ctx() | Initialise the MD5 computation * | mutt_md5_process_block() | Process a block with MD5 * | mutt_md5_process_bytes() | Process a block of data + * | mutt_md5_process() | Process a string * | mutt_md5_read_ctx() | Read from the context into a buffer * | mutt_md5_toascii() | Produce an ASCII MD5 digest from a buffer */ @@ -339,6 +340,16 @@ void *mutt_md5_buf(const char *buffer, size_t len, void *resbuf) return mutt_md5_finish_ctx(&ctx, resbuf); } +/** + * mutt_md5_process - Process a NULL-terminated string + * @param s String to process + * @param ctx MD5 context + */ +void mutt_md5_process(const char *s, struct Md5Ctx *ctx) +{ + mutt_md5_process_bytes(s, strlen(s), ctx); +} + /** * mutt_md5_process_bytes - Process a block of data * @param buffer Buffer to process diff --git a/mutt/md5.h b/mutt/md5.h index ecdfc2a84..e028eb153 100644 --- a/mutt/md5.h +++ b/mutt/md5.h @@ -49,6 +49,7 @@ void *mutt_md5_buf(const char *buffer, size_t len, void *resbuf); void *mutt_md5_finish_ctx(struct Md5Ctx *ctx, void *resbuf); void mutt_md5_init_ctx(struct Md5Ctx *ctx); void mutt_md5_process_bytes(const void *buffer, size_t len, struct Md5Ctx *ctx); +void mutt_md5_process(const char *s, struct Md5Ctx *ctx); void mutt_md5_toascii(const void *resbuf, char *digest); #endif /* _MUTT_MD5_H */ diff --git a/pop_auth.c b/pop_auth.c index fa9385959..e7999332b 100644 --- a/pop_auth.c +++ b/pop_auth.c @@ -233,9 +233,8 @@ static enum PopAuthRes pop_auth_apop(struct PopData *pop_data, const char *metho /* Compute the authentication hash to send to the server */ mutt_md5_init_ctx(&ctx); - mutt_md5_process_bytes(pop_data->timestamp, strlen(pop_data->timestamp), &ctx); - mutt_md5_process_bytes(pop_data->conn->account.pass, - strlen(pop_data->conn->account.pass), &ctx); + mutt_md5_process(pop_data->timestamp, &ctx); + mutt_md5_process(pop_data->conn->account.pass, &ctx); mutt_md5_finish_ctx(&ctx, digest); mutt_md5_toascii(digest, hash); diff --git a/test/main.c b/test/main.c index eaeb5580a..d517d5815 100644 --- a/test/main.c +++ b/test/main.c @@ -9,6 +9,7 @@ NEOMUTT_TEST_ITEM(test_base64_lengths) \ NEOMUTT_TEST_ITEM(test_rfc2047) \ NEOMUTT_TEST_ITEM(test_md5) \ + NEOMUTT_TEST_ITEM(test_md5_bytes) \ NEOMUTT_TEST_ITEM(test_md5_buffer) /****************************************************************************** diff --git a/test/md5.c b/test/md5.c index 544968d23..5ff83fef8 100644 --- a/test/md5.c +++ b/test/md5.c @@ -22,7 +22,7 @@ static const struct }; /* clang-format on */ -void test_md5(void) +void test_md5_bytes(void) { for (size_t i = 0; i < mutt_array_size(test_data); ++i) { @@ -42,6 +42,26 @@ void test_md5(void) } } +void test_md5(void) +{ + for (size_t i = 0; i < mutt_array_size(test_data); ++i) + { + struct Md5Ctx ctx; + unsigned char buf[16]; + char digest[33]; + mutt_md5_init_ctx(&ctx); + mutt_md5_process(test_data[i].text, &ctx); + mutt_md5_finish_ctx(&ctx, buf); + mutt_md5_toascii(buf, digest); + if (!TEST_CHECK(strcmp(test_data[i].hash, digest) == 0)) + { + TEST_MSG("Iteration: %zu", i); + TEST_MSG("Expected : %s", test_data[i].hash); + TEST_MSG("Actual : %s", digest); + } + } +} + void test_md5_buffer(void) { for (size_t i = 0; i < mutt_array_size(test_data); ++i)