Blame SOURCES/Use-OpenSSL-s-KBKDF-and-KRB5KDF-for-deriving-long-te.patch

cb4cef
From 21e3b9a4463f1d1aeb71de8a27c298f1307d186b Mon Sep 17 00:00:00 2001
cb4cef
From: Robbie Harwood <rharwood@redhat.com>
cb4cef
Date: Fri, 4 Oct 2019 14:49:29 -0400
cb4cef
Subject: [PATCH] Use OpenSSL's KBKDF and KRB5KDF for deriving long-term keys
cb4cef
cb4cef
If supported, use OpenSSL-provided KBKDF (aes-sha2 and camellia) and
cb4cef
KRB5KDF (3des and aes-sha1).  We already use OpenSSL's PBKDF2 where
cb4cef
appropriate.  OpenSSL added support for these KDFs in 3.0.
cb4cef
cb4cef
(cherry picked from commit ef8d11f6fb1232201c9efd2ae2ed567023fb85d2)
cb4cef
[rharwood@redhat.com: 3des removal]
cb4cef
---
cb4cef
 src/lib/crypto/krb/derive.c | 409 ++++++++++++++++++++++++++++--------
cb4cef
 1 file changed, 324 insertions(+), 85 deletions(-)
cb4cef
cb4cef
diff --git a/src/lib/crypto/krb/derive.c b/src/lib/crypto/krb/derive.c
cb4cef
index 6707a7308..8e474b38e 100644
cb4cef
--- a/src/lib/crypto/krb/derive.c
cb4cef
+++ b/src/lib/crypto/krb/derive.c
cb4cef
@@ -27,6 +27,12 @@
cb4cef
 
cb4cef
 #include "crypto_int.h"
cb4cef
 
cb4cef
+#ifdef HAVE_EVP_KDF_FETCH
cb4cef
+#include <openssl/core_names.h>
cb4cef
+#include <openssl/evp.h>
cb4cef
+#include <openssl/kdf.h>
cb4cef
+#endif
cb4cef
+
cb4cef
 static krb5_key
cb4cef
 find_cached_dkey(struct derived_key *list, const krb5_data *constant)
cb4cef
 {
cb4cef
@@ -77,55 +83,251 @@ cleanup:
cb4cef
     return ENOMEM;
cb4cef
 }
cb4cef
 
cb4cef
+#ifdef HAVE_EVP_KDF_FETCH
cb4cef
 static krb5_error_code
