srp.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /** @fileOverview Javascript SRP implementation.
  2. *
  3. * This file contains a partial implementation of the SRP (Secure Remote
  4. * Password) password-authenticated key exchange protocol. Given a user
  5. * identity, salt, and SRP group, it generates the SRP verifier that may
  6. * be sent to a remote server to establish and SRP account.
  7. *
  8. * For more information, see http://srp.stanford.edu/.
  9. *
  10. * @author Quinn Slack
  11. */
  12. /**
  13. * Compute the SRP verifier from the username, password, salt, and group.
  14. * @class SRP
  15. */
  16. sjcl.keyexchange.srp = {
  17. /**
  18. * Calculates SRP v, the verifier.
  19. * v = g^x mod N [RFC 5054]
  20. * @param {String} I The username.
  21. * @param {String} P The password.
  22. * @param {Object} s A bitArray of the salt.
  23. * @param {Object} group The SRP group. Use sjcl.keyexchange.srp.knownGroup
  24. to obtain this object.
  25. * @return {Object} A bitArray of SRP v.
  26. */
  27. makeVerifier: function(I, P, s, group) {
  28. var x;
  29. // From RFC 5054:
  30. // v = g^x mod N
  31. x = this.makeX(I, P, s);
  32. x = sjcl.bn.fromBits(x);
  33. return group.g.powermod(x, group.N);
  34. },
  35. /**
  36. * Calculates SRP x.
  37. * x = SHA1(<salt> | SHA(<username> | ":" | <raw password>)) [RFC 2945]
  38. * @param {String} I The username.
  39. * @param {String} P The password.
  40. * @param {Object} s A bitArray of the salt.
  41. * @return {Object} A bitArray of SRP x.
  42. */
  43. makeX: function(I, P, s) {
  44. var inner = sjcl.hash.sha1.hash(I + ':' + P);
  45. return sjcl.hash.sha1.hash(sjcl.bitArray.concat(s, inner));
  46. },
  47. /**
  48. * Returns the known SRP group with the given size (in bits).
  49. * @param {String} i The size of the known SRP group.
  50. * @return {Object} An object with "N" and "g" properties.
  51. */
  52. knownGroup:function(i) {
  53. if (typeof i !== "string") { i = i.toString(); }
  54. if (!this._didInitKnownGroups) { this._initKnownGroups(); }
  55. return this._knownGroups[i];
  56. },
  57. /**
  58. * Initializes bignum objects for known group parameters.
  59. * @private
  60. */
  61. _didInitKnownGroups: false,
  62. _initKnownGroups:function() {
  63. var i, size, group;
  64. for (i=0; i < this._knownGroupSizes.length; i++) {
  65. size = this._knownGroupSizes[i].toString();
  66. group = this._knownGroups[size];
  67. group.N = new sjcl.bn(group.N);
  68. group.g = new sjcl.bn(group.g);
  69. }
  70. this._didInitKnownGroups = true;
  71. },
  72. _knownGroupSizes: [1024, 1536, 2048],
  73. _knownGroups: {
  74. 1024: {
  75. N: "EEAF0AB9ADB38DD69C33F80AFA8FC5E86072618775FF3C0B9EA2314C" +
  76. "9C256576D674DF7496EA81D3383B4813D692C6E0E0D5D8E250B98BE4" +
  77. "8E495C1D6089DAD15DC7D7B46154D6B6CE8EF4AD69B15D4982559B29" +
  78. "7BCF1885C529F566660E57EC68EDBC3C05726CC02FD4CBF4976EAA9A" +
  79. "FD5138FE8376435B9FC61D2FC0EB06E3",
  80. g:2
  81. },
  82. 1536: {
  83. N: "9DEF3CAFB939277AB1F12A8617A47BBBDBA51DF499AC4C80BEEEA961" +
  84. "4B19CC4D5F4F5F556E27CBDE51C6A94BE4607A291558903BA0D0F843" +
  85. "80B655BB9A22E8DCDF028A7CEC67F0D08134B1C8B97989149B609E0B" +
  86. "E3BAB63D47548381DBC5B1FC764E3F4B53DD9DA1158BFD3E2B9C8CF5" +
  87. "6EDF019539349627DB2FD53D24B7C48665772E437D6C7F8CE442734A" +
  88. "F7CCB7AE837C264AE3A9BEB87F8A2FE9B8B5292E5A021FFF5E91479E" +
  89. "8CE7A28C2442C6F315180F93499A234DCF76E3FED135F9BB",
  90. g: 2
  91. },
  92. 2048: {
  93. N: "AC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC319294" +
  94. "3DB56050A37329CBB4A099ED8193E0757767A13DD52312AB4B03310D" +
  95. "CD7F48A9DA04FD50E8083969EDB767B0CF6095179A163AB3661A05FB" +
  96. "D5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF74" +
  97. "7359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A" +
  98. "436C6481F1D2B9078717461A5B9D32E688F87748544523B524B0D57D" +
  99. "5EA77A2775D2ECFA032CFBDBF52FB3786160279004E57AE6AF874E73" +
  100. "03CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DBFBB6" +
  101. "94B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F" +
  102. "9E4AFF73",
  103. g: 2
  104. }
  105. }
  106. };