Class: CryptoToolchain::BlackBoxes::DSAKeypair

Inherits:
Object
  • Object
show all
Defined in:
lib/crypto_toolchain/black_boxes/dsa_keypair.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(p: DSA_P, q: DSA_Q, g: DSA_G, private_key: nil, dangerous: false) ⇒ DSAKeypair

Returns a new instance of DSAKeypair.



4
5
6
7
8
9
10
# File 'lib/crypto_toolchain/black_boxes/dsa_keypair.rb', line 4

def initialize(p: DSA_P, q: DSA_Q, g: DSA_G, private_key: nil, dangerous: false)
  @p = p
  @q = q
  @g = g
  @private_key = numberize(private_key) unless private_key.nil?
  @safe = !dangerous
end

Instance Attribute Details

#gObject (readonly)

Returns the value of attribute g.



12
13
14
# File 'lib/crypto_toolchain/black_boxes/dsa_keypair.rb', line 12

def g
  @g
end

#pObject (readonly)

Returns the value of attribute p.



12
13
14
# File 'lib/crypto_toolchain/black_boxes/dsa_keypair.rb', line 12

def p
  @p
end

#qObject (readonly)

Returns the value of attribute q.



12
13
14
# File 'lib/crypto_toolchain/black_boxes/dsa_keypair.rb', line 12

def q
  @q
end

#safeObject (readonly)

Returns the value of attribute safe.



12
13
14
# File 'lib/crypto_toolchain/black_boxes/dsa_keypair.rb', line 12

def safe
  @safe
end

Instance Method Details

#private_keyObject



41
42
43
# File 'lib/crypto_toolchain/black_boxes/dsa_keypair.rb', line 41

def private_key
  @private_key ||= rand(1..DSA_Q)
end

#public_keyObject



45
46
47
# File 'lib/crypto_toolchain/black_boxes/dsa_keypair.rb', line 45

def public_key
  @public_key ||= g.modpow(private_key, p)
end

#sign(m, k: nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/crypto_toolchain/black_boxes/dsa_keypair.rb', line 14

def sign(m, k: nil)
  r = s = 0
  k ||= rand(2...q)
  loop do
    r = g.modpow(k, p) % q
    next if safe && r == 0
    digest = CryptoToolchain::Utilities::SHA1.digest(m).to_number
    s = k.modinv(q) * ( digest + (private_key * r)) % q
    next if safe && s == 0
    return [r.to_bin_string, s.to_bin_string]
  end
end

#verify(m, r:, s:, public_key: self.public_key) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/crypto_toolchain/black_boxes/dsa_keypair.rb', line 27

def verify(m, r: , s: , public_key: self.public_key)
  s = s.to_number
  r = r.to_number
  if safe && !(0 < r && r < q) && (0 < s && s < q)
    return false
  end
  w = s.invmod(q)
  u_1 = (CryptoToolchain::Utilities::SHA1.digest(m).to_number * w) % q
  u_2 = (r * w) % q
  # a*b % n = [(a % n) * (b % n)] % m
  v = ((g.modpow(u_1, p) * public_key.modpow(u_2, p)) % p) % q
  v == r
end