Skip to content

Commit

Permalink
Use TypedArrays instead of ArrayBuffers with WebCrypto (bluesky-socia…
Browse files Browse the repository at this point in the history
…l#742)

* pass in ui8array not array buffer to webcrypto

* added test
  • Loading branch information
dholms authored Mar 31, 2023
1 parent 563c84c commit 3c3569d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/crypto/src/p256/keypair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class EcdsaKeypair implements Keypair {
const buf = await webcrypto.subtle.sign(
{ name: 'ECDSA', hash: { name: 'SHA-256' } },
this.keypair.privateKey,
msg.buffer,
new Uint8Array(msg),
)
return new Uint8Array(buf)
}
Expand Down
6 changes: 3 additions & 3 deletions packages/crypto/src/p256/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ export const verify = async (
return webcrypto.subtle.verify(
{ name: 'ECDSA', hash: { name: 'SHA-256' } },
importedKey,
sig,
data,
new Uint8Array(sig),
new Uint8Array(data),
)
}

Expand All @@ -56,7 +56,7 @@ export const importEcdsaPublicKey = async (
): Promise<CryptoKey> => {
return webcrypto.subtle.importKey(
'raw',
keyBytes,
new Uint8Array(keyBytes),
{ name: 'ECDSA', namedCurve: 'P-256' },
true,
['verify'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import EcdsaKeypair from '../src/p256/keypair'
import Secp256k1Keypair from '../src/secp256k1/keypair'
import * as p256 from '../src/p256/operations'
import * as secp from '../src/secp256k1/operations'
import { randomBytes } from '../src'

describe('exports and reimports keys', () => {
describe('keypairs', () => {
describe('secp256k1', () => {
let keypair: Secp256k1Keypair
let imported: Secp256k1Keypair

it('has the same DID', async () => {
it('has the same DID on import', async () => {
keypair = await Secp256k1Keypair.create({ exportable: true })
const exported = await keypair.export()
imported = await Secp256k1Keypair.import(exported, { exportable: true })
Expand All @@ -24,13 +25,23 @@ describe('exports and reimports keys', () => {

expect(validSig).toBeTruthy()
})

it('produces a valid signature on a typed array of a large arraybuffer', async () => {
const bytes = await randomBytes(8192)
const arrBuf = bytes.buffer
const sliceView = new Uint8Array(arrBuf, 1024, 1024)
expect(sliceView.buffer.byteLength).toBe(8192)
const sig = await imported.sign(sliceView)
const validSig = await secp.verifyDidSig(keypair.did(), sliceView, sig)
expect(validSig).toBeTruthy()
})
})

describe('P-256', () => {
let keypair: EcdsaKeypair
let imported: EcdsaKeypair

it('has the same DID', async () => {
it('has the same DID on import', async () => {
keypair = await EcdsaKeypair.create({ exportable: true })
const exported = await keypair.export()
imported = await EcdsaKeypair.import(exported, { exportable: true })
Expand All @@ -46,5 +57,15 @@ describe('exports and reimports keys', () => {

expect(validSig).toBeTruthy()
})

it('produces a valid signature on a typed array of a large arraybuffer', async () => {
const bytes = await randomBytes(8192)
const arrBuf = bytes.buffer
const sliceView = new Uint8Array(arrBuf, 1024, 1024)
expect(sliceView.buffer.byteLength).toBe(8192)
const sig = await imported.sign(sliceView)
const validSig = await p256.verifyDidSig(keypair.did(), sliceView, sig)
expect(validSig).toBeTruthy()
})
})
})

0 comments on commit 3c3569d

Please sign in to comment.