2 * Copyright (c) 2015, Secure Endpoints Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "krb5_locl.h"
39 * As described in SP800-108 5.1 (for HMAC)
41 * @param context Kerberos 5 context
42 * @param kdf_K1 Base key material.
43 * @param kdf_label A string that identifies the purpose for the derived key.
44 * @param kdf_context A binary string containing parties, nonce, etc.
45 * @param md Message digest function to use for PRF.
46 * @param kdf_K0 Derived key data.
48 * @return Return an error code for an failure or 0 on success.
49 * @ingroup krb5_crypto
52 _krb5_SP800_108_HMAC_KDF(krb5_context context
,
53 const krb5_data
*kdf_K1
,
54 const krb5_data
*kdf_label
,
55 const krb5_data
*kdf_context
,
60 unsigned char *p
= kdf_K0
->data
;
61 size_t i
, n
, left
= kdf_K0
->length
;
62 unsigned char hmac
[EVP_MAX_MD_SIZE
];
63 unsigned int h
= EVP_MD_size(md
);
64 const size_t L
= kdf_K0
->length
;
66 heim_assert(md
!= NULL
, "SP800-108 KDF internal error");
72 for (i
= 0; i
<= n
; i
++) {
76 if (HMAC_Init_ex(&c
, kdf_K1
->data
, kdf_K1
->length
, md
, NULL
) == 0) {
78 return krb5_enomem(context
);
81 _krb5_put_int(tmp
, i
+ 1, 4);
82 HMAC_Update(&c
, tmp
, 4);
83 HMAC_Update(&c
, kdf_label
->data
, kdf_label
->length
);
84 HMAC_Update(&c
, (unsigned char *)"", 1);
86 HMAC_Update(&c
, kdf_context
->data
, kdf_context
->length
);
87 _krb5_put_int(tmp
, L
* 8, 4);
88 HMAC_Update(&c
, tmp
, 4);
90 HMAC_Final(&c
, hmac
, &h
);
91 len
= h
> left
? left
: h
;