PBKDF2.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (c) 2023, stelar7 <dudedbz@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Math.h>
  8. #include <LibCrypto/BigInt/UnsignedBigInteger.h>
  9. namespace Crypto::Hash {
  10. // https://www.rfc-editor.org/rfc/rfc2898#section-5.2
  11. class PBKDF2 {
  12. public:
  13. template<typename PRF>
  14. static ErrorOr<ByteBuffer> derive_key(ReadonlyBytes password, ReadonlyBytes salt, u32 iterations, u32 key_length_bytes)
  15. requires requires(PRF t) {
  16. t.digest_size();
  17. }
  18. {
  19. PRF prf(password);
  20. // Note: hLen denotes the length in octets of the pseudorandom function output
  21. u32 h_len = prf.digest_size();
  22. // 1. If dkLen > (2^32 - 1) * hLen, output "derived key too long" and stop.
  23. if (key_length_bytes > (AK::pow(2.0, 32.0) - 1) * h_len)
  24. return Error::from_string_view("derived key too long"sv);
  25. // 2 . Let l be the number of hLen-octet blocks in the derived key rounding up,
  26. // and let r be the number of octets in the last block
  27. u32 l = AK::ceil_div(key_length_bytes, h_len);
  28. u32 r = key_length_bytes - (l - 1) * h_len;
  29. // 3. For each block of the derived key apply the function F defined
  30. // below to the password P, the salt S, the iteration count c, and
  31. // the block index to compute the block:
  32. ByteBuffer ui = TRY(ByteBuffer::create_zeroed(h_len));
  33. ByteBuffer ti = TRY(ByteBuffer::create_zeroed(h_len));
  34. ByteBuffer key = TRY(ByteBuffer::create_zeroed(key_length_bytes));
  35. // T_i = F (P, S, c, i)
  36. u8 iteration_bytes[4];
  37. for (u32 i = 1; i <= l; i++) {
  38. iteration_bytes[3] = i;
  39. iteration_bytes[2] = ((i >> 8) & 0xff);
  40. iteration_bytes[1] = ((i >> 16) & 0xff);
  41. iteration_bytes[0] = ((i >> 24) & 0xff);
  42. prf.update(salt);
  43. prf.update(ReadonlyBytes { iteration_bytes, 4 });
  44. auto digest = prf.digest();
  45. ui.overwrite(0, digest.immutable_data(), h_len);
  46. ti.overwrite(0, digest.immutable_data(), h_len);
  47. // U_1 = PRF (P, S || INT (i))
  48. // U_j = PRF (P, U_{j-1})
  49. // F (P, S, c, i) = U_1 \xor U_2 \xor ... \xor U_c
  50. for (u32 j = 2; j <= iterations; j++) {
  51. prf.update(ui.bytes());
  52. auto digest_inner = prf.digest();
  53. ui.overwrite(0, digest_inner.immutable_data(), h_len);
  54. UnsignedBigInteger ti_temp = UnsignedBigInteger::import_data(ti.data(), ti.size());
  55. UnsignedBigInteger ui_temp = UnsignedBigInteger::import_data(ui.data(), ui.size());
  56. UnsignedBigInteger r_temp = ti_temp.bitwise_xor(ui_temp);
  57. r_temp.export_data(ti.bytes());
  58. }
  59. // 4. Concatenate the blocks and extract the first dkLen octets to produce a derived key DK:
  60. key.overwrite((i - 1) * h_len, ti.data(), i == l ? r : h_len);
  61. }
  62. // 5. Output the derived key DK
  63. return key;
  64. }
  65. };
  66. }