static char *hmacpw(const char *pw, const char *hash) { int i; for (i=0; hmac_list[i] && strcmp(hmac_list[i]->hh_name, hash); i++) ; if (hmac_list[i]) { struct hmac_hashinfo *hmac=hmac_list[i]; unsigned char *p=malloc(hmac->hh_L*2); char *q=malloc(hmac->hh_L*4+1); unsigned i; if (!p || !q) { perror("malloc"); exit(1); } hmac_hashkey(hmac, pw, strlen(pw), p, p+hmac->hh_L); for (i=0; i<hmac->hh_L*2; i++) sprintf(q+i*2, "%02x", (int)p[i]); free(p); return (q); } return (NULL); }
static char *mktoken(const char *hash, const char *t, time_t now) { char now_s[NUMBUFSIZE]; char *p; unsigned char *q; int i; char *r; libmail_strh_time_t(now, now_s); p=malloc(strlen(hash)+strlen(now_s)+3+hmac_sha1.hh_L*2); if (!p) return (NULL); strcat(strcpy(p, hash), now_s); q=malloc(hmac_sha1.hh_L*3); if (!q) { free(p); return (NULL); } hmac_hashkey(&hmac_sha1, t, sizeof(cur_token), q, q + hmac_sha1.hh_L); hmac_hashtext(&hmac_sha1, p, strlen(p), q, q + hmac_sha1.hh_L, q + hmac_sha1.hh_L*2); strcpy(p, now_s); r=p + strlen(p); *r++='-'; for (i=0; i<hmac_sha1.hh_L; i++) { int c=(unsigned char)q[hmac_sha1.hh_L*2+i]; *r++ = xdigit[c / 16]; *r++ = xdigit[c % 16]; } *r=0; free(q); return (p); }
std::string mail::imaphmac::operator()(std::string password, std::string challenge) const { std::string i, o, b; i.insert(i.end(), hmac.hh_L, (char)0); o.insert(o.end(), hmac.hh_L, (char)0); b.insert(b.end(), hmac.hh_L, (char)0); hmac_hashkey( &hmac, &*password.begin(), password.size(), (unsigned char *)&*o.begin(), (unsigned char *)&*i.begin()); hmac_hashtext( &hmac, &*challenge.begin(), challenge.size(), (unsigned char *)&*o.begin(), (unsigned char *)&*i.begin(), (unsigned char *)&*b.begin()); return (b); }
int main() { unsigned i, j; for (i=0; i<sizeof(testcases)/sizeof(testcases[0]); i++) { SHA1_DIGEST ok, ik, hash; hmac_hashkey(&hmac_sha1, testcases[i].key, strlen(testcases[i].key), ok, ik); hmac_hashtext(&hmac_sha1, testcases[i].data, strlen(testcases[i].data), ok, ik, hash); printf("test_cases[%i].digest=0x", i+1); for (j=0; j<SHA1_DIGEST_SIZE; j++) printf("%02x", hash[j]); printf("\n"); } return (0); }