Skip to content

Commit

Permalink
Sodium (#1172)
Browse files Browse the repository at this point in the history
* Use Native libsodium when available

* add newline

* fix typo of exports

* add to webpack ignore

* Update Secretbox.js
  • Loading branch information
macdja38 authored and Gawdl3y committed Feb 6, 2017
1 parent 02c23a8 commit 4994474
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -45,6 +45,7 @@
"erlpack": "hammerandchisel/erlpack",
"node-opus": "^0.2.0",
"opusscript": "^0.0.2",
"sodium": "^2.0.1",
"uws": "^0.12.0"
},
"devDependencies": {
Expand All @@ -65,6 +66,7 @@
"opusscript": false,
"node-opus": false,
"tweet-nacl": false,
"sodium": false,
"src/sharding/Shard.js": false,
"src/sharding/ShardClientUtil.js": false,
"src/sharding/ShardingManager.js": false,
Expand All @@ -82,6 +84,7 @@
"src/client/voice/receiver/VoiceReadable.js": false,
"src/client/voice/receiver/VoiceReceiver.js": false,
"src/client/voice/util/SecretKey.js": false,
"src/client/voice/util/Secretbox.js": false,
"src/client/voice/ClientVoiceManager.js": false,
"src/client/voice/VoiceConnection.js": false,
"src/client/voice/VoiceUDPClient.js": false,
Expand Down
5 changes: 3 additions & 2 deletions src/client/voice/dispatcher/StreamDispatcher.js
@@ -1,7 +1,8 @@
const VolumeInterface = require('../util/VolumeInterface');
const NaCl = require('tweetnacl');
const VoiceBroadcast = require('../VoiceBroadcast');

const secretbox = require('../util/Secretbox');

const nonce = Buffer.alloc(24);
nonce.fill(0);

Expand Down Expand Up @@ -149,7 +150,7 @@ class StreamDispatcher extends VolumeInterface {
packetBuffer.writeUIntBE(this.player.voiceConnection.authentication.ssrc, 8, 4);

packetBuffer.copy(nonce, 0, 0, 12);
buffer = NaCl.secretbox(buffer, nonce, this.player.voiceConnection.authentication.secretKey.key);
buffer = secretbox.close(buffer, nonce, this.player.voiceConnection.authentication.secretKey.key);
for (let i = 0; i < buffer.length; i++) packetBuffer[i + 12] = buffer[i];

return packetBuffer;
Expand Down
4 changes: 2 additions & 2 deletions src/client/voice/receiver/VoiceReceiver.js
@@ -1,5 +1,5 @@
const EventEmitter = require('events').EventEmitter;
const NaCl = require('tweetnacl');
const secretbox = require('../util/Secretbox');
const Readable = require('./VoiceReadable');
const OpusEncoders = require('../opus/OpusEngineList');

Expand Down Expand Up @@ -123,7 +123,7 @@ class VoiceReceiver extends EventEmitter {

handlePacket(msg, user) {
msg.copy(nonce, 0, 0, 12);
let data = NaCl.secretbox.open(msg.slice(12), nonce, this.voiceConnection.authentication.secretKey.key);
let data = secretbox.open(msg.slice(12), nonce, this.voiceConnection.authentication.secretKey.key);
if (!data) {
/**
* Emitted whenever a voice packet experiences a problem.
Expand Down
13 changes: 13 additions & 0 deletions src/client/voice/util/Secretbox.js
@@ -0,0 +1,13 @@
try {
const sodium = require('sodium');
module.exports = {
open: sodium.api.crypto_secretbox_open,
close: sodium.api.crypto_secretbox,
};
} catch (err) {
const tweetnacl = require('tweetnacl');
module.exports = {
open: tweetnacl.secretbox.open,
close: tweetnacl.secretbox,
};
}

0 comments on commit 4994474

Please sign in to comment.