cb4cef
-derive_random_rfc3961(const struct krb5_enc_provider *enc,
cb4cef
-                      krb5_key inkey, krb5_data *outrnd,
cb4cef
-                      const krb5_data *in_constant)
cb4cef
+openssl_kbdkf_counter_hmac(const struct krb5_hash_provider *hash,
cb4cef
+                           krb5_key inkey, krb5_data *outrnd,
cb4cef
+                           const krb5_data *label, const krb5_data *context)
cb4cef
 {
cb4cef
-    size_t blocksize, keybytes, n;
cb4cef
     krb5_error_code ret;
cb4cef
-    krb5_data block = empty_data();
cb4cef
+    EVP_KDF *kdf = NULL;
cb4cef
+    EVP_KDF_CTX *kctx = NULL;
cb4cef
+    OSSL_PARAM params[6];
cb4cef
+    size_t i = 0;
cb4cef
+    char *digest;
cb4cef
 
cb4cef
-    blocksize = enc->block_size;
cb4cef
-    keybytes = enc->keybytes;
cb4cef
+    /* On NULL hash, preserve default behavior for pbkdf2_string_to_key(). */
cb4cef
+    if (hash == NULL || !strcmp(hash->hash_name, "SHA1")) {
cb4cef
+        digest = "SHA1";
cb4cef
+    } else if (!strcmp(hash->hash_name, "SHA-256")) {
cb4cef
+        digest = "SHA256";
cb4cef
+    } else if (!strcmp(hash->hash_name, "SHA-384")) {
cb4cef
+        digest = "SHA384";
cb4cef
+    } else {
cb4cef
+        ret = KRB5_CRYPTO_INTERNAL;
cb4cef
+        goto done;
cb4cef
+    }
cb4cef
 
cb4cef
-    if (blocksize == 1)
cb4cef
-        return KRB5_BAD_ENCTYPE;
cb4cef
-    if (inkey->keyblock.length != enc->keylength || outrnd->length != keybytes)
cb4cef
+    kdf = EVP_KDF_fetch(NULL, "KBKDF", NULL);
cb4cef
+    if (!kdf) {
cb4cef
+        ret = KRB5_CRYPTO_INTERNAL;
cb4cef
+        goto done;
cb4cef
+    }
cb4cef
+
cb4cef
+    kctx = EVP_KDF_CTX_new(kdf);
cb4cef
+    if (!kctx) {
cb4cef
+        ret = KRB5_CRYPTO_INTERNAL;
cb4cef
+        goto done;
cb4cef
+    }
cb4cef
+
cb4cef
+    params[i++] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
cb4cef
+                                                   digest, 0);
cb4cef
+    params[i++] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_MAC,
cb4cef
+                                                   "HMAC", 0);
cb4cef
+    params[i++] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
cb4cef
+                                                    inkey->keyblock.contents,
cb4cef
+                                                    inkey->keyblock.length);
cb4cef
+    params[i++] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO,
cb4cef
+                                                    context->data,
cb4cef
+                                                    context->length);
cb4cef
+    params[i++] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
cb4cef
+                                                    label->data,
cb4cef
+                                                    label->length);
cb4cef
+    params[i] = OSSL_PARAM_construct_end();
cb4cef
+    if (EVP_KDF_derive(kctx, (unsigned char *)outrnd->data, outrnd->length,
cb4cef
+                       params) <= 0) {
cb4cef
+        ret = KRB5_CRYPTO_INTERNAL;
cb4cef
+        goto done;
cb4cef
+    }
cb4cef
+
cb4cef
+    ret = 0;
cb4cef
+done:
cb4cef
+    if (ret)
cb4cef
+        zap(outrnd->data, outrnd->length);
cb4cef
+    EVP_KDF_free(kdf);
cb4cef
+    EVP_KDF_CTX_free(kctx);
cb4cef
+    return ret;
cb4cef
+}
cb4cef
+
cb4cef
+static krb5_error_code
cb4cef
+openssl_kbkdf_feedback_cmac(const struct krb5_enc_provider *enc,
cb4cef
+                            krb5_key inkey, krb5_data *outrnd,
cb4cef
+                            const krb5_data *in_constant)
cb4cef
+{
cb4cef
+    krb5_error_code ret;
cb4cef
+    EVP_KDF *kdf = NULL;
cb4cef
+    EVP_KDF_CTX *kctx = NULL;
cb4cef
+    OSSL_PARAM params[7];
cb4cef
+    size_t i = 0;
cb4cef
+    char *cipher;
cb4cef
+    static unsigned char zeroes[16];
cb4cef
+
cb4cef
+    memset(zeroes, 0, sizeof(zeroes));
cb4cef
+
cb4cef
+    if (!memcmp(enc, &krb5int_enc_camellia128, sizeof(*enc))) {
cb4cef
+        cipher = "CAMELLIA-128-CBC";
cb4cef
+    } else if (!memcmp(enc, &krb5int_enc_camellia256, sizeof(*enc))) {
cb4cef
+        cipher = "CAMELLIA-256-CBC";
cb4cef
+    } else {
cb4cef
+        ret = KRB5_CRYPTO_INTERNAL;
cb4cef
+        goto done;
cb4cef
+    }
cb4cef
+
cb4cef
+    kdf = EVP_KDF_fetch(NULL, "KBKDF", NULL);
cb4cef
+    if (!kdf) {
cb4cef
+        ret = KRB5_CRYPTO_INTERNAL;
cb4cef
+        goto done;
cb4cef
+    }
cb4cef
+
cb4cef
+    kctx = EVP_KDF_CTX_new(kdf);
cb4cef
+    if (!kctx) {
cb4cef
+        ret = KRB5_CRYPTO_INTERNAL;
cb4cef
+        goto done;
cb4cef
+    }
cb4cef
+
cb4cef
+    params[i++] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_MODE,
cb4cef
+                                                   "FEEDBACK", 0);
cb4cef
+    params[i++] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_MAC,
cb4cef
+                                                   "CMAC", 0);
cb4cef
+    params[i++] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_CIPHER,
cb4cef
+                                                   cipher, 0);
cb4cef
+    params[i++] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
cb4cef
+                                                    inkey->keyblock.contents,
cb4cef
+                                                    inkey->keyblock.length);
cb4cef
+    params[i++] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
cb4cef
+                                                    in_constant->data,
cb4cef
+                                                    in_constant->length);
cb4cef
+    params[i++] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SEED,
cb4cef
+                                                    zeroes, sizeof(zeroes));
cb4cef
+    params[i] = OSSL_PARAM_construct_end();
cb4cef
+    if (EVP_KDF_derive(kctx, (unsigned char *)outrnd->data, outrnd->length,
cb4cef
+                       params) <= 0) {
cb4cef
+        ret = KRB5_CRYPTO_INTERNAL;
cb4cef
+        goto done;
cb4cef
+    }
cb4cef
+
cb4cef
+    ret = 0;
cb4cef
+done:
cb4cef
+    if (ret)
cb4cef
+        zap(outrnd->data, outrnd->length);
cb4cef
+    EVP_KDF_free(kdf);
cb4cef
+    EVP_KDF_CTX_free(kctx);
cb4cef
+    return ret;
cb4cef
+}
cb4cef
+
cb4cef
+static krb5_error_code
cb4cef
+openssl_krb5kdf(const struct krb5_enc_provider *enc, krb5_key inkey,
cb4cef
+                krb5_data *outrnd, const krb5_data *in_constant)
cb4cef
+{
cb4cef
+    krb5_error_code ret;
cb4cef
+    EVP_KDF *kdf = NULL;
cb4cef
+    EVP_KDF_CTX *kctx = NULL;
cb4cef
+    OSSL_PARAM params[4];
cb4cef
+    size_t i = 0;
cb4cef
+    char *cipher;
cb4cef
+
cb4cef
+    if (inkey->keyblock.length != enc->keylength ||
cb4cef
+        outrnd->length != enc->keybytes) {
cb4cef
+        return KRB5_CRYPTO_INTERNAL;
cb4cef
+    }
cb4cef
+
cb4cef
+    if (!memcmp(enc, &krb5int_enc_aes128, sizeof(*enc))) {
cb4cef
+        cipher = "AES-128-CBC";
cb4cef
+    } else if (!memcmp(enc, &krb5int_enc_aes256, sizeof(*enc))) {
cb4cef
+        cipher = "AES-256-CBC";
cb4cef
+    } else {
cb4cef
+        ret = KRB5_CRYPTO_INTERNAL;
cb4cef
+        goto done;
cb4cef
+    }
cb4cef
+
cb4cef
+    kdf = EVP_KDF_fetch(NULL, "KRB5KDF", NULL);
cb4cef
+    if (kdf == NULL) {
cb4cef
+        ret = KRB5_CRYPTO_INTERNAL;
cb4cef
+        goto done;
cb4cef
+    }
cb4cef
+
cb4cef
+    kctx = EVP_KDF_CTX_new(kdf);
cb4cef
+    if (kctx == NULL) {
cb4cef
+        ret = KRB5_CRYPTO_INTERNAL;
cb4cef
+        goto done;
cb4cef
+    }
cb4cef
+
cb4cef
+    params[i++] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_CIPHER,
cb4cef
+                                                   cipher, 0);
cb4cef
+    params[i++] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
cb4cef
+                                                    inkey->keyblock.contents,
cb4cef
+                                                    inkey->keyblock.length);
cb4cef
+    params[i++] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_CONSTANT,
cb4cef
+                                                    in_constant->data,
cb4cef
+                                                    in_constant->length);
cb4cef
+    params[i] = OSSL_PARAM_construct_end();
cb4cef
+    if (EVP_KDF_derive(kctx, (unsigned char *)outrnd->data, outrnd->length,
cb4cef
+                       params) <= 0) {
cb4cef
+        ret = KRB5_CRYPTO_INTERNAL;
cb4cef
+        goto done;
cb4cef
+    }
cb4cef
+
cb4cef
+    ret = 0;
cb4cef
+done:
cb4cef
+    if (ret)
cb4cef
+        zap(outrnd->data, outrnd->length);
cb4cef
+    EVP_KDF_free(kdf);
cb4cef
+    EVP_KDF_CTX_free(kctx);
cb4cef
+    return ret;
cb4cef
+}
cb4cef
+
cb4cef
+#else /* HAVE_EVP_KDF_FETCH */
cb4cef
+
cb4cef
+/*
cb4cef
+ * NIST SP800-108 KDF in counter mode (section 5.1).
cb4cef
+ * Parameters:
cb4cef
+ *   - HMAC (with hash as the hash provider) is the PRF.
cb4cef
+ *   - A block counter of four bytes is used.
cb4cef
+ *   - Four bytes are used to encode the output length in the PRF input.
cb4cef
+ *
cb4cef
+ * There are no uses requiring more than a single PRF invocation.
cb4cef
+ */
cb4cef
+static krb5_error_code
cb4cef
+builtin_sp800_108_counter_hmac(const struct krb5_hash_provider *hash,
cb4cef
+                               krb5_key inkey, krb5_data *outrnd,
cb4cef
+                               const krb5_data *label,
cb4cef
+                               const krb5_data *context)
cb4cef
+{
cb4cef
+    krb5_crypto_iov iov[5];
cb4cef
+    krb5_error_code ret;
cb4cef
+    krb5_data prf;
cb4cef
+    unsigned char ibuf[4], lbuf[4];
cb4cef
+
cb4cef
+    if (hash == NULL || outrnd->length > hash->hashsize)
cb4cef
         return KRB5_CRYPTO_INTERNAL;
cb4cef
 
cb4cef
     /* Allocate encryption data buffer. */
cb4cef
-    ret = alloc_data(&block, blocksize);
cb4cef
+    ret = alloc_data(&prf, hash->hashsize);
cb4cef
     if (ret)
cb4cef
         return ret;
cb4cef
 
cb4cef
-    /* Initialize the input block. */
cb4cef
-    if (in_constant->length == blocksize) {
cb4cef
-        memcpy(block.data, in_constant->data, blocksize);
cb4cef
-    } else {
cb4cef
-        krb5int_nfold(in_constant->length * 8,
cb4cef
-                      (unsigned char *) in_constant->data,
cb4cef
-                      blocksize * 8, (unsigned char *) block.data);
cb4cef
-    }
cb4cef
+    /* [i]2: four-byte big-endian binary string giving the block counter (1) */
cb4cef
+    iov[0].flags = KRB5_CRYPTO_TYPE_DATA;
cb4cef
+    iov[0].data = make_data(ibuf, sizeof(ibuf));
cb4cef
+    store_32_be(1, ibuf);
cb4cef
+    /* Label */
cb4cef
+    iov[1].flags = KRB5_CRYPTO_TYPE_DATA;
cb4cef
+    iov[1].data = *label;
cb4cef
+    /* 0x00: separator byte */
cb4cef
+    iov[2].flags = KRB5_CRYPTO_TYPE_DATA;
cb4cef
+    iov[2].data = make_data("", 1);
cb4cef
+    /* Context */
cb4cef
+    iov[3].flags = KRB5_CRYPTO_TYPE_DATA;
cb4cef
+    iov[3].data = *context;
cb4cef
+    /* [L]2: four-byte big-endian binary string giving the output length */
cb4cef
+    iov[4].flags = KRB5_CRYPTO_TYPE_DATA;
cb4cef
+    iov[4].data = make_data(lbuf, sizeof(lbuf));
cb4cef
+    store_32_be(outrnd->length * 8, lbuf);
cb4cef
 
cb4cef
-    /* Loop encrypting the blocks until enough key bytes are generated. */
cb4cef
-    n = 0;
cb4cef
-    while (n < keybytes) {
cb4cef
-        ret = encrypt_block(enc, inkey, &block);
cb4cef
-        if (ret)
cb4cef
-            goto cleanup;
cb4cef
-
cb4cef
-        if ((keybytes - n) <= blocksize) {
cb4cef
-            memcpy(outrnd->data + n, block.data, (keybytes - n));
cb4cef
-            break;
cb4cef
-        }
cb4cef
-
cb4cef
-        memcpy(outrnd->data + n, block.data, blocksize);
cb4cef
-        n += blocksize;
cb4cef
-    }
cb4cef
-
cb4cef
-cleanup:
cb4cef
-    zapfree(block.data, blocksize);
cb4cef
+    ret = krb5int_hmac(hash, inkey, iov, 5, &prf;;
cb4cef
+    if (!ret)
cb4cef
+        memcpy(outrnd->data, prf.data, outrnd->length);
cb4cef
+    zapfree(prf.data, prf.length);
cb4cef
     return ret;
cb4cef
 }
cb4cef
 
cb4cef
@@ -139,9 +341,9 @@ cleanup:
cb4cef
  *   - Four bytes are used to encode the output length in the PRF input.
cb4cef
  */
cb4cef
 static krb5_error_code
cb4cef
-derive_random_sp800_108_feedback_cmac(const struct krb5_enc_provider *enc,
cb4cef
-                                      krb5_key inkey, krb5_data *outrnd,
cb4cef
-                                      const krb5_data *in_constant)
cb4cef
+builtin_sp800_108_feedback_cmac(const struct krb5_enc_provider *enc,
cb4cef
+                                krb5_key inkey, krb5_data *outrnd,
cb4cef
+                                const krb5_data *in_constant)
cb4cef
 {
cb4cef
     size_t blocksize, keybytes, n;
cb4cef
     krb5_crypto_iov iov[6];
cb4cef
@@ -204,56 +406,94 @@ cleanup:
cb4cef
     return ret;
cb4cef
 }
cb4cef
 
cb4cef
-/*
cb4cef
- * NIST SP800-108 KDF in counter mode (section 5.1).
cb4cef
- * Parameters:
cb4cef
- *   - HMAC (with hash as the hash provider) is the PRF.
cb4cef
- *   - A block counter of four bytes is used.
cb4cef
- *   - Four bytes are used to encode the output length in the PRF input.
cb4cef
- *
cb4cef
- * There are no uses requiring more than a single PRF invocation.
cb4cef
- */
cb4cef
+static krb5_error_code
cb4cef
+builtin_derive_random_rfc3961(const struct krb5_enc_provider *enc,
cb4cef
+                              krb5_key inkey, krb5_data *outrnd,
cb4cef
+                              const krb5_data *in_constant)
cb4cef
+{
cb4cef
+    size_t blocksize, keybytes, n;
cb4cef
+    krb5_error_code ret;
cb4cef
+    krb5_data block = empty_data();
cb4cef
+
cb4cef
+    blocksize = enc->block_size;
cb4cef
+    keybytes = enc->keybytes;
cb4cef
+
cb4cef
+    if (blocksize == 1)
cb4cef
+        return KRB5_BAD_ENCTYPE;
cb4cef
+    if (inkey->keyblock.length != enc->keylength || outrnd->length != keybytes)
cb4cef
+        return KRB5_CRYPTO_INTERNAL;
cb4cef
+
cb4cef
+    /* Allocate encryption data buffer. */
cb4cef
+    ret = alloc_data(&block, blocksize);
cb4cef
+    if (ret)
cb4cef
+        return ret;
cb4cef
+
cb4cef
+    /* Initialize the input block. */
cb4cef
+    if (in_constant->length == blocksize) {
cb4cef
+        memcpy(block.data, in_constant->data, blocksize);
cb4cef
+    } else {
cb4cef
+        krb5int_nfold(in_constant->length * 8,
cb4cef
+                      (unsigned char *) in_constant->data,
cb4cef
+                      blocksize * 8, (unsigned char *) block.data);
cb4cef
+    }
cb4cef
+
cb4cef
+    /* Loop encrypting the blocks until enough key bytes are generated. */
cb4cef
+    n = 0;
cb4cef
+    while (n < keybytes) {
cb4cef
+        ret = encrypt_block(enc, inkey, &block);
cb4cef
+        if (ret)
cb4cef
+            goto cleanup;
cb4cef
+
cb4cef
+        if ((keybytes - n) <= blocksize) {
cb4cef
+            memcpy(outrnd->data + n, block.data, (keybytes - n));
cb4cef
+            break;
cb4cef
+        }
cb4cef
+
cb4cef
+        memcpy(outrnd->data + n, block.data, blocksize);
cb4cef
+        n += blocksize;
cb4cef
+    }
cb4cef
+
cb4cef
+cleanup:
cb4cef
+    zapfree(block.data, blocksize);
cb4cef
+    return ret;
cb4cef
+}
cb4cef
+#endif /* HAVE_EVP_KDF_FETCH */
cb4cef
+
cb4cef
 krb5_error_code
cb4cef
 k5_sp800_108_counter_hmac(const struct krb5_hash_provider *hash,
cb4cef
                           krb5_key inkey, krb5_data *outrnd,
cb4cef
                           const krb5_data *label, const krb5_data *context)
cb4cef
 {
cb4cef
-    krb5_crypto_iov iov[5];
cb4cef
-    krb5_error_code ret;
cb4cef
-    krb5_data prf;
cb4cef
-    unsigned char ibuf[4], lbuf[4];
cb4cef
+#ifdef HAVE_EVP_KDF_FETCH
cb4cef
+    return openssl_kbdkf_counter_hmac(hash, inkey, outrnd, label, context);
cb4cef
+#else
cb4cef
+    return builtin_sp800_108_counter_hmac(hash, inkey, outrnd, label,
cb4cef
+                                          context);
cb4cef
+#endif
cb4cef
+}
cb4cef
 
cb4cef
-    if (hash == NULL || outrnd->length > hash->hashsize)
cb4cef
-        return KRB5_CRYPTO_INTERNAL;
cb4cef
+static krb5_error_code
cb4cef
+sp800_108_feedback_cmac(const struct krb5_enc_provider *enc,
cb4cef
+                           krb5_key inkey, krb5_data *outrnd,
cb4cef
+                           const krb5_data *in_constant)
cb4cef
+{
cb4cef
+#ifdef HAVE_EVP_KDF_FETCH
cb4cef
+    return openssl_kbkdf_feedback_cmac(enc, inkey, outrnd, in_constant);
cb4cef
+#else
cb4cef
+    return builtin_sp800_108_feedback_cmac(enc, inkey, outrnd, in_constant);
cb4cef
+#endif
cb4cef
+}
cb4cef
 
cb4cef
-    /* Allocate encryption data buffer. */
cb4cef
-    ret = alloc_data(&prf, hash->hashsize);
cb4cef
-    if (ret)
cb4cef
-        return ret;
cb4cef
-
cb4cef
-    /* [i]2: four-byte big-endian binary string giving the block counter (1) */
cb4cef
-    iov[0].flags = KRB5_CRYPTO_TYPE_DATA;
cb4cef
-    iov[0].data = make_data(ibuf, sizeof(ibuf));
cb4cef
-    store_32_be(1, ibuf);
cb4cef
-    /* Label */
cb4cef
-    iov[1].flags = KRB5_CRYPTO_TYPE_DATA;
cb4cef
-    iov[1].data = *label;
cb4cef
-    /* 0x00: separator byte */
cb4cef
-    iov[2].flags = KRB5_CRYPTO_TYPE_DATA;
cb4cef
-    iov[2].data = make_data("", 1);
cb4cef
-    /* Context */
cb4cef
-    iov[3].flags = KRB5_CRYPTO_TYPE_DATA;
cb4cef
-    iov[3].data = *context;
cb4cef
-    /* [L]2: four-byte big-endian binary string giving the output length */
cb4cef
-    iov[4].flags = KRB5_CRYPTO_TYPE_DATA;
cb4cef
-    iov[4].data = make_data(lbuf, sizeof(lbuf));
cb4cef
-    store_32_be(outrnd->length * 8, lbuf);
cb4cef
-
cb4cef
-    ret = krb5int_hmac(hash, inkey, iov, 5, &prf;;
cb4cef
-    if (!ret)
cb4cef
-        memcpy(outrnd->data, prf.data, outrnd->length);
cb4cef
-    zapfree(prf.data, prf.length);
cb4cef
-    return ret;
cb4cef
+static krb5_error_code
cb4cef
+derive_random_rfc3961(const struct krb5_enc_provider *enc,
cb4cef
+                         krb5_key inkey, krb5_data *outrnd,
cb4cef
+                         const krb5_data *in_constant)
cb4cef
+{
cb4cef
+#ifdef HAVE_EVP_KDF_FETCH
cb4cef
+    return openssl_krb5kdf(enc, inkey, outrnd, in_constant);
cb4cef
+#else
cb4cef
+    return builtin_derive_random_rfc3961(enc, inkey, outrnd, in_constant);
cb4cef
+#endif
cb4cef
 }
cb4cef
 
cb4cef
 krb5_error_code
cb4cef
@@ -268,8 +508,7 @@ krb5int_derive_random(const struct krb5_enc_provider *enc,
cb4cef
     case DERIVE_RFC3961:
cb4cef
         return derive_random_rfc3961(enc, inkey, outrnd, in_constant);
cb4cef
     case DERIVE_SP800_108_CMAC:
cb4cef
-        return derive_random_sp800_108_feedback_cmac(enc, inkey, outrnd,
cb4cef
-                                                     in_constant);
cb4cef
+        return sp800_108_feedback_cmac(enc, inkey, outrnd, in_constant);
cb4cef
     case DERIVE_SP800_108_HMAC:
cb4cef
         return k5_sp800_108_counter_hmac(hash, inkey, outrnd, in_constant,
cb4cef
                                          &empty);