PeculiarVentures / webcrypto-liner

webcrypto-liner is a polyfill that let's down-level User Agents (like IE/Edge) use libraries that depend on WebCrypto. (Keywords: Javascript, WebCrypto, Shim, Polyfill)
MIT License
149 stars 26 forks source link

arraybuffer bug #46

Closed borisreitman closed 6 years ago

borisreitman commented 6 years ago

Passing Uint8Array() to crypto.subtle.decrypt doesn't work correctly with webcrypto-liner, because the webcrypto-liner is using the underlying buffer of the data array, which could be bigger. But it does work with native crypto.subtle.decrypt. All the native functions accept Uint8Array in addition to raw ArrayBuffer, and work correctly.

This javascript code shows the problem:

    var ciphertext = base64js.toByteArray(ciphertext_base64_str);
    var buf = new ArrayBuffer(ciphertext.length  + 10);
    var byte_array = new Uint8Array(buf);
    byte_array.set(ciphertext, 10);
    var ciphertext2 = byte_array.subarray(10, byte_array.length);
    console.log(ciphertext);
    console.log(ciphertext2);

    if (base64js.fromByteArray(ciphertext) == base64js.fromByteArray(ciphertext2)){
      console.log("byte arrays are the same");
    } else {
      console.log("byte arrays are different");
    }

    symmetric_decrypt(channel_key, ciphertext, nonce, function(plaintext){
      console.log("A plaintext: ", plaintext);
    });

    symmetric_decrypt(channel_key, ciphertext2, nonce, function(plaintext){
      console.log("B plaintext: ", plaintext);
    });

  function symmetric_decrypt(encryption_key_obj, data, nonce, cb){
    crypto_subtle.decrypt(
      { 
        name: "AES-GCM",
        iv: nonce,
        tagLength: 128,
      },
      encryption_key_obj, 
      data
    )
    .then(function(decrypted){
      var decrypted_byte_array = new Uint8Array(decrypted);
      cb(decrypted_byte_array);
    })
    .catch(function(err){
      console.error(err);
    });
  }

There's an error decrypting ciphertext2. The issue doesn't happen if I don't include webcrypto-liner.shim.js

arraybuffer_test.html.txt

microshine commented 6 years ago

@borisreitman I fixed subarray bug in webcrypto-core and published new version of webcrypto-liner. Could you try new version?

borisreitman commented 6 years ago

Yes, the issue is fixed, thanks.