This repository has been archived by the owner on Jan 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
_crypto.js
132 lines (107 loc) · 5.91 KB
/
_crypto.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
// Copyright 2018 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under
// the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT> or
// the Modified BSD license <LICENSE-BSD or https://opensource.org/licenses/BSD-3-Clause>,
// at your option.
//
// This file may not be copied, modified, or distributed except according to those terms.
//
// Please review the Licences for the specific language governing permissions and limitations
// relating to use of the SAFE Network Software.
const ffi = require('ffi');
const ref = require("ref");
const Struct = require('ref-struct');
const base = require('./_base');
const t = base.types;
const h = base.helpers;
const SignPubKeyHandle = t.ObjectHandle;
const SignSecKeyHandle = t.ObjectHandle;
const EncryptPubKeyHandle = t.ObjectHandle;
const EncryptSecKeyHandle = t.ObjectHandle;
const EncryptKeyHandle = t.ObjectHandle;
const strToBuffer = (str) => {
let res = str;
if (!Buffer.isBuffer(str)) {
res = Buffer.from(str);
}
return [res, res.length]
}
const toKeyBytesBuffer = (type) => (app, key) => {
let keyArr = key;
if (!Buffer.isBuffer(key)) {
const b = Buffer.from(key);
if (b.length != type.size) throw Error(`Sign/Enc Keys _must be_ ${type.size} bytes long.`)
keyArr = type(b).ref().readPointer(0);
}
return [app, keyArr];
}
const appStrToBuffer = (appPtr, str, ...varArgs) => {
const buf = strToBuffer(str);
return [appPtr, ...buf, ...varArgs];
}
// Helper to create a copy of the received key bytes array as it might
// be overwritten after the callback finishes.
// TODO: it seems it's wrong to return the FFI struct type but just a buffer
const copyKeyBytesArray = (type) => (res) => type(ref.reinterpret(res[0], type.size));
module.exports = {
types: {
SignPubKeyHandle,
SignSecKeyHandle,
EncryptPubKeyHandle,
EncryptSecKeyHandle,
EncryptKeyHandle
},
functions: {
app_pub_sign_key: [t.Void, [t.AppPtr, 'pointer', 'pointer']],
app_pub_enc_key: [t.Void, [t.AppPtr, 'pointer', 'pointer']],
sign_generate_key_pair: [t.Void, [t.AppPtr, 'pointer', 'pointer']],
sign_pub_key_new: [t.Void, [t.AppPtr, ref.refType(t.SIGN_PUBLICKEYBYTES), 'pointer', 'pointer']],
sign_pub_key_get: [t.Void, [t.AppPtr, SignPubKeyHandle, 'pointer', 'pointer']],
sign_pub_key_free: [t.Void, [t.AppPtr, SignPubKeyHandle, 'pointer', 'pointer']],
sign_sec_key_new: [t.Void, [t.AppPtr, ref.refType(t.SIGN_SECRETKEYBYTES), 'pointer', 'pointer']],
sign_sec_key_get: [t.Void, [t.AppPtr, SignSecKeyHandle, 'pointer', 'pointer']],
sign_sec_key_free: [t.Void, [t.AppPtr, SignSecKeyHandle, 'pointer', 'pointer']],
enc_generate_key_pair: [t.Void, [t.AppPtr, 'pointer', 'pointer']],
enc_pub_key_new: [t.Void, [t.AppPtr, ref.refType(t.ASYM_PUBLICKEYBYTES), 'pointer', 'pointer']],
enc_pub_key_get: [t.Void, [t.AppPtr, EncryptPubKeyHandle, 'pointer', 'pointer']],
enc_pub_key_free: [t.Void, [t.AppPtr, EncryptPubKeyHandle, 'pointer', 'pointer']],
enc_secret_key_new: [t.Void, [t.AppPtr, ref.refType(t.ASYM_SECRETKEYBYTES), 'pointer', 'pointer']],
enc_secret_key_get: [t.Void, [t.AppPtr, EncryptSecKeyHandle, 'pointer', 'pointer']],
enc_secret_key_free: [t.Void, [t.AppPtr, EncryptSecKeyHandle, 'pointer', 'pointer']],
encrypt: [t.Void, [t.AppPtr, t.u8Pointer, t.usize, EncryptPubKeyHandle, EncryptSecKeyHandle, 'pointer', 'pointer']],
decrypt: [t.Void, [t.AppPtr, t.u8Pointer, t.usize, EncryptPubKeyHandle, EncryptSecKeyHandle, 'pointer', 'pointer']],
encrypt_sealed_box: [t.Void, [t.AppPtr, t.u8Pointer, t.usize, EncryptPubKeyHandle, 'pointer', 'pointer']],
decrypt_sealed_box: [t.Void, [t.AppPtr, t.u8Pointer, t.usize, EncryptPubKeyHandle, EncryptSecKeyHandle, 'pointer', 'pointer']],
sign: [t.Void, [t.AppPtr, t.u8Pointer, t.usize, SignSecKeyHandle, 'pointer', 'pointer']],
verify: [t.Void, [t.AppPtr, t.u8Pointer, t.usize, SignPubKeyHandle, 'pointer', 'pointer']],
sha3_hash: [t.Void, [t.u8Pointer, t.usize, 'pointer', 'pointer']],
generate_nonce: [t.Void, ['pointer', 'pointer']]
},
api: {
app_pub_sign_key: h.Promisified(null, SignPubKeyHandle),
app_pub_enc_key: h.Promisified(null, EncryptKeyHandle),
sign_generate_key_pair: h.Promisified(null, [SignPubKeyHandle, SignSecKeyHandle]),
sign_pub_key_new: h.Promisified(toKeyBytesBuffer(t.SIGN_PUBLICKEYBYTES), SignPubKeyHandle),
sign_pub_key_get: h.Promisified(null, ['pointer'], copyKeyBytesArray(t.SIGN_PUBLICKEYBYTES)),
sign_pub_key_free: h.Promisified(null, []),
sign_sec_key_new: h.Promisified(toKeyBytesBuffer(t.SIGN_SECRETKEYBYTES), SignSecKeyHandle),
sign_sec_key_get: h.Promisified(null, ['pointer'], copyKeyBytesArray(t.SIGN_SECRETKEYBYTES)),
sign_sec_key_free: h.Promisified(null, []),
enc_generate_key_pair: h.Promisified(null, [EncryptPubKeyHandle, EncryptSecKeyHandle]),
enc_pub_key_new: h.Promisified(toKeyBytesBuffer(t.ASYM_PUBLICKEYBYTES), EncryptPubKeyHandle),
enc_pub_key_get: h.Promisified(null, ['pointer'], copyKeyBytesArray(t.ASYM_PUBLICKEYBYTES)),
enc_pub_key_free: h.Promisified(null, []),
enc_secret_key_new: h.Promisified(toKeyBytesBuffer(t.ASYM_SECRETKEYBYTES), EncryptPubKeyHandle),
enc_secret_key_get: h.Promisified(null, ['pointer'], copyKeyBytesArray(t.ASYM_SECRETKEYBYTES)),
enc_secret_key_free: h.Promisified(null, []),
encrypt: h.Promisified(appStrToBuffer, [t.u8Pointer, t.usize], h.asBuffer),
decrypt: h.Promisified(appStrToBuffer, [t.u8Pointer, t.usize], h.asBuffer),
encrypt_sealed_box: h.Promisified(appStrToBuffer, [t.u8Pointer, t.usize], h.asBuffer),
decrypt_sealed_box: h.Promisified(appStrToBuffer, [t.u8Pointer, t.usize], h.asBuffer),
sign: h.Promisified(appStrToBuffer, [t.u8Pointer, t.usize], h.asBuffer),
verify: h.Promisified(appStrToBuffer, [t.u8Pointer, t.usize], h.asBuffer),
sha3_hash: h.Promisified(strToBuffer, [t.u8Pointer, t.usize], h.asBuffer),
generate_nonce: h.Promisified(null, t.ASYM_NONCEBYTES)
}
};