pbkdf2.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright 2021-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #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>
  15. /*
  16. * test vector from
  17. * https://datatracker.ietf.org/doc/html/rfc7914
  18. */
  19. /*
  20. * Hard coding a password into an application is very bad.
  21. * It is done here solely for educational purposes.
  22. */
  23. static unsigned char password[] = {
  24. 'P', 'a', 's', 's', 'w', 'o', 'r', 'd'
  25. };
  26. /*
  27. * The salt is better not being hard coded too. Each password should have a
  28. * different salt if possible. The salt is not considered secret information
  29. * and is safe to store with an encrypted password.
  30. */
  31. static unsigned char pbkdf2_salt[] = {
  32. 'N', 'a', 'C', 'l'
  33. };
  34. /*
  35. * The iteration parameter can be variable or hard coded. The disadvantage with
  36. * hard coding them is that they cannot easily be adjusted for future
  37. * technological improvements appear.
  38. */
  39. static unsigned int pbkdf2_iterations = 80000;
  40. static const unsigned char expected_output[] = {
  41. 0x4d, 0xdc, 0xd8, 0xf6, 0x0b, 0x98, 0xbe, 0x21,
  42. 0x83, 0x0c, 0xee, 0x5e, 0xf2, 0x27, 0x01, 0xf9,
  43. 0x64, 0x1a, 0x44, 0x18, 0xd0, 0x4c, 0x04, 0x14,
  44. 0xae, 0xff, 0x08, 0x87, 0x6b, 0x34, 0xab, 0x56,
  45. 0xa1, 0xd4, 0x25, 0xa1, 0x22, 0x58, 0x33, 0x54,
  46. 0x9a, 0xdb, 0x84, 0x1b, 0x51, 0xc9, 0xb3, 0x17,
  47. 0x6a, 0x27, 0x2b, 0xde, 0xbb, 0xa1, 0xd0, 0x78,
  48. 0x47, 0x8f, 0x62, 0xb3, 0x97, 0xf3, 0x3c, 0x8d
  49. };
  50. int main(int argc, char **argv)
  51. {
  52. int ret = EXIT_FAILURE;
  53. EVP_KDF *kdf = NULL;
  54. EVP_KDF_CTX *kctx = NULL;
  55. unsigned char out[64];
  56. OSSL_PARAM params[5], *p = params;
  57. OSSL_LIB_CTX *library_context = NULL;
  58. library_context = OSSL_LIB_CTX_new();
  59. if (library_context == NULL) {
  60. fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");
  61. goto end;
  62. }
  63. /* Fetch the key derivation function implementation */
  64. kdf = EVP_KDF_fetch(library_context, "PBKDF2", NULL);
  65. if (kdf == NULL) {
  66. fprintf(stderr, "EVP_KDF_fetch() returned NULL\n");
  67. goto end;
  68. }
  69. /* Create a context for the key derivation operation */
  70. kctx = EVP_KDF_CTX_new(kdf);
  71. if (kctx == NULL) {
  72. fprintf(stderr, "EVP_KDF_CTX_new() returned NULL\n");
  73. goto end;
  74. }
  75. /* Set password */
  76. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD, password,
  77. sizeof(password));
  78. /* Set salt */
  79. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, pbkdf2_salt,
  80. sizeof(pbkdf2_salt));
  81. /* Set iteration count (default 2048) */
  82. *p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_ITER, &pbkdf2_iterations);
  83. /* Set the underlying hash function used to derive the key */
  84. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
  85. "SHA256", 0);
  86. *p = OSSL_PARAM_construct_end();
  87. /* Derive the key */
  88. if (EVP_KDF_derive(kctx, out, sizeof(out), params) != 1) {
  89. fprintf(stderr, "EVP_KDF_derive() failed\n");
  90. goto end;
  91. }
  92. if (CRYPTO_memcmp(expected_output, out, sizeof(expected_output)) != 0) {
  93. fprintf(stderr, "Generated key does not match expected value\n");
  94. goto end;
  95. }
  96. printf("Success\n");
  97. ret = EXIT_SUCCESS;
  98. end:
  99. EVP_KDF_CTX_free(kctx);
  100. EVP_KDF_free(kdf);
  101. OSSL_LIB_CTX_free(library_context);
  102. return ret;
  103. }