summaryrefslogtreecommitdiff
path: root/frontend/gamma/js/Clipperz/Crypto/BigInt.js
Unidiff
Diffstat (limited to 'frontend/gamma/js/Clipperz/Crypto/BigInt.js') (more/less context) (ignore whitespace changes)
-rw-r--r--frontend/gamma/js/Clipperz/Crypto/BigInt.js23
1 files changed, 10 insertions, 13 deletions
diff --git a/frontend/gamma/js/Clipperz/Crypto/BigInt.js b/frontend/gamma/js/Clipperz/Crypto/BigInt.js
index 41483a3..031ed30 100644
--- a/frontend/gamma/js/Clipperz/Crypto/BigInt.js
+++ b/frontend/gamma/js/Clipperz/Crypto/BigInt.js
@@ -1,118 +1,116 @@
1/* 1/*
2 2
3Copyright 2008-2011 Clipperz Srl 3Copyright 2008-2013 Clipperz Srl
4 4
5This file is part of Clipperz Community Edition. 5This file is part of Clipperz, the online password manager.
6Clipperz Community Edition is an online password manager.
7For further information about its features and functionalities please 6For further information about its features and functionalities please
8refer to http://www.clipperz.com. 7refer to http://www.clipperz.com.
9 8
10* Clipperz Community Edition is free software: you can redistribute 9* Clipperz is free software: you can redistribute it and/or modify it
11 it and/or modify it under the terms of the GNU Affero General Public 10 under the terms of the GNU Affero General Public License as published
12 License as published by the Free Software Foundation, either version 11 by the Free Software Foundation, either version 3 of the License, or
13 3 of the License, or (at your option) any later version. 12 (at your option) any later version.
14 13
15* Clipperz Community Edition is distributed in the hope that it will 14* Clipperz is distributed in the hope that it will be useful, but
16 be useful, but WITHOUT ANY WARRANTY; without even the implied 15 WITHOUT ANY WARRANTY; without even the implied warranty of
17 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU Affero General Public License for more details. 17 See the GNU Affero General Public License for more details.
19 18
20* You should have received a copy of the GNU Affero General Public 19* You should have received a copy of the GNU Affero General Public
21 License along with Clipperz Community Edition. If not, see 20 License along with Clipperz. If not, see http://www.gnu.org/licenses/.
22 <http://www.gnu.org/licenses/>.
23 21
24*/ 22*/
25 23
26if (typeof(Clipperz) == 'undefined') { Clipperz = {}; } 24if (typeof(Clipperz) == 'undefined') { Clipperz = {}; }
27if (typeof(Clipperz.Crypto) == 'undefined') { Clipperz.Crypto = {}; } 25if (typeof(Clipperz.Crypto) == 'undefined') { Clipperz.Crypto = {}; }
28 26
29//############################################################################# 27//#############################################################################
30 //Downloaded on March 05, 2007 from http://www.leemon.com/crypto/BigInt.js 28 //Downloaded on March 05, 2007 from http://www.leemon.com/crypto/BigInt.js
31//############################################################################# 29//#############################################################################
32 30
33 31
34//////////////////////////////////////////////////////////////////////////////////////// 32////////////////////////////////////////////////////////////////////////////////////////
35// Big Integer Library v. 5.0 33// Big Integer Library v. 5.0
36// Created 2000, last modified 2006 34// Created 2000, last modified 2006
37// Leemon Baird 35// Leemon Baird
38// www.leemon.com 36// www.leemon.com
39// 37//
40// This file is public domain. You can use it for any purpose without restriction. 38// This file is public domain. You can use it for any purpose without restriction.
41// I do not guarantee that it is correct, so use it at your own risk. If you use 39// I do not guarantee that it is correct, so use it at your own risk. If you use
42// it for something interesting, I'd appreciate hearing about it. If you find 40// it for something interesting, I'd appreciate hearing about it. If you find
43// any bugs or make any improvements, I'd appreciate hearing about those too. 41// any bugs or make any improvements, I'd appreciate hearing about those too.
44// It would also be nice if my name and address were left in the comments. 42// It would also be nice if my name and address were left in the comments.
45// But none of that is required. 43// But none of that is required.
46// 44//
47// This code defines a bigInt library for arbitrary-precision integers. 45// This code defines a bigInt library for arbitrary-precision integers.
48// A bigInt is an array of integers storing the value in chunks of bpe bits, 46// A bigInt is an array of integers storing the value in chunks of bpe bits,
49// little endian (buff[0] is the least significant word). 47// little endian (buff[0] is the least significant word).
50// Negative bigInts are stored two's complement. 48// Negative bigInts are stored two's complement.
51// Some functions assume their parameters have at least one leading zero element. 49// Some functions assume their parameters have at least one leading zero element.
52// Functions with an underscore at the end of the name have unpredictable behavior in case of overflow, 50// Functions with an underscore at the end of the name have unpredictable behavior in case of overflow,
53// so the caller must make sure overflow won't happen. 51// so the caller must make sure overflow won't happen.
54// For each function where a parameter is modified, that same 52// For each function where a parameter is modified, that same
55// variable must not be used as another argument too. 53// variable must not be used as another argument too.
56// So, you cannot square x by doing multMod_(x,x,n). 54// So, you cannot square x by doing multMod_(x,x,n).
57// You must use squareMod_(x,n) instead, or do y=dup(x); multMod_(x,y,n). 55// You must use squareMod_(x,n) instead, or do y=dup(x); multMod_(x,y,n).
58// 56//
59// These functions are designed to avoid frequent dynamic memory allocation in the inner loop. 57// These functions are designed to avoid frequent dynamic memory allocation in the inner loop.
60// For most functions, if it needs a BigInt as a local variable it will actually use 58// For most functions, if it needs a BigInt as a local variable it will actually use
61// a global, and will only allocate to it when it's not the right size. This ensures 59// a global, and will only allocate to it when it's not the right size. This ensures
62// that when a function is called repeatedly with same-sized parameters, it only allocates 60// that when a function is called repeatedly with same-sized parameters, it only allocates
63// memory on the first call. 61// memory on the first call.
64// 62//
65// Note that for cryptographic purposes, the calls to Math.random() must 63// Note that for cryptographic purposes, the calls to Math.random() must
66// be replaced with calls to a better pseudorandom number generator. 64// be replaced with calls to a better pseudorandom number generator.
67// 65//
68// In the following, "bigInt" means a bigInt with at least one leading zero element, 66// In the following, "bigInt" means a bigInt with at least one leading zero element,
69// and "integer" means a nonnegative integer less than radix. In some cases, integer 67// and "integer" means a nonnegative integer less than radix. In some cases, integer
70// can be negative. Negative bigInts are 2s complement. 68// can be negative. Negative bigInts are 2s complement.
71// 69//
72// The following functions do not modify their inputs, but dynamically allocate memory every time they are called: 70// The following functions do not modify their inputs, but dynamically allocate memory every time they are called:
73// 71//
74// function bigInt2str(x,base) //convert a bigInt into a string in a given base, from base 2 up to base 95 72// function bigInt2str(x,base) //convert a bigInt into a string in a given base, from base 2 up to base 95
75// function dup(x) //returns a copy of bigInt x 73// function dup(x) //returns a copy of bigInt x
76// function findPrimes(n) //return array of all primes less than integer n 74// function findPrimes(n) //return array of all primes less than integer n
77// function int2bigInt(t,n,m) //convert integer t to a bigInt with at least n bits and m array elements 75// function int2bigInt(t,n,m) //convert integer t to a bigInt with at least n bits and m array elements
78// function int2bigInt(s,b,n,m) //convert string s in base b to a bigInt with at least n bits and m array elements 76// function int2bigInt(s,b,n,m) //convert string s in base b to a bigInt with at least n bits and m array elements
79// function trim(x,k) //return a copy of x with exactly k leading zero elements 77// function trim(x,k) //return a copy of x with exactly k leading zero elements
80// 78//
81// The following functions do not modify their inputs, so there is never a problem with the result being too big: 79// The following functions do not modify their inputs, so there is never a problem with the result being too big:
82// 80//
83// function bitSize(x) //returns how many bits long the bigInt x is, not counting leading zeros 81// function bitSize(x) //returns how many bits long the bigInt x is, not counting leading zeros
84// function equals(x,y) //is the bigInt x equal to the bigint y? 82// function equals(x,y) //is the bigInt x equal to the bigint y?
85// function equalsInt(x,y) //is bigint x equal to integer y? 83// function equalsInt(x,y) //is bigint x equal to integer y?
86// function greater(x,y) //is x>y? (x and y are nonnegative bigInts) 84// function greater(x,y) //is x>y? (x and y are nonnegative bigInts)
87// function greaterShift(x,y,shift)//is (x <<(shift*bpe)) > y? 85// function greaterShift(x,y,shift)//is (x <<(shift*bpe)) > y?
88// function isZero(x) //is the bigInt x equal to zero? 86// function isZero(x) //is the bigInt x equal to zero?
89// function millerRabin(x,b) //does one round of Miller-Rabin base integer b say that bigInt x is possibly prime (as opposed to definitely composite)? 87// function millerRabin(x,b) //does one round of Miller-Rabin base integer b say that bigInt x is possibly prime (as opposed to definitely composite)?
90// function modInt(x,n) //return x mod n for bigInt x and integer n. 88// function modInt(x,n) //return x mod n for bigInt x and integer n.
91// function negative(x) //is bigInt x negative? 89// function negative(x) //is bigInt x negative?
92// 90//
93// The following functions do not modify their inputs, but allocate memory and call functions with underscores 91// The following functions do not modify their inputs, but allocate memory and call functions with underscores
94// 92//
95// function add(x,y) //return (x+y) for bigInts x and y. 93// function add(x,y) //return (x+y) for bigInts x and y.
96// function addInt(x,n) //return (x+n) where x is a bigInt and n is an integer. 94// function addInt(x,n) //return (x+n) where x is a bigInt and n is an integer.
97// function expand(x,n) //return a copy of x with at least n elements, adding leading zeros if needed 95// function expand(x,n) //return a copy of x with at least n elements, adding leading zeros if needed
98// function inverseMod(x,n) //return (x**(-1) mod n) for bigInts x and n. If no inverse exists, it returns null 96// function inverseMod(x,n) //return (x**(-1) mod n) for bigInts x and n. If no inverse exists, it returns null
99// function mod(x,n) //return a new bigInt equal to (x mod n) for bigInts x and n. 97// function mod(x,n) //return a new bigInt equal to (x mod n) for bigInts x and n.
100// function mult(x,y) //return x*y for bigInts x and y. This is faster when y<x. 98// function mult(x,y) //return x*y for bigInts x and y. This is faster when y<x.
101// function multMod(x,y,n) //return (x*y mod n) for bigInts x,y,n. For greater speed, let y<x. 99// function multMod(x,y,n) //return (x*y mod n) for bigInts x,y,n. For greater speed, let y<x.
102// function powMod(x,y,n) //return (x**y mod n) where x,y,n are bigInts and ** is exponentiation. 0**0=1. Faster for odd n. 100// function powMod(x,y,n) //return (x**y mod n) where x,y,n are bigInts and ** is exponentiation. 0**0=1. Faster for odd n.
103// function randTruePrime(k) //return a new, random, k-bit, true prime using Maurer's algorithm. 101// function randTruePrime(k) //return a new, random, k-bit, true prime using Maurer's algorithm.
104// function sub(x,y) //return (x-y) for bigInts x and y. Negative answers will be 2s complement 102// function sub(x,y) //return (x-y) for bigInts x and y. Negative answers will be 2s complement
105// 103//
106// The following functions write a bigInt result to one of the parameters, but 104// The following functions write a bigInt result to one of the parameters, but
107// the result is never bigger than the original, so there can't be overflow problems: 105// the result is never bigger than the original, so there can't be overflow problems:
108// 106//
109// function divInt_(x,n) //do x=floor(x/n) for bigInt x and integer n, and return the remainder 107// function divInt_(x,n) //do x=floor(x/n) for bigInt x and integer n, and return the remainder
110// function GCD_(x,y) //set x to the greatest common divisor of bigInts x and y, (y is destroyed). 108// function GCD_(x,y) //set x to the greatest common divisor of bigInts x and y, (y is destroyed).
111// function halve_(x) //do x=floor(|x|/2)*sgn(x) for bigInt x in 2's complement 109// function halve_(x) //do x=floor(|x|/2)*sgn(x) for bigInt x in 2's complement
112// function mod_(x,n) //do x=x mod n for bigInts x and n. 110// function mod_(x,n) //do x=x mod n for bigInts x and n.
113// function rightShift_(x,n) //right shift bigInt x by n bits. 0 <= n < bpe. 111// function rightShift_(x,n) //right shift bigInt x by n bits. 0 <= n < bpe.
114// 112//
115// The following functions write a bigInt result to one of the parameters. The caller is responsible for 113// The following functions write a bigInt result to one of the parameters. The caller is responsible for
116// ensuring it is large enough to hold the result. 114// ensuring it is large enough to hold the result.
117// 115//
118// function addInt_(x,n) //do x=x+n where x is a bigInt and n is an integer 116// function addInt_(x,n) //do x=x+n where x is a bigInt and n is an integer
@@ -1384,193 +1382,192 @@ function mont_(x,y,n,np) {
1384 } 1382 }
1385 for (;j<kn;j++) { 1383 for (;j<kn;j++) {
1386 c+=sa[j]+ui*n[j]; 1384 c+=sa[j]+ui*n[j];
1387 sa[j-1]=c & mask; 1385 sa[j-1]=c & mask;
1388 c>>=bpe; 1386 c>>=bpe;
1389 } 1387 }
1390 sa[j-1]=c & mask; 1388 sa[j-1]=c & mask;
1391 } 1389 }
1392 1390
1393 if (!greater(n,sa)) 1391 if (!greater(n,sa))
1394 sub_(sa,n); 1392 sub_(sa,n);
1395 copy_(x,sa); 1393 copy_(x,sa);
1396} 1394}
1397 1395
1398 1396
1399 1397
1400 1398
1401//############################################################################# 1399//#############################################################################
1402//############################################################################# 1400//#############################################################################
1403//############################################################################# 1401//#############################################################################
1404//############################################################################# 1402//#############################################################################
1405//############################################################################# 1403//#############################################################################
1406//############################################################################# 1404//#############################################################################
1407//############################################################################# 1405//#############################################################################
1408 1406
1409 1407
1410 1408
1411 1409
1412 1410
1413//############################################################################# 1411//#############################################################################
1414 1412
1415Clipperz.Crypto.BigInt = function (aValue, aBase) { 1413Clipperz.Crypto.BigInt = function (aValue, aBase) {
1416 varbase; 1414 varbase;
1417 varvalue; 1415 varvalue;
1418 1416
1419 if (typeof(aValue) == 'object') { 1417 if (typeof(aValue) == 'object') {
1420 this._internalValue = aValue; 1418 this._internalValue = aValue;
1421 } else { 1419 } else {
1422 if (typeof(aValue) == 'undefined') { 1420 if (typeof(aValue) == 'undefined') {
1423 value = "0"; 1421 value = "0";
1424 } else { 1422 } else {
1425 value = aValue + ""; 1423 value = aValue + "";
1426 } 1424 }
1427 1425
1428 if (typeof(aBase) == 'undefined') { 1426 if (typeof(aBase) == 'undefined') {
1429 base = 10; 1427 base = 10;
1430 } else { 1428 } else {
1431 base = aBase; 1429 base = aBase;
1432 } 1430 }
1433 1431
1434 this._internalValue = str2bigInt(value, base, 1, 1); 1432 this._internalValue = str2bigInt(value, base, 1, 1);
1435 } 1433 }
1436 1434
1437 return this; 1435 return this;
1438} 1436}
1439 1437
1440//============================================================================= 1438//=============================================================================
1441 1439
1442MochiKit.Base.update(Clipperz.Crypto.BigInt.prototype, { 1440MochiKit.Base.update(Clipperz.Crypto.BigInt.prototype, {
1443 1441
1444 'clone': function() { 1442 'clone': function() {
1445 return new Clipperz.Crypto.BigInt(this.internalValue()); 1443 return new Clipperz.Crypto.BigInt(this.internalValue());
1446 }, 1444 },
1447 1445
1448 //------------------------------------------------------------------------- 1446 //-------------------------------------------------------------------------
1449 1447
1450 'internalValue': function () { 1448 'internalValue': function () {
1451 return this._internalValue; 1449 return this._internalValue;
1452 }, 1450 },
1453 1451
1454 //------------------------------------------------------------------------- 1452 //-------------------------------------------------------------------------
1455 1453
1456 'isBigInt': true, 1454 'isBigInt': true,
1457 1455
1458 //------------------------------------------------------------------------- 1456 //-------------------------------------------------------------------------
1459 1457
1460 'toString': function(aBase) { 1458 'toString': function(aBase) {
1461 return this.asString(aBase); 1459 return this.asString(aBase);
1462 }, 1460 },
1463 1461
1464 //------------------------------------------------------------------------- 1462 //-------------------------------------------------------------------------
1465 1463
1466 'asString': function (aBase, minimumLength) { 1464 'asString': function (aBase, minimumLength) {
1467 varresult; 1465 varresult;
1468 varbase; 1466 varbase;
1469 1467
1470 if (typeof(aBase) == 'undefined') { 1468 if (typeof(aBase) == 'undefined') {
1471 base = 10; 1469 base = 10;
1472 } else { 1470 } else {
1473 base = aBase; 1471 base = aBase;
1474 } 1472 }
1475 1473
1476 result = bigInt2str(this.internalValue(), base).toLowerCase(); 1474 result = bigInt2str(this.internalValue(), base).toLowerCase();
1477 1475
1478 if ((typeof(minimumLength) != 'undefined') && (result.length < minimumLength)) { 1476 if ((typeof(minimumLength) != 'undefined') && (result.length < minimumLength)) {
1479 var i, c; 1477 var i, c;
1480 //MochiKit.Logging.logDebug(">>> FIXING BigInt.asString length issue")
1481 c = (minimumLength - result.length); 1478 c = (minimumLength - result.length);
1482 for (i=0; i<c; i++) { 1479 for (i=0; i<c; i++) {
1483 result = '0' + result; 1480 result = '0' + result;
1484 } 1481 }
1485 } 1482 }
1486 1483
1487 return result; 1484 return result;
1488 }, 1485 },
1489 1486
1490 //------------------------------------------------------------------------- 1487 //-------------------------------------------------------------------------
1491 1488
1492 'asByteArray': function() { 1489 'asByteArray': function() {
1493 return new Clipperz.ByteArray("0x" + this.asString(16), 16); 1490 return new Clipperz.ByteArray("0x" + this.asString(16), 16);
1494 }, 1491 },
1495 1492
1496 //------------------------------------------------------------------------- 1493 //-------------------------------------------------------------------------
1497 1494
1498 'equals': function (aValue) { 1495 'equals': function (aValue) {
1499 var result; 1496 var result;
1500 1497
1501 if (aValue.isBigInt) { 1498 if (aValue.isBigInt) {
1502 result = equals(this.internalValue(), aValue.internalValue()); 1499 result = equals(this.internalValue(), aValue.internalValue());
1503 } else if (typeof(aValue) == "number") { 1500 } else if (typeof(aValue) == "number") {
1504 result = equalsInt(this.internalValue(), aValue); 1501 result = equalsInt(this.internalValue(), aValue);
1505 } else { 1502 } else {
1506 throw Clipperz.Crypt.BigInt.exception.UnknownType; 1503 throw Clipperz.Crypt.BigInt.exception.UnknownType;
1507 } 1504 }
1508 1505
1509 return result; 1506 return result;
1510 }, 1507 },
1511 1508
1512 //------------------------------------------------------------------------- 1509 //-------------------------------------------------------------------------
1513 1510
1514 'compare': function(aValue) { 1511 'compare': function(aValue) {
1515/* 1512/*
1516 var result; 1513 var result;
1517 var thisAsString; 1514 var thisAsString;
1518 var aValueAsString; 1515 var aValueAsString;
1519 1516
1520 thisAsString = this.asString(10); 1517 thisAsString = this.asString(10);
1521 aValueAsString = aValue.asString(10); 1518 aValueAsString = aValue.asString(10);
1522 1519
1523 result = MochiKit.Base.compare(thisAsString.length, aValueAsString.length); 1520 result = MochiKit.Base.compare(thisAsString.length, aValueAsString.length);
1524 if (result == 0) { 1521 if (result == 0) {
1525 result = MochiKit.Base.compare(thisAsString, aValueAsString); 1522 result = MochiKit.Base.compare(thisAsString, aValueAsString);
1526 } 1523 }
1527 1524
1528 return result; 1525 return result;
1529*/ 1526*/
1530 var result; 1527 var result;
1531 1528
1532 if (equals(this.internalValue(), aValue.internalValue())) { 1529 if (equals(this.internalValue(), aValue.internalValue())) {
1533 result = 0; 1530 result = 0;
1534 } else if (greater(this.internalValue(), aValue.internalValue())) { 1531 } else if (greater(this.internalValue(), aValue.internalValue())) {
1535 result = 1; 1532 result = 1;
1536 } else { 1533 } else {
1537 result = -1; 1534 result = -1;
1538 } 1535 }
1539 1536
1540 return result; 1537 return result;
1541 }, 1538 },
1542 1539
1543 //------------------------------------------------------------------------- 1540 //-------------------------------------------------------------------------
1544 1541
1545 'add': function (aValue) { 1542 'add': function (aValue) {
1546 var result; 1543 var result;
1547 1544
1548 if (aValue.isBigInt) { 1545 if (aValue.isBigInt) {
1549 result = add(this.internalValue(), aValue.internalValue()); 1546 result = add(this.internalValue(), aValue.internalValue());
1550 } else { 1547 } else {
1551 result = addInt(this.internalValue(), aValue); 1548 result = addInt(this.internalValue(), aValue);
1552 } 1549 }
1553 1550
1554 return new Clipperz.Crypto.BigInt(result); 1551 return new Clipperz.Crypto.BigInt(result);
1555 }, 1552 },
1556 1553
1557 //------------------------------------------------------------------------- 1554 //-------------------------------------------------------------------------
1558 1555
1559 'subtract': function (aValue) { 1556 'subtract': function (aValue) {
1560 var result; 1557 var result;
1561 var value; 1558 var value;
1562 1559
1563 if (aValue.isBigInt) { 1560 if (aValue.isBigInt) {
1564 value = aValue; 1561 value = aValue;
1565 } else { 1562 } else {
1566 value = new Clipperz.Crypto.BigInt(aValue); 1563 value = new Clipperz.Crypto.BigInt(aValue);
1567 } 1564 }
1568 1565
1569 result = sub(this.internalValue(), value.internalValue()); 1566 result = sub(this.internalValue(), value.internalValue());
1570 1567
1571 return new Clipperz.Crypto.BigInt(result); 1568 return new Clipperz.Crypto.BigInt(result);
1572 }, 1569 },
1573 1570
1574 //------------------------------------------------------------------------- 1571 //-------------------------------------------------------------------------
1575 1572
1576 'multiply': function (aValue, aModule) { 1573 'multiply': function (aValue, aModule) {