diff options
| -rw-r--r-- | spec/calculate_spec.js (renamed from spec/constants_spec.js) | 12 | ||||
| -rw-r--r-- | spec/runner.html | 4 | ||||
| -rw-r--r-- | spec/signup_spec.js | 6 | ||||
| -rw-r--r-- | src/srp_calculate.js (renamed from src/srp_constants.js) | 37 | ||||
| -rw-r--r-- | src/srp_session.js | 32 | 
5 files changed, 49 insertions, 42 deletions
| diff --git a/spec/constants_spec.js b/spec/calculate_spec.js index a8aa7fd..f60c343 100644 --- a/spec/constants_spec.js +++ b/spec/calculate_spec.js @@ -1,7 +1,7 @@ -describe("Constants", function() { +describe("Calculate", function() {    beforeEach(function() { -    constants = new srp.Constants(); +    calculate = new srp.Calculate();    });    // login attempt with correct password that failed never the less: @@ -19,17 +19,17 @@ describe("Constants", function() {    };    it("calculates the proper A", function() { -    expect(constants.calcA(compare.a)).toBe(compare.aa); +    expect(calculate.A(compare.a)).toBe(compare.aa);    });    it("calculates the right x", function() { -    x = constants.calcX("testuser","password","7686acb8") +    x = calculate.X("testuser","password","7686acb8")      expect(x).toBe('84d6bb567ddf584b1d8c8728289644d45dbfbb02deedd05c0f64db96740f0398');    });    it("calculates the right verifier", function() { -    x = constants.calcX(compare.username, compare.password, compare.salt) -    expect(constants.calcV(x)).toBe(compare.v); +    x = calculate.X(compare.username, compare.password, compare.salt) +    expect(calculate.V(x)).toBe(compare.v);    });  }); diff --git a/spec/runner.html b/spec/runner.html index e94cd24..3a458df 100644 --- a/spec/runner.html +++ b/spec/runner.html @@ -23,14 +23,14 @@    <script type="text/javascript" src="../lib/jsbn2.js"></script>    <script type="text/javascript" src="../src/srp.js"></script>    <script type="text/javascript" src="../src/jqueryRest.js"></script> -  <script type="text/javascript" src="../src/srp_constants.js"></script> +  <script type="text/javascript" src="../src/srp_calculate.js"></script>    <script type="text/javascript" src="../src/srp_session.js"></script>    <!-- include spec files here... -->    <script type="text/javascript" src="helper.js"></script>    <script type="text/javascript" src="signup_spec.js"></script>    <script type="text/javascript" src="login_spec.js"></script> -  <script type="text/javascript" src="constants_spec.js"></script> +  <script type="text/javascript" src="calculate_spec.js"></script>    <script type="text/javascript" src="session_spec.js"></script>    <script type="text/javascript"> diff --git a/spec/signup_spec.js b/spec/signup_spec.js index 70c6823..4f7a65d 100644 --- a/spec/signup_spec.js +++ b/spec/signup_spec.js @@ -9,9 +9,9 @@ describe("Signup with srp var", function() {    beforeEach(function() {      specHelper.setupFakeXHR.apply(this); -    constants = new srp.Constants(); -    constants.randomSalt = function() {return "4c78c3f8"}; -    srp.session = new srp.Session(undefined, undefined, constants); +    calculate = new srp.Calculate(); +    calculate.randomSalt = function() {return "4c78c3f8"}; +    srp.session = new srp.Session(undefined, undefined, calculate);    });    afterEach(function() { diff --git a/src/srp_constants.js b/src/srp_calculate.js index 3d7ec85..8928114 100644 --- a/src/srp_constants.js +++ b/src/srp_calculate.js @@ -1,4 +1,4 @@ -srp.Constants = function() { +srp.Calculate = function() {    // Variables used in the SRP protocol    var Nstr = "eeaf0ab9adb38dd69c33f80afa8fc5e86072618775ff3c0b9ea2314c9c256576d674df7496ea81d3383b4813d692c6e0e0d5d8e250b98be48e495c1d6089dad15dc7d7b46154d6b6ce8ef4ad69b15d4982559b297bcf1885c529f566660e57ec68edbc3c05726cc02fd4cbf4976eaa9afd5138fe8376435b9fc61d2fc0eb06e3"; @@ -7,30 +7,30 @@ srp.Constants = function() {    var k = new BigInteger("bf66c44a428916cad64aa7c679f3fd897ad4c375e9bbb4cbf2f5de241d618ef0", 16);    var rng = new SecureRandom(); -  this.calcA = function(_a) {  +  this.A = function(_a) {       a = new BigInteger(_a, 16);      return g.modPow(a, N).toString(16);    };    // Calculates the X value    // x = H(s, H(I:p)) -  this.calcX = function(login, password, salt) { -    var salted = salt + SHA256(login + ":" + password) -    return SHA256(hex2a(salted)); +  this.X = function(login, password, salt) { +    var salted = salt + this.hash(login + ":" + password) +    return this.hashHex(salted);    }; -  this.calcV = this.calcA; +  this.V = this.A;    // u = H(A,B) -  this.calcU = function(A, B) { -    return SHA256(hex2a(A + B));  +  this.U = function(A, B) { +    return this.hashHex(A + B);     };    //S = (B - kg^x) ^ (a + ux) -  this.calcS = function(_a, _A, _B, _x) { +  this.S = function(_a, _A, _B, _x) {      var a = new BigInteger(_a, 16);      var x = new BigInteger(_x, 16); -    var u = new BigInteger(this.calcU(_A, _B), 16); +    var u = new BigInteger(this.U(_A, _B), 16);      var B = new BigInteger(_B, 16);      var kgx = k.multiply(g.modPow(x, N));   @@ -39,20 +39,25 @@ srp.Constants = function() {      return B.subtract(kgx).modPow(aux, N).toString(16);     } -  this.calcK = function(_S) { -    return SHA256(hex2a(_S)); +  this.K = function(_S) { +    return this.hashHex(_S);    }    this.nXorG = function() { -    var hashN = SHA256(hex2a(Nstr)); -    var hashG = SHA256(hex2a(g.toString(16))); +    var hashN = this.hashHex(Nstr); +    var hashG = this.hashHex(g.toString(16));      return hexXor(hashN, hashG);    }; -  this.hash = function(hexString) { -    return removeLeading0(SHA256(hex2a(hexString))); +  this.hashHex = function(hexString) { +    return this.hash(hex2a(hexString));    }; +  this.hash = function(string) { +    return removeLeading0(SHA256(string)); +  }; + +    this.isInvalidEphemeral = function(a) {      return (g.modPow(a, N) == 0);    }; diff --git a/src/srp_session.js b/src/srp_session.js index babb96a..ccade72 100644 --- a/src/srp_session.js +++ b/src/srp_session.js @@ -1,9 +1,10 @@ -srp.Session = function(login, password, constants) { -   +srp.Session = function(login, password, calculate) { -  var constants = constants || new srp.Constants(); -  var a = constants.randomEphemeral(); -  var A = constants.calcA(a); +  // default for injected dependency +  calculate = calculate || new srp.Calculate(); + +  var a = calculate.randomEphemeral(); +  var A = calculate.A(a);    var S = null;    var K = null;    var M = null; @@ -18,17 +19,17 @@ srp.Session = function(login, password, constants) {    this.calculateAndSetA = function(_a) {      a = _a; -    A = constants.calcA(_a); +    A = calculate.A(_a);      return A;    };    this.signup = function() { -    var salt = constants.randomSalt(); -    var x = constants.calcX(this.getI(), this.getPass(), salt); +    var salt = calculate.randomSalt(); +    var x = calculate.X(this.getI(), this.getPass(), salt);      return {        login: this.getI(),        password_salt: salt, -      password_verifier: constants.calcV(x) +      password_verifier: calculate.V(x)      };    }; @@ -61,15 +62,16 @@ srp.Session = function(login, password, constants) {    {          //S -> C: s | B      var B = ephemeral; -    var x = constants.calcX(this.getI(), this.getPass(), salt); -    S = constants.calcS(a, A, B, x); -    K = constants.calcK(S); +    var x = calculate.X(this.getI(), this.getPass(), salt); +    S = calculate.S(a, A, B, x); +    K = calculate.K(S);      // M = H(H(N) xor H(g), H(I), s, A, B, K) -    var xor = constants.nXorG(); -    M = constants.hash(xor + SHA256(I) + salt + A + B + K); +    var xor = calculate.nXorG(); +    var hash_i = calculate.hash(I) +    M = calculate.hashHex(xor + hash_i + salt + A + B + K);      //M2 = H(A, M, K) -    M2 = constants.hash(A + M + K); +    M2 = calculate.hashHex(A + M + K);    }; | 
