Class: CryptoToolchain::Tools::LowExponentRSASignatureForgery

Inherits:
Object
  • Object
show all
Defined in:
lib/crypto_toolchain/tools/low_exponent_rsa_signature_forgery.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message:, keypair:) ⇒ LowExponentRSASignatureForgery

Returns a new instance of LowExponentRSASignatureForgery.



5
6
7
8
# File 'lib/crypto_toolchain/tools/low_exponent_rsa_signature_forgery.rb', line 5

def initialize(message: , keypair: )
  @keypair = keypair
  @message = message
end

Instance Attribute Details

#keypairObject (readonly)

Returns the value of attribute keypair.



9
10
11
# File 'lib/crypto_toolchain/tools/low_exponent_rsa_signature_forgery.rb', line 9

def keypair
  @keypair
end

#messageObject (readonly)

Returns the value of attribute message.



9
10
11
# File 'lib/crypto_toolchain/tools/low_exponent_rsa_signature_forgery.rb', line 9

def message
  @message
end

Instance Method Details

#executeObject

Raises:

  • (RuntimeError)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/crypto_toolchain/tools/low_exponent_rsa_signature_forgery.rb', line 11

def execute
  digest = CryptoToolchain::Utilities::SHA1.digest(message)
  asn = ASN1.fetch(:sha1)
  max = (keypair.bits / 8) - (asn.bytesize + digest.bytesize + 3)
  (1..max).reverse_each do |padlen|
    forged = "\x01\xff\x00#{asn}#{digest}#{0.chr * padlen}".
      to_number.
      root(3, round: :up).
      to_bin_string
    found = keypair.verify(message, signature: forged)
    return forged if found
  end
  raise RuntimeError.new("Couldn't forge a signature")
end