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