Path: blob/main/crypto/openssl/demos/kdf/pbkdf2.c
108036 views
/*1* Copyright 2021-2023 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89#include <stdio.h>10#include <openssl/core_names.h>11#include <openssl/crypto.h>12#include <openssl/kdf.h>13#include <openssl/obj_mac.h>14#include <openssl/params.h>1516/*17* test vector from18* https://datatracker.ietf.org/doc/html/rfc791419*/2021/*22* Hard coding a password into an application is very bad.23* It is done here solely for educational purposes.24*/25static unsigned char password[] = {26'P', 'a', 's', 's', 'w', 'o', 'r', 'd'27};2829/*30* The salt is better not being hard coded too. Each password should have a31* different salt if possible. The salt is not considered secret information32* and is safe to store with an encrypted password.33*/34static unsigned char pbkdf2_salt[] = {35'N', 'a', 'C', 'l'36};3738/*39* The iteration parameter can be variable or hard coded. The disadvantage with40* hard coding them is that they cannot easily be adjusted for future41* technological improvements appear.42*/43static unsigned int pbkdf2_iterations = 80000;4445static const unsigned char expected_output[] = {46470x4d, 0xdc, 0xd8, 0xf6, 0x0b, 0x98, 0xbe, 0x21,480x83, 0x0c, 0xee, 0x5e, 0xf2, 0x27, 0x01, 0xf9,490x64, 0x1a, 0x44, 0x18, 0xd0, 0x4c, 0x04, 0x14,500xae, 0xff, 0x08, 0x87, 0x6b, 0x34, 0xab, 0x56,510xa1, 0xd4, 0x25, 0xa1, 0x22, 0x58, 0x33, 0x54,520x9a, 0xdb, 0x84, 0x1b, 0x51, 0xc9, 0xb3, 0x17,530x6a, 0x27, 0x2b, 0xde, 0xbb, 0xa1, 0xd0, 0x78,540x47, 0x8f, 0x62, 0xb3, 0x97, 0xf3, 0x3c, 0x8d55};5657int main(int argc, char **argv)58{59int ret = EXIT_FAILURE;60EVP_KDF *kdf = NULL;61EVP_KDF_CTX *kctx = NULL;62unsigned char out[64];63OSSL_PARAM params[5], *p = params;64OSSL_LIB_CTX *library_context = NULL;6566library_context = OSSL_LIB_CTX_new();67if (library_context == NULL) {68fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");69goto end;70}7172/* Fetch the key derivation function implementation */73kdf = EVP_KDF_fetch(library_context, "PBKDF2", NULL);74if (kdf == NULL) {75fprintf(stderr, "EVP_KDF_fetch() returned NULL\n");76goto end;77}7879/* Create a context for the key derivation operation */80kctx = EVP_KDF_CTX_new(kdf);81if (kctx == NULL) {82fprintf(stderr, "EVP_KDF_CTX_new() returned NULL\n");83goto end;84}8586/* Set password */87*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD, password,88sizeof(password));89/* Set salt */90*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, pbkdf2_salt,91sizeof(pbkdf2_salt));92/* Set iteration count (default 2048) */93*p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_ITER, &pbkdf2_iterations);94/* Set the underlying hash function used to derive the key */95*p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,96"SHA256", 0);97*p = OSSL_PARAM_construct_end();9899/* Derive the key */100if (EVP_KDF_derive(kctx, out, sizeof(out), params) != 1) {101fprintf(stderr, "EVP_KDF_derive() failed\n");102goto end;103}104105if (CRYPTO_memcmp(expected_output, out, sizeof(expected_output)) != 0) {106fprintf(stderr, "Generated key does not match expected value\n");107goto end;108}109110printf("Success\n");111112ret = EXIT_SUCCESS;113end:114EVP_KDF_CTX_free(kctx);115EVP_KDF_free(kdf);116OSSL_LIB_CTX_free(library_context);117return ret;118}119120121