EMSA_PSS.h 5.4 KB

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