EMSA_PSS.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (c) 2020, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Array.h>
  8. #include <AK/Format.h>
  9. #include <AK/Memory.h>
  10. #include <AK/Random.h>
  11. #include <AK/Vector.h>
  12. #include <LibCrypto/PK/Code/Code.h>
  13. namespace Crypto::PK {
  14. template<typename HashFunction, size_t SaltSize>
  15. class EMSA_PSS : public Code<HashFunction> {
  16. public:
  17. template<typename... Args>
  18. EMSA_PSS(Args... args)
  19. : Code<HashFunction>(args...)
  20. {
  21. m_buffer = Bytes { m_data_buffer, sizeof(m_data_buffer) };
  22. }
  23. static constexpr auto SaltLength = SaltSize;
  24. virtual void encode(ReadonlyBytes in, ByteBuffer& out, size_t em_bits) override
  25. {
  26. // FIXME: we're supposed to check if in.size() > HashFunction::input_limitation
  27. // however, all of our current hash functions can hash unlimited blocks
  28. auto& hash_fn = this->hasher();
  29. hash_fn.update(in);
  30. auto message_hash = hash_fn.digest();
  31. constexpr auto hash_length = HashFunction::DigestSize;
  32. auto em_length = (em_bits + 7) / 8;
  33. u8 salt[SaltLength];
  34. fill_with_random(salt);
  35. if (em_length < hash_length + SaltLength + 2) {
  36. dbgln("Ooops...encoding error");
  37. return;
  38. }
  39. constexpr Array<u8, 8> zeros {};
  40. m_buffer.overwrite(0, zeros.data(), 8);
  41. m_buffer.overwrite(8, message_hash.data, HashFunction::DigestSize);
  42. m_buffer.overwrite(8 + HashFunction::DigestSize, salt, SaltLength);
  43. hash_fn.update(m_buffer);
  44. auto hash = hash_fn.digest();
  45. Vector<u8, 256> DB_data;
  46. DB_data.resize(em_length - HashFunction::DigestSize - 1);
  47. Bytes DB = DB_data;
  48. auto DB_offset = 0;
  49. for (size_t i = 0; i < em_length - SaltLength - HashFunction::DigestSize - 2; ++i)
  50. DB[DB_offset++] = 0;
  51. DB[DB_offset++] = 0x01;
  52. DB.overwrite(DB_offset, salt, SaltLength);
  53. auto mask_length = em_length - HashFunction::DigestSize - 1;
  54. Vector<u8, 256> DB_mask;
  55. DB_mask.resize(mask_length);
  56. Bytes DB_mask_buffer { DB_mask };
  57. // FIXME: we should probably allow reading from u8*
  58. MGF1(ReadonlyBytes { hash.data, HashFunction::DigestSize }, mask_length, DB_mask_buffer);
  59. for (size_t i = 0; i < DB.size(); ++i)
  60. DB_data[i] ^= DB_mask[i];
  61. auto count = (8 - (em_length * 8 - em_bits));
  62. DB_data[0] &= (0xff >> count) << count;
  63. out.overwrite(0, DB.data(), DB.size());
  64. out.overwrite(DB.size(), hash.data, hash_fn.DigestSize);
  65. out[DB.size() + hash_fn.DigestSize] = 0xbc;
  66. }
  67. virtual VerificationConsistency verify(ReadonlyBytes msg, ReadonlyBytes emsg, size_t em_bits) override
  68. {
  69. auto& hash_fn = this->hasher();
  70. hash_fn.update(msg);
  71. auto message_hash = hash_fn.digest();
  72. if (emsg.size() < HashFunction::DigestSize + SaltLength + 2)
  73. return VerificationConsistency::Inconsistent;
  74. if (emsg[emsg.size() - 1] != 0xbc)
  75. return VerificationConsistency::Inconsistent;
  76. auto mask_length = emsg.size() - HashFunction::DigestSize - 1;
  77. auto masked_DB = emsg.slice(0, mask_length);
  78. auto H = emsg.slice(mask_length, HashFunction::DigestSize);
  79. auto length_to_check = 8 * emsg.size() - em_bits;
  80. auto octet = masked_DB[0];
  81. for (size_t i = 0; i < length_to_check; ++i)
  82. if ((octet >> (8 - i)) & 0x01)
  83. return VerificationConsistency::Inconsistent;
  84. Vector<u8, 256> DB_mask;
  85. DB_mask.resize(mask_length);
  86. Bytes DB_mask_buffer { DB_mask };
  87. MGF1(H, mask_length, DB_mask_buffer);
  88. Vector<u8, 256> DB;
  89. DB.resize(mask_length);
  90. for (size_t i = 0; i < mask_length; ++i)
  91. DB[i] = masked_DB[i] ^ DB_mask[i];
  92. DB[0] &= 0xff >> (8 - length_to_check);
  93. auto check_octets = emsg.size() - HashFunction::DigestSize - SaltLength - 2;
  94. for (size_t i = 0; i < check_octets; ++i) {
  95. if (DB[i])
  96. return VerificationConsistency::Inconsistent;
  97. }
  98. if (DB[check_octets + 1] != 0x01)
  99. return VerificationConsistency::Inconsistent;
  100. auto* salt = DB.span().offset(mask_length - SaltLength);
  101. u8 m_prime[8 + HashFunction::DigestSize + SaltLength] { 0 };
  102. auto m_prime_buffer = Bytes { m_prime, sizeof(m_prime) };
  103. m_prime_buffer.overwrite(8, message_hash.data, HashFunction::DigestSize);
  104. m_prime_buffer.overwrite(8 + HashFunction::DigestSize, salt, SaltLength);
  105. hash_fn.update(m_prime_buffer);
  106. auto H_prime = hash_fn.digest();
  107. if (!timing_safe_compare(message_hash.data, H_prime.data, HashFunction::DigestSize))
  108. return VerificationConsistency::Inconsistent;
  109. return VerificationConsistency::Consistent;
  110. }
  111. void MGF1(ReadonlyBytes seed, size_t length, Bytes out)
  112. {
  113. auto& hash_fn = this->hasher();
  114. ByteBuffer T;
  115. for (size_t counter = 0; counter < length / HashFunction::DigestSize - 1; ++counter) {
  116. hash_fn.update(seed);
  117. hash_fn.update((u8*)&counter, 4);
  118. if (auto result = T.try_append(hash_fn.digest().data, HashFunction::DigestSize); result.is_error()) {
  119. dbgln("EMSA_PSS: MGF1 digest failed: {}", result.error());
  120. return;
  121. }
  122. }
  123. out.overwrite(0, T.data(), length);
  124. }
  125. private:
  126. u8 m_data_buffer[8 + HashFunction::DigestSize + SaltLength];
  127. Bytes m_buffer;
  128. };
  129. }