Advertisement
thornik

C# sample for SRP-6

Oct 19th, 2022 (edited)
1,179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.75 KB | Cybersecurity | 0 0
  1. // SRP-6 sample of client and server
  2. // refer this lib: https://github.com/secure-remote-password/srp.net
  3.  
  4. static string verifier, salt;
  5. static SrpEphemeral clientEphemeral;
  6. static SrpEphemeral serverEphemeral;
  7.  
  8. static void DoIt(string[] args)
  9. {
  10.     var user = "vasyan";
  11.     var psw = "123456";
  12.     // register user in a system
  13.     (salt, verifier) = SignUp(user, psw);// (user, salt, verifier) are transferred (open way) to the server and saved in DB
  14.     // log in from client
  15.     clientEphemeral = CLIENT_Login();
  16.     // server receives from user 'user_name' and 'clientEphemeral.Public'
  17.     serverEphemeral = SERVER_AcceptLogin(user, clientEphemeral.Public);
  18.     // client receives from server salt and serverEphemeral.Public
  19.     var clientSession = CLIENT_MakeSession(serverEphemeral.Public);
  20.     // server receives clientSession.Proof and computes session key
  21.     var serverSession = SERVER_AcceptSession(clientSession.Proof);
  22.     if (serverSession == null) return;//! it means client provided wrong user/psw
  23.     // server sends serverSession.Proof to the client to verify, client:
  24.     Log.Warning("CLIENT: verify session...");
  25.     if (CLIENT_VerifySession(clientSession, serverSession.Proof))
  26.         Log.Trace("Session verified!");
  27.     else
  28.         Log.Error("Session FAILED!");
  29. }
  30.  
  31. static (string, string) SignUp(string user, string psw)
  32. {
  33.     var cliSRP = new SrpClient();
  34.     var salt = cliSRP.GenerateSalt();
  35.     var privateKey = cliSRP.DerivePrivateKey(salt, user, psw);
  36.     var verifier = cliSRP.DeriveVerifier(privateKey);
  37.     return (salt, verifier);
  38. }
  39.  
  40. static SrpEphemeral CLIENT_Login()
  41. {
  42.     var cliSRP = new SrpClient();
  43.     return cliSRP.GenerateEphemeral();
  44. }
  45.  
  46. static SrpEphemeral SERVER_AcceptLogin(string user, string cliPubKey)
  47. {
  48.     var srvSRP = new SrpServer();
  49.     return srvSRP.GenerateEphemeral(verifier);// server generates keys using user's verifier, obtained by user_name
  50. }
  51.  
  52. static SrpSession CLIENT_MakeSession(string srvPubKey)
  53. {
  54.     var cliSRP = new SrpClient();
  55.     var privateKey = cliSRP.DerivePrivateKey(salt, "vasyan", "123456");
  56.     return cliSRP.DeriveSession(clientEphemeral.Secret, srvPubKey, salt, "vasyan", privateKey);
  57. }
  58.  
  59. static SrpSession SERVER_AcceptSession(string clientSessproof)
  60. {
  61.     var srvSRP = new SrpServer();
  62.     try {
  63.         return srvSRP.DeriveSession(serverEphemeral.Secret, clientEphemeral.Public, salt, "vasyan", verifier, clientSessproof);
  64.     } catch {
  65.         Log.Error("Auth FAILED!");
  66.         return null;
  67.     }
  68. }
  69.  
  70. static bool CLIENT_VerifySession(SrpSession cliSession, string srvSessProof)
  71. {
  72.     var cliSRP = new SrpClient();
  73.     try {
  74.         cliSRP.VerifySession(clientEphemeral.Public, cliSession, srvSessProof);
  75.     } catch { return false; }
  76.     return true;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement