forked from EOSIO/eosjs-ecc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
key_utils.js
225 lines (194 loc) · 7.29 KB
/
key_utils.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
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
const base58 = require('bs58')
const assert = require('assert')
const randomBytes = require('randombytes');
const hash = require('./hash');
module.exports = {
random32ByteBuffer,
addEntropy,
cpuEntropy,
entropyCount: () => entropyCount,
checkDecode,
checkEncode
}
let entropyPos = 0, entropyCount = 0
const externalEntropyArray = randomBytes(101)
/**
Additional forms of entropy are used. A week random number generator can run out of entropy. This should ensure even the worst random number implementation will be reasonably safe.
@arg {number} [cpuEntropyBits = 0] generate entropy on the fly. This is
not required, entropy can be added in advanced via addEntropy or initialize().
@arg {boolean} [safe = true] false for testing, otherwise this will be
true to ensure initialize() was called.
@return a random buffer obtained from the secure random number generator. Additional entropy is used.
*/
function random32ByteBuffer({cpuEntropyBits = 0, safe = true} = {}) {
assert(typeof cpuEntropyBits, 'number', 'cpuEntropyBits')
assert(typeof safe, 'boolean', 'boolean')
if(safe) {
assert(entropyCount >= 128, 'Call initialize() to add entropy')
}
// if(entropyCount > 0) {
// console.log(`Additional private key entropy: ${entropyCount} events`)
// }
const hash_array = []
hash_array.push(randomBytes(32))
hash_array.push(Buffer.from(cpuEntropy(cpuEntropyBits)))
hash_array.push(externalEntropyArray)
hash_array.push(browserEntropy())
return hash.sha256(Buffer.concat(hash_array))
}
/**
Adds entropy. This may be called many times while the amount of data saved
is accumulatively reduced to 101 integers. Data is retained in RAM for the
life of this module.
@example React <code>
componentDidMount() {
this.refs.MyComponent.addEventListener("mousemove", this.onEntropyEvent, {capture: false, passive: true})
}
componentWillUnmount() {
this.refs.MyComponent.removeEventListener("mousemove", this.onEntropyEvent);
}
onEntropyEvent = (e) => {
if(e.type === 'mousemove')
key_utils.addEntropy(e.pageX, e.pageY, e.screenX, e.screenY)
else
console.log('onEntropyEvent Unknown', e.type, e)
}
</code>
*/
function addEntropy(...ints) {
assert.equal(externalEntropyArray.length, 101, 'externalEntropyArray')
entropyCount += ints.length
for(const i of ints) {
const pos = entropyPos++ % 101
const i2 = externalEntropyArray[pos] += i
if(i2 > 9007199254740991)
externalEntropyArray[pos] = 0
}
}
/**
This runs in just under 1 second and ensures a minimum of cpuEntropyBits
bits of entropy are gathered.
Based on more-entropy. @see https://github.com/keybase/more-entropy/blob/master/src/generator.iced
@arg {number} [cpuEntropyBits = 128]
@return {array} counts gathered by measuring variations in the CPU speed during floating point operations.
*/
function cpuEntropy(cpuEntropyBits = 128) {
let collected = []
let lastCount = null
let lowEntropySamples = 0
while(collected.length < cpuEntropyBits) {
const count = floatingPointCount()
if(lastCount != null) {
const delta = count - lastCount
if(Math.abs(delta) < 1) {
lowEntropySamples++
continue
}
// how many bits of entropy were in this sample
const bits = Math.floor(log2(Math.abs(delta)) + 1)
if(bits < 4) {
if(bits < 2) {
lowEntropySamples++
}
continue
}
collected.push(delta)
}
lastCount = count
}
if(lowEntropySamples > 10) {
const pct = Number(lowEntropySamples / cpuEntropyBits * 100).toFixed(2)
// Is this algorithm getting inefficient?
console.warn(`WARN: ${pct}% low CPU entropy re-sampled`);
}
return collected
}
/**
@private
Count while performing floating point operations during a fixed time
(7 ms for example). Using a fixed time makes this algorithm
predictable in runtime.
*/
function floatingPointCount() {
const workMinMs = 7
const d = Date.now()
let i = 0, x = 0
while (Date.now() < d + workMinMs + 1) {
x = Math.sin(Math.sqrt(Math.log(++i + x)))
}
return i
}
const log2 = x => Math.log(x) / Math.LN2
/**
@private
Attempt to gather and hash information from the browser's window, history, and supported mime types. For non-browser environments this simply includes secure random data. In any event, the information is re-hashed in a loop for 25 milliseconds seconds.
@return {Buffer} 32 bytes
*/
function browserEntropy() {
let entropyStr = Array(randomBytes(101)).join()
try {
entropyStr += (new Date()).toString() + " " + window.screen.height + " " + window.screen.width + " " +
window.screen.colorDepth + " " + " " + window.screen.availHeight + " " + window.screen.availWidth + " " +
window.screen.pixelDepth + navigator.language + " " + window.location + " " + window.history.length;
for (let i = 0, mimeType; i < navigator.mimeTypes.length; i++) {
mimeType = navigator.mimeTypes[i];
entropyStr += mimeType.description + " " + mimeType.type + " " + mimeType.suffixes + " ";
}
} catch(error) {
//nodejs:ReferenceError: window is not defined
entropyStr += hash.sha256((new Date()).toString())
}
const b = new Buffer(entropyStr);
entropyStr += b.toString('binary') + " " + (new Date()).toString();
let entropy = entropyStr;
const start_t = Date.now();
while (Date.now() - start_t < 25)
entropy = hash.sha256(entropy);
return entropy;
}
/**
@arg {Buffer} keyBuffer data
@arg {string} keyType = sha256x2, K1, etc
@return {string} checksum encoded base58 string
*/
function checkEncode(keyBuffer, keyType = null) {
assert(Buffer.isBuffer(keyBuffer), 'expecting keyBuffer<Buffer>')
if(keyType === 'sha256x2') { // legacy
const checksum = hash.sha256(hash.sha256(keyBuffer)).slice(0, 4)
return base58.encode(Buffer.concat([keyBuffer, checksum]))
} else {
const check = [keyBuffer]
if(keyType) {
check.push(Buffer.from(keyType))
}
const checksum = hash.ripemd160(Buffer.concat(check)).slice(0, 4)
return base58.encode(Buffer.concat([keyBuffer, checksum]))
}
}
/**
@arg {Buffer} keyString data
@arg {string} keyType = sha256x2, K1, etc
@return {string} checksum encoded base58 string
*/
function checkDecode(keyString, keyType = null) {
assert(keyString != null, 'private key expected')
const buffer = new Buffer(base58.decode(keyString))
const checksum = buffer.slice(-4)
const key = buffer.slice(0, -4)
let newCheck
if(keyType === 'sha256x2') { // legacy
newCheck = hash.sha256(hash.sha256(key)).slice(0, 4) // WIF (legacy)
} else {
const check = [key]
if(keyType) {
check.push(Buffer.from(keyType))
}
newCheck = hash.ripemd160(Buffer.concat(check)).slice(0, 4) //PVT
}
if (checksum.toString() !== newCheck.toString()) {
throw new Error('Invalid checksum, ' +
`${checksum.toString('hex')} != ${newCheck.toString('hex')}`
)
}
return key
}