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

[SDK-3570] Use Web Crypto API for encrypting and decrypting #1299

Conversation

lawrence-forooghian
Copy link
Collaborator

@lawrence-forooghian lawrence-forooghian commented May 24, 2023

Note: This is based on top of #1311; please review that one first.

This PR changes the web crypto code to use the Web Crypto API instead of the CryptoJS library. See commit messages for more details.

Resolves #1292, resolves #1296, resolves #1300.

@lawrence-forooghian lawrence-forooghian changed the base branch from main to convert-crypto-to-TypeScript May 24, 2023 19:57
@lawrence-forooghian lawrence-forooghian force-pushed the 1292-use-web-crypto-for-encrypting-and-decrypting branch from 745e6a5 to 4fb7f0e Compare May 24, 2023 20:04
@github-actions github-actions bot temporarily deployed to staging/pull/1299/features May 24, 2023 20:04 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1299/bundle-report May 24, 2023 20:06 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1299/typedoc May 24, 2023 20:06 Inactive
@lawrence-forooghian lawrence-forooghian force-pushed the 1292-use-web-crypto-for-encrypting-and-decrypting branch from 4fb7f0e to a7a8221 Compare May 24, 2023 20:21
@github-actions github-actions bot temporarily deployed to staging/pull/1299/features May 24, 2023 20:21 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1299/bundle-report May 24, 2023 20:22 Inactive
@lawrence-forooghian lawrence-forooghian force-pushed the 1292-use-web-crypto-for-encrypting-and-decrypting branch from a7a8221 to 2357cc0 Compare May 24, 2023 20:23
@github-actions github-actions bot temporarily deployed to staging/pull/1299/features May 24, 2023 20:23 Inactive
@lawrence-forooghian lawrence-forooghian force-pushed the 1292-use-web-crypto-for-encrypting-and-decrypting branch from 2357cc0 to 2332a84 Compare May 24, 2023 20:24
@github-actions github-actions bot temporarily deployed to staging/pull/1299/features May 24, 2023 20:24 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1299/bundle-report May 24, 2023 20:25 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1299/typedoc May 24, 2023 20:26 Inactive
@lawrence-forooghian lawrence-forooghian force-pushed the convert-crypto-to-TypeScript branch 4 times, most recently from 61f442a to a56148c Compare May 25, 2023 18:10
@lawrence-forooghian lawrence-forooghian force-pushed the 1292-use-web-crypto-for-encrypting-and-decrypting branch from 2332a84 to 979ded8 Compare May 29, 2023 13:49
@github-actions github-actions bot temporarily deployed to staging/pull/1299/features May 29, 2023 13:49 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1299/typedoc May 29, 2023 13:51 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1299/bundle-report May 29, 2023 13:51 Inactive
@lawrence-forooghian lawrence-forooghian force-pushed the 1292-use-web-crypto-for-encrypting-and-decrypting branch from 979ded8 to 29bb2b8 Compare May 29, 2023 16:45
@github-actions github-actions bot temporarily deployed to staging/pull/1299/features May 29, 2023 16:45 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1299/bundle-report May 29, 2023 16:47 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1299/typedoc May 29, 2023 16:47 Inactive
@lawrence-forooghian lawrence-forooghian changed the title 1292 use web crypto for encrypting and decrypting Use Web Crypto API for encrypting and decrypting May 30, 2023
@lawrence-forooghian lawrence-forooghian changed the base branch from convert-crypto-to-TypeScript to 1293-make-ICipher.decrypt-async May 30, 2023 12:06
@lawrence-forooghian lawrence-forooghian force-pushed the 1292-use-web-crypto-for-encrypting-and-decrypting branch from 29bb2b8 to 854578d Compare May 30, 2023 12:11
@github-actions github-actions bot temporarily deployed to staging/pull/1299/features May 30, 2023 12:11 Inactive
Base automatically changed from 1293-make-ICipher.decrypt-async to integration/v2 June 5, 2023 21:14
Instead, make it so that we can compare any two Bufferlike objects. When
I was changing some test code (hence, not TypeScript) that made use of
bufferCompare, I found myself having to think more than I would have
liked about which type of objects I could pass it.
Gives richer type information to compiler (i.e. whether the object was
created with the ArrayBuffer constructor instead of whether it just
conforms to the ArrayBuffer interface), which I believe a type guard
can't express.

I’m doing this because the the compiler seemed to consider a TypedArray
to satisfy the ArrayBuffer interface:

> const foo: TypedArray = new Uint8Array();
> const bar: ArrayBuffer = foo; // This compiles, weird!

The effect of this was that, given an object of TypeScript type
`ArrayBuffer | TypedArray`, then, after performing an `is ArrayBuffer`
type guard that returned false, the compiler believed the object to be
of type `never` (when in fact it could still be a TypedArray).
This is a more broad definition that means that our web Bufferlike type
agrees with the web standards’ list of binary data types [1].

[1] https://www.w3.org/TR/WebIDL-1/#common-BufferSource
Part of #1300 (removing CryptoJS functionality from web’s BufferUtils).
And make it return just a boolean instead of a number — we aren’t making
use of the ordering information it provides, which I don’t really
understand and don’t want to spend time trying to reproduce when I
reimplement the web version of this method.
Now, web’s BufferUtils only uses the CryptoJS library to provide the
functionality needed by our web Crypto class for:

- checking if something is a WordArray
- converting something to a WordArray
- converting a WordArray to an ArrayBuffer

We will remove this remaining CryptoJS code after implementing #1299
(removing the use of CryptoJS in our web Crypto class).

Resolves #1300.
It’s only a WordArray on web, not in Node.
This test is executed on all platforms, but WordArray is only used on
web.
I’m going to want to inject a BufferUtils instance in an upcoming
commit.
Preparation for #1292 (using Web Crypto API for encrypting and
decrypting).
Preparation for #1292 (using Web Crypto API for encrypting and
decrypting).
The web crypto code now no longer emits any WordArray objects — it only
uses them internally.

Preparation for #1292 (using Web Crypto API for encrypting and
decrypting).
All of the rest of the crypto code is built around a non-configurable
block length, so this property is just misleading.
This removes our use of the CryptoJS library for performing encryption
and decryption operations, instead using the browser’s built-in crypto
APIs. We’re doing this as part of our work to remove the CryptoJS
library (#1239) to reduce the size of our SDK.

Resolves #1292.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants