-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathrsablind.js
114 lines (100 loc) · 2.9 KB
/
rsablind.js
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
const secureRandom = require('secure-random');
const BigInteger = require('jsbn').BigInteger;
const sha256 = require('js-sha256');
const NodeRSA = require('node-rsa');
function keyGeneration(params) {
const key = new NodeRSA(params || { b: 2048 });
return key;
}
function keyProperties(key) {
return {
E: new BigInteger(key.keyPair.e.toString()),
N: key.keyPair.n,
D: key.keyPair.d,
};
}
function messageToHash(message) {
const messageHash = sha256(message);
return messageHash;
}
function messageToHashInt(message) {
const messageHash = messageToHash(message);
const messageBig = new BigInteger(messageHash, 16);
return messageBig;
}
function blind({ message, key, N, E }) {
const messageHash = messageToHashInt(message);
N = key ? key.keyPair.n : new BigInteger(N.toString());
E = key
? new BigInteger(key.keyPair.e.toString())
: new BigInteger(E.toString());
const bigOne = new BigInteger('1');
let gcd;
let r;
do {
r = new BigInteger(secureRandom(64)).mod(N);
gcd = r.gcd(N);
} while (
!gcd.equals(bigOne) ||
r.compareTo(N) >= 0 ||
r.compareTo(bigOne) <= 0
);
const blinded = messageHash.multiply(r.modPow(E, N)).mod(N);
return {
blinded,
r,
};
}
function sign({ blinded, key }) {
const { N, D } = keyProperties(key);
blinded = new BigInteger(blinded.toString());
const signed = blinded.modPow(D, N);
return signed;
}
function unblind({ signed, key, r, N }) {
r = new BigInteger(r.toString());
N = key ? key.keyPair.n : new BigInteger(N.toString());
signed = new BigInteger(signed.toString());
const unblinded = signed.multiply(r.modInverse(N)).mod(N);
return unblinded;
}
function verify({ unblinded, key, message, E, N }) {
unblinded = new BigInteger(unblinded.toString());
const messageHash = messageToHashInt(message);
N = key ? key.keyPair.n : new BigInteger(N.toString());
E = key
? new BigInteger(key.keyPair.e.toString())
: new BigInteger(E.toString());
const originalMsg = unblinded.modPow(E, N);
const result = messageHash.equals(originalMsg);
return result;
}
function verify2({ unblinded, key, message }) {
unblinded = new BigInteger(unblinded.toString());
const messageHash = messageToHashInt(message);
const { D, N } = keyProperties(key);
const msgSig = messageHash.modPow(D, N);
const result = unblinded.equals(msgSig);
return result;
}
function verifyBlinding({ blinded, r, unblinded, key, E, N }) {
const messageHash = messageToHashInt(unblinded);
r = new BigInteger(r.toString());
N = key ? key.keyPair.n : new BigInteger(N.toString());
E = key
? new BigInteger(key.keyPair.e.toString())
: new BigInteger(E.toString());
const blindedHere = messageHash.multiply(r.modPow(E, N)).mod(N);
const result = blindedHere.equals(blinded);
return result;
}
module.exports = {
keyGeneration,
messageToHash,
blind,
sign,
unblind,
verify,
verify2,
verifyBlinding,
};