forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sjcl.d.ts
560 lines (441 loc) · 18.5 KB
/
sjcl.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
// Type definitions for sjcl v1.0.1
// Project: http://crypto.stanford.edu/sjcl/
// Definitions by: Eugene Chernyshov <https://github.com/Evgenus>
// 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<number>;
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<number> {
}
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<T> {
fromBits(bits: BitArray): T;
toBits(value: T): BitArray;
}
interface SjclCodecs {
utf8String: SjclCodec<string>;
hex: SjclCodec<string>;
bytes: SjclCodec<number[]>;
base64: SjclCodec<string>;
base64url: SjclCodec<string>;
}
// ________________________________________________________________________
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<SjclEllipticalPoint>;
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<P extends SjclECCPublicKey, S extends SjclECCSecretKey> {
pub: P;
sec: S;
}
interface SjclKeysGenerator<P extends SjclECCPublicKey, S extends SjclECCSecretKey> {
(curve: SjclEllipticalCurve, paranoia: number, sec?: BigNumber): SjclKeyPair<P, S>;
(curve: number, paranoia: number, sec?: BigNumber): SjclKeyPair<P, S>;
}
interface SjclECCPublicKeyData {
x: BitArray;
y: BitArray;
}
class SjclECCPublicKey {
get(): SjclECCPublicKeyData;
}
class SjclECCSecretKey {
get(): BitArray;
}
interface SjclECCPublicKeyFactory<T extends SjclECCPublicKey> {
new (curve: SjclEllipticalCurve, point: SjclEllipticalPoint): T;
new (curve: SjclEllipticalCurve, point: BitArray): T;
}
interface SjclECCSecretKeyFactory<T extends SjclECCSecretKey> {
new (curve: SjclEllipticalCurve, exponent: BigNumber): T;
}
interface SjclECCBasic {
publicKey: SjclECCPublicKeyFactory<SjclECCPublicKey>;
secretKey: SjclECCSecretKeyFactory<SjclECCSecretKey>;
generateKeys(cn: string): SjclKeysGenerator<SjclECCPublicKey, SjclECCSecretKey>;
}
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<SjclElGamalPublicKey>;
secretKey: SjclECCSecretKeyFactory<SjclElGamalSecretKey>;
generateKeys: SjclKeysGenerator<SjclElGamalPublicKey, SjclElGamalSecretKey>;
}
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<SjclEcdsaPublicKey>;
secretKey: SjclECCSecretKeyFactory<SjclEcdsaSecretKey>;
generateKeys: SjclKeysGenerator<SjclEcdsaPublicKey, SjclEcdsaSecretKey>;
}
// ________________________________________________________________________
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<T> {
(value: T): BigNumber;
}
interface BigNumberBinaryOperator extends One<number>, One<string>, One<BigNumber> {
}
interface Two<T1, T2> {
(x: T1, N: T2): BigNumber;
}
interface Bind1<T> extends Two<number, T>, Two<string, T>, Two<BigNumber, T> {
}
interface BigNumberTrinaryOperator extends Bind1<number>, Bind1<string>, Bind1<BigNumber> {
}
}
}
declare module "sjcl" {
export = sjcl;
}