Page MenuHomePhabricator

[native][AES] Implement generateKey function

Authored by bartek on Wed, Mar 8, 9:56 AM.
Referenced Files
Unknown Object (File)
Sun, Mar 26, 5:43 AM
Unknown Object (File)
Thu, Mar 16, 2:25 AM
Unknown Object (File)
Sat, Mar 11, 7:49 AM
Unknown Object (File)
Sat, Mar 11, 7:49 AM
Unknown Object (File)
Fri, Mar 10, 10:22 PM



This diff implements a function, that generates AES-256 key using platforms' native APIs.

iOS docs:
Android docs:

Depends on D7002

Test Plan

Ensured the function returns random numbers on both platforms:

import { generateKey } from './utils/aes-crypto-module.js';

// should display sth like: [82, 226, ..., 215, 19, 13] // 32 values

Diff Detail

rCOMM Comm
Lint Not Applicable
Tests Not Applicable

Event Timeline

bartek held this revision as a draft.
bartek published this revision for review.Wed, Mar 8, 10:31 AM
bartek added inline comments.
29 ↗(On Diff #23537)

I pass the result by argument because returning TypedArray doesn't work correctly. TypedArrays support is still experimental/undocumented feature.

The same is done in latest SDK-48 expo-crypto module:

atul requested changes to this revision.Thu, Mar 9, 12:02 PM

Left a note about the error-handling on the Swift side, definitely let me know if there's something I'm missing. I think we should just be able to match what we're doing on the Android/Kotlin side?

iOS docs:

(Think you might've meant to link to

16–18 ↗(On Diff #23537)

Before we even generate key via SymmetricKey, should we check that destination.byteLength is 32 and throw InvalidDestinationSizeException() if not?

It seems like destination is the input that we want to validate, so we should just do that directly? It's not really so much that SymmetricKey is giving us incorrect size key since it's (I'm assuming) always going to give us exactly the size we're asking for?

Feel free to re-request review if there's something I'm missing here.

12 ↗(On Diff #23537)

Kind of confusing naming that we're passing key to a function named generateKey.

Maybe buffer or keyBuffer or keyDestination or something along those lines?

This revision now requires changes to proceed.Thu, Mar 9, 12:02 PM
bartek marked an inline comment as not done.Fri, Mar 10, 12:51 AM
bartek added inline comments.
16–18 ↗(On Diff #23537)

The only reason the guard is below SymmetricKey is that I wanted to use its bitCount property to get the desired destination length, but you're right that it can be done this way:

guard destination.byteLength == KEY_SIZE else {
  throw InvalidKeyLengthException()
let key = SymmetricKey(size: .bits256)

I'll refactor this as it is more clear solution

12 ↗(On Diff #23537)

Good idea, keyBuffer sounds better

Address code review feedback

Sweet, thanks for addressing those comments

This revision is now accepted and ready to land.Mon, Mar 13, 4:53 PM