EMSA_PSS.h 5.5 KB

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