summaryrefslogtreecommitdiff
path: root/lib/srp/server.rb
blob: cf213c99951b0e0dc8baa7f2e9623368bcb75666 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
require File.expand_path(File.dirname(__FILE__) + '/util')

module SRP
  class Server

    include Util

    def initialize(salt, verifier)
      @salt = salt
      @verifier = verifier
    end

    def initialize_auth(aa)
      @aa = aa
      @b = bigrand(32).hex
      # B = g^b + k v (mod N)
      @bb = (modpow(GENERATOR, @b, PRIME_N) + multiplier * @verifier) % PRIME_N
      u = calculate_u(@aa, @bb, PRIME_N)
      return @bb, u
    end

    def authenticate(m)
      u = calculate_u(@aa, @bb, PRIME_N)
      base = (modpow(@verifier, u, PRIME_N) * @aa) % PRIME_N
      server_s = modpow(base, @b, PRIME_N)
      if(m == calculate_m(@aa, @bb, server_s))
        puts "A = %x" % [@aa]
        puts "M = %x" % [m]
        puts "s = %x" % [server_s]
        return calculate_m(@aa, m, server_s)
      end
    end


    protected

    def calculate_u(aa, bb, n)
      nlen = 2 * ((('%x' % [n]).length * 4 + 7) >> 3)
      aahex = '%x' % [aa]
      bbhex = '%x' % [bb]
      return sha256_str("%x%x" % [aa, bb]).hex
      hashin = '0' * (nlen - aahex.length) + aahex \
        + '0' * (nlen - bbhex.length) + bbhex
      sha256_str(hashin).hex
    end

  end
end