@@ -3,8 +3,148 @@ defmodule Mix.Tasks.Demo.Srp do
33 @ requirements [ "loadpaths" , "app.config" , "app.start" ]
44
55 use Mix.Task
6+ require Logger
7+
8+ defmodule NonInteractiveClientGood do
9+ use Zkp.SrpChor.Chorex , :srpclient
10+
11+ import Zkp.SrpChor , only: [ hash_things: 1 ]
12+
13+ @ impl true
14+ def get_id ( ) do
15+ "alice"
16+ end
17+
18+ @ impl true
19+ def compute_secret ( g , n , s , big_b , k , id ) do
20+ passwd = "hello world"
21+
22+ a = Enum . random ( 2 .. n )
23+ big_a = mpow ( g , a , n )
24+ x = hash_things ( [ id , s , passwd ] )
25+ u = hash_things ( [ big_a , big_b ] )
26+
27+ secret_k =
28+ mpow (
29+ ( n + as_int ( big_b ) ) - rem ( as_int ( k ) * as_int ( mpow ( g , x , n ) ) , n ) ,
30+ a + as_int ( u ) * as_int ( x ) ,
31+ n
32+ )
33+
34+ m1 = hash_things ( [ big_a , big_b , secret_k ] )
35+ { big_a , m1 , secret_k }
36+ end
37+
38+ @ impl true
39+ def valid_m2? ( big_a , m1 , k , m2 ) do
40+ hash_things ( [ big_a , m1 , k ] ) == m2
41+ end
42+
43+ defdelegate mpow ( a , b , c ) , to: :crypto , as: :mod_pow
44+ defdelegate as_int ( n ) , to: :crypto , as: :bytes_to_integer
45+
46+ @ impl true
47+ def gen_verification_token ( username , password , salt , g , p ) do
48+ x = hash_things ( [ username , salt , password ] )
49+ mpow ( g , x , p )
50+ end
51+ end
52+
53+ defmodule NonInteractiveClientBad do
54+ use Zkp.SrpChor.Chorex , :srpclient
55+
56+ import Zkp.SrpChor , only: [ hash_things: 1 ]
57+
58+ @ impl true
59+ def get_id ( ) do
60+ "alice"
61+ end
62+
63+ @ impl true
64+ def compute_secret ( g , n , s , big_b , k , id ) do
65+ passwd = "NOT THE PASSWORD!"
66+
67+ a = Enum . random ( 2 .. n )
68+ big_a = mpow ( g , a , n )
69+ x = hash_things ( [ id , s , passwd ] )
70+ u = hash_things ( [ big_a , big_b ] )
71+
72+ secret_k =
73+ mpow (
74+ ( n + as_int ( big_b ) ) - rem ( as_int ( k ) * as_int ( mpow ( g , x , n ) ) , n ) ,
75+ a + as_int ( u ) * as_int ( x ) ,
76+ n
77+ )
78+
79+ m1 = hash_things ( [ big_a , big_b , secret_k ] )
80+ { big_a , m1 , secret_k }
81+ end
82+
83+ @ impl true
84+ def valid_m2? ( big_a , m1 , k , m2 ) do
85+ hash_things ( [ big_a , m1 , k ] ) == m2
86+ end
87+
88+ defdelegate mpow ( a , b , c ) , to: :crypto , as: :mod_pow
89+ defdelegate as_int ( n ) , to: :crypto , as: :bytes_to_integer
90+
91+ @ impl true
92+ def gen_verification_token ( username , password , salt , g , p ) do
93+ x = hash_things ( [ username , salt , password ] )
94+ mpow ( g , x , p )
95+ end
96+ end
697
798 def run ( _args ) do
8- IO . puts ( "Hey look running the SRP demo!" )
99+ Logger . info ( "Starting non-interactive SRP login demo" )
100+ Logger . info ( "Registration: using username 'alice', password 'hello world'" )
101+ Logger . info ( "You should see 'Server responds {:registered, \" alice\" }' and 'Client responds :registered'" )
102+
103+ Chorex . start ( Zkp.SrpChor.Chorex ,
104+ % { SrpServer => Zkp.SrpServerImpl ,
105+ SrpClient => NonInteractiveClientGood } ,
106+ [ { "alice" , "hello world" } , :register ] )
107+
108+ receive do
109+ { :chorex_return , SrpServer , resp } -> IO . puts ( "Server responds #{ inspect resp } " )
110+ end
111+
112+ receive do
113+ { :chorex_return , SrpClient , resp } -> IO . puts ( "Client responds #{ inspect resp } " )
114+ end
115+
116+ Logger . info ( "Registration successful. Attempting login with good credentials." )
117+ Logger . info ( "You should see the same value twice---this means the server and client have computed the same value." )
118+
119+ Chorex . start ( Zkp.SrpChor.Chorex ,
120+ % { SrpServer => Zkp.SrpServerImpl ,
121+ SrpClient => NonInteractiveClientGood } ,
122+ [ ] )
123+
124+ receive do
125+ { :chorex_return , SrpServer , resp } -> IO . puts ( "Server responds #{ inspect resp } " )
126+ end
127+
128+ receive do
129+ { :chorex_return , SrpClient , resp } -> IO . puts ( "Client responds #{ inspect resp } " )
130+ end
131+
132+ Logger . info ( "Now trying to login with bad credentials." )
133+ Logger . info ( "You should see server and client rejecting the digest" )
134+
135+ Chorex . start ( Zkp.SrpChor.Chorex ,
136+ % { SrpServer => Zkp.SrpServerImpl ,
137+ SrpClient => NonInteractiveClientBad } ,
138+ [ ] )
139+
140+ receive do
141+ { :chorex_return , SrpServer , resp } -> IO . puts ( "Server responds #{ inspect resp } " )
142+ end
143+
144+ receive do
145+ { :chorex_return , SrpClient , resp } -> IO . puts ( "Client responds #{ inspect resp } " )
146+ end
147+
148+ Logger . info ( "SRP demo complete" )
9149 end
10150end
0 commit comments