#include "crypto_int.h"
krb5_error_code krb5int_hmacmd5_checksum(const struct krb5_cksumtypes *ctp,
krb5_key key, krb5_keyusage usage,
const krb5_crypto_iov *data,
size_t num_data,
krb5_data *output)
{
krb5_keyusage ms_usage;
krb5_error_code ret;
krb5_keyblock ks, *keyblock;
krb5_crypto_iov *hash_iov = NULL, iov;
krb5_data ds = empty_data(), hashval = empty_data();
char t[4];
if (key == NULL || key->keyblock.length > ctp->hash->blocksize)
return KRB5_BAD_ENCTYPE;
if (ctp->ctype == CKSUMTYPE_HMAC_MD5_ARCFOUR) {
ret = alloc_data(&ds, ctp->hash->hashsize);
if (ret != 0)
goto cleanup;
iov.flags = KRB5_CRYPTO_TYPE_DATA;
iov.data = make_data("signaturekey", 13);
ret = krb5int_hmac(ctp->hash, key, &iov, 1, &ds);
if (ret)
goto cleanup;
ks.length = ds.length;
ks.contents = (krb5_octet *) ds.data;
keyblock = &ks;
} else
keyblock = &key->keyblock;
ms_usage = krb5int_arcfour_translate_usage(usage);
store_32_le(ms_usage, t);
hash_iov = k5calloc(num_data + 1, sizeof(krb5_crypto_iov), &ret);
if (hash_iov == NULL)
goto cleanup;
hash_iov[0].flags = KRB5_CRYPTO_TYPE_DATA;
hash_iov[0].data = make_data(t, 4);
memcpy(hash_iov + 1, data, num_data * sizeof(krb5_crypto_iov));
ret = alloc_data(&hashval, ctp->hash->hashsize);
if (ret != 0)
goto cleanup;
ret = ctp->hash->hash(hash_iov, num_data + 1, &hashval);
if (ret != 0)
goto cleanup;
iov.flags = KRB5_CRYPTO_TYPE_DATA;
iov.data = hashval;
ret = krb5int_hmac_keyblock(ctp->hash, keyblock, &iov, 1, output);
cleanup:
zapfree(ds.data, ds.length);
zapfree(hashval.data, hashval.length);
free(hash_iov);
return ret;
}
krb5_error_code
k5_hmac_md5(const krb5_data *key, const krb5_crypto_iov *data, size_t num_data,
krb5_data *output)
{
krb5_error_code ret;
const struct krb5_hash_provider *hash = &krb5int_hash_md5;
krb5_keyblock keyblock = { 0 };
krb5_data hashed_key;
uint8_t hkeybuf[16];
krb5_crypto_iov iov;
if (key->length > hash->blocksize) {
hashed_key = make_data(hkeybuf, sizeof(hkeybuf));
iov.flags = KRB5_CRYPTO_TYPE_DATA;
iov.data = *key;
ret = hash->hash(&iov, 1, &hashed_key);
if (ret)
return ret;
key = &hashed_key;
}
keyblock.magic = KV5M_KEYBLOCK;
keyblock.length = key->length;
keyblock.contents = (uint8_t *)key->data;
return krb5int_hmac_keyblock(hash, &keyblock, data, num_data, output);
}