Skip to content

Commit

Permalink
Merge pull request video-dev#111 from kanongil/decrypt-fixes
Browse files Browse the repository at this point in the history
Improve decryption implementation
  • Loading branch information
mangui committed Dec 4, 2015
2 parents cd5132c + 39bd0c5 commit 17a1e14
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
3 changes: 2 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"globals" : {
"console" : true,
"MediaSource" : true,
"performance" : true
"performance" : true,
"crypto" : true
}
}
16 changes: 8 additions & 8 deletions src/crypt/aes128-decrypter.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,20 @@ class AES128Decrypter {

// pull out the words of the IV to ensure we don't modify the
// passed-in reference and easier access
init0 = initVector[0];
init1 = initVector[1];
init2 = initVector[2];
init3 = initVector[3];
init0 = ~~initVector[0];
init1 = ~~initVector[1];
init2 = ~~initVector[2];
init3 = ~~initVector[3];

// decrypt four word sequences, applying cipher-block chaining (CBC)
// to each decrypted block
for (wordIx = 0; wordIx < encrypted32.length; wordIx += 4) {
// convert big-endian (network order) words into little-endian
// (javascript order)
encrypted0 = this.ntoh(encrypted32[wordIx]);
encrypted1 = this.ntoh(encrypted32[wordIx + 1]);
encrypted2 = this.ntoh(encrypted32[wordIx + 2]);
encrypted3 = this.ntoh(encrypted32[wordIx + 3]);
encrypted0 = ~~this.ntoh(encrypted32[wordIx]);
encrypted1 = ~~this.ntoh(encrypted32[wordIx + 1]);
encrypted2 = ~~this.ntoh(encrypted32[wordIx + 2]);
encrypted3 = ~~this.ntoh(encrypted32[wordIx + 3]);

// decrypt the block
decipher.decrypt(encrypted0,
Expand Down
23 changes: 14 additions & 9 deletions src/crypt/decrypter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ class Decrypter {

constructor(hls) {
this.hls = hls;
this.disableWebCrypto = false;
try {
const browserCrypto = window ? window.crypto : crypto;
this.subtle = browserCrypto.subtle || browserCrypto.webkitSubtle;
this.disableWebCrypto = !this.subtle;
} catch (e) {
this.disableWebCrypto = true;
}
}

destroy() {
Expand All @@ -27,17 +33,16 @@ class Decrypter {
decryptByWebCrypto(data, key, iv, callback) {
logger.log('decrypting by WebCrypto API');

var localthis = this;
window.crypto.subtle.importKey('raw', key, { name : 'AES-CBC', length : 128 }, false, ['decrypt']).
then(function (importedKey) {
window.crypto.subtle.decrypt({ name : 'AES-CBC', iv : iv.buffer }, importedKey, data).
this.subtle.importKey('raw', key, { name : 'AES-CBC', length : 128 }, false, ['decrypt']).
then((importedKey) => {
this.subtle.decrypt({ name : 'AES-CBC', iv : iv.buffer }, importedKey, data).
then(callback).
catch (function (err) {
localthis.onWebCryptoError(err, data, key, iv, callback);
catch ((err) => {
this.onWebCryptoError(err, data, key, iv, callback);
});
}).
catch (function (err) {
localthis.onWebCryptoError(err, data, key, iv, callback);
catch ((err) => {
this.onWebCryptoError(err, data, key, iv, callback);
});
}

Expand Down
5 changes: 5 additions & 0 deletions src/demux/demuxer.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ class Demuxer {
this.w = null;
} else {
this.demuxer.destroy();
this.demuxer = null;
}
if (this.decrypter) {
this.decrypter.destroy();
this.decrypter = null;
}
}

Expand Down

0 comments on commit 17a1e14

Please sign in to comment.