/*
- * Copyright 2018-2019 The OpenSSL Project Authors. All Rights Reserved.
- * Copyright (c) 2018-2019, Oracle and/or its affiliates. All rights reserved.
+ * Copyright 2018-2020 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright (c) 2018-2020, Oracle and/or its affiliates. All rights reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
return kctx;
}
+static OSSL_PARAM *construct_tls1_prf_params(const char *digest, const char *secret,
+ const char *seed)
+{
+ OSSL_PARAM *params = OPENSSL_malloc(sizeof(OSSL_PARAM) * 4);
+ OSSL_PARAM *p = params;
+
+ *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
+ (char *)digest, 0);
+ *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SECRET,
+ (unsigned char *)secret,
+ strlen(secret));
+ *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SEED,
+ (unsigned char *)seed,
+ strlen(seed));
+ *p = OSSL_PARAM_construct_end();
+
+ return params;
+}
+
static int test_kdf_tls1_prf(void)
{
int ret;
EVP_KDF_CTX *kctx = NULL;
unsigned char out[16];
- OSSL_PARAM params[4], *p = params;
+ OSSL_PARAM *params;
static const unsigned char expected[sizeof(out)] = {
0x8e, 0x4d, 0x93, 0x25, 0x30, 0xd7, 0x65, 0xa0,
0xaa, 0xe9, 0x74, 0xc3, 0x04, 0x73, 0x5e, 0xcc
};
- *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
- (char *)"sha256", sizeof("sha256"));
- *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SECRET,
- (unsigned char *)"secret",
- (size_t)6);
- *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SEED,
- (unsigned char *)"seed",
- (size_t)4);
- *p = OSSL_PARAM_construct_end();
+ params = construct_tls1_prf_params("sha256", "secret", "seed");
ret =
- TEST_ptr(kctx = get_kdfbyname(SN_tls1_prf))
+ TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_TLS1_PRF))
&& TEST_true(EVP_KDF_CTX_set_params(kctx, params))
&& TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out)), 0)
&& TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
EVP_KDF_CTX_free(kctx);
+ OPENSSL_free(params);
+ return ret;
+}
+
+static int test_kdf_tls1_prf_invalid_digest(void)
+{
+ int ret;
+ EVP_KDF_CTX *kctx = NULL;
+ OSSL_PARAM *params;
+
+ params = construct_tls1_prf_params("blah", "secret", "seed");
+
+ ret =
+ TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_TLS1_PRF))
+ && TEST_false(EVP_KDF_CTX_set_params(kctx, params));
+
+ EVP_KDF_CTX_free(kctx);
+ OPENSSL_free(params);
+ return ret;
+}
+
+static int test_kdf_tls1_prf_zero_output_size(void)
+{
+ int ret;
+ EVP_KDF_CTX *kctx = NULL;
+ unsigned char out[16];
+ OSSL_PARAM *params;
+
+ params = construct_tls1_prf_params("sha256", "secret", "seed");
+
+ /* Negative test - derive should fail */
+ ret =
+ TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_TLS1_PRF))
+ && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
+ && TEST_int_eq(EVP_KDF_derive(kctx, out, 0), 0);
+
+ EVP_KDF_CTX_free(kctx);
+ OPENSSL_free(params);
+ return ret;
+}
+
+static int test_kdf_tls1_prf_empty_secret(void)
+{
+ int ret;
+ EVP_KDF_CTX *kctx = NULL;
+ unsigned char out[16];
+ OSSL_PARAM *params;
+
+ params = construct_tls1_prf_params("sha256", "", "seed");
+
+ ret =
+ TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_TLS1_PRF))
+ && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
+ && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out)), 0);
+
+ EVP_KDF_CTX_free(kctx);
+ OPENSSL_free(params);
+ return ret;
+}
+
+static int test_kdf_tls1_prf_1byte_secret(void)
+{
+ int ret;
+ EVP_KDF_CTX *kctx = NULL;
+ unsigned char out[16];
+ OSSL_PARAM *params;
+
+ params = construct_tls1_prf_params("sha256", "1", "seed");
+
+ ret =
+ TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_TLS1_PRF))
+ && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
+ && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out)), 0);
+
+ EVP_KDF_CTX_free(kctx);
+ OPENSSL_free(params);
+ return ret;
+}
+
+static int test_kdf_tls1_prf_empty_seed(void)
+{
+ int ret;
+ EVP_KDF_CTX *kctx = NULL;
+ unsigned char out[16];
+ OSSL_PARAM *params;
+
+ params = construct_tls1_prf_params("sha256", "secret", "");
+
+ /* Negative test - derive should fail */
+ ret =
+ TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_TLS1_PRF))
+ && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
+ && TEST_int_eq(EVP_KDF_derive(kctx, out, sizeof(out)), 0);
+
+ EVP_KDF_CTX_free(kctx);
+ OPENSSL_free(params);
return ret;
}
+static int test_kdf_tls1_prf_1byte_seed(void)
+{
+ int ret;
+ EVP_KDF_CTX *kctx = NULL;
+ unsigned char out[16];
+ OSSL_PARAM *params;
+
+ params = construct_tls1_prf_params("sha256", "secret", "1");
+
+ ret =
+ TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_TLS1_PRF))
+ && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
+ && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out)), 0);
+
+ EVP_KDF_CTX_free(kctx);
+ OPENSSL_free(params);
+ return ret;
+}
+
+static OSSL_PARAM *construct_hkdf_params(char *digest, char *key,
+ size_t keylen, char *salt, char *info)
+{
+ OSSL_PARAM *params = OPENSSL_malloc(sizeof(OSSL_PARAM) * 5);
+ OSSL_PARAM *p = params;
+
+ *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
+ digest, 0);
+ *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
+ salt, strlen(salt));
+ *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
+ (unsigned char *)key, keylen);
+ *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO,
+ info, strlen(info));
+ *p = OSSL_PARAM_construct_end();
+
+ return params;
+}
+
static int test_kdf_hkdf(void)
{
int ret;
EVP_KDF_CTX *kctx;
unsigned char out[10];
- OSSL_PARAM params[5], *p = params;
+ OSSL_PARAM *params;
static const unsigned char expected[sizeof(out)] = {
0x2a, 0xc4, 0x36, 0x9f, 0x52, 0x59, 0x96, 0xf8, 0xde, 0x13
};
- *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
- (char *)"sha256", sizeof("sha256"));
- *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
- (unsigned char *)"salt", 4);
- *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
- (unsigned char *)"secret", 6);
- *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO,
- (unsigned char *)"label", 5);
- *p = OSSL_PARAM_construct_end();
+ params = construct_hkdf_params("sha256", "secret", 6, "salt", "label");
ret =
- TEST_ptr(kctx = get_kdfbyname(SN_hkdf))
+ TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HKDF))
&& TEST_true(EVP_KDF_CTX_set_params(kctx, params))
&& TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out)), 0)
&& TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
EVP_KDF_CTX_free(kctx);
+ OPENSSL_free(params);
+ return ret;
+}
+
+static int test_kdf_hkdf_invalid_digest(void)
+{
+ int ret;
+ EVP_KDF_CTX *kctx;
+ OSSL_PARAM *params;
+
+ params = construct_hkdf_params("blah", "secret", 6, "salt", "label");
+
+ ret =
+ TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HKDF))
+ && TEST_false(EVP_KDF_CTX_set_params(kctx, params));
+
+ EVP_KDF_CTX_free(kctx);
+ OPENSSL_free(params);
return ret;
}
+static int test_kdf_hkdf_zero_output_size(void)
+{
+ int ret;
+ EVP_KDF_CTX *kctx;
+ unsigned char out[10];
+ OSSL_PARAM *params;
+
+ params = construct_hkdf_params("sha256", "secret", 6, "salt", "label");
+
+ /* Negative test - derive should fail */
+ ret =
+ TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HKDF))
+ && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
+ && TEST_int_eq(EVP_KDF_derive(kctx, out, 0), 0);
+
+ EVP_KDF_CTX_free(kctx);
+ OPENSSL_free(params);
+ return ret;
+}
+
+static int test_kdf_hkdf_empty_key(void)
+{
+ int ret;
+ EVP_KDF_CTX *kctx;
+ unsigned char out[10];
+ OSSL_PARAM *params;
+
+ params = construct_hkdf_params("sha256", "", 0, "salt", "label");
+
+ ret =
+ TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HKDF))
+ && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
+ && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out)), 0);
+
+ EVP_KDF_CTX_free(kctx);
+ OPENSSL_free(params);
+ return ret;
+}
+
+static int test_kdf_hkdf_1byte_key(void)
+{
+ int ret;
+ EVP_KDF_CTX *kctx;
+ unsigned char out[10];
+ OSSL_PARAM *params;
+
+ params = construct_hkdf_params("sha256", "1", 1, "salt", "label");
+
+ ret =
+ TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HKDF))
+ && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
+ && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out)), 0);
+
+ EVP_KDF_CTX_free(kctx);
+ OPENSSL_free(params);
+ return ret;
+}
+
+static int test_kdf_hkdf_empty_salt(void)
+{
+ int ret;
+ EVP_KDF_CTX *kctx;
+ unsigned char out[10];
+ OSSL_PARAM *params;
+
+ params = construct_hkdf_params("sha256", "secret", 6, "", "label");
+
+ ret =
+ TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HKDF))
+ && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
+ && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out)), 0);
+
+ EVP_KDF_CTX_free(kctx);
+ OPENSSL_free(params);
+ return ret;
+}
+
+static OSSL_PARAM *construct_pbkdf2_params(char *pass, char *digest, char *salt,
+ unsigned int *iter, int *mode)
+{
+ OSSL_PARAM *params = OPENSSL_malloc(sizeof(OSSL_PARAM) * 6);
+ OSSL_PARAM *p = params;
+
+ *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD,
+ (unsigned char *)pass, strlen(pass));
+ *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
+ (unsigned char *)salt, strlen(salt));
+ *p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_ITER, iter);
+ *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
+ digest, 0);
+ *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_PKCS5, mode);
+ *p = OSSL_PARAM_construct_end();
+
+ return params;
+}
+
static int test_kdf_pbkdf2(void)
{
int ret = 0;
EVP_KDF_CTX *kctx;
unsigned char out[25];
- size_t len = 0;
unsigned int iterations = 4096;
int mode = 0;
- OSSL_PARAM params[6], *p = params;
+ OSSL_PARAM *params;
const unsigned char expected[sizeof(out)] = {
0x34, 0x8c, 0x89, 0xdb, 0xcb, 0xd3, 0x2b, 0x2f,
0x32, 0xd8, 0x14, 0xb8, 0x11, 0x6e, 0x84, 0xcf,
0x1c
};
- if (sizeof(len) > 32)
- len = SIZE_MAX;
+ params = construct_pbkdf2_params("passwordPASSWORDpassword", "sha256",
+ "saltSALTsaltSALTsaltSALTsaltSALTsalt",
+ &iterations, &mode);
- *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD,
- (unsigned char *)
- "passwordPASSWORDpassword", 24);
- *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
- (unsigned char *)
- "saltSALTsaltSALTsaltSALTsaltSALTsalt",
- 36);
- *p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_ITER, &iterations);
- *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
- (char *)"sha256", 7);
- *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_PKCS5, &mode);
- *p = OSSL_PARAM_construct_end();
-
- if (!TEST_ptr(kctx = get_kdfbyname(LN_id_pbkdf2))
+ if (!TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_PBKDF2))
|| !TEST_true(EVP_KDF_CTX_set_params(kctx, params))
|| !TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out)), 0)
- || !TEST_mem_eq(out, sizeof(out), expected, sizeof(expected))
+ || !TEST_mem_eq(out, sizeof(out), expected, sizeof(expected)))
+ goto err;
+
+ ret = 1;
+err:
+ EVP_KDF_CTX_free(kctx);
+ OPENSSL_free(params);
+ return ret;
+}
+
+static int test_kdf_pbkdf2_small_output(void)
+{
+ int ret = 0;
+ EVP_KDF_CTX *kctx;
+ unsigned char out[25];
+ unsigned int iterations = 4096;
+ int mode = 0;
+ OSSL_PARAM *params;
+
+ params = construct_pbkdf2_params("passwordPASSWORDpassword", "sha256",
+ "saltSALTsaltSALTsaltSALTsaltSALTsalt",
+ &iterations, &mode);
+
+ if (!TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_PBKDF2))
|| !TEST_true(EVP_KDF_CTX_set_params(kctx, params))
/* A key length that is too small should fail */
- || !TEST_int_eq(EVP_KDF_derive(kctx, out, 112 / 8 - 1), 0)
+ || !TEST_int_eq(EVP_KDF_derive(kctx, out, 112 / 8 - 1), 0))
+ goto err;
+
+ ret = 1;
+err:
+ EVP_KDF_CTX_free(kctx);
+ OPENSSL_free(params);
+ return ret;
+}
+
+static int test_kdf_pbkdf2_large_output(void)
+{
+ int ret = 0;
+ EVP_KDF_CTX *kctx;
+ unsigned char out[25];
+ size_t len = 0;
+ unsigned int iterations = 4096;
+ int mode = 0;
+ OSSL_PARAM *params;
+
+ if (sizeof(len) > 32)
+ len = SIZE_MAX;
+
+ params = construct_pbkdf2_params("passwordPASSWORDpassword", "sha256",
+ "saltSALTsaltSALTsaltSALTsaltSALTsalt",
+ &iterations, &mode);
+
+ if (!TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_PBKDF2))
+ || !TEST_true(EVP_KDF_CTX_set_params(kctx, params))
/* A key length that is too large should fail */
|| (len != 0 && !TEST_int_eq(EVP_KDF_derive(kctx, out, len), 0)))
goto err;
-#if 0
-/* TODO */
- /* Salt length less than 128 bits should fail */
- || TEST_int_eq(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SALT,
- "123456781234567",
- (size_t)15), 0)
- /* A small iteration count should fail */
- || TEST_int_eq(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_ITER, 1), 0)
- || TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_PBKDF2_PKCS5_MODE,
- 1), 0)
- /* Small salts will pass if the "pkcs5" mode is enabled */
- || TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SALT,
- "123456781234567",
- (size_t)15), 0)
- /* A small iteration count will pass if "pkcs5" mode is enabled */
- || TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_ITER, 1), 0)
- /*
- * If the "pkcs5" mode is disabled then the small salt and iter will
- * fail when the derive gets called.
- */
- || TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_PBKDF2_PKCS5_MODE,
- 0), 0)
- || TEST_int_eq(EVP_KDF_derive(kctx, out, sizeof(out)), 0);
-#endif
+
+ ret = 1;
+err:
+ EVP_KDF_CTX_free(kctx);
+ OPENSSL_free(params);
+ return ret;
+}
+
+static int test_kdf_pbkdf2_small_salt(void)
+{
+ int ret = 0;
+ EVP_KDF_CTX *kctx;
+ unsigned int iterations = 4096;
+ int mode = 0;
+ OSSL_PARAM *params;
+
+ params = construct_pbkdf2_params("passwordPASSWORDpassword", "sha256",
+ "saltSALT",
+ &iterations, &mode);
+
+ if (!TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_PBKDF2))
+ /* A salt that is too small should fail */
+ || !TEST_false(EVP_KDF_CTX_set_params(kctx, params)))
+ goto err;
+
+ ret = 1;
+err:
+ EVP_KDF_CTX_free(kctx);
+ OPENSSL_free(params);
+ return ret;
+}
+
+static int test_kdf_pbkdf2_small_iterations(void)
+{
+ int ret = 0;
+ EVP_KDF_CTX *kctx;
+ unsigned int iterations = 1;
+ int mode = 0;
+ OSSL_PARAM *params;
+
+ params = construct_pbkdf2_params("passwordPASSWORDpassword", "sha256",
+ "saltSALTsaltSALTsaltSALTsaltSALTsalt",
+ &iterations, &mode);
+
+ if (!TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_PBKDF2))
+ /* An iteration count that is too small should fail */
+ || !TEST_false(EVP_KDF_CTX_set_params(kctx, params)))
+ goto err;
+
+ ret = 1;
+err:
+ EVP_KDF_CTX_free(kctx);
+ OPENSSL_free(params);
+ return ret;
+}
+
+static int test_kdf_pbkdf2_small_salt_pkcs5(void)
+{
+ int ret = 0;
+ EVP_KDF_CTX *kctx;
+ unsigned char out[25];
+ unsigned int iterations = 4096;
+ int mode = 1;
+ OSSL_PARAM *params;
+ OSSL_PARAM mode_params[2];
+
+ params = construct_pbkdf2_params("passwordPASSWORDpassword", "sha256",
+ "saltSALT",
+ &iterations, &mode);
+
+ if (!TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_PBKDF2))
+ /* A salt that is too small should pass in pkcs5 mode */
+ || !TEST_true(EVP_KDF_CTX_set_params(kctx, params))
+ || !TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out)), 0))
+ goto err;
+
+ mode = 0;
+ mode_params[0] = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_PKCS5, &mode);
+ mode_params[1] = OSSL_PARAM_construct_end();
+
+ /* If the "pkcs5" mode is disabled then the derive will now fail */
+ if (!TEST_true(EVP_KDF_CTX_set_params(kctx, mode_params))
+ || !TEST_int_eq(EVP_KDF_derive(kctx, out, sizeof(out)), 0))
+ goto err;
+
+ ret = 1;
+err:
+ EVP_KDF_CTX_free(kctx);
+ OPENSSL_free(params);
+ return ret;
+}
+
+static int test_kdf_pbkdf2_small_iterations_pkcs5(void)
+{
+ int ret = 0;
+ EVP_KDF_CTX *kctx;
+ unsigned char out[25];
+ unsigned int iterations = 1;
+ int mode = 1;
+ OSSL_PARAM *params;
+ OSSL_PARAM mode_params[2];
+
+ params = construct_pbkdf2_params("passwordPASSWORDpassword", "sha256",
+ "saltSALTsaltSALTsaltSALTsaltSALTsalt",
+ &iterations, &mode);
+
+ if (!TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_PBKDF2))
+ /* An iteration count that is too small will pass in pkcs5 mode */
+ || !TEST_true(EVP_KDF_CTX_set_params(kctx, params))
+ || !TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out)), 0))
+ goto err;
+
+ mode = 0;
+ mode_params[0] = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_PKCS5, &mode);
+ mode_params[1] = OSSL_PARAM_construct_end();
+
+ /* If the "pkcs5" mode is disabled then the derive will now fail */
+ if (!TEST_true(EVP_KDF_CTX_set_params(kctx, mode_params))
+ || !TEST_int_eq(EVP_KDF_derive(kctx, out, sizeof(out)), 0))
+ goto err;
+
+ ret = 1;
+err:
+ EVP_KDF_CTX_free(kctx);
+ OPENSSL_free(params);
+ return ret;
+}
+
+static int test_kdf_pbkdf2_invalid_digest(void)
+{
+ int ret = 0;
+ EVP_KDF_CTX *kctx;
+ unsigned int iterations = 4096;
+ int mode = 0;
+ OSSL_PARAM *params;
+
+ params = construct_pbkdf2_params("passwordPASSWORDpassword", "blah",
+ "saltSALTsaltSALTsaltSALTsaltSALTsalt",
+ &iterations, &mode);
+
+ if (!TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_PBKDF2))
+ /* Unknown digest should fail */
+ || !TEST_false(EVP_KDF_CTX_set_params(kctx, params)))
+ goto err;
+
ret = 1;
err:
EVP_KDF_CTX_free(kctx);
+ OPENSSL_free(params);
return ret;
}
*p = OSSL_PARAM_construct_end();
ret =
- TEST_ptr(kctx = get_kdfbyname(SN_id_scrypt))
+ TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_SCRYPT))
&& TEST_true(EVP_KDF_CTX_set_params(kctx, params))
/* failure test *//*
&& TEST_int_le(EVP_KDF_derive(kctx, out, sizeof(out)), 0)*/
*p = OSSL_PARAM_construct_end();
ret =
- TEST_ptr(kctx = get_kdfbyname(SN_sskdf))
+ TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_SSKDF))
&& TEST_true(EVP_KDF_CTX_set_params(kctx, params))
&& TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out)), 0)
&& TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
*p = OSSL_PARAM_construct_end();
ret =
- TEST_ptr(kctx = get_kdfbyname(SN_x963kdf))
+ TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_X963KDF))
&& TEST_true(EVP_KDF_CTX_set_params(kctx, params))
&& TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out)), 0)
&& TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
return ret;
}
+#if !defined(OPENSSL_NO_CMAC) && !defined(OPENSSL_NO_CAMELLIA)
+/*
+ * KBKDF test vectors from RFC 6803 (Camellia Encryption for Kerberos 5)
+ * section 10.
+ */
+static int test_kdf_kbkdf_6803_128(void)
+{
+ int ret = 0, i, p;
+ EVP_KDF_CTX *kctx;
+ OSSL_PARAM params[7];
+ static unsigned char input_key[] = {
+ 0x57, 0xD0, 0x29, 0x72, 0x98, 0xFF, 0xD9, 0xD3,
+ 0x5D, 0xE5, 0xA4, 0x7F, 0xB4, 0xBD, 0xE2, 0x4B,
+ };
+ static unsigned char constants[][5] = {
+ { 0x00, 0x00, 0x00, 0x02, 0x99 },
+ { 0x00, 0x00, 0x00, 0x02, 0xaa },
+ { 0x00, 0x00, 0x00, 0x02, 0x55 },
+ };
+ static unsigned char outputs[][16] = {
+ {0xD1, 0x55, 0x77, 0x5A, 0x20, 0x9D, 0x05, 0xF0,
+ 0x2B, 0x38, 0xD4, 0x2A, 0x38, 0x9E, 0x5A, 0x56},
+ {0x64, 0xDF, 0x83, 0xF8, 0x5A, 0x53, 0x2F, 0x17,
+ 0x57, 0x7D, 0x8C, 0x37, 0x03, 0x57, 0x96, 0xAB},
+ {0x3E, 0x4F, 0xBD, 0xF3, 0x0F, 0xB8, 0x25, 0x9C,
+ 0x42, 0x5C, 0xB6, 0xC9, 0x6F, 0x1F, 0x46, 0x35}
+ };
+ static unsigned char iv[16] = { 0 };
+ unsigned char result[16] = { 0 };
+
+ for (i = 0; i < 3; i++) {
+ p = 0;
+ params[p++] = OSSL_PARAM_construct_utf8_string(
+ OSSL_KDF_PARAM_CIPHER, "CAMELLIA-128-CBC", 0);
+ params[p++] = OSSL_PARAM_construct_utf8_string(
+ OSSL_KDF_PARAM_MAC, "CMAC", 0);
+ params[p++] = OSSL_PARAM_construct_utf8_string(
+ OSSL_KDF_PARAM_MODE, "FEEDBACK", 0);
+ params[p++] = OSSL_PARAM_construct_octet_string(
+ OSSL_KDF_PARAM_KEY, input_key, sizeof(input_key));
+ params[p++] = OSSL_PARAM_construct_octet_string(
+ OSSL_KDF_PARAM_SALT, constants[i], sizeof(constants[i]));
+ params[p++] = OSSL_PARAM_construct_octet_string(
+ OSSL_KDF_PARAM_SEED, iv, sizeof(iv));
+ params[p] = OSSL_PARAM_construct_end();
+
+ kctx = get_kdfbyname("KBKDF");
+ ret = TEST_ptr(kctx)
+ && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
+ && TEST_int_gt(EVP_KDF_derive(kctx, result, sizeof(result)), 0)
+ && TEST_mem_eq(result, sizeof(result), outputs[i],
+ sizeof(outputs[i]));
+ EVP_KDF_CTX_free(kctx);
+ if (ret != 1)
+ return ret;
+ }
+
+ return ret;
+}
+
+static int test_kdf_kbkdf_6803_256(void)
+{
+ int ret = 0, i, p;
+ EVP_KDF_CTX *kctx;
+ OSSL_PARAM params[7];
+ static unsigned char input_key[] = {
+ 0xB9, 0xD6, 0x82, 0x8B, 0x20, 0x56, 0xB7, 0xBE,
+ 0x65, 0x6D, 0x88, 0xA1, 0x23, 0xB1, 0xFA, 0xC6,
+ 0x82, 0x14, 0xAC, 0x2B, 0x72, 0x7E, 0xCF, 0x5F,
+ 0x69, 0xAF, 0xE0, 0xC4, 0xDF, 0x2A, 0x6D, 0x2C,
+ };
+ static unsigned char constants[][5] = {
+ { 0x00, 0x00, 0x00, 0x02, 0x99 },
+ { 0x00, 0x00, 0x00, 0x02, 0xaa },
+ { 0x00, 0x00, 0x00, 0x02, 0x55 },
+ };
+ static unsigned char outputs[][32] = {
+ {0xE4, 0x67, 0xF9, 0xA9, 0x55, 0x2B, 0xC7, 0xD3,
+ 0x15, 0x5A, 0x62, 0x20, 0xAF, 0x9C, 0x19, 0x22,
+ 0x0E, 0xEE, 0xD4, 0xFF, 0x78, 0xB0, 0xD1, 0xE6,
+ 0xA1, 0x54, 0x49, 0x91, 0x46, 0x1A, 0x9E, 0x50,
+ },
+ {0x41, 0x2A, 0xEF, 0xC3, 0x62, 0xA7, 0x28, 0x5F,
+ 0xC3, 0x96, 0x6C, 0x6A, 0x51, 0x81, 0xE7, 0x60,
+ 0x5A, 0xE6, 0x75, 0x23, 0x5B, 0x6D, 0x54, 0x9F,
+ 0xBF, 0xC9, 0xAB, 0x66, 0x30, 0xA4, 0xC6, 0x04,
+ },
+ {0xFA, 0x62, 0x4F, 0xA0, 0xE5, 0x23, 0x99, 0x3F,
+ 0xA3, 0x88, 0xAE, 0xFD, 0xC6, 0x7E, 0x67, 0xEB,
+ 0xCD, 0x8C, 0x08, 0xE8, 0xA0, 0x24, 0x6B, 0x1D,
+ 0x73, 0xB0, 0xD1, 0xDD, 0x9F, 0xC5, 0x82, 0xB0,
+ },
+ };
+ static unsigned char iv[16] = { 0 };
+ unsigned char result[32] = { 0 };
+
+ for (i = 0; i < 3; i++) {
+ p = 0;
+ params[p++] = OSSL_PARAM_construct_utf8_string(
+ OSSL_KDF_PARAM_CIPHER, "CAMELLIA-256-CBC", 0);
+ params[p++] = OSSL_PARAM_construct_utf8_string(
+ OSSL_KDF_PARAM_MAC, "CMAC", 0);
+ params[p++] = OSSL_PARAM_construct_utf8_string(
+ OSSL_KDF_PARAM_MODE, "FEEDBACK", 0);
+ params[p++] = OSSL_PARAM_construct_octet_string(
+ OSSL_KDF_PARAM_KEY, input_key, sizeof(input_key));
+ params[p++] = OSSL_PARAM_construct_octet_string(
+ OSSL_KDF_PARAM_SALT, constants[i], sizeof(constants[i]));
+ params[p++] = OSSL_PARAM_construct_octet_string(
+ OSSL_KDF_PARAM_SEED, iv, sizeof(iv));
+ params[p] = OSSL_PARAM_construct_end();
+
+ kctx = get_kdfbyname("KBKDF");
+ ret = TEST_ptr(kctx)
+ && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
+ && TEST_int_gt(EVP_KDF_derive(kctx, result, sizeof(result)), 0)
+ && TEST_mem_eq(result, sizeof(result), outputs[i],
+ sizeof(outputs[i]));
+ EVP_KDF_CTX_free(kctx);
+ if (ret != 1)
+ return ret;
+ }
+
+ return ret;
+}
+#endif
+
+static OSSL_PARAM *construct_kbkdf_params(char *digest, char *mac, unsigned char *key,
+ size_t keylen, char *salt, char *info)
+{
+ OSSL_PARAM *params = OPENSSL_malloc(sizeof(OSSL_PARAM) * 7);
+ OSSL_PARAM *p = params;
+
+ *p++ = OSSL_PARAM_construct_utf8_string(
+ OSSL_KDF_PARAM_DIGEST, digest, 0);
+ *p++ = OSSL_PARAM_construct_utf8_string(
+ OSSL_KDF_PARAM_MAC, mac, 0);
+ *p++ = OSSL_PARAM_construct_utf8_string(
+ OSSL_KDF_PARAM_MODE, "COUNTER", 0);
+ *p++ = OSSL_PARAM_construct_octet_string(
+ OSSL_KDF_PARAM_KEY, key, keylen);
+ *p++ = OSSL_PARAM_construct_octet_string(
+ OSSL_KDF_PARAM_SALT, salt, strlen(salt));
+ *p++ = OSSL_PARAM_construct_octet_string(
+ OSSL_KDF_PARAM_INFO, info, strlen(info));
+ *p = OSSL_PARAM_construct_end();
+
+ return params;
+}
+
+static int test_kdf_kbkdf_invalid_digest(void)
+{
+ int ret;
+ EVP_KDF_CTX *kctx;
+ OSSL_PARAM *params;
+
+ static unsigned char key[] = {0x01};
+
+ params = construct_kbkdf_params("blah", "HMAC", key, 1, "prf", "test");
+
+ /* Negative test case - set_params should fail */
+ kctx = get_kdfbyname("KBKDF");
+ ret = TEST_ptr(kctx)
+ && TEST_false(EVP_KDF_CTX_set_params(kctx, params));
+
+ EVP_KDF_CTX_free(kctx);
+ OPENSSL_free(params);
+ return ret;
+}
+
+static int test_kdf_kbkdf_invalid_mac(void)
+{
+ int ret;
+ EVP_KDF_CTX *kctx;
+ OSSL_PARAM *params;
+
+ static unsigned char key[] = {0x01};
+
+ params = construct_kbkdf_params("sha256", "blah", key, 1, "prf", "test");
+
+ /* Negative test case - set_params should fail */
+ kctx = get_kdfbyname("KBKDF");
+ ret = TEST_ptr(kctx)
+ && TEST_false(EVP_KDF_CTX_set_params(kctx, params));
+
+ EVP_KDF_CTX_free(kctx);
+ OPENSSL_free(params);
+ return ret;
+}
+
+static int test_kdf_kbkdf_empty_key(void)
+{
+ int ret;
+ EVP_KDF_CTX *kctx;
+ OSSL_PARAM *params;
+
+ static unsigned char key[] = {0x01};
+ unsigned char result[32] = { 0 };
+
+ params = construct_kbkdf_params("sha256", "HMAC", key, 0, "prf", "test");
+
+ /* Negative test case - derive should fail */
+ kctx = get_kdfbyname("KBKDF");
+ ret = TEST_ptr(kctx)
+ && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
+ && TEST_int_eq(EVP_KDF_derive(kctx, result, sizeof(result)), 0);
+
+ EVP_KDF_CTX_free(kctx);
+ OPENSSL_free(params);
+ return ret;
+}
+
+static int test_kdf_kbkdf_1byte_key(void)
+{
+ int ret;
+ EVP_KDF_CTX *kctx;
+ OSSL_PARAM *params;
+
+ static unsigned char key[] = {0x01};
+ unsigned char result[32] = { 0 };
+
+ params = construct_kbkdf_params("sha256", "HMAC", key, 1, "prf", "test");
+
+ kctx = get_kdfbyname("KBKDF");
+ ret = TEST_ptr(kctx)
+ && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
+ && TEST_int_gt(EVP_KDF_derive(kctx, result, sizeof(result)), 0);
+
+ EVP_KDF_CTX_free(kctx);
+ OPENSSL_free(params);
+ return ret;
+}
+
+static int test_kdf_kbkdf_zero_output_size(void)
+{
+ int ret;
+ EVP_KDF_CTX *kctx;
+ OSSL_PARAM *params;
+
+ static unsigned char key[] = {0x01};
+ unsigned char result[32] = { 0 };
+
+ params = construct_kbkdf_params("sha256", "HMAC", key, 1, "prf", "test");
+
+ /* Negative test case - derive should fail */
+ kctx = get_kdfbyname("KBKDF");
+ ret = TEST_ptr(kctx)
+ && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
+ && TEST_int_eq(EVP_KDF_derive(kctx, result, 0), 0);
+
+ EVP_KDF_CTX_free(kctx);
+ OPENSSL_free(params);
+ return ret;
+}
+
+/* Two test vectors from RFC 8009 (AES Encryption with HMAC-SHA2 for Kerberos
+ * 5) appendix A. */
+static int test_kdf_kbkdf_8009_prf1(void)
+{
+ int ret, i = 0;
+ EVP_KDF_CTX *kctx;
+ OSSL_PARAM params[6];
+ char *label = "prf", *digest = "sha256", *prf_input = "test",
+ *mac = "HMAC";
+ static unsigned char input_key[] = {
+ 0x37, 0x05, 0xD9, 0x60, 0x80, 0xC1, 0x77, 0x28,
+ 0xA0, 0xE8, 0x00, 0xEA, 0xB6, 0xE0, 0xD2, 0x3C,
+ };
+ static unsigned char output[] = {
+ 0x9D, 0x18, 0x86, 0x16, 0xF6, 0x38, 0x52, 0xFE,
+ 0x86, 0x91, 0x5B, 0xB8, 0x40, 0xB4, 0xA8, 0x86,
+ 0xFF, 0x3E, 0x6B, 0xB0, 0xF8, 0x19, 0xB4, 0x9B,
+ 0x89, 0x33, 0x93, 0xD3, 0x93, 0x85, 0x42, 0x95,
+ };
+ unsigned char result[sizeof(output)] = { 0 };
+
+ params[i++] = OSSL_PARAM_construct_utf8_string(
+ OSSL_KDF_PARAM_DIGEST, digest, 0);
+ params[i++] = OSSL_PARAM_construct_utf8_string(
+ OSSL_KDF_PARAM_MAC, mac, 0);
+ params[i++] = OSSL_PARAM_construct_octet_string(
+ OSSL_KDF_PARAM_KEY, input_key, sizeof(input_key));
+ params[i++] = OSSL_PARAM_construct_octet_string(
+ OSSL_KDF_PARAM_SALT, label, strlen(label));
+ params[i++] = OSSL_PARAM_construct_octet_string(
+ OSSL_KDF_PARAM_INFO, prf_input, strlen(prf_input));
+ params[i] = OSSL_PARAM_construct_end();
+
+ kctx = get_kdfbyname("KBKDF");
+ ret = TEST_ptr(kctx)
+ && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
+ && TEST_int_gt(EVP_KDF_derive(kctx, result, sizeof(result)), 0)
+ && TEST_mem_eq(result, sizeof(result), output, sizeof(output));
+
+ EVP_KDF_CTX_free(kctx);
+ return ret;
+}
+
+static int test_kdf_kbkdf_8009_prf2(void)
+{
+ int ret, i = 0;
+ EVP_KDF_CTX *kctx;
+ OSSL_PARAM params[6];
+ char *label = "prf", *digest = "sha384", *prf_input = "test",
+ *mac = "HMAC";
+ static unsigned char input_key[] = {
+ 0x6D, 0x40, 0x4D, 0x37, 0xFA, 0xF7, 0x9F, 0x9D,
+ 0xF0, 0xD3, 0x35, 0x68, 0xD3, 0x20, 0x66, 0x98,
+ 0x00, 0xEB, 0x48, 0x36, 0x47, 0x2E, 0xA8, 0xA0,
+ 0x26, 0xD1, 0x6B, 0x71, 0x82, 0x46, 0x0C, 0x52,
+ };
+ static unsigned char output[] = {
+ 0x98, 0x01, 0xF6, 0x9A, 0x36, 0x8C, 0x2B, 0xF6,
+ 0x75, 0xE5, 0x95, 0x21, 0xE1, 0x77, 0xD9, 0xA0,
+ 0x7F, 0x67, 0xEF, 0xE1, 0xCF, 0xDE, 0x8D, 0x3C,
+ 0x8D, 0x6F, 0x6A, 0x02, 0x56, 0xE3, 0xB1, 0x7D,
+ 0xB3, 0xC1, 0xB6, 0x2A, 0xD1, 0xB8, 0x55, 0x33,
+ 0x60, 0xD1, 0x73, 0x67, 0xEB, 0x15, 0x14, 0xD2,
+ };
+ unsigned char result[sizeof(output)] = { 0 };
+
+ params[i++] = OSSL_PARAM_construct_utf8_string(
+ OSSL_KDF_PARAM_DIGEST, digest, 0);
+ params[i++] = OSSL_PARAM_construct_utf8_string(
+ OSSL_KDF_PARAM_MAC, mac, 0);
+ params[i++] = OSSL_PARAM_construct_octet_string(
+ OSSL_KDF_PARAM_KEY, input_key, sizeof(input_key));
+ params[i++] = OSSL_PARAM_construct_octet_string(
+ OSSL_KDF_PARAM_SALT, label, strlen(label));
+ params[i++] = OSSL_PARAM_construct_octet_string(
+ OSSL_KDF_PARAM_INFO, prf_input, strlen(prf_input));
+ params[i] = OSSL_PARAM_construct_end();
+
+ kctx = get_kdfbyname("KBKDF");
+ ret = TEST_ptr(kctx)
+ && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
+ && TEST_int_gt(EVP_KDF_derive(kctx, result, sizeof(result)), 0)
+ && TEST_mem_eq(result, sizeof(result), output, sizeof(output));
+
+ EVP_KDF_CTX_free(kctx);
+ return ret;
+}
+
+#if !defined(OPENSSL_NO_CMAC)
+/*
+ * Test vector taken from
+ * https://csrc.nist.gov/CSRC/media/Projects/
+ * Cryptographic-Algorithm-Validation-Program/documents/KBKDF800-108/CounterMode.zip
+ * Note: Only 32 bit counter is supported ([RLEN=32_BITS])
+ */
+static int test_kdf_kbkdf_fixedinfo(void)
+{
+ int ret;
+ EVP_KDF_CTX *kctx;
+ OSSL_PARAM params[8], *p = params;
+ static char *cipher = "AES128";
+ static char *mac = "CMAC";
+ static char *mode = "COUNTER";
+ int use_l = 0;
+ int use_separator = 0;
+
+ static unsigned char input_key[] = {
+ 0xc1, 0x0b, 0x15, 0x2e, 0x8c, 0x97, 0xb7, 0x7e,
+ 0x18, 0x70, 0x4e, 0x0f, 0x0b, 0xd3, 0x83, 0x05,
+ };
+ static unsigned char fixed_input[] = {
+ 0x98, 0xcd, 0x4c, 0xbb, 0xbe, 0xbe, 0x15, 0xd1,
+ 0x7d, 0xc8, 0x6e, 0x6d, 0xba, 0xd8, 0x00, 0xa2,
+ 0xdc, 0xbd, 0x64, 0xf7, 0xc7, 0xad, 0x0e, 0x78,
+ 0xe9, 0xcf, 0x94, 0xff, 0xdb, 0xa8, 0x9d, 0x03,
+ 0xe9, 0x7e, 0xad, 0xf6, 0xc4, 0xf7, 0xb8, 0x06,
+ 0xca, 0xf5, 0x2a, 0xa3, 0x8f, 0x09, 0xd0, 0xeb,
+ 0x71, 0xd7, 0x1f, 0x49, 0x7b, 0xcc, 0x69, 0x06,
+ 0xb4, 0x8d, 0x36, 0xc4,
+
+ };
+ static unsigned char output[] = {
+ 0x26, 0xfa, 0xf6, 0x19, 0x08, 0xad, 0x9e, 0xe8,
+ 0x81, 0xb8, 0x30, 0x5c, 0x22, 0x1d, 0xb5, 0x3f,
+ };
+ unsigned char result[sizeof(output)] = { 0 };
+
+ *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_CIPHER, cipher, 0);
+ *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_MAC, mac, 0);
+ *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_MODE, mode, 0);
+ *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, input_key,
+ sizeof(input_key));
+ *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO,
+ fixed_input, sizeof(fixed_input));
+ *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_KBKDF_USE_L, &use_l);
+ *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_KBKDF_USE_SEPARATOR,
+ &use_separator);
+ *p = OSSL_PARAM_construct_end();
+
+ kctx = get_kdfbyname("KBKDF");
+ ret = TEST_ptr(kctx)
+ && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
+ && TEST_int_gt(EVP_KDF_derive(kctx, result, sizeof(result)), 0)
+ && TEST_mem_eq(result, sizeof(result), output, sizeof(output));
+
+ EVP_KDF_CTX_free(kctx);
+ return ret;
+}
+#endif /* OPENSSL_NO_CMAC */
+
static int test_kdf_ss_hmac(void)
{
int ret;
*p = OSSL_PARAM_construct_end();
ret =
- TEST_ptr(kctx = get_kdfbyname(SN_sskdf))
+ TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_SSKDF))
&& TEST_true(EVP_KDF_CTX_set_params(kctx, params))
&& TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out)), 0)
&& TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
*p = OSSL_PARAM_construct_end();
ret =
- TEST_ptr(kctx = get_kdfbyname(SN_sskdf))
+ TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_SSKDF))
&& TEST_true(EVP_KDF_CTX_set_params(kctx, params))
&& TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out)), 0)
&& TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
*p = OSSL_PARAM_construct_end();
ret =
- TEST_ptr(kctx = get_kdfbyname(SN_sshkdf))
+ TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_SSHKDF))
&& TEST_true(EVP_KDF_CTX_set_params(kctx, params))
&& TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out)), 0)
&& TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
static int test_kdf_get_kdf(void)
{
- const EVP_KDF *kdf1, *kdf2;
+ EVP_KDF *kdf1 = NULL, *kdf2 = NULL;
ASN1_OBJECT *obj;
+ int ok = 1;
- return
- TEST_ptr(obj = OBJ_nid2obj(NID_id_pbkdf2))
- && TEST_ptr(kdf1 = EVP_get_kdfbyname(LN_id_pbkdf2))
- && TEST_ptr(kdf2 = EVP_get_kdfbyname(OBJ_nid2sn(OBJ_obj2nid(obj))))
- && TEST_ptr_eq(kdf1, kdf2)
- && TEST_ptr(kdf1 = EVP_get_kdfbyname(SN_tls1_prf))
- && TEST_ptr(kdf2 = EVP_get_kdfbyname(LN_tls1_prf))
- && TEST_ptr_eq(kdf1, kdf2)
- && TEST_ptr(kdf2 = EVP_get_kdfbyname(OBJ_nid2sn(NID_tls1_prf)))
- && TEST_ptr_eq(kdf1, kdf2);
+ if (!TEST_ptr(obj = OBJ_nid2obj(NID_id_pbkdf2))
+ || !TEST_ptr(kdf1 = EVP_KDF_fetch(NULL, OSSL_KDF_NAME_PBKDF2, NULL))
+ || !TEST_ptr(kdf2 = EVP_KDF_fetch(NULL, OBJ_nid2sn(OBJ_obj2nid(obj)),
+ NULL))
+ || !TEST_ptr_eq(kdf1, kdf2))
+ ok = 0;
+ EVP_KDF_free(kdf1);
+ kdf1 = NULL;
+ EVP_KDF_free(kdf2);
+ kdf2 = NULL;
+
+ if (!TEST_ptr(kdf1 = EVP_KDF_fetch(NULL, SN_tls1_prf, NULL))
+ || !TEST_ptr(kdf2 = EVP_KDF_fetch(NULL, LN_tls1_prf, NULL))
+ || !TEST_ptr_eq(kdf1, kdf2))
+ ok = 0;
+ /* kdf1 is re-used below, so don't free it here */
+ EVP_KDF_free(kdf2);
+ kdf2 = NULL;
+
+ if (!TEST_ptr(kdf2 = EVP_KDF_fetch(NULL, OBJ_nid2sn(NID_tls1_prf), NULL))
+ || !TEST_ptr_eq(kdf1, kdf2))
+ ok = 0;
+ EVP_KDF_free(kdf1);
+ kdf1 = NULL;
+ EVP_KDF_free(kdf2);
+ kdf2 = NULL;
+
+ return ok;
}
#ifndef OPENSSL_NO_CMS
};
*p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
- (char *)"sha1", sizeof("sha1"));
+ (char *)"sha1", 0);
*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, z,
sizeof(z));
*p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_CEK_ALG,
- (char *)cek_alg,
- strlen(cek_alg) + 1);
+ (char *)cek_alg, 0);
*p = OSSL_PARAM_construct_end();
ret =
- TEST_ptr(kctx = get_kdfbyname(SN_x942kdf))
+ TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_X942KDF_ASN1))
&& TEST_true(EVP_KDF_CTX_set_params(kctx, params))
&& TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out)), 0)
&& TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
}
#endif /* OPENSSL_NO_CMS */
+static int test_kdf_krb5kdf(void)
+{
+ int ret;
+ EVP_KDF_CTX *kctx;
+ OSSL_PARAM params[4], *p = params;
+ unsigned char out[16];
+ static unsigned char key[] = {
+ 0x42, 0x26, 0x3C, 0x6E, 0x89, 0xF4, 0xFC, 0x28,
+ 0xB8, 0xDF, 0x68, 0xEE, 0x09, 0x79, 0x9F, 0x15
+ };
+ static unsigned char constant[] = {
+ 0x00, 0x00, 0x00, 0x02, 0x99
+ };
+ static const unsigned char expected[sizeof(out)] = {
+ 0x34, 0x28, 0x0A, 0x38, 0x2B, 0xC9, 0x27, 0x69,
+ 0xB2, 0xDA, 0x2F, 0x9E, 0xF0, 0x66, 0x85, 0x4B
+ };
+
+ *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_CIPHER,
+ (char *)"AES-128-CBC",
+ sizeof("AES-128-CBC"));
+ *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, key,
+ sizeof(key));
+ *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_CONSTANT,
+ constant, sizeof(constant));
+ *p = OSSL_PARAM_construct_end();
+
+ ret =
+ TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_KRB5KDF))
+ && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
+ && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out)), 0)
+ && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
+
+ EVP_KDF_CTX_free(kctx);
+ return ret;
+}
+
int setup_tests(void)
{
+#if !defined(OPENSSL_NO_CMAC) && !defined(OPENSSL_NO_CAMELLIA)
+ ADD_TEST(test_kdf_kbkdf_6803_128);
+ ADD_TEST(test_kdf_kbkdf_6803_256);
+#endif
+ ADD_TEST(test_kdf_kbkdf_invalid_digest);
+ ADD_TEST(test_kdf_kbkdf_invalid_mac);
+ ADD_TEST(test_kdf_kbkdf_zero_output_size);
+ ADD_TEST(test_kdf_kbkdf_empty_key);
+ ADD_TEST(test_kdf_kbkdf_1byte_key);
+ ADD_TEST(test_kdf_kbkdf_8009_prf1);
+ ADD_TEST(test_kdf_kbkdf_8009_prf2);
+#if !defined(OPENSSL_NO_CMAC)
+ ADD_TEST(test_kdf_kbkdf_fixedinfo);
+#endif
ADD_TEST(test_kdf_get_kdf);
ADD_TEST(test_kdf_tls1_prf);
+ ADD_TEST(test_kdf_tls1_prf_invalid_digest);
+ ADD_TEST(test_kdf_tls1_prf_zero_output_size);
+ ADD_TEST(test_kdf_tls1_prf_empty_secret);
+ ADD_TEST(test_kdf_tls1_prf_1byte_secret);
+ ADD_TEST(test_kdf_tls1_prf_empty_seed);
+ ADD_TEST(test_kdf_tls1_prf_1byte_seed);
ADD_TEST(test_kdf_hkdf);
+ ADD_TEST(test_kdf_hkdf_invalid_digest);
+ ADD_TEST(test_kdf_hkdf_zero_output_size);
+ ADD_TEST(test_kdf_hkdf_empty_key);
+ ADD_TEST(test_kdf_hkdf_1byte_key);
+ ADD_TEST(test_kdf_hkdf_empty_salt);
ADD_TEST(test_kdf_pbkdf2);
+ ADD_TEST(test_kdf_pbkdf2_small_output);
+ ADD_TEST(test_kdf_pbkdf2_large_output);
+ ADD_TEST(test_kdf_pbkdf2_small_salt);
+ ADD_TEST(test_kdf_pbkdf2_small_iterations);
+ ADD_TEST(test_kdf_pbkdf2_small_salt_pkcs5);
+ ADD_TEST(test_kdf_pbkdf2_small_iterations_pkcs5);
+ ADD_TEST(test_kdf_pbkdf2_invalid_digest);
#ifndef OPENSSL_NO_SCRYPT
ADD_TEST(test_kdf_scrypt);
#endif
#ifndef OPENSSL_NO_CMS
ADD_TEST(test_kdf_x942_asn1);
#endif
+ ADD_TEST(test_kdf_krb5kdf);
return 1;
}