Skip to content

Commit

Permalink
merge: md5 improvements
Browse files Browse the repository at this point in the history
 * Remove unused functions, unexpose implementation functions
 * Implement and use mutt_md5_toascii
 * Initial tests
 * Use mutt_md5_toascii some more
 * Add and use an API to process a NULL-terminated string
 * More API polishing
 * Rename test cases to better reflect what they're doing
  • Loading branch information
flatcap committed Jan 24, 2018
2 parents 9f7a252 + 454a262 commit 1ee137f
Show file tree
Hide file tree
Showing 8 changed files with 315 additions and 285 deletions.
14 changes: 5 additions & 9 deletions hcache/hcache.c
Original file line number Diff line number Diff line change
Expand Up @@ -606,12 +606,8 @@ static const char *hcache_per_folder(const char *path, const char *folder, hcach
unsigned char m[16]; /* binary md5sum */
char name[_POSIX_PATH_MAX];
snprintf(name, sizeof(name), "%s|%s", hcache_get_ops()->name, folder);
mutt_md5_buf(name, strlen(name), &m);
snprintf(name, sizeof(name),
"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8], m[9], m[10],
m[11], m[12], m[13], m[14], m[15]);

mutt_md5(name, m);
mutt_md5_toascii(m, name);
rc = snprintf(hcpath, sizeof(hcpath), "%s%s%s%s", path, slash ? "" : "/", name, suffix);
}

Expand Down Expand Up @@ -758,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 */
Expand Down
17 changes: 5 additions & 12 deletions imap/auth_cram.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,15 @@ 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 */
if (secret_len > MD5_BLOCK_LEN)
{
mutt_md5_buf(password, secret_len, hash_passwd);
mutt_md5_bytes(password, secret_len, hash_passwd);
mutt_str_strfcpy((char *) secret, (char *) hash_passwd, MD5_DIGEST_LEN);
secret_len = MD5_DIGEST_LEN;
}
Expand All @@ -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 */
Expand Down Expand Up @@ -163,14 +162,8 @@ enum ImapAuthRes imap_auth_cram_md5(struct ImapData *idata, const char *method)
*/
hmac_md5(idata->conn->account.pass, obuf, hmac_response);
/* dubious optimisation I saw elsewhere: make the whole string in one call */
snprintf(
obuf, sizeof(obuf),
"%s %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
idata->conn->account.user, hmac_response[0], hmac_response[1],
hmac_response[2], hmac_response[3], hmac_response[4], hmac_response[5],
hmac_response[6], hmac_response[7], hmac_response[8], hmac_response[9],
hmac_response[10], hmac_response[11], hmac_response[12],
hmac_response[13], hmac_response[14], hmac_response[15]);
int off = snprintf(obuf, sizeof(obuf), "%s ", idata->conn->account.user);
mutt_md5_toascii(hmac_response, obuf + off);
mutt_debug(2, "CRAM response: %s\n", obuf);

/* ibuf must be long enough to store the base64 encoding of obuf,
Expand Down
Loading

0 comments on commit 1ee137f

Please sign in to comment.