This repository has been archived by the owner on Apr 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
160 lines (152 loc) · 4.46 KB
/
index.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
'use strict';
const binding = require('./binding.node');
const GUARD_PAGE = Buffer.alloc(4096);
const NO_AAD = GUARD_PAGE.slice(0, 0);
const NO_TAG = GUARD_PAGE.slice(0, 0);
if (!Number.isInteger(binding.CIPHER_BLOCK_MAX)) {
throw new Error('!Number.isInteger(binding.CIPHER_BLOCK_MAX)');
}
for (var key in binding) {
if (/^[A-Z_]+$/.test(key)) {
module.exports[key] = binding[key];
} else if (!/^(cipher|hash|hmac)$/.test(key)) {
throw new Error('non-whitelisted binding property: ' + key);
}
}
module.exports.cipher = function(...args) {
if (args.length === 19 || args.length === 20) return binding.cipher(...args);
if (args.length >= 5 && args.length <= 8) {
var algorithm = args[0];
var encrypt = args[1];
var key = args[2];
var iv = args[3];
var source = args[4];
if (!Buffer.isBuffer(key)) throw new Error(binding.E_KEY);
if (!Buffer.isBuffer(iv)) throw new Error(binding.E_IV);
if (!Buffer.isBuffer(source)) throw new Error(binding.E_SOURCE);
var target = Buffer.alloc(source.length + binding.CIPHER_BLOCK_MAX);
var aad = args.length >= 7 ? args[5] : NO_AAD;
var tag = args.length >= 7 ? args[6] : NO_TAG;
if (!Buffer.isBuffer(aad)) throw new Error(binding.E_AAD);
if (!Buffer.isBuffer(tag)) throw new Error(binding.E_TAG);
var parameters = [
algorithm,
encrypt,
key,
0,
key.length,
iv,
0,
iv.length,
source,
0,
source.length,
target,
0,
aad,
0,
aad.length,
tag,
0,
tag.length
];
if (args.length === 5 || args.length === 7) {
return target.slice(0, binding.cipher(...parameters));
} else if (args.length === 6 || args.length === 8) {
var end = args[args.length - 1];
if (typeof end !== 'function') throw new Error(binding.E_CALLBACK);
return binding.cipher(...parameters,
function(error, targetSize) {
if (error) return end(error);
end(undefined, target.slice(0, targetSize));
}
);
} else {
// Unreachable. This is defense in depth.
throw new Error(binding.E_ARGUMENTS);
}
} else if (args.length === 13) {
return binding.cipher(...args, NO_AAD, 0, 0, NO_TAG, 0, 0);
} else if (args.length === 14) {
var end = args.pop(); // Remove callback from args.
if (typeof end !== 'function') throw new Error(binding.E_CALLBACK);
return binding.cipher(...args, NO_AAD, 0, 0, NO_TAG, 0, 0, end);
} else {
throw new Error(binding.E_ARGUMENTS);
}
};
module.exports.hash = function(...args) {
if (args.length === 6 || args.length === 7) return binding.hash(...args);
if (args.length < 2 || args.length > 3) throw new Error(binding.E_ARGUMENTS);
var algorithm = args[0];
var source = args[1];
if (!Buffer.isBuffer(source)) throw new Error(binding.E_SOURCE);
var target = Buffer.alloc(64); // Support up to 512 bits.
if (args.length === 2) {
return target.slice(0, binding.hash(
algorithm,
source,
0,
source.length,
target,
0
));
} else {
var end = args[args.length - 1];
if (typeof end !== 'function') throw new Error(binding.E_CALLBACK);
return binding.hash(
algorithm,
source,
0,
source.length,
target,
0,
function(error, targetSize) {
if (error) return end(error);
end(undefined, target.slice(0, targetSize));
}
);
}
};
module.exports.hmac = function(...args) {
if (args.length === 9 || args.length === 10) return binding.hmac(...args);
if (args.length < 3 || args.length > 4) throw new Error(binding.E_ARGUMENTS);
var algorithm = args[0];
var key = args[1];
var source = args[2];
if (!Buffer.isBuffer(key)) throw new Error(binding.E_KEY);
if (!Buffer.isBuffer(source)) throw new Error(binding.E_SOURCE);
var target = Buffer.alloc(64); // Support up to 512 bits.
if (args.length === 3) {
return target.slice(0, binding.hmac(
algorithm,
key,
0,
key.length,
source,
0,
source.length,
target,
0
));
} else {
var end = args[args.length - 1];
if (typeof end !== 'function') throw new Error(binding.E_CALLBACK);
return binding.hmac(
algorithm,
key,
0,
key.length,
source,
0,
source.length,
target,
0,
function(error, targetSize) {
if (error) return end(error);
end(undefined, target.slice(0, targetSize));
}
);
}
};
// S.D.G.