dh.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. "use strict";
  2. /**
  3. * @fileOverview
  4. * EC Diffie-Hellman operations on Curve25519.
  5. */
  6. /*
  7. * Copyright (c) 2014 Mega Limited
  8. * under the MIT License.
  9. *
  10. * Authors: Guy K. Kloss
  11. *
  12. * You should have received a copy of the license along with this program.
  13. */
  14. var core = require('./core');
  15. var utils = require('./utils');
  16. var curve255 = require('./curve255');
  17. /**
  18. * @exports jodid25519/dh
  19. * EC Diffie-Hellman operations on Curve25519.
  20. *
  21. * @description
  22. * EC Diffie-Hellman operations on Curve25519.
  23. */
  24. var ns = {};
  25. function _toString(vector) {
  26. var u = new Uint16Array(vector);
  27. return (new Buffer(new Uint8Array(u.buffer)));
  28. }
  29. function _fromString(vector) {
  30. if (Buffer.isBuffer(vector)) {
  31. var u = new Uint8Array(vector);
  32. return (new Uint16Array(u.buffer));
  33. }
  34. var result = new Array(16);
  35. for (var i = 0, l = 0; i < vector.length; i += 2) {
  36. result[l] = (vector.charCodeAt(i + 1) << 8) | vector.charCodeAt(i);
  37. l++;
  38. }
  39. return result;
  40. }
  41. /**
  42. * Computes a key through scalar multiplication of a point on the curve 25519.
  43. *
  44. * This function is used for the DH key-exchange protocol. It computes a
  45. * key based on a secret key with a public component (opponent's public key
  46. * or curve base point if not given) by using scalar multiplication.
  47. *
  48. * Before multiplication, some bit operations are applied to the
  49. * private key to ensure it is a valid Curve25519 secret key.
  50. * It is the user's responsibility to make sure that the private
  51. * key is a uniformly random, secret value.
  52. *
  53. * @function
  54. * @param privateComponent {string}
  55. * Private point as byte string on the curve.
  56. * @param publicComponent {string}
  57. * Public point as byte string on the curve. If not given, the curve's
  58. * base point is used.
  59. * @returns {string}
  60. * Key point as byte string resulting from scalar product.
  61. */
  62. ns.computeKey = function(privateComponent, publicComponent) {
  63. if (publicComponent) {
  64. return _toString(curve255.curve25519(_fromString(privateComponent),
  65. _fromString(publicComponent)));
  66. } else {
  67. return _toString(curve255.curve25519(_fromString(privateComponent)));
  68. }
  69. };
  70. /**
  71. * Computes the public key to a private key on the curve 25519.
  72. *
  73. * Before multiplication, some bit operations are applied to the
  74. * private key to ensure it is a valid Curve25519 secret key.
  75. * It is the user's responsibility to make sure that the private
  76. * key is a uniformly random, secret value.
  77. *
  78. * @function
  79. * @param privateKey {string}
  80. * Private point as byte string on the curve.
  81. * @returns {string}
  82. * Public key point as byte string resulting from scalar product.
  83. */
  84. ns.publicKey = function(privateKey) {
  85. return _toString(curve255.curve25519(_fromString(privateKey)));
  86. };
  87. /**
  88. * Generates a new random private key of 32 bytes length (256 bit).
  89. *
  90. * @function
  91. * @returns {string}
  92. * Byte string containing a new random private key seed.
  93. */
  94. ns.generateKey = function() {
  95. return core.generateKey(true);
  96. };
  97. module.exports = ns;