comparison scripts/minifier/otr/dep/salsa20.js @ 12:1596660ddf72

Add minifier script for otr.js and its dependencies
author souliane <souliane@mailoo.org>
date Wed, 03 Sep 2014 19:38:05 +0200
parents
children
comparison
equal deleted inserted replaced
11:4920c8da790b 12:1596660ddf72
1 // Salsa20 implementation
2 // Contributed to Cryptocat by Dmitry Chestnykh
3 // 21-01-2013
4
5 ;(function (root, factory) {
6
7 if (typeof define === 'function' && define.amd) {
8 define(factory)
9 } else if (typeof module !== 'undefined' && module.exports) {
10 module.exports = factory()
11 } else {
12 root.Salsa20 = factory()
13 }
14
15 }(this, function () {
16
17 function Salsa20(key, nonce) {
18 // Constants.
19 this.rounds = 20; // number of Salsa rounds
20 this.sigmaWords = [0x61707865, 0x3320646e, 0x79622d32, 0x6b206574];
21
22 // State.
23 this.keyWords = []; // key words
24 this.nonceWords = [0, 0]; // nonce words
25 this.counterWords = [0, 0]; // block counter words
26
27 // Output buffer.
28 this.block = []; // output block of 64 bytes
29 this.blockUsed = 64; // number of block bytes used
30
31 this.setKey(key);
32 this.setNonce(nonce);
33 }
34
35 // setKey sets the key to the given 32-byte array.
36 Salsa20.prototype.setKey = function(key) {
37 for (var i = 0, j = 0; i < 8; i++, j += 4) {
38 this.keyWords[i] = (key[j] & 0xff) |
39 ((key[j+1] & 0xff)<<8) |
40 ((key[j+2] & 0xff)<<16) |
41 ((key[j+3] & 0xff)<<24);
42 }
43 this._reset();
44 };
45
46 // setNonce sets the nonce to the given 8-byte array.
47 Salsa20.prototype.setNonce = function(nonce) {
48 this.nonceWords[0] = (nonce[0] & 0xff) |
49 ((nonce[1] & 0xff)<<8) |
50 ((nonce[2] & 0xff)<<16) |
51 ((nonce[3] & 0xff)<<24);
52 this.nonceWords[1] = (nonce[4] & 0xff) |
53 ((nonce[5] & 0xff)<<8) |
54 ((nonce[6] & 0xff)<<16) |
55 ((nonce[7] & 0xff)<<24);
56 this._reset();
57 };
58
59 // getBytes returns the next numberOfBytes bytes of stream.
60 Salsa20.prototype.getBytes = function(numberOfBytes) {
61 var out = new Array(numberOfBytes);
62 for (var i = 0; i < numberOfBytes; i++) {
63 if (this.blockUsed == 64) {
64 this._generateBlock();
65 this._incrementCounter();
66 this.blockUsed = 0;
67 }
68 out[i] = this.block[this.blockUsed];
69 this.blockUsed++;
70 }
71 return out;
72 };
73
74 Salsa20.prototype.getHexString = function(numberOfBytes) {
75 var hex=['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'];
76 var out = [];
77 var bytes = this.getBytes(numberOfBytes);
78 for(var i = 0; i < bytes.length; i++) {
79 out.push(hex[(bytes[i] >> 4) & 15]);
80 out.push(hex[bytes[i] & 15]);
81 }
82 return out.join('');
83 };
84
85 // Private methods.
86
87 Salsa20.prototype._reset = function() {
88 this.counterWords[0] = 0;
89 this.counterWords[1] = 0;
90 this.blockUsed = 64;
91 };
92
93 // _incrementCounter increments block counter.
94 Salsa20.prototype._incrementCounter = function() {
95 // Note: maximum 2^64 blocks.
96 this.counterWords[0] = (this.counterWords[0] + 1) & 0xffffffff;
97 if (this.counterWords[0] == 0) {
98 this.counterWords[1] = (this.counterWords[1] + 1) & 0xffffffff;
99 }
100 };
101
102 // _generateBlock generates 64 bytes from key, nonce, and counter,
103 // and puts the result into this.block.
104 Salsa20.prototype._generateBlock = function() {
105 var j0 = this.sigmaWords[0],
106 j1 = this.keyWords[0],
107 j2 = this.keyWords[1],
108 j3 = this.keyWords[2],
109 j4 = this.keyWords[3],
110 j5 = this.sigmaWords[1],
111 j6 = this.nonceWords[0],
112 j7 = this.nonceWords[1],
113 j8 = this.counterWords[0],
114 j9 = this.counterWords[1],
115 j10 = this.sigmaWords[2],
116 j11 = this.keyWords[4],
117 j12 = this.keyWords[5],
118 j13 = this.keyWords[6],
119 j14 = this.keyWords[7],
120 j15 = this.sigmaWords[3];
121
122 var x0 = j0, x1 = j1, x2 = j2, x3 = j3, x4 = j4, x5 = j5, x6 = j6, x7 = j7,
123 x8 = j8, x9 = j9, x10 = j10, x11 = j11, x12 = j12, x13 = j13, x14 = j14, x15 = j15;
124
125 var u;
126
127 for (var i = 0; i < this.rounds; i += 2) {
128 u = x0 + x12;
129 x4 ^= (u<<7) | (u>>>(32-7));
130 u = x4 + x0;
131 x8 ^= (u<<9) | (u>>>(32-9));
132 u = x8 + x4;
133 x12 ^= (u<<13) | (u>>>(32-13));
134 u = x12 + x8;
135 x0 ^= (u<<18) | (u>>>(32-18));
136
137 u = x5 + x1;
138 x9 ^= (u<<7) | (u>>>(32-7));
139 u = x9 + x5;
140 x13 ^= (u<<9) | (u>>>(32-9));
141 u = x13 + x9;
142 x1 ^= (u<<13) | (u>>>(32-13));
143 u = x1 + x13;
144 x5 ^= (u<<18) | (u>>>(32-18));
145
146 u = x10 + x6;
147 x14 ^= (u<<7) | (u>>>(32-7));
148 u = x14 + x10;
149 x2 ^= (u<<9) | (u>>>(32-9));
150 u = x2 + x14;
151 x6 ^= (u<<13) | (u>>>(32-13));
152 u = x6 + x2;
153 x10 ^= (u<<18) | (u>>>(32-18));
154
155 u = x15 + x11;
156 x3 ^= (u<<7) | (u>>>(32-7));
157 u = x3 + x15;
158 x7 ^= (u<<9) | (u>>>(32-9));
159 u = x7 + x3;
160 x11 ^= (u<<13) | (u>>>(32-13));
161 u = x11 + x7;
162 x15 ^= (u<<18) | (u>>>(32-18));
163
164 u = x0 + x3;
165 x1 ^= (u<<7) | (u>>>(32-7));
166 u = x1 + x0;
167 x2 ^= (u<<9) | (u>>>(32-9));
168 u = x2 + x1;
169 x3 ^= (u<<13) | (u>>>(32-13));
170 u = x3 + x2;
171 x0 ^= (u<<18) | (u>>>(32-18));
172
173 u = x5 + x4;
174 x6 ^= (u<<7) | (u>>>(32-7));
175 u = x6 + x5;
176 x7 ^= (u<<9) | (u>>>(32-9));
177 u = x7 + x6;
178 x4 ^= (u<<13) | (u>>>(32-13));
179 u = x4 + x7;
180 x5 ^= (u<<18) | (u>>>(32-18));
181
182 u = x10 + x9;
183 x11 ^= (u<<7) | (u>>>(32-7));
184 u = x11 + x10;
185 x8 ^= (u<<9) | (u>>>(32-9));
186 u = x8 + x11;
187 x9 ^= (u<<13) | (u>>>(32-13));
188 u = x9 + x8;
189 x10 ^= (u<<18) | (u>>>(32-18));
190
191 u = x15 + x14;
192 x12 ^= (u<<7) | (u>>>(32-7));
193 u = x12 + x15;
194 x13 ^= (u<<9) | (u>>>(32-9));
195 u = x13 + x12;
196 x14 ^= (u<<13) | (u>>>(32-13));
197 u = x14 + x13;
198 x15 ^= (u<<18) | (u>>>(32-18));
199 }
200
201 x0 += j0;
202 x1 += j1;
203 x2 += j2;
204 x3 += j3;
205 x4 += j4;
206 x5 += j5;
207 x6 += j6;
208 x7 += j7;
209 x8 += j8;
210 x9 += j9;
211 x10 += j10;
212 x11 += j11;
213 x12 += j12;
214 x13 += j13;
215 x14 += j14;
216 x15 += j15;
217
218 this.block[ 0] = ( x0 >>> 0) & 0xff; this.block[ 1] = ( x0 >>> 8) & 0xff;
219 this.block[ 2] = ( x0 >>> 16) & 0xff; this.block[ 3] = ( x0 >>> 24) & 0xff;
220 this.block[ 4] = ( x1 >>> 0) & 0xff; this.block[ 5] = ( x1 >>> 8) & 0xff;
221 this.block[ 6] = ( x1 >>> 16) & 0xff; this.block[ 7] = ( x1 >>> 24) & 0xff;
222 this.block[ 8] = ( x2 >>> 0) & 0xff; this.block[ 9] = ( x2 >>> 8) & 0xff;
223 this.block[10] = ( x2 >>> 16) & 0xff; this.block[11] = ( x2 >>> 24) & 0xff;
224 this.block[12] = ( x3 >>> 0) & 0xff; this.block[13] = ( x3 >>> 8) & 0xff;
225 this.block[14] = ( x3 >>> 16) & 0xff; this.block[15] = ( x3 >>> 24) & 0xff;
226 this.block[16] = ( x4 >>> 0) & 0xff; this.block[17] = ( x4 >>> 8) & 0xff;
227 this.block[18] = ( x4 >>> 16) & 0xff; this.block[19] = ( x4 >>> 24) & 0xff;
228 this.block[20] = ( x5 >>> 0) & 0xff; this.block[21] = ( x5 >>> 8) & 0xff;
229 this.block[22] = ( x5 >>> 16) & 0xff; this.block[23] = ( x5 >>> 24) & 0xff;
230 this.block[24] = ( x6 >>> 0) & 0xff; this.block[25] = ( x6 >>> 8) & 0xff;
231 this.block[26] = ( x6 >>> 16) & 0xff; this.block[27] = ( x6 >>> 24) & 0xff;
232 this.block[28] = ( x7 >>> 0) & 0xff; this.block[29] = ( x7 >>> 8) & 0xff;
233 this.block[30] = ( x7 >>> 16) & 0xff; this.block[31] = ( x7 >>> 24) & 0xff;
234 this.block[32] = ( x8 >>> 0) & 0xff; this.block[33] = ( x8 >>> 8) & 0xff;
235 this.block[34] = ( x8 >>> 16) & 0xff; this.block[35] = ( x8 >>> 24) & 0xff;
236 this.block[36] = ( x9 >>> 0) & 0xff; this.block[37] = ( x9 >>> 8) & 0xff;
237 this.block[38] = ( x9 >>> 16) & 0xff; this.block[39] = ( x9 >>> 24) & 0xff;
238 this.block[40] = (x10 >>> 0) & 0xff; this.block[41] = (x10 >>> 8) & 0xff;
239 this.block[42] = (x10 >>> 16) & 0xff; this.block[43] = (x10 >>> 24) & 0xff;
240 this.block[44] = (x11 >>> 0) & 0xff; this.block[45] = (x11 >>> 8) & 0xff;
241 this.block[46] = (x11 >>> 16) & 0xff; this.block[47] = (x11 >>> 24) & 0xff;
242 this.block[48] = (x12 >>> 0) & 0xff; this.block[49] = (x12 >>> 8) & 0xff;
243 this.block[50] = (x12 >>> 16) & 0xff; this.block[51] = (x12 >>> 24) & 0xff;
244 this.block[52] = (x13 >>> 0) & 0xff; this.block[53] = (x13 >>> 8) & 0xff;
245 this.block[54] = (x13 >>> 16) & 0xff; this.block[55] = (x13 >>> 24) & 0xff;
246 this.block[56] = (x14 >>> 0) & 0xff; this.block[57] = (x14 >>> 8) & 0xff;
247 this.block[58] = (x14 >>> 16) & 0xff; this.block[59] = (x14 >>> 24) & 0xff;
248 this.block[60] = (x15 >>> 0) & 0xff; this.block[61] = (x15 >>> 8) & 0xff;
249 this.block[62] = (x15 >>> 16) & 0xff; this.block[63] = (x15 >>> 24) & 0xff;
250 };
251
252 return Salsa20
253
254 }))