Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto: use of more primordials broke webcrypto's aes decrypt #36083

Closed
panva opened this issue Nov 11, 2020 · 2 comments
Closed

crypto: use of more primordials broke webcrypto's aes decrypt #36083

panva opened this issue Nov 11, 2020 · 2 comments
Assignees

Comments

@panva
Copy link
Member

panva commented Nov 11, 2020

#36012 broke webcrypto's use of ArrayBufferView (and the like) in at least AES decrypt operations.

  • Version: v15.2.0
  • Subsystem: crypto.webcrypto

What steps will reproduce the bug?

This snippet works in v15.1.0, but fails in v15.2.0

// some.mjs

import { webcrypto as crypto } from 'crypto'

const secretKey = await crypto.subtle.generateKey(
    {
        name: "AES-GCM",
        length: 256,
    },
    false,
    ["encrypt", "decrypt"],
)

const iv = crypto.getRandomValues(new Uint8Array(12))
const aad = crypto.getRandomValues(new Uint8Array(32))

const encrypted = await crypto.subtle.encrypt(
  {
      name: "AES-GCM",
      iv,
      additionalData: aad,
      tagLength: 128
  },
  secretKey,
  crypto.getRandomValues(new Uint8Array(32))
)

await crypto.subtle.decrypt(
  {
      name: "AES-GCM",
      iv,
      additionalData: aad,
      tagLength: 128,
  },
  secretKey,
  new Uint8Array(encrypted),
)

How often does it reproduce? Is there a required condition?

What is the expected behavior?

All webcrypto arguments that are ArrayBuffer can also be Int8Array | Int16Array | Int32Array | Uint8Array | Uint16Array | Uint32Array | Uint8ClampedArray | Float32Array | Float64Array | DataView.

What do you see instead?

#36012 use of ArrayBufferPrototypeSlice in lib/internal/crypto/aes.js broke this particular snippet, possibly other too.

TypeError: Method ArrayBuffer.prototype.slice called on incompatible receiver [object Uint8Array]
    at Uint8Array.slice (<anonymous>)
    at node:internal/per_context/primordials:23:32
    at asyncAesGcmCipher (node:internal/crypto/aes:186:13)
    at Object.aesCipher (node:internal/crypto/aes:209:28)
    at cipherOrWrap (node:internal/crypto/webcrypto:625:10)
    at SubtleCrypto.decrypt (node:internal/crypto/webcrypto:640:10)
    at file:///Users/panva/repo/node/some.mjs:26:21
@panva
Copy link
Member Author

panva commented Nov 11, 2020

cc @jasnell @Trott @aduh95 (for #36012)

@aduh95
Copy link
Contributor

aduh95 commented Nov 11, 2020

Thanks a lot for the report and the repro snippet. I have added a fix and a test case in #36087.

codebytere pushed a commit that referenced this issue Nov 22, 2020
Refs: https://www.w3.org/TR/WebCryptoAPI/#subtlecrypto-interface
Fixes: #36083

PR-URL: #36087
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants