2
0

pbe_scrypt.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright 2015-2021 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 <openssl/evp.h>
  10. #include <openssl/err.h>
  11. #include <openssl/kdf.h>
  12. #include <openssl/core_names.h>
  13. #include "internal/numbers.h"
  14. #ifndef OPENSSL_NO_SCRYPT
  15. /*
  16. * Maximum permitted memory allow this to be overridden with Configuration
  17. * option: e.g. -DSCRYPT_MAX_MEM=0 for maximum possible.
  18. */
  19. #ifdef SCRYPT_MAX_MEM
  20. # if SCRYPT_MAX_MEM == 0
  21. # undef SCRYPT_MAX_MEM
  22. /*
  23. * Although we could theoretically allocate SIZE_MAX memory that would leave
  24. * no memory available for anything else so set limit as half that.
  25. */
  26. # define SCRYPT_MAX_MEM (SIZE_MAX/2)
  27. # endif
  28. #else
  29. /* Default memory limit: 32 MB */
  30. # define SCRYPT_MAX_MEM (1024 * 1024 * 32)
  31. #endif
  32. int EVP_PBE_scrypt_ex(const char *pass, size_t passlen,
  33. const unsigned char *salt, size_t saltlen,
  34. uint64_t N, uint64_t r, uint64_t p, uint64_t maxmem,
  35. unsigned char *key, size_t keylen,
  36. OSSL_LIB_CTX *ctx, const char *propq)
  37. {
  38. const char *empty = "";
  39. int rv = 1;
  40. EVP_KDF *kdf;
  41. EVP_KDF_CTX *kctx;
  42. OSSL_PARAM params[7], *z = params;
  43. if (r > UINT32_MAX || p > UINT32_MAX) {
  44. ERR_raise(ERR_LIB_EVP, EVP_R_PARAMETER_TOO_LARGE);
  45. return 0;
  46. }
  47. /* Maintain existing behaviour. */
  48. if (pass == NULL) {
  49. pass = empty;
  50. passlen = 0;
  51. }
  52. if (salt == NULL) {
  53. salt = (const unsigned char *)empty;
  54. saltlen = 0;
  55. }
  56. if (maxmem == 0)
  57. maxmem = SCRYPT_MAX_MEM;
  58. /* Use OSSL_LIB_CTX_set0_default() if you need a library context */
  59. kdf = EVP_KDF_fetch(ctx, OSSL_KDF_NAME_SCRYPT, propq);
  60. kctx = EVP_KDF_CTX_new(kdf);
  61. EVP_KDF_free(kdf);
  62. if (kctx == NULL)
  63. return 0;
  64. *z++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD,
  65. (unsigned char *)pass,
  66. passlen);
  67. *z++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
  68. (unsigned char *)salt, saltlen);
  69. *z++ = OSSL_PARAM_construct_uint64(OSSL_KDF_PARAM_SCRYPT_N, &N);
  70. *z++ = OSSL_PARAM_construct_uint64(OSSL_KDF_PARAM_SCRYPT_R, &r);
  71. *z++ = OSSL_PARAM_construct_uint64(OSSL_KDF_PARAM_SCRYPT_P, &p);
  72. *z++ = OSSL_PARAM_construct_uint64(OSSL_KDF_PARAM_SCRYPT_MAXMEM, &maxmem);
  73. *z = OSSL_PARAM_construct_end();
  74. if (EVP_KDF_derive(kctx, key, keylen, params) != 1)
  75. rv = 0;
  76. EVP_KDF_CTX_free(kctx);
  77. return rv;
  78. }
  79. int EVP_PBE_scrypt(const char *pass, size_t passlen,
  80. const unsigned char *salt, size_t saltlen,
  81. uint64_t N, uint64_t r, uint64_t p, uint64_t maxmem,
  82. unsigned char *key, size_t keylen)
  83. {
  84. return EVP_PBE_scrypt_ex(pass, passlen, salt, saltlen, N, r, p, maxmem,
  85. key, keylen, NULL, NULL);
  86. }
  87. #endif