Class: CryptoToolchain::Tools::DSARecoverPrivateKeyFromNonce

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(public_key:, message:, r:, s:, p: DSA_P, q: DSA_Q, g: DSA_G) ⇒ DSARecoverPrivateKeyFromNonce

Returns a new instance of DSARecoverPrivateKeyFromNonce.



4
5
6
7
8
9
10
11
12
# File 'lib/crypto_toolchain/tools/dsa_recover_private_key_from_nonce.rb', line 4

def initialize(public_key: , message: , r: , s: , p: DSA_P, q: DSA_Q, g: DSA_G)
  @public_key = numberize(public_key)
  @p = p
  @q = q
  @g = g
  @r = numberize(r)
  @s = numberize(s)
  @message = message
end

Instance Attribute Details

#gObject (readonly)

Returns the value of attribute g.



14
15
16
# File 'lib/crypto_toolchain/tools/dsa_recover_private_key_from_nonce.rb', line 14

def g
  @g
end

#messageObject (readonly)

Returns the value of attribute message.



14
15
16
# File 'lib/crypto_toolchain/tools/dsa_recover_private_key_from_nonce.rb', line 14

def message
  @message
end

#pObject (readonly)

Returns the value of attribute p.



14
15
16
# File 'lib/crypto_toolchain/tools/dsa_recover_private_key_from_nonce.rb', line 14

def p
  @p
end

#public_keyObject (readonly)

Returns the value of attribute public_key.



14
15
16
# File 'lib/crypto_toolchain/tools/dsa_recover_private_key_from_nonce.rb', line 14

def public_key
  @public_key
end

#qObject (readonly)

Returns the value of attribute q.



14
15
16
# File 'lib/crypto_toolchain/tools/dsa_recover_private_key_from_nonce.rb', line 14

def q
  @q
end

#rObject (readonly)

Returns the value of attribute r.



14
15
16
# File 'lib/crypto_toolchain/tools/dsa_recover_private_key_from_nonce.rb', line 14

def r
  @r
end

#sObject (readonly)

Returns the value of attribute s.



14
15
16
# File 'lib/crypto_toolchain/tools/dsa_recover_private_key_from_nonce.rb', line 14

def s
  @s
end

Instance Method Details

#execute(min: 1, max: 0xffffffff) ⇒ Object

Raises:

  • (RuntimeError)


31
32
33
34
35
36
# File 'lib/crypto_toolchain/tools/dsa_recover_private_key_from_nonce.rb', line 31

def execute(min: 1, max: 0xffffffff)
  (min..max).each do |k|
    return private_key_from(k: k) if valid_k?(k)
  end
  raise RuntimeError.new("Could not recover key")
end

#private_key_from(k:) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/crypto_toolchain/tools/dsa_recover_private_key_from_nonce.rb', line 22

def private_key_from(k: )
  #     (s * k) - H(msg)
  # x = ----------------  mod q
  #             r
  numerator = ((s * k) - CryptoToolchain::Utilities::SHA1.digest(message).to_number) % q
  denominator = r.invmod(q)
  ((numerator * denominator) % q).to_bin_string
end

#valid_k?(k) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
# File 'lib/crypto_toolchain/tools/dsa_recover_private_key_from_nonce.rb', line 16

def valid_k?(k)
  x = private_key_from(k: k)
  kp = CryptoToolchain::BlackBoxes::DSAKeypair.new(p: p, q: q, g: g, private_key: x)
  kp.public_key == public_key
end