]> granicus.if.org Git - neomutt/commitdiff
MD5 - Add and use an API to process a NULL-terminated string
authorPietro Cerutti <gahr@gahr.ch>
Wed, 24 Jan 2018 12:17:35 +0000 (12:17 +0000)
committerRichard Russon <rich@flatcap.org>
Wed, 24 Jan 2018 18:39:57 +0000 (18:39 +0000)
hcache/hcache.c
imap/auth_cram.c
mutt/md5.c
mutt/md5.h
pop_auth.c
test/main.c
test/md5.c

index 886503652385702bcd9485bdae0d0d564b7d1d0a..08d7e7808d7762b644e619eb50482f4327a4fab0 100644 (file)
@@ -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 */
index a56a5e1884b07aaab5898b0d9ddadd8c967ba70b..6d0baf95b2eb5d61cb60407e97dd4f7cb6280e58 100644 (file)
@@ -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 */
index 792207a6ba21a94868f821c89404d065897f38ed..86b22577458b79b98231cf2bf8d4c8cf32a19a7f 100644 (file)
@@ -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
index ecdfc2a84886782bee5735796cf5620098643ade..e028eb153312a8b9fcb4586d226b28c19b59102f 100644 (file)
@@ -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 */
index fa93859594fb8a822475ad88c70251fb74b012af..e7999332b5bc6d784d2eda179ec360a869511328 100644 (file)
@@ -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);
 
index eaeb5580a909934b1cbba6be4ad344b250d6f8ed..d517d5815d258c9ca371d2a7bc372d0af344b44b 100644 (file)
@@ -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)
 
 /******************************************************************************
index 544968d230f805f0acc7ffaad652aa04091a4346..5ff83fef89f11f911afe6184ae3433bd6e29b0a6 100644 (file)
@@ -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)