diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index af326634b1..bf47e04a9e 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -306,6 +306,7 @@ All definitions files include a header with the author and editors, so at some p * [SignalR](http://www.asp.net/signalr) (by [Boris Yankov](https://github.com/borisyankov)) * [simple-cw-node](https://github.com/astronaughts/simple-cw-node) (by [vvakame](https://github.com/vvakame)) * [Sinon](http://sinonjs.org/) (by [William Sears](https://github.com/mrbigdog2u)) +* [sjcl](http://crypto.stanford.edu/sjcl/) (by [Eugene Chernyshov](https://github.com/Evgenus)) * [SlickGrid](https://github.com/mleibman/SlickGrid) (by [Josh Baldwin](https://github.com/jbaldwin)) * [smoothie](https://github.com/joewalnes/smoothie) (by [Mike H. Hawley](https://github.com/mikehhawley) and [Drew Noakes](https://drewnoakes.com)) * [socket.io](http://socket.io) (by [William Orr](https://github.com/worr)) diff --git a/sjcl/sjcl-tests.ts b/sjcl/sjcl-tests.ts new file mode 100644 index 0000000000..4ff603203f --- /dev/null +++ b/sjcl/sjcl-tests.ts @@ -0,0 +1,267 @@ +/// + +var b: boolean; +var n: number; +var s: string; +var bn: sjcl.BigNumber; +var ba: sjcl.BitArray; + +function testBigNumber() { + bn = new sjcl.bn(); + bn = new sjcl.bn(0); + bn = new sjcl.bn("0"); + bn = new sjcl.bn(bn); + + bn = bn.initWith(0); + bn = bn.initWith("0"); + bn = bn.initWith(bn); + + bn = bn.addM(0); + bn = bn.addM("0"); + bn = bn.addM(bn); + + bn = bn.subM(0); + bn = bn.subM("0"); + bn = bn.subM(bn); + + bn = bn.mod(0); + bn = bn.mod("0"); + bn = bn.mod(bn); + + bn = bn.inverseMod(0); + bn = bn.inverseMod("0"); + bn = bn.inverseMod(bn); + + bn = bn.add(0); + bn = bn.add("0"); + bn = bn.add(bn); + + bn = bn.sub(0); + bn = bn.sub("0"); + bn = bn.sub(bn); + + bn = bn.mul(0); + bn = bn.mul("0"); + bn = bn.mul(bn); + + bn = bn.mulmod(0, 0); + bn = bn.mulmod(0, "0"); + bn = bn.mulmod(0, bn); + bn = bn.mulmod("0", 0); + bn = bn.mulmod("0", "0"); + bn = bn.mulmod("0", bn); + bn = bn.mulmod(bn, 0); + bn = bn.mulmod(bn, "0"); + bn = bn.mulmod(bn, bn); + + bn = bn.powermod(0, 0); + bn = bn.powermod(0, "0"); + bn = bn.powermod(0, bn); + bn = bn.powermod("0", 0); + bn = bn.powermod("0", "0"); + bn = bn.powermod("0", bn); + bn = bn.powermod(bn, 0); + bn = bn.powermod(bn, "0"); + bn = bn.powermod(bn, bn); + + bn = bn.copy(); + + b = bn.equals(0); + b = bn.equals(bn); + + b = bn.greaterEquals(0); + b = bn.greaterEquals(bn); + + n = bn.getLimb(0); + + s = bn.toString(); + + bn = bn.doubleM(); + + bn = bn.halveM(); + + bn = bn.square(); + + bn = bn.power(1); + bn = bn.power([1, 1]); + bn = bn.power(bn); + + bn = bn.trim(); + + bn = bn.reduce(); + + bn = bn.fullReduce(); + + bn = bn.normalize(); + + bn = bn.cnormalize(); + + ba = bn.toBits(); + ba = bn.toBits(1); + + n = bn.bitLength(); + + bn = sjcl.bn.fromBits(ba); +} + +function testBitArray() { + ba = sjcl.bitArray.bitSlice(ba, 0, 1); + + n = sjcl.bitArray.extract(ba, 0, 1); + + ba = sjcl.bitArray.concat(ba, ba); + + n = sjcl.bitArray.bitLength(ba); + + ba = sjcl.bitArray.clamp(ba, 0); + + n = sjcl.bitArray.partial(1, 1); + n = sjcl.bitArray.partial(1, 1, 0); + + n = sjcl.bitArray.getPartial(0); + + b = sjcl.bitArray.equal(ba, ba); + + ba = sjcl.bitArray._shiftRight(ba, 0); + ba = sjcl.bitArray._shiftRight(ba, 0, 0); + ba = sjcl.bitArray._shiftRight(ba, 0, 0, ba); +} + +function testCodecs() { + s = sjcl.codec.base64.fromBits(ba); + ba = sjcl.codec.base64.toBits(s); + + s = sjcl.codec.base64url.fromBits(ba); + ba = sjcl.codec.base64url.toBits(s); + + s = sjcl.codec.hex.fromBits(ba); + ba = sjcl.codec.hex.toBits(s); + + s = sjcl.codec.utf8String.fromBits(ba); + ba = sjcl.codec.utf8String.toBits(s); + + var bytes: number[] = sjcl.codec.bytes.fromBits(ba); + ba = sjcl.codec.bytes.toBits(bytes); +} + +function testHashes() { + var hash: sjcl.SjclHash; + ba = hash.reset().update("xxx").update(ba).finalize(); + + hash = new sjcl.hash.sha1(); + hash = new sjcl.hash.sha1(hash); + ba = sjcl.hash.sha1.hash(ba); + ba = sjcl.hash.sha1.hash("xxx"); + + hash = new sjcl.hash.sha256(); + hash = new sjcl.hash.sha256(hash); + ba = sjcl.hash.sha256.hash(ba); + ba = sjcl.hash.sha256.hash("xxx"); + + hash = new sjcl.hash.sha512(); + hash = new sjcl.hash.sha512(hash); + ba = sjcl.hash.sha512.hash(ba); + ba = sjcl.hash.sha512.hash("xxx"); +} + +function testSymetric() { + var aes = new sjcl.cipher.aes([0, 0, 0, 0]); + + ba = sjcl.mode.cbc.encrypt(aes, ba, ba); + ba = sjcl.mode.cbc.encrypt(aes, ba, ba, ba); + + ba = sjcl.mode.gcm.encrypt(aes, ba, ba); + ba = sjcl.mode.gcm.encrypt(aes, ba, ba, ba); + ba = sjcl.mode.gcm.encrypt(aes, ba, ba, ba, 128); + + ba = sjcl.mode.ccm.encrypt(aes, ba, ba); + ba = sjcl.mode.ccm.encrypt(aes, ba, ba, ba); + ba = sjcl.mode.ccm.encrypt(aes, ba, ba, ba, 128); + + ba = sjcl.mode.ocb2.encrypt(aes, ba, ba); + ba = sjcl.mode.ocb2.encrypt(aes, ba, ba, ba); + ba = sjcl.mode.ocb2.encrypt(aes, ba, ba, ba, 128); + ba = sjcl.mode.ocb2.encrypt(aes, ba, ba, ba, 128, false); +} + +function testHmacPbdkf2() { + ba = sjcl.misc.pbkdf2("xxx", "xxx"); + ba = sjcl.misc.pbkdf2("xxx", "xxx", 1000); + ba = sjcl.misc.pbkdf2("xxx", "xxx", 1000, 12); + ba = sjcl.misc.pbkdf2("xxx", "xxx", 1000, 12, sjcl.misc.hmac); + + ba = sjcl.misc.pbkdf2("xxx", ba); + ba = sjcl.misc.pbkdf2("xxx", ba, 1000); + ba = sjcl.misc.pbkdf2("xxx", ba, 1000, 12); + ba = sjcl.misc.pbkdf2("xxx", ba, 1000, 12, sjcl.misc.hmac); + + ba = sjcl.misc.pbkdf2(ba, "xxx"); + ba = sjcl.misc.pbkdf2(ba, "xxx", 1000); + ba = sjcl.misc.pbkdf2(ba, "xxx", 1000, 12); + ba = sjcl.misc.pbkdf2(ba, "xxx", 1000, 12, sjcl.misc.hmac); + + ba = sjcl.misc.pbkdf2(ba, ba); + ba = sjcl.misc.pbkdf2(ba, ba, 1000); + ba = sjcl.misc.pbkdf2(ba, ba, 1000, 12); + ba = sjcl.misc.pbkdf2(ba, ba, 1000, 12, sjcl.misc.hmac); + + var hmac: sjcl.SjclHmac; + hmac = new sjcl.misc.hmac(ba); + hmac = new sjcl.misc.hmac(ba, sjcl.hash.sha512); + + ba = hmac.mac("xxx"); + ba = hmac.mac(ba); + + ba = hmac.encrypt("xxx"); + ba = hmac.encrypt(ba); + + hmac.reset(); + + hmac.update("xxx"); + hmac.update(ba); + + ba = hmac.digest(); +} + +function testECC() { + var keys = sjcl.ecc.elGamal.generateKeys(192, 0); + + var ciphertext = sjcl.encrypt(keys.pub, "hello world"); + var plaintext = sjcl.decrypt(keys.sec, ciphertext); + + // TODO: Maybe deeper testing required. Let me know +} + +function testRandom() { + b = sjcl.random.isReady(); + ba = sjcl.random.randomWords(8); + ba = sjcl.random.randomWords(8, 6); + + var rnd = new sjcl.prng(1); + ba = rnd.randomWords(16); + ba = rnd.randomWords(16, 6); +} + +function testSRP() { + var group = sjcl.keyexchange.srp.knownGroup(1024); + ba = sjcl.codec.hex.toBits(s); + ba = sjcl.keyexchange.srp.makeX(s, s, ba); + ba = sjcl.keyexchange.srp.makeVerifier(s, s, ba, group); +} + +function testConvenince() { + var x: sjcl.SjclCipherEncrypted; + x = sjcl.encrypt("xxx", "text"); + s = sjcl.decrypt(ba, x); + + x = sjcl.encrypt("xxx", "text", { iv: ba, salt: ba }); + s = sjcl.decrypt(ba, x, { iv: ba, salt: ba }); + + var y: sjcl.SjclCipherDecrypted; + + sjcl.encrypt("xxx", "text", { iv: ba, salt: ba, mode: "gcm" }, x); + s = sjcl.decrypt(ba, x, { iv: ba, salt: ba, mode: "gcm" }, y); + + sjcl.encrypt("xxx", "text", { iv: ba, salt: ba, mode: "gcm", iter: 200 }, x); + s = sjcl.decrypt(ba, x, { iv: ba, salt: ba, mode: "gcm", iter: 200 }, y); +} \ No newline at end of file diff --git a/sjcl/sjcl.d.ts b/sjcl/sjcl.d.ts new file mode 100644 index 0000000000..8413edb2fd --- /dev/null +++ b/sjcl/sjcl.d.ts @@ -0,0 +1,556 @@ +// Type definitions for sjcl v1.0.1 +// Project: http://crypto.stanford.edu/sjcl/ +// Definitions by: Eugene Chernyshov +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module sjcl { + + export var bn: BigNumberStatic; + export var bitArray: BitArrayStatic; + export var codec: SjclCodecs; + export var hash: SjclHashes; + export var exception: SjclExceptions; + export var cipher: SjclCiphers; + export var mode: SjclModes; + export var misc: SjclMisc; + export var ecc: SjclEllipticCurveCryptography; + export var random: SjclRandom; + export var prng: SjclRandomStatic; + export var keyexchange: SjclKeyExchange; + export var json: SjclJson; + export var encrypt: SjclConveninceEncryptor; + export var decrypt: SjclConveninceDecryptor; + + // ________________________________________________________________________ + + interface BigNumber { + radix: number; + maxMul: number; + + copy(): BigNumber; + + /// Initializes this with it, either as a bn, a number, or a hex string. + initWith: TypeHelpers.BigNumberBinaryOperator; + + /// Returns true if "this" and "that" are equal. Calls fullReduce(). + /// Equality test is in constant time. + equals(that: number): boolean; + equals(that: BigNumber): boolean; + + /// Get the i'th limb of this, zero if i is too large. + getLimb(index: number): number; + + /// Constant time comparison function. + /// Returns 1 if this >= that, or zero otherwise. + greaterEquals(that: number): boolean; + greaterEquals(that: BigNumber): boolean; + + /// Convert to a hex string. + toString(): string; + + /// this += that. Does not normalize. + addM: TypeHelpers.BigNumberBinaryOperator; + + /// this *= 2. Requires normalized; ends up normalized. + doubleM(): BigNumber; + + /// this /= 2, rounded down. Requires normalized; ends up normalized. + halveM(): BigNumber; + + /// this -= that. Does not normalize. + subM: TypeHelpers.BigNumberBinaryOperator; + + mod: TypeHelpers.BigNumberBinaryOperator; + + /// return inverse mod prime p. p must be odd. Binary extended Euclidean algorithm mod p. + inverseMod: TypeHelpers.BigNumberBinaryOperator; + + /// this + that. Does not normalize. + add: TypeHelpers.BigNumberBinaryOperator; + + /// this - that. Does not normalize. + sub: TypeHelpers.BigNumberBinaryOperator; + + /// this * that. Normalizes and reduces. + mul: TypeHelpers.BigNumberBinaryOperator; + + /// this ^ 2. Normalizes and reduces. + square(): BigNumber; + + /// this ^ n. Uses square-and-multiply. Normalizes and reduces. + power(n: number): BigNumber; + power(n: BigNumber): BigNumber; + power(a: number[]): BigNumber; + + /// this * that mod N + mulmod: TypeHelpers.BigNumberTrinaryOperator; + + /// this ^ x mod N + powermod: TypeHelpers.BigNumberTrinaryOperator; + + trim(): BigNumber; + + /// Reduce mod a modulus. Stubbed for subclassing. + reduce(): BigNumber; + + /// Reduce and normalize. + fullReduce(): BigNumber; + + /// Propagate carries. + normalize(): BigNumber; + + /// Constant-time normalize. Does not allocate additional space. + cnormalize(): BigNumber; + + /// Serialize to a bit array + toBits(len?: number): BitArray; + + /// Return the length in bits, rounded up to the nearest byte. + bitLength(): number; + } + + interface BigNumberStatic { + new (): BigNumber; + new (n: string): BigNumber; + new (n: number): BigNumber; + new (n: BigNumber): BigNumber; + + fromBits(bits: BitArray): BigNumber; + random: TypeHelpers.Bind1; + prime: { + p127: PseudoMersennePrimeStatic; + // Bernstein's prime for Curve25519 + p25519: PseudoMersennePrimeStatic; + // Koblitz primes + p192k: PseudoMersennePrimeStatic; + p224k: PseudoMersennePrimeStatic; + p256k: PseudoMersennePrimeStatic; + // NIST primes + p192: PseudoMersennePrimeStatic; + p224: PseudoMersennePrimeStatic; + p256: PseudoMersennePrimeStatic; + p384: PseudoMersennePrimeStatic; + p521: PseudoMersennePrimeStatic; + }; + + pseudoMersennePrime(exponent: number, coeff: number[][]): PseudoMersennePrimeStatic; + } + + interface PseudoMersennePrime extends BigNumber { + reduce(): PseudoMersennePrime; + fullReduce(): PseudoMersennePrime; + inverse(): PseudoMersennePrime; + } + + interface PseudoMersennePrimeStatic extends BigNumberStatic { + new (): PseudoMersennePrime; + new (n: string): PseudoMersennePrime; + new (n: number): PseudoMersennePrime; + new (n: BigNumber): PseudoMersennePrime; + } + + // ________________________________________________________________________ + + interface BitArray extends Array { + } + + interface BitArrayStatic { + /// Array slices in units of bits. + bitSlice(a: BitArray, bstart: number, bend: number): BitArray; + + /// Extract a number packed into a bit array. + extract(a: BitArray, bstart: number, blenth: number): number; + + /// Concatenate two bit arrays. + concat(a1: BitArray, a2: BitArray): BitArray + + /// Find the length of an array of bits. + bitLength(a: BitArray): number; + + /// Truncate an array. + clamp(a: BitArray, len: number): BitArray; + + /// Make a partial word for a bit array. + partial(len: number, x: number, _end?: number): number; + + /// Get the number of bits used by a partial word. + getPartial(x: number): number; + + /// Compare two arrays for equality in a predictable amount of time. + equal(a: BitArray, b: BitArray): boolean; + + /// Shift an array right. + _shiftRight(a: BitArray, shift: number, carry?: number, out?: BitArray): BitArray; + + /// xor a block of 4 words together. + _xor4(x: number[], y: number[]): number[]; + } + + // ________________________________________________________________________ + + interface SjclCodec { + fromBits(bits: BitArray): T; + toBits(value: T): BitArray; + } + + interface SjclCodecs { + utf8String: SjclCodec; + hex: SjclCodec; + bytes: SjclCodec; + base64: SjclCodec; + base64url: SjclCodec; + } + + // ________________________________________________________________________ + + interface SjclHash { + reset(): SjclHash; + update(data: string): SjclHash; + update(data: BitArray): SjclHash; + finalize(): BitArray; + } + + interface SjclHashStatic { + new (hash?: SjclHash): SjclHash; + hash(data: string): BitArray; + hash(data: BitArray): BitArray; + } + + interface SjclHashes { + sha1: SjclHashStatic; + sha256: SjclHashStatic; + sha512: SjclHashStatic; + } + + // ________________________________________________________________________ + + interface SjclExceptions { + corrupt: SjclExceptionFactory; + invalid: SjclExceptionFactory; + bug: SjclExceptionFactory; + notReady: SjclExceptionFactory; + } + + interface SjclExceptionFactory { + new (message: string): Error; + } + + // ________________________________________________________________________ + + interface SjclCiphers { + aes: SjclCipherStatic; + } + + interface SjclCipher { + encrypt(data: number[]): number[]; + decrypt(data: number[]): number[]; + } + + interface SjclCipherStatic { + new (key: number[]): SjclCipher; + } + + // ________________________________________________________________________ + + interface SjclModes { + gcm: SjclGCMMode; + ccm: SjclCCMMode; + ocb2: SjclOCB2Mode; + cbc: SjclCBCMode; + } + + interface SjclGCMMode { + encrypt(prp: SjclCipher, plaintext: BitArray, iv: BitArray, adata?: BitArray, tlen?: number): BitArray; + decrypt(prp: SjclCipher, ciphertext: BitArray, iv: BitArray, adata?: BitArray, tlen?: number): BitArray; + } + + interface SjclCCMMode { + encrypt(prp: SjclCipher, plaintext: BitArray, iv: BitArray, adata?: BitArray, tlen?: number): BitArray; + decrypt(prp: SjclCipher, ciphertext: BitArray, iv: BitArray, adata?: BitArray, tlen?: number): BitArray; + } + + interface SjclOCB2Mode { + encrypt(prp: SjclCipher, plaintext: BitArray, iv: BitArray, adata?: BitArray, tlen?: number, premac?: boolean): BitArray; + decrypt(prp: SjclCipher, ciphertext: BitArray, iv: BitArray, adata?: BitArray, tlen?: number, premac?: boolean): BitArray; + pmac(prp: SjclCipher, adata: BitArray): number[]; + } + + interface SjclCBCMode { + encrypt(prp: SjclCipher, plaintext: BitArray, iv: BitArray, adata?: BitArray): BitArray; + decrypt(prp: SjclCipher, ciphertext: BitArray, iv: BitArray, adata?: BitArray): BitArray; + } + + // ________________________________________________________________________ + + interface Pbkdf2Params { + iter?: number; + salt?: BitArray; + } + + interface SjclMisc { + pbkdf2(password: string, salt: string, count?: number, length?: number, Prff?: SjclPseudorandomFunctionFamilyStatic): BitArray; + pbkdf2(password: BitArray, salt: string, count?: number, length?: number, Prff?: SjclPseudorandomFunctionFamilyStatic): BitArray; + pbkdf2(password: BitArray, salt: BitArray, count?: number, length?: number, Prff?: SjclPseudorandomFunctionFamilyStatic): BitArray; + pbkdf2(password: string, salt: BitArray, count?: number, length?: number, Prff?: SjclPseudorandomFunctionFamilyStatic): BitArray; + hmac: SjclHmacStatic; + cachedPbkdf2(password: string, obj?: Pbkdf2Params): { + key: BitArray; + salt: BitArray; + }; + } + + class SjclPseudorandomFunctionFamily { + encrypt(data: string): BitArray; + encrypt(data: BitArray): BitArray; + } + + interface SjclHmac extends SjclPseudorandomFunctionFamily { + mac(data: string): BitArray; + mac(data: BitArray): BitArray; + reset(): void; + update(data: string): void; + update(data: BitArray): void; + digest(): BitArray; + } + + interface SjclPseudorandomFunctionFamilyStatic { + new (key: BitArray): SjclPseudorandomFunctionFamily; + } + + interface SjclHmacStatic { + new (key: BitArray, Hash?: SjclHashStatic): SjclHmac; + } + + // ________________________________________________________________________ + + interface SjclEllipticCurveCryptography { + point: SjclEllipticalPointStatic; + pointJac: SjclPointJacobianStatic; + curve: SjclEllipticalCurveStatic; + curves: { + c192: SjclEllipticalCurve; + c224: SjclEllipticalCurve; + c256: SjclEllipticalCurve; + c384: SjclEllipticalCurve; + k192: SjclEllipticalCurve; + k224: SjclEllipticalCurve; + k256: SjclEllipticalCurve; + }; + basicKey: SjclECCBasic; + elGamal: SjclElGamal; + ecdsa: SjclEcdsa; + } + + interface SjclEllipticalPoint { + toJac(): SjclPointJacobian; + mult(k: BigNumber): SjclEllipticalPoint; + mult2(k: BigNumber, k2: BigNumber, affine2: SjclEllipticalPoint): SjclEllipticalPoint; + multiples(): Array; + isValid(): boolean; + toBits(): BitArray; + } + + interface SjclEllipticalPointStatic { + new (curve: SjclEllipticalCurve, x?: BigNumber, y?: BigNumber): SjclEllipticalPoint; + } + + interface SjclPointJacobian { + add(T: SjclEllipticalPoint): SjclPointJacobian; + doubl(): SjclPointJacobian; + toAffine(): SjclEllipticalPoint; + mult(k: BigNumber, affine: SjclEllipticalPoint): SjclPointJacobian; + mult2(k1: BigNumber, affine: SjclEllipticalPoint, k2: BigNumber, affine2: SjclEllipticalPoint): SjclPointJacobian; + isValid(): boolean; + } + + interface SjclPointJacobianStatic { + new (curve: SjclEllipticalCurve, x?: BigNumber, y?: BigNumber, z?: BigNumber):SjclPointJacobian; + } + + interface SjclEllipticalCurve { + fromBits(bits: BitArray): SjclEllipticalPoint; + } + + interface SjclEllipticalCurveStatic { + new (Field: BigNumber, r: BigNumber, a: BigNumber, b: BigNumber, x: BigNumber, y: BigNumber): SjclEllipticalCurve; + } + + interface SjclKeyPair

{ + pub: P; + sec: S; + } + + interface SjclKeysGenerator

{ + (curve: SjclEllipticalCurve, paranoia: number, sec?: BigNumber): SjclKeyPair; + (curve: number, paranoia: number, sec?: BigNumber): SjclKeyPair; + } + + interface SjclECCPublicKeyData { + x: BitArray; + y: BitArray; + } + + class SjclECCPublicKey { + get(): SjclECCPublicKeyData; + } + + class SjclECCSecretKey { + get(): BitArray; + } + + interface SjclECCPublicKeyFactory { + new (curve: SjclEllipticalCurve, point: SjclEllipticalPoint): T; + new (curve: SjclEllipticalCurve, point: BitArray): T; + } + + interface SjclECCSecretKeyFactory { + new (curve: SjclEllipticalCurve, exponent: BigNumber): T; + } + + interface SjclECCBasic { + publicKey: SjclECCPublicKeyFactory; + secretKey: SjclECCSecretKeyFactory; + generateKeys(cn: string): SjclKeysGenerator; + } + + class SjclElGamalPublicKey extends SjclECCPublicKey { + kem(paranoia: number): { + key: BitArray; + tag: BitArray; + }; + } + + class SjclElGamalSecretKey extends SjclECCSecretKey { + unkem(tag: BitArray): BitArray; + dh(pk: SjclECCPublicKey): BitArray; + } + + interface SjclElGamal { + publicKey: SjclECCPublicKeyFactory; + secretKey: SjclECCSecretKeyFactory; + generateKeys: SjclKeysGenerator; + } + + class SjclEcdsaPublicKey extends SjclECCPublicKey { + verify(hash: BitArray, rs: BitArray, fakeLegacyVersion: boolean): boolean; + } + + class SjclEcdsaSecretKey extends SjclECCSecretKey { + sign(hash: BitArray, paranoia: number, fakeLegacyVersion: boolean, fixedKForTesting?: BigNumber): BitArray; + } + + interface SjclEcdsa { + publicKey: SjclECCPublicKeyFactory; + secretKey: SjclECCSecretKeyFactory; + generateKeys: SjclKeysGenerator; + } + + // ________________________________________________________________________ + + interface SjclRandom { + randomWords(nwords: number, paranoia?: number): BitArray; + setDefaultParanoia(paranoia: number, allowZeroParanoia: string): void; + addEntropy(data: number, estimatedEntropy: number, source: string): void; + addEntropy(data: number[], estimatedEntropy: number, source: string): void; + addEntropy(data: string, estimatedEntropy: number, source: string): void; + isReady(paranoia?: number): boolean; + getProgress(paranoia?: number): number; + startCollectors(): void; + stopCollectors(): void; + addEventListener(name: string, cb: Function): void; + removeEventListener(name: string, cb: Function): void; + } + + interface SjclRandomStatic { + new (defaultParanoia: number): SjclRandom; + } + + // ________________________________________________________________________ + + interface SjclKeyExchange { + srp: SecureRemotePassword; + } + + interface SjclSRPGroup { + N: BigNumber; + g: BigNumber; + } + + interface SecureRemotePassword { + makeVerifier(username: string, password: string, salt: BitArray, group: SjclSRPGroup): BitArray; + makeX(username: string, password: string, salt: BitArray): BitArray; + knownGroup(i: string): SjclSRPGroup; + knownGroup(i: number): SjclSRPGroup; + } + + // ________________________________________________________________________ + + interface SjclCipherParams { + v?: number; + iter?: number; + ks?: number; + ts?: number; + mode?: string; + adata?: string; + cipher?: string; + } + + interface SjclCipherEncryptParams extends SjclCipherParams { + salt: BitArray; + iv: BitArray; + } + + interface SjclCipherDecryptParams extends SjclCipherParams { + salt?: BitArray; + iv?: BitArray; + } + + interface SjclCipherEncrypted extends SjclCipherEncryptParams { + kemtag?: BitArray; + ct: BitArray; + } + + interface SjclCipherDecrypted extends SjclCipherEncrypted { + key: BitArray; + } + + interface SjclConveninceEncryptor { + (password: string, plaintext: string, params?: SjclCipherEncryptParams, rp?: SjclCipherEncrypted): SjclCipherEncrypted; + (password: BitArray, plaintext: string, params?: SjclCipherEncryptParams, rp?: SjclCipherEncrypted): SjclCipherEncrypted; + (password: SjclElGamalPublicKey, plaintext: string, params?: SjclCipherEncryptParams, rp?: SjclCipherEncrypted): SjclCipherEncrypted; + } + + interface SjclConveninceDecryptor { + (password: string, ciphertext: SjclCipherEncrypted, params?: SjclCipherDecryptParams, rp?: SjclCipherDecrypted): string; + (password: BitArray, ciphertext: SjclCipherEncrypted, params?: SjclCipherDecryptParams, rp?: SjclCipherDecrypted): string; + (password: SjclElGamalSecretKey, ciphertext: SjclCipherEncrypted, params?: SjclCipherDecryptParams, rp?: SjclCipherDecrypted): string; + } + + interface SjclJson { + encrypt: SjclConveninceEncryptor; + decrypt: SjclConveninceDecryptor; + encode(obj: Object): string; + decode(obj: string): Object; + } + + // ________________________________________________________________________ + + module TypeHelpers { + interface One { + (value: T): BigNumber; + } + + interface BigNumberBinaryOperator extends One, One, One { + } + + interface Two { + (x: T1, N: T2): BigNumber; + } + + interface Bind1 extends Two, Two, Two { + } + + interface BigNumberTrinaryOperator extends Bind1, Bind1, Bind1 { + } + } +}