1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/crypto/krb/checksum_hmac_md5.c */
3 /*
4 * Copyright (C) 2009 by the Massachusetts Institute of Technology.
5 * All rights reserved.
6 *
7 * Export of this software from the United States of America may
8 * require a specific license from the United States Government.
9 * It is the responsibility of any person or organization contemplating
10 * export to obtain such a license before exporting.
11 *
12 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13 * distribute this software and its documentation for any purpose and
14 * without fee is hereby granted, provided that the above copyright
15 * notice appear in all copies and that both that copyright notice and
16 * this permission notice appear in supporting documentation, and that
17 * the name of M.I.T. not be used in advertising or publicity pertaining
18 * to distribution of the software without specific, written prior
19 * permission. Furthermore if you modify this software you must label
20 * your software as modified software and not distribute it in such a
21 * fashion that it might be confused with the original M.I.T. software.
22 * M.I.T. makes no representations about the suitability of
23 * this software for any purpose. It is provided "as is" without express
24 * or implied warranty.
25 */
26
27 /*
28 * Microsoft HMAC-MD5 and MD5-HMAC checksums (see RFC 4757):
29 * HMAC(KS, hash(msusage || input))
30 * KS is HMAC(key, "signaturekey\0") for HMAC-MD5, or just the key for
31 * MD5-HMAC.
32 */
33
34 #include "crypto_int.h"
35
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)36 krb5_error_code krb5int_hmacmd5_checksum(const struct krb5_cksumtypes *ctp,
37 krb5_key key, krb5_keyusage usage,
38 const krb5_crypto_iov *data,
39 size_t num_data,
40 krb5_data *output)
41 {
42 krb5_keyusage ms_usage;
43 krb5_error_code ret;
44 krb5_keyblock ks, *keyblock;
45 krb5_crypto_iov *hash_iov = NULL, iov;
46 krb5_data ds = empty_data(), hashval = empty_data();
47 char t[4];
48
49 if (key == NULL || key->keyblock.length > ctp->hash->blocksize)
50 return KRB5_BAD_ENCTYPE;
51 if (ctp->ctype == CKSUMTYPE_HMAC_MD5_ARCFOUR) {
52 /* Compute HMAC(key, "signaturekey\0") to get the signing key ks. */
53 ret = alloc_data(&ds, ctp->hash->hashsize);
54 if (ret != 0)
55 goto cleanup;
56
57 iov.flags = KRB5_CRYPTO_TYPE_DATA;
58 iov.data = make_data("signaturekey", 13);
59 ret = krb5int_hmac(ctp->hash, key, &iov, 1, &ds);
60 if (ret)
61 goto cleanup;
62 ks.length = ds.length;
63 ks.contents = (krb5_octet *) ds.data;
64 keyblock = &ks;
65 } else /* For md5-hmac, just use the key. */
66 keyblock = &key->keyblock;
67
68 /* Compute the MD5 value of the input. */
69 ms_usage = krb5int_arcfour_translate_usage(usage);
70 store_32_le(ms_usage, t);
71 hash_iov = k5calloc(num_data + 1, sizeof(krb5_crypto_iov), &ret);
72 if (hash_iov == NULL)
73 goto cleanup;
74 hash_iov[0].flags = KRB5_CRYPTO_TYPE_DATA;
75 hash_iov[0].data = make_data(t, 4);
76 memcpy(hash_iov + 1, data, num_data * sizeof(krb5_crypto_iov));
77 ret = alloc_data(&hashval, ctp->hash->hashsize);
78 if (ret != 0)
79 goto cleanup;
80 ret = ctp->hash->hash(hash_iov, num_data + 1, &hashval);
81 if (ret != 0)
82 goto cleanup;
83
84 /* Compute HMAC(ks, md5value). */
85 iov.flags = KRB5_CRYPTO_TYPE_DATA;
86 iov.data = hashval;
87 ret = krb5int_hmac_keyblock(ctp->hash, keyblock, &iov, 1, output);
88
89 cleanup:
90 zapfree(ds.data, ds.length);
91 zapfree(hashval.data, hashval.length);
92 free(hash_iov);
93 return ret;
94 }
95
96 krb5_error_code
k5_hmac_md5(const krb5_data * key,const krb5_crypto_iov * data,size_t num_data,krb5_data * output)97 k5_hmac_md5(const krb5_data *key, const krb5_crypto_iov *data, size_t num_data,
98 krb5_data *output)
99 {
100 krb5_error_code ret;
101 const struct krb5_hash_provider *hash = &krb5int_hash_md5;
102 krb5_keyblock keyblock = { 0 };
103 krb5_data hashed_key;
104 uint8_t hkeybuf[16];
105 krb5_crypto_iov iov;
106
107 /* Hash the key if it is longer than the block size. */
108 if (key->length > hash->blocksize) {
109 hashed_key = make_data(hkeybuf, sizeof(hkeybuf));
110 iov.flags = KRB5_CRYPTO_TYPE_DATA;
111 iov.data = *key;
112 ret = hash->hash(&iov, 1, &hashed_key);
113 if (ret)
114 return ret;
115 key = &hashed_key;
116 }
117
118 keyblock.magic = KV5M_KEYBLOCK;
119 keyblock.length = key->length;
120 keyblock.contents = (uint8_t *)key->data;
121 return krb5int_hmac_keyblock(hash, &keyblock, data, num_data, output);
122 }
123