summaryrefslogtreecommitdiff
path: root/lib/srp/authentication.rb
blob: 96f68feb0b320b95f1309db4e9ac73896951e98b (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
51
52
53
54
55
56
57
58
59
require File.expand_path(File.dirname(__FILE__) + '/util')

module SRP
  module Authentication

    include Util

    class Session
      include Util
      attr_accessor :aa, :bb

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

      def u
        calculate_u(aa, bb, PRIME_N)
      end

      def secret
        @s ||= calculate_secret
      end

      def m1
        calculate_m(aa, bb, secret)
      end

      def m2
        calculate_m(aa, m1, secret)
      end

      protected

      def calculate_secret
        base = (modpow(@verifier, u, PRIME_N) * aa) % PRIME_N
        modpow(base, @b, PRIME_N)
      end
    end

    def initialize_auth(aa)
      return Session.new(aa, verifier)
    end

    def authenticate(m, session)
      if(m == session.m1)
        return session.m2
      end
    end


  end

end