Skip to content

Commit

Permalink
refactor getTag
Browse files Browse the repository at this point in the history
  • Loading branch information
inwaar committed Oct 6, 2024
1 parent 271b0c2 commit d4acf8f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 51 deletions.
35 changes: 21 additions & 14 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,18 @@ class Client extends EventEmitter {
this._transformer = new PropertyTransformer();

/**
* Client options
*
* @type {CLIENT_OPTIONS}
* @type {EncryptionService}
* @private
*/
this._options = { ...CLIENT_OPTIONS, ...options };
this._encryptionService;

/**
* @type {EncryptionService}
* Client options
*
* @type {CLIENT_OPTIONS}
* @private
*/
this._encryptionService = new EncryptionService();
this._options = { ...CLIENT_OPTIONS, ...options };

/**
* @private
Expand Down Expand Up @@ -212,6 +212,9 @@ class Client extends EventEmitter {
async _initialize() {
this._dispose();

// The encryption service is stateful so requires re-initialization
this._encryptionService = new EncryptionService();

try {
await this._socketSend({ t: 'scan' });
await this._reconnect();
Expand Down Expand Up @@ -360,17 +363,19 @@ class Client extends EventEmitter {
* @private
*/
async _sendBindRequest() {
const encryptedMessage = this._encryptionService.encrypt({
mac: this._cid,
t: 'bind',
uid: 0,
});

await this._socketSend({
cid: 'app',
i: 1,
t: 'pack',
uid: 0,
pack: this._encryptionService.encrypt({
mac: this._cid,
t: 'bind',
uid: 0,
}),
tag: this._encryptionService.getTag(),
pack: encryptedMessage.payload,
tag: encryptedMessage.tag,
});
}

Expand Down Expand Up @@ -417,13 +422,15 @@ class Client extends EventEmitter {
*/
async _sendRequest(message) {
this._trace('OUT.MSG', message, this._encryptionService.getKey());

const encryptedMessage = this._encryptionService.encrypt(message);
await this._socketSend({
cid: 'app',
i: 0,
t: 'pack',
uid: 0,
pack: this._encryptionService.encrypt(message),
tag: this._encryptionService.getTag(),
pack: encryptedMessage.payload,
tag: encryptedMessage.tag,
});
}

Expand Down
58 changes: 21 additions & 37 deletions src/encryption-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

const crypto = require('crypto');

/**
* @typedef EncryptedMessage
* @property {string} payload
* @property {string|undefined} tag
* @private
*/

/**
* @private
*/
Expand Down Expand Up @@ -63,20 +70,10 @@ class EncryptionService {
* Encrypt UDP message
*
* @param {object} output Request object
* @returns {string}
* @returns {EncryptedMessage}
*/
encrypt(output) {
const result = this._activeCipher.encrypt(output);
return result;
}

/**
* Required for GCM
*
* @returns {string|undefined}
*/
getTag() {
return this._activeCipher.getTag();
return this._activeCipher.encrypt(output);
}
}

Expand Down Expand Up @@ -128,21 +125,15 @@ class EcbCipher {
* Encrypt UDP message
*
* @param {object} output Request object
* @returns {string}
* @returns {EncryptedMessage}
*/
encrypt(output) {
const cipher = crypto.createCipheriv('aes-128-ecb', this._key, '');
const str = cipher.update(JSON.stringify(output), 'utf8', 'base64');
return str + cipher.final('base64');
}

/**
* Not applicable for ECB
*
* @returns {string|undefined}
*/
getTag() {
return undefined;
const encrypted = str + cipher.final('base64');
return {
payload: encrypted,
};
}
}

Expand Down Expand Up @@ -209,10 +200,10 @@ class GcmCipher {
}

/**
* Encrypt UDP message. Sets _encTag to be received before sending with getTag() and added to message.
* Encrypt UDP message
*
* @param {object} output Request object
* @returns {string}
* @returns {EncryptedMessage}
*/
encrypt(output) {
const cipher = crypto.createCipheriv(
Expand All @@ -223,19 +214,12 @@ class GcmCipher {
cipher.setAAD(GCM_AEAD);
const str = cipher.update(JSON.stringify(output), 'utf8', 'base64');
const outstr = str + cipher.final('base64');
this._encTag = cipher.getAuthTag().toString('base64').toString('utf-8');
return outstr;
}
const tag = cipher.getAuthTag().toString('base64').toString('utf-8');

/**
* Receive and clear the last generated tag
*
* @returns {string|undefined}
*/
getTag() {
const tmpTag = this._encTag;
this._encTag = undefined;
return tmpTag;
return {
payload: outstr,
tag,
};
}
}

Expand Down

0 comments on commit d4acf8f

Please sign in to comment.