-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathindex.js
216 lines (176 loc) · 5.25 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
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
"use strict";
var sodium = require("chloride");
var pb = require("private-box");
var u = require("./util");
var isBuffer = Buffer.isBuffer;
//UTILS
function clone(obj) {
var _obj = {};
for (var k in obj) {
if (Object.hasOwnProperty.call(obj, k)) _obj[k] = obj[k];
}
return _obj;
}
var hmac = sodium.crypto_auth;
exports.hash = u.hash;
exports.getTag = u.getTag;
function isObject(o) {
return "object" === typeof o;
}
function isString(s) {
return "string" === typeof s;
}
var curves = {};
curves.ed25519 = require("./sodium");
function getCurve(keys) {
var curve = keys.curve;
if (!keys.curve && isString(keys.public)) keys = keys.public;
if (!curve && isString(keys)) curve = u.getTag(keys);
if (!curves[curve]) {
throw new Error(
"unknown curve:" + curve + " expected: " + Object.keys(curves)
);
}
return curve;
}
//this should return a key pair:
// {curve: curve, public: Buffer, private: Buffer}
exports.generate = function (curve, seed, feedFormat) {
curve = curve || "ed25519";
if (!curves[curve]) throw new Error("unknown curve:" + curve);
return u.keysToJSON(curves[curve].generate(seed), curve, feedFormat);
};
//import functions for loading/saving keys from storage
var storage = require("./storage")(exports.generate);
for (var key in storage) exports[key] = storage[key];
exports.loadOrCreate = function (filename, generateOpts, cb) {
if (!cb) {
cb = generateOpts;
generateOpts = null;
}
exports.load(filename, function (err, keys) {
if (!err) return cb(null, keys);
exports.create(filename, generateOpts, cb);
});
};
exports.loadOrCreateSync = function (filename, generateOpts) {
try {
return exports.loadSync(filename);
} catch (err) {
return exports.createSync(filename, generateOpts);
}
};
//takes a public key and a hash and returns a signature.
//(a signature must be a node buffer)
function sign(keys, hmac_key, msg) {
if (!msg) {
msg = hmac_key;
hmac_key = null;
}
if (isString(msg)) msg = Buffer.from(msg);
if (!isBuffer(msg)) throw new Error("msg should be buffer");
if (hmac_key) msg = hmac(msg, u.toBuffer(hmac_key));
var curve = getCurve(keys);
return (
curves[curve]
.sign(u.toBuffer(keys.private || keys), msg)
.toString("base64") +
".sig." +
curve
);
}
exports.sign = sign;
//takes a public key, signature, optional hmac_key and a hash
//and returns true if the signature was valid.
function verify(keys, sig, hmac_key, msg) {
if (!Buffer.isBuffer(sig) && isObject(sig))
throw new Error(
"signature should be base64 string, did you mean verifyObj(public, signed_obj)"
);
if (!msg) {
msg = hmac_key;
hmac_key = null;
}
let bufferMsg = isBuffer(msg) ? msg : Buffer.from(msg);
if (hmac_key) bufferMsg = hmac(bufferMsg, u.toBuffer(hmac_key));
return curves[getCurve(keys)].verify(
u.toBuffer(keys.public || keys),
u.toBuffer(sig),
bufferMsg
);
}
exports.verify = verify;
// OTHER CRYTPO FUNCTIONS
exports.signObj = function (keys, hmac_key, obj) {
if (!obj) (obj = hmac_key), (hmac_key = null);
var _obj = clone(obj);
var b = Buffer.from(JSON.stringify(_obj, null, 2));
if (hmac_key) b = hmac(b, u.toBuffer(hmac_key));
_obj.signature = sign(keys, b);
return _obj;
};
exports.verifyObj = function (keys, hmac_key, obj) {
if (!obj) (obj = hmac_key), (hmac_key = null);
obj = clone(obj);
var sig = obj.signature;
delete obj.signature;
var b = Buffer.from(JSON.stringify(obj, null, 2));
if (hmac_key) b = hmac(b, u.toBuffer(hmac_key));
return verify(keys, sig, b);
};
exports.box = function (msg, recipients) {
msg = Buffer.from(JSON.stringify(msg));
recipients = recipients.map(function (keys) {
return sodium.crypto_sign_ed25519_pk_to_curve25519(
u.toBuffer(keys.public || keys)
);
});
return pb.multibox(msg, recipients).toString("base64") + ".box";
};
function ssbSecretKeyToPrivateBoxSecret(keys) {
return sodium.crypto_sign_ed25519_sk_to_curve25519(
u.toBuffer(keys.private || keys)
);
}
exports.ssbSecretKeyToPrivateBoxSecret = ssbSecretKeyToPrivateBoxSecret;
exports.unboxKey = function (boxed, keys) {
boxed = u.toBuffer(boxed);
var sk = ssbSecretKeyToPrivateBoxSecret(keys);
return pb.multibox_open_key(boxed, sk);
};
exports.unboxBody = function (boxed, key) {
if (!key) return null;
boxed = u.toBuffer(boxed);
key = u.toBuffer(key);
var msg = pb.multibox_open_body(boxed, key);
try {
return JSON.parse("" + msg);
} catch (_) {
return;
}
};
exports.unbox = function (boxed, keys) {
boxed = u.toBuffer(boxed);
var sk =
keys._exchangeKey ||
sodium.crypto_sign_ed25519_sk_to_curve25519(
u.toBuffer(keys.private || keys)
);
if (keys.private) keys._exchangeKey = sk; //if keys is an object, cache the curve key.
try {
var msg = pb.multibox_open(boxed, sk);
return JSON.parse("" + msg);
} catch (_) {
return;
}
};
exports.secretBox = function secretBox(data, key) {
if (!data) return;
var ptxt = Buffer.from(JSON.stringify(data));
return sodium.crypto_secretbox_easy(ptxt, key.slice(0, 24), key);
};
exports.secretUnbox = function secretUnbox(ctxt, key) {
var ptxt = sodium.crypto_secretbox_open_easy(ctxt, key.slice(0, 24), key);
if (!ptxt) return;
return JSON.parse(ptxt.toString());
};