scrypt.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 scrypt_salt[] = {
  32. 'N', 'a', 'C', 'l'
  33. };
  34. /*
  35. * The SCRYPT parameters 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 scrypt_n = 1024;
  40. static unsigned int scrypt_r = 8;
  41. static unsigned int scrypt_p = 16;
  42. static const unsigned char expected_output[] = {
  43. 0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00,
  44. 0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9, 0xfe,
  45. 0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30,
  46. 0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37, 0x31, 0x62,
  47. 0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88,
  48. 0x6f, 0xf1, 0x09, 0x27, 0x9d, 0x98, 0x30, 0xda,
  49. 0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d,
  50. 0x83, 0x60, 0xcb, 0xdf, 0xa2, 0xcc, 0x06, 0x40
  51. };
  52. int main(int argc, char **argv)
  53. {
  54. int ret = EXIT_FAILURE;
  55. EVP_KDF *kdf = NULL;
  56. EVP_KDF_CTX *kctx = NULL;
  57. unsigned char out[64];
  58. OSSL_PARAM params[6], *p = params;
  59. OSSL_LIB_CTX *library_context = NULL;
  60. library_context = OSSL_LIB_CTX_new();
  61. if (library_context == NULL) {
  62. fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");
  63. goto end;
  64. }
  65. /* Fetch the key derivation function implementation */
  66. kdf = EVP_KDF_fetch(library_context, "SCRYPT", NULL);
  67. if (kdf == NULL) {
  68. fprintf(stderr, "EVP_KDF_fetch() returned NULL\n");
  69. goto end;
  70. }
  71. /* Create a context for the key derivation operation */
  72. kctx = EVP_KDF_CTX_new(kdf);
  73. if (kctx == NULL) {
  74. fprintf(stderr, "EVP_KDF_CTX_new() returned NULL\n");
  75. goto end;
  76. }
  77. /* Set password */
  78. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD, password,
  79. sizeof(password));
  80. /* Set salt */
  81. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, scrypt_salt,
  82. sizeof(scrypt_salt));
  83. /* Set N (default 1048576) */
  84. *p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_SCRYPT_N, &scrypt_n);
  85. /* Set R (default 8) */
  86. *p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_SCRYPT_R, &scrypt_r);
  87. /* Set P (default 1) */
  88. *p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_SCRYPT_P, &scrypt_p);
  89. *p = OSSL_PARAM_construct_end();
  90. /* Derive the key */
  91. if (EVP_KDF_derive(kctx, out, sizeof(out), params) != 1) {
  92. fprintf(stderr, "EVP_KDF_derive() failed\n");
  93. goto end;
  94. }
  95. if (CRYPTO_memcmp(expected_output, out, sizeof(expected_output)) != 0) {
  96. fprintf(stderr, "Generated key does not match expected value\n");
  97. goto end;
  98. }
  99. printf("Success\n");
  100. ret = EXIT_SUCCESS;
  101. end:
  102. EVP_KDF_CTX_free(kctx);
  103. EVP_KDF_free(kdf);
  104. OSSL_LIB_CTX_free(library_context);
  105. return ret;
  106. }