krb5: Red Hat gssproxy FILE ccache remove cred compatibility
[heimdal.git] / lib / krb5 / sp800-108-kdf.c
blob4a12067c68bdeae689bb4b2b40b52699f75c9264
1 /*
2 * Copyright (c) 2015, Secure Endpoints Inc.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
15 * distribution.
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"
35 * SP800-108 KDF
38 /**
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
51 krb5_error_code
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,
56 const EVP_MD *md,
57 krb5_data *kdf_K0)
59 HMAC_CTX c;
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");
68 HMAC_CTX_init(&c);
70 n = L / h;
72 for (i = 0; i <= n; i++) {
73 unsigned char tmp[4];
74 size_t len;
76 if (HMAC_Init_ex(&c, kdf_K1->data, kdf_K1->length, md, NULL) == 0) {
77 HMAC_CTX_cleanup(&c);
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);
85 if (kdf_context)
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;
92 memcpy(p, hmac, len);
93 p += len;
94 left -= len;
97 HMAC_CTX_cleanup(&c);
99 return 0;