curve255.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. "use strict";
  2. /**
  3. * @fileOverview
  4. * Core operations on curve 25519 required for the higher level modules.
  5. */
  6. /*
  7. * Copyright (c) 2007, 2013, 2014 Michele Bini
  8. * Copyright (c) 2014 Mega Limited
  9. * under the MIT License.
  10. *
  11. * Authors: Guy K. Kloss, Michele Bini
  12. *
  13. * You should have received a copy of the license along with this program.
  14. */
  15. var core = require('./core');
  16. var utils = require('./utils');
  17. /**
  18. * @exports jodid25519/curve255
  19. * Legacy compatibility module for Michele Bini's previous curve255.js.
  20. *
  21. * @description
  22. * Legacy compatibility module for Michele Bini's previous curve255.js.
  23. *
  24. * <p>
  25. * This code presents an API with all key formats as previously available
  26. * from Michele Bini's curve255.js implementation.
  27. * </p>
  28. */
  29. var ns = {};
  30. function curve25519_raw(f, c) {
  31. var a, x_1, q;
  32. x_1 = c;
  33. a = core.dbl(x_1, core.ONE());
  34. q = [x_1, core.ONE()];
  35. var n = 255;
  36. while (core.getbit(f, n) == 0) {
  37. n--;
  38. // For correct constant-time operation, bit 255 should always be
  39. // set to 1 so the following 'while' loop is never entered.
  40. if (n < 0) {
  41. return core.ZERO();
  42. }
  43. }
  44. n--;
  45. var aq = [a, q];
  46. while (n >= 0) {
  47. var r, s;
  48. var b = core.getbit(f, n);
  49. r = core.sum(aq[0][0], aq[0][1], aq[1][0], aq[1][1], x_1);
  50. s = core.dbl(aq[1 - b][0], aq[1 - b][1]);
  51. aq[1 - b] = s;
  52. aq[b] = r;
  53. n--;
  54. }
  55. q = aq[1];
  56. q[1] = core.invmodp(q[1]);
  57. q[0] = core.mulmodp(q[0], q[1]);
  58. core.reduce(q[0]);
  59. return q[0];
  60. }
  61. function curve25519b32(a, b) {
  62. return _base32encode(curve25519(_base32decode(a),
  63. _base32decode(b)));
  64. }
  65. function curve25519(f, c) {
  66. if (!c) {
  67. c = core.BASE();
  68. }
  69. f[0] &= 0xFFF8;
  70. f[15] = (f[15] & 0x7FFF) | 0x4000;
  71. return curve25519_raw(f, c);
  72. }
  73. function _hexEncodeVector(k) {
  74. var hexKey = utils.hexEncode(k);
  75. // Pad with '0' at the front.
  76. hexKey = new Array(64 + 1 - hexKey.length).join('0') + hexKey;
  77. // Invert bytes.
  78. return hexKey.split(/(..)/).reverse().join('');
  79. }
  80. function _hexDecodeVector(v) {
  81. // assert(length(x) == 64);
  82. // Invert bytes.
  83. var hexKey = v.split(/(..)/).reverse().join('');
  84. return utils.hexDecode(hexKey);
  85. }
  86. // Expose some functions to the outside through this name space.
  87. /**
  88. * Computes the scalar product of a point on the curve 25519.
  89. *
  90. * This function is used for the DH key-exchange protocol.
  91. *
  92. * Before multiplication, some bit operations are applied to the
  93. * private key to ensure it is a valid Curve25519 secret key.
  94. * It is the user's responsibility to make sure that the private
  95. * key is a uniformly random, secret value.
  96. *
  97. * @function
  98. * @param f {array}
  99. * Private key.
  100. * @param c {array}
  101. * Public point on the curve. If not given, the curve's base point is used.
  102. * @returns {array}
  103. * Key point resulting from scalar product.
  104. */
  105. ns.curve25519 = curve25519;
  106. /**
  107. * Computes the scalar product of a point on the curve 25519.
  108. *
  109. * This variant does not make sure that the private key is valid.
  110. * The user has the responsibility to ensure the private key is
  111. * valid or that this results in a safe protocol. Unless you know
  112. * exactly what you are doing, you should not use this variant,
  113. * please use 'curve25519' instead.
  114. *
  115. * @function
  116. * @param f {array}
  117. * Private key.
  118. * @param c {array}
  119. * Public point on the curve. If not given, the curve's base point is used.
  120. * @returns {array}
  121. * Key point resulting from scalar product.
  122. */
  123. ns.curve25519_raw = curve25519_raw;
  124. /**
  125. * Encodes the internal representation of a key to a canonical hex
  126. * representation.
  127. *
  128. * This is the format commonly used in other libraries and for
  129. * test vectors, and is equivalent to the hex dump of the key in
  130. * little-endian binary format.
  131. *
  132. * @function
  133. * @param n {array}
  134. * Array representation of key.
  135. * @returns {string}
  136. * Hexadecimal string representation of key.
  137. */
  138. ns.hexEncodeVector = _hexEncodeVector;
  139. /**
  140. * Decodes a canonical hex representation of a key
  141. * to an internally compatible array representation.
  142. *
  143. * @function
  144. * @param n {string}
  145. * Hexadecimal string representation of key.
  146. * @returns {array}
  147. * Array representation of key.
  148. */
  149. ns.hexDecodeVector = _hexDecodeVector;
  150. /**
  151. * Encodes the internal representation of a key into a
  152. * hexadecimal representation.
  153. *
  154. * This is a strict positional notation, most significant digit first.
  155. *
  156. * @function
  157. * @param n {array}
  158. * Array representation of key.
  159. * @returns {string}
  160. * Hexadecimal string representation of key.
  161. */
  162. ns.hexencode = utils.hexEncode;
  163. /**
  164. * Decodes a hex representation of a key to an internally
  165. * compatible array representation.
  166. *
  167. * @function
  168. * @param n {string}
  169. * Hexadecimal string representation of key.
  170. * @returns {array}
  171. * Array representation of key.
  172. */
  173. ns.hexdecode = utils.hexDecode;
  174. /**
  175. * Encodes the internal representation of a key to a base32
  176. * representation.
  177. *
  178. * @function
  179. * @param n {array}
  180. * Array representation of key.
  181. * @returns {string}
  182. * Base32 string representation of key.
  183. */
  184. ns.base32encode = utils.base32encode;
  185. /**
  186. * Decodes a base32 representation of a key to an internally
  187. * compatible array representation.
  188. *
  189. * @function
  190. * @param n {string}
  191. * Base32 string representation of key.
  192. * @returns {array}
  193. * Array representation of key.
  194. */
  195. ns.base32decode = utils.base32decode;
  196. module.exports = ns;