// Decrypt DH parameters and synch the local time with the server time
function decryptDHParams(context) {
    var newNonce = utility.string2Buffer(context.newNonce, 32);
    var serverNonce = utility.string2Buffer(context.resPQ.server_nonce, 16);
    if (logger.isDebugEnabled()) {
        logger.debug('newNonce = %s, serverNonce = %s', newNonce.toString('hex'), serverNonce.toString('hex'));
    }
    var hashNS = utility.createSHAHash([newNonce, serverNonce]);
    var hashSN = utility.createSHAHash([serverNonce, newNonce]);
    var hashNN = utility.createSHAHash([newNonce, newNonce]);
    if (logger.isDebugEnabled()) {
        logger.debug('hashNS = %s, hashSN = %s, hashNN = %s',
            hashNS.toString('hex'), hashSN.toString('hex'), hashNN.toString('hex'));
    }
    // Create the AES key
    context.aes = {
        key: Buffer.concat([hashNS, hashSN.slice(0, 12)]),
        iv: Buffer.concat([Buffer.concat([hashSN.slice(12), hashNN]), newNonce.slice(0, 4)]),
        toPrintable: tl.utility.toPrintable
    };

    if (logger.isDebugEnabled()) {
        logger.debug('aesKey = %s', context.aes.toPrintable());
    }
    // Decrypt the message
    var answerWithHash = security.cipher.aesDecrypt(
        context.serverDHParams.encrypted_answer,
        context.aes.key,
        context.aes.iv
    );
    context.decryptedDHParams = answerWithHash;
    return context;
}
Ejemplo n.º 2
0
AuthKey.encryptAuthKey = function (authKey, password) {
    var plainKey = Buffer.concat([authKey.id, authKey.value]);
    var passwordHash = utility.createSHAHash(new Buffer(password), 'sha512');
    var aesKey = passwordHash.slice(0, 32);
    var aesIv = passwordHash.slice(32, 64);
    return security.cipher.aesEncrypt(plainKey, aesKey, aesIv).slice(0);
};
Ejemplo n.º 3
0
AuthKey.decryptAuthKey = function (buffer, password) {
    var passwordHash = utility.createSHAHash(new Buffer(password), 'sha512');
    var aesKey = passwordHash.slice(0, 32);
    var aesIv = passwordHash.slice(32, 64);
    var decrypted = security.cipher.aesDecrypt(buffer, aesKey, aesIv);
    var id = decrypted.slice(0, 8);
    var value = decrypted.slice(8, 264);
    var hash = utility.createSHAHash(value);
    return (hash.slice(-8).toString('hex') == id.toString('hex')) ? new AuthKey(id, value) : null;
};
// Encrypt the pq_inner_data with RSA
function encryptPQInnerDataWithRSA(context) {
    // Create the data with hash to be encrypt
    var hash = utility.createSHAHash(context.pqInnerData);
    var dataWithHash = Buffer.concat([hash, context.pqInnerData]);
    if (logger.isDebugEnabled()) {
        logger.debug('Data to be encrypted contains: hash(%s), pqInnerData(%s), total length %s',
            hash.length, context.pqInnerData.length, dataWithHash.length);
    }
    // Encrypt data with RSA
    context.encryptedData = security.cipher.rsaEncrypt(dataWithHash, context.publicKey);
    return context;
}