summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAzul <azul@leap.se>2012-07-26 11:33:29 +0200
committerAzul <azul@leap.se>2012-07-26 11:33:29 +0200
commit933df5e096e7dd9af1491b0679d588eb4254aaa1 (patch)
tree36d3b8cc3e2992fc394ad265b67468e4e53c43a1
parenta88fa3560940f46ec1ff77f3e79d10d9bf34149a (diff)
SRP::Authentication::Session holds the per session data
-rw-r--r--lib/srp/authentication.rb50
-rw-r--r--lib/srp/client.rb8
2 files changed, 43 insertions, 15 deletions
diff --git a/lib/srp/authentication.rb b/lib/srp/authentication.rb
index f4b2e70..96f68fe 100644
--- a/lib/srp/authentication.rb
+++ b/lib/srp/authentication.rb
@@ -5,21 +5,49 @@ module SRP
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)
- @aa = aa
- @b = bigrand(32).hex
- # B = g^b + k v (mod N)
- @bb = (modpow(GENERATOR, @b, PRIME_N) + multiplier * verifier) % PRIME_N
- return @bb
+ return Session.new(aa, verifier)
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))
- return calculate_m(@aa, m, server_s)
+ def authenticate(m, session)
+ if(m == session.m1)
+ return session.m2
end
end
diff --git a/lib/srp/client.rb b/lib/srp/client.rb
index 1be2461..667d5ba 100644
--- a/lib/srp/client.rb
+++ b/lib/srp/client.rb
@@ -19,10 +19,10 @@ module SRP
x = calculate_x(username, password, salt)
a = bigrand(32).hex
aa = modpow(GENERATOR, a, PRIME_N) # A = g^a (mod N)
- bb = server.initialize_auth(aa)
- u = calculate_u(aa, bb, PRIME_N)
- client_s = calculate_client_s(x, a, bb, u)
- server.authenticate(calculate_m(aa,bb,client_s))
+ session = server.initialize_auth(aa)
+ u = calculate_u(aa, session.bb, PRIME_N)
+ client_s = calculate_client_s(x, a, session.bb, u)
+ server.authenticate(calculate_m(aa,session.bb,client_s), session)
end
protected