Path: blob/main/crypto/openssl/demos/kdf/argon2.c
108623 views
/*1* Copyright 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/params.h>14#include <openssl/thread.h>1516/*17* Example showing how to use Argon2 KDF.18* See man EVP_KDF-ARGON2 for more information.19*20* test vector from21* https://datatracker.ietf.org/doc/html/rfc910622*/2324/*25* Hard coding a password into an application is very bad.26* It is done here solely for educational purposes.27*/28static unsigned char password[] = {290x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,300x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,310x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,320x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x0133};3435/*36* The salt is better not being hard coded too. Each password should have a37* different salt if possible. The salt is not considered secret information38* and is safe to store with an encrypted password.39*/40static unsigned char salt[] = {410x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,420x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x0243};4445/*46* Optional secret for KDF47*/48static unsigned char secret[] = {490x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x0350};5152/*53* Optional additional data54*/55static unsigned char ad[] = {560x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,570x04, 0x04, 0x04, 0x0458};5960/*61* Argon2 cost parameters62*/63static uint32_t memory_cost = 32;64static uint32_t iteration_cost = 3;65static uint32_t parallel_cost = 4;6667static const unsigned char expected_output[] = {680x0d, 0x64, 0x0d, 0xf5, 0x8d, 0x78, 0x76, 0x6c,690x08, 0xc0, 0x37, 0xa3, 0x4a, 0x8b, 0x53, 0xc9,700xd0, 0x1e, 0xf0, 0x45, 0x2d, 0x75, 0xb6, 0x5e,710xb5, 0x25, 0x20, 0xe9, 0x6b, 0x01, 0xe6, 0x5972};7374int main(int argc, char **argv)75{76int rv = EXIT_FAILURE;77EVP_KDF *kdf = NULL;78EVP_KDF_CTX *kctx = NULL;79unsigned char out[32];80OSSL_PARAM params[9], *p = params;81OSSL_LIB_CTX *library_context = NULL;82unsigned int threads;8384library_context = OSSL_LIB_CTX_new();85if (library_context == NULL) {86fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");87goto end;88}8990/* Fetch the key derivation function implementation */91kdf = EVP_KDF_fetch(library_context, "argon2id", NULL);92if (kdf == NULL) {93fprintf(stderr, "EVP_KDF_fetch() returned NULL\n");94goto end;95}9697/* Create a context for the key derivation operation */98kctx = EVP_KDF_CTX_new(kdf);99if (kctx == NULL) {100fprintf(stderr, "EVP_KDF_CTX_new() returned NULL\n");101goto end;102}103104/*105* Thread support can be turned off; use serialization if we cannot106* set requested number of threads.107*/108threads = parallel_cost;109if (OSSL_set_max_threads(library_context, parallel_cost) != 1) {110uint64_t max_threads = OSSL_get_max_threads(library_context);111112if (max_threads == 0)113threads = 1;114else if (max_threads < parallel_cost)115threads = max_threads;116}117118/* Set password */119*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD, password, sizeof(password));120/* Set salt */121*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, salt, sizeof(salt));122/* Set optional additional data */123*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_ARGON2_AD, ad, sizeof(ad));124/* Set optional secret */125*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SECRET, secret, sizeof(secret));126/* Set iteration count */127*p++ = OSSL_PARAM_construct_uint32(OSSL_KDF_PARAM_ITER, &iteration_cost);128/* Set threads performing derivation (can be decreased) */129*p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_THREADS, &threads);130/* Set parallel cost */131*p++ = OSSL_PARAM_construct_uint32(OSSL_KDF_PARAM_ARGON2_LANES, ¶llel_cost);132/* Set memory requirement */133*p++ = OSSL_PARAM_construct_uint32(OSSL_KDF_PARAM_ARGON2_MEMCOST, &memory_cost);134*p = OSSL_PARAM_construct_end();135136/* Derive the key */137if (EVP_KDF_derive(kctx, out, sizeof(out), params) != 1) {138fprintf(stderr, "EVP_KDF_derive() failed\n");139goto end;140}141142if (CRYPTO_memcmp(expected_output, out, sizeof(expected_output)) != 0) {143fprintf(stderr, "Generated key does not match expected value\n");144goto end;145}146147printf("Success\n");148149rv = EXIT_SUCCESS;150end:151EVP_KDF_CTX_free(kctx);152EVP_KDF_free(kdf);153OSSL_LIB_CTX_free(library_context);154return rv;155}156157158