Class: CryptoToolchain::SRP::SimpleServer

Inherits:
Server show all
Includes:
Framework
Defined in:
lib/crypto_toolchain/srp/simple_server.rb

Constant Summary

Constants included from Framework

Framework::EVENT_WHITELIST

Instance Attribute Summary collapse

Attributes included from Framework

#email, #g, #k, #key, #n, #password, #privkey, #pubkey, #socket

Attributes inherited from Server

#client_pubkey, #v

Instance Method Summary collapse

Methods included from Framework

#error_received, #event_loop, #go!, #shutdown_received, #write_message

Constructor Details

#initialize(n: CryptoToolchain::NIST_P, g: CryptoToolchain::NIST_G, k: 3, email: "[email protected]", password: "i<3porkchops", privkey: nil, pubkey: nil, u: (rand(1..0x0000ffff)), malicious: false, salt: rand(1..0xffffffff), socket:) ⇒ SimpleServer

Returns a new instance of SimpleServer.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/crypto_toolchain/srp/simple_server.rb', line 6

def initialize(n: CryptoToolchain::NIST_P, g: CryptoToolchain::NIST_G,
              k: 3, email: "[email protected]", password: "i<3porkchops",
              privkey: nil, pubkey: nil, u: (rand(1..0x0000ffff)), malicious: false,
              salt: rand(1..0xffffffff), socket: )
  @n        = n
  @g        = g
  @k        = k
  @email    = email,
  @password = password
  @socket   = socket
  @privkey  = privkey || rand(1..0xffffffff) % n
  @pubkey    = pubkey || g.modpow(@privkey, n)
  @u         = u
  @salt      = salt
  xH         = Digest::SHA256.hexdigest("#{salt}#{password}")
  x          = xH.to_i(16)
  @v         = g.modpow(x, n)
  @malicious = malicious
end

Instance Attribute Details

#maliciousObject (readonly) Also known as: malicious?

Returns the value of attribute malicious.



26
27
28
# File 'lib/crypto_toolchain/srp/simple_server.rb', line 26

def malicious
  @malicious
end

#recovered_passwordObject (readonly)

Returns the value of attribute recovered_password.



26
27
28
# File 'lib/crypto_toolchain/srp/simple_server.rb', line 26

def recovered_password
  @recovered_password
end

#saltObject (readonly)

Returns the value of attribute salt.



26
27
28
# File 'lib/crypto_toolchain/srp/simple_server.rb', line 26

def salt
  @salt
end

#uObject (readonly)

Returns the value of attribute u.



26
27
28
# File 'lib/crypto_toolchain/srp/simple_server.rb', line 26

def u
  @u
end

Instance Method Details

#crack(hmac) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/crypto_toolchain/srp/simple_server.rb', line 47

def crack(hmac)
  wordlist.each_with_index do |word, i|
    _x = Digest::SHA256.hexdigest("#{salt}#{word}").to_i(16)
    _v = g.modpow(_x, n)
    _secret = (client_pubkey * _v.modpow(u, n)).modpow(privkey, n)
    _key = Digest::SHA256.hexdigest(_secret.to_s)
    word_hmac = OpenSSL::HMAC.hexdigest("SHA256", _key, salt.to_s)
    return word if word_hmac == hmac
  end
  nil
end

#hello_received(email, _client_pubkey) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/crypto_toolchain/srp/simple_server.rb', line 29

def hello_received(email, _client_pubkey)
  @client_pubkey = _client_pubkey.to_i
  write_message("hello", salt, pubkey, u)
  #  S = (A * v**u) ** b % N
  secret = (client_pubkey * v.modpow(u, n)).modpow(privkey, n)
  puts "SimpleServer generated secret #{secret}" if DEBUG
  @key = Digest::SHA256.hexdigest(secret.to_s)
end

#verify_received(hmac) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/crypto_toolchain/srp/simple_server.rb', line 59

def verify_received(hmac)
  if malicious?
    @recovered_password = crack(hmac)
    puts "Recovered #{@recovered_password}" if DEBUG
  end
  super(hmac)
end

#wordlistObject



38
39
40
41
42
43
44
45
# File 'lib/crypto_toolchain/srp/simple_server.rb', line 38

def wordlist
  return @wordlist if defined? @wordlist
  _words = File.readlines("/usr/share/dict/words").
    shuffle[0...100].
    map(&:strip)
  _words << "i<3porkchops"
  @wordlist = _words.shuffle
end