EVP_KDF-ARGON2.pod 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. =pod
  2. =head1 NAME
  3. EVP_KDF-ARGON2 - The Argon2 EVP KDF implementation
  4. =head1 DESCRIPTION
  5. Support for computing the B<argon2> password-based KDF through the B<EVP_KDF>
  6. API.
  7. The EVP_KDF-ARGON2 algorithm implements the Argon2 password-based key
  8. derivation function, as described in IETF RFC 9106. It is memory-hard in
  9. the sense that it deliberately requires a significant amount of RAM for efficient
  10. computation. The intention of this is to render brute forcing of passwords on
  11. systems that lack large amounts of main memory (such as GPUs or ASICs)
  12. computationally infeasible.
  13. Argon2d (Argon2i) uses data-dependent (data-independent) memory access and
  14. primary seek to address trade-off (side-channel) attacks.
  15. Argon2id is a hybrid construction which, in the first two slices of the first
  16. pass, generates reference addresses data-independently as in Argon2i, whereas
  17. in later slices and next passes it generates them data-dependently as in
  18. Argon2d.
  19. Sbox-hardened version Argon2ds is not supported.
  20. For more information, please refer to RFC 9106.
  21. =head2 Supported parameters
  22. The supported parameters are:
  23. =over 4
  24. =item "pass" (B<OSSL_KDF_PARAM_PASSWORD>) <octet string>
  25. =item "salt" (B<OSSL_KDF_PARAM_SALT>) <octet string>
  26. =item "secret" (B<OSSL_KDF_PARAM_SECRET>) <octet string>
  27. =item "iter" (B<OSSL_KDF_PARAM_ITER>) <unsigned integer>
  28. =item "size" (B<OSSL_KDF_PARAM_SIZE>) <unsigned integer>
  29. =item "properties" (B<OSSL_KDF_PARAM_PROPERTIES>) <UTF8 string>
  30. These parameters work as described in L<EVP_KDF(3)/PARAMETERS>.
  31. Note that RFC 9106 recommends 128 bits salt for most applications, or 64 bits
  32. salt in the case of space constraints. At least 128 bits output length is
  33. recommended.
  34. Note that secret (or pepper) is an optional secret data used along the
  35. password.
  36. =item "threads" (B<OSSL_KDF_PARAM_THREADS>) <unsigned integer>
  37. The number of threads, bounded above by the number of lanes.
  38. This can only be used with built-in thread support. Threading must be
  39. explicitly enabled. See EXAMPLES section for more information.
  40. =item "ad" (B<OSSL_KDF_PARAM_ARGON2_AD>) <octet string>
  41. Optional associated data, may be used to "tag" a group of keys, or tie them
  42. to a particular public key, without having to modify salt.
  43. =item "lanes" (B<OSSL_KDF_PARAM_ARGON2_LANES>) <unsigned integer>
  44. Argon2 splits the requested memory size into lanes, each of which is designed
  45. to be processed in parallel. For example, on a system with p cores, it's
  46. recommended to use p lanes.
  47. The number of lanes is used to derive the key. It is possible to specify
  48. more lanes than the number of available computational threads. This is
  49. especially encouraged if multi-threading is disabled.
  50. =item "memcost" (B<OSSL_KDF_PARAM_ARGON2_MEMCOST>) <unsigned integer>
  51. Memory cost parameter (the number of 1k memory blocks used).
  52. =item "version" (B<OSSL_KDF_PARAM_ARGON2_VERSION>) <unsigned integer>
  53. Argon2 version. Supported values: 0x10, 0x13 (default).
  54. =item "early_clean" (B<OSSL_KDF_PARAM_EARLY_CLEAN>) <unsigned integer>
  55. If set (nonzero), password and secret stored in Argon2 context are zeroed
  56. early during initial hash computation, as soon as they are not needed.
  57. Otherwise, they are zeroed along the rest of Argon2 context data on clear,
  58. free, reset.
  59. This can be useful if, for example, multiple keys with different ad value
  60. are to be generated from a single password and secret.
  61. =back
  62. =head1 EXAMPLES
  63. This example uses Argon2d with password "1234567890", salt "saltsalt",
  64. using 2 lanes, 2 threads, and memory cost of 65536:
  65. #include <string.h> /* strlen */
  66. #include <openssl/core_names.h> /* OSSL_KDF_* */
  67. #include <openssl/params.h> /* OSSL_PARAM_* */
  68. #include <openssl/thread.h> /* OSSL_set_max_threads */
  69. #include <openssl/kdf.h> /* EVP_KDF_* */
  70. int main(void)
  71. {
  72. int retval = 1;
  73. EVP_KDF *kdf = NULL;
  74. EVP_KDF_CTX *kctx = NULL;
  75. OSSL_PARAM params[6], *p = params;
  76. /* argon2 params, please refer to RFC9106 for recommended defaults */
  77. uint32_t lanes = 2, threads = 2, memcost = 65536;
  78. char pwd[] = "1234567890", salt[] = "saltsalt";
  79. /* derive result */
  80. size_t outlen = 128;
  81. unsigned char result[outlen];
  82. /* required if threads > 1 */
  83. if (OSSL_set_max_threads(NULL, threads) != 1)
  84. goto fail;
  85. p = params;
  86. *p++ = OSSL_PARAM_construct_uint32(OSSL_KDF_PARAM_THREADS, &threads);
  87. *p++ = OSSL_PARAM_construct_uint32(OSSL_KDF_PARAM_ARGON2_LANES,
  88. &lanes);
  89. *p++ = OSSL_PARAM_construct_uint32(OSSL_KDF_PARAM_ARGON2_MEMCOST,
  90. &memcost);
  91. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
  92. salt,
  93. strlen((const char *)salt));
  94. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD,
  95. pwd,
  96. strlen((const char *)pwd));
  97. *p++ = OSSL_PARAM_construct_end();
  98. if ((kdf = EVP_KDF_fetch(NULL, "ARGON2D", NULL)) == NULL)
  99. goto fail;
  100. if ((kctx = EVP_KDF_CTX_new(kdf)) == NULL)
  101. goto fail;
  102. if (EVP_KDF_derive(kctx, &result[0], outlen, params) != 1)
  103. goto fail;
  104. printf("Output = %s\n", OPENSSL_buf2hexstr(result, outlen));
  105. retval = 0;
  106. fail:
  107. EVP_KDF_free(kdf);
  108. EVP_KDF_CTX_free(kctx);
  109. OSSL_set_max_threads(NULL, 0);
  110. return retval;
  111. }
  112. =head1 NOTES
  113. "ARGON2I", "ARGON2D", and "ARGON2ID" are the names for this implementation; it
  114. can be used with the EVP_KDF_fetch() function.
  115. =head1 CONFORMING TO
  116. RFC 9106 Argon2, see L<https://www.rfc-editor.org/rfc/rfc9106.txt>.
  117. =head1 SEE ALSO
  118. L<EVP_KDF(3)>,
  119. L<EVP_KDF_CTX_new(3)>,
  120. L<EVP_KDF_CTX_free(3)>,
  121. L<EVP_KDF_CTX_set_params(3)>,
  122. L<EVP_KDF_derive(3)>,
  123. L<EVP_KDF(3)/PARAMETERS>
  124. =head1 HISTORY
  125. This functionality was added to OpenSSL 3.2.
  126. =head1 COPYRIGHT
  127. Copyright 2022-2024 The OpenSSL Project Authors. All Rights Reserved.
  128. Licensed under the Apache License 2.0 (the "License"). You may not use
  129. this file except in compliance with the License. You can obtain a copy
  130. in the file LICENSE in the source distribution or at
  131. L<https://www.openssl.org/source/license.html>.
  132. =cut