Path: blob/main/crypto/openssl/demos/kdf/scrypt.c
105264 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 scrypt_salt[] = {35'N', 'a', 'C', 'l'36};3738/*39* The SCRYPT parameters 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 scrypt_n = 1024;44static unsigned int scrypt_r = 8;45static unsigned int scrypt_p = 16;4647static const unsigned char expected_output[] = {48490xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00,500x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9, 0xfe,510x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30,520xe7, 0x73, 0x76, 0x63, 0x4b, 0x37, 0x31, 0x62,530x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88,540x6f, 0xf1, 0x09, 0x27, 0x9d, 0x98, 0x30, 0xda,550xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d,560x83, 0x60, 0xcb, 0xdf, 0xa2, 0xcc, 0x06, 0x4057};5859int main(int argc, char **argv)60{61int ret = EXIT_FAILURE;62EVP_KDF *kdf = NULL;63EVP_KDF_CTX *kctx = NULL;64unsigned char out[64];65OSSL_PARAM params[6], *p = params;66OSSL_LIB_CTX *library_context = NULL;6768library_context = OSSL_LIB_CTX_new();69if (library_context == NULL) {70fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");71goto end;72}7374/* Fetch the key derivation function implementation */75kdf = EVP_KDF_fetch(library_context, "SCRYPT", NULL);76if (kdf == NULL) {77fprintf(stderr, "EVP_KDF_fetch() returned NULL\n");78goto end;79}8081/* Create a context for the key derivation operation */82kctx = EVP_KDF_CTX_new(kdf);83if (kctx == NULL) {84fprintf(stderr, "EVP_KDF_CTX_new() returned NULL\n");85goto end;86}8788/* Set password */89*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD, password,90sizeof(password));91/* Set salt */92*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, scrypt_salt,93sizeof(scrypt_salt));94/* Set N (default 1048576) */95*p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_SCRYPT_N, &scrypt_n);96/* Set R (default 8) */97*p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_SCRYPT_R, &scrypt_r);98/* Set P (default 1) */99*p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_SCRYPT_P, &scrypt_p);100*p = OSSL_PARAM_construct_end();101102/* Derive the key */103if (EVP_KDF_derive(kctx, out, sizeof(out), params) != 1) {104fprintf(stderr, "EVP_KDF_derive() failed\n");105goto end;106}107108if (CRYPTO_memcmp(expected_output, out, sizeof(expected_output)) != 0) {109fprintf(stderr, "Generated key does not match expected value\n");110goto end;111}112113printf("Success\n");114115ret = EXIT_SUCCESS;116end:117EVP_KDF_CTX_free(kctx);118EVP_KDF_free(kdf);119OSSL_LIB_CTX_free(library_context);120return ret;121}122123124