srp.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 to obtain this object.
  24. * @return {Object} A bitArray of SRP v.
  25. */
  26. makeVerifier: function(I, P, s, group) {
  27. var x;
  28. // From RFC 5054:
  29. // v = g^x mod N
  30. x = this.makeX(I, P, s);
  31. x = sjcl.bn.fromBits(x);
  32. return group.g.powermod(x, group.N);
  33. },
  34. /**
  35. * Calculates SRP x.
  36. * x = SHA1(<salt> | SHA(<username> | ":" | <raw password>)) [RFC 2945]
  37. * @param {String} I The username.
  38. * @param {String} P The password.
  39. * @param {Object} s A bitArray of the salt.
  40. * @return {Object} A bitArray of SRP x.
  41. */
  42. makeX: function(I, P, s) {
  43. var inner = sjcl.hash.sha1.hash(I + ':' + P);
  44. return sjcl.hash.sha1.hash(sjcl.bitArray.concat(s, inner));
  45. },
  46. /**
  47. * Returns the known SRP group with the given size (in bits).
  48. * @param {String} i The size of the known SRP group.
  49. * @return {Object} An object with "N" and "g" properties.
  50. */
  51. knownGroup:function(i) {
  52. if (typeof i !== "string") { i = i.toString(); }
  53. if (!this._didInitKnownGroups) { this._initKnownGroups(); }
  54. return this._knownGroups[i];
  55. },
  56. /**
  57. * Initializes bignum objects for known group parameters.
  58. * @private
  59. */
  60. _didInitKnownGroups: false,
  61. _initKnownGroups:function() {
  62. var i, size, group;
  63. for (i=0; i < this._knownGroupSizes.length; i++) {
  64. size = this._knownGroupSizes[i].toString();
  65. group = this._knownGroups[size];
  66. group.N = new sjcl.bn(group.N);
  67. group.g = new sjcl.bn(group.g);
  68. }
  69. this._didInitKnownGroups = true;
  70. },
  71. _knownGroupSizes: [1024, 1536, 2048],
  72. _knownGroups: {
  73. 1024: {
  74. N: "EEAF0AB9ADB38DD69C33F80AFA8FC5E86072618775FF3C0B9EA2314C" +
  75. "9C256576D674DF7496EA81D3383B4813D692C6E0E0D5D8E250B98BE4" +
  76. "8E495C1D6089DAD15DC7D7B46154D6B6CE8EF4AD69B15D4982559B29" +
  77. "7BCF1885C529F566660E57EC68EDBC3C05726CC02FD4CBF4976EAA9A" +
  78. "FD5138FE8376435B9FC61D2FC0EB06E3",
  79. g:2
  80. },
  81. 1536: {
  82. N: "9DEF3CAFB939277AB1F12A8617A47BBBDBA51DF499AC4C80BEEEA961" +
  83. "4B19CC4D5F4F5F556E27CBDE51C6A94BE4607A291558903BA0D0F843" +
  84. "80B655BB9A22E8DCDF028A7CEC67F0D08134B1C8B97989149B609E0B" +
  85. "E3BAB63D47548381DBC5B1FC764E3F4B53DD9DA1158BFD3E2B9C8CF5" +
  86. "6EDF019539349627DB2FD53D24B7C48665772E437D6C7F8CE442734A" +
  87. "F7CCB7AE837C264AE3A9BEB87F8A2FE9B8B5292E5A021FFF5E91479E" +
  88. "8CE7A28C2442C6F315180F93499A234DCF76E3FED135F9BB",
  89. g: 2
  90. },
  91. 2048: {
  92. N: "AC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC319294" +
  93. "3DB56050A37329CBB4A099ED8193E0757767A13DD52312AB4B03310D" +
  94. "CD7F48A9DA04FD50E8083969EDB767B0CF6095179A163AB3661A05FB" +
  95. "D5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF74" +
  96. "7359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A" +
  97. "436C6481F1D2B9078717461A5B9D32E688F87748544523B524B0D57D" +
  98. "5EA77A2775D2ECFA032CFBDBF52FB3786160279004E57AE6AF874E73" +
  99. "03CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DBFBB6" +
  100. "94B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F" +
  101. "9E4AFF73",
  102. g: 2
  103. }
  104. }
  105. };