forked from cyph/ntru.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
post.js
executable file
·142 lines (114 loc) · 3.24 KB
/
post.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
;
function dataReturn (returnValue, result) {
if (returnValue === 0) {
return result;
}
else {
throw new Error('NTRU error: ' + returnValue);
}
}
function dataResult (buffer, bytes) {
return new Uint8Array(
new Uint8Array(Module.HEAPU8.buffer, buffer, bytes)
);
}
function dataFree (buffer) {
try {
Module._free(buffer);
}
catch (_) {}
}
var Module, publicKeyBytes, privateKeyBytes, cyphertextBytes, plaintextBytes;
var initiated = moduleReady.then(function () {
Module = finalModule;
Module._ntrujs_init();
publicKeyBytes = Module._ntrujs_public_key_bytes();
privateKeyBytes = Module._ntrujs_private_key_bytes();
cyphertextBytes = Module._ntrujs_encrypted_bytes();
plaintextBytes = Module._ntrujs_decrypted_bytes();
});
var ntru = {
publicKeyBytes: initiated.then(function () { return publicKeyBytes; }),
privateKeyBytes: initiated.then(function () { return privateKeyBytes; }),
cyphertextBytes: initiated.then(function () { return cyphertextBytes; }),
plaintextBytes: initiated.then(function () { return plaintextBytes; }),
keyPair: function () { return initiated.then(function () {
var publicKeyBuffer = Module._malloc(publicKeyBytes);
var privateKeyBuffer = Module._malloc(privateKeyBytes);
try {
var returnValue = Module._ntrujs_keypair(
publicKeyBuffer,
privateKeyBuffer
);
return dataReturn(returnValue, {
publicKey: dataResult(publicKeyBuffer, publicKeyBytes),
privateKey: dataResult(privateKeyBuffer, privateKeyBytes)
});
}
finally {
dataFree(publicKeyBuffer);
dataFree(privateKeyBuffer);
}
}); },
encrypt: function (message, publicKey) { return initiated.then(function () {
if (message.length > plaintextBytes) {
throw new Error('Plaintext length exceeds ntru.plaintextBytes.');
}
var messageBuffer = Module._malloc(message.length);
var publicKeyBuffer = Module._malloc(publicKeyBytes);
var encryptedBuffer = Module._malloc(cyphertextBytes);
Module.writeArrayToMemory(message, messageBuffer);
Module.writeArrayToMemory(publicKey, publicKeyBuffer);
try {
var returnValue = Module._ntrujs_encrypt(
messageBuffer,
message.length,
publicKeyBuffer,
encryptedBuffer
);
return dataReturn(
returnValue,
dataResult(encryptedBuffer, cyphertextBytes)
);
}
finally {
dataFree(messageBuffer);
dataFree(publicKeyBuffer);
dataFree(encryptedBuffer);
}
}); },
decrypt: function (encrypted, privateKey) { return initiated.then(function () {
var encryptedBuffer = Module._malloc(cyphertextBytes);
var privateKeyBuffer = Module._malloc(privateKeyBytes);
var decryptedBuffer = Module._malloc(plaintextBytes);
Module.writeArrayToMemory(encrypted, encryptedBuffer);
Module.writeArrayToMemory(privateKey, privateKeyBuffer);
try {
var returnValue = Module._ntrujs_decrypt(
encryptedBuffer,
privateKeyBuffer,
decryptedBuffer
);
if (returnValue >= 0) {
return dataResult(decryptedBuffer, returnValue);
}
else {
dataReturn(-returnValue);
}
}
finally {
dataFree(encryptedBuffer);
dataFree(privateKeyBuffer);
dataFree(decryptedBuffer);
}
}); }
};
return ntru;
}());
if (typeof module !== 'undefined' && module.exports) {
ntru.ntru = ntru;
module.exports = ntru;
}
else {
self.ntru = ntru;
